efi-pstore.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 int __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. if (efivar_entry_iter_begin())
  120. return -EINTR;
  121. } else if (turn_off_scanning)
  122. entry->scanning = false;
  123. return 0;
  124. }
  125. /**
  126. * efi_pstore_scan_sysfs_exit
  127. * @pos: scanning entry
  128. * @next: next entry
  129. * @head: list head
  130. * @stop: a flag checking if scanning will stop
  131. */
  132. static int efi_pstore_scan_sysfs_exit(struct efivar_entry *pos,
  133. struct efivar_entry *next,
  134. struct list_head *head, bool stop)
  135. {
  136. int ret = __efi_pstore_scan_sysfs_exit(pos, true);
  137. if (ret)
  138. return ret;
  139. if (stop)
  140. ret = __efi_pstore_scan_sysfs_exit(next, &next->list != head);
  141. return ret;
  142. }
  143. /**
  144. * efi_pstore_sysfs_entry_iter
  145. *
  146. * @data: function-specific data to pass to callback
  147. * @pos: entry to begin iterating from
  148. *
  149. * You MUST call efivar_enter_iter_begin() before this function, and
  150. * efivar_entry_iter_end() afterwards.
  151. *
  152. * It is possible to begin iteration from an arbitrary entry within
  153. * the list by passing @pos. @pos is updated on return to point to
  154. * the next entry of the last one passed to efi_pstore_read_func().
  155. * To begin iterating from the beginning of the list @pos must be %NULL.
  156. */
  157. static int efi_pstore_sysfs_entry_iter(void *data, struct efivar_entry **pos)
  158. {
  159. struct efivar_entry *entry, *n;
  160. struct list_head *head = &efivar_sysfs_list;
  161. int size = 0;
  162. int ret;
  163. if (!*pos) {
  164. list_for_each_entry_safe(entry, n, head, list) {
  165. efi_pstore_scan_sysfs_enter(entry, n, head);
  166. size = efi_pstore_read_func(entry, data);
  167. ret = efi_pstore_scan_sysfs_exit(entry, n, head,
  168. size < 0);
  169. if (ret)
  170. return ret;
  171. if (size)
  172. break;
  173. }
  174. *pos = n;
  175. return size;
  176. }
  177. list_for_each_entry_safe_from((*pos), n, head, list) {
  178. efi_pstore_scan_sysfs_enter((*pos), n, head);
  179. size = efi_pstore_read_func((*pos), data);
  180. ret = efi_pstore_scan_sysfs_exit((*pos), n, head, size < 0);
  181. if (ret)
  182. return ret;
  183. if (size)
  184. break;
  185. }
  186. *pos = n;
  187. return size;
  188. }
  189. /**
  190. * efi_pstore_read
  191. *
  192. * This function returns a size of NVRAM entry logged via efi_pstore_write().
  193. * The meaning and behavior of efi_pstore/pstore are as below.
  194. *
  195. * size > 0: Got data of an entry logged via efi_pstore_write() successfully,
  196. * and pstore filesystem will continue reading subsequent entries.
  197. * size == 0: Entry was not logged via efi_pstore_write(),
  198. * and efi_pstore driver will continue reading subsequent entries.
  199. * size < 0: Failed to get data of entry logging via efi_pstore_write(),
  200. * and pstore will stop reading entry.
  201. */
  202. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  203. int *count, struct timespec *timespec,
  204. char **buf, bool *compressed,
  205. ssize_t *ecc_notice_size,
  206. struct pstore_info *psi)
  207. {
  208. struct pstore_read_data data;
  209. ssize_t size;
  210. data.id = id;
  211. data.type = type;
  212. data.count = count;
  213. data.timespec = timespec;
  214. data.compressed = compressed;
  215. data.ecc_notice_size = ecc_notice_size;
  216. data.buf = buf;
  217. *data.buf = kzalloc(EFIVARS_DATA_SIZE_MAX, GFP_KERNEL);
  218. if (!*data.buf)
  219. return -ENOMEM;
  220. if (efivar_entry_iter_begin()) {
  221. kfree(*data.buf);
  222. return -EINTR;
  223. }
  224. size = efi_pstore_sysfs_entry_iter(&data,
  225. (struct efivar_entry **)&psi->data);
  226. efivar_entry_iter_end();
  227. if (size <= 0)
  228. kfree(*data.buf);
  229. return size;
  230. }
  231. static int efi_pstore_write(enum pstore_type_id type,
  232. enum kmsg_dump_reason reason, u64 *id,
  233. unsigned int part, int count, bool compressed, size_t size,
  234. struct pstore_info *psi)
  235. {
  236. char name[DUMP_NAME_LEN];
  237. efi_char16_t efi_name[DUMP_NAME_LEN];
  238. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  239. int i, ret = 0;
  240. sprintf(name, "dump-type%u-%u-%d-%lu-%c", type, part, count,
  241. get_seconds(), compressed ? 'C' : 'D');
  242. for (i = 0; i < DUMP_NAME_LEN; i++)
  243. efi_name[i] = name[i];
  244. efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
  245. !pstore_cannot_block_path(reason),
  246. size, psi->buf);
  247. if (reason == KMSG_DUMP_OOPS)
  248. efivar_run_worker();
  249. *id = part;
  250. return ret;
  251. };
  252. struct pstore_erase_data {
  253. u64 id;
  254. enum pstore_type_id type;
  255. int count;
  256. struct timespec time;
  257. efi_char16_t *name;
  258. };
  259. /*
  260. * Clean up an entry with the same name
  261. */
  262. static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
  263. {
  264. struct pstore_erase_data *ed = data;
  265. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  266. efi_char16_t efi_name_old[DUMP_NAME_LEN];
  267. efi_char16_t *efi_name = ed->name;
  268. unsigned long ucs2_len = ucs2_strlen(ed->name);
  269. char name_old[DUMP_NAME_LEN];
  270. int i;
  271. if (efi_guidcmp(entry->var.VendorGuid, vendor))
  272. return 0;
  273. if (ucs2_strncmp(entry->var.VariableName,
  274. efi_name, (size_t)ucs2_len)) {
  275. /*
  276. * Check if an old format, which doesn't support
  277. * holding multiple logs, remains.
  278. */
  279. sprintf(name_old, "dump-type%u-%u-%lu", ed->type,
  280. (unsigned int)ed->id, ed->time.tv_sec);
  281. for (i = 0; i < DUMP_NAME_LEN; i++)
  282. efi_name_old[i] = name_old[i];
  283. if (ucs2_strncmp(entry->var.VariableName, efi_name_old,
  284. ucs2_strlen(efi_name_old)))
  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. /* found */
  296. __efivar_entry_delete(entry);
  297. return 1;
  298. }
  299. static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
  300. struct timespec time, struct pstore_info *psi)
  301. {
  302. struct pstore_erase_data edata;
  303. struct efivar_entry *entry = NULL;
  304. char name[DUMP_NAME_LEN];
  305. efi_char16_t efi_name[DUMP_NAME_LEN];
  306. int found, i;
  307. unsigned int part;
  308. do_div(id, 1000);
  309. part = do_div(id, 100);
  310. sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count, time.tv_sec);
  311. for (i = 0; i < DUMP_NAME_LEN; i++)
  312. efi_name[i] = name[i];
  313. edata.id = part;
  314. edata.type = type;
  315. edata.count = count;
  316. edata.time = time;
  317. edata.name = efi_name;
  318. if (efivar_entry_iter_begin())
  319. return -EINTR;
  320. found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
  321. if (found && !entry->scanning) {
  322. efivar_entry_iter_end();
  323. efivar_unregister(entry);
  324. } else
  325. efivar_entry_iter_end();
  326. return 0;
  327. }
  328. static struct pstore_info efi_pstore_info = {
  329. .owner = THIS_MODULE,
  330. .name = "efi",
  331. .flags = PSTORE_FLAGS_DMESG,
  332. .open = efi_pstore_open,
  333. .close = efi_pstore_close,
  334. .read = efi_pstore_read,
  335. .write = efi_pstore_write,
  336. .erase = efi_pstore_erase,
  337. };
  338. static __init int efivars_pstore_init(void)
  339. {
  340. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  341. return 0;
  342. if (!efivars_kobject())
  343. return 0;
  344. if (efivars_pstore_disable)
  345. return 0;
  346. efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
  347. if (!efi_pstore_info.buf)
  348. return -ENOMEM;
  349. efi_pstore_info.bufsize = 1024;
  350. spin_lock_init(&efi_pstore_info.buf_lock);
  351. if (pstore_register(&efi_pstore_info)) {
  352. kfree(efi_pstore_info.buf);
  353. efi_pstore_info.buf = NULL;
  354. efi_pstore_info.bufsize = 0;
  355. }
  356. return 0;
  357. }
  358. static __exit void efivars_pstore_exit(void)
  359. {
  360. if (!efi_pstore_info.bufsize)
  361. return;
  362. pstore_unregister(&efi_pstore_info);
  363. kfree(efi_pstore_info.buf);
  364. efi_pstore_info.buf = NULL;
  365. efi_pstore_info.bufsize = 0;
  366. }
  367. module_init(efivars_pstore_init);
  368. module_exit(efivars_pstore_exit);
  369. MODULE_DESCRIPTION("EFI variable backend for pstore");
  370. MODULE_LICENSE("GPL");
  371. MODULE_ALIAS("platform:efivars");