board.c 7.2 KB

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