vpd.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vpd.c
  4. *
  5. * Driver for exporting VPD content to sysfs.
  6. *
  7. * Copyright 2017 Google Inc.
  8. */
  9. #include <linux/ctype.h>
  10. #include <linux/init.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/kobject.h>
  14. #include <linux/list.h>
  15. #include <linux/module.h>
  16. #include <linux/of_address.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/sysfs.h>
  20. #include "coreboot_table.h"
  21. #include "vpd_decode.h"
  22. #define CB_TAG_VPD 0x2c
  23. #define VPD_CBMEM_MAGIC 0x43524f53
  24. static struct kobject *vpd_kobj;
  25. struct vpd_cbmem {
  26. u32 magic;
  27. u32 version;
  28. u32 ro_size;
  29. u32 rw_size;
  30. u8 blob[];
  31. };
  32. struct vpd_section {
  33. bool enabled;
  34. const char *name;
  35. char *raw_name; /* the string name_raw */
  36. struct kobject *kobj; /* vpd/name directory */
  37. char *baseaddr;
  38. struct bin_attribute bin_attr; /* vpd/name_raw bin_attribute */
  39. struct list_head attribs; /* key/value in vpd_attrib_info list */
  40. };
  41. struct vpd_attrib_info {
  42. char *key;
  43. const char *value;
  44. struct bin_attribute bin_attr;
  45. struct list_head list;
  46. };
  47. static struct vpd_section ro_vpd;
  48. static struct vpd_section rw_vpd;
  49. static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp,
  50. struct bin_attribute *bin_attr, char *buf,
  51. loff_t pos, size_t count)
  52. {
  53. struct vpd_attrib_info *info = bin_attr->private;
  54. return memory_read_from_buffer(buf, count, &pos, info->value,
  55. info->bin_attr.size);
  56. }
  57. /*
  58. * vpd_section_check_key_name()
  59. *
  60. * The VPD specification supports only [a-zA-Z0-9_]+ characters in key names but
  61. * old firmware versions may have entries like "S/N" which are problematic when
  62. * exporting them as sysfs attributes. These keys present in old firmwares are
  63. * ignored.
  64. *
  65. * Returns VPD_OK for a valid key name, VPD_FAIL otherwise.
  66. *
  67. * @key: The key name to check
  68. * @key_len: key name length
  69. */
  70. static int vpd_section_check_key_name(const u8 *key, s32 key_len)
  71. {
  72. int c;
  73. while (key_len-- > 0) {
  74. c = *key++;
  75. if (!isalnum(c) && c != '_')
  76. return VPD_FAIL;
  77. }
  78. return VPD_OK;
  79. }
  80. static int vpd_section_attrib_add(const u8 *key, u32 key_len,
  81. const u8 *value, u32 value_len,
  82. void *arg)
  83. {
  84. int ret;
  85. struct vpd_section *sec = arg;
  86. struct vpd_attrib_info *info;
  87. /*
  88. * Return VPD_OK immediately to decode next entry if the current key
  89. * name contains invalid characters.
  90. */
  91. if (vpd_section_check_key_name(key, key_len) != VPD_OK)
  92. return VPD_OK;
  93. info = kzalloc(sizeof(*info), GFP_KERNEL);
  94. if (!info)
  95. return -ENOMEM;
  96. info->key = kstrndup(key, key_len, GFP_KERNEL);
  97. if (!info->key) {
  98. ret = -ENOMEM;
  99. goto free_info;
  100. }
  101. sysfs_bin_attr_init(&info->bin_attr);
  102. info->bin_attr.attr.name = info->key;
  103. info->bin_attr.attr.mode = 0444;
  104. info->bin_attr.size = value_len;
  105. info->bin_attr.read = vpd_attrib_read;
  106. info->bin_attr.private = info;
  107. info->value = value;
  108. INIT_LIST_HEAD(&info->list);
  109. ret = sysfs_create_bin_file(sec->kobj, &info->bin_attr);
  110. if (ret)
  111. goto free_info_key;
  112. list_add_tail(&info->list, &sec->attribs);
  113. return 0;
  114. free_info_key:
  115. kfree(info->key);
  116. free_info:
  117. kfree(info);
  118. return ret;
  119. }
  120. static void vpd_section_attrib_destroy(struct vpd_section *sec)
  121. {
  122. struct vpd_attrib_info *info;
  123. struct vpd_attrib_info *temp;
  124. list_for_each_entry_safe(info, temp, &sec->attribs, list) {
  125. sysfs_remove_bin_file(sec->kobj, &info->bin_attr);
  126. kfree(info->key);
  127. kfree(info);
  128. }
  129. }
  130. static ssize_t vpd_section_read(struct file *filp, struct kobject *kobp,
  131. struct bin_attribute *bin_attr, char *buf,
  132. loff_t pos, size_t count)
  133. {
  134. struct vpd_section *sec = bin_attr->private;
  135. return memory_read_from_buffer(buf, count, &pos, sec->baseaddr,
  136. sec->bin_attr.size);
  137. }
  138. static int vpd_section_create_attribs(struct vpd_section *sec)
  139. {
  140. s32 consumed;
  141. int ret;
  142. consumed = 0;
  143. do {
  144. ret = vpd_decode_string(sec->bin_attr.size, sec->baseaddr,
  145. &consumed, vpd_section_attrib_add, sec);
  146. } while (ret == VPD_OK);
  147. return 0;
  148. }
  149. static int vpd_section_init(const char *name, struct vpd_section *sec,
  150. phys_addr_t physaddr, size_t size)
  151. {
  152. int err;
  153. sec->baseaddr = memremap(physaddr, size, MEMREMAP_WB);
  154. if (!sec->baseaddr)
  155. return -ENOMEM;
  156. sec->name = name;
  157. /* We want to export the raw partition with name ${name}_raw */
  158. sec->raw_name = kasprintf(GFP_KERNEL, "%s_raw", name);
  159. if (!sec->raw_name) {
  160. err = -ENOMEM;
  161. goto err_memunmap;
  162. }
  163. sysfs_bin_attr_init(&sec->bin_attr);
  164. sec->bin_attr.attr.name = sec->raw_name;
  165. sec->bin_attr.attr.mode = 0444;
  166. sec->bin_attr.size = size;
  167. sec->bin_attr.read = vpd_section_read;
  168. sec->bin_attr.private = sec;
  169. err = sysfs_create_bin_file(vpd_kobj, &sec->bin_attr);
  170. if (err)
  171. goto err_free_raw_name;
  172. sec->kobj = kobject_create_and_add(name, vpd_kobj);
  173. if (!sec->kobj) {
  174. err = -EINVAL;
  175. goto err_sysfs_remove;
  176. }
  177. INIT_LIST_HEAD(&sec->attribs);
  178. vpd_section_create_attribs(sec);
  179. sec->enabled = true;
  180. return 0;
  181. err_sysfs_remove:
  182. sysfs_remove_bin_file(vpd_kobj, &sec->bin_attr);
  183. err_free_raw_name:
  184. kfree(sec->raw_name);
  185. err_memunmap:
  186. memunmap(sec->baseaddr);
  187. return err;
  188. }
  189. static int vpd_section_destroy(struct vpd_section *sec)
  190. {
  191. if (sec->enabled) {
  192. vpd_section_attrib_destroy(sec);
  193. kobject_put(sec->kobj);
  194. sysfs_remove_bin_file(vpd_kobj, &sec->bin_attr);
  195. kfree(sec->raw_name);
  196. memunmap(sec->baseaddr);
  197. sec->enabled = false;
  198. }
  199. return 0;
  200. }
  201. static int vpd_sections_init(phys_addr_t physaddr)
  202. {
  203. struct vpd_cbmem *temp;
  204. struct vpd_cbmem header;
  205. int ret = 0;
  206. temp = memremap(physaddr, sizeof(struct vpd_cbmem), MEMREMAP_WB);
  207. if (!temp)
  208. return -ENOMEM;
  209. memcpy(&header, temp, sizeof(struct vpd_cbmem));
  210. memunmap(temp);
  211. if (header.magic != VPD_CBMEM_MAGIC)
  212. return -ENODEV;
  213. if (header.ro_size) {
  214. ret = vpd_section_init("ro", &ro_vpd,
  215. physaddr + sizeof(struct vpd_cbmem),
  216. header.ro_size);
  217. if (ret)
  218. return ret;
  219. }
  220. if (header.rw_size) {
  221. ret = vpd_section_init("rw", &rw_vpd,
  222. physaddr + sizeof(struct vpd_cbmem) +
  223. header.ro_size, header.rw_size);
  224. if (ret) {
  225. vpd_section_destroy(&ro_vpd);
  226. return ret;
  227. }
  228. }
  229. return 0;
  230. }
  231. static int vpd_probe(struct coreboot_device *dev)
  232. {
  233. int ret;
  234. vpd_kobj = kobject_create_and_add("vpd", firmware_kobj);
  235. if (!vpd_kobj)
  236. return -ENOMEM;
  237. ret = vpd_sections_init(dev->cbmem_ref.cbmem_addr);
  238. if (ret) {
  239. kobject_put(vpd_kobj);
  240. return ret;
  241. }
  242. return 0;
  243. }
  244. static int vpd_remove(struct coreboot_device *dev)
  245. {
  246. vpd_section_destroy(&ro_vpd);
  247. vpd_section_destroy(&rw_vpd);
  248. kobject_put(vpd_kobj);
  249. return 0;
  250. }
  251. static struct coreboot_driver vpd_driver = {
  252. .probe = vpd_probe,
  253. .remove = vpd_remove,
  254. .drv = {
  255. .name = "vpd",
  256. },
  257. .tag = CB_TAG_VPD,
  258. };
  259. module_coreboot_driver(vpd_driver);
  260. MODULE_AUTHOR("Google, Inc.");
  261. MODULE_LICENSE("GPL");