blob: 5002d50e37817314d1fe3e449ad991ade1969f6b [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{
21 efivar_entry_iter_begin();
22 psi->data = NULL;
23 return 0;
24}
25
26static int efi_pstore_close(struct pstore_info *psi)
27{
28 efivar_entry_iter_end();
29 psi->data = NULL;
30 return 0;
31}
32
33struct pstore_read_data {
34 u64 *id;
35 enum pstore_type_id *type;
36 int *count;
37 struct timespec *timespec;
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -070038 bool *compressed;
Matt Fleming04851772013-02-08 15:48:51 +000039 char **buf;
40};
41
42static int efi_pstore_read_func(struct efivar_entry *entry, void *data)
43{
44 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
45 struct pstore_read_data *cb_data = data;
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -070046 char name[DUMP_NAME_LEN], data_type;
Matt Fleming04851772013-02-08 15:48:51 +000047 int i;
48 int cnt;
49 unsigned int part;
50 unsigned long time, size;
51
52 if (efi_guidcmp(entry->var.VendorGuid, vendor))
53 return 0;
54
55 for (i = 0; i < DUMP_NAME_LEN; i++)
56 name[i] = entry->var.VariableName[i];
57
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -070058 if (sscanf(name, "dump-type%u-%u-%d-%lu-%c",
59 cb_data->type, &part, &cnt, &time, &data_type) == 5) {
60 *cb_data->id = part;
61 *cb_data->count = cnt;
62 cb_data->timespec->tv_sec = time;
63 cb_data->timespec->tv_nsec = 0;
64 if (data_type == 'C')
65 *cb_data->compressed = true;
66 else
67 *cb_data->compressed = false;
68 } else if (sscanf(name, "dump-type%u-%u-%d-%lu",
Matt Fleming04851772013-02-08 15:48:51 +000069 cb_data->type, &part, &cnt, &time) == 4) {
70 *cb_data->id = part;
71 *cb_data->count = cnt;
72 cb_data->timespec->tv_sec = time;
73 cb_data->timespec->tv_nsec = 0;
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -070074 *cb_data->compressed = false;
Matt Fleming04851772013-02-08 15:48:51 +000075 } else if (sscanf(name, "dump-type%u-%u-%lu",
76 cb_data->type, &part, &time) == 3) {
77 /*
78 * Check if an old format,
79 * which doesn't support holding
80 * multiple logs, remains.
81 */
82 *cb_data->id = part;
83 *cb_data->count = 0;
84 cb_data->timespec->tv_sec = time;
85 cb_data->timespec->tv_nsec = 0;
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -070086 *cb_data->compressed = false;
Matt Fleming04851772013-02-08 15:48:51 +000087 } else
88 return 0;
89
Matt Fleming8a415b82013-04-29 20:08:02 +010090 entry->var.DataSize = 1024;
91 __efivar_entry_get(entry, &entry->var.Attributes,
92 &entry->var.DataSize, entry->var.Data);
93 size = entry->var.DataSize;
94
Thomas Meyer77418922013-06-01 11:40:02 +020095 *cb_data->buf = kmemdup(entry->var.Data, size, GFP_KERNEL);
Matt Fleming04851772013-02-08 15:48:51 +000096 if (*cb_data->buf == NULL)
97 return -ENOMEM;
Matt Fleming04851772013-02-08 15:48:51 +000098 return size;
99}
100
101static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
102 int *count, struct timespec *timespec,
Aruna Balakrishnaiah9a4e1392013-08-16 13:53:19 -0700103 char **buf, bool *compressed,
104 struct pstore_info *psi)
Matt Fleming04851772013-02-08 15:48:51 +0000105{
106 struct pstore_read_data data;
107
108 data.id = id;
109 data.type = type;
110 data.count = count;
111 data.timespec = timespec;
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -0700112 data.compressed = compressed;
Matt Fleming04851772013-02-08 15:48:51 +0000113 data.buf = buf;
114
115 return __efivar_entry_iter(efi_pstore_read_func, &efivar_sysfs_list, &data,
116 (struct efivar_entry **)&psi->data);
117}
118
119static int efi_pstore_write(enum pstore_type_id type,
120 enum kmsg_dump_reason reason, u64 *id,
Aruna Balakrishnaiahb3b515b2013-08-16 13:52:47 -0700121 unsigned int part, int count, bool compressed, size_t size,
Matt Fleming04851772013-02-08 15:48:51 +0000122 struct pstore_info *psi)
123{
124 char name[DUMP_NAME_LEN];
125 efi_char16_t efi_name[DUMP_NAME_LEN];
126 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
127 int i, ret = 0;
128
Aruna Balakrishnaiahf8c62f32013-08-16 13:57:51 -0700129 sprintf(name, "dump-type%u-%u-%d-%lu-%c", type, part, count,
130 get_seconds(), compressed ? 'C' : 'D');
Matt Fleming04851772013-02-08 15:48:51 +0000131
132 for (i = 0; i < DUMP_NAME_LEN; i++)
133 efi_name[i] = name[i];
134
135 efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
136 !pstore_cannot_block_path(reason),
137 size, psi->buf);
138
139 if (reason == KMSG_DUMP_OOPS)
140 efivar_run_worker();
141
142 *id = part;
143 return ret;
144};
145
146struct pstore_erase_data {
147 u64 id;
148 enum pstore_type_id type;
149 int count;
150 struct timespec time;
151 efi_char16_t *name;
152};
153
154/*
155 * Clean up an entry with the same name
156 */
157static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
158{
159 struct pstore_erase_data *ed = data;
160 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
161 efi_char16_t efi_name_old[DUMP_NAME_LEN];
162 efi_char16_t *efi_name = ed->name;
Matt Fleminga614e192013-04-30 11:30:24 +0100163 unsigned long ucs2_len = ucs2_strlen(ed->name);
Matt Fleming04851772013-02-08 15:48:51 +0000164 char name_old[DUMP_NAME_LEN];
165 int i;
166
167 if (efi_guidcmp(entry->var.VendorGuid, vendor))
168 return 0;
169
Matt Fleminga614e192013-04-30 11:30:24 +0100170 if (ucs2_strncmp(entry->var.VariableName,
171 efi_name, (size_t)ucs2_len)) {
Matt Fleming04851772013-02-08 15:48:51 +0000172 /*
173 * Check if an old format, which doesn't support
174 * holding multiple logs, remains.
175 */
176 sprintf(name_old, "dump-type%u-%u-%lu", ed->type,
177 (unsigned int)ed->id, ed->time.tv_sec);
178
179 for (i = 0; i < DUMP_NAME_LEN; i++)
180 efi_name_old[i] = name_old[i];
181
Matt Fleminga614e192013-04-30 11:30:24 +0100182 if (ucs2_strncmp(entry->var.VariableName, efi_name_old,
183 ucs2_strlen(efi_name_old)))
Matt Fleming04851772013-02-08 15:48:51 +0000184 return 0;
185 }
186
187 /* found */
188 __efivar_entry_delete(entry);
Matt Fleming12abcfd2013-04-29 20:06:37 +0100189 list_del(&entry->list);
190
Matt Fleming04851772013-02-08 15:48:51 +0000191 return 1;
192}
193
194static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
195 struct timespec time, struct pstore_info *psi)
196{
197 struct pstore_erase_data edata;
Matt Fleming4ee39e92013-04-29 19:31:45 +0100198 struct efivar_entry *entry = NULL;
Matt Fleming04851772013-02-08 15:48:51 +0000199 char name[DUMP_NAME_LEN];
200 efi_char16_t efi_name[DUMP_NAME_LEN];
201 int found, i;
202
203 sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
204 time.tv_sec);
205
206 for (i = 0; i < DUMP_NAME_LEN; i++)
207 efi_name[i] = name[i];
208
209 edata.id = id;
210 edata.type = type;
211 edata.count = count;
212 edata.time = time;
213 edata.name = efi_name;
214
215 efivar_entry_iter_begin();
216 found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
217 efivar_entry_iter_end();
218
219 if (found)
220 efivar_unregister(entry);
221
222 return 0;
223}
224
225static struct pstore_info efi_pstore_info = {
226 .owner = THIS_MODULE,
227 .name = "efi",
228 .open = efi_pstore_open,
229 .close = efi_pstore_close,
230 .read = efi_pstore_read,
231 .write = efi_pstore_write,
232 .erase = efi_pstore_erase,
233};
234
235static __init int efivars_pstore_init(void)
236{
237 if (!efi_enabled(EFI_RUNTIME_SERVICES))
238 return 0;
239
240 if (!efivars_kobject())
241 return 0;
242
243 if (efivars_pstore_disable)
244 return 0;
245
246 efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
247 if (!efi_pstore_info.buf)
248 return -ENOMEM;
249
250 efi_pstore_info.bufsize = 1024;
251 spin_lock_init(&efi_pstore_info.buf_lock);
252
Lenny Szubowicz0d838342013-06-28 16:14:11 -0400253 if (pstore_register(&efi_pstore_info)) {
254 kfree(efi_pstore_info.buf);
255 efi_pstore_info.buf = NULL;
256 efi_pstore_info.bufsize = 0;
257 }
Matt Fleming04851772013-02-08 15:48:51 +0000258
259 return 0;
260}
261
262static __exit void efivars_pstore_exit(void)
263{
264}
265
266module_init(efivars_pstore_init);
267module_exit(efivars_pstore_exit);
268
269MODULE_DESCRIPTION("EFI variable backend for pstore");
270MODULE_LICENSE("GPL");