board.c 7.3 KB

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