u-boot-env.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2022 - 2023 Rafał Miłecki <rafal@milecki.pl>
  4. */
  5. #include <linux/crc32.h>
  6. #include <linux/etherdevice.h>
  7. #include <linux/export.h>
  8. #include <linux/hex.h>
  9. #include <linux/if_ether.h>
  10. #include <linux/nvmem-consumer.h>
  11. #include <linux/nvmem-provider.h>
  12. #include <linux/of.h>
  13. #include <linux/slab.h>
  14. #include "u-boot-env.h"
  15. struct u_boot_env_image_single {
  16. __le32 crc32;
  17. uint8_t data[];
  18. } __packed;
  19. struct u_boot_env_image_redundant {
  20. __le32 crc32;
  21. u8 mark;
  22. uint8_t data[];
  23. } __packed;
  24. struct u_boot_env_image_broadcom {
  25. __le32 magic;
  26. __le32 len;
  27. __le32 crc32;
  28. DECLARE_FLEX_ARRAY(uint8_t, data);
  29. } __packed;
  30. static int u_boot_env_read_post_process_ethaddr(void *context, const char *id, int index,
  31. unsigned int offset, void *buf, size_t bytes)
  32. {
  33. u8 mac[ETH_ALEN];
  34. if (bytes != MAC_ADDR_STR_LEN)
  35. return -EINVAL;
  36. if (!mac_pton(buf, mac))
  37. return -EINVAL;
  38. if (index)
  39. eth_addr_add(mac, index);
  40. ether_addr_copy(buf, mac);
  41. return 0;
  42. }
  43. static int u_boot_env_parse_cells(struct device *dev, struct nvmem_device *nvmem, uint8_t *buf,
  44. size_t data_offset, size_t data_len)
  45. {
  46. char *data = buf + data_offset;
  47. char *var, *value, *eq;
  48. for (var = data;
  49. var < data + data_len && *var;
  50. var = value + strlen(value) + 1) {
  51. struct nvmem_cell_info info = {};
  52. eq = strchr(var, '=');
  53. if (!eq)
  54. break;
  55. *eq = '\0';
  56. value = eq + 1;
  57. info.name = devm_kstrdup(dev, var, GFP_KERNEL);
  58. if (!info.name)
  59. return -ENOMEM;
  60. info.offset = data_offset + value - data;
  61. info.bytes = strlen(value);
  62. info.np = of_get_child_by_name(dev->of_node, info.name);
  63. if (!strcmp(var, "ethaddr")) {
  64. info.raw_len = strlen(value);
  65. info.bytes = ETH_ALEN;
  66. info.read_post_process = u_boot_env_read_post_process_ethaddr;
  67. }
  68. nvmem_add_one_cell(nvmem, &info);
  69. }
  70. return 0;
  71. }
  72. int u_boot_env_parse(struct device *dev, struct nvmem_device *nvmem,
  73. enum u_boot_env_format format)
  74. {
  75. size_t crc32_data_offset;
  76. size_t crc32_data_len;
  77. size_t crc32_offset;
  78. uint32_t *crc32_addr;
  79. size_t data_offset;
  80. size_t data_len;
  81. size_t dev_size;
  82. uint32_t crc32;
  83. uint32_t calc;
  84. uint8_t *buf;
  85. u32 env_size;
  86. int bytes;
  87. int err;
  88. dev_size = device_property_read_u32(dev, "env-size", &env_size) ?
  89. nvmem_dev_size(nvmem) : (size_t)env_size;
  90. buf = kzalloc(dev_size, GFP_KERNEL);
  91. if (!buf) {
  92. err = -ENOMEM;
  93. goto err_out;
  94. }
  95. bytes = nvmem_device_read(nvmem, 0, dev_size, buf);
  96. if (bytes < 0) {
  97. err = bytes;
  98. goto err_kfree;
  99. } else if (bytes != dev_size) {
  100. err = -EIO;
  101. goto err_kfree;
  102. }
  103. switch (format) {
  104. case U_BOOT_FORMAT_SINGLE:
  105. crc32_offset = offsetof(struct u_boot_env_image_single, crc32);
  106. crc32_data_offset = offsetof(struct u_boot_env_image_single, data);
  107. data_offset = offsetof(struct u_boot_env_image_single, data);
  108. break;
  109. case U_BOOT_FORMAT_REDUNDANT:
  110. crc32_offset = offsetof(struct u_boot_env_image_redundant, crc32);
  111. crc32_data_offset = offsetof(struct u_boot_env_image_redundant, data);
  112. data_offset = offsetof(struct u_boot_env_image_redundant, data);
  113. break;
  114. case U_BOOT_FORMAT_BROADCOM:
  115. crc32_offset = offsetof(struct u_boot_env_image_broadcom, crc32);
  116. crc32_data_offset = offsetof(struct u_boot_env_image_broadcom, data);
  117. data_offset = offsetof(struct u_boot_env_image_broadcom, data);
  118. break;
  119. }
  120. if (dev_size < data_offset) {
  121. dev_err(dev, "Device too small for u-boot-env\n");
  122. err = -EIO;
  123. goto err_kfree;
  124. }
  125. crc32_addr = (uint32_t *)(buf + crc32_offset);
  126. crc32 = *crc32_addr;
  127. crc32_data_len = dev_size - crc32_data_offset;
  128. data_len = dev_size - data_offset;
  129. calc = crc32_le(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
  130. if (calc != crc32) {
  131. dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32);
  132. err = -EINVAL;
  133. goto err_kfree;
  134. }
  135. buf[dev_size - 1] = '\0';
  136. err = u_boot_env_parse_cells(dev, nvmem, buf, data_offset, data_len);
  137. err_kfree:
  138. kfree(buf);
  139. err_out:
  140. return err;
  141. }
  142. EXPORT_SYMBOL_GPL(u_boot_env_parse);
  143. static int u_boot_env_add_cells(struct nvmem_layout *layout)
  144. {
  145. struct device *dev = &layout->dev;
  146. enum u_boot_env_format format;
  147. format = (uintptr_t)device_get_match_data(dev);
  148. return u_boot_env_parse(dev, layout->nvmem, format);
  149. }
  150. static int u_boot_env_probe(struct nvmem_layout *layout)
  151. {
  152. layout->add_cells = u_boot_env_add_cells;
  153. return nvmem_layout_register(layout);
  154. }
  155. static void u_boot_env_remove(struct nvmem_layout *layout)
  156. {
  157. nvmem_layout_unregister(layout);
  158. }
  159. static const struct of_device_id u_boot_env_of_match_table[] = {
  160. { .compatible = "u-boot,env", .data = (void *)U_BOOT_FORMAT_SINGLE, },
  161. { .compatible = "u-boot,env-redundant-bool", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
  162. { .compatible = "u-boot,env-redundant-count", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
  163. { .compatible = "brcm,env", .data = (void *)U_BOOT_FORMAT_BROADCOM, },
  164. {},
  165. };
  166. static struct nvmem_layout_driver u_boot_env_layout = {
  167. .driver = {
  168. .name = "u-boot-env-layout",
  169. .of_match_table = u_boot_env_of_match_table,
  170. },
  171. .probe = u_boot_env_probe,
  172. .remove = u_boot_env_remove,
  173. };
  174. module_nvmem_layout_driver(u_boot_env_layout);
  175. MODULE_AUTHOR("Rafał Miłecki");
  176. MODULE_LICENSE("GPL");
  177. MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);
  178. MODULE_DESCRIPTION("NVMEM layout driver for U-Boot environment variables");