1 // SPDX-License-Identifier: GPL-2.0+
4 #include <linux/module.h>
5 #include <linux/pstore.h>
6 #include <linux/slab.h>
7 #include <linux/ucs2_string.h>
9 #define DUMP_NAME_LEN 66
11 #define EFIVARS_DATA_SIZE_MAX 1024
13 static bool efivars_pstore_disable =
14 IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
16 module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
18 #define PSTORE_EFI_ATTRIBUTES \
19 (EFI_VARIABLE_NON_VOLATILE | \
20 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
21 EFI_VARIABLE_RUNTIME_ACCESS)
23 static LIST_HEAD(efi_pstore_list);
24 static DECLARE_WORK(efivar_work, NULL);
26 static int efi_pstore_open(struct pstore_info *psi)
32 static int efi_pstore_close(struct pstore_info *psi)
38 static inline u64 generic_id(u64 timestamp, unsigned int part, int count)
40 return (timestamp * 100 + part) * 1000 + count;
43 static int efi_pstore_read_func(struct efivar_entry *entry,
44 struct pstore_record *record)
46 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
47 char name[DUMP_NAME_LEN], data_type;
54 if (efi_guidcmp(entry->var.VendorGuid, vendor))
57 for (i = 0; i < DUMP_NAME_LEN; i++)
58 name[i] = entry->var.VariableName[i];
60 if (sscanf(name, "dump-type%u-%u-%d-%llu-%c",
61 &record->type, &part, &cnt, &time, &data_type) == 5) {
62 record->id = generic_id(time, part, cnt);
65 record->time.tv_sec = time;
66 record->time.tv_nsec = 0;
68 record->compressed = true;
70 record->compressed = false;
71 record->ecc_notice_size = 0;
72 } else if (sscanf(name, "dump-type%u-%u-%d-%llu",
73 &record->type, &part, &cnt, &time) == 4) {
74 record->id = generic_id(time, part, cnt);
77 record->time.tv_sec = time;
78 record->time.tv_nsec = 0;
79 record->compressed = false;
80 record->ecc_notice_size = 0;
81 } else if (sscanf(name, "dump-type%u-%u-%llu",
82 &record->type, &part, &time) == 3) {
84 * Check if an old format,
85 * which doesn't support holding
86 * multiple logs, remains.
88 record->id = generic_id(time, part, 0);
91 record->time.tv_sec = time;
92 record->time.tv_nsec = 0;
93 record->compressed = false;
94 record->ecc_notice_size = 0;
98 entry->var.DataSize = 1024;
99 __efivar_entry_get(entry, &entry->var.Attributes,
100 &entry->var.DataSize, entry->var.Data);
101 size = entry->var.DataSize;
102 memcpy(record->buf, entry->var.Data,
103 (size_t)min_t(unsigned long, EFIVARS_DATA_SIZE_MAX, size));
109 * efi_pstore_scan_sysfs_enter
110 * @pos: scanning entry
114 static void efi_pstore_scan_sysfs_enter(struct efivar_entry *pos,
115 struct efivar_entry *next,
116 struct list_head *head)
118 pos->scanning = true;
119 if (&next->list != head)
120 next->scanning = true;
124 * __efi_pstore_scan_sysfs_exit
125 * @entry: deleting entry
126 * @turn_off_scanning: Check if a scanning flag should be turned off
128 static inline int __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry,
129 bool turn_off_scanning)
131 if (entry->deleting) {
132 list_del(&entry->list);
133 efivar_entry_iter_end();
135 if (efivar_entry_iter_begin())
137 } else if (turn_off_scanning)
138 entry->scanning = false;
144 * efi_pstore_scan_sysfs_exit
145 * @pos: scanning entry
148 * @stop: a flag checking if scanning will stop
150 static int efi_pstore_scan_sysfs_exit(struct efivar_entry *pos,
151 struct efivar_entry *next,
152 struct list_head *head, bool stop)
154 int ret = __efi_pstore_scan_sysfs_exit(pos, true);
160 ret = __efi_pstore_scan_sysfs_exit(next, &next->list != head);
165 * efi_pstore_sysfs_entry_iter
167 * @record: pstore record to pass to callback
169 * You MUST call efivar_entry_iter_begin() before this function, and
170 * efivar_entry_iter_end() afterwards.
173 static int efi_pstore_sysfs_entry_iter(struct pstore_record *record)
175 struct efivar_entry **pos = (struct efivar_entry **)&record->psi->data;
176 struct efivar_entry *entry, *n;
177 struct list_head *head = &efi_pstore_list;
182 list_for_each_entry_safe(entry, n, head, list) {
183 efi_pstore_scan_sysfs_enter(entry, n, head);
185 size = efi_pstore_read_func(entry, record);
186 ret = efi_pstore_scan_sysfs_exit(entry, n, head,
197 list_for_each_entry_safe_from((*pos), n, head, list) {
198 efi_pstore_scan_sysfs_enter((*pos), n, head);
200 size = efi_pstore_read_func((*pos), record);
201 ret = efi_pstore_scan_sysfs_exit((*pos), n, head, size < 0);
214 * This function returns a size of NVRAM entry logged via efi_pstore_write().
215 * The meaning and behavior of efi_pstore/pstore are as below.
217 * size > 0: Got data of an entry logged via efi_pstore_write() successfully,
218 * and pstore filesystem will continue reading subsequent entries.
219 * size == 0: Entry was not logged via efi_pstore_write(),
220 * and efi_pstore driver will continue reading subsequent entries.
221 * size < 0: Failed to get data of entry logging via efi_pstore_write(),
222 * and pstore will stop reading entry.
224 static ssize_t efi_pstore_read(struct pstore_record *record)
228 record->buf = kzalloc(EFIVARS_DATA_SIZE_MAX, GFP_KERNEL);
232 if (efivar_entry_iter_begin()) {
236 size = efi_pstore_sysfs_entry_iter(record);
237 efivar_entry_iter_end();
247 static int efi_pstore_write(struct pstore_record *record)
249 char name[DUMP_NAME_LEN];
250 efi_char16_t efi_name[DUMP_NAME_LEN];
251 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
254 record->id = generic_id(record->time.tv_sec, record->part,
257 /* Since we copy the entire length of name, make sure it is wiped. */
258 memset(name, 0, sizeof(name));
260 snprintf(name, sizeof(name), "dump-type%u-%u-%d-%lld-%c",
261 record->type, record->part, record->count,
262 (long long)record->time.tv_sec,
263 record->compressed ? 'C' : 'D');
265 for (i = 0; i < DUMP_NAME_LEN; i++)
266 efi_name[i] = name[i];
268 ret = efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
269 false, record->size, record->psi->buf);
271 if (record->reason == KMSG_DUMP_OOPS && try_module_get(THIS_MODULE))
272 if (!schedule_work(&efivar_work))
273 module_put(THIS_MODULE);
279 * Clean up an entry with the same name
281 static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
283 efi_char16_t *efi_name = data;
284 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
285 unsigned long ucs2_len = ucs2_strlen(efi_name);
287 if (efi_guidcmp(entry->var.VendorGuid, vendor))
290 if (ucs2_strncmp(entry->var.VariableName, efi_name, (size_t)ucs2_len))
293 if (entry->scanning) {
295 * Skip deletion because this entry will be deleted
296 * after scanning is completed.
298 entry->deleting = true;
300 list_del(&entry->list);
303 __efivar_entry_delete(entry);
308 static int efi_pstore_erase_name(const char *name)
310 struct efivar_entry *entry = NULL;
311 efi_char16_t efi_name[DUMP_NAME_LEN];
314 for (i = 0; i < DUMP_NAME_LEN; i++) {
315 efi_name[i] = name[i];
320 if (efivar_entry_iter_begin())
323 found = __efivar_entry_iter(efi_pstore_erase_func, &efi_pstore_list,
325 efivar_entry_iter_end();
327 if (found && !entry->scanning)
330 return found ? 0 : -ENOENT;
333 static int efi_pstore_erase(struct pstore_record *record)
335 char name[DUMP_NAME_LEN];
338 snprintf(name, sizeof(name), "dump-type%u-%u-%d-%lld",
339 record->type, record->part, record->count,
340 (long long)record->time.tv_sec);
341 ret = efi_pstore_erase_name(name);
345 snprintf(name, sizeof(name), "dump-type%u-%u-%lld",
346 record->type, record->part, (long long)record->time.tv_sec);
347 ret = efi_pstore_erase_name(name);
352 static struct pstore_info efi_pstore_info = {
353 .owner = THIS_MODULE,
355 .flags = PSTORE_FLAGS_DMESG,
356 .open = efi_pstore_open,
357 .close = efi_pstore_close,
358 .read = efi_pstore_read,
359 .write = efi_pstore_write,
360 .erase = efi_pstore_erase,
363 static int efi_pstore_callback(efi_char16_t *name, efi_guid_t vendor,
364 unsigned long name_size, void *data)
366 struct efivar_entry *entry;
369 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
373 memcpy(entry->var.VariableName, name, name_size);
374 entry->var.VendorGuid = vendor;
376 ret = efivar_entry_add(entry, &efi_pstore_list);
383 static int efi_pstore_update_entry(efi_char16_t *name, efi_guid_t vendor,
384 unsigned long name_size, void *data)
386 struct efivar_entry *entry = data;
388 if (efivar_entry_find(name, vendor, &efi_pstore_list, false))
391 memcpy(entry->var.VariableName, name, name_size);
392 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
397 static void efi_pstore_update_entries(struct work_struct *work)
399 struct efivar_entry *entry;
402 /* Add new sysfs entries */
404 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
408 err = efivar_init(efi_pstore_update_entry, entry,
409 false, &efi_pstore_list);
413 efivar_entry_add(entry, &efi_pstore_list);
417 module_put(THIS_MODULE);
420 static __init int efivars_pstore_init(void)
424 if (!efivars_kobject() || !efivar_supports_writes())
427 if (efivars_pstore_disable)
430 ret = efivar_init(efi_pstore_callback, NULL, true, &efi_pstore_list);
434 efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
435 if (!efi_pstore_info.buf)
438 efi_pstore_info.bufsize = 1024;
440 if (pstore_register(&efi_pstore_info)) {
441 kfree(efi_pstore_info.buf);
442 efi_pstore_info.buf = NULL;
443 efi_pstore_info.bufsize = 0;
446 INIT_WORK(&efivar_work, efi_pstore_update_entries);
451 static __exit void efivars_pstore_exit(void)
453 if (!efi_pstore_info.bufsize)
456 pstore_unregister(&efi_pstore_info);
457 kfree(efi_pstore_info.buf);
458 efi_pstore_info.buf = NULL;
459 efi_pstore_info.bufsize = 0;
462 module_init(efivars_pstore_init);
463 module_exit(efivars_pstore_exit);
465 MODULE_DESCRIPTION("EFI variable backend for pstore");
466 MODULE_LICENSE("GPL");
467 MODULE_ALIAS("platform:efivars");