efi-pstore.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <linux/efi.h>
  3. #include <linux/module.h>
  4. #include <linux/pstore.h>
  5. #include <linux/slab.h>
  6. #include <linux/ucs2_string.h>
  7. MODULE_IMPORT_NS("EFIVAR");
  8. #define DUMP_NAME_LEN 66
  9. static unsigned int record_size = 1024;
  10. module_param(record_size, uint, 0444);
  11. MODULE_PARM_DESC(record_size, "size of each pstore UEFI var (in bytes, min/default=1024)");
  12. #define PSTORE_EFI_ATTRIBUTES \
  13. (EFI_VARIABLE_NON_VOLATILE | \
  14. EFI_VARIABLE_BOOTSERVICE_ACCESS | \
  15. EFI_VARIABLE_RUNTIME_ACCESS)
  16. static bool pstore_disable = IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
  17. static int efivars_pstore_init(void);
  18. static void efivars_pstore_exit(void);
  19. static int efi_pstore_disable_set(const char *val, const struct kernel_param *kp)
  20. {
  21. int err;
  22. bool old_pstore_disable = pstore_disable;
  23. err = param_set_bool(val, kp);
  24. if (err)
  25. return err;
  26. if (old_pstore_disable != pstore_disable) {
  27. if (pstore_disable)
  28. efivars_pstore_exit();
  29. else
  30. efivars_pstore_init();
  31. }
  32. return 0;
  33. }
  34. static const struct kernel_param_ops pstore_disable_ops = {
  35. .set = efi_pstore_disable_set,
  36. .get = param_get_bool,
  37. };
  38. module_param_cb(pstore_disable, &pstore_disable_ops, &pstore_disable, 0644);
  39. __MODULE_PARM_TYPE(pstore_disable, "bool");
  40. static int efi_pstore_open(struct pstore_info *psi)
  41. {
  42. int err;
  43. err = efivar_lock();
  44. if (err)
  45. return err;
  46. psi->data = kzalloc(record_size, GFP_KERNEL);
  47. if (!psi->data)
  48. return -ENOMEM;
  49. return 0;
  50. }
  51. static int efi_pstore_close(struct pstore_info *psi)
  52. {
  53. efivar_unlock();
  54. kfree(psi->data);
  55. return 0;
  56. }
  57. static inline u64 generic_id(u64 timestamp, unsigned int part, int count)
  58. {
  59. return (timestamp * 100 + part) * 1000 + count;
  60. }
  61. static int efi_pstore_read_func(struct pstore_record *record,
  62. efi_char16_t *varname)
  63. {
  64. unsigned long wlen, size = record_size;
  65. char name[DUMP_NAME_LEN], data_type;
  66. efi_status_t status;
  67. int cnt;
  68. unsigned int part;
  69. u64 time;
  70. ucs2_as_utf8(name, varname, DUMP_NAME_LEN);
  71. if (sscanf(name, "dump-type%u-%u-%d-%llu-%c",
  72. &record->type, &part, &cnt, &time, &data_type) == 5) {
  73. record->id = generic_id(time, part, cnt);
  74. record->part = part;
  75. record->count = cnt;
  76. record->time.tv_sec = time;
  77. record->time.tv_nsec = 0;
  78. if (data_type == 'C')
  79. record->compressed = true;
  80. else
  81. record->compressed = false;
  82. record->ecc_notice_size = 0;
  83. } else if (sscanf(name, "dump-type%u-%u-%d-%llu",
  84. &record->type, &part, &cnt, &time) == 4) {
  85. record->id = generic_id(time, part, cnt);
  86. record->part = part;
  87. record->count = cnt;
  88. record->time.tv_sec = time;
  89. record->time.tv_nsec = 0;
  90. record->compressed = false;
  91. record->ecc_notice_size = 0;
  92. } else if (sscanf(name, "dump-type%u-%u-%llu",
  93. &record->type, &part, &time) == 3) {
  94. /*
  95. * Check if an old format,
  96. * which doesn't support holding
  97. * multiple logs, remains.
  98. */
  99. record->id = generic_id(time, part, 0);
  100. record->part = part;
  101. record->count = 0;
  102. record->time.tv_sec = time;
  103. record->time.tv_nsec = 0;
  104. record->compressed = false;
  105. record->ecc_notice_size = 0;
  106. } else
  107. return 0;
  108. record->buf = kmalloc(size, GFP_KERNEL);
  109. if (!record->buf)
  110. return -ENOMEM;
  111. status = efivar_get_variable(varname, &LINUX_EFI_CRASH_GUID, NULL,
  112. &size, record->buf);
  113. if (status != EFI_SUCCESS) {
  114. kfree(record->buf);
  115. return efi_status_to_err(status);
  116. }
  117. /*
  118. * Store the name of the variable in the pstore_record priv field, so
  119. * we can reuse it later if we need to delete the EFI variable from the
  120. * variable store.
  121. */
  122. wlen = (ucs2_strnlen(varname, DUMP_NAME_LEN) + 1) * sizeof(efi_char16_t);
  123. record->priv = kmemdup(varname, wlen, GFP_KERNEL);
  124. if (!record->priv) {
  125. kfree(record->buf);
  126. return -ENOMEM;
  127. }
  128. return size;
  129. }
  130. static ssize_t efi_pstore_read(struct pstore_record *record)
  131. {
  132. efi_char16_t *varname = record->psi->data;
  133. efi_guid_t guid = LINUX_EFI_CRASH_GUID;
  134. unsigned long varname_size;
  135. efi_status_t status;
  136. for (;;) {
  137. /*
  138. * A small set of old UEFI implementations reject sizes
  139. * above a certain threshold, the lowest seen in the wild
  140. * is 512.
  141. *
  142. * TODO: Commonize with the iteration implementation in
  143. * fs/efivarfs to keep all the quirks in one place.
  144. */
  145. varname_size = 512;
  146. /*
  147. * If this is the first read() call in the pstore enumeration,
  148. * varname will be the empty string, and the GetNextVariable()
  149. * runtime service call will return the first EFI variable in
  150. * its own enumeration order, ignoring the guid argument.
  151. *
  152. * Subsequent calls to GetNextVariable() must pass the name and
  153. * guid values returned by the previous call, which is why we
  154. * store varname in record->psi->data. Given that we only
  155. * enumerate variables with the efi-pstore GUID, there is no
  156. * need to record the guid return value.
  157. */
  158. status = efivar_get_next_variable(&varname_size, varname, &guid);
  159. if (status == EFI_NOT_FOUND)
  160. return 0;
  161. if (status != EFI_SUCCESS)
  162. return efi_status_to_err(status);
  163. /* skip variables that don't concern us */
  164. if (efi_guidcmp(guid, LINUX_EFI_CRASH_GUID))
  165. continue;
  166. return efi_pstore_read_func(record, varname);
  167. }
  168. }
  169. static int efi_pstore_write(struct pstore_record *record)
  170. {
  171. char name[DUMP_NAME_LEN];
  172. efi_char16_t efi_name[DUMP_NAME_LEN];
  173. efi_status_t status;
  174. int i;
  175. record->id = generic_id(record->time.tv_sec, record->part,
  176. record->count);
  177. /* Since we copy the entire length of name, make sure it is wiped. */
  178. memset(name, 0, sizeof(name));
  179. snprintf(name, sizeof(name), "dump-type%u-%u-%d-%lld-%c",
  180. record->type, record->part, record->count,
  181. (long long)record->time.tv_sec,
  182. record->compressed ? 'C' : 'D');
  183. for (i = 0; i < DUMP_NAME_LEN; i++)
  184. efi_name[i] = name[i];
  185. if (efivar_trylock())
  186. return -EBUSY;
  187. status = efivar_set_variable_locked(efi_name, &LINUX_EFI_CRASH_GUID,
  188. PSTORE_EFI_ATTRIBUTES,
  189. record->size, record->psi->buf,
  190. true);
  191. efivar_unlock();
  192. return efi_status_to_err(status);
  193. };
  194. static int efi_pstore_erase(struct pstore_record *record)
  195. {
  196. efi_status_t status;
  197. status = efivar_set_variable(record->priv, &LINUX_EFI_CRASH_GUID,
  198. PSTORE_EFI_ATTRIBUTES, 0, NULL);
  199. if (status != EFI_SUCCESS && status != EFI_NOT_FOUND)
  200. return efi_status_to_err(status);
  201. return 0;
  202. }
  203. static struct pstore_info efi_pstore_info = {
  204. .owner = THIS_MODULE,
  205. .name = KBUILD_MODNAME,
  206. .flags = PSTORE_FLAGS_DMESG,
  207. .open = efi_pstore_open,
  208. .close = efi_pstore_close,
  209. .read = efi_pstore_read,
  210. .write = efi_pstore_write,
  211. .erase = efi_pstore_erase,
  212. };
  213. static int efivars_pstore_init(void)
  214. {
  215. if (!efivar_supports_writes())
  216. return 0;
  217. if (pstore_disable)
  218. return 0;
  219. /*
  220. * Notice that 1024 is the minimum here to prevent issues with
  221. * decompression algorithms that were spotted during tests;
  222. * even in the case of not using compression, smaller values would
  223. * just pollute more the pstore FS with many small collected files.
  224. */
  225. if (record_size < 1024)
  226. record_size = 1024;
  227. efi_pstore_info.buf = kmalloc(record_size, GFP_KERNEL);
  228. if (!efi_pstore_info.buf)
  229. return -ENOMEM;
  230. efi_pstore_info.bufsize = record_size;
  231. if (pstore_register(&efi_pstore_info)) {
  232. kfree(efi_pstore_info.buf);
  233. efi_pstore_info.buf = NULL;
  234. efi_pstore_info.bufsize = 0;
  235. }
  236. return 0;
  237. }
  238. static void efivars_pstore_exit(void)
  239. {
  240. if (!efi_pstore_info.bufsize)
  241. return;
  242. pstore_unregister(&efi_pstore_info);
  243. kfree(efi_pstore_info.buf);
  244. efi_pstore_info.buf = NULL;
  245. efi_pstore_info.bufsize = 0;
  246. }
  247. module_init(efivars_pstore_init);
  248. module_exit(efivars_pstore_exit);
  249. MODULE_DESCRIPTION("EFI variable backend for pstore");
  250. MODULE_LICENSE("GPL");
  251. MODULE_ALIAS("platform:efivars");