dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / firmware / efi / efi-pstore.c
blob9ea13e8d12ec252455c10cf923c24bfb09f9f191
1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/efi.h>
4 #include <linux/module.h>
5 #include <linux/pstore.h>
6 #include <linux/slab.h>
7 #include <linux/ucs2_string.h>
9 #define DUMP_NAME_LEN 66
11 static bool efivars_pstore_disable =
12 IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
14 module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
16 #define PSTORE_EFI_ATTRIBUTES \
17 (EFI_VARIABLE_NON_VOLATILE | \
18 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
19 EFI_VARIABLE_RUNTIME_ACCESS)
21 static int efi_pstore_open(struct pstore_info *psi)
23 psi->data = NULL;
24 return 0;
27 static int efi_pstore_close(struct pstore_info *psi)
29 psi->data = NULL;
30 return 0;
33 static inline u64 generic_id(u64 timestamp, unsigned int part, int count)
35 return (timestamp * 100 + part) * 1000 + count;
38 static int efi_pstore_read_func(struct efivar_entry *entry,
39 struct pstore_record *record)
41 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
42 char name[DUMP_NAME_LEN], data_type;
43 int i;
44 int cnt;
45 unsigned int part;
46 unsigned long size;
47 u64 time;
49 if (efi_guidcmp(entry->var.VendorGuid, vendor))
50 return 0;
52 for (i = 0; i < DUMP_NAME_LEN; i++)
53 name[i] = entry->var.VariableName[i];
55 if (sscanf(name, "dump-type%u-%u-%d-%llu-%c",
56 &record->type, &part, &cnt, &time, &data_type) == 5) {
57 record->id = generic_id(time, part, cnt);
58 record->part = part;
59 record->count = cnt;
60 record->time.tv_sec = time;
61 record->time.tv_nsec = 0;
62 if (data_type == 'C')
63 record->compressed = true;
64 else
65 record->compressed = false;
66 record->ecc_notice_size = 0;
67 } else if (sscanf(name, "dump-type%u-%u-%d-%llu",
68 &record->type, &part, &cnt, &time) == 4) {
69 record->id = generic_id(time, part, cnt);
70 record->part = part;
71 record->count = cnt;
72 record->time.tv_sec = time;
73 record->time.tv_nsec = 0;
74 record->compressed = false;
75 record->ecc_notice_size = 0;
76 } else if (sscanf(name, "dump-type%u-%u-%llu",
77 &record->type, &part, &time) == 3) {
79 * Check if an old format,
80 * which doesn't support holding
81 * multiple logs, remains.
83 record->id = generic_id(time, part, 0);
84 record->part = part;
85 record->count = 0;
86 record->time.tv_sec = time;
87 record->time.tv_nsec = 0;
88 record->compressed = false;
89 record->ecc_notice_size = 0;
90 } else
91 return 0;
93 entry->var.DataSize = 1024;
94 __efivar_entry_get(entry, &entry->var.Attributes,
95 &entry->var.DataSize, entry->var.Data);
96 size = entry->var.DataSize;
97 memcpy(record->buf, entry->var.Data,
98 (size_t)min_t(unsigned long, EFIVARS_DATA_SIZE_MAX, size));
100 return size;
104 * efi_pstore_scan_sysfs_enter
105 * @pos: scanning entry
106 * @next: next entry
107 * @head: list head
109 static void efi_pstore_scan_sysfs_enter(struct efivar_entry *pos,
110 struct efivar_entry *next,
111 struct list_head *head)
113 pos->scanning = true;
114 if (&next->list != head)
115 next->scanning = true;
119 * __efi_pstore_scan_sysfs_exit
120 * @entry: deleting entry
121 * @turn_off_scanning: Check if a scanning flag should be turned off
123 static inline int __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry,
124 bool turn_off_scanning)
126 if (entry->deleting) {
127 list_del(&entry->list);
128 efivar_entry_iter_end();
129 efivar_unregister(entry);
130 if (efivar_entry_iter_begin())
131 return -EINTR;
132 } else if (turn_off_scanning)
133 entry->scanning = false;
135 return 0;
139 * efi_pstore_scan_sysfs_exit
140 * @pos: scanning entry
141 * @next: next entry
142 * @head: list head
143 * @stop: a flag checking if scanning will stop
145 static int efi_pstore_scan_sysfs_exit(struct efivar_entry *pos,
146 struct efivar_entry *next,
147 struct list_head *head, bool stop)
149 int ret = __efi_pstore_scan_sysfs_exit(pos, true);
151 if (ret)
152 return ret;
154 if (stop)
155 ret = __efi_pstore_scan_sysfs_exit(next, &next->list != head);
156 return ret;
160 * efi_pstore_sysfs_entry_iter
162 * @record: pstore record to pass to callback
164 * You MUST call efivar_enter_iter_begin() before this function, and
165 * efivar_entry_iter_end() afterwards.
168 static int efi_pstore_sysfs_entry_iter(struct pstore_record *record)
170 struct efivar_entry **pos = (struct efivar_entry **)&record->psi->data;
171 struct efivar_entry *entry, *n;
172 struct list_head *head = &efivar_sysfs_list;
173 int size = 0;
174 int ret;
176 if (!*pos) {
177 list_for_each_entry_safe(entry, n, head, list) {
178 efi_pstore_scan_sysfs_enter(entry, n, head);
180 size = efi_pstore_read_func(entry, record);
181 ret = efi_pstore_scan_sysfs_exit(entry, n, head,
182 size < 0);
183 if (ret)
184 return ret;
185 if (size)
186 break;
188 *pos = n;
189 return size;
192 list_for_each_entry_safe_from((*pos), n, head, list) {
193 efi_pstore_scan_sysfs_enter((*pos), n, head);
195 size = efi_pstore_read_func((*pos), record);
196 ret = efi_pstore_scan_sysfs_exit((*pos), n, head, size < 0);
197 if (ret)
198 return ret;
199 if (size)
200 break;
202 *pos = n;
203 return size;
207 * efi_pstore_read
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.
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.
219 static ssize_t efi_pstore_read(struct pstore_record *record)
221 ssize_t size;
223 record->buf = kzalloc(EFIVARS_DATA_SIZE_MAX, GFP_KERNEL);
224 if (!record->buf)
225 return -ENOMEM;
227 if (efivar_entry_iter_begin()) {
228 size = -EINTR;
229 goto out;
231 size = efi_pstore_sysfs_entry_iter(record);
232 efivar_entry_iter_end();
234 out:
235 if (size <= 0) {
236 kfree(record->buf);
237 record->buf = NULL;
239 return size;
242 static int efi_pstore_write(struct pstore_record *record)
244 char name[DUMP_NAME_LEN];
245 efi_char16_t efi_name[DUMP_NAME_LEN];
246 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
247 int i, ret = 0;
249 record->id = generic_id(record->time.tv_sec, record->part,
250 record->count);
252 /* Since we copy the entire length of name, make sure it is wiped. */
253 memset(name, 0, sizeof(name));
255 snprintf(name, sizeof(name), "dump-type%u-%u-%d-%lld-%c",
256 record->type, record->part, record->count,
257 (long long)record->time.tv_sec,
258 record->compressed ? 'C' : 'D');
260 for (i = 0; i < DUMP_NAME_LEN; i++)
261 efi_name[i] = name[i];
263 ret = efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
264 preemptible(), record->size, record->psi->buf);
266 if (record->reason == KMSG_DUMP_OOPS)
267 efivar_run_worker();
269 return ret;
273 * Clean up an entry with the same name
275 static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
277 efi_char16_t *efi_name = data;
278 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
279 unsigned long ucs2_len = ucs2_strlen(efi_name);
281 if (efi_guidcmp(entry->var.VendorGuid, vendor))
282 return 0;
284 if (ucs2_strncmp(entry->var.VariableName, efi_name, (size_t)ucs2_len))
285 return 0;
287 if (entry->scanning) {
289 * Skip deletion because this entry will be deleted
290 * after scanning is completed.
292 entry->deleting = true;
293 } else
294 list_del(&entry->list);
296 /* found */
297 __efivar_entry_delete(entry);
299 return 1;
302 static int efi_pstore_erase_name(const char *name)
304 struct efivar_entry *entry = NULL;
305 efi_char16_t efi_name[DUMP_NAME_LEN];
306 int found, i;
308 for (i = 0; i < DUMP_NAME_LEN; i++) {
309 efi_name[i] = name[i];
310 if (name[i] == '\0')
311 break;
314 if (efivar_entry_iter_begin())
315 return -EINTR;
317 found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list,
318 efi_name, &entry);
319 efivar_entry_iter_end();
321 if (found && !entry->scanning)
322 efivar_unregister(entry);
324 return found ? 0 : -ENOENT;
327 static int efi_pstore_erase(struct pstore_record *record)
329 char name[DUMP_NAME_LEN];
330 int ret;
332 snprintf(name, sizeof(name), "dump-type%u-%u-%d-%lld",
333 record->type, record->part, record->count,
334 (long long)record->time.tv_sec);
335 ret = efi_pstore_erase_name(name);
336 if (ret != -ENOENT)
337 return ret;
339 snprintf(name, sizeof(name), "dump-type%u-%u-%lld",
340 record->type, record->part, (long long)record->time.tv_sec);
341 ret = efi_pstore_erase_name(name);
343 return ret;
346 static struct pstore_info efi_pstore_info = {
347 .owner = THIS_MODULE,
348 .name = "efi",
349 .flags = PSTORE_FLAGS_DMESG,
350 .open = efi_pstore_open,
351 .close = efi_pstore_close,
352 .read = efi_pstore_read,
353 .write = efi_pstore_write,
354 .erase = efi_pstore_erase,
357 static __init int efivars_pstore_init(void)
359 if (!efi_enabled(EFI_RUNTIME_SERVICES))
360 return 0;
362 if (!efivars_kobject())
363 return 0;
365 if (efivars_pstore_disable)
366 return 0;
368 efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
369 if (!efi_pstore_info.buf)
370 return -ENOMEM;
372 efi_pstore_info.bufsize = 1024;
374 if (pstore_register(&efi_pstore_info)) {
375 kfree(efi_pstore_info.buf);
376 efi_pstore_info.buf = NULL;
377 efi_pstore_info.bufsize = 0;
380 return 0;
383 static __exit void efivars_pstore_exit(void)
385 if (!efi_pstore_info.bufsize)
386 return;
388 pstore_unregister(&efi_pstore_info);
389 kfree(efi_pstore_info.buf);
390 efi_pstore_info.buf = NULL;
391 efi_pstore_info.bufsize = 0;
394 module_init(efivars_pstore_init);
395 module_exit(efivars_pstore_exit);
397 MODULE_DESCRIPTION("EFI variable backend for pstore");
398 MODULE_LICENSE("GPL");
399 MODULE_ALIAS("platform:efivars");