blob: 743fd426f21bf29299b850833d0137348a2bca64 [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
31struct pstore_read_data {
32 u64 *id;
33 enum pstore_type_id *type;
34 int *count;
35 struct timespec *timespec;
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -070036 bool *compressed;
Matt Fleming04851772013-02-08 15:48:51 +000037 char **buf;
38};
39
Madper Xiefdeadb42013-11-29 15:58:57 +080040static inline u64 generic_id(unsigned long timestamp,
41 unsigned int part, int count)
42{
43 return (timestamp * 100 + part) * 1000 + count;
44}
45
Matt Fleming04851772013-02-08 15:48:51 +000046static int efi_pstore_read_func(struct efivar_entry *entry, void *data)
47{
48 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
49 struct pstore_read_data *cb_data = data;
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -070050 char name[DUMP_NAME_LEN], data_type;
Matt Fleming04851772013-02-08 15:48:51 +000051 int i;
52 int cnt;
53 unsigned int part;
54 unsigned long time, size;
55
56 if (efi_guidcmp(entry->var.VendorGuid, vendor))
57 return 0;
58
59 for (i = 0; i < DUMP_NAME_LEN; i++)
60 name[i] = entry->var.VariableName[i];
61
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -070062 if (sscanf(name, "dump-type%u-%u-%d-%lu-%c",
63 cb_data->type, &part, &cnt, &time, &data_type) == 5) {
Madper Xiefdeadb42013-11-29 15:58:57 +080064 *cb_data->id = generic_id(time, part, cnt);
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -070065 *cb_data->count = cnt;
66 cb_data->timespec->tv_sec = time;
67 cb_data->timespec->tv_nsec = 0;
68 if (data_type == 'C')
69 *cb_data->compressed = true;
70 else
71 *cb_data->compressed = false;
72 } else if (sscanf(name, "dump-type%u-%u-%d-%lu",
Matt Fleming04851772013-02-08 15:48:51 +000073 cb_data->type, &part, &cnt, &time) == 4) {
Madper Xiefdeadb42013-11-29 15:58:57 +080074 *cb_data->id = generic_id(time, part, cnt);
Matt Fleming04851772013-02-08 15:48:51 +000075 *cb_data->count = cnt;
76 cb_data->timespec->tv_sec = time;
77 cb_data->timespec->tv_nsec = 0;
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -070078 *cb_data->compressed = false;
Matt Fleming04851772013-02-08 15:48:51 +000079 } else if (sscanf(name, "dump-type%u-%u-%lu",
80 cb_data->type, &part, &time) == 3) {
81 /*
82 * Check if an old format,
83 * which doesn't support holding
84 * multiple logs, remains.
85 */
Madper Xiefdeadb42013-11-29 15:58:57 +080086 *cb_data->id = generic_id(time, part, 0);
Matt Fleming04851772013-02-08 15:48:51 +000087 *cb_data->count = 0;
88 cb_data->timespec->tv_sec = time;
89 cb_data->timespec->tv_nsec = 0;
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -070090 *cb_data->compressed = false;
Matt Fleming04851772013-02-08 15:48:51 +000091 } else
92 return 0;
93
Matt Fleming8a415b82013-04-29 20:08:02 +010094 entry->var.DataSize = 1024;
95 __efivar_entry_get(entry, &entry->var.Attributes,
96 &entry->var.DataSize, entry->var.Data);
97 size = entry->var.DataSize;
Seiji Aguchie0d59732013-10-30 15:27:26 -040098 memcpy(*cb_data->buf, entry->var.Data,
99 (size_t)min_t(unsigned long, EFIVARS_DATA_SIZE_MAX, size));
Matt Fleming8a415b82013-04-29 20:08:02 +0100100
Matt Fleming04851772013-02-08 15:48:51 +0000101 return size;
102}
103
Seiji Aguchie0d59732013-10-30 15:27:26 -0400104/**
105 * efi_pstore_scan_sysfs_enter
106 * @entry: scanning entry
107 * @next: next entry
108 * @head: list head
109 */
110static void efi_pstore_scan_sysfs_enter(struct efivar_entry *pos,
111 struct efivar_entry *next,
112 struct list_head *head)
113{
114 pos->scanning = true;
115 if (&next->list != head)
116 next->scanning = true;
117}
118
119/**
120 * __efi_pstore_scan_sysfs_exit
121 * @entry: deleting entry
122 * @turn_off_scanning: Check if a scanning flag should be turned off
123 */
124static inline void __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry,
125 bool turn_off_scanning)
126{
127 if (entry->deleting) {
128 list_del(&entry->list);
129 efivar_entry_iter_end();
130 efivar_unregister(entry);
131 efivar_entry_iter_begin();
132 } else if (turn_off_scanning)
133 entry->scanning = false;
134}
135
136/**
137 * efi_pstore_scan_sysfs_exit
138 * @pos: scanning entry
139 * @next: next entry
140 * @head: list head
141 * @stop: a flag checking if scanning will stop
142 */
143static void efi_pstore_scan_sysfs_exit(struct efivar_entry *pos,
144 struct efivar_entry *next,
145 struct list_head *head, bool stop)
146{
147 __efi_pstore_scan_sysfs_exit(pos, true);
148 if (stop)
149 __efi_pstore_scan_sysfs_exit(next, &next->list != head);
150}
151
152/**
153 * efi_pstore_sysfs_entry_iter
154 *
155 * @data: function-specific data to pass to callback
156 * @pos: entry to begin iterating from
157 *
158 * You MUST call efivar_enter_iter_begin() before this function, and
159 * efivar_entry_iter_end() afterwards.
160 *
161 * It is possible to begin iteration from an arbitrary entry within
162 * the list by passing @pos. @pos is updated on return to point to
163 * the next entry of the last one passed to efi_pstore_read_func().
164 * To begin iterating from the beginning of the list @pos must be %NULL.
165 */
166static int efi_pstore_sysfs_entry_iter(void *data, struct efivar_entry **pos)
167{
168 struct efivar_entry *entry, *n;
169 struct list_head *head = &efivar_sysfs_list;
170 int size = 0;
171
172 if (!*pos) {
173 list_for_each_entry_safe(entry, n, head, list) {
174 efi_pstore_scan_sysfs_enter(entry, n, head);
175
176 size = efi_pstore_read_func(entry, data);
177 efi_pstore_scan_sysfs_exit(entry, n, head, size < 0);
178 if (size)
179 break;
180 }
181 *pos = n;
182 return size;
183 }
184
185 list_for_each_entry_safe_from((*pos), n, head, list) {
186 efi_pstore_scan_sysfs_enter((*pos), n, head);
187
188 size = efi_pstore_read_func((*pos), data);
189 efi_pstore_scan_sysfs_exit((*pos), n, head, size < 0);
190 if (size)
191 break;
192 }
193 *pos = n;
194 return size;
195}
196
197/**
198 * efi_pstore_read
199 *
200 * This function returns a size of NVRAM entry logged via efi_pstore_write().
201 * The meaning and behavior of efi_pstore/pstore are as below.
202 *
203 * size > 0: Got data of an entry logged via efi_pstore_write() successfully,
204 * and pstore filesystem will continue reading subsequent entries.
205 * size == 0: Entry was not logged via efi_pstore_write(),
206 * and efi_pstore driver will continue reading subsequent entries.
207 * size < 0: Failed to get data of entry logging via efi_pstore_write(),
208 * and pstore will stop reading entry.
209 */
Matt Fleming04851772013-02-08 15:48:51 +0000210static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
211 int *count, struct timespec *timespec,
Aruna Balakrishnaiah9a4e1392013-08-16 13:53:19 -0700212 char **buf, bool *compressed,
213 struct pstore_info *psi)
Matt Fleming04851772013-02-08 15:48:51 +0000214{
215 struct pstore_read_data data;
Seiji Aguchie0d59732013-10-30 15:27:26 -0400216 ssize_t size;
Matt Fleming04851772013-02-08 15:48:51 +0000217
218 data.id = id;
219 data.type = type;
220 data.count = count;
221 data.timespec = timespec;
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -0700222 data.compressed = compressed;
Matt Fleming04851772013-02-08 15:48:51 +0000223 data.buf = buf;
224
Seiji Aguchie0d59732013-10-30 15:27:26 -0400225 *data.buf = kzalloc(EFIVARS_DATA_SIZE_MAX, GFP_KERNEL);
226 if (!*data.buf)
227 return -ENOMEM;
228
229 efivar_entry_iter_begin();
230 size = efi_pstore_sysfs_entry_iter(&data,
231 (struct efivar_entry **)&psi->data);
232 efivar_entry_iter_end();
233 if (size <= 0)
234 kfree(*data.buf);
235 return size;
Matt Fleming04851772013-02-08 15:48:51 +0000236}
237
238static int efi_pstore_write(enum pstore_type_id type,
239 enum kmsg_dump_reason reason, u64 *id,
Aruna Balakrishnaiahb3b515b2013-08-16 13:52:47 -0700240 unsigned int part, int count, bool compressed, size_t size,
Matt Fleming04851772013-02-08 15:48:51 +0000241 struct pstore_info *psi)
242{
243 char name[DUMP_NAME_LEN];
244 efi_char16_t efi_name[DUMP_NAME_LEN];
245 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
246 int i, ret = 0;
247
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -0700248 sprintf(name, "dump-type%u-%u-%d-%lu-%c", type, part, count,
249 get_seconds(), compressed ? 'C' : 'D');
Matt Fleming04851772013-02-08 15:48:51 +0000250
251 for (i = 0; i < DUMP_NAME_LEN; i++)
252 efi_name[i] = name[i];
253
254 efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
255 !pstore_cannot_block_path(reason),
256 size, psi->buf);
257
258 if (reason == KMSG_DUMP_OOPS)
259 efivar_run_worker();
260
261 *id = part;
262 return ret;
263};
264
265struct pstore_erase_data {
266 u64 id;
267 enum pstore_type_id type;
268 int count;
269 struct timespec time;
270 efi_char16_t *name;
271};
272
273/*
274 * Clean up an entry with the same name
275 */
276static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
277{
278 struct pstore_erase_data *ed = data;
279 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
280 efi_char16_t efi_name_old[DUMP_NAME_LEN];
281 efi_char16_t *efi_name = ed->name;
Matt Fleminga614e192013-04-30 11:30:24 +0100282 unsigned long ucs2_len = ucs2_strlen(ed->name);
Matt Fleming04851772013-02-08 15:48:51 +0000283 char name_old[DUMP_NAME_LEN];
284 int i;
285
286 if (efi_guidcmp(entry->var.VendorGuid, vendor))
287 return 0;
288
Matt Fleminga614e192013-04-30 11:30:24 +0100289 if (ucs2_strncmp(entry->var.VariableName,
290 efi_name, (size_t)ucs2_len)) {
Matt Fleming04851772013-02-08 15:48:51 +0000291 /*
292 * Check if an old format, which doesn't support
293 * holding multiple logs, remains.
294 */
295 sprintf(name_old, "dump-type%u-%u-%lu", ed->type,
296 (unsigned int)ed->id, ed->time.tv_sec);
297
298 for (i = 0; i < DUMP_NAME_LEN; i++)
299 efi_name_old[i] = name_old[i];
300
Matt Fleminga614e192013-04-30 11:30:24 +0100301 if (ucs2_strncmp(entry->var.VariableName, efi_name_old,
302 ucs2_strlen(efi_name_old)))
Matt Fleming04851772013-02-08 15:48:51 +0000303 return 0;
304 }
305
Seiji Aguchie0d59732013-10-30 15:27:26 -0400306 if (entry->scanning) {
307 /*
308 * Skip deletion because this entry will be deleted
309 * after scanning is completed.
310 */
311 entry->deleting = true;
312 } else
313 list_del(&entry->list);
314
Matt Fleming04851772013-02-08 15:48:51 +0000315 /* found */
316 __efivar_entry_delete(entry);
Matt Fleming12abcfd2013-04-29 20:06:37 +0100317
Matt Fleming04851772013-02-08 15:48:51 +0000318 return 1;
319}
320
321static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
322 struct timespec time, struct pstore_info *psi)
323{
324 struct pstore_erase_data edata;
Matt Fleming4ee39e92013-04-29 19:31:45 +0100325 struct efivar_entry *entry = NULL;
Matt Fleming04851772013-02-08 15:48:51 +0000326 char name[DUMP_NAME_LEN];
327 efi_char16_t efi_name[DUMP_NAME_LEN];
328 int found, i;
Madper Xiefdeadb42013-11-29 15:58:57 +0800329 unsigned int part;
Matt Fleming04851772013-02-08 15:48:51 +0000330
Madper Xiefdeadb42013-11-29 15:58:57 +0800331 do_div(id, 1000);
332 part = do_div(id, 100);
333 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count, time.tv_sec);
Matt Fleming04851772013-02-08 15:48:51 +0000334
335 for (i = 0; i < DUMP_NAME_LEN; i++)
336 efi_name[i] = name[i];
337
Madper Xiefdeadb42013-11-29 15:58:57 +0800338 edata.id = part;
Matt Fleming04851772013-02-08 15:48:51 +0000339 edata.type = type;
340 edata.count = count;
341 edata.time = time;
342 edata.name = efi_name;
343
344 efivar_entry_iter_begin();
345 found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
Matt Fleming04851772013-02-08 15:48:51 +0000346
Seiji Aguchie0d59732013-10-30 15:27:26 -0400347 if (found && !entry->scanning) {
348 efivar_entry_iter_end();
Matt Fleming04851772013-02-08 15:48:51 +0000349 efivar_unregister(entry);
Seiji Aguchie0d59732013-10-30 15:27:26 -0400350 } else
351 efivar_entry_iter_end();
Matt Fleming04851772013-02-08 15:48:51 +0000352
353 return 0;
354}
355
356static struct pstore_info efi_pstore_info = {
357 .owner = THIS_MODULE,
358 .name = "efi",
359 .open = efi_pstore_open,
360 .close = efi_pstore_close,
361 .read = efi_pstore_read,
362 .write = efi_pstore_write,
363 .erase = efi_pstore_erase,
364};
365
366static __init int efivars_pstore_init(void)
367{
368 if (!efi_enabled(EFI_RUNTIME_SERVICES))
369 return 0;
370
371 if (!efivars_kobject())
372 return 0;
373
374 if (efivars_pstore_disable)
375 return 0;
376
377 efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
378 if (!efi_pstore_info.buf)
379 return -ENOMEM;
380
381 efi_pstore_info.bufsize = 1024;
382 spin_lock_init(&efi_pstore_info.buf_lock);
383
Lenny Szubowicz0d838342013-06-28 16:14:11 -0400384 if (pstore_register(&efi_pstore_info)) {
385 kfree(efi_pstore_info.buf);
386 efi_pstore_info.buf = NULL;
387 efi_pstore_info.bufsize = 0;
388 }
Matt Fleming04851772013-02-08 15:48:51 +0000389
390 return 0;
391}
392
393static __exit void efivars_pstore_exit(void)
394{
395}
396
397module_init(efivars_pstore_init);
398module_exit(efivars_pstore_exit);
399
400MODULE_DESCRIPTION("EFI variable backend for pstore");
401MODULE_LICENSE("GPL");