efi-pstore.c 9.8 KB

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