board.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018 Stefan Roese <sr@denx.de>
  4. */
  5. #include <common.h>
  6. #include <env.h>
  7. #include <env_internal.h>
  8. #include <init.h>
  9. #include <led.h>
  10. #include <malloc.h>
  11. #include <net.h>
  12. #include <spi.h>
  13. #include <spi_flash.h>
  14. #include <u-boot/crc.h>
  15. #include <uuid.h>
  16. #include <linux/ctype.h>
  17. #include <linux/io.h>
  18. #define MT76XX_AGPIO_CFG 0x1000003c
  19. #define FACTORY_DATA_OFFS 0xc0000
  20. #define FACTORY_DATA_SECT_SIZE 0x10000
  21. #if ((CONFIG_ENV_OFFSET_REDUND + CONFIG_ENV_SIZE) > FACTORY_DATA_OFFS)
  22. #error "U-Boot image with environment too big (overlapping with factory-data)!"
  23. #endif
  24. #define FACTORY_DATA_USER_OFFS 0x140
  25. #define FACTORY_DATA_SIZE 0x1f0
  26. #define FACTORY_DATA_CRC_LEN (FACTORY_DATA_SIZE - \
  27. FACTORY_DATA_USER_OFFS - sizeof(u32))
  28. #define FACTORY_DATA_MAGIC 0xCAFEBABE
  29. struct factory_data_values {
  30. u8 pad_1[4];
  31. u8 wifi_mac[6]; /* offs: 0x004: binary value */
  32. u8 pad_2[30];
  33. u8 eth_mac[6]; /* offs: 0x028: binary value */
  34. u8 pad_3[FACTORY_DATA_USER_OFFS - 4 - 6 - 30 - 6];
  35. /* User values start here at offset 0x140 */
  36. u32 crc;
  37. u32 magic;
  38. u32 version;
  39. char ipr_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
  40. char hqv_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
  41. char unielec_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
  42. };
  43. int board_early_init_f(void)
  44. {
  45. void __iomem *gpio_mode;
  46. /* Configure digital vs analog GPIOs */
  47. gpio_mode = ioremap_nocache(MT76XX_AGPIO_CFG, 0x100);
  48. iowrite32(0x00fe01ff, gpio_mode);
  49. return 0;
  50. }
  51. static bool prepare_uuid_var(const char *fd_ptr, const char *env_var_name,
  52. char errorchar)
  53. {
  54. char str[UUID_STR_LEN + 1] = { 0 }; /* Enough for UUID stuff */
  55. bool env_updated = false;
  56. char *env;
  57. int i;
  58. memcpy(str, fd_ptr, UUID_STR_LEN);
  59. /* Convert non-ascii character to 'X' */
  60. for (i = 0; i < UUID_STR_LEN; i++) {
  61. if (!(isascii(str[i]) && isprint(str[i])))
  62. str[i] = errorchar;
  63. }
  64. env = env_get(env_var_name);
  65. if (strcmp(env, str)) {
  66. env_set(env_var_name, str);
  67. env_updated = true;
  68. }
  69. return env_updated;
  70. }
  71. static void factory_data_env_config(void)
  72. {
  73. struct factory_data_values *fd;
  74. struct spi_flash *sf;
  75. int env_updated = 0;
  76. char str[UUID_STR_LEN + 1]; /* Enough for UUID stuff */
  77. char *env;
  78. u8 *buf;
  79. u32 crc;
  80. int ret;
  81. u8 *ptr;
  82. buf = malloc(FACTORY_DATA_SIZE);
  83. if (!buf) {
  84. printf("F-Data:Unable to allocate buffer\n");
  85. return;
  86. }
  87. /*
  88. * Get values from factory-data area in SPI NOR
  89. */
  90. sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
  91. CONFIG_SF_DEFAULT_CS,
  92. CONFIG_SF_DEFAULT_SPEED,
  93. CONFIG_SF_DEFAULT_MODE);
  94. if (!sf) {
  95. printf("F-Data:Unable to access SPI NOR flash\n");
  96. goto err_free;
  97. }
  98. ret = spi_flash_read(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SIZE,
  99. (void *)buf);
  100. if (ret) {
  101. printf("F-Data:Unable to read factory-data from SPI NOR\n");
  102. goto err_spi_flash;
  103. }
  104. fd = (struct factory_data_values *)buf;
  105. if (fd->magic != FACTORY_DATA_MAGIC)
  106. printf("F-Data:Magic value not correct\n");
  107. crc = crc32(0, (u8 *)&fd->magic, FACTORY_DATA_CRC_LEN);
  108. if (crc != fd->crc)
  109. printf("F-Data:CRC not correct\n");
  110. else
  111. printf("F-Data:factory-data version %x detected\n",
  112. fd->version);
  113. /* Handle wifi_mac env variable */
  114. ptr = fd->wifi_mac;
  115. sprintf(str, "%pM", ptr);
  116. if (!is_valid_ethaddr(ptr))
  117. printf("F-Data:Invalid MAC addr: wifi_mac %s\n", str);
  118. env = env_get("wifiaddr");
  119. if (strcmp(env, str)) {
  120. env_set("wifiaddr", str);
  121. env_updated = 1;
  122. }
  123. /* Handle eth_mac env variable */
  124. ptr = fd->eth_mac;
  125. sprintf(str, "%pM", ptr);
  126. if (!is_valid_ethaddr(ptr))
  127. printf("F-Data:Invalid MAC addr: eth_mac %s\n", str);
  128. env = env_get("ethaddr");
  129. if (strcmp(env, str)) {
  130. env_set("ethaddr", str);
  131. env_updated = 1;
  132. }
  133. /* Handle UUID env variables */
  134. env_updated |= prepare_uuid_var(fd->ipr_id, "linuxmoduleid", 'X');
  135. env_updated |= prepare_uuid_var(fd->hqv_id, "linuxmodulehqvid", '\0');
  136. env_updated |= prepare_uuid_var(fd->unielec_id,
  137. "linuxmoduleunielecid", '\0');
  138. /* Check if the environment was updated and needs to get stored */
  139. if (env_updated != 0) {
  140. printf("F-Data:Values don't match env values -> saving\n");
  141. env_save();
  142. } else {
  143. debug("F-Data:Values match current env values\n");
  144. }
  145. err_spi_flash:
  146. spi_flash_free(sf);
  147. err_free:
  148. free(buf);
  149. }
  150. int board_late_init(void)
  151. {
  152. if (IS_ENABLED(CONFIG_LED))
  153. led_default_state();
  154. factory_data_env_config();
  155. return 0;
  156. }
  157. static void copy_or_generate_uuid(char *fd_ptr, const char *env_var_name)
  158. {
  159. char str[UUID_STR_LEN + 1] = { 0 }; /* Enough for UUID stuff */
  160. char *env;
  161. /* Don't use the UUID dest place, as the \0 char won't fit */
  162. env = env_get(env_var_name);
  163. if (env)
  164. strncpy(str, env, UUID_STR_LEN);
  165. else
  166. gen_rand_uuid_str(str, UUID_STR_FORMAT_STD);
  167. memcpy(fd_ptr, str, UUID_STR_LEN);
  168. }
  169. /*
  170. * Helper function to provide some sane factory-data values for testing
  171. * purpose, when these values are not programmed correctly
  172. */
  173. int do_fd_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  174. {
  175. struct factory_data_values *fd;
  176. struct spi_flash *sf;
  177. u8 *buf;
  178. int ret = CMD_RET_FAILURE;
  179. buf = malloc(FACTORY_DATA_SECT_SIZE);
  180. if (!buf) {
  181. printf("F-Data:Unable to allocate buffer\n");
  182. return CMD_RET_FAILURE;
  183. }
  184. sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
  185. CONFIG_SF_DEFAULT_CS,
  186. CONFIG_SF_DEFAULT_SPEED,
  187. CONFIG_SF_DEFAULT_MODE);
  188. if (!sf) {
  189. printf("F-Data:Unable to access SPI NOR flash\n");
  190. goto err_free;
  191. }
  192. /* Generate the factory-data struct */
  193. /* Fist read complete sector into buffer */
  194. ret = spi_flash_read(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE,
  195. (void *)buf);
  196. if (ret) {
  197. printf("F-Data:spi_flash_read failed (%d)\n", ret);
  198. goto err_spi_flash;
  199. }
  200. fd = (struct factory_data_values *)buf;
  201. fd->magic = FACTORY_DATA_MAGIC;
  202. fd->version = 0x1;
  203. /* Use existing MAC and UUID values or generate some random ones */
  204. if (!eth_env_get_enetaddr("wifiaddr", fd->wifi_mac)) {
  205. net_random_ethaddr(fd->wifi_mac);
  206. /* to get a different seed value for the MAC address */
  207. mdelay(10);
  208. }
  209. if (!eth_env_get_enetaddr("ethaddr", fd->eth_mac))
  210. net_random_ethaddr(fd->eth_mac);
  211. copy_or_generate_uuid(fd->ipr_id, "linuxmoduleid");
  212. copy_or_generate_uuid(fd->hqv_id, "linuxmodulehqvid");
  213. copy_or_generate_uuid(fd->unielec_id, "linuxmoduleunielecid");
  214. printf("New factory-data values:\n");
  215. printf("wifiaddr=%pM\n", fd->wifi_mac);
  216. printf("ethaddr=%pM\n", fd->eth_mac);
  217. /*
  218. * We don't have the \0 char at the end, so we need to specify the
  219. * length in the printf format instead
  220. */
  221. printf("linuxmoduleid=%." __stringify(UUID_STR_LEN) "s\n", fd->ipr_id);
  222. printf("linuxmodulehqvid=%." __stringify(UUID_STR_LEN) "s\n",
  223. fd->hqv_id);
  224. printf("linuxmoduleunielecid=%." __stringify(UUID_STR_LEN) "s\n",
  225. fd->unielec_id);
  226. fd->crc = crc32(0, (u8 *)&fd->magic, FACTORY_DATA_CRC_LEN);
  227. ret = spi_flash_erase(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE);
  228. if (ret) {
  229. printf("F-Data:spi_flash_erase failed (%d)\n", ret);
  230. goto err_spi_flash;
  231. }
  232. ret = spi_flash_write(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE,
  233. buf);
  234. if (ret) {
  235. printf("F-Data:spi_flash_write failed (%d)\n", ret);
  236. goto err_spi_flash;
  237. }
  238. printf("F-Data:factory-data values written to SPI NOR flash\n");
  239. err_spi_flash:
  240. spi_flash_free(sf);
  241. err_free:
  242. free(buf);
  243. return ret;
  244. }
  245. #ifndef CONFIG_SPL_BUILD
  246. U_BOOT_CMD(
  247. fd_write, 1, 0, do_fd_write,
  248. "Write test factory-data values to SPI NOR",
  249. "\n"
  250. );
  251. #endif