efi-pstore.c 9.27 KB
Newer Older
1
2
3
#include <linux/efi.h>
#include <linux/module.h>
#include <linux/pstore.h>
4
#include <linux/slab.h>
5
#include <linux/ucs2_string.h>
6

Kees Cook's avatar
Kees Cook committed
7
#define DUMP_NAME_LEN 66
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

static bool efivars_pstore_disable =
	IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);

module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);

#define PSTORE_EFI_ATTRIBUTES \
	(EFI_VARIABLE_NON_VOLATILE | \
	 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
	 EFI_VARIABLE_RUNTIME_ACCESS)

static int efi_pstore_open(struct pstore_info *psi)
{
	psi->data = NULL;
	return 0;
}

static int efi_pstore_close(struct pstore_info *psi)
{
	psi->data = NULL;
	return 0;
}

31
static inline u64 generic_id(u64 timestamp, unsigned int part, int count)
32
{
33
	return (timestamp * 100 + part) * 1000 + count;
34
35
}

36
37
static int efi_pstore_read_func(struct efivar_entry *entry,
				struct pstore_record *record)
38
39
{
	efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
40
	char name[DUMP_NAME_LEN], data_type;
41
42
43
	int i;
	int cnt;
	unsigned int part;
44
45
	unsigned long size;
	u64 time;
46
47
48
49
50
51
52

	if (efi_guidcmp(entry->var.VendorGuid, vendor))
		return 0;

	for (i = 0; i < DUMP_NAME_LEN; i++)
		name[i] = entry->var.VariableName[i];

53
	if (sscanf(name, "dump-type%u-%u-%d-%llu-%c",
54
55
		   &record->type, &part, &cnt, &time, &data_type) == 5) {
		record->id = generic_id(time, part, cnt);
56
		record->part = part;
57
58
59
		record->count = cnt;
		record->time.tv_sec = time;
		record->time.tv_nsec = 0;
60
		if (data_type == 'C')
61
			record->compressed = true;
62
		else
63
64
			record->compressed = false;
		record->ecc_notice_size = 0;
65
	} else if (sscanf(name, "dump-type%u-%u-%d-%llu",
66
67
		   &record->type, &part, &cnt, &time) == 4) {
		record->id = generic_id(time, part, cnt);
68
		record->part = part;
69
70
71
72
73
		record->count = cnt;
		record->time.tv_sec = time;
		record->time.tv_nsec = 0;
		record->compressed = false;
		record->ecc_notice_size = 0;
74
	} else if (sscanf(name, "dump-type%u-%u-%llu",
75
			  &record->type, &part, &time) == 3) {
76
77
78
79
80
		/*
		 * Check if an old format,
		 * which doesn't support holding
		 * multiple logs, remains.
		 */
81
		record->id = generic_id(time, part, 0);
82
		record->part = part;
83
84
85
86
87
		record->count = 0;
		record->time.tv_sec = time;
		record->time.tv_nsec = 0;
		record->compressed = false;
		record->ecc_notice_size = 0;
88
89
90
	} else
		return 0;

91
92
93
94
	entry->var.DataSize = 1024;
	__efivar_entry_get(entry, &entry->var.Attributes,
			   &entry->var.DataSize, entry->var.Data);
	size = entry->var.DataSize;
95
	memcpy(record->buf, entry->var.Data,
96
	       (size_t)min_t(unsigned long, EFIVARS_DATA_SIZE_MAX, size));
97

98
99
100
	return size;
}

101
102
/**
 * efi_pstore_scan_sysfs_enter
103
 * @pos: scanning entry
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
 * @next: next entry
 * @head: list head
 */
static void efi_pstore_scan_sysfs_enter(struct efivar_entry *pos,
					struct efivar_entry *next,
					struct list_head *head)
{
	pos->scanning = true;
	if (&next->list != head)
		next->scanning = true;
}

/**
 * __efi_pstore_scan_sysfs_exit
 * @entry: deleting entry
 * @turn_off_scanning: Check if a scanning flag should be turned off
 */
121
static inline int __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry,
122
123
124
125
126
127
						bool turn_off_scanning)
{
	if (entry->deleting) {
		list_del(&entry->list);
		efivar_entry_iter_end();
		efivar_unregister(entry);
128
129
		if (efivar_entry_iter_begin())
			return -EINTR;
130
131
	} else if (turn_off_scanning)
		entry->scanning = false;
132
133

	return 0;
134
135
136
137
138
139
140
141
142
}

