update_cmd.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. #include <asm/arch/ark-common.h>
  21. extern struct SYS_INFO sys_info;
  22. extern int burn_data_to_partition(char *partition_name);
  23. extern int ark_check_data_from_partition(char *part_name,unsigned int crc_data);
  24. extern int ark_check_data_from_devide(char *file_name,unsigned int crc_data);
  25. typedef enum {
  26. MMC = 0,
  27. USB,
  28. NFLASH
  29. } MEDIA_TYPE;
  30. static int get_data_from_ota(char *file_name)
  31. {
  32. int ret = -1;
  33. char cmd[128] = { 0 };
  34. unsigned int file_size,crc_src;
  35. unsigned int *srcdata = (unsigned int *)(env_get_hex("loadaddr", 0));
  36. printf("file_name=%s\n", file_name);
  37. sprintf(cmd, "ubifsload %s %s ", env_get("loadaddr"), file_name);
  38. printf("cmd=%s\n", cmd);
  39. ret = run_command(cmd, 0);
  40. if(ret)
  41. return 1;
  42. file_size = env_get_ulong("filesize", 16, 0x2000);
  43. crc_src = crc32(0, (const unsigned char *)srcdata, file_size);
  44. ret = ark_check_data_from_devide(file_name,crc_src);
  45. if(ret)
  46. return 1;
  47. return 0;
  48. }
  49. static int ark_update_partition(char *partition_name, char *file_name)
  50. {
  51. int ret;
  52. ret = get_data_from_ota(file_name);
  53. if (!ret) {
  54. burn_data_to_partition(partition_name);
  55. return 0;
  56. }
  57. return 1;
  58. }
  59. unsigned int updata_the_ubootspl(void)
  60. {
  61. int ret;
  62. ret = get_data_from_ota("ubootspl.bin");
  63. if (!ret)
  64. {
  65. run_command("switchecc 1", 0);
  66. mdelay(100);
  67. ret = burn_data_to_partition("bootstrap");
  68. mdelay(100);
  69. run_command("switchecc 0", 0);
  70. if(ret)
  71. return 1;
  72. }
  73. return 0;
  74. }
  75. int get_crc_data_from_ota(char *file_name)
  76. {
  77. int ret = -1;
  78. unsigned int file_size;
  79. char cmd[128] = { 0 };
  80. unsigned int *srcdata = (unsigned int *)(env_get_hex("loadaddr", 0));
  81. memset(&sys_info, 0, sizeof(sys_info));
  82. printf("file_name=%s\n", file_name);
  83. sprintf(cmd, "ubifsload %s %s ", env_get("loadaddr"),file_name);
  84. printf("cmd=%s\n", cmd);
  85. ret = run_command(cmd, 0);
  86. if(ret)
  87. return 1;
  88. file_size = env_get_ulong("filesize", 16, 0x2000);
  89. memcpy(&sys_info, srcdata, file_size);
  90. printf("uboot_crc = 0x%x\n",sys_info.uboot_crc);
  91. printf("fdt_crc = 0x%x\n",sys_info.fdt_crc);
  92. printf("zImage_crc = 0x%x\n",sys_info.zImage_crc);
  93. printf("rootfs_crc = 0x%x\n",sys_info.rootfs_crc);
  94. printf("ubootspl_crc = 0x%x\n",sys_info.ubootspl_crc);
  95. printf("bootanimation_crc = 0x%x\n",sys_info.bootanimation_crc);
  96. printf("bootlogo_crc = 0x%x\n",sys_info.bootlogo_crc);
  97. return 0;
  98. }
  99. #define ARK1668_UPDATE_MAGIC "ada7f0c6-7c86-11e9-8f9e-2a86e4085a59"
  100. int do_Update_flash(cmd_tbl_t * cmdtp, int flag, int argc, char *const argv[])
  101. {
  102. char cmd[32];
  103. unsigned int index = 0;
  104. unsigned int loadaddr;
  105. char *update_fdt = NULL;
  106. unsigned int ret = 0;
  107. unsigned int resetuboot = 0;
  108. unsigned int dis_pos_x;
  109. unsigned int dis_pos_y;
  110. resetuboot = env_get_hex("ubootreset", 0);
  111. printf("resetuboot = %d \n",resetuboot);
  112. sprintf((char *)cmd,"disconfig 0");
  113. run_command(cmd, 0);
  114. sprintf(cmd, "ubi part ota");
  115. printf("cmd=%s\n", cmd);
  116. run_command(cmd, 0);
  117. sprintf((char *)cmd,"disconfig 5");
  118. run_command(cmd, 0);
  119. mdelay(300);
  120. sprintf(cmd, "ubifsmount ubi:ota");
  121. printf("cmd=%s\n", cmd);
  122. run_command(cmd, 0);
  123. mdelay(300);
  124. sprintf((char *)cmd,"disconfig 8");
  125. run_command(cmd, 0);
  126. loadaddr = env_get_hex("loadaddr", 0);
  127. update_fdt = env_get("boardfdt");
  128. printf("update_fdt:%s\n", update_fdt);
  129. sprintf(cmd, "ubifsload %s %s ", env_get("loadaddr"),"update-magic");
  130. run_command(cmd, 0);
  131. if (loadaddr && !memcmp((void *)loadaddr, ARK1668_UPDATE_MAGIC, strlen(ARK1668_UPDATE_MAGIC))) {
  132. printf("ARK1668_UPDATE_MAGIC CHECK OK!!\n");
  133. }else {
  134. printf("Wrong update magic, do not update from flash.\n");
  135. goto bootoldsys;
  136. }
  137. sprintf((char *)cmd,"disconfig 10");
  138. run_command(cmd, 0);
  139. get_crc_data_from_ota("crcdata.bin");
  140. dis_pos_x = 6;
  141. dis_pos_y = 20;
  142. printf("\r\n *****Now update ubootspl.bin ......\r\n");
  143. ret = updata_the_ubootspl();
  144. mdelay(30);
  145. if(!ret)
  146. {
  147. sprintf(cmd, "setenv bootstrapsize %s",env_get("filesize"));
  148. printf("cmd=%s\n", cmd);
  149. run_command(cmd, 0);
  150. mdelay(30);
  151. sprintf((char *)cmd,"disconfig 5");
  152. run_command(cmd, 0);
  153. mdelay(30);
  154. sprintf((char *)cmd,"disstring %d %d %s %d",dis_pos_x,dis_pos_y,"bootstrap",0);
  155. printf("cmd=%s\n", cmd);
  156. run_command(cmd, 0);
  157. }
  158. else
  159. {
  160. mdelay(30);
  161. sprintf((char *)cmd,"disstring %d %d %s %d",dis_pos_x,dis_pos_y,"bootstrap",1);
  162. printf("cmd=%s\n", cmd);
  163. run_command(cmd, 0);
  164. goto bootoldsys;
  165. }
  166. ret = ark_update_partition("bootloader", "u-boot.img");
  167. if(!ret)
  168. {
  169. sprintf(cmd, "setenv bootloadersize %s",env_get("filesize"));
  170. printf("cmd=%s\n", cmd);
  171. run_command(cmd, 0);
  172. mdelay(30);
  173. sprintf((char *)cmd,"disconfig 10");
  174. run_command(cmd, 0);
  175. mdelay(30);
  176. dis_pos_y = dis_pos_y + 20;
  177. sprintf((char *)cmd,"disstring %d %d %s %d",dis_pos_x,dis_pos_y,"bootloader",0);
  178. printf("cmd=%s\n", cmd);
  179. run_command(cmd, 0);
  180. if(resetuboot == 0 )
  181. {
  182. env_set("ubootreset", "1");
  183. env_set("update_from_ota", "yes");
  184. env_set("need_update", "no");
  185. run_command("saveenv", 0);
  186. mdelay(20);
  187. run_command("reset", 0);
  188. while(1);
  189. }
  190. env_set("ubootreset", "0");
  191. run_command("saveenv", 0);
  192. mdelay(20);
  193. }
  194. else
  195. {
  196. dis_pos_y = dis_pos_y + 20;
  197. sprintf((char *)cmd,"disstring %d %d %s %d",dis_pos_x,dis_pos_y,"bootloader",1);
  198. printf("cmd=%s\n", cmd);
  199. run_command(cmd, 0);
  200. goto bootoldsys;
  201. }
  202. ret = ark_update_partition("fdt", update_fdt);
  203. if(!ret)
  204. {
  205. dis_pos_y = dis_pos_y + 20;
  206. sprintf(cmd, "setenv fdtsize %s",env_get("filesize"));
  207. run_command(cmd, 0);
  208. printf("cmd=%s\n", cmd);
  209. mdelay(30);
  210. sprintf((char *)cmd,"disconfig 15");
  211. run_command(cmd, 0);
  212. mdelay(30);
  213. sprintf((char *)cmd,"disstring %d %d %s %d",dis_pos_x,dis_pos_y,"fdt",0);
  214. printf("cmd=%s\n", cmd);
  215. run_command(cmd, 0);
  216. }
  217. else
  218. {
  219. mdelay(30);
  220. dis_pos_y = dis_pos_y + 20;
  221. sprintf((char *)cmd,"disstring %d %d %s %d",dis_pos_x,dis_pos_y,"fdt",1);
  222. printf("cmd=%s\n", cmd);
  223. run_command(cmd, 0);
  224. goto bootoldsys;
  225. }
  226. ret = ark_update_partition("kernel", "zImage");
  227. if(!ret)
  228. {
  229. sprintf(cmd, "setenv kernelsize_a %s",env_get("filesize"));
  230. printf("cmd=%s\n", cmd);
  231. run_command(cmd, 0);
  232. mdelay(30);
  233. sprintf((char *)cmd,"disconfig 25");
  234. run_command(cmd, 0);
  235. mdelay(30);
  236. dis_pos_y = dis_pos_y + 20;
  237. sprintf((char *)cmd,"disstring %d %d %s %d",dis_pos_x,dis_pos_y,"kernel",0);
  238. printf("cmd=%s\n", cmd);
  239. run_command(cmd, 0);
  240. }
  241. else
  242. {
  243. dis_pos_y = dis_pos_y + 20;
  244. sprintf((char *)cmd,"disstring %d %d %s %d",dis_pos_x,dis_pos_y,"kernel",1);
  245. printf("cmd=%s\n", cmd);
  246. run_command(cmd, 0);
  247. goto bootoldsys;
  248. }
  249. #if 0
  250. ret = ark_update_partition("bootanimation", "bootanimation");
  251. if(!ret)
  252. {
  253. sprintf(cmd, "setenv bootanimationsize %s",env_get("filesize"));
  254. run_command(cmd, 0);
  255. printf("cmd=%s\n", cmd);
  256. mdelay(30);
  257. sprintf((char *)cmd,"disconfig 30");
  258. run_command(cmd, 0);
  259. mdelay(30);
  260. dis_pos_y = dis_pos_y + 20;
  261. sprintf((char *)cmd,"disstring %d %d %s %d",dis_pos_x,dis_pos_y,"bootanimation",0);
  262. printf("cmd=%s\n", cmd);
  263. run_command(cmd, 0);
  264. }
  265. else
  266. {
  267. dis_pos_y = dis_pos_y + 20;
  268. sprintf((char *)cmd,"disstring %d %d %s %d",dis_pos_x,dis_pos_y,"bootanimation",1);
  269. printf("cmd=%s\n", cmd);
  270. run_command(cmd, 0);
  271. goto bootoldsys;
  272. }
  273. #endif
  274. ret = ark_update_partition("bootloader_bak", "u-boot.img");
  275. if(!ret)
  276. {
  277. sprintf((char *)cmd,"disconfig 40");
  278. run_command(cmd, 0);
  279. dis_pos_y = dis_pos_y + 20;
  280. sprintf((char *)cmd,"disstring %d %d %s %d",dis_pos_x,dis_pos_y,"bootloader_bak",0);
  281. printf("cmd=%s\n", cmd);
  282. run_command(cmd, 0);
  283. }
  284. else
  285. {
  286. dis_pos_y = dis_pos_y + 20;
  287. sprintf((char *)cmd,"disstring %d %d %s %d",dis_pos_x,dis_pos_y,"bootloader_bak",1);
  288. printf("cmd=%s\n", cmd);
  289. run_command(cmd, 0);
  290. goto bootoldsys;
  291. }
  292. ret = ark_update_partition("rootfs","rootfs.ubi");
  293. if(!ret)
  294. {
  295. sprintf((char *)cmd,"disconfig 95");
  296. run_command(cmd, 0);
  297. mdelay(30);
  298. dis_pos_y = dis_pos_y + 20;
  299. sprintf((char *)cmd,"disstring %d %d %s %d",dis_pos_x,dis_pos_y,"rootfs",0);
  300. printf("cmd=%s\n", cmd);
  301. run_command(cmd, 0);
  302. }
  303. else
  304. {
  305. mdelay(30);
  306. dis_pos_y = dis_pos_y + 20;
  307. sprintf((char *)cmd,"disstring %d %d %s %d",dis_pos_x,dis_pos_y,"rootfs",1);
  308. printf("cmd=%s\n", cmd);
  309. run_command(cmd, 0);
  310. goto bootoldsys;
  311. }
  312. printf("##set update_from_flash no##\n");
  313. env_set("update_from_ota", "no");
  314. mdelay(10);
  315. env_set("need_update", "no");
  316. mdelay(10);
  317. sprintf(cmd, "saveenv");
  318. printf("cmd=%s\n", cmd);
  319. run_command(cmd, 0);
  320. sprintf((char *)cmd,"disconfig 100");
  321. run_command(cmd, 0);
  322. return 0;
  323. bootoldsys:
  324. env_set("update_from_ota", "no");
  325. mdelay(10);
  326. sprintf(cmd, "saveenv");
  327. printf("cmd=%s\n", cmd);
  328. run_command(cmd, 0);
  329. mdelay(20);
  330. sprintf(cmd, "run nandboot");
  331. printf("cmd=%s\n", cmd);
  332. run_command(cmd, 0);
  333. return 1;
  334. }
  335. U_BOOT_CMD(UpdateFlash, 4, 0, do_Update_flash, "updata Flash ", "<interface> <dev[:part]> <filename>\n");