efi-pstore.c 9.2 KB

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