1 #include <linux/efi.h> 2 #include <linux/module.h> 3 #include <linux/pstore.h> 4 #include <linux/slab.h> 5 #include <linux/ucs2_string.h> 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 { 21 efivar_entry_iter_begin(); 22 psi->data = NULL; 23 return 0; 24 } 25 26 static int efi_pstore_close(struct pstore_info *psi) 27 { 28 efivar_entry_iter_end(); 29 psi->data = NULL; 30 return 0; 31 } 32 33 struct pstore_read_data { 34 u64 *id; 35 enum pstore_type_id *type; 36 int *count; 37 struct timespec *timespec; 38 bool *compressed; 39 char **buf; 40 }; 41 42 static 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; 46 char name[DUMP_NAME_LEN], data_type; 47 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 58 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", 69 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; 74 *cb_data->compressed = false; 75 } 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; 86 *cb_data->compressed = false; 87 } else 88 return 0; 89 90 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 95 *cb_data->buf = kmemdup(entry->var.Data, size, GFP_KERNEL); 96 if (*cb_data->buf == NULL) 97 return -ENOMEM; 98 return size; 99 } 100 101 static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type, 102 int *count, struct timespec *timespec, 103 char **buf, bool *compressed, 104 struct pstore_info *psi) 105 { 106 struct pstore_read_data data; 107 108 data.id = id; 109 data.type = type; 110 data.count = count; 111 data.timespec = timespec; 112 data.compressed = compressed; 113 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 119 static int efi_pstore_write(enum pstore_type_id type, 120 enum kmsg_dump_reason reason, u64 *id, 121 unsigned int part, int count, bool compressed, size_t size, 122 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 129 sprintf(name, "dump-type%u-%u-%d-%lu-%c", type, part, count, 130 get_seconds(), compressed ? 'C' : 'D'); 131 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 146 struct 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 */ 157 static 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; 163 unsigned long ucs2_len = ucs2_strlen(ed->name); 164 char name_old[DUMP_NAME_LEN]; 165 int i; 166 167 if (efi_guidcmp(entry->var.VendorGuid, vendor)) 168 return 0; 169 170 if (ucs2_strncmp(entry->var.VariableName, 171 efi_name, (size_t)ucs2_len)) { 172 /* 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 182 if (ucs2_strncmp(entry->var.VariableName, efi_name_old, 183 ucs2_strlen(efi_name_old))) 184 return 0; 185 } 186 187 /* found */ 188 __efivar_entry_delete(entry); 189 list_del(&entry->list); 190 191 return 1; 192 } 193 194 static 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; 198 struct efivar_entry *entry = NULL; 199 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 225 static 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 235 static __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 253 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 } 258 259 return 0; 260 } 261 262 static __exit void efivars_pstore_exit(void) 263 { 264 } 265 266 module_init(efivars_pstore_init); 267 module_exit(efivars_pstore_exit); 268 269 MODULE_DESCRIPTION("EFI variable backend for pstore"); 270 MODULE_LICENSE("GPL"); 271