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;
39 static int efi_pstore_read_func(struct efivar_entry *entry, void *data)
41 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
42 struct pstore_read_data *cb_data = data;
43 char name[DUMP_NAME_LEN];
47 unsigned long time, size;
49 if (efi_guidcmp(entry->var.VendorGuid, vendor))
52 for (i = 0; i < DUMP_NAME_LEN; i++)
53 name[i] = entry->var.VariableName[i];
55 if (sscanf(name, "dump-type%u-%u-%d-%lu",
56 cb_data->type, &part, &cnt, &time) == 4) {
58 *cb_data->count = cnt;
59 cb_data->timespec->tv_sec = time;
60 cb_data->timespec->tv_nsec = 0;
61 } else if (sscanf(name, "dump-type%u-%u-%lu",
62 cb_data->type, &part, &time) == 3) {
64 * Check if an old format,
65 * which doesn't support holding
66 * multiple logs, remains.
70 cb_data->timespec->tv_sec = time;
71 cb_data->timespec->tv_nsec = 0;
75 entry->var.DataSize = 1024;
76 __efivar_entry_get(entry, &entry->var.Attributes,
77 &entry->var.DataSize, entry->var.Data);
78 size = entry->var.DataSize;
79 memcpy(*cb_data->buf, entry->var.Data,
80 (size_t)min_t(unsigned long, EFIVARS_DATA_SIZE_MAX, size));
86 * efi_pstore_scan_sysfs_enter
87 * @entry: scanning entry
91 static void efi_pstore_scan_sysfs_enter(struct efivar_entry *pos,
92 struct efivar_entry *next,
93 struct list_head *head)
96 if (&next->list != head)
97 next->scanning = true;
101 * __efi_pstore_scan_sysfs_exit
102 * @entry: deleting entry
103 * @turn_off_scanning: Check if a scanning flag should be turned off
105 static inline void __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry,
106 bool turn_off_scanning)
108 if (entry->deleting) {
109 list_del(&entry->list);
110 efivar_entry_iter_end();
111 efivar_unregister(entry);
112 efivar_entry_iter_begin();
113 } else if (turn_off_scanning)
114 entry->scanning = false;
118 * efi_pstore_scan_sysfs_exit
119 * @pos: scanning entry
122 * @stop: a flag checking if scanning will stop
124 static void efi_pstore_scan_sysfs_exit(struct efivar_entry *pos,
125 struct efivar_entry *next,
126 struct list_head *head, bool stop)
128 __efi_pstore_scan_sysfs_exit(pos, true);
130 __efi_pstore_scan_sysfs_exit(next, &next->list != head);
134 * efi_pstore_sysfs_entry_iter
136 * @data: function-specific data to pass to callback
137 * @pos: entry to begin iterating from
139 * You MUST call efivar_enter_iter_begin() before this function, and
140 * efivar_entry_iter_end() afterwards.
142 * It is possible to begin iteration from an arbitrary entry within
143 * the list by passing @pos. @pos is updated on return to point to
144 * the next entry of the last one passed to efi_pstore_read_func().
145 * To begin iterating from the beginning of the list @pos must be %NULL.
147 static int efi_pstore_sysfs_entry_iter(void *data, struct efivar_entry **pos)
149 struct efivar_entry *entry, *n;
150 struct list_head *head = &efivar_sysfs_list;
154 list_for_each_entry_safe(entry, n, head, list) {
155 efi_pstore_scan_sysfs_enter(entry, n, head);
157 size = efi_pstore_read_func(entry, data);
158 efi_pstore_scan_sysfs_exit(entry, n, head, size < 0);
166 list_for_each_entry_safe_from((*pos), n, head, list) {
167 efi_pstore_scan_sysfs_enter((*pos), n, head);
169 size = efi_pstore_read_func((*pos), data);
170 efi_pstore_scan_sysfs_exit((*pos), n, head, size < 0);
181 * This function returns a size of NVRAM entry logged via efi_pstore_write().
182 * The meaning and behavior of efi_pstore/pstore are as below.
184 * size > 0: Got data of an entry logged via efi_pstore_write() successfully,
185 * and pstore filesystem will continue reading subsequent entries.
186 * size == 0: Entry was not logged via efi_pstore_write(),
187 * and efi_pstore driver will continue reading subsequent entries.
188 * size < 0: Failed to get data of entry logging via efi_pstore_write(),
189 * and pstore will stop reading entry.
191 static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
192 int *count, struct timespec *timespec,
193 char **buf, struct pstore_info *psi)
195 struct pstore_read_data data;
201 data.timespec = timespec;
204 *data.buf = kzalloc(EFIVARS_DATA_SIZE_MAX, GFP_KERNEL);
208 efivar_entry_iter_begin();
209 size = efi_pstore_sysfs_entry_iter(&data,
210 (struct efivar_entry **)&psi->data);
211 efivar_entry_iter_end();
217 static int efi_pstore_write(enum pstore_type_id type,
218 enum kmsg_dump_reason reason, u64 *id,
219 unsigned int part, int count, size_t size,
220 struct pstore_info *psi)
222 char name[DUMP_NAME_LEN];
223 efi_char16_t efi_name[DUMP_NAME_LEN];
224 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
227 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
230 for (i = 0; i < DUMP_NAME_LEN; i++)
231 efi_name[i] = name[i];
233 efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
234 !pstore_cannot_block_path(reason),
237 if (reason == KMSG_DUMP_OOPS)
244 struct pstore_erase_data {
246 enum pstore_type_id type;
248 struct timespec time;
253 * Clean up an entry with the same name
255 static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
257 struct pstore_erase_data *ed = data;
258 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
259 efi_char16_t efi_name_old[DUMP_NAME_LEN];
260 efi_char16_t *efi_name = ed->name;
261 unsigned long ucs2_len = ucs2_strlen(ed->name);
262 char name_old[DUMP_NAME_LEN];
265 if (efi_guidcmp(entry->var.VendorGuid, vendor))
268 if (ucs2_strncmp(entry->var.VariableName,
269 efi_name, (size_t)ucs2_len)) {
271 * Check if an old format, which doesn't support
272 * holding multiple logs, remains.
274 sprintf(name_old, "dump-type%u-%u-%lu", ed->type,
275 (unsigned int)ed->id, ed->time.tv_sec);
277 for (i = 0; i < DUMP_NAME_LEN; i++)
278 efi_name_old[i] = name_old[i];
280 if (ucs2_strncmp(entry->var.VariableName, efi_name_old,
281 ucs2_strlen(efi_name_old)))
285 if (entry->scanning) {
287 * Skip deletion because this entry will be deleted
288 * after scanning is completed.
290 entry->deleting = true;
292 list_del(&entry->list);
295 __efivar_entry_delete(entry);
300 static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
301 struct timespec time, struct pstore_info *psi)
303 struct pstore_erase_data edata;
304 struct efivar_entry *entry = NULL;
305 char name[DUMP_NAME_LEN];
306 efi_char16_t efi_name[DUMP_NAME_LEN];
309 sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
312 for (i = 0; i < DUMP_NAME_LEN; i++)
313 efi_name[i] = name[i];
319 edata.name = efi_name;
321 efivar_entry_iter_begin();
322 found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
324 if (found && !entry->scanning) {
325 efivar_entry_iter_end();
326 efivar_unregister(entry);
328 efivar_entry_iter_end();
333 static struct pstore_info efi_pstore_info = {
334 .owner = THIS_MODULE,
336 .open = efi_pstore_open,
337 .close = efi_pstore_close,
338 .read = efi_pstore_read,
339 .write = efi_pstore_write,
340 .erase = efi_pstore_erase,
343 static __init int efivars_pstore_init(void)
345 if (!efi_enabled(EFI_RUNTIME_SERVICES))
348 if (!efivars_kobject())
351 if (efivars_pstore_disable)
354 efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
355 if (!efi_pstore_info.buf)
358 efi_pstore_info.bufsize = 1024;
359 spin_lock_init(&efi_pstore_info.buf_lock);
361 if (pstore_register(&efi_pstore_info)) {
362 kfree(efi_pstore_info.buf);
363 efi_pstore_info.buf = NULL;
364 efi_pstore_info.bufsize = 0;
370 static __exit void efivars_pstore_exit(void)
374 module_init(efivars_pstore_init);
375 module_exit(efivars_pstore_exit);
377 MODULE_DESCRIPTION("EFI variable backend for pstore");
378 MODULE_LICENSE("GPL");