efi-pstore.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #define EFIVARS_DATA_SIZE_MAX 1024
  10. static bool efivars_pstore_disable =
  11. IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
  12. module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
  13. #define PSTORE_EFI_ATTRIBUTES \
  14. (EFI_VARIABLE_NON_VOLATILE | \
  15. EFI_VARIABLE_BOOTSERVICE_ACCESS | \
  16. EFI_VARIABLE_RUNTIME_ACCESS)
  17. static int efi_pstore_open(struct pstore_info *psi)
  18. {
  19. int err;
  20. err = efivar_lock();
  21. if (err)
  22. return err;
  23. psi->data = kzalloc(EFIVARS_DATA_SIZE_MAX, GFP_KERNEL);
  24. if (!psi->data)
  25. return -ENOMEM;
  26. return 0;
  27. }
  28. static int efi_pstore_close(struct pstore_info *psi)
  29. {
  30. efivar_unlock();
  31. kfree(psi->data);
  32. return 0;
  33. }
  34. static inline u64 generic_id(u64 timestamp, unsigned int part, int count)
  35. {
  36. return (timestamp * 100 + part) * 1000 + count;
  37. }
  38. static int efi_pstore_read_func(struct pstore_record *record,
  39. efi_char16_t *varname)
  40. {
  41. unsigned long wlen, size = EFIVARS_DATA_SIZE_MAX;
  42. char name[DUMP_NAME_LEN], data_type;
  43. efi_status_t status;
  44. int cnt;
  45. unsigned int part;
  46. u64 time;
  47. ucs2_as_utf8(name, varname, DUMP_NAME_LEN);
  48. if (sscanf(name, "dump-type%u-%u-%d-%llu-%c",
  49. &record->type, &part, &cnt, &time, &data_type) == 5) {
  50. record->id = generic_id(time, part, cnt);
  51. record->part = part;
  52. record->count = cnt;
  53. record->time.tv_sec = time;
  54. record->time.tv_nsec = 0;
  55. if (data_type == 'C')
  56. record->compressed = true;
  57. else
  58. record->compressed = false;
  59. record->ecc_notice_size = 0;
  60. } else if (sscanf(name, "dump-type%u-%u-%d-%llu",
  61. &record->type, &part, &cnt, &time) == 4) {
  62. record->id = generic_id(time, part, cnt);
  63. record->part = part;
  64. record->count = cnt;
  65. record->time.tv_sec = time;
  66. record->time.tv_nsec = 0;
  67. record->compressed = false;
  68. record->ecc_notice_size = 0;
  69. } else if (sscanf(name, "dump-type%u-%u-%llu",
  70. &record->type, &part, &time) == 3) {
  71. /*
  72. * Check if an old format,
  73. * which doesn't support holding
  74. * multiple logs, remains.
  75. */
  76. record->id = generic_id(time, part, 0);
  77. record->part = part;
  78. record->count = 0;
  79. record->time.tv_sec = time;
  80. record->time.tv_nsec = 0;
  81. record->compressed = false;
  82. record->ecc_notice_size = 0;
  83. } else
  84. return 0;
  85. record->buf = kmalloc(size, GFP_KERNEL);
  86. if (!record->buf)
  87. return -ENOMEM;
  88. status = efivar_get_variable(varname, &LINUX_EFI_CRASH_GUID, NULL,
  89. &size, record->buf);
  90. if (status != EFI_SUCCESS) {
  91. kfree(record->buf);
  92. return -EIO;
  93. }
  94. /*
  95. * Store the name of the variable in the pstore_record priv field, so
  96. * we can reuse it later if we need to delete the EFI variable from the
  97. * variable store.
  98. */
  99. wlen = (ucs2_strnlen(varname, DUMP_NAME_LEN) + 1) * sizeof(efi_char16_t);
  100. record->priv = kmemdup(varname, wlen, GFP_KERNEL);
  101. if (!record->priv) {
  102. kfree(record->buf);
  103. return -ENOMEM;
  104. }
  105. return size;
  106. }
  107. static ssize_t efi_pstore_read(struct pstore_record *record)
  108. {
  109. efi_char16_t *varname = record->psi->data;
  110. efi_guid_t guid = LINUX_EFI_CRASH_GUID;
  111. unsigned long varname_size;
  112. efi_status_t status;
  113. for (;;) {
  114. varname_size = EFIVARS_DATA_SIZE_MAX;
  115. /*
  116. * If this is the first read() call in the pstore enumeration,
  117. * varname will be the empty string, and the GetNextVariable()
  118. * runtime service call will return the first EFI variable in
  119. * its own enumeration order, ignoring the guid argument.
  120. *
  121. * Subsequent calls to GetNextVariable() must pass the name and
  122. * guid values returned by the previous call, which is why we
  123. * store varname in record->psi->data. Given that we only
  124. * enumerate variables with the efi-pstore GUID, there is no
  125. * need to record the guid return value.
  126. */
  127. status = efivar_get_next_variable(&varname_size, varname, &guid);
  128. if (status == EFI_NOT_FOUND)
  129. return 0;
  130. if (status != EFI_SUCCESS)
  131. return -EIO;
  132. /* skip variables that don't concern us */
  133. if (efi_guidcmp(guid, LINUX_EFI_CRASH_GUID))
  134. continue;
  135. return efi_pstore_read_func(record, varname);
  136. }
  137. }
  138. static int efi_pstore_write(struct pstore_record *record)
  139. {
  140. char name[DUMP_NAME_LEN];
  141. efi_char16_t efi_name[DUMP_NAME_LEN];
  142. efi_status_t status;
  143. int i;
  144. record->id = generic_id(record->time.tv_sec, record->part,
  145. record->count);
  146. /* Since we copy the entire length of name, make sure it is wiped. */
  147. memset(name, 0, sizeof(name));
  148. snprintf(name, sizeof(name), "dump-type%u-%u-%d-%lld-%c",
  149. record->type, record->part, record->count,
  150. (long long)record->time.tv_sec,
  151. record->compressed ? 'C' : 'D');
  152. for (i = 0; i < DUMP_NAME_LEN; i++)
  153. efi_name[i] = name[i];
  154. if (efivar_trylock())
  155. return -EBUSY;
  156. status = efivar_set_variable_locked(efi_name, &LINUX_EFI_CRASH_GUID,
  157. PSTORE_EFI_ATTRIBUTES,
  158. record->size, record->psi->buf,
  159. true);
  160. efivar_unlock();
  161. return status == EFI_SUCCESS ? 0 : -EIO;
  162. };
  163. static int efi_pstore_erase(struct pstore_record *record)
  164. {
  165. efi_status_t status;
  166. status = efivar_set_variable(record->priv, &LINUX_EFI_CRASH_GUID,
  167. PSTORE_EFI_ATTRIBUTES, 0, NULL);
  168. if (status != EFI_SUCCESS && status != EFI_NOT_FOUND)
  169. return -EIO;
  170. return 0;
  171. }
  172. static struct pstore_info efi_pstore_info = {
  173. .owner = THIS_MODULE,
  174. .name = "efi",
  175. .flags = PSTORE_FLAGS_DMESG,
  176. .open = efi_pstore_open,
  177. .close = efi_pstore_close,
  178. .read = efi_pstore_read,
  179. .write = efi_pstore_write,
  180. .erase = efi_pstore_erase,
  181. };
  182. static __init int efivars_pstore_init(void)
  183. {
  184. if (!efivar_supports_writes())
  185. return 0;
  186. if (efivars_pstore_disable)
  187. return 0;
  188. efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
  189. if (!efi_pstore_info.buf)
  190. return -ENOMEM;
  191. efi_pstore_info.bufsize = 1024;
  192. if (pstore_register(&efi_pstore_info)) {
  193. kfree(efi_pstore_info.buf);
  194. efi_pstore_info.buf = NULL;
  195. efi_pstore_info.bufsize = 0;
  196. }
  197. return 0;
  198. }
  199. static __exit void efivars_pstore_exit(void)
  200. {
  201. if (!efi_pstore_info.bufsize)
  202. return;
  203. pstore_unregister(&efi_pstore_info);
  204. kfree(efi_pstore_info.buf);
  205. efi_pstore_info.buf = NULL;
  206. efi_pstore_info.bufsize = 0;
  207. }
  208. module_init(efivars_pstore_init);
  209. module_exit(efivars_pstore_exit);
  210. MODULE_DESCRIPTION("EFI variable backend for pstore");
  211. MODULE_LICENSE("GPL");
  212. MODULE_ALIAS("platform:efivars");