2 #include <linux/module.h>
3 #include <linux/pstore.h>
5 #define DUMP_NAME_LEN 52
7 static bool efivars_pstore_disable =
8 IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
10 module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
12 #define PSTORE_EFI_ATTRIBUTES \
13 (EFI_VARIABLE_NON_VOLATILE | \
14 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
15 EFI_VARIABLE_RUNTIME_ACCESS)
17 static int efi_pstore_open(struct pstore_info *psi)
19 efivar_entry_iter_begin();
24 static int efi_pstore_close(struct pstore_info *psi)
26 efivar_entry_iter_end();
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 __efivar_entry_size(entry, &size);
76 *cb_data->buf = kmalloc(size, GFP_KERNEL);
77 if (*cb_data->buf == NULL)
79 memcpy(*cb_data->buf, entry->var.Data, size);
83 static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
84 int *count, struct timespec *timespec,
85 char **buf, struct pstore_info *psi)
87 struct pstore_read_data data;
92 data.timespec = timespec;
95 return __efivar_entry_iter(efi_pstore_read_func, &efivar_sysfs_list, &data,
96 (struct efivar_entry **)&psi->data);
99 static int efi_pstore_write(enum pstore_type_id type,
100 enum kmsg_dump_reason reason, u64 *id,
101 unsigned int part, int count, size_t size,
102 struct pstore_info *psi)
104 char name[DUMP_NAME_LEN];
105 efi_char16_t efi_name[DUMP_NAME_LEN];
106 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
109 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
112 for (i = 0; i < DUMP_NAME_LEN; i++)
113 efi_name[i] = name[i];
115 efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
116 !pstore_cannot_block_path(reason),
119 if (reason == KMSG_DUMP_OOPS)
126 struct pstore_erase_data {
128 enum pstore_type_id type;
130 struct timespec time;
135 * Clean up an entry with the same name
137 static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
139 struct pstore_erase_data *ed = data;
140 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
141 efi_char16_t efi_name_old[DUMP_NAME_LEN];
142 efi_char16_t *efi_name = ed->name;
143 unsigned long utf16_len = utf16_strlen(ed->name);
144 char name_old[DUMP_NAME_LEN];
147 if (efi_guidcmp(entry->var.VendorGuid, vendor))
150 if (utf16_strncmp(entry->var.VariableName,
151 efi_name, (size_t)utf16_len)) {
153 * Check if an old format, which doesn't support
154 * holding multiple logs, remains.
156 sprintf(name_old, "dump-type%u-%u-%lu", ed->type,
157 (unsigned int)ed->id, ed->time.tv_sec);
159 for (i = 0; i < DUMP_NAME_LEN; i++)
160 efi_name_old[i] = name_old[i];
162 if (utf16_strncmp(entry->var.VariableName, efi_name_old,
163 utf16_strlen(efi_name_old)))
168 __efivar_entry_delete(entry);
172 static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
173 struct timespec time, struct pstore_info *psi)
175 struct pstore_erase_data edata;
176 struct efivar_entry *entry;
177 char name[DUMP_NAME_LEN];
178 efi_char16_t efi_name[DUMP_NAME_LEN];
181 sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
184 for (i = 0; i < DUMP_NAME_LEN; i++)
185 efi_name[i] = name[i];
191 edata.name = efi_name;
193 efivar_entry_iter_begin();
194 found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
195 efivar_entry_iter_end();
198 efivar_unregister(entry);
203 static struct pstore_info efi_pstore_info = {
204 .owner = THIS_MODULE,
206 .open = efi_pstore_open,
207 .close = efi_pstore_close,
208 .read = efi_pstore_read,
209 .write = efi_pstore_write,
210 .erase = efi_pstore_erase,
213 static __init int efivars_pstore_init(void)
215 if (!efi_enabled(EFI_RUNTIME_SERVICES))
218 if (!efivars_kobject())
221 if (efivars_pstore_disable)
224 efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
225 if (!efi_pstore_info.buf)
228 efi_pstore_info.bufsize = 1024;
229 spin_lock_init(&efi_pstore_info.buf_lock);
231 pstore_register(&efi_pstore_info);
236 static __exit void efivars_pstore_exit(void)
240 module_init(efivars_pstore_init);
241 module_exit(efivars_pstore_exit);
243 MODULE_DESCRIPTION("EFI variable backend for pstore");
244 MODULE_LICENSE("GPL");