/**
 * efi_pstore_scan_sysfs_exit
 * @pos: scanning entry
 * @next: next entry
 * @head: list head
 * @stop: a flag checking if scanning will stop
 */
143
static int efi_pstore_scan_sysfs_exit(struct efivar_entry *pos,
144
145
146
				       struct efivar_entry *next,
				       struct list_head *head, bool stop)
{
147
148
149
150
151
	int ret = __efi_pstore_scan_sysfs_exit(pos, true);

	if (ret)
		return ret;

152
	if (stop)
153
154
		ret = __efi_pstore_scan_sysfs_exit(next, &next->list != head);
	return ret;
155
156
157
158
159
}

/**
 * efi_pstore_sysfs_entry_iter
 *
160
 * @record: pstore record to pass to callback
161
162
163
164
165
 *
 * You MUST call efivar_enter_iter_begin() before this function, and
 * efivar_entry_iter_end() afterwards.
 *
 */
166
static int efi_pstore_sysfs_entry_iter(struct pstore_record *record)
167
{
168
	struct efivar_entry **pos = (struct efivar_entry **)&record->psi->data;
169
170
171
	struct efivar_entry *entry, *n;
	struct list_head *head = &efivar_sysfs_list;
	int size = 0;
172
	int ret;
173
174
175
176
177

	if (!*pos) {
		list_for_each_entry_safe(entry, n, head, list) {
			efi_pstore_scan_sysfs_enter(entry, n, head);

178
			size = efi_pstore_read_func(entry, record);
179
180
181
182
			ret = efi_pstore_scan_sysfs_exit(entry, n, head,
							 size < 0);
			if (ret)
				return ret;
183
184
185
186
187
188
189
190
191
192
			if (size)
				break;
		}
		*pos = n;
		return size;
	}

	list_for_each_entry_safe_from((*pos), n, head, list) {
		efi_pstore_scan_sysfs_enter((*pos), n, head);

193
		size = efi_pstore_read_func((*pos), record);
194
195
196
		ret = efi_pstore_scan_sysfs_exit((*pos), n, head, size < 0);
		if (ret)
			return ret;
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
		if (size)
			break;
	}
	*pos = n;
	return size;
}

/**
 * efi_pstore_read
 *
 * This function returns a size of NVRAM entry logged via efi_pstore_write().
 * The meaning and behavior of efi_pstore/pstore are as below.
 *
 * size > 0: Got data of an entry logged via efi_pstore_write() successfully,
 *           and pstore filesystem will continue reading subsequent entries.
 * size == 0: Entry was not logged via efi_pstore_write(),
 *            and efi_pstore driver will continue reading subsequent entries.
 * size < 0: Failed to get data of entry logging via efi_pstore_write(),
 *           and pstore will stop reading entry.
 */
217
static ssize_t efi_pstore_read(struct pstore_record *record)
218
{
219
	ssize_t size;
220

221
222
	record->buf = kzalloc(EFIVARS_DATA_SIZE_MAX, GFP_KERNEL);
	if (!record->buf)
223
224
		return -ENOMEM;

225
	if (efivar_entry_iter_begin()) {
226
227
		size = -EINTR;
		goto out;
228
	}
229
	size = efi_pstore_sysfs_entry_iter(record);
230
	efivar_entry_iter_end();
231
232
233
234
235
236

out:
	if (size <= 0) {
		kfree(record->buf);
		record->buf = NULL;
	}
237
	return size;
238
239
}

240
static int efi_pstore_write(struct pstore_record *record)
241
242
243
244
245
246
{
	char name[DUMP_NAME_LEN];
	efi_char16_t efi_name[DUMP_NAME_LEN];
	efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
	int i, ret = 0;

247
248
249
	record->id = generic_id(record->time.tv_sec, record->part,
				record->count);

Kees Cook's avatar
Kees Cook committed
250
251
252
	/* Since we copy the entire length of name, make sure it is wiped. */
	memset(name, 0, sizeof(name));

253
	snprintf(name, sizeof(name), "dump-type%u-%u-%d-%lld-%c",
254
		 record->type, record->part, record->count,
255
256
		 (long long)record->time.tv_sec,
		 record->compressed ? 'C' : 'D');
257
258
259
260

	for (i = 0; i < DUMP_NAME_LEN; i++)
		efi_name[i] = name[i];

261
	ret = efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
262
263
			      !pstore_cannot_block_path(record->reason),
			      record->size, record->psi->buf);
264

265
	if (record->reason == KMSG_DUMP_OOPS)
266
267
268
269
270
271
272
273
274
275
		efivar_run_worker();

	return ret;
};

