blob: bda24129e85ba77cb3960828193a92751d9cf27c [file] [log] [blame]
Matt Fleming04851772013-02-08 15:48:51 +00001#include <linux/efi.h>
2#include <linux/module.h>
3#include <linux/pstore.h>
Linus Torvalds20b4fb42013-05-01 17:51:54 -07004#include <linux/slab.h>
Matt Fleminga614e192013-04-30 11:30:24 +01005#include <linux/ucs2_string.h>
Matt Fleming04851772013-02-08 15:48:51 +00006
7#define DUMP_NAME_LEN 52
8
9static bool efivars_pstore_disable =
10 IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
11
12module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
13
14#define PSTORE_EFI_ATTRIBUTES \
15 (EFI_VARIABLE_NON_VOLATILE | \
16 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
17 EFI_VARIABLE_RUNTIME_ACCESS)
18
19static int efi_pstore_open(struct pstore_info *psi)
20{
Matt Fleming04851772013-02-08 15:48:51 +000021 psi->data = NULL;
22 return 0;
23}
24
25static int efi_pstore_close(struct pstore_info *psi)
26{
Matt Fleming04851772013-02-08 15:48:51 +000027 psi->data = NULL;
28 return 0;
29}
30
Madper Xiefdeadb42013-11-29 15:58:57 +080031static inline u64 generic_id(unsigned long timestamp,
32 unsigned int part, int count)
33{
Andrzej Zaborowski783ee432014-06-09 16:50:40 +020034 return ((u64) timestamp * 100 + part) * 1000 + count;
Madper Xiefdeadb42013-11-29 15:58:57 +080035}
36
Kees Cook125cc422017-03-03 22:09:18 -080037static int efi_pstore_read_func(struct efivar_entry *entry,
38 struct pstore_record *record)
Matt Fleming04851772013-02-08 15:48:51 +000039{
40 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -070041 char name[DUMP_NAME_LEN], data_type;
Matt Fleming04851772013-02-08 15:48:51 +000042 int i;
43 int cnt;
44 unsigned int part;
45 unsigned long time, size;
46
47 if (efi_guidcmp(entry->var.VendorGuid, vendor))
48 return 0;
49
50 for (i = 0; i < DUMP_NAME_LEN; i++)
51 name[i] = entry->var.VariableName[i];
52
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -070053 if (sscanf(name, "dump-type%u-%u-%d-%lu-%c",
Kees Cook125cc422017-03-03 22:09:18 -080054 &record->type, &part, &cnt, &time, &data_type) == 5) {
55 record->id = generic_id(time, part, cnt);
56 record->count = cnt;
57 record->time.tv_sec = time;
58 record->time.tv_nsec = 0;
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -070059 if (data_type == 'C')
Kees Cook125cc422017-03-03 22:09:18 -080060 record->compressed = true;
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -070061 else
Kees Cook125cc422017-03-03 22:09:18 -080062 record->compressed = false;
63 record->ecc_notice_size = 0;
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -070064 } else if (sscanf(name, "dump-type%u-%u-%d-%lu",
Kees Cook125cc422017-03-03 22:09:18 -080065 &record->type, &part, &cnt, &time) == 4) {
66 record->id = generic_id(time, part, cnt);
67 record->count = cnt;
68 record->time.tv_sec = time;
69 record->time.tv_nsec = 0;
70 record->compressed = false;
71 record->ecc_notice_size = 0;
Matt Fleming04851772013-02-08 15:48:51 +000072 } else if (sscanf(name, "dump-type%u-%u-%lu",
Kees Cook125cc422017-03-03 22:09:18 -080073 &record->type, &part, &time) == 3) {
Matt Fleming04851772013-02-08 15:48:51 +000074 /*
75 * Check if an old format,
76 * which doesn't support holding
77 * multiple logs, remains.
78 */
Kees Cook125cc422017-03-03 22:09:18 -080079 record->id = generic_id(time, part, 0);
80 record->count = 0;
81 record->time.tv_sec = time;
82 record->time.tv_nsec = 0;
83 record->compressed = false;
84 record->ecc_notice_size = 0;
Matt Fleming04851772013-02-08 15:48:51 +000085 } else
86 return 0;
87
Matt Fleming8a415b82013-04-29 20:08:02 +010088 entry->var.DataSize = 1024;
89 __efivar_entry_get(entry, &entry->var.Attributes,
90 &entry->var.DataSize, entry->var.Data);
91 size = entry->var.DataSize;
Kees Cook125cc422017-03-03 22:09:18 -080092 memcpy(record->buf, entry->var.Data,
Seiji Aguchie0d59732013-10-30 15:27:26 -040093 (size_t)min_t(unsigned long, EFIVARS_DATA_SIZE_MAX, size));
Matt Fleming8a415b82013-04-29 20:08:02 +010094
Matt Fleming04851772013-02-08 15:48:51 +000095 return size;
96}
97
Seiji Aguchie0d59732013-10-30 15:27:26 -040098/**
99 * efi_pstore_scan_sysfs_enter
Geliang Tanga07e7442015-10-31 23:23:14 +0800100 * @pos: scanning entry
Seiji Aguchie0d59732013-10-30 15:27:26 -0400101 * @next: next entry
102 * @head: list head
103 */
104static void efi_pstore_scan_sysfs_enter(struct efivar_entry *pos,
105 struct efivar_entry *next,
106 struct list_head *head)
107{
108 pos->scanning = true;
109 if (&next->list != head)
110 next->scanning = true;
111}
112
113/**
114 * __efi_pstore_scan_sysfs_exit
115 * @entry: deleting entry
116 * @turn_off_scanning: Check if a scanning flag should be turned off
117 */
Sylvain Chouleur21b3ddd2016-07-15 21:36:30 +0200118static inline int __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry,
Seiji Aguchie0d59732013-10-30 15:27:26 -0400119 bool turn_off_scanning)
120{
121 if (entry->deleting) {
122 list_del(&entry->list);
123 efivar_entry_iter_end();
124 efivar_unregister(entry);
Sylvain Chouleur21b3ddd2016-07-15 21:36:30 +0200125 if (efivar_entry_iter_begin())
126 return -EINTR;
Seiji Aguchie0d59732013-10-30 15:27:26 -0400127 } else if (turn_off_scanning)
128 entry->scanning = false;
Sylvain Chouleur21b3ddd2016-07-15 21:36:30 +0200129
130 return 0;
Seiji Aguchie0d59732013-10-30 15:27:26 -0400131}
132
133/**
134 * efi_pstore_scan_sysfs_exit
135 * @pos: scanning entry
136 * @next: next entry
137 * @head: list head
138 * @stop: a flag checking if scanning will stop
139 */
Sylvain Chouleur21b3ddd2016-07-15 21:36:30 +0200140static int efi_pstore_scan_sysfs_exit(struct efivar_entry *pos,
Seiji Aguchie0d59732013-10-30 15:27:26 -0400141 struct efivar_entry *next,
142 struct list_head *head, bool stop)
143{
Sylvain Chouleur21b3ddd2016-07-15 21:36:30 +0200144 int ret = __efi_pstore_scan_sysfs_exit(pos, true);
145
146 if (ret)
147 return ret;
148
Seiji Aguchie0d59732013-10-30 15:27:26 -0400149 if (stop)
Sylvain Chouleur21b3ddd2016-07-15 21:36:30 +0200150 ret = __efi_pstore_scan_sysfs_exit(next, &next->list != head);
151 return ret;
Seiji Aguchie0d59732013-10-30 15:27:26 -0400152}
153
154/**
155 * efi_pstore_sysfs_entry_iter
156 *
Kees Cook125cc422017-03-03 22:09:18 -0800157 * @record: pstore record to pass to callback
Seiji Aguchie0d59732013-10-30 15:27:26 -0400158 * @pos: entry to begin iterating from
159 *
160 * You MUST call efivar_enter_iter_begin() before this function, and
161 * efivar_entry_iter_end() afterwards.
162 *
163 * It is possible to begin iteration from an arbitrary entry within
164 * the list by passing @pos. @pos is updated on return to point to
165 * the next entry of the last one passed to efi_pstore_read_func().
166 * To begin iterating from the beginning of the list @pos must be %NULL.
167 */
Kees Cook125cc422017-03-03 22:09:18 -0800168static int efi_pstore_sysfs_entry_iter(struct pstore_record *record,
169 struct efivar_entry **pos)
Seiji Aguchie0d59732013-10-30 15:27:26 -0400170{
171 struct efivar_entry *entry, *n;
172 struct list_head *head = &efivar_sysfs_list;
173 int size = 0;
Sylvain Chouleur21b3ddd2016-07-15 21:36:30 +0200174 int ret;
Seiji Aguchie0d59732013-10-30 15:27:26 -0400175
176 if (!*pos) {
177 list_for_each_entry_safe(entry, n, head, list) {
178 efi_pstore_scan_sysfs_enter(entry, n, head);
179
Kees Cook125cc422017-03-03 22:09:18 -0800180 size = efi_pstore_read_func(entry, record);
Sylvain Chouleur21b3ddd2016-07-15 21:36:30 +0200181 ret = efi_pstore_scan_sysfs_exit(entry, n, head,
182 size < 0);
183 if (ret)
184 return ret;
Seiji Aguchie0d59732013-10-30 15:27:26 -0400185 if (size)
186 break;
187 }
188 *pos = n;
189 return size;
190 }
191
192 list_for_each_entry_safe_from((*pos), n, head, list) {
193 efi_pstore_scan_sysfs_enter((*pos), n, head);
194
Kees Cook125cc422017-03-03 22:09:18 -0800195 size = efi_pstore_read_func((*pos), record);
Sylvain Chouleur21b3ddd2016-07-15 21:36:30 +0200196 ret = efi_pstore_scan_sysfs_exit((*pos), n, head, size < 0);
197 if (ret)
198 return ret;
Seiji Aguchie0d59732013-10-30 15:27:26 -0400199 if (size)
200 break;
201 }
202 *pos = n;
203 return size;
204}
205
206/**
207 * efi_pstore_read
208 *
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.
211 *
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.
218 */
Kees Cook125cc422017-03-03 22:09:18 -0800219static ssize_t efi_pstore_read(struct pstore_record *record)
Matt Fleming04851772013-02-08 15:48:51 +0000220{
Kees Cook125cc422017-03-03 22:09:18 -0800221 struct efivar_entry *entry = (struct efivar_entry *)record->psi->data;
Seiji Aguchie0d59732013-10-30 15:27:26 -0400222 ssize_t size;
Matt Fleming04851772013-02-08 15:48:51 +0000223
Kees Cook125cc422017-03-03 22:09:18 -0800224 record->buf = kzalloc(EFIVARS_DATA_SIZE_MAX, GFP_KERNEL);
225 if (!record->buf)
Seiji Aguchie0d59732013-10-30 15:27:26 -0400226 return -ENOMEM;
227
Sylvain Chouleur21b3ddd2016-07-15 21:36:30 +0200228 if (efivar_entry_iter_begin()) {
Kees Cook125cc422017-03-03 22:09:18 -0800229 size = -EINTR;
230 goto out;
Sylvain Chouleur21b3ddd2016-07-15 21:36:30 +0200231 }
Kees Cook125cc422017-03-03 22:09:18 -0800232 size = efi_pstore_sysfs_entry_iter(record, &entry);
Seiji Aguchie0d59732013-10-30 15:27:26 -0400233 efivar_entry_iter_end();
Kees Cook125cc422017-03-03 22:09:18 -0800234
235out:
236 if (size <= 0) {
237 kfree(record->buf);
238 record->buf = NULL;
239 }
Seiji Aguchie0d59732013-10-30 15:27:26 -0400240 return size;
Matt Fleming04851772013-02-08 15:48:51 +0000241}
242
243static int efi_pstore_write(enum pstore_type_id type,
244 enum kmsg_dump_reason reason, u64 *id,
Aruna Balakrishnaiahb3b515b2013-08-16 13:52:47 -0700245 unsigned int part, int count, bool compressed, size_t size,
Matt Fleming04851772013-02-08 15:48:51 +0000246 struct pstore_info *psi)
247{
248 char name[DUMP_NAME_LEN];
249 efi_char16_t efi_name[DUMP_NAME_LEN];
250 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
251 int i, ret = 0;
252
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -0700253 sprintf(name, "dump-type%u-%u-%d-%lu-%c", type, part, count,
254 get_seconds(), compressed ? 'C' : 'D');
Matt Fleming04851772013-02-08 15:48:51 +0000255
256 for (i = 0; i < DUMP_NAME_LEN; i++)
257 efi_name[i] = name[i];
258
259 efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
260 !pstore_cannot_block_path(reason),
261 size, psi->buf);
262
263 if (reason == KMSG_DUMP_OOPS)
264 efivar_run_worker();
265
266 *id = part;
267 return ret;
268};
269
270struct pstore_erase_data {
271 u64 id;
272 enum pstore_type_id type;
273 int count;
274 struct timespec time;
275 efi_char16_t *name;
276};
277
278/*
279 * Clean up an entry with the same name
280 */
281static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
282{
283 struct pstore_erase_data *ed = data;
284 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
285 efi_char16_t efi_name_old[DUMP_NAME_LEN];
286 efi_char16_t *efi_name = ed->name;
Matt Fleminga614e192013-04-30 11:30:24 +0100287 unsigned long ucs2_len = ucs2_strlen(ed->name);
Matt Fleming04851772013-02-08 15:48:51 +0000288 char name_old[DUMP_NAME_LEN];
289 int i;
290
291 if (efi_guidcmp(entry->var.VendorGuid, vendor))
292 return 0;
293
Matt Fleminga614e192013-04-30 11:30:24 +0100294 if (ucs2_strncmp(entry->var.VariableName,
295 efi_name, (size_t)ucs2_len)) {
Matt Fleming04851772013-02-08 15:48:51 +0000296 /*
297 * Check if an old format, which doesn't support
298 * holding multiple logs, remains.
299 */
300 sprintf(name_old, "dump-type%u-%u-%lu", ed->type,
301 (unsigned int)ed->id, ed->time.tv_sec);
302
303 for (i = 0; i < DUMP_NAME_LEN; i++)
304 efi_name_old[i] = name_old[i];
305
Matt Fleminga614e192013-04-30 11:30:24 +0100306 if (ucs2_strncmp(entry->var.VariableName, efi_name_old,
307 ucs2_strlen(efi_name_old)))
Matt Fleming04851772013-02-08 15:48:51 +0000308 return 0;
309 }
310
Seiji Aguchie0d59732013-10-30 15:27:26 -0400311 if (entry->scanning) {
312 /*
313 * Skip deletion because this entry will be deleted
314 * after scanning is completed.
315 */
316 entry->deleting = true;
317 } else
318 list_del(&entry->list);
319
Matt Fleming04851772013-02-08 15:48:51 +0000320 /* found */
321 __efivar_entry_delete(entry);
Matt Fleming12abcfd2013-04-29 20:06:37 +0100322
Matt Fleming04851772013-02-08 15:48:51 +0000323 return 1;
324}
325
326static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
327 struct timespec time, struct pstore_info *psi)
328{
329 struct pstore_erase_data edata;
Matt Fleming4ee39e92013-04-29 19:31:45 +0100330 struct efivar_entry *entry = NULL;
Matt Fleming04851772013-02-08 15:48:51 +0000331 char name[DUMP_NAME_LEN];
332 efi_char16_t efi_name[DUMP_NAME_LEN];
333 int found, i;
Madper Xiefdeadb42013-11-29 15:58:57 +0800334 unsigned int part;
Matt Fleming04851772013-02-08 15:48:51 +0000335
Madper Xiefdeadb42013-11-29 15:58:57 +0800336 do_div(id, 1000);
337 part = do_div(id, 100);
338 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count, time.tv_sec);
Matt Fleming04851772013-02-08 15:48:51 +0000339
340 for (i = 0; i < DUMP_NAME_LEN; i++)
341 efi_name[i] = name[i];
342
Madper Xiefdeadb42013-11-29 15:58:57 +0800343 edata.id = part;
Matt Fleming04851772013-02-08 15:48:51 +0000344 edata.type = type;
345 edata.count = count;
346 edata.time = time;
347 edata.name = efi_name;
348
Sylvain Chouleur21b3ddd2016-07-15 21:36:30 +0200349 if (efivar_entry_iter_begin())
350 return -EINTR;
Matt Fleming04851772013-02-08 15:48:51 +0000351 found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
Matt Fleming04851772013-02-08 15:48:51 +0000352
Seiji Aguchie0d59732013-10-30 15:27:26 -0400353 if (found && !entry->scanning) {
354 efivar_entry_iter_end();
Matt Fleming04851772013-02-08 15:48:51 +0000355 efivar_unregister(entry);
Seiji Aguchie0d59732013-10-30 15:27:26 -0400356 } else
357 efivar_entry_iter_end();
Matt Fleming04851772013-02-08 15:48:51 +0000358
359 return 0;
360}
361
362static struct pstore_info efi_pstore_info = {
363 .owner = THIS_MODULE,
364 .name = "efi",
Namhyung Kimc950fd62016-07-28 00:08:25 +0900365 .flags = PSTORE_FLAGS_DMESG,
Matt Fleming04851772013-02-08 15:48:51 +0000366 .open = efi_pstore_open,
367 .close = efi_pstore_close,
368 .read = efi_pstore_read,
369 .write = efi_pstore_write,
370 .erase = efi_pstore_erase,
371};
372
373static __init int efivars_pstore_init(void)
374{
375 if (!efi_enabled(EFI_RUNTIME_SERVICES))
376 return 0;
377
378 if (!efivars_kobject())
379 return 0;
380
381 if (efivars_pstore_disable)
382 return 0;
383
384 efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
385 if (!efi_pstore_info.buf)
386 return -ENOMEM;
387
388 efi_pstore_info.bufsize = 1024;
389 spin_lock_init(&efi_pstore_info.buf_lock);
390
Lenny Szubowicz0d838342013-06-28 16:14:11 -0400391 if (pstore_register(&efi_pstore_info)) {
392 kfree(efi_pstore_info.buf);
393 efi_pstore_info.buf = NULL;
394 efi_pstore_info.bufsize = 0;
395 }
Matt Fleming04851772013-02-08 15:48:51 +0000396
397 return 0;
398}
399
400static __exit void efivars_pstore_exit(void)
401{
Geliang Tangcae73162015-11-07 12:43:48 +0800402 if (!efi_pstore_info.bufsize)
403 return;
404
405 pstore_unregister(&efi_pstore_info);
406 kfree(efi_pstore_info.buf);
407 efi_pstore_info.buf = NULL;
408 efi_pstore_info.bufsize = 0;
Matt Fleming04851772013-02-08 15:48:51 +0000409}
410
411module_init(efivars_pstore_init);
412module_exit(efivars_pstore_exit);
413
414MODULE_DESCRIPTION("EFI variable backend for pstore");
415MODULE_LICENSE("GPL");
Ben Hutchings9ac4d5a2015-09-28 01:44:16 +0100416MODULE_ALIAS("platform:efivars");