u-boot-env.c 5.1 KB

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