update_cmd.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2002
  4. * Richard Jones, rjones@nexus-tech.net
  5. */
  6. /*
  7. * Boot support
  8. */
  9. #include <common.h>
  10. #include <command.h>
  11. #include <s_record.h>
  12. #include <net.h>
  13. #include <ata.h>
  14. #include <asm/io.h>
  15. #include <mapmem.h>
  16. #include <part.h>
  17. #include <fat.h>
  18. #include <fs.h>
  19. #include <environment.h>
  20. typedef enum {
  21. MMC = 0,
  22. USB,
  23. NFLASH
  24. } MEDIA_TYPE;
  25. static int get_data_from_media(MEDIA_TYPE type, char index, char *file_name)
  26. {
  27. int ret = -1;
  28. char cmd[128] = { 0 };
  29. printf("file_name=%s\n", file_name);
  30. if (type == NFLASH)
  31. sprintf(cmd, "ubifsload %s %s ", env_get("loadaddr"), file_name);
  32. else if (type == MMC)
  33. sprintf(cmd, "fatload mmc %d %s %s", index, env_get("loadaddr"), file_name);
  34. else if (type == USB)
  35. sprintf(cmd, "fatload usb %d %s %s", index, env_get("loadaddr"), file_name);
  36. printf("cmd=%s\n", cmd);
  37. ret = run_command(cmd, 0);
  38. return ret;
  39. }
  40. static int burn_data_2_partition(char *partition_name)
  41. {
  42. char cmd[128] = { 0 };
  43. int ret = -1;
  44. int file_size = 0;
  45. sprintf(cmd, "nand erase.part %s", partition_name);
  46. printf("cmd=%s\n", cmd);
  47. ret = run_command(cmd, 0);
  48. if (ret)
  49. return ret;
  50. file_size = env_get_ulong("filesize", 16, 0x2000);
  51. // sprintf(cmd, "nand write ${loadaddr} %s ${filesize}",partition_name);
  52. sprintf(cmd, "nand write %s %s 0x%x", env_get("loadaddr"), partition_name, file_size);
  53. printf("cmd=%s\n", cmd);
  54. ret = run_command(cmd, 0);
  55. return ret;
  56. }
  57. static int ark_update_partition(MEDIA_TYPE type, char card_index, char *partition_name, char *file_name)
  58. {
  59. int ret;
  60. ret = get_data_from_media(type, card_index, file_name);
  61. if (!ret) {
  62. burn_data_2_partition(partition_name);
  63. return 0;
  64. }
  65. return 1;
  66. }
  67. int do_UpdataOther(cmd_tbl_t * cmdtp, int flag, int argc, char *const argv[])
  68. {
  69. unsigned long type = 0;
  70. unsigned long card_index = 0;
  71. char *filename = NULL;
  72. char *partname = NULL;
  73. unsigned int ret = 0;
  74. strict_strtoul(argv[1], 16, &type);
  75. strict_strtoul(argv[2], 16, &card_index);
  76. partname = argv[3];
  77. filename = argv[4];
  78. ret = ark_update_partition(type, card_index, partname, filename);
  79. mdelay(1000);
  80. return ret;
  81. }
  82. U_BOOT_CMD(Updata, 5, 1, do_UpdataOther, "updata boot loader and save the ", "<interface> <dev[:part]> <filename>\n");
  83. unsigned int updata_the_ubootspl(void)
  84. {
  85. int ret;
  86. ret = get_data_from_media(2, 0, "ubootspl.bin");
  87. if (!ret)
  88. {
  89. run_command("switchecc 1", 0);
  90. mdelay(100);
  91. burn_data_2_partition("bootstrap");
  92. mdelay(100);
  93. run_command("switchecc 0", 0);
  94. return 1;
  95. }
  96. return 0;
  97. }
  98. #define ARK1668_UPDATE_MAGIC "ada7f0c6-7c86-11e9-8f9e-2a86e4085a59"
  99. int do_Update_flash(cmd_tbl_t * cmdtp, int flag, int argc, char *const argv[])
  100. {
  101. char cmd[32];
  102. unsigned int index = 0;
  103. unsigned int loadaddr;
  104. char *update_fdt = NULL;
  105. unsigned int ret = 0;
  106. sprintf((char *)cmd,"disconfig 0");
  107. run_command(cmd, 0);
  108. sprintf(cmd, "ubi part ota");
  109. printf("cmd=%s\n", cmd);
  110. run_command(cmd, 0);
  111. sprintf((char *)cmd,"disconfig 5");
  112. run_command(cmd, 0);
  113. mdelay(300);
  114. sprintf(cmd, "ubifsmount ubi:ota");
  115. printf("cmd=%s\n", cmd);
  116. run_command(cmd, 0);
  117. mdelay(300);
  118. sprintf((char *)cmd,"disconfig 8");
  119. run_command(cmd, 0);
  120. loadaddr = env_get_hex("loadaddr", 0);
  121. update_fdt = env_get("nandfdt");
  122. printf("update_fdt:%s\n", update_fdt);
  123. sprintf(cmd, "ubifsload %s %s ", env_get("loadaddr"),"update-magic");
  124. run_command(cmd, 0);
  125. if (loadaddr && !memcmp((void *)loadaddr, ARK1668_UPDATE_MAGIC, strlen(ARK1668_UPDATE_MAGIC))) {
  126. //env_set("update_from_flash", "no");
  127. //sprintf(cmd, "saveenv");
  128. printf("ARK1668_UPDATE_MAGIC CHECK OK!!\n");
  129. //run_command(cmd, 0);
  130. } else {
  131. printf("Wrong update magic, do not update from flash.\n");
  132. goto bootoldsys;
  133. }
  134. sprintf((char *)cmd,"disconfig 10");
  135. run_command(cmd, 0);
  136. printf("\r\n *****Now update ubootspl.bin ......\r\n");
  137. updata_the_ubootspl();
  138. printf("\r\n **** update from update bootloader .....\r\n");
  139. sprintf(cmd, "Updata 0x%x 0x%x %s %s", NFLASH, index, "bootloader", "u-boot.img");
  140. printf("cmd=%s\n", cmd);
  141. run_command(cmd, 0);
  142. sprintf((char *)cmd,"disconfig 15");
  143. run_command(cmd, 0);
  144. printf("\r\n **** update from update fdt .....\r\n");
  145. sprintf(cmd, "Updata 0x%x 0x%x %s %s", NFLASH, index, "fdt", update_fdt);
  146. printf("cmd=%s\n", cmd);
  147. ret = run_command(cmd, 0);
  148. mdelay(30);
  149. if(ret == 0)
  150. {
  151. sprintf(cmd, "setenv fdtsize %s",env_get("filesize"));
  152. run_command(cmd, 0);
  153. printf("cmd=%s\n", cmd);
  154. mdelay(30);
  155. }
  156. sprintf((char *)cmd,"disconfig 20");
  157. run_command(cmd, 0);
  158. printf("\r\n **** update from update kernel .....\r\n");
  159. sprintf(cmd, "Updata 0x%x 0x%x %s %s", NFLASH, index, "kernel", "zImage");
  160. printf("cmd=%s\n", cmd);
  161. ret = run_command(cmd, 0);
  162. mdelay(30);
  163. if(ret == 0)
  164. {
  165. sprintf(cmd, "setenv kernelsize %s",env_get("filesize"));
  166. printf("cmd=%s\n", cmd);
  167. run_command(cmd, 0);
  168. }
  169. sprintf((char *)cmd,"disconfig 35");
  170. run_command(cmd, 0);
  171. printf("\r\n **** update from update rootfs .....\r\n");
  172. sprintf(cmd, "Updata 0x%x 0x%x %s %s", NFLASH, index, "rootfs", "rootfs.ubi");
  173. printf("cmd=%s\n", cmd);
  174. ret = run_command(cmd, 0);
  175. mdelay(30);
  176. if(ret == 0)
  177. {
  178. sprintf(cmd, "setenv rootfssize %s",env_get("filesize"));
  179. printf("cmd=%s\n", cmd);
  180. run_command(cmd, 0);
  181. mdelay(30);
  182. }
  183. sprintf((char *)cmd,"disconfig 80");
  184. run_command(cmd, 0);
  185. printf("\r\n **** update from update bootloader back.....\r\n");
  186. sprintf(cmd, "Updata 0x%x 0x%x %s %s", NFLASH, index, "bootloader_back", "u-boot.img");
  187. printf("cmd=%s\n", cmd);
  188. run_command(cmd, 0);
  189. sprintf((char *)cmd,"disconfig 90");
  190. run_command(cmd, 0);
  191. env_set("update_from_flash", "no");
  192. mdelay(10);
  193. env_set("need_update", "no");
  194. mdelay(10);
  195. sprintf((char *)cmd,"disconfig 100");
  196. run_command(cmd, 0);
  197. bootoldsys:
  198. sprintf(cmd, "saveenv");
  199. printf("cmd=%s\n", cmd);
  200. run_command(cmd, 0);
  201. mdelay(20);
  202. sprintf(cmd, "run nandboot");
  203. printf("cmd=%s\n", cmd);
  204. run_command(cmd, 0);
  205. return 1;
  206. }
  207. U_BOOT_CMD(UpdateFlash, 4, 0, do_Update_flash, "updata Flash ", "<interface> <dev[:part]> <filename>\n");