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 struct pstore_read_data {
33 enum pstore_type_id *type;
35 struct timespec *timespec;
37 ssize_t *ecc_notice_size;
41 static inline u64 generic_id(unsigned long timestamp,
42 unsigned int part, int count)
44 return ((u64) timestamp * 100 + part) * 1000 + count;
47 static int efi_pstore_read_func(struct efivar_entry *entry, void *data)
49 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
50 struct pstore_read_data *cb_data = data;
51 char name[DUMP_NAME_LEN], data_type;
55 unsigned long time, size;
57 if (efi_guidcmp(entry->var.VendorGuid, vendor))
60 for (i = 0; i < DUMP_NAME_LEN; i++)
61 name[i] = entry->var.VariableName[i];
63 if (sscanf(name, "dump-type%u-%u-%d-%lu-%c",
64 cb_data->type, &part, &cnt, &time, &data_type) == 5) {
65 *cb_data->id = generic_id(time, part, cnt);
66 *cb_data->count = cnt;
67 cb_data->timespec->tv_sec = time;
68 cb_data->timespec->tv_nsec = 0;
70 *cb_data->compressed = true;
72 *cb_data->compressed = false;
73 *cb_data->ecc_notice_size = 0;
74 } else if (sscanf(name, "dump-type%u-%u-%d-%lu",
75 cb_data->type, &part, &cnt, &time) == 4) {
76 *cb_data->id = generic_id(time, part, cnt);
77 *cb_data->count = cnt;
78 cb_data->timespec->tv_sec = time;
79 cb_data->timespec->tv_nsec = 0;
80 *cb_data->compressed = false;
81 *cb_data->ecc_notice_size = 0;
82 } else if (sscanf(name, "dump-type%u-%u-%lu",
83 cb_data->type, &part, &time) == 3) {
85 * Check if an old format,
86 * which doesn't support holding
87 * multiple logs, remains.
89 *cb_data->id = generic_id(time, part, 0);
91 cb_data->timespec->tv_sec = time;
92 cb_data->timespec->tv_nsec = 0;
93 *cb_data->compressed = false;
94 *cb_data->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(*cb_data->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 void __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();
134 efivar_unregister(entry);
135 efivar_entry_iter_begin();
136 } else if (turn_off_scanning)
137 entry->scanning = false;
141 * efi_pstore_scan_sysfs_exit
142 * @pos: scanning entry
145 * @stop: a flag checking if scanning will stop
147 static void efi_pstore_scan_sysfs_exit(struct efivar_entry *pos,
148 struct efivar_entry *next,
149 struct list_head *head, bool stop)
151 __efi_pstore_scan_sysfs_exit(pos, true);
153 __efi_pstore_scan_sysfs_exit(next, &next->list != head);
157 * efi_pstore_sysfs_entry_iter
159 * @data: function-specific data to pass to callback
160 * @pos: entry to begin iterating from
162 * You MUST call efivar_enter_iter_begin() before this function, and
163 * efivar_entry_iter_end() afterwards.
165 * It is possible to begin iteration from an arbitrary entry within
166 * the list by passing @pos. @pos is updated on return to point to
167 * the next entry of the last one passed to efi_pstore_read_func().
168 * To begin iterating from the beginning of the list @pos must be %NULL.
170 static int efi_pstore_sysfs_entry_iter(void *data, struct efivar_entry **pos)
172 struct efivar_entry *entry, *n;
173 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, data);
181 efi_pstore_scan_sysfs_exit(entry, n, head, size < 0);
189 list_for_each_entry_safe_from((*pos), n, head, list) {
190 efi_pstore_scan_sysfs_enter((*pos), n, head);
192 size = efi_pstore_read_func((*pos), data);
193 efi_pstore_scan_sysfs_exit((*pos), n, head, size < 0);
204 * This function returns a size of NVRAM entry logged via efi_pstore_write().
205 * The meaning and behavior of efi_pstore/pstore are as below.
207 * size > 0: Got data of an entry logged via efi_pstore_write() successfully,
208 * and pstore filesystem will continue reading subsequent entries.
209 * size == 0: Entry was not logged via efi_pstore_write(),
210 * and efi_pstore driver will continue reading subsequent entries.
211 * size < 0: Failed to get data of entry logging via efi_pstore_write(),
212 * and pstore will stop reading entry.
214 static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
215 int *count, struct timespec *timespec,
216 char **buf, bool *compressed,
217 ssize_t *ecc_notice_size,
218 struct pstore_info *psi)
220 struct pstore_read_data data;
226 data.timespec = timespec;
227 data.compressed = compressed;
228 data.ecc_notice_size = ecc_notice_size;
231 *data.buf = kzalloc(EFIVARS_DATA_SIZE_MAX, GFP_KERNEL);
235 efivar_entry_iter_begin();
236 size = efi_pstore_sysfs_entry_iter(&data,
237 (struct efivar_entry **)&psi->data);
238 efivar_entry_iter_end();
244 static int efi_pstore_write(enum pstore_type_id type,
245 enum kmsg_dump_reason reason, u64 *id,
246 unsigned int part, int count, bool compressed, size_t size,
247 struct pstore_info *psi)
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 sprintf(name, "dump-type%u-%u-%d-%lu-%c", type, part, count,
255 get_seconds(), compressed ? 'C' : 'D');
257 for (i = 0; i < DUMP_NAME_LEN; i++)
258 efi_name[i] = name[i];
260 efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
261 !pstore_cannot_block_path(reason),
264 if (reason == KMSG_DUMP_OOPS)
271 struct pstore_erase_data {
273 enum pstore_type_id type;
275 struct timespec time;
280 * Clean up an entry with the same name
282 static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
284 struct pstore_erase_data *ed = data;
285 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
286 efi_char16_t efi_name_old[DUMP_NAME_LEN];
287 efi_char16_t *efi_name = ed->name;
288 unsigned long ucs2_len = ucs2_strlen(ed->name);
289 char name_old[DUMP_NAME_LEN];
292 if (efi_guidcmp(entry->var.VendorGuid, vendor))
295 if (ucs2_strncmp(entry->var.VariableName,
296 efi_name, (size_t)ucs2_len)) {
298 * Check if an old format, which doesn't support
299 * holding multiple logs, remains.
301 sprintf(name_old, "dump-type%u-%u-%lu", ed->type,
302 (unsigned int)ed->id, ed->time.tv_sec);
304 for (i = 0; i < DUMP_NAME_LEN; i++)
305 efi_name_old[i] = name_old[i];
307 if (ucs2_strncmp(entry->var.VariableName, efi_name_old,
308 ucs2_strlen(efi_name_old)))
312 if (entry->scanning) {
314 * Skip deletion because this entry will be deleted
315 * after scanning is completed.
317 entry->deleting = true;
319 list_del(&entry->list);
322 __efivar_entry_delete(entry);
327 static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
328 struct timespec time, struct pstore_info *psi)
330 struct pstore_erase_data edata;
331 struct efivar_entry *entry = NULL;
332 char name[DUMP_NAME_LEN];
333 efi_char16_t efi_name[DUMP_NAME_LEN];
338 part = do_div(id, 100);
339 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count, time.tv_sec);
341 for (i = 0; i < DUMP_NAME_LEN; i++)
342 efi_name[i] = name[i];
348 edata.name = efi_name;
350 efivar_entry_iter_begin();
351 found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
353 if (found && !entry->scanning) {
354 efivar_entry_iter_end();
355 efivar_unregister(entry);
357 efivar_entry_iter_end();
362 static struct pstore_info efi_pstore_info = {
363 .owner = THIS_MODULE,
365 .flags = PSTORE_FLAGS_FRAGILE,
366 .open = efi_pstore_open,
367 .close = efi_pstore_close,
368 .read = efi_pstore_read,
369 .write = efi_pstore_write,
370 .erase = efi_pstore_erase,
373 static __init int efivars_pstore_init(void)
375 if (!efi_enabled(EFI_RUNTIME_SERVICES))
378 if (!efivars_kobject())
381 if (efivars_pstore_disable)
384 efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
385 if (!efi_pstore_info.buf)
388 efi_pstore_info.bufsize = 1024;
389 spin_lock_init(&efi_pstore_info.buf_lock);
391 if (pstore_register(&efi_pstore_info)) {
392 kfree(efi_pstore_info.buf);
393 efi_pstore_info.buf = NULL;
394 efi_pstore_info.bufsize = 0;
400 static __exit void efivars_pstore_exit(void)
402 if (!efi_pstore_info.bufsize)
405 pstore_unregister(&efi_pstore_info);
406 kfree(efi_pstore_info.buf);
407 efi_pstore_info.buf = NULL;
408 efi_pstore_info.bufsize = 0;
411 module_init(efivars_pstore_init);
412 module_exit(efivars_pstore_exit);
414 MODULE_DESCRIPTION("EFI variable backend for pstore");
415 MODULE_LICENSE("GPL");
416 MODULE_ALIAS("platform:efivars");