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 static bool efivars_pstore_disable
=
12 IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE
);
14 module_param_named(pstore_disable
, efivars_pstore_disable
, bool, 0644);
16 #define PSTORE_EFI_ATTRIBUTES \
17 (EFI_VARIABLE_NON_VOLATILE | \
18 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
19 EFI_VARIABLE_RUNTIME_ACCESS)
21 static int efi_pstore_open(struct pstore_info
*psi
)
27 static int efi_pstore_close(struct pstore_info
*psi
)
33 static inline u64
generic_id(u64 timestamp
, unsigned int part
, int count
)
35 return (timestamp
* 100 + part
) * 1000 + count
;
38 static int efi_pstore_read_func(struct efivar_entry
*entry
,
39 struct pstore_record
*record
)
41 efi_guid_t vendor
= LINUX_EFI_CRASH_GUID
;
42 char name
[DUMP_NAME_LEN
], data_type
;
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-%llu-%c",
56 &record
->type
, &part
, &cnt
, &time
, &data_type
) == 5) {
57 record
->id
= generic_id(time
, part
, cnt
);
60 record
->time
.tv_sec
= time
;
61 record
->time
.tv_nsec
= 0;
63 record
->compressed
= true;
65 record
->compressed
= false;
66 record
->ecc_notice_size
= 0;
67 } else if (sscanf(name
, "dump-type%u-%u-%d-%llu",
68 &record
->type
, &part
, &cnt
, &time
) == 4) {
69 record
->id
= generic_id(time
, part
, cnt
);
72 record
->time
.tv_sec
= time
;
73 record
->time
.tv_nsec
= 0;
74 record
->compressed
= false;
75 record
->ecc_notice_size
= 0;
76 } else if (sscanf(name
, "dump-type%u-%u-%llu",
77 &record
->type
, &part
, &time
) == 3) {
79 * Check if an old format,
80 * which doesn't support holding
81 * multiple logs, remains.
83 record
->id
= generic_id(time
, part
, 0);
86 record
->time
.tv_sec
= time
;
87 record
->time
.tv_nsec
= 0;
88 record
->compressed
= false;
89 record
->ecc_notice_size
= 0;
93 entry
->var
.DataSize
= 1024;
94 __efivar_entry_get(entry
, &entry
->var
.Attributes
,
95 &entry
->var
.DataSize
, entry
->var
.Data
);
96 size
= entry
->var
.DataSize
;
97 memcpy(record
->buf
, entry
->var
.Data
,
98 (size_t)min_t(unsigned long, EFIVARS_DATA_SIZE_MAX
, size
));
104 * efi_pstore_scan_sysfs_enter
105 * @pos: scanning entry
109 static void efi_pstore_scan_sysfs_enter(struct efivar_entry
*pos
,
110 struct efivar_entry
*next
,
111 struct list_head
*head
)
113 pos
->scanning
= true;
114 if (&next
->list
!= head
)
115 next
->scanning
= true;
119 * __efi_pstore_scan_sysfs_exit
120 * @entry: deleting entry
121 * @turn_off_scanning: Check if a scanning flag should be turned off
123 static inline int __efi_pstore_scan_sysfs_exit(struct efivar_entry
*entry
,
124 bool turn_off_scanning
)
126 if (entry
->deleting
) {
127 list_del(&entry
->list
);
128 efivar_entry_iter_end();
129 efivar_unregister(entry
);
130 if (efivar_entry_iter_begin())
132 } else if (turn_off_scanning
)
133 entry
->scanning
= false;
139 * efi_pstore_scan_sysfs_exit
140 * @pos: scanning entry
143 * @stop: a flag checking if scanning will stop
145 static int efi_pstore_scan_sysfs_exit(struct efivar_entry
*pos
,
146 struct efivar_entry
*next
,
147 struct list_head
*head
, bool stop
)
149 int ret
= __efi_pstore_scan_sysfs_exit(pos
, true);
155 ret
= __efi_pstore_scan_sysfs_exit(next
, &next
->list
!= head
);
160 * efi_pstore_sysfs_entry_iter
162 * @record: pstore record to pass to callback
164 * You MUST call efivar_enter_iter_begin() before this function, and
165 * efivar_entry_iter_end() afterwards.
168 static int efi_pstore_sysfs_entry_iter(struct pstore_record
*record
)
170 struct efivar_entry
**pos
= (struct efivar_entry
**)&record
->psi
->data
;
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
)
223 record
->buf
= kzalloc(EFIVARS_DATA_SIZE_MAX
, GFP_KERNEL
);
227 if (efivar_entry_iter_begin()) {
231 size
= efi_pstore_sysfs_entry_iter(record
);
232 efivar_entry_iter_end();
242 static int efi_pstore_write(struct pstore_record
*record
)
244 char name
[DUMP_NAME_LEN
];
245 efi_char16_t efi_name
[DUMP_NAME_LEN
];
246 efi_guid_t vendor
= LINUX_EFI_CRASH_GUID
;
249 record
->id
= generic_id(record
->time
.tv_sec
, record
->part
,
252 /* Since we copy the entire length of name, make sure it is wiped. */
253 memset(name
, 0, sizeof(name
));
255 snprintf(name
, sizeof(name
), "dump-type%u-%u-%d-%lld-%c",
256 record
->type
, record
->part
, record
->count
,
257 (long long)record
->time
.tv_sec
,
258 record
->compressed
? 'C' : 'D');
260 for (i
= 0; i
< DUMP_NAME_LEN
; i
++)
261 efi_name
[i
] = name
[i
];
263 ret
= efivar_entry_set_safe(efi_name
, vendor
, PSTORE_EFI_ATTRIBUTES
,
264 preemptible(), record
->size
, record
->psi
->buf
);
266 if (record
->reason
== KMSG_DUMP_OOPS
)
273 * Clean up an entry with the same name
275 static int efi_pstore_erase_func(struct efivar_entry
*entry
, void *data
)
277 efi_char16_t
*efi_name
= data
;
278 efi_guid_t vendor
= LINUX_EFI_CRASH_GUID
;
279 unsigned long ucs2_len
= ucs2_strlen(efi_name
);
281 if (efi_guidcmp(entry
->var
.VendorGuid
, vendor
))
284 if (ucs2_strncmp(entry
->var
.VariableName
, efi_name
, (size_t)ucs2_len
))
287 if (entry
->scanning
) {
289 * Skip deletion because this entry will be deleted
290 * after scanning is completed.
292 entry
->deleting
= true;
294 list_del(&entry
->list
);
297 __efivar_entry_delete(entry
);
302 static int efi_pstore_erase_name(const char *name
)
304 struct efivar_entry
*entry
= NULL
;
305 efi_char16_t efi_name
[DUMP_NAME_LEN
];
308 for (i
= 0; i
< DUMP_NAME_LEN
; i
++) {
309 efi_name
[i
] = name
[i
];
314 if (efivar_entry_iter_begin())
317 found
= __efivar_entry_iter(efi_pstore_erase_func
, &efivar_sysfs_list
,
319 efivar_entry_iter_end();
321 if (found
&& !entry
->scanning
)
322 efivar_unregister(entry
);
324 return found
? 0 : -ENOENT
;
327 static int efi_pstore_erase(struct pstore_record
*record
)
329 char name
[DUMP_NAME_LEN
];
332 snprintf(name
, sizeof(name
), "dump-type%u-%u-%d-%lld",
333 record
->type
, record
->part
, record
->count
,
334 (long long)record
->time
.tv_sec
);
335 ret
= efi_pstore_erase_name(name
);
339 snprintf(name
, sizeof(name
), "dump-type%u-%u-%lld",
340 record
->type
, record
->part
, (long long)record
->time
.tv_sec
);
341 ret
= efi_pstore_erase_name(name
);
346 static struct pstore_info efi_pstore_info
= {
347 .owner
= THIS_MODULE
,
349 .flags
= PSTORE_FLAGS_DMESG
,
350 .open
= efi_pstore_open
,
351 .close
= efi_pstore_close
,
352 .read
= efi_pstore_read
,
353 .write
= efi_pstore_write
,
354 .erase
= efi_pstore_erase
,
357 static __init
int efivars_pstore_init(void)
359 if (!efi_enabled(EFI_RUNTIME_SERVICES
))
362 if (!efivars_kobject())
365 if (efivars_pstore_disable
)
368 efi_pstore_info
.buf
= kmalloc(4096, GFP_KERNEL
);
369 if (!efi_pstore_info
.buf
)
372 efi_pstore_info
.bufsize
= 1024;
374 if (pstore_register(&efi_pstore_info
)) {
375 kfree(efi_pstore_info
.buf
);
376 efi_pstore_info
.buf
= NULL
;
377 efi_pstore_info
.bufsize
= 0;
383 static __exit
void efivars_pstore_exit(void)
385 if (!efi_pstore_info
.bufsize
)
388 pstore_unregister(&efi_pstore_info
);
389 kfree(efi_pstore_info
.buf
);
390 efi_pstore_info
.buf
= NULL
;
391 efi_pstore_info
.bufsize
= 0;
394 module_init(efivars_pstore_init
);
395 module_exit(efivars_pstore_exit
);
397 MODULE_DESCRIPTION("EFI variable backend for pstore");
398 MODULE_LICENSE("GPL");
399 MODULE_ALIAS("platform:efivars");