Merge remote-tracking branch 'lsk/v3.10/topic/gator' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / firmware / efi / efi-pstore.c
1 #include <linux/efi.h>
2 #include <linux/module.h>
3 #include <linux/pstore.h>
4 #include <linux/slab.h>
5 #include <linux/ucs2_string.h>
6
7 #define DUMP_NAME_LEN 52
8
9 static bool efivars_pstore_disable =
10         IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
11
12 module_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
19 static int efi_pstore_open(struct pstore_info *psi)
20 {
21         psi->data = NULL;
22         return 0;
23 }
24
25 static int efi_pstore_close(struct pstore_info *psi)
26 {
27         psi->data = NULL;
28         return 0;
29 }
30
31 struct pstore_read_data {
32         u64 *id;
33         enum pstore_type_id *type;
34         int *count;
35         struct timespec *timespec;
36         char **buf;
37 };
38
39 static int efi_pstore_read_func(struct efivar_entry *entry, void *data)
40 {
41         efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
42         struct pstore_read_data *cb_data = data;
43         char name[DUMP_NAME_LEN];
44         int i;
45         int cnt;
46         unsigned int part;
47         unsigned long time, size;
48
49         if (efi_guidcmp(entry->var.VendorGuid, vendor))
50                 return 0;
51
52         for (i = 0; i < DUMP_NAME_LEN; i++)
53                 name[i] = entry->var.VariableName[i];
54
55         if (sscanf(name, "dump-type%u-%u-%d-%lu",
56                    cb_data->type, &part, &cnt, &time) == 4) {
57                 *cb_data->id = part;
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) {
63                 /*
64                  * Check if an old format,
65                  * which doesn't support holding
66                  * multiple logs, remains.
67                  */
68                 *cb_data->id = part;
69                 *cb_data->count = 0;
70                 cb_data->timespec->tv_sec = time;
71                 cb_data->timespec->tv_nsec = 0;
72         } else
73                 return 0;
74
75         entry->var.DataSize = 1024;
76         __efivar_entry_get(entry, &entry->var.Attributes,
77                            &entry->var.DataSize, entry->var.Data);
78         size = entry->var.DataSize;
79         memcpy(*cb_data->buf, entry->var.Data,
80                (size_t)min_t(unsigned long, EFIVARS_DATA_SIZE_MAX, size));
81
82         return size;
83 }
84
85 /**
86  * efi_pstore_scan_sysfs_enter
87  * @entry: scanning entry
88  * @next: next entry
89  * @head: list head
90  */
91 static void efi_pstore_scan_sysfs_enter(struct efivar_entry *pos,
92                                         struct efivar_entry *next,
93                                         struct list_head *head)
94 {
95         pos->scanning = true;
96         if (&next->list != head)
97                 next->scanning = true;
98 }
99
100 /**
101  * __efi_pstore_scan_sysfs_exit
102  * @entry: deleting entry
103  * @turn_off_scanning: Check if a scanning flag should be turned off
104  */
105 static inline void __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry,
106                                                 bool turn_off_scanning)
107 {
108         if (entry->deleting) {
109                 list_del(&entry->list);
110                 efivar_entry_iter_end();
111                 efivar_unregister(entry);
112                 efivar_entry_iter_begin();
113         } else if (turn_off_scanning)
114                 entry->scanning = false;
115 }
116
117 /**
118  * efi_pstore_scan_sysfs_exit
119  * @pos: scanning entry
120  * @next: next entry
121  * @head: list head
122  * @stop: a flag checking if scanning will stop
123  */
124 static void efi_pstore_scan_sysfs_exit(struct efivar_entry *pos,
125                                        struct efivar_entry *next,
126                                        struct list_head *head, bool stop)
127 {
128         __efi_pstore_scan_sysfs_exit(pos, true);
129         if (stop)
130                 __efi_pstore_scan_sysfs_exit(next, &next->list != head);
131 }
132
133 /**
134  * efi_pstore_sysfs_entry_iter
135  *
136  * @data: function-specific data to pass to callback
137  * @pos: entry to begin iterating from
138  *
139  * You MUST call efivar_enter_iter_begin() before this function, and
140  * efivar_entry_iter_end() afterwards.
141  *
142  * It is possible to begin iteration from an arbitrary entry within
143  * the list by passing @pos. @pos is updated on return to point to
144  * the next entry of the last one passed to efi_pstore_read_func().
145  * To begin iterating from the beginning of the list @pos must be %NULL.
146  */
147 static int efi_pstore_sysfs_entry_iter(void *data, struct efivar_entry **pos)
148 {
149         struct efivar_entry *entry, *n;
150         struct list_head *head = &efivar_sysfs_list;
151         int size = 0;
152
153         if (!*pos) {
154                 list_for_each_entry_safe(entry, n, head, list) {
155                         efi_pstore_scan_sysfs_enter(entry, n, head);
156
157                         size = efi_pstore_read_func(entry, data);
158                         efi_pstore_scan_sysfs_exit(entry, n, head, size < 0);
159                         if (size)
160                                 break;
161                 }
162                 *pos = n;
163                 return size;
164         }
165
166         list_for_each_entry_safe_from((*pos), n, head, list) {
167                 efi_pstore_scan_sysfs_enter((*pos), n, head);
168
169                 size = efi_pstore_read_func((*pos), data);
170                 efi_pstore_scan_sysfs_exit((*pos), n, head, size < 0);
171                 if (size)
172                         break;
173         }
174         *pos = n;
175         return size;
176 }
177
178 /**
179  * efi_pstore_read
180  *
181  * This function returns a size of NVRAM entry logged via efi_pstore_write().
182  * The meaning and behavior of efi_pstore/pstore are as below.
183  *
184  * size > 0: Got data of an entry logged via efi_pstore_write() successfully,
185  *           and pstore filesystem will continue reading subsequent entries.
186  * size == 0: Entry was not logged via efi_pstore_write(),
187  *            and efi_pstore driver will continue reading subsequent entries.
188  * size < 0: Failed to get data of entry logging via efi_pstore_write(),
189  *           and pstore will stop reading entry.
190  */
191 static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
192                                int *count, struct timespec *timespec,
193                                char **buf, struct pstore_info *psi)
194 {
195         struct pstore_read_data data;
196         ssize_t size;
197
198         data.id = id;
199         data.type = type;
200         data.count = count;
201         data.timespec = timespec;
202         data.buf = buf;
203
204         *data.buf = kzalloc(EFIVARS_DATA_SIZE_MAX, GFP_KERNEL);
205         if (!*data.buf)
206                 return -ENOMEM;
207
208         efivar_entry_iter_begin();
209         size = efi_pstore_sysfs_entry_iter(&data,
210                                            (struct efivar_entry **)&psi->data);
211         efivar_entry_iter_end();
212         if (size <= 0)
213                 kfree(*data.buf);
214         return size;
215 }
216
217 static int efi_pstore_write(enum pstore_type_id type,
218                 enum kmsg_dump_reason reason, u64 *id,
219                 unsigned int part, int count, size_t size,
220                 struct pstore_info *psi)
221 {
222         char name[DUMP_NAME_LEN];
223         efi_char16_t efi_name[DUMP_NAME_LEN];
224         efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
225         int i, ret = 0;
226
227         sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
228                 get_seconds());
229
230         for (i = 0; i < DUMP_NAME_LEN; i++)
231                 efi_name[i] = name[i];
232
233         efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
234                               !pstore_cannot_block_path(reason),
235                               size, psi->buf);
236
237         if (reason == KMSG_DUMP_OOPS)
238                 efivar_run_worker();
239
240         *id = part;
241         return ret;
242 };
243
244 struct pstore_erase_data {
245         u64 id;
246         enum pstore_type_id type;
247         int count;
248         struct timespec time;
249         efi_char16_t *name;
250 };
251
252 /*
253  * Clean up an entry with the same name
254  */
255 static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
256 {
257         struct pstore_erase_data *ed = data;
258         efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
259         efi_char16_t efi_name_old[DUMP_NAME_LEN];
260         efi_char16_t *efi_name = ed->name;
261         unsigned long ucs2_len = ucs2_strlen(ed->name);
262         char name_old[DUMP_NAME_LEN];
263         int i;
264
265         if (efi_guidcmp(entry->var.VendorGuid, vendor))
266                 return 0;
267
268         if (ucs2_strncmp(entry->var.VariableName,
269                           efi_name, (size_t)ucs2_len)) {
270                 /*
271                  * Check if an old format, which doesn't support
272                  * holding multiple logs, remains.
273                  */
274                 sprintf(name_old, "dump-type%u-%u-%lu", ed->type,
275                         (unsigned int)ed->id, ed->time.tv_sec);
276
277                 for (i = 0; i < DUMP_NAME_LEN; i++)
278                         efi_name_old[i] = name_old[i];
279
280                 if (ucs2_strncmp(entry->var.VariableName, efi_name_old,
281                                   ucs2_strlen(efi_name_old)))
282                         return 0;
283         }
284
285         if (entry->scanning) {
286                 /*
287                  * Skip deletion because this entry will be deleted
288                  * after scanning is completed.
289                  */
290                 entry->deleting = true;
291         } else
292                 list_del(&entry->list);
293
294         /* found */
295         __efivar_entry_delete(entry);
296
297         return 1;
298 }
299
300 static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
301                             struct timespec time, struct pstore_info *psi)
302 {
303         struct pstore_erase_data edata;
304         struct efivar_entry *entry = NULL;
305         char name[DUMP_NAME_LEN];
306         efi_char16_t efi_name[DUMP_NAME_LEN];
307         int found, i;
308
309         sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
310                 time.tv_sec);
311
312         for (i = 0; i < DUMP_NAME_LEN; i++)
313                 efi_name[i] = name[i];
314
315         edata.id = id;
316         edata.type = type;
317         edata.count = count;
318         edata.time = time;
319         edata.name = efi_name;
320
321         efivar_entry_iter_begin();
322         found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
323
324         if (found && !entry->scanning) {
325                 efivar_entry_iter_end();
326                 efivar_unregister(entry);
327         } else
328                 efivar_entry_iter_end();
329
330         return 0;
331 }
332
333 static struct pstore_info efi_pstore_info = {
334         .owner          = THIS_MODULE,
335         .name           = "efi",
336         .open           = efi_pstore_open,
337         .close          = efi_pstore_close,
338         .read           = efi_pstore_read,
339         .write          = efi_pstore_write,
340         .erase          = efi_pstore_erase,
341 };
342
343 static __init int efivars_pstore_init(void)
344 {
345         if (!efi_enabled(EFI_RUNTIME_SERVICES))
346                 return 0;
347
348         if (!efivars_kobject())
349                 return 0;
350
351         if (efivars_pstore_disable)
352                 return 0;
353
354         efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
355         if (!efi_pstore_info.buf)
356                 return -ENOMEM;
357
358         efi_pstore_info.bufsize = 1024;
359         spin_lock_init(&efi_pstore_info.buf_lock);
360
361         if (pstore_register(&efi_pstore_info)) {
362                 kfree(efi_pstore_info.buf);
363                 efi_pstore_info.buf = NULL;
364                 efi_pstore_info.bufsize = 0;
365         }
366
367         return 0;
368 }
369
370 static __exit void efivars_pstore_exit(void)
371 {
372 }
373
374 module_init(efivars_pstore_init);
375 module_exit(efivars_pstore_exit);
376
377 MODULE_DESCRIPTION("EFI variable backend for pstore");
378 MODULE_LICENSE("GPL");