board.c 7.3 KB

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