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)
21 efivar_entry_iter_begin();
26 static int efi_pstore_close(struct pstore_info *psi)
28 efivar_entry_iter_end();
33 struct pstore_read_data {
35 enum pstore_type_id *type;
37 struct timespec *timespec;
41 static inline u64 generic_id(unsigned long timestamp,
42 unsigned int part, int count)
44 return (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];
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",
64 cb_data->type, &part, &cnt, &time) == 4) {
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;
69 } else if (sscanf(name, "dump-type%u-%u-%lu",
70 cb_data->type, &part, &time) == 3) {
72 * Check if an old format,
73 * which doesn't support holding
74 * multiple logs, remains.
76 *cb_data->id = generic_id(time, part, 0);
78 cb_data->timespec->tv_sec = time;
79 cb_data->timespec->tv_nsec = 0;
83 entry->var.DataSize = 1024;
84 __efivar_entry_get(entry, &entry->var.Attributes,
85 &entry->var.DataSize, entry->var.Data);
86 size = entry->var.DataSize;
88 *cb_data->buf = kmalloc(size, GFP_KERNEL);
89 if (*cb_data->buf == NULL)
91 memcpy(*cb_data->buf, entry->var.Data, size);
95 static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
96 int *count, struct timespec *timespec,
97 char **buf, struct pstore_info *psi)
99 struct pstore_read_data data;
104 data.timespec = timespec;
107 return __efivar_entry_iter(efi_pstore_read_func, &efivar_sysfs_list, &data,
108 (struct efivar_entry **)&psi->data);
111 static int efi_pstore_write(enum pstore_type_id type,
112 enum kmsg_dump_reason reason, u64 *id,
113 unsigned int part, int count, size_t size,
114 struct pstore_info *psi)
116 char name[DUMP_NAME_LEN];
117 efi_char16_t efi_name[DUMP_NAME_LEN];
118 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
121 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
124 for (i = 0; i < DUMP_NAME_LEN; i++)
125 efi_name[i] = name[i];
127 efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
128 !pstore_cannot_block_path(reason),
131 if (reason == KMSG_DUMP_OOPS)
138 struct pstore_erase_data {
140 enum pstore_type_id type;
142 struct timespec time;
147 * Clean up an entry with the same name
149 static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
151 struct pstore_erase_data *ed = data;
152 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
153 efi_char16_t efi_name_old[DUMP_NAME_LEN];
154 efi_char16_t *efi_name = ed->name;
155 unsigned long ucs2_len = ucs2_strlen(ed->name);
156 char name_old[DUMP_NAME_LEN];
159 if (efi_guidcmp(entry->var.VendorGuid, vendor))
162 if (ucs2_strncmp(entry->var.VariableName,
163 efi_name, (size_t)ucs2_len)) {
165 * Check if an old format, which doesn't support
166 * holding multiple logs, remains.
168 sprintf(name_old, "dump-type%u-%u-%lu", ed->type,
169 (unsigned int)ed->id, ed->time.tv_sec);
171 for (i = 0; i < DUMP_NAME_LEN; i++)
172 efi_name_old[i] = name_old[i];
174 if (ucs2_strncmp(entry->var.VariableName, efi_name_old,
175 ucs2_strlen(efi_name_old)))
180 __efivar_entry_delete(entry);
181 list_del(&entry->list);
186 static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
187 struct timespec time, struct pstore_info *psi)
189 struct pstore_erase_data edata;
190 struct efivar_entry *entry = NULL;
191 char name[DUMP_NAME_LEN];
192 efi_char16_t efi_name[DUMP_NAME_LEN];
197 part = do_div(id, 100);
198 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count, time.tv_sec);
200 for (i = 0; i < DUMP_NAME_LEN; i++)
201 efi_name[i] = name[i];
207 edata.name = efi_name;
209 efivar_entry_iter_begin();
210 found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
211 efivar_entry_iter_end();
214 efivar_unregister(entry);
219 static struct pstore_info efi_pstore_info = {
220 .owner = THIS_MODULE,
222 .open = efi_pstore_open,
223 .close = efi_pstore_close,
224 .read = efi_pstore_read,
225 .write = efi_pstore_write,
226 .erase = efi_pstore_erase,
229 static __init int efivars_pstore_init(void)
231 if (!efi_enabled(EFI_RUNTIME_SERVICES))
234 if (!efivars_kobject())
237 if (efivars_pstore_disable)
240 efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
241 if (!efi_pstore_info.buf)
244 efi_pstore_info.bufsize = 1024;
245 spin_lock_init(&efi_pstore_info.buf_lock);
247 pstore_register(&efi_pstore_info);
252 static __exit void efivars_pstore_exit(void)
256 module_init(efivars_pstore_init);
257 module_exit(efivars_pstore_exit);
259 MODULE_DESCRIPTION("EFI variable backend for pstore");
260 MODULE_LICENSE("GPL");