efi-pstore: Make efi-pstore return a unique id
[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         efivar_entry_iter_begin();
22         psi->data = NULL;
23         return 0;
24 }
25
26 static int efi_pstore_close(struct pstore_info *psi)
27 {
28         efivar_entry_iter_end();
29         psi->data = NULL;
30         return 0;
31 }
32
33 struct pstore_read_data {
34         u64 *id;
35         enum pstore_type_id *type;
36         int *count;
37         struct timespec *timespec;
38         char **buf;
39 };
40
41 static inline u64 generic_id(unsigned long timestamp,
42                              unsigned int part, int count)
43 {
44         return (timestamp * 100 + part) * 1000 + count;
45 }
46
47 static int efi_pstore_read_func(struct efivar_entry *entry, void *data)
48 {
49         efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
50         struct pstore_read_data *cb_data = data;
51         char name[DUMP_NAME_LEN];
52         int i;
53         int cnt;
54         unsigned int part;
55         unsigned long time, size;
56
57         if (efi_guidcmp(entry->var.VendorGuid, vendor))
58                 return 0;
59
60         for (i = 0; i < DUMP_NAME_LEN; i++)
61                 name[i] = entry->var.VariableName[i];
62
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) {
71                 /*
72                  * Check if an old format,
73                  * which doesn't support holding
74                  * multiple logs, remains.
75                  */
76                 *cb_data->id = generic_id(time, part, 0);
77                 *cb_data->count = 0;
78                 cb_data->timespec->tv_sec = time;
79                 cb_data->timespec->tv_nsec = 0;
80         } else
81                 return 0;
82
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;
87
88         *cb_data->buf = kmalloc(size, GFP_KERNEL);
89         if (*cb_data->buf == NULL)
90                 return -ENOMEM;
91         memcpy(*cb_data->buf, entry->var.Data, size);
92         return size;
93 }
94
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)
98 {
99         struct pstore_read_data data;
100
101         data.id = id;
102         data.type = type;
103         data.count = count;
104         data.timespec = timespec;
105         data.buf = buf;
106
107         return __efivar_entry_iter(efi_pstore_read_func, &efivar_sysfs_list, &data,
108                                    (struct efivar_entry **)&psi->data);
109 }
110
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)
115 {
116         char name[DUMP_NAME_LEN];
117         efi_char16_t efi_name[DUMP_NAME_LEN];
118         efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
119         int i, ret = 0;
120
121         sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
122                 get_seconds());
123
124         for (i = 0; i < DUMP_NAME_LEN; i++)
125                 efi_name[i] = name[i];
126
127         efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
128                               !pstore_cannot_block_path(reason),
129                               size, psi->buf);
130
131         if (reason == KMSG_DUMP_OOPS)
132                 efivar_run_worker();
133
134         *id = part;
135         return ret;
136 };
137
138 struct pstore_erase_data {
139         u64 id;
140         enum pstore_type_id type;
141         int count;
142         struct timespec time;
143         efi_char16_t *name;
144 };
145
146 /*
147  * Clean up an entry with the same name
148  */
149 static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
150 {
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];
157         int i;
158
159         if (efi_guidcmp(entry->var.VendorGuid, vendor))
160                 return 0;
161
162         if (ucs2_strncmp(entry->var.VariableName,
163                           efi_name, (size_t)ucs2_len)) {
164                 /*
165                  * Check if an old format, which doesn't support
166                  * holding multiple logs, remains.
167                  */
168                 sprintf(name_old, "dump-type%u-%u-%lu", ed->type,
169                         (unsigned int)ed->id, ed->time.tv_sec);
170
171                 for (i = 0; i < DUMP_NAME_LEN; i++)
172                         efi_name_old[i] = name_old[i];
173
174                 if (ucs2_strncmp(entry->var.VariableName, efi_name_old,
175                                   ucs2_strlen(efi_name_old)))
176                         return 0;
177         }
178
179         /* found */
180         __efivar_entry_delete(entry);
181         list_del(&entry->list);
182
183         return 1;
184 }
185
186 static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
187                             struct timespec time, struct pstore_info *psi)
188 {
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];
193         int found, i;
194         unsigned int part;
195
196         do_div(id, 1000);
197         part = do_div(id, 100);
198         sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count, time.tv_sec);
199
200         for (i = 0; i < DUMP_NAME_LEN; i++)
201                 efi_name[i] = name[i];
202
203         edata.id = part;
204         edata.type = type;
205         edata.count = count;
206         edata.time = time;
207         edata.name = efi_name;
208
209         efivar_entry_iter_begin();
210         found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
211         efivar_entry_iter_end();
212
213         if (found)
214                 efivar_unregister(entry);
215
216         return 0;
217 }
218
219 static struct pstore_info efi_pstore_info = {
220         .owner          = THIS_MODULE,
221         .name           = "efi",
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,
227 };
228
229 static __init int efivars_pstore_init(void)
230 {
231         if (!efi_enabled(EFI_RUNTIME_SERVICES))
232                 return 0;
233
234         if (!efivars_kobject())
235                 return 0;
236
237         if (efivars_pstore_disable)
238                 return 0;
239
240         efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
241         if (!efi_pstore_info.buf)
242                 return -ENOMEM;
243
244         efi_pstore_info.bufsize = 1024;
245         spin_lock_init(&efi_pstore_info.buf_lock);
246
247         pstore_register(&efi_pstore_info);
248
249         return 0;
250 }
251
252 static __exit void efivars_pstore_exit(void)
253 {
254 }
255
256 module_init(efivars_pstore_init);
257 module_exit(efivars_pstore_exit);
258
259 MODULE_DESCRIPTION("EFI variable backend for pstore");
260 MODULE_LICENSE("GPL");