efi-pstore.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. efivar_entry_iter_begin();
  17. psi->data = NULL;
  18. return 0;
  19. }
  20. static int efi_pstore_close(struct pstore_info *psi)
  21. {
  22. efivar_entry_iter_end();
  23. psi->data = NULL;
  24. return 0;
  25. }
  26. struct pstore_read_data {
  27. u64 *id;
  28. enum pstore_type_id *type;
  29. int *count;
  30. struct timespec *timespec;
  31. char **buf;
  32. };
  33. static int efi_pstore_read_func(struct efivar_entry *entry, void *data)
  34. {
  35. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  36. struct pstore_read_data *cb_data = data;
  37. char name[DUMP_NAME_LEN];
  38. int i;
  39. int cnt;
  40. unsigned int part;
  41. unsigned long time, size;
  42. if (efi_guidcmp(entry->var.VendorGuid, vendor))
  43. return 0;
  44. for (i = 0; i < DUMP_NAME_LEN; i++)
  45. name[i] = entry->var.VariableName[i];
  46. if (sscanf(name, "dump-type%u-%u-%d-%lu",
  47. cb_data->type, &part, &cnt, &time) == 4) {
  48. *cb_data->id = part;
  49. *cb_data->count = cnt;
  50. cb_data->timespec->tv_sec = time;
  51. cb_data->timespec->tv_nsec = 0;
  52. } else if (sscanf(name, "dump-type%u-%u-%lu",
  53. cb_data->type, &part, &time) == 3) {
  54. /*
  55. * Check if an old format,
  56. * which doesn't support holding
  57. * multiple logs, remains.
  58. */
  59. *cb_data->id = part;
  60. *cb_data->count = 0;
  61. cb_data->timespec->tv_sec = time;
  62. cb_data->timespec->tv_nsec = 0;
  63. } else
  64. return 0;
  65. entry->var.DataSize = 1024;
  66. __efivar_entry_get(entry, &entry->var.Attributes,
  67. &entry->var.DataSize, entry->var.Data);
  68. size = entry->var.DataSize;
  69. *cb_data->buf = kmemdup(entry->var.Data, size, GFP_KERNEL);
  70. if (*cb_data->buf == NULL)
  71. return -ENOMEM;
  72. return size;
  73. }
  74. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  75. int *count, struct timespec *timespec,
  76. char **buf, struct pstore_info *psi)
  77. {
  78. struct pstore_read_data data;
  79. data.id = id;
  80. data.type = type;
  81. data.count = count;
  82. data.timespec = timespec;
  83. data.buf = buf;
  84. return __efivar_entry_iter(efi_pstore_read_func, &efivar_sysfs_list, &data,
  85. (struct efivar_entry **)&psi->data);
  86. }
  87. static int efi_pstore_write(enum pstore_type_id type,
  88. enum kmsg_dump_reason reason, u64 *id,
  89. unsigned int part, int count, size_t hsize, size_t size,
  90. struct pstore_info *psi)
  91. {
  92. char name[DUMP_NAME_LEN];
  93. efi_char16_t efi_name[DUMP_NAME_LEN];
  94. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  95. int i, ret = 0;
  96. sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
  97. get_seconds());
  98. for (i = 0; i < DUMP_NAME_LEN; i++)
  99. efi_name[i] = name[i];
  100. efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
  101. !pstore_cannot_block_path(reason),
  102. size, psi->buf);
  103. if (reason == KMSG_DUMP_OOPS)
  104. efivar_run_worker();
  105. *id = part;
  106. return ret;
  107. };
  108. struct pstore_erase_data {
  109. u64 id;
  110. enum pstore_type_id type;
  111. int count;
  112. struct timespec time;
  113. efi_char16_t *name;
  114. };
  115. /*
  116. * Clean up an entry with the same name
  117. */
  118. static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
  119. {
  120. struct pstore_erase_data *ed = data;
  121. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  122. efi_char16_t efi_name_old[DUMP_NAME_LEN];
  123. efi_char16_t *efi_name = ed->name;
  124. unsigned long ucs2_len = ucs2_strlen(ed->name);
  125. char name_old[DUMP_NAME_LEN];
  126. int i;
  127. if (efi_guidcmp(entry->var.VendorGuid, vendor))
  128. return 0;
  129. if (ucs2_strncmp(entry->var.VariableName,
  130. efi_name, (size_t)ucs2_len)) {
  131. /*
  132. * Check if an old format, which doesn't support
  133. * holding multiple logs, remains.
  134. */
  135. sprintf(name_old, "dump-type%u-%u-%lu", ed->type,
  136. (unsigned int)ed->id, ed->time.tv_sec);
  137. for (i = 0; i < DUMP_NAME_LEN; i++)
  138. efi_name_old[i] = name_old[i];
  139. if (ucs2_strncmp(entry->var.VariableName, efi_name_old,
  140. ucs2_strlen(efi_name_old)))
  141. return 0;
  142. }
  143. /* found */
  144. __efivar_entry_delete(entry);
  145. list_del(&entry->list);
  146. return 1;
  147. }
  148. static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
  149. struct timespec time, struct pstore_info *psi)
  150. {
  151. struct pstore_erase_data edata;
  152. struct efivar_entry *entry = NULL;
  153. char name[DUMP_NAME_LEN];
  154. efi_char16_t efi_name[DUMP_NAME_LEN];
  155. int found, i;
  156. sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
  157. time.tv_sec);
  158. for (i = 0; i < DUMP_NAME_LEN; i++)
  159. efi_name[i] = name[i];
  160. edata.id = id;
  161. edata.type = type;
  162. edata.count = count;
  163. edata.time = time;
  164. edata.name = efi_name;
  165. efivar_entry_iter_begin();
  166. found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
  167. efivar_entry_iter_end();
  168. if (found)
  169. efivar_unregister(entry);
  170. return 0;
  171. }
  172. static struct pstore_info efi_pstore_info = {
  173. .owner = THIS_MODULE,
  174. .name = "efi",
  175. .open = efi_pstore_open,
  176. .close = efi_pstore_close,
  177. .read = efi_pstore_read,
  178. .write = efi_pstore_write,
  179. .erase = efi_pstore_erase,
  180. };
  181. static __init int efivars_pstore_init(void)
  182. {
  183. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  184. return 0;
  185. if (!efivars_kobject())
  186. return 0;
  187. if (efivars_pstore_disable)
  188. return 0;
  189. efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
  190. if (!efi_pstore_info.buf)
  191. return -ENOMEM;
  192. efi_pstore_info.bufsize = 1024;
  193. spin_lock_init(&efi_pstore_info.buf_lock);
  194. if (pstore_register(&efi_pstore_info)) {
  195. kfree(efi_pstore_info.buf);
  196. efi_pstore_info.buf = NULL;
  197. efi_pstore_info.bufsize = 0;
  198. }
  199. return 0;
  200. }
  201. static __exit void efivars_pstore_exit(void)
  202. {
  203. }
  204. module_init(efivars_pstore_init);
  205. module_exit(efivars_pstore_exit);
  206. MODULE_DESCRIPTION("EFI variable backend for pstore");
  207. MODULE_LICENSE("GPL");