Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 1 | #include <linux/efi.h> |
| 2 | #include <linux/module.h> |
| 3 | #include <linux/pstore.h> |
Linus Torvalds | 20b4fb4 | 2013-05-01 17:51:54 -0700 | [diff] [blame] | 4 | #include <linux/slab.h> |
Matt Fleming | a614e19 | 2013-04-30 11:30:24 +0100 | [diff] [blame] | 5 | #include <linux/ucs2_string.h> |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 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 | { |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 21 | psi->data = NULL; |
| 22 | return 0; |
| 23 | } |
| 24 | |
| 25 | static int efi_pstore_close(struct pstore_info *psi) |
| 26 | { |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 27 | psi->data = NULL; |
| 28 | return 0; |
| 29 | } |
| 30 | |
Madper Xie | fdeadb4 | 2013-11-29 15:58:57 +0800 | [diff] [blame] | 31 | static inline u64 generic_id(unsigned long timestamp, |
| 32 | unsigned int part, int count) |
| 33 | { |
Andrzej Zaborowski | 783ee43 | 2014-06-09 16:50:40 +0200 | [diff] [blame] | 34 | return ((u64) timestamp * 100 + part) * 1000 + count; |
Madper Xie | fdeadb4 | 2013-11-29 15:58:57 +0800 | [diff] [blame] | 35 | } |
| 36 | |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 37 | static int efi_pstore_read_func(struct efivar_entry *entry, |
| 38 | struct pstore_record *record) |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 39 | { |
| 40 | efi_guid_t vendor = LINUX_EFI_CRASH_GUID; |
Aruna Balakrishnaiah | f8c62f3 | 2013-08-16 13:57:51 -0700 | [diff] [blame] | 41 | char name[DUMP_NAME_LEN], data_type; |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 42 | int i; |
| 43 | int cnt; |
| 44 | unsigned int part; |
| 45 | unsigned long time, size; |
| 46 | |
| 47 | if (efi_guidcmp(entry->var.VendorGuid, vendor)) |
| 48 | return 0; |
| 49 | |
| 50 | for (i = 0; i < DUMP_NAME_LEN; i++) |
| 51 | name[i] = entry->var.VariableName[i]; |
| 52 | |
Aruna Balakrishnaiah | f8c62f3 | 2013-08-16 13:57:51 -0700 | [diff] [blame] | 53 | if (sscanf(name, "dump-type%u-%u-%d-%lu-%c", |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 54 | &record->type, &part, &cnt, &time, &data_type) == 5) { |
| 55 | record->id = generic_id(time, part, cnt); |
| 56 | record->count = cnt; |
| 57 | record->time.tv_sec = time; |
| 58 | record->time.tv_nsec = 0; |
Aruna Balakrishnaiah | f8c62f3 | 2013-08-16 13:57:51 -0700 | [diff] [blame] | 59 | if (data_type == 'C') |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 60 | record->compressed = true; |
Aruna Balakrishnaiah | f8c62f3 | 2013-08-16 13:57:51 -0700 | [diff] [blame] | 61 | else |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 62 | record->compressed = false; |
| 63 | record->ecc_notice_size = 0; |
Aruna Balakrishnaiah | f8c62f3 | 2013-08-16 13:57:51 -0700 | [diff] [blame] | 64 | } else if (sscanf(name, "dump-type%u-%u-%d-%lu", |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 65 | &record->type, &part, &cnt, &time) == 4) { |
| 66 | record->id = generic_id(time, part, cnt); |
| 67 | record->count = cnt; |
| 68 | record->time.tv_sec = time; |
| 69 | record->time.tv_nsec = 0; |
| 70 | record->compressed = false; |
| 71 | record->ecc_notice_size = 0; |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 72 | } else if (sscanf(name, "dump-type%u-%u-%lu", |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 73 | &record->type, &part, &time) == 3) { |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 74 | /* |
| 75 | * Check if an old format, |
| 76 | * which doesn't support holding |
| 77 | * multiple logs, remains. |
| 78 | */ |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 79 | record->id = generic_id(time, part, 0); |
| 80 | record->count = 0; |
| 81 | record->time.tv_sec = time; |
| 82 | record->time.tv_nsec = 0; |
| 83 | record->compressed = false; |
| 84 | record->ecc_notice_size = 0; |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 85 | } else |
| 86 | return 0; |
| 87 | |
Matt Fleming | 8a415b8 | 2013-04-29 20:08:02 +0100 | [diff] [blame] | 88 | entry->var.DataSize = 1024; |
| 89 | __efivar_entry_get(entry, &entry->var.Attributes, |
| 90 | &entry->var.DataSize, entry->var.Data); |
| 91 | size = entry->var.DataSize; |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 92 | memcpy(record->buf, entry->var.Data, |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 93 | (size_t)min_t(unsigned long, EFIVARS_DATA_SIZE_MAX, size)); |
Matt Fleming | 8a415b8 | 2013-04-29 20:08:02 +0100 | [diff] [blame] | 94 | |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 95 | return size; |
| 96 | } |
| 97 | |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 98 | /** |
| 99 | * efi_pstore_scan_sysfs_enter |
Geliang Tang | a07e744 | 2015-10-31 23:23:14 +0800 | [diff] [blame] | 100 | * @pos: scanning entry |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 101 | * @next: next entry |
| 102 | * @head: list head |
| 103 | */ |
| 104 | static void efi_pstore_scan_sysfs_enter(struct efivar_entry *pos, |
| 105 | struct efivar_entry *next, |
| 106 | struct list_head *head) |
| 107 | { |
| 108 | pos->scanning = true; |
| 109 | if (&next->list != head) |
| 110 | next->scanning = true; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * __efi_pstore_scan_sysfs_exit |
| 115 | * @entry: deleting entry |
| 116 | * @turn_off_scanning: Check if a scanning flag should be turned off |
| 117 | */ |
Sylvain Chouleur | 21b3ddd | 2016-07-15 21:36:30 +0200 | [diff] [blame] | 118 | static inline int __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry, |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 119 | bool turn_off_scanning) |
| 120 | { |
| 121 | if (entry->deleting) { |
| 122 | list_del(&entry->list); |
| 123 | efivar_entry_iter_end(); |
| 124 | efivar_unregister(entry); |
Sylvain Chouleur | 21b3ddd | 2016-07-15 21:36:30 +0200 | [diff] [blame] | 125 | if (efivar_entry_iter_begin()) |
| 126 | return -EINTR; |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 127 | } else if (turn_off_scanning) |
| 128 | entry->scanning = false; |
Sylvain Chouleur | 21b3ddd | 2016-07-15 21:36:30 +0200 | [diff] [blame] | 129 | |
| 130 | return 0; |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
| 134 | * efi_pstore_scan_sysfs_exit |
| 135 | * @pos: scanning entry |
| 136 | * @next: next entry |
| 137 | * @head: list head |
| 138 | * @stop: a flag checking if scanning will stop |
| 139 | */ |
Sylvain Chouleur | 21b3ddd | 2016-07-15 21:36:30 +0200 | [diff] [blame] | 140 | static int efi_pstore_scan_sysfs_exit(struct efivar_entry *pos, |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 141 | struct efivar_entry *next, |
| 142 | struct list_head *head, bool stop) |
| 143 | { |
Sylvain Chouleur | 21b3ddd | 2016-07-15 21:36:30 +0200 | [diff] [blame] | 144 | int ret = __efi_pstore_scan_sysfs_exit(pos, true); |
| 145 | |
| 146 | if (ret) |
| 147 | return ret; |
| 148 | |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 149 | if (stop) |
Sylvain Chouleur | 21b3ddd | 2016-07-15 21:36:30 +0200 | [diff] [blame] | 150 | ret = __efi_pstore_scan_sysfs_exit(next, &next->list != head); |
| 151 | return ret; |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
| 155 | * efi_pstore_sysfs_entry_iter |
| 156 | * |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 157 | * @record: pstore record to pass to callback |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 158 | * |
| 159 | * You MUST call efivar_enter_iter_begin() before this function, and |
| 160 | * efivar_entry_iter_end() afterwards. |
| 161 | * |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 162 | */ |
Kees Cook | 6f61dd3 | 2017-05-12 14:52:34 -0700 | [diff] [blame] | 163 | static int efi_pstore_sysfs_entry_iter(struct pstore_record *record) |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 164 | { |
Kees Cook | 6f61dd3 | 2017-05-12 14:52:34 -0700 | [diff] [blame] | 165 | struct efivar_entry **pos = (struct efivar_entry **)&record->psi->data; |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 166 | struct efivar_entry *entry, *n; |
| 167 | struct list_head *head = &efivar_sysfs_list; |
| 168 | int size = 0; |
Sylvain Chouleur | 21b3ddd | 2016-07-15 21:36:30 +0200 | [diff] [blame] | 169 | int ret; |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 170 | |
| 171 | if (!*pos) { |
| 172 | list_for_each_entry_safe(entry, n, head, list) { |
| 173 | efi_pstore_scan_sysfs_enter(entry, n, head); |
| 174 | |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 175 | size = efi_pstore_read_func(entry, record); |
Sylvain Chouleur | 21b3ddd | 2016-07-15 21:36:30 +0200 | [diff] [blame] | 176 | ret = efi_pstore_scan_sysfs_exit(entry, n, head, |
| 177 | size < 0); |
| 178 | if (ret) |
| 179 | return ret; |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 180 | if (size) |
| 181 | break; |
| 182 | } |
| 183 | *pos = n; |
| 184 | return size; |
| 185 | } |
| 186 | |
| 187 | list_for_each_entry_safe_from((*pos), n, head, list) { |
| 188 | efi_pstore_scan_sysfs_enter((*pos), n, head); |
| 189 | |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 190 | size = efi_pstore_read_func((*pos), record); |
Sylvain Chouleur | 21b3ddd | 2016-07-15 21:36:30 +0200 | [diff] [blame] | 191 | ret = efi_pstore_scan_sysfs_exit((*pos), n, head, size < 0); |
| 192 | if (ret) |
| 193 | return ret; |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 194 | if (size) |
| 195 | break; |
| 196 | } |
| 197 | *pos = n; |
| 198 | return size; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * efi_pstore_read |
| 203 | * |
| 204 | * This function returns a size of NVRAM entry logged via efi_pstore_write(). |
| 205 | * The meaning and behavior of efi_pstore/pstore are as below. |
| 206 | * |
| 207 | * size > 0: Got data of an entry logged via efi_pstore_write() successfully, |
| 208 | * and pstore filesystem will continue reading subsequent entries. |
| 209 | * size == 0: Entry was not logged via efi_pstore_write(), |
| 210 | * and efi_pstore driver will continue reading subsequent entries. |
| 211 | * size < 0: Failed to get data of entry logging via efi_pstore_write(), |
| 212 | * and pstore will stop reading entry. |
| 213 | */ |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 214 | static ssize_t efi_pstore_read(struct pstore_record *record) |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 215 | { |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 216 | ssize_t size; |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 217 | |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 218 | record->buf = kzalloc(EFIVARS_DATA_SIZE_MAX, GFP_KERNEL); |
| 219 | if (!record->buf) |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 220 | return -ENOMEM; |
| 221 | |
Sylvain Chouleur | 21b3ddd | 2016-07-15 21:36:30 +0200 | [diff] [blame] | 222 | if (efivar_entry_iter_begin()) { |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 223 | size = -EINTR; |
| 224 | goto out; |
Sylvain Chouleur | 21b3ddd | 2016-07-15 21:36:30 +0200 | [diff] [blame] | 225 | } |
Kees Cook | 6f61dd3 | 2017-05-12 14:52:34 -0700 | [diff] [blame] | 226 | size = efi_pstore_sysfs_entry_iter(record); |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 227 | efivar_entry_iter_end(); |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 228 | |
| 229 | out: |
| 230 | if (size <= 0) { |
| 231 | kfree(record->buf); |
| 232 | record->buf = NULL; |
| 233 | } |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 234 | return size; |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 237 | static int efi_pstore_write(struct pstore_record *record) |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 238 | { |
| 239 | char name[DUMP_NAME_LEN]; |
| 240 | efi_char16_t efi_name[DUMP_NAME_LEN]; |
| 241 | efi_guid_t vendor = LINUX_EFI_CRASH_GUID; |
| 242 | int i, ret = 0; |
| 243 | |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 244 | snprintf(name, sizeof(name), "dump-type%u-%u-%d-%lu-%c", |
| 245 | record->type, record->part, record->count, |
| 246 | get_seconds(), record->compressed ? 'C' : 'D'); |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 247 | |
| 248 | for (i = 0; i < DUMP_NAME_LEN; i++) |
| 249 | efi_name[i] = name[i]; |
| 250 | |
Evgeny Kalugin | fee929b | 2017-04-04 17:02:42 +0100 | [diff] [blame] | 251 | ret = efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES, |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 252 | !pstore_cannot_block_path(record->reason), |
| 253 | record->size, record->psi->buf); |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 254 | |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 255 | if (record->reason == KMSG_DUMP_OOPS) |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 256 | efivar_run_worker(); |
| 257 | |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 258 | record->id = record->part; |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 259 | return ret; |
| 260 | }; |
| 261 | |
| 262 | struct pstore_erase_data { |
Kees Cook | a61072a | 2017-03-04 23:31:19 -0800 | [diff] [blame] | 263 | struct pstore_record *record; |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 264 | efi_char16_t *name; |
| 265 | }; |
| 266 | |
| 267 | /* |
| 268 | * Clean up an entry with the same name |
| 269 | */ |
| 270 | static int efi_pstore_erase_func(struct efivar_entry *entry, void *data) |
| 271 | { |
| 272 | struct pstore_erase_data *ed = data; |
| 273 | efi_guid_t vendor = LINUX_EFI_CRASH_GUID; |
| 274 | efi_char16_t efi_name_old[DUMP_NAME_LEN]; |
| 275 | efi_char16_t *efi_name = ed->name; |
Matt Fleming | a614e19 | 2013-04-30 11:30:24 +0100 | [diff] [blame] | 276 | unsigned long ucs2_len = ucs2_strlen(ed->name); |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 277 | char name_old[DUMP_NAME_LEN]; |
| 278 | int i; |
| 279 | |
| 280 | if (efi_guidcmp(entry->var.VendorGuid, vendor)) |
| 281 | return 0; |
| 282 | |
Matt Fleming | a614e19 | 2013-04-30 11:30:24 +0100 | [diff] [blame] | 283 | if (ucs2_strncmp(entry->var.VariableName, |
| 284 | efi_name, (size_t)ucs2_len)) { |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 285 | /* |
| 286 | * Check if an old format, which doesn't support |
| 287 | * holding multiple logs, remains. |
| 288 | */ |
Kees Cook | a61072a | 2017-03-04 23:31:19 -0800 | [diff] [blame] | 289 | snprintf(name_old, sizeof(name_old), "dump-type%u-%u-%lu", |
| 290 | ed->record->type, (unsigned int)ed->record->id, |
| 291 | ed->record->time.tv_sec); |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 292 | |
| 293 | for (i = 0; i < DUMP_NAME_LEN; i++) |
| 294 | efi_name_old[i] = name_old[i]; |
| 295 | |
Matt Fleming | a614e19 | 2013-04-30 11:30:24 +0100 | [diff] [blame] | 296 | if (ucs2_strncmp(entry->var.VariableName, efi_name_old, |
| 297 | ucs2_strlen(efi_name_old))) |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 298 | return 0; |
| 299 | } |
| 300 | |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 301 | if (entry->scanning) { |
| 302 | /* |
| 303 | * Skip deletion because this entry will be deleted |
| 304 | * after scanning is completed. |
| 305 | */ |
| 306 | entry->deleting = true; |
| 307 | } else |
| 308 | list_del(&entry->list); |
| 309 | |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 310 | /* found */ |
| 311 | __efivar_entry_delete(entry); |
Matt Fleming | 12abcfd | 2013-04-29 20:06:37 +0100 | [diff] [blame] | 312 | |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 313 | return 1; |
| 314 | } |
| 315 | |
Kees Cook | a61072a | 2017-03-04 23:31:19 -0800 | [diff] [blame] | 316 | static int efi_pstore_erase(struct pstore_record *record) |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 317 | { |
| 318 | struct pstore_erase_data edata; |
Matt Fleming | 4ee39e9 | 2013-04-29 19:31:45 +0100 | [diff] [blame] | 319 | struct efivar_entry *entry = NULL; |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 320 | char name[DUMP_NAME_LEN]; |
| 321 | efi_char16_t efi_name[DUMP_NAME_LEN]; |
| 322 | int found, i; |
Madper Xie | fdeadb4 | 2013-11-29 15:58:57 +0800 | [diff] [blame] | 323 | unsigned int part; |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 324 | |
Kees Cook | a61072a | 2017-03-04 23:31:19 -0800 | [diff] [blame] | 325 | do_div(record->id, 1000); |
| 326 | part = do_div(record->id, 100); |
| 327 | snprintf(name, sizeof(name), "dump-type%u-%u-%d-%lu", |
| 328 | record->type, record->part, record->count, |
| 329 | record->time.tv_sec); |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 330 | |
| 331 | for (i = 0; i < DUMP_NAME_LEN; i++) |
| 332 | efi_name[i] = name[i]; |
| 333 | |
Kees Cook | a61072a | 2017-03-04 23:31:19 -0800 | [diff] [blame] | 334 | edata.record = record; |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 335 | edata.name = efi_name; |
| 336 | |
Sylvain Chouleur | 21b3ddd | 2016-07-15 21:36:30 +0200 | [diff] [blame] | 337 | if (efivar_entry_iter_begin()) |
| 338 | return -EINTR; |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 339 | found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry); |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 340 | |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 341 | if (found && !entry->scanning) { |
| 342 | efivar_entry_iter_end(); |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 343 | efivar_unregister(entry); |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 344 | } else |
| 345 | efivar_entry_iter_end(); |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | static struct pstore_info efi_pstore_info = { |
| 351 | .owner = THIS_MODULE, |
| 352 | .name = "efi", |
Namhyung Kim | c950fd6 | 2016-07-28 00:08:25 +0900 | [diff] [blame] | 353 | .flags = PSTORE_FLAGS_DMESG, |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 354 | .open = efi_pstore_open, |
| 355 | .close = efi_pstore_close, |
| 356 | .read = efi_pstore_read, |
| 357 | .write = efi_pstore_write, |
| 358 | .erase = efi_pstore_erase, |
| 359 | }; |
| 360 | |
| 361 | static __init int efivars_pstore_init(void) |
| 362 | { |
| 363 | if (!efi_enabled(EFI_RUNTIME_SERVICES)) |
| 364 | return 0; |
| 365 | |
| 366 | if (!efivars_kobject()) |
| 367 | return 0; |
| 368 | |
| 369 | if (efivars_pstore_disable) |
| 370 | return 0; |
| 371 | |
| 372 | efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL); |
| 373 | if (!efi_pstore_info.buf) |
| 374 | return -ENOMEM; |
| 375 | |
| 376 | efi_pstore_info.bufsize = 1024; |
| 377 | spin_lock_init(&efi_pstore_info.buf_lock); |
| 378 | |
Lenny Szubowicz | 0d83834 | 2013-06-28 16:14:11 -0400 | [diff] [blame] | 379 | if (pstore_register(&efi_pstore_info)) { |
| 380 | kfree(efi_pstore_info.buf); |
| 381 | efi_pstore_info.buf = NULL; |
| 382 | efi_pstore_info.bufsize = 0; |
| 383 | } |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | static __exit void efivars_pstore_exit(void) |
| 389 | { |
Geliang Tang | cae7316 | 2015-11-07 12:43:48 +0800 | [diff] [blame] | 390 | if (!efi_pstore_info.bufsize) |
| 391 | return; |
| 392 | |
| 393 | pstore_unregister(&efi_pstore_info); |
| 394 | kfree(efi_pstore_info.buf); |
| 395 | efi_pstore_info.buf = NULL; |
| 396 | efi_pstore_info.bufsize = 0; |
Matt Fleming | 0485177 | 2013-02-08 15:48:51 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | module_init(efivars_pstore_init); |
| 400 | module_exit(efivars_pstore_exit); |
| 401 | |
| 402 | MODULE_DESCRIPTION("EFI variable backend for pstore"); |
| 403 | MODULE_LICENSE("GPL"); |
Ben Hutchings | 9ac4d5a | 2015-09-28 01:44:16 +0100 | [diff] [blame] | 404 | MODULE_ALIAS("platform:efivars"); |