efi-pstore.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. #define DUMP_NAME_LEN 52
  7. static bool efivars_pstore_disable =
  8. IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
  9. module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
  10. #define PSTORE_EFI_ATTRIBUTES \
  11. (EFI_VARIABLE_NON_VOLATILE | \
  12. EFI_VARIABLE_BOOTSERVICE_ACCESS | \
  13. EFI_VARIABLE_RUNTIME_ACCESS)
  14. static int efi_pstore_open(struct pstore_info *psi)
  15. {
  16. psi->data = NULL;
  17. return 0;
  18. }
  19. static int efi_pstore_close(struct pstore_info *psi)
  20. {
  21. psi->data = NULL;
  22. return 0;
  23. }
  24. struct pstore_read_data {
  25. u64 *id;
  26. enum pstore_type_id *type;
  27. int *count;
  28. struct timespec *timespec;
  29. bool *compressed;
  30. ssize_t *ecc_notice_size;
  31. char **buf;
  32. };
  33. static inline u64 generic_id(unsigned long timestamp,
  34. unsigned int part, int count)
  35. {
  36. return ((u64) timestamp * 100 + part) * 1000 + count;
  37. }
  38. static int efi_pstore_read_func(struct efivar_entry *entry, void *data)
  39. {
  40. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  41. struct pstore_read_data *cb_data = data;
  42. char name[DUMP_NAME_LEN], data_type;
  43. int i;
  44. int cnt;
  45. unsigned int part;
  46. unsigned long time, size;
  47. if (efi_guidcmp(entry->var.VendorGuid, vendor))
  48. return 0;
  49. for (i = 0; i < DUMP_NAME_LEN; i++)
  50. name[i] = entry->var.VariableName[i];
  51. if (sscanf(name, "dump-type%u-%u-%d-%lu-%c",
  52. cb_data->type, &part, &cnt, &time, &data_type) == 5) {
  53. *cb_data->id = generic_id(time, part, cnt);
  54. *cb_data->count = cnt;
  55. cb_data->timespec->tv_sec = time;
  56. cb_data->timespec->tv_nsec = 0;
  57. if (data_type == 'C')
  58. *cb_data->compressed = true;
  59. else
  60. *cb_data->compressed = false;
  61. *cb_data->ecc_notice_size = 0;
  62. } else if (sscanf(name, "dump-type%u-%u-%d-%lu",
  63. cb_data->type, &part, &cnt, &time) == 4) {
  64. *cb_data->id = generic_id(time, part, cnt);
  65. *cb_data->count = cnt;
  66. cb_data->timespec->tv_sec = time;
  67. cb_data->timespec->tv_nsec = 0;
  68. *cb_data->compressed = false;
  69. *cb_data->ecc_notice_size = 0;
  70. } else if (sscanf(name, "dump-type%u-%u-%lu",
  71. cb_data->type, &part, &time) == 3) {
  72. /*
  73. * Check if an old format,
  74. * which doesn't support holding
  75. * multiple logs, remains.
  76. */
  77. *cb_data->id = generic_id(time, part, 0);
  78. *cb_data->count = 0;
  79. cb_data->timespec->tv_sec = time;
  80. cb_data->timespec->tv_nsec = 0;
  81. *cb_data->compressed = false;
  82. *cb_data->ecc_notice_size = 0;
  83. } else
  84. return 0;
  85. entry->var.DataSize = 1024;
  86. __efivar_entry_get(entry, &entry->var.Attributes,
  87. &entry->var.DataSize, entry->var.Data);
  88. size = entry->var.DataSize;
  89. memcpy(*cb_data->buf, entry->var.Data,
  90. (size_t)min_t(unsigned long, EFIVARS_DATA_SIZE_MAX, size));
  91. return size;
  92. }
  93. /**
  94. * efi_pstore_scan_sysfs_enter
  95. * @pos: scanning entry
  96. * @next: next entry
  97. * @head: list head
  98. */
  99. static void efi_pstore_scan_sysfs_enter(struct efivar_entry *pos,
  100. struct efivar_entry *next,
  101. struct list_head *head)
  102. {
  103. pos->scanning = true;
  104. if (&next->list != head)
  105. next->scanning = true;
  106. }
  107. /**
  108. * __efi_pstore_scan_sysfs_exit
  109. * @entry: deleting entry
  110. * @turn_off_scanning: Check if a scanning flag should be turned off
  111. */
  112. static inline void __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry,
  113. bool turn_off_scanning)
  114. {
  115. if (entry->deleting) {
  116. list_del(&entry->list);
  117. efivar_entry_iter_end();
  118. efivar_unregister(entry);
  119. efivar_entry_iter_begin();
  120. } else if (turn_off_scanning)
  121. entry->scanning = false;
  122. }
  123. /**
  124. * efi_pstore_scan_sysfs_exit
  125. * @pos: scanning entry
  126. * @next: next entry
  127. * @head: list head
  128. * @stop: a flag checking if scanning will stop
  129. */
  130. static void efi_pstore_scan_sysfs_exit(struct efivar_entry *pos,
  131. struct efivar_entry *next,
  132. struct list_head *head, bool stop)
  133. {
  134. __efi_pstore_scan_sysfs_exit(pos, true);
  135. if (stop)
  136. __efi_pstore_scan_sysfs_exit(next, &next->list != head);
  137. }
  138. /**
  139. * efi_pstore_sysfs_entry_iter
  140. *
  141. * @data: function-specific data to pass to callback
  142. * @pos: entry to begin iterating from
  143. *
  144. * You MUST call efivar_enter_iter_begin() before this function, and
  145. * efivar_entry_iter_end() afterwards.
  146. *
  147. * It is possible to begin iteration from an arbitrary entry within
  148. * the list by passing @pos. @pos is updated on return to point to
  149. * the next entry of the last one passed to efi_pstore_read_func().
  150. * To begin iterating from the beginning of the list @pos must be %NULL.
  151. */
  152. static int efi_pstore_sysfs_entry_iter(void *data, struct efivar_entry **pos)
  153. {
  154. struct efivar_entry *entry, *n;
  155. struct list_head *head = &efivar_sysfs_list;
  156. int size = 0;
  157. if (!*pos) {
  158. list_for_each_entry_safe(entry, n, head, list) {
  159. efi_pstore_scan_sysfs_enter(entry, n, head);
  160. size = efi_pstore_read_func(entry, data);
  161. efi_pstore_scan_sysfs_exit(entry, n, head, size < 0);
  162. if (size)
  163. break;
  164. }
  165. *pos = n;
  166. return size;
  167. }
  168. list_for_each_entry_safe_from((*pos), n, head, list) {
  169. efi_pstore_scan_sysfs_enter((*pos), n, head);
  170. size = efi_pstore_read_func((*pos), data);
  171. efi_pstore_scan_sysfs_exit((*pos), n, head, size < 0);
  172. if (size)
  173. break;
  174. }
  175. *pos = n;
  176. return size;
  177. }
  178. /**
  179. * efi_pstore_read
  180. *
  181. * This function returns a size of NVRAM entry logged via efi_pstore_write().
  182. * The meaning and behavior of efi_pstore/pstore are as below.
  183. *
  184. * size > 0: Got data of an entry logged via efi_pstore_write() successfully,
  185. * and pstore filesystem will continue reading subsequent entries.
  186. * size == 0: Entry was not logged via efi_pstore_write(),
  187. * and efi_pstore driver will continue reading subsequent entries.
  188. * size < 0: Failed to get data of entry logging via efi_pstore_write(),
  189. * and pstore will stop reading entry.
  190. */
  191. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  192. int *count, struct timespec *timespec,
  193. char **buf, bool *compressed,
  194. ssize_t *ecc_notice_size,
  195. struct pstore_info *psi)
  196. {
  197. struct pstore_read_data data;
  198. ssize_t size;
  199. data.id = id;
  200. data.type = type;
  201. data.count = count;
  202. data.timespec = timespec;
  203. data.compressed = compressed;
  204. data.ecc_notice_size = ecc_notice_size;
  205. data.buf = buf;
  206. *data.buf = kzalloc(EFIVARS_DATA_SIZE_MAX, GFP_KERNEL);
  207. if (!*data.buf)
  208. return -ENOMEM;
  209. efivar_entry_iter_begin();
  210. size = efi_pstore_sysfs_entry_iter(&data,
  211. (struct efivar_entry **)&psi->data);
  212. efivar_entry_iter_end();
  213. if (size <= 0)
  214. kfree(*data.buf);
  215. return size;
  216. }
  217. static int efi_pstore_write(enum pstore_type_id type,
  218. enum kmsg_dump_reason reason, u64 *id,
  219. unsigned int part, int count, bool compressed, size_t size,
  220. struct pstore_info *psi)
  221. {
  222. char name[DUMP_NAME_LEN];
  223. efi_char16_t efi_name[DUMP_NAME_LEN];
  224. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  225. int i, ret = 0;
  226. sprintf(name, "dump-type%u-%u-%d-%lu-%c", type, part, count,
  227. get_seconds(), compressed ? 'C' : 'D');
  228. for (i = 0; i < DUMP_NAME_LEN; i++)
  229. efi_name[i] = name[i];
  230. efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
  231. !pstore_cannot_block_path(reason),
  232. size, psi->buf);
  233. if (reason == KMSG_DUMP_OOPS)
  234. efivar_run_worker();
  235. *id = part;
  236. return ret;
  237. };
  238. struct pstore_erase_data {
  239. u64 id;
  240. enum pstore_type_id type;
  241. int count;
  242. struct timespec time;
  243. efi_char16_t *name;
  244. };
  245. /*
  246. * Clean up an entry with the same name
  247. */
  248. static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
  249. {
  250. struct pstore_erase_data *ed = data;
  251. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  252. efi_char16_t efi_name_old[DUMP_NAME_LEN];
  253. efi_char16_t *efi_name = ed->name;
  254. unsigned long ucs2_len = ucs2_strlen(ed->name);
  255. char name_old[DUMP_NAME_LEN];
  256. int i;
  257. if (efi_guidcmp(entry->var.VendorGuid, vendor))
  258. return 0;
  259. if (ucs2_strncmp(entry->var.VariableName,
  260. efi_name, (size_t)ucs2_len)) {
  261. /*
  262. * Check if an old format, which doesn't support
  263. * holding multiple logs, remains.
  264. */
  265. sprintf(name_old, "dump-type%u-%u-%lu", ed->type,
  266. (unsigned int)ed->id, ed->time.tv_sec);
  267. for (i = 0; i < DUMP_NAME_LEN; i++)
  268. efi_name_old[i] = name_old[i];
  269. if (ucs2_strncmp(entry->var.VariableName, efi_name_old,
  270. ucs2_strlen(efi_name_old)))
  271. return 0;
  272. }
  273. if (entry->scanning) {
  274. /*
  275. * Skip deletion because this entry will be deleted
  276. * after scanning is completed.
  277. */
  278. entry->deleting = true;
  279. } else
  280. list_del(&entry->list);
  281. /* found */
  282. __efivar_entry_delete(entry);
  283. return 1;
  284. }
  285. static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
  286. struct timespec time, struct pstore_info *psi)
  287. {
  288. struct pstore_erase_data edata;
  289. struct efivar_entry *entry = NULL;
  290. char name[DUMP_NAME_LEN];
  291. efi_char16_t efi_name[DUMP_NAME_LEN];
  292. int found, i;
  293. unsigned int part;
  294. do_div(id, 1000);
  295. part = do_div(id, 100);
  296. sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count, time.tv_sec);
  297. for (i = 0; i < DUMP_NAME_LEN; i++)
  298. efi_name[i] = name[i];
  299. edata.id = part;
  300. edata.type = type;
  301. edata.count = count;
  302. edata.time = time;
  303. edata.name = efi_name;
  304. efivar_entry_iter_begin();
  305. found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
  306. if (found && !entry->scanning) {
  307. efivar_entry_iter_end();
  308. efivar_unregister(entry);
  309. } else
  310. efivar_entry_iter_end();
  311. return 0;
  312. }
  313. static struct pstore_info efi_pstore_info = {
  314. .owner = THIS_MODULE,
  315. .name = "efi",
  316. .flags = PSTORE_FLAGS_FRAGILE,
  317. .open = efi_pstore_open,
  318. .close = efi_pstore_close,
  319. .read = efi_pstore_read,
  320. .write = efi_pstore_write,
  321. .erase = efi_pstore_erase,
  322. };
  323. static __init int efivars_pstore_init(void)
  324. {
  325. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  326. return 0;
  327. if (!efivars_kobject())
  328. return 0;
  329. if (efivars_pstore_disable)
  330. return 0;
  331. efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
  332. if (!efi_pstore_info.buf)
  333. return -ENOMEM;
  334. efi_pstore_info.bufsize = 1024;
  335. spin_lock_init(&efi_pstore_info.buf_lock);
  336. if (pstore_register(&efi_pstore_info)) {
  337. kfree(efi_pstore_info.buf);
  338. efi_pstore_info.buf = NULL;
  339. efi_pstore_info.bufsize = 0;
  340. }
  341. return 0;
  342. }
  343. static __exit void efivars_pstore_exit(void)
  344. {
  345. if (!efi_pstore_info.bufsize)
  346. return;
  347. pstore_unregister(&efi_pstore_info);
  348. kfree(efi_pstore_info.buf);
  349. efi_pstore_info.buf = NULL;
  350. efi_pstore_info.bufsize = 0;
  351. }
  352. module_init(efivars_pstore_init);
  353. module_exit(efivars_pstore_exit);
  354. MODULE_DESCRIPTION("EFI variable backend for pstore");
  355. MODULE_LICENSE("GPL");
  356. MODULE_ALIAS("platform:efivars");