board.c 7.3 KB

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