/*
 * Clean up an entry with the same name
 */
static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
{
Kees Cook's avatar
Kees Cook committed
276
	efi_char16_t *efi_name = data;
277
	efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
Kees Cook's avatar
Kees Cook committed
278
	unsigned long ucs2_len = ucs2_strlen(efi_name);
279
280
281
282

	if (efi_guidcmp(entry->var.VendorGuid, vendor))
		return 0;

Kees Cook's avatar
Kees Cook committed
283
284
	if (ucs2_strncmp(entry->var.VariableName, efi_name, (size_t)ucs2_len))
		return 0;
285

286
287
288
289
290
291
292
293
294
	if (entry->scanning) {
		/*
		 * Skip deletion because this entry will be deleted
		 * after scanning is completed.
		 */
		entry->deleting = true;
	} else
		list_del(&entry->list);

295
296
	/* found */
	__efivar_entry_delete(entry);
297

298
299
300
	return 1;
}

Kees Cook's avatar
Kees Cook committed
301
static int efi_pstore_erase_name(const char *name)
302
{
303
	struct efivar_entry *entry = NULL;
304
305
306
	efi_char16_t efi_name[DUMP_NAME_LEN];
	int found, i;

Kees Cook's avatar
Kees Cook committed
307
	for (i = 0; i < DUMP_NAME_LEN; i++) {
308
		efi_name[i] = name[i];
Kees Cook's avatar
Kees Cook committed
309
310
311
		if (name[i] == '\0')
			break;
	}
312

313
314
	if (efivar_entry_iter_begin())
		return -EINTR;
315

Kees Cook's avatar
Kees Cook committed
316
317
318
319
320
	found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list,
				    efi_name, &entry);
	efivar_entry_iter_end();

	if (found && !entry->scanning)
321
322
		efivar_unregister(entry);

Kees Cook's avatar
Kees Cook committed
323
324
325
326
327
328
329
330
	return found ? 0 : -ENOENT;
}

static int efi_pstore_erase(struct pstore_record *record)
{
	char name[DUMP_NAME_LEN];
	int ret;

331
	snprintf(name, sizeof(name), "dump-type%u-%u-%d-%lld",
Kees Cook's avatar
Kees Cook committed
332
		 record->type, record->part, record->count,
333
		 (long long)record->time.tv_sec);
Kees Cook's avatar
Kees Cook committed
334
335
336
337
	ret = efi_pstore_erase_name(name);
	if (ret != -ENOENT)
		return ret;

338
339
	snprintf(name, sizeof(name), "dump-type%u-%u-%lld",
		record->type, record->part, (long long)record->time.tv_sec);
Kees Cook's avatar
Kees Cook committed
340
341
342
	ret = efi_pstore_erase_name(name);

	return ret;
343
344
345
346
347
}

static struct pstore_info efi_pstore_info = {
	.owner		= THIS_MODULE,
	.name		= "efi",
348
	.flags		= PSTORE_FLAGS_DMESG,
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
	.open		= efi_pstore_open,
	.close		= efi_pstore_close,
	.read		= efi_pstore_read,
	.write		= efi_pstore_write,
	.erase		= efi_pstore_erase,
};

static __init int efivars_pstore_init(void)
{
	if (!efi_enabled(EFI_RUNTIME_SERVICES))
		return 0;

	if (!efivars_kobject())
		return 0;

	if (efivars_pstore_disable)
		return 0;

	efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
	if (!efi_pstore_info.buf)
		return -ENOMEM;

	efi_pstore_info.bufsize = 1024;
	spin_lock_init(&efi_pstore_info.buf_lock);

374
375
376
377
378
	if (pstore_register(&efi_pstore_info)) {
		kfree(efi_pstore_info.buf);
		efi_pstore_info.buf = NULL;
		efi_pstore_info.bufsize = 0;
	}
379
380
381
382
383
384

	return 0;
}

static __exit void efivars_pstore_exit(void)
{
385
386
387
388
389
390
391
	if (!efi_pstore_info.bufsize)
		return;

	pstore_unregister(&efi_pstore_info);
	kfree(efi_pstore_info.buf);
	efi_pstore_info.buf = NULL;
	efi_pstore_info.bufsize = 0;
392
393
394
395
396
397
398
}

module_init(efivars_pstore_init);
module_exit(efivars_pstore_exit);

MODULE_DESCRIPTION("EFI variable backend for pstore");
MODULE_LICENSE("GPL");
399
MODULE_ALIAS("platform:efivars");