vpd.c 7.7 KB

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