2 #include <linux/module.h>
3 #include <linux/pstore.h>
4 #include <linux/slab.h>
5 #include <linux/ucs2_string.h>
7 #define DUMP_NAME_LEN 52
9 static bool efivars_pstore_disable =
10 IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
12 module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
14 #define PSTORE_EFI_ATTRIBUTES \
15 (EFI_VARIABLE_NON_VOLATILE | \
16 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
17 EFI_VARIABLE_RUNTIME_ACCESS)
19 static int efi_pstore_open(struct pstore_info *psi)
25 static int efi_pstore_close(struct pstore_info *psi)
31 static inline u64 generic_id(unsigned long timestamp,
32 unsigned int part, int count)
34 return ((u64) timestamp * 100 + part) * 1000 + count;
37 static int efi_pstore_read_func(struct efivar_entry *entry,
38 struct pstore_record *record)
40 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
41 char name[DUMP_NAME_LEN], data_type;
45 unsigned long time, size;
47 if (efi_guidcmp(entry->var.VendorGuid, vendor))
50 for (i = 0; i < DUMP_NAME_LEN; i++)
51 name[i] = entry->var.VariableName[i];
53 if (sscanf(name, "dump-type%u-%u-%d-%lu-%c",
54 &record->type, &part, &cnt, &time, &data_type) == 5) {
55 record->id = generic_id(time, part, cnt);
57 record->time.tv_sec = time;
58 record->time.tv_nsec = 0;
60 record->compressed = true;
62 record->compressed = false;
63 record->ecc_notice_size = 0;
64 } else if (sscanf(name, "dump-type%u-%u-%d-%lu",
65 &record->type, &part, &cnt, &time) == 4) {
66 record->id = generic_id(time, part, cnt);
68 record->time.tv_sec = time;
69 record->time.tv_nsec = 0;
70 record->compressed = false;
71 record->ecc_notice_size = 0;
72 } else if (sscanf(name, "dump-type%u-%u-%lu",
73 &record->type, &part, &time) == 3) {
75 * Check if an old format,
76 * which doesn't support holding
77 * multiple logs, remains.
79 record->id = generic_id(time, part, 0);
81 record->time.tv_sec = time;
82 record->time.tv_nsec = 0;
83 record->compressed = false;
84 record->ecc_notice_size = 0;
88 entry->var.DataSize = 1024;
89 __efivar_entry_get(entry, &entry->var.Attributes,
90 &entry->var.DataSize, entry->var.Data);
91 size = entry->var.DataSize;
92 memcpy(record->buf, entry->var.Data,
93 (size_t)min_t(unsigned long, EFIVARS_DATA_SIZE_MAX, size));
99 * efi_pstore_scan_sysfs_enter
100 * @pos: scanning entry
104 static void efi_pstore_scan_sysfs_enter(struct efivar_entry *pos,
105 struct efivar_entry *next,
106 struct list_head *head)
108 pos->scanning = true;
109 if (&next->list != head)
110 next->scanning = true;
114 * __efi_pstore_scan_sysfs_exit
115 * @entry: deleting entry
116 * @turn_off_scanning: Check if a scanning flag should be turned off
118 static inline int __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry,
119 bool turn_off_scanning)
121 if (entry->deleting) {
122 list_del(&entry->list);
123 efivar_entry_iter_end();
124 efivar_unregister(entry);
125 if (efivar_entry_iter_begin())
127 } else if (turn_off_scanning)
128 entry->scanning = false;
134 * efi_pstore_scan_sysfs_exit
135 * @pos: scanning entry
138 * @stop: a flag checking if scanning will stop
140 static int efi_pstore_scan_sysfs_exit(struct efivar_entry *pos,
141 struct efivar_entry *next,
142 struct list_head *head, bool stop)
144 int ret = __efi_pstore_scan_sysfs_exit(pos, true);
150 ret = __efi_pstore_scan_sysfs_exit(next, &next->list != head);
155 * efi_pstore_sysfs_entry_iter
157 * @record: pstore record to pass to callback
158 * @pos: entry to begin iterating from
160 * You MUST call efivar_enter_iter_begin() before this function, and
161 * efivar_entry_iter_end() afterwards.
163 * It is possible to begin iteration from an arbitrary entry within
164 * the list by passing @pos. @pos is updated on return to point to
165 * the next entry of the last one passed to efi_pstore_read_func().
166 * To begin iterating from the beginning of the list @pos must be %NULL.
168 static int efi_pstore_sysfs_entry_iter(struct pstore_record *record,
169 struct efivar_entry **pos)
171 struct efivar_entry *entry, *n;
172 struct list_head *head = &efivar_sysfs_list;
177 list_for_each_entry_safe(entry, n, head, list) {
178 efi_pstore_scan_sysfs_enter(entry, n, head);
180 size = efi_pstore_read_func(entry, record);
181 ret = efi_pstore_scan_sysfs_exit(entry, n, head,
192 list_for_each_entry_safe_from((*pos), n, head, list) {
193 efi_pstore_scan_sysfs_enter((*pos), n, head);
195 size = efi_pstore_read_func((*pos), record);
196 ret = efi_pstore_scan_sysfs_exit((*pos), n, head, size < 0);
209 * This function returns a size of NVRAM entry logged via efi_pstore_write().
210 * The meaning and behavior of efi_pstore/pstore are as below.
212 * size > 0: Got data of an entry logged via efi_pstore_write() successfully,
213 * and pstore filesystem will continue reading subsequent entries.
214 * size == 0: Entry was not logged via efi_pstore_write(),
215 * and efi_pstore driver will continue reading subsequent entries.
216 * size < 0: Failed to get data of entry logging via efi_pstore_write(),
217 * and pstore will stop reading entry.
219 static ssize_t efi_pstore_read(struct pstore_record *record)
221 struct efivar_entry *entry = (struct efivar_entry *)record->psi->data;
224 record->buf = kzalloc(EFIVARS_DATA_SIZE_MAX, GFP_KERNEL);
228 if (efivar_entry_iter_begin()) {
232 size = efi_pstore_sysfs_entry_iter(record, &entry);
233 efivar_entry_iter_end();
243 static int efi_pstore_write(struct pstore_record *record)
245 char name[DUMP_NAME_LEN];
246 efi_char16_t efi_name[DUMP_NAME_LEN];
247 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
250 snprintf(name, sizeof(name), "dump-type%u-%u-%d-%lu-%c",
251 record->type, record->part, record->count,
252 get_seconds(), record->compressed ? 'C' : 'D');
254 for (i = 0; i < DUMP_NAME_LEN; i++)
255 efi_name[i] = name[i];
257 efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
258 !pstore_cannot_block_path(record->reason),
259 record->size, record->psi->buf);
261 if (record->reason == KMSG_DUMP_OOPS)
264 record->id = record->part;
268 struct pstore_erase_data {
270 enum pstore_type_id type;
272 struct timespec time;
277 * Clean up an entry with the same name
279 static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
281 struct pstore_erase_data *ed = data;
282 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
283 efi_char16_t efi_name_old[DUMP_NAME_LEN];
284 efi_char16_t *efi_name = ed->name;
285 unsigned long ucs2_len = ucs2_strlen(ed->name);
286 char name_old[DUMP_NAME_LEN];
289 if (efi_guidcmp(entry->var.VendorGuid, vendor))
292 if (ucs2_strncmp(entry->var.VariableName,
293 efi_name, (size_t)ucs2_len)) {
295 * Check if an old format, which doesn't support
296 * holding multiple logs, remains.
298 sprintf(name_old, "dump-type%u-%u-%lu", ed->type,
299 (unsigned int)ed->id, ed->time.tv_sec);
301 for (i = 0; i < DUMP_NAME_LEN; i++)
302 efi_name_old[i] = name_old[i];
304 if (ucs2_strncmp(entry->var.VariableName, efi_name_old,
305 ucs2_strlen(efi_name_old)))
309 if (entry->scanning) {
311 * Skip deletion because this entry will be deleted
312 * after scanning is completed.
314 entry->deleting = true;
316 list_del(&entry->list);
319 __efivar_entry_delete(entry);
324 static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
325 struct timespec time, struct pstore_info *psi)
327 struct pstore_erase_data edata;
328 struct efivar_entry *entry = NULL;
329 char name[DUMP_NAME_LEN];
330 efi_char16_t efi_name[DUMP_NAME_LEN];
335 part = do_div(id, 100);
336 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count, time.tv_sec);
338 for (i = 0; i < DUMP_NAME_LEN; i++)
339 efi_name[i] = name[i];
345 edata.name = efi_name;
347 if (efivar_entry_iter_begin())
349 found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
351 if (found && !entry->scanning) {
352 efivar_entry_iter_end();
353 efivar_unregister(entry);
355 efivar_entry_iter_end();
360 static struct pstore_info efi_pstore_info = {
361 .owner = THIS_MODULE,
363 .flags = PSTORE_FLAGS_DMESG,
364 .open = efi_pstore_open,
365 .close = efi_pstore_close,
366 .read = efi_pstore_read,
367 .write = efi_pstore_write,
368 .erase = efi_pstore_erase,
371 static __init int efivars_pstore_init(void)
373 if (!efi_enabled(EFI_RUNTIME_SERVICES))
376 if (!efivars_kobject())
379 if (efivars_pstore_disable)
382 efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
383 if (!efi_pstore_info.buf)
386 efi_pstore_info.bufsize = 1024;
387 spin_lock_init(&efi_pstore_info.buf_lock);
389 if (pstore_register(&efi_pstore_info)) {
390 kfree(efi_pstore_info.buf);
391 efi_pstore_info.buf = NULL;
392 efi_pstore_info.bufsize = 0;
398 static __exit void efivars_pstore_exit(void)
400 if (!efi_pstore_info.bufsize)
403 pstore_unregister(&efi_pstore_info);
404 kfree(efi_pstore_info.buf);
405 efi_pstore_info.buf = NULL;
406 efi_pstore_info.bufsize = 0;
409 module_init(efivars_pstore_init);
410 module_exit(efivars_pstore_exit);
412 MODULE_DESCRIPTION("EFI variable backend for pstore");
413 MODULE_LICENSE("GPL");
414 MODULE_ALIAS("platform:efivars");