1 // SPDX-License-Identifier: GPL-2.0+ 2 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> 8 9 #define DUMP_NAME_LEN 66 10 11 static bool efivars_pstore_disable = 12 IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE); 13 14 module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644); 15 16 #define PSTORE_EFI_ATTRIBUTES \ 17 (EFI_VARIABLE_NON_VOLATILE | \ 18 EFI_VARIABLE_BOOTSERVICE_ACCESS | \ 19 EFI_VARIABLE_RUNTIME_ACCESS) 20 21 static int efi_pstore_open(struct pstore_info *psi) 22 { 23 psi->data = NULL; 24 return 0; 25 } 26 27 static int efi_pstore_close(struct pstore_info *psi) 28 { 29 psi->data = NULL; 30 return 0; 31 } 32 33 static inline u64 generic_id(u64 timestamp, unsigned int part, int count) 34 { 35 return (timestamp * 100 + part) * 1000 + count; 36 } 37 38 static int efi_pstore_read_func(struct efivar_entry *entry, 39 struct pstore_record *record) 40 { 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; 48 49 if (efi_guidcmp(entry->var.VendorGuid, vendor)) 50 return 0; 51 52 for (i = 0; i < DUMP_NAME_LEN; i++) 53 name[i] = entry->var.VariableName[i]; 54 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) { 78 /* 79 * Check if an old format, 80 * which doesn't support holding 81 * multiple logs, remains. 82 */ 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; 92 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)); 99 100 return size; 101 } 102 103 /** 104 * efi_pstore_scan_sysfs_enter 105 * @pos: scanning entry 106 * @next: next entry 107 * @head: list head 108 */ 109 static void efi_pstore_scan_sysfs_enter(struct efivar_entry *pos, 110 struct efivar_entry *next, 111 struct list_head *head) 112 { 113 pos->scanning = true; 114 if (&next->list != head) 115 next->scanning = true; 116 } 117 118 /** 119 * __efi_pstore_scan_sysfs_exit 120 * @entry: deleting entry 121 * @turn_off_scanning: Check if a scanning flag should be turned off 122 */ 123 static inline int __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry, 124 bool turn_off_scanning) 125 { 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; 134 135 return 0; 136 } 137 138 /** 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 144 */ 145 static int efi_pstore_scan_sysfs_exit(struct efivar_entry *pos, 146 struct efivar_entry *next, 147 struct list_head *head, bool stop) 148 { 149 int ret = __efi_pstore_scan_sysfs_exit(pos, true); 150 151 if (ret) 152 return ret; 153 154 if (stop) 155 ret = __efi_pstore_scan_sysfs_exit(next, &next->list != head); 156 return ret; 157 } 158 159 /** 160 * efi_pstore_sysfs_entry_iter 161 * 162 * @record: pstore record to pass to callback 163 * 164 * You MUST call efivar_enter_iter_begin() before this function, and 165 * efivar_entry_iter_end() afterwards. 166 * 167 */ 168 static int efi_pstore_sysfs_entry_iter(struct pstore_record *record) 169 { 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; 175 176 if (!*pos) { 177 list_for_each_entry_safe(entry, n, head, list) { 178 efi_pstore_scan_sysfs_enter(entry, n, head); 179 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; 187 } 188 *pos = n; 189 return size; 190 } 191 192 list_for_each_entry_safe_from((*pos), n, head, list) { 193 efi_pstore_scan_sysfs_enter((*pos), n, head); 194 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; 201 } 202 *pos = n; 203 return size; 204 } 205 206 /** 207 * efi_pstore_read 208 * 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. 211 * 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. 218 */ 219 static ssize_t efi_pstore_read(struct pstore_record *record) 220 { 221 ssize_t size; 222 223 record->buf = kzalloc(EFIVARS_DATA_SIZE_MAX, GFP_KERNEL); 224 if (!record->buf) 225 return -ENOMEM; 226 227 if (efivar_entry_iter_begin()) { 228 size = -EINTR; 229 goto out; 230 } 231 size = efi_pstore_sysfs_entry_iter(record); 232 efivar_entry_iter_end(); 233 234 out: 235 if (size <= 0) { 236 kfree(record->buf); 237 record->buf = NULL; 238 } 239 return size; 240 } 241 242 static int efi_pstore_write(struct pstore_record *record) 243 { 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; 248 249 record->id = generic_id(record->time.tv_sec, record->part, 250 record->count); 251 252 /* Since we copy the entire length of name, make sure it is wiped. */ 253 memset(name, 0, sizeof(name)); 254 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'); 259 260 for (i = 0; i < DUMP_NAME_LEN; i++) 261 efi_name[i] = name[i]; 262 263 ret = efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES, 264 preemptible(), record->size, record->psi->buf); 265 266 if (record->reason == KMSG_DUMP_OOPS) 267 efivar_run_worker(); 268 269 return ret; 270 }; 271 272 /* 273 * Clean up an entry with the same name 274 */ 275 static int efi_pstore_erase_func(struct efivar_entry *entry, void *data) 276 { 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); 280 281 if (efi_guidcmp(entry->var.VendorGuid, vendor)) 282 return 0; 283 284 if (ucs2_strncmp(entry->var.VariableName, efi_name, (size_t)ucs2_len)) 285 return 0; 286 287 if (entry->scanning) { 288 /* 289 * Skip deletion because this entry will be deleted 290 * after scanning is completed. 291 */ 292 entry->deleting = true; 293 } else 294 list_del(&entry->list); 295 296 /* found */ 297 __efivar_entry_delete(entry); 298 299 return 1; 300 } 301 302 static int efi_pstore_erase_name(const char *name) 303 { 304 struct efivar_entry *entry = NULL; 305 efi_char16_t efi_name[DUMP_NAME_LEN]; 306 int found, i; 307 308 for (i = 0; i < DUMP_NAME_LEN; i++) { 309 efi_name[i] = name[i]; 310 if (name[i] == '\0') 311 break; 312 } 313 314 if (efivar_entry_iter_begin()) 315 return -EINTR; 316 317 found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, 318 efi_name, &entry); 319 efivar_entry_iter_end(); 320 321 if (found && !entry->scanning) 322 efivar_unregister(entry); 323 324 return found ? 0 : -ENOENT; 325 } 326 327 static int efi_pstore_erase(struct pstore_record *record) 328 { 329 char name[DUMP_NAME_LEN]; 330 int ret; 331 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; 338 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); 342 343 return ret; 344 } 345 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, 355 }; 356 357 static __init int efivars_pstore_init(void) 358 { 359 if (!efi_enabled(EFI_RUNTIME_SERVICES)) 360 return 0; 361 362 if (!efivars_kobject()) 363 return 0; 364 365 if (efivars_pstore_disable) 366 return 0; 367 368 efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL); 369 if (!efi_pstore_info.buf) 370 return -ENOMEM; 371 372 efi_pstore_info.bufsize = 1024; 373 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; 378 } 379 380 return 0; 381 } 382 383 static __exit void efivars_pstore_exit(void) 384 { 385 if (!efi_pstore_info.bufsize) 386 return; 387 388 pstore_unregister(&efi_pstore_info); 389 kfree(efi_pstore_info.buf); 390 efi_pstore_info.buf = NULL; 391 efi_pstore_info.bufsize = 0; 392 } 393 394 module_init(efivars_pstore_init); 395 module_exit(efivars_pstore_exit); 396 397 MODULE_DESCRIPTION("EFI variable backend for pstore"); 398 MODULE_LICENSE("GPL"); 399 MODULE_ALIAS("platform:efivars"); 400