efi-pstore.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 = kmalloc(size, GFP_KERNEL);
  70. if (*cb_data->buf == NULL)
  71. return -ENOMEM;
  72. memcpy(*cb_data->buf, entry->var.Data, size);
  73. return size;
  74. }
  75. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  76. int *count, struct timespec *timespec,
  77. char **buf, struct pstore_info *psi)
  78. {
  79. struct pstore_read_data data;
  80. data.id = id;
  81. data.type = type;
  82. data.count = count;
  83. data.timespec = timespec;
  84. data.buf = buf;
  85. return __efivar_entry_iter(efi_pstore_read_func, &efivar_sysfs_list, &data,
  86. (struct efivar_entry **)&psi->data);
  87. }
  88. static int efi_pstore_write(enum pstore_type_id type,
  89. enum kmsg_dump_reason reason, u64 *id,
  90. unsigned int part, int count, size_t size,
  91. struct pstore_info *psi)
  92. {
  93. char name[DUMP_NAME_LEN];
  94. efi_char16_t efi_name[DUMP_NAME_LEN];
  95. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  96. int i, ret = 0;
  97. sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
  98. get_seconds());
  99. for (i = 0; i < DUMP_NAME_LEN; i++)
  100. efi_name[i] = name[i];
  101. efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
  102. !pstore_cannot_block_path(reason),
  103. size, psi->buf);
  104. if (reason == KMSG_DUMP_OOPS)
  105. efivar_run_worker();
  106. *id = part;
  107. return ret;
  108. };
  109. struct pstore_erase_data {
  110. u64 id;
  111. enum pstore_type_id type;
  112. int count;
  113. struct timespec time;
  114. efi_char16_t *name;
  115. };
  116. /*
  117. * Clean up an entry with the same name
  118. */
  119. static int efi_pstore_erase_func(struct efivar_entry *entry, void *data)
  120. {
  121. struct pstore_erase_data *ed = data;
  122. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  123. efi_char16_t efi_name_old[DUMP_NAME_LEN];
  124. efi_char16_t *efi_name = ed->name;
  125. unsigned long ucs2_len = ucs2_strlen(ed->name);
  126. char name_old[DUMP_NAME_LEN];
  127. int i;
  128. if (efi_guidcmp(entry->var.VendorGuid, vendor))
  129. return 0;
  130. if (ucs2_strncmp(entry->var.VariableName,
  131. efi_name, (size_t)ucs2_len)) {
  132. /*
  133. * Check if an old format, which doesn't support
  134. * holding multiple logs, remains.
  135. */
  136. sprintf(name_old, "dump-type%u-%u-%lu", ed->type,
  137. (unsigned int)ed->id, ed->time.tv_sec);
  138. for (i = 0; i < DUMP_NAME_LEN; i++)
  139. efi_name_old[i] = name_old[i];
  140. if (ucs2_strncmp(entry->var.VariableName, efi_name_old,
  141. ucs2_strlen(efi_name_old)))
  142. return 0;
  143. }
  144. /* found */
  145. __efivar_entry_delete(entry);
  146. list_del(&entry->list);
  147. return 1;
  148. }
  149. static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
  150. struct timespec time, struct pstore_info *psi)
  151. {
  152. struct pstore_erase_data edata;
  153. struct efivar_entry *entry = NULL;
  154. char name[DUMP_NAME_LEN];
  155. efi_char16_t efi_name[DUMP_NAME_LEN];
  156. int found, i;
  157. sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
  158. time.tv_sec);
  159. for (i = 0; i < DUMP_NAME_LEN; i++)
  160. efi_name[i] = name[i];
  161. edata.id = id;
  162. edata.type = type;
  163. edata.count = count;
  164. edata.time = time;
  165. edata.name = efi_name;
  166. efivar_entry_iter_begin();
  167. found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
  168. efivar_entry_iter_end();
  169. if (found)
  170. efivar_unregister(entry);
  171. return 0;
  172. }
  173. static struct pstore_info efi_pstore_info = {
  174. .owner = THIS_MODULE,
  175. .name = "efi",
  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 (!efi_enabled(EFI_RUNTIME_SERVICES))
  185. return 0;
  186. if (!efivars_kobject())
  187. return 0;
  188. if (efivars_pstore_disable)
  189. return 0;
  190. efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
  191. if (!efi_pstore_info.buf)
  192. return -ENOMEM;
  193. efi_pstore_info.bufsize = 1024;
  194. spin_lock_init(&efi_pstore_info.buf_lock);
  195. pstore_register(&efi_pstore_info);
  196. return 0;
  197. }
  198. static __exit void efivars_pstore_exit(void)
  199. {
  200. }
  201. module_init(efivars_pstore_init);
  202. module_exit(efivars_pstore_exit);
  203. MODULE_DESCRIPTION("EFI variable backend for pstore");
  204. MODULE_LICENSE("GPL");