blob: af45c42086e1e4c7622bcb9dd5ebb50604ab0bdc [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>
Matt Fleminga614e192013-04-30 11:30:24 +01004#include <linux/ucs2_string.h>
Matt Fleming04851772013-02-08 15:48:51 +00005
6#define DUMP_NAME_LEN 52
7
8static bool efivars_pstore_disable =
9 IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
10
11module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
12
13#define PSTORE_EFI_ATTRIBUTES \
14 (EFI_VARIABLE_NON_VOLATILE | \
15 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
16 EFI_VARIABLE_RUNTIME_ACCESS)
17
18static int efi_pstore_open(struct pstore_info *psi)
19{
20 efivar_entry_iter_begin();
21 psi->data = NULL;
22 return 0;
23}
24
25static int efi_pstore_close(struct pstore_info *psi)
26{
27 efivar_entry_iter_end();
28 psi->data = NULL;
29 return 0;
30}
31
32struct pstore_read_data {
33 u64 *id;
34 enum pstore_type_id *type;
35 int *count;
36 struct timespec *timespec;
37 char **buf;
38};
39
40static int efi_pstore_read_func(struct efivar_entry *entry, void *data)
41{
42 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
43 struct pstore_read_data *cb_data = data;
44 char name[DUMP_NAME_LEN];
45 int i;
46 int cnt;
47 unsigned int part;
48 unsigned long time, size;
49
50 if (efi_guidcmp(entry->var.VendorGuid, vendor))
51 return 0;
52
53 for (i = 0; i < DUMP_NAME_LEN; i++)
54 name[i] = entry->var.VariableName[i];
55
56 if (sscanf(name, "dump-type%u-%u-%d-%lu",
57 cb_data->type, &part, &cnt, &time) == 4) {
58 *cb_data->id = part;
59 *cb_data->count = cnt;
60 cb_data->timespec->tv_sec = time;
61 cb_data->timespec->tv_nsec = 0;
62 } else if (sscanf(name, "dump-type%u-%u-%lu",
63 cb_data->type, &part, &time) == 3) {
64 /*
65 * Check if an old format,
66 * which doesn't support holding
67 * multiple logs, remains.
68 */
69 *cb_data->id = part;
70 *cb_data->count = 0;
71 cb_data->timespec->tv_sec = time;
72 cb_data->timespec->tv_nsec = 0;
73 } else
74 return 0;
75
76 __efivar_entry_size(entry, &size);
77 *cb_data->buf = kmalloc(size, GFP_KERNEL);
78 if (*cb_data->buf == NULL)
79 return -ENOMEM;
80 memcpy(*cb_data->buf, entry->var.Data, size);
81 return size;
82}
83
84static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
85 int *count, struct timespec *timespec,
86 char **buf, struct pstore_info *psi)
87{
88 struct pstore_read_data data;
89
90 data.id = id;
91 data.type = type;
92 data.count = count;
93 data.timespec = timespec;
94 data.buf = buf;
95
96 return __efivar_entry_iter(efi_pstore_read_func, &efivar_sysfs_list, &data,
97 (struct efivar_entry **)&psi->data);
98}
99
100static int efi_pstore_write(enum pstore_type_id type,
101 enum kmsg_dump_reason reason, u64 *id,
102 unsigned int part, int count, size_t size,
103 struct pstore_info *psi)
104{
105 char name[DUMP_NAME_LEN];
106 efi_char16_t efi_name[DUMP_NAME_LEN];
107 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
108 int i, ret = 0;
109
110 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
111 get_seconds());
112
113 for (i = 0; i < DUMP_NAME_LEN; i++)
114 efi_name[i] = name[i];
115
116 efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
117 !pstore_cannot_block_path(reason),
118 size, psi->buf);
119
120 if (reason == KMSG_DUMP_OOPS)
121 efivar_run_worker();
122
123 *id = part;
124 return ret;
125};
126
127struct pstore_erase_data {
128 u64 id;
129 enum pstore_type_id type;
130 int count;
131 struct timespec time;
132 efi_char16_t *name;
133};
134
135/*
136 * Clean up an entry with the same name
137 */
138static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
139{
140 struct pstore_erase_data *ed = data;
141 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
142 efi_char16_t efi_name_old[DUMP_NAME_LEN];
143 efi_char16_t *efi_name = ed->name;
Matt Fleminga614e192013-04-30 11:30:24 +0100144 unsigned long ucs2_len = ucs2_strlen(ed->name);
Matt Fleming04851772013-02-08 15:48:51 +0000145 char name_old[DUMP_NAME_LEN];
146 int i;
147
148 if (efi_guidcmp(entry->var.VendorGuid, vendor))
149 return 0;
150
Matt Fleminga614e192013-04-30 11:30:24 +0100151 if (ucs2_strncmp(entry->var.VariableName,
152 efi_name, (size_t)ucs2_len)) {
Matt Fleming04851772013-02-08 15:48:51 +0000153 /*
154 * Check if an old format, which doesn't support
155 * holding multiple logs, remains.
156 */
157 sprintf(name_old, "dump-type%u-%u-%lu", ed->type,
158 (unsigned int)ed->id, ed->time.tv_sec);
159
160 for (i = 0; i < DUMP_NAME_LEN; i++)
161 efi_name_old[i] = name_old[i];
162
Matt Fleminga614e192013-04-30 11:30:24 +0100163 if (ucs2_strncmp(entry->var.VariableName, efi_name_old,
164 ucs2_strlen(efi_name_old)))
Matt Fleming04851772013-02-08 15:48:51 +0000165 return 0;
166 }
167
168 /* found */
169 __efivar_entry_delete(entry);
Matt Fleming12abcfd2013-04-29 20:06:37 +0100170 list_del(&entry->list);
171
Matt Fleming04851772013-02-08 15:48:51 +0000172 return 1;
173}
174
175static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
176 struct timespec time, struct pstore_info *psi)
177{
178 struct pstore_erase_data edata;
Matt Fleming4ee39e92013-04-29 19:31:45 +0100179 struct efivar_entry *entry = NULL;
Matt Fleming04851772013-02-08 15:48:51 +0000180 char name[DUMP_NAME_LEN];
181 efi_char16_t efi_name[DUMP_NAME_LEN];
182 int found, i;
183
184 sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
185 time.tv_sec);
186
187 for (i = 0; i < DUMP_NAME_LEN; i++)
188 efi_name[i] = name[i];
189
190 edata.id = id;
191 edata.type = type;
192 edata.count = count;
193 edata.time = time;
194 edata.name = efi_name;
195
196 efivar_entry_iter_begin();
197 found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
198 efivar_entry_iter_end();
199
200 if (found)
201 efivar_unregister(entry);
202
203 return 0;
204}
205
206static struct pstore_info efi_pstore_info = {
207 .owner = THIS_MODULE,
208 .name = "efi",
209 .open = efi_pstore_open,
210 .close = efi_pstore_close,
211 .read = efi_pstore_read,
212 .write = efi_pstore_write,
213 .erase = efi_pstore_erase,
214};
215
216static __init int efivars_pstore_init(void)
217{
218 if (!efi_enabled(EFI_RUNTIME_SERVICES))
219 return 0;
220
221 if (!efivars_kobject())
222 return 0;
223
224 if (efivars_pstore_disable)
225 return 0;
226
227 efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
228 if (!efi_pstore_info.buf)
229 return -ENOMEM;
230
231 efi_pstore_info.bufsize = 1024;
232 spin_lock_init(&efi_pstore_info.buf_lock);
233
234 pstore_register(&efi_pstore_info);
235
236 return 0;
237}
238
239static __exit void efivars_pstore_exit(void)
240{
241}
242
243module_init(efivars_pstore_init);
244module_exit(efivars_pstore_exit);
245
246MODULE_DESCRIPTION("EFI variable backend for pstore");
247MODULE_LICENSE("GPL");