2 #include <linux/module.h>
3 #include <linux/pstore.h>
4 #include <linux/ucs2_string.h>
6 #define DUMP_NAME_LEN 52
8 static bool efivars_pstore_disable =
9 IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
11 module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
13 #define PSTORE_EFI_ATTRIBUTES \
14 (EFI_VARIABLE_NON_VOLATILE | \
15 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
16 EFI_VARIABLE_RUNTIME_ACCESS)
18 static int efi_pstore_open(struct pstore_info *psi)
20 efivar_entry_iter_begin();
25 static int efi_pstore_close(struct pstore_info *psi)
27 efivar_entry_iter_end();
32 struct pstore_read_data {
34 enum pstore_type_id *type;
36 struct timespec *timespec;
40 static int efi_pstore_read_func(struct efivar_entry *entry, void *data)
42 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
43 struct pstore_read_data *cb_data = data;
44 char name[DUMP_NAME_LEN];
48 unsigned long time, size;
50 if (efi_guidcmp(entry->var.VendorGuid, vendor))
53 for (i = 0; i < DUMP_NAME_LEN; i++)
54 name[i] = entry->var.VariableName[i];
56 if (sscanf(name, "dump-type%u-%u-%d-%lu",
57 cb_data->type, &part, &cnt, &time) == 4) {
59 *cb_data->count = cnt;
60 cb_data->timespec->tv_sec = time;
61 cb_data->timespec->tv_nsec = 0;
62 } else if (sscanf(name, "dump-type%u-%u-%lu",
63 cb_data->type, &part, &time) == 3) {
65 * Check if an old format,
66 * which doesn't support holding
67 * multiple logs, remains.
71 cb_data->timespec->tv_sec = time;
72 cb_data->timespec->tv_nsec = 0;
76 __efivar_entry_size(entry, &size);
77 *cb_data->buf = kmalloc(size, GFP_KERNEL);
78 if (*cb_data->buf == NULL)
80 memcpy(*cb_data->buf, entry->var.Data, size);
84 static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
85 int *count, struct timespec *timespec,
86 char **buf, struct pstore_info *psi)
88 struct pstore_read_data data;
93 data.timespec = timespec;
96 return __efivar_entry_iter(efi_pstore_read_func, &efivar_sysfs_list, &data,
97 (struct efivar_entry **)&psi->data);
100 static int efi_pstore_write(enum pstore_type_id type,
101 enum kmsg_dump_reason reason, u64 *id,
102 unsigned int part, int count, size_t size,
103 struct pstore_info *psi)
105 char name[DUMP_NAME_LEN];
106 efi_char16_t efi_name[DUMP_NAME_LEN];
107 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
110 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
113 for (i = 0; i < DUMP_NAME_LEN; i++)
114 efi_name[i] = name[i];
116 efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
117 !pstore_cannot_block_path(reason),
120 if (reason == KMSG_DUMP_OOPS)
127 struct pstore_erase_data {
129 enum pstore_type_id type;
131 struct timespec time;
136 * Clean up an entry with the same name
138 static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
140 struct pstore_erase_data *ed = data;
141 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
142 efi_char16_t efi_name_old[DUMP_NAME_LEN];
143 efi_char16_t *efi_name = ed->name;
144 unsigned long ucs2_len = ucs2_strlen(ed->name);
145 char name_old[DUMP_NAME_LEN];
148 if (efi_guidcmp(entry->var.VendorGuid, vendor))
151 if (ucs2_strncmp(entry->var.VariableName,
152 efi_name, (size_t)ucs2_len)) {
154 * Check if an old format, which doesn't support
155 * holding multiple logs, remains.
157 sprintf(name_old, "dump-type%u-%u-%lu", ed->type,
158 (unsigned int)ed->id, ed->time.tv_sec);
160 for (i = 0; i < DUMP_NAME_LEN; i++)
161 efi_name_old[i] = name_old[i];
163 if (ucs2_strncmp(entry->var.VariableName, efi_name_old,
164 ucs2_strlen(efi_name_old)))
169 __efivar_entry_delete(entry);
173 static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
174 struct timespec time, struct pstore_info *psi)
176 struct pstore_erase_data edata;
177 struct efivar_entry *entry;
178 char name[DUMP_NAME_LEN];
179 efi_char16_t efi_name[DUMP_NAME_LEN];
182 sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
185 for (i = 0; i < DUMP_NAME_LEN; i++)
186 efi_name[i] = name[i];
192 edata.name = efi_name;
194 efivar_entry_iter_begin();
195 found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
196 efivar_entry_iter_end();
199 efivar_unregister(entry);
204 static struct pstore_info efi_pstore_info = {
205 .owner = THIS_MODULE,
207 .open = efi_pstore_open,
208 .close = efi_pstore_close,
209 .read = efi_pstore_read,
210 .write = efi_pstore_write,
211 .erase = efi_pstore_erase,
214 static __init int efivars_pstore_init(void)
216 if (!efi_enabled(EFI_RUNTIME_SERVICES))
219 if (!efivars_kobject())
222 if (efivars_pstore_disable)
225 efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
226 if (!efi_pstore_info.buf)
229 efi_pstore_info.bufsize = 1024;
230 spin_lock_init(&efi_pstore_info.buf_lock);
232 pstore_register(&efi_pstore_info);
237 static __exit void efivars_pstore_exit(void)
241 module_init(efivars_pstore_init);
242 module_exit(efivars_pstore_exit);
244 MODULE_DESCRIPTION("EFI variable backend for pstore");
245 MODULE_LICENSE("GPL");