vpd.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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, s32 key_len,
  89. const u8 *value, s32 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 = kzalloc(key_len + 1, GFP_KERNEL);
  105. if (!info->key) {
  106. ret = -ENOMEM;
  107. goto free_info;
  108. }
  109. memcpy(info->key, key, key_len);
  110. sysfs_bin_attr_init(&info->bin_attr);
  111. info->bin_attr.attr.name = info->key;
  112. info->bin_attr.attr.mode = 0444;
  113. info->bin_attr.size = value_len;
  114. info->bin_attr.read = vpd_attrib_read;
  115. info->bin_attr.private = info;
  116. info->value = value;
  117. INIT_LIST_HEAD(&info->list);
  118. ret = sysfs_create_bin_file(sec->kobj, &info->bin_attr);
  119. if (ret)
  120. goto free_info_key;
  121. list_add_tail(&info->list, &sec->attribs);
  122. return 0;
  123. free_info_key:
  124. kfree(info->key);
  125. free_info:
  126. kfree(info);
  127. return ret;
  128. }
  129. static void vpd_section_attrib_destroy(struct vpd_section *sec)
  130. {
  131. struct vpd_attrib_info *info;
  132. struct vpd_attrib_info *temp;
  133. list_for_each_entry_safe(info, temp, &sec->attribs, list) {
  134. sysfs_remove_bin_file(sec->kobj, &info->bin_attr);
  135. kfree(info->key);
  136. kfree(info);
  137. }
  138. }
  139. static ssize_t vpd_section_read(struct file *filp, struct kobject *kobp,
  140. struct bin_attribute *bin_attr, char *buf,
  141. loff_t pos, size_t count)
  142. {
  143. struct vpd_section *sec = bin_attr->private;
  144. return memory_read_from_buffer(buf, count, &pos, sec->baseaddr,
  145. sec->bin_attr.size);
  146. }
  147. static int vpd_section_create_attribs(struct vpd_section *sec)
  148. {
  149. s32 consumed;
  150. int ret;
  151. consumed = 0;
  152. do {
  153. ret = vpd_decode_string(sec->bin_attr.size, sec->baseaddr,
  154. &consumed, vpd_section_attrib_add, sec);
  155. } while (ret == VPD_OK);
  156. return 0;
  157. }
  158. static int vpd_section_init(const char *name, struct vpd_section *sec,
  159. phys_addr_t physaddr, size_t size)
  160. {
  161. int ret;
  162. int raw_len;
  163. sec->baseaddr = memremap(physaddr, size, MEMREMAP_WB);
  164. if (!sec->baseaddr)
  165. return -ENOMEM;
  166. sec->name = name;
  167. /* We want to export the raw partion with name ${name}_raw */
  168. raw_len = strlen(name) + 5;
  169. sec->raw_name = kzalloc(raw_len, GFP_KERNEL);
  170. strncpy(sec->raw_name, name, raw_len);
  171. strncat(sec->raw_name, "_raw", raw_len);
  172. sysfs_bin_attr_init(&sec->bin_attr);
  173. sec->bin_attr.attr.name = sec->raw_name;
  174. sec->bin_attr.attr.mode = 0444;
  175. sec->bin_attr.size = size;
  176. sec->bin_attr.read = vpd_section_read;
  177. sec->bin_attr.private = sec;
  178. ret = sysfs_create_bin_file(vpd_kobj, &sec->bin_attr);
  179. if (ret)
  180. goto free_sec;
  181. sec->kobj = kobject_create_and_add(name, vpd_kobj);
  182. if (!sec->kobj) {
  183. ret = -EINVAL;
  184. goto sysfs_remove;
  185. }
  186. INIT_LIST_HEAD(&sec->attribs);
  187. vpd_section_create_attribs(sec);
  188. sec->enabled = true;
  189. return 0;
  190. sysfs_remove:
  191. sysfs_remove_bin_file(vpd_kobj, &sec->bin_attr);
  192. free_sec:
  193. kfree(sec->raw_name);
  194. iounmap(sec->baseaddr);
  195. return ret;
  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. iounmap(sec->baseaddr);
  205. }
  206. return 0;
  207. }
  208. static int vpd_sections_init(phys_addr_t physaddr)
  209. {
  210. struct vpd_cbmem __iomem *temp;
  211. struct vpd_cbmem header;
  212. int ret = 0;
  213. temp = memremap(physaddr, sizeof(struct vpd_cbmem), MEMREMAP_WB);
  214. if (!temp)
  215. return -ENOMEM;
  216. memcpy_fromio(&header, temp, sizeof(struct vpd_cbmem));
  217. iounmap(temp);
  218. if (header.magic != VPD_CBMEM_MAGIC)
  219. return -ENODEV;
  220. if (header.ro_size) {
  221. ret = vpd_section_init("ro", &ro_vpd,
  222. physaddr + sizeof(struct vpd_cbmem),
  223. header.ro_size);
  224. if (ret)
  225. return ret;
  226. }
  227. if (header.rw_size) {
  228. ret = vpd_section_init("rw", &rw_vpd,
  229. physaddr + sizeof(struct vpd_cbmem) +
  230. header.ro_size, header.rw_size);
  231. if (ret)
  232. return ret;
  233. }
  234. return 0;
  235. }
  236. static int vpd_probe(struct platform_device *pdev)
  237. {
  238. int ret;
  239. struct lb_cbmem_ref entry;
  240. ret = coreboot_table_find(CB_TAG_VPD, &entry, sizeof(entry));
  241. if (ret)
  242. return ret;
  243. return vpd_sections_init(entry.cbmem_addr);
  244. }
  245. static struct platform_driver vpd_driver = {
  246. .probe = vpd_probe,
  247. .driver = {
  248. .name = "vpd",
  249. },
  250. };
  251. static int __init vpd_platform_init(void)
  252. {
  253. struct platform_device *pdev;
  254. pdev = platform_device_register_simple("vpd", -1, NULL, 0);
  255. if (IS_ERR(pdev))
  256. return PTR_ERR(pdev);
  257. vpd_kobj = kobject_create_and_add("vpd", firmware_kobj);
  258. if (!vpd_kobj)
  259. return -ENOMEM;
  260. memset(&ro_vpd, 0, sizeof(ro_vpd));
  261. memset(&rw_vpd, 0, sizeof(rw_vpd));
  262. platform_driver_register(&vpd_driver);
  263. return 0;
  264. }
  265. static void __exit vpd_platform_exit(void)
  266. {
  267. vpd_section_destroy(&ro_vpd);
  268. vpd_section_destroy(&rw_vpd);
  269. kobject_put(vpd_kobj);
  270. }
  271. module_init(vpd_platform_init);
  272. module_exit(vpd_platform_exit);
  273. MODULE_AUTHOR("Google, Inc.");
  274. MODULE_LICENSE("GPL");