dl_commands.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2015. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. */
  31. #include <stdlib.h>
  32. #include <app.h>
  33. #include <debug.h>
  34. #include <arch/arm.h>
  35. #include <dev/udc.h>
  36. #include <string.h>
  37. #include <kernel/thread.h>
  38. #include <kernel/event.h>
  39. #include <arch/ops.h>
  40. #include <target.h>
  41. #include <platform.h>
  42. #include <platform/mt_reg_base.h>
  43. #include <platform/boot_mode.h>
  44. #include <platform/mtk_wdt.h>
  45. #include <platform/mt_rtc.h>
  46. #include <bootimg.h>
  47. #ifdef MTK_GPT_SCHEME_SUPPORT
  48. #include <platform/partition.h>
  49. #else
  50. #include <mt_partition.h>
  51. #endif
  52. #if !defined(MTK_EMMC_SUPPORT) && !defined(MTK_UFS_SUPPORT)
  53. #include <platform/mtk_nand.h>
  54. #endif
  55. #include <img_info.h>
  56. #include <part_interface.h>
  57. #include <block_generic_interface.h>
  58. /*For gpt update*/
  59. #ifdef MTK_GPT_SCHEME_SUPPORT
  60. #if defined(PLATFORM_FASTBOOT_EMPTY_STORAGE) || defined(MTK_GPT_UPDATE_SUPPORT)
  61. extern part_t *partition;
  62. extern int gpt_partition_table_update(const char *arg, void *data, unsigned sz);
  63. extern int read_gpt(part_t *part);
  64. #endif
  65. #endif
  66. /*For image write*/
  67. #include "sparse_format.h"
  68. #include "dl_commands.h"
  69. #ifdef MTK_MMC_COMBO_DRV
  70. #include <mmc_core.h>
  71. #else
  72. #include <platform/mmc_core.h>
  73. #endif
  74. #include <platform/mt_gpt.h>
  75. #include <platform/errno.h>
  76. #ifdef FASTBOOT_WHOLE_FLASH_SUPPORT
  77. extern part_t *partition;
  78. extern int read_gpt(part_t *part);
  79. #endif
  80. #include "fastboot.h"
  81. #include "mt_pmic.h"
  82. #include "blockheader.h"
  83. #ifdef MTK_ULTRA_FLASH
  84. #include "transfer.h"
  85. #endif
  86. #ifdef MBLOCK_LIB_SUPPORT
  87. #include <mblock.h>
  88. #endif
  89. #define MODULE_NAME "FASTBOOT_DOWNLOAD"
  90. #define MAX_RSP_SIZE 64
  91. extern void *download_base;
  92. #if defined(MTK_MLC_NAND_SUPPORT) || defined(MTK_TLC_NAND_SUPPORT)
  93. extern unsigned long long download_max;
  94. #else
  95. extern unsigned download_max;
  96. #endif
  97. extern unsigned download_size;
  98. extern unsigned fastboot_state;
  99. /*LXO: !Download related command*/
  100. #define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
  101. #define INVALID_PTN -1
  102. //For test: Display info on boot screen
  103. #define DISPLAY_INFO_ON_LCM
  104. #if (defined(MTK_UFS_SUPPORT) || defined(MTK_EMMC_SUPPORT)) //use another macro.
  105. #define EMMC_TYPE
  106. #else
  107. #define NAND_TYPE
  108. #endif
  109. #if defined(MTK_SPI_NOR_SUPPORT)
  110. #define NOR_TYPE
  111. #endif
  112. extern void video_printf (const char *fmt, ...);
  113. extern int video_get_rows(void);
  114. extern void video_set_cursor(int row, int col);
  115. extern void video_clean_screen(void);
  116. #if defined(MTK_MLC_NAND_SUPPORT) || defined(MTK_TLC_NAND_SUPPORT)
  117. extern int nand_write_img(u64 addr, void *data, u32 img_sz,u64 partition_size,int partition_type);
  118. extern int nand_write_img_ex(u64 addr, void *data, u32 length,u64 total_size, u32 *next_offset, u64 partition_start,u64 partition_size, int img_type);
  119. #else
  120. extern int nand_write_img(u32 addr, void *data, u32 img_sz,u32 partition_size,int partition_type);
  121. extern int nand_write_img_ex(u32 addr, void *data, u32 length,u32 total_size, u32 *next_offset, u32 partition_start,u32 partition_size, int img_type);
  122. #endif
  123. extern u32 gpt4_tick2time_ms (u32 tick);
  124. unsigned start_time_ms;
  125. #define TIME_STAMP gpt4_tick2time_ms(gpt4_get_current_tick())
  126. #define TIME_START {start_time_ms = gpt4_tick2time_ms(gpt4_get_current_tick());}
  127. #define TIME_ELAPSE (gpt4_tick2time_ms(gpt4_get_current_tick()) - start_time_ms)
  128. extern int usb_write(void *buf, unsigned len);
  129. extern int usb_read(void *buf, unsigned len);
  130. extern int sec_dl_permission_chk(const char *part_name, unsigned int *permitted);
  131. extern int sec_format_permission_chk(const char *part_name, unsigned int *permitted);
  132. bool flash_storage(const char* arg, void* data, unsigned sz);
  133. /* todo: give lk strtoul and nuke this */
  134. static u64 hex2uint64(const char *x)
  135. {
  136. u64 n = 0;
  137. while (*x) {
  138. switch (*x) {
  139. case '0':
  140. case '1':
  141. case '2':
  142. case '3':
  143. case '4':
  144. case '5':
  145. case '6':
  146. case '7':
  147. case '8':
  148. case '9':
  149. n = (n << 4) | (*x - '0');
  150. break;
  151. case 'a':
  152. case 'b':
  153. case 'c':
  154. case 'd':
  155. case 'e':
  156. case 'f':
  157. n = (n << 4) | (*x - 'a' + 10);
  158. break;
  159. case 'A':
  160. case 'B':
  161. case 'C':
  162. case 'D':
  163. case 'E':
  164. case 'F':
  165. n = (n << 4) | (*x - 'A' + 10);
  166. break;
  167. default:
  168. return n;
  169. }
  170. x++;
  171. }
  172. return n;
  173. }
  174. static unsigned hex2unsigned(const char *x)
  175. {
  176. return (unsigned)hex2uint64(x);
  177. }
  178. extern BOOT_ARGUMENT *g_boot_arg;
  179. void* special_heap_alloc(int length)
  180. {
  181. void* buf = (void*)(u32)mblock_reserve_ext(&g_boot_arg->mblock_info,
  182. length, 0x10000, 0xC0000000, 0, "fastboot_heap");
  183. return buf;
  184. }
  185. static void init_display_xy()
  186. {
  187. #if defined(DISPLAY_INFO_ON_LCM)
  188. video_clean_screen();
  189. video_set_cursor(video_get_rows()/2, 0);
  190. //video_set_cursor(1, 0);
  191. #endif
  192. }
  193. static void display_info(const char* msg)
  194. {
  195. #if defined(DISPLAY_INFO_ON_LCM)
  196. if (msg == 0) {
  197. return;
  198. }
  199. video_printf("%s\n", msg);
  200. #endif
  201. }
  202. void display_progress(const char* msg_prefix, unsigned size, unsigned totle_size)
  203. {
  204. #if defined(DISPLAY_INFO_ON_LCM)
  205. unsigned vel = 0;
  206. u64 prog = 0;
  207. unsigned time = TIME_ELAPSE;
  208. if (msg_prefix == 0) {
  209. msg_prefix = "Unknown";
  210. }
  211. if (time != 0) {
  212. vel = (unsigned)(size / time); //approximate 1024/1000
  213. vel /= 1000;
  214. time /= 1000;
  215. }
  216. if (totle_size != 0) {
  217. prog = (u64)size*100/totle_size;
  218. }
  219. video_printf("%s > %3d%% Time:%4d s Vel:%3d MB/s ", msg_prefix, (unsigned)prog, time, vel);
  220. #endif
  221. }
  222. static void display_speed_info(const char* msg_prefix, unsigned size)
  223. {
  224. #if defined(DISPLAY_INFO_ON_LCM)
  225. unsigned vel = 0;
  226. unsigned time = TIME_ELAPSE;
  227. if (msg_prefix == 0) {
  228. msg_prefix = "Unknown";
  229. }
  230. if (time != 0) {
  231. vel = (unsigned)(size / time); //approximate 1024/1000
  232. vel /= 1000;
  233. time /= 1000;
  234. }
  235. video_printf("\n%s Time:%d s Vel:%d MB/s \n", msg_prefix, time, vel);
  236. #endif
  237. }
  238. static void fastboot_fail_wrapper(const char* msg)
  239. {
  240. display_info(msg);
  241. fastboot_fail(msg);
  242. }
  243. static void fastboot_ok_wrapper(const char* msg, unsigned data_size)
  244. {
  245. display_speed_info(msg, data_size);
  246. fastboot_okay("");
  247. }
  248. void cmd_install_sig(const char *arg, void *data, unsigned sz)
  249. {
  250. fastboot_fail_wrapper("Signature command not supported");
  251. }
  252. static char error_msg[512];
  253. void set_response_msg(const char* s)
  254. {
  255. strncpy(error_msg, s, 511);
  256. }
  257. bool power_check()
  258. {
  259. //3500 mV. shut down threshold 3.45V
  260. if (get_bat_sense_volt(5) < 3500) {
  261. return false;
  262. } else {
  263. return true;
  264. }
  265. }
  266. #ifdef MTK_ULTRA_FLASH
  267. bool g_ultra_flash_enable = true;
  268. char partition_name_history[MAX_PARTITION_NAME_LEN]={0};
  269. int partition_name_history_len = 0;
  270. void switch_max_download_len(bool ultra_en);
  271. bool is_special_partition(const char* partition);
  272. bool cmd_download_ultra(const char *arg, void *data, unsigned sz);
  273. bool cmd_download_standard(const char *arg, void *data, unsigned sz);
  274. #endif
  275. void cmd_download(const char *arg, void *data, unsigned sz)
  276. {
  277. bool result = false;
  278. #ifdef MTK_ULTRA_FLASH
  279. if(g_ultra_flash_enable && (partition_name_history_len!=0) && (!is_special_partition(partition_name_history)))
  280. {
  281. result = cmd_download_ultra(arg, data, sz);
  282. }
  283. else
  284. {
  285. result = cmd_download_standard(arg, data, sz);
  286. }
  287. if(!result)
  288. {
  289. //restore the default max download size.
  290. switch_max_download_len(false);
  291. }
  292. #else
  293. cmd_download_standard(arg, data, sz);
  294. #endif
  295. return;
  296. }
  297. bool cmd_download_standard(const char *arg, void *data, unsigned sz)
  298. {
  299. char response[MAX_RSP_SIZE];
  300. unsigned len = hex2unsigned(arg);
  301. int r;
  302. if (!power_check()) {
  303. fastboot_fail("low power, need battery charging.");
  304. return false;
  305. }
  306. init_display_xy();
  307. download_size = 0;
  308. if (len > download_max) {
  309. fastboot_fail_wrapper("data is too large");
  310. return false;
  311. }
  312. if(snprintf(response, MAX_RSP_SIZE, "DATA%08x", len) < 0)
  313. {
  314. fastboot_fail("ghost snprintf error.");
  315. return false;
  316. }
  317. if (usb_write(response, strlen(response)) < 0) {
  318. return false;
  319. }
  320. display_info("USB Transferring... ");
  321. TIME_START;
  322. r = usb_read(download_base, len);
  323. if ((r < 0) || ((unsigned) r != len)) {
  324. fastboot_fail_wrapper("Read USB error");
  325. fastboot_state = STATE_ERROR;
  326. return false;
  327. }
  328. download_size = len;
  329. fastboot_ok_wrapper("USB Transmission OK", len);
  330. return true;
  331. }
  332. #ifdef NAND_TYPE
  333. int get_nand_image_type(const char* arg)
  334. {
  335. int img_type = 0;
  336. if (!strncmp(arg, "system", strlen("system")) ||
  337. !strcmp(arg, "userdata") ||
  338. !strcmp(arg, "fat") ) {
  339. #if defined(MTK_NAND_UBIFS_SUPPORT) || defined(MTK_NAND_MTK_FTL_SUPPORT)
  340. img_type = UBIFS_IMG;
  341. #else
  342. img_type = YFFS2_IMG;
  343. #endif
  344. } else {
  345. img_type = RAW_DATA_IMG;
  346. }
  347. return img_type;
  348. }
  349. #endif
  350. extern part_dev_t *mt_part_get_device(void);
  351. bool cmd_flash_mmc_img(const char* arg, void* data, unsigned sz)
  352. {
  353. unsigned long long ptn = 0;
  354. unsigned long long size = 0;
  355. part_dev_t *dev;
  356. int index = INVALID_PTN;
  357. #if (defined(MTK_UFS_SUPPORT) || defined(MTK_NEW_COMBO_EMMC_SUPPORT))
  358. unsigned int part_id;
  359. #endif
  360. #ifdef MTK_SPI_NOR_SUPPORT
  361. if (!strcmp(arg, "NOR")) {
  362. ptn = 0;
  363. part_id = 0; /* If need, example : NOR_PART_BOOT1 */
  364. goto write_nor_part;
  365. } else
  366. #endif
  367. #ifdef FASTBOOT_WHOLE_FLASH_SUPPORT
  368. if (!strcmp(arg, "boot0")) {
  369. ptn = 0;
  370. part_id = EMMC_PART_BOOT1;
  371. mmc_emmc_boot_prepare();
  372. goto write_part;
  373. } else if (!strcmp(arg,"boot1")) {
  374. ptn = 0;
  375. part_id = EMMC_PART_BOOT2;
  376. goto write_part;
  377. } else if (!strcmp(arg, "partition")) {
  378. ptn = 0;
  379. part_id = EMMC_PART_USER;
  380. goto write_part;
  381. }
  382. #else
  383. if (!strcmp(arg, "partition")) {
  384. dprintf(ALWAYS, "Attempt to write partition image.\n");
  385. set_response_msg("Do not support this operation.");
  386. return false;
  387. }
  388. #endif
  389. else {
  390. index = partition_get_index(arg);
  391. if (index == INVALID_PTN) {
  392. set_response_msg("This partition doesn't exist");
  393. return false;
  394. }
  395. ptn = partition_get_offset(index);
  396. if (ptn == (unsigned long long)(-1)) {
  397. set_response_msg("partition table doesn't exist");
  398. return false;
  399. }
  400. #if (defined(MTK_UFS_SUPPORT) || defined(MTK_NEW_COMBO_EMMC_SUPPORT))
  401. part_id = partition_get_region(index);
  402. #endif
  403. if (!strcmp(arg, "boot") || !strcmp(arg, "boot_a") ||
  404. !strcmp(arg, "boot_b") || !strcmp(arg, "recovery")) {
  405. if (memcmp((void *)data, BOOTIMG_MAGIC, BOOTIMG_MAGIC_SZ-1)) {
  406. set_response_msg("image is not a boot image");
  407. return false;
  408. }
  409. }
  410. if (!strncmp(arg, "preloader", strlen("preloader"))) {
  411. dev = mt_part_get_device();
  412. #if (defined(MTK_EMMC_SUPPORT) || defined(MTK_UFS_SUPPORT))
  413. if (dev->blkdev->type == BOOTDEV_SDMMC) {
  414. int boot_wp = mmc_get_card(0)->raw_ext_csd[EXT_CSD_BOOT_WP];
  415. if (boot_wp & EXT_CSD_BOOT_WP_EN_PERM_WP) {
  416. set_response_msg("flash preloader is not permitted.");
  417. return false;
  418. }
  419. }
  420. #endif
  421. #ifdef PLATFORM_FASTBOOT_EMPTY_STORAGE
  422. if (process_preloader(data, &sz)) {
  423. set_response_msg("not a valid preloader.");
  424. return false;
  425. }
  426. ptn = 0;
  427. #else
  428. if (is_preloader_bin_format(data)) {
  429. //preloader_xxx.bin
  430. if (dev->blkdev->type == BOOTDEV_SDMMC)
  431. ptn = HEADER_BLOCK_SIZE_EMMC;
  432. else if (dev->blkdev->type == BOOTDEV_UFS)
  433. ptn = HEADER_BLOCK_SIZE_UFS;
  434. else
  435. ptn = HEADER_BLOCK_SIZE_NAND;
  436. }
  437. else //migh be preloader.img. offset = 0
  438. ptn = 0;
  439. #endif
  440. }
  441. size = partition_get_size(index);
  442. if (ROUND_TO_PAGE(sz,511) > size) {
  443. set_response_msg("size too large");
  444. return false;
  445. }
  446. }
  447. #ifdef FASTBOOT_WHOLE_FLASH_SUPPORT
  448. write_part:
  449. #endif // FASTBOOT_WHOLE_FLASH_SUPPORT
  450. #ifdef EMMC_TYPE
  451. #if (defined(MTK_UFS_SUPPORT) || defined(MTK_NEW_COMBO_EMMC_SUPPORT))
  452. dprintf (ALWAYS, "partid %d, addr 0x%llx, size 0x%x\n", part_id, ptn, sz);
  453. if (emmc_write(part_id, ptn , (unsigned int *)data, sz) != sz)
  454. #else
  455. if (emmc_write(ptn , (unsigned int *)data, sz) != sz)
  456. #endif
  457. #endif
  458. #ifdef NAND_TYPE
  459. #if defined(MTK_MLC_NAND_SUPPORT) || defined(MTK_TLC_NAND_SUPPORT)
  460. if (nand_write_img((u64)ptn, (char*)data, sz,(u64)size,get_nand_image_type(arg)))
  461. #else
  462. if (nand_write_img((u32)ptn, (char*)data, sz,(u32)size,get_nand_image_type(arg)))
  463. #endif
  464. #endif
  465. {
  466. set_response_msg("flash write failure");
  467. return false;
  468. }
  469. #ifdef FASTBOOT_WHOLE_FLASH_SUPPORT
  470. if (!strcmp(arg, "partition")) {
  471. read_gpt(partition);
  472. }
  473. #endif
  474. //fastboot_okay("");
  475. return true;
  476. #ifdef NOR_TYPE
  477. #ifdef MTK_SPI_NOR_SUPPORT
  478. write_nor_part:
  479. dprintf (ALWAYS, "partid %d, addr 0x%llx, size 0x%x\n", part_id, ptn, sz);
  480. if (nor_write_part(part_id, ptn , (unsigned int *)data, (u64)sz) != sz) {
  481. set_response_msg("SPI NOR flash write failure");
  482. return false;
  483. }
  484. //fastboot_okay("");
  485. return false;
  486. #endif
  487. #endif
  488. }
  489. bool cmd_flash_mmc_sparse_img(const char* arg, void* data, unsigned sz)
  490. {
  491. unsigned int chunk;
  492. unsigned int chunk_data_sz;
  493. static uint32_t *fill_buf = NULL;
  494. uint32_t fill_val;
  495. uint32_t chunk_blk_cnt = 0;
  496. uint32_t i;
  497. unsigned long long size_wrote = 0;
  498. sparse_header_t *sparse_header;
  499. chunk_header_t *chunk_header;
  500. uint32_t total_blocks = 0;
  501. unsigned long long ptn = 0;
  502. unsigned long long size = 0;
  503. int index = INVALID_PTN;
  504. #if (defined(MTK_UFS_SUPPORT) || defined(MTK_NEW_COMBO_EMMC_SUPPORT))
  505. unsigned int part_id;
  506. #endif
  507. index = partition_get_index(arg);
  508. if (INVALID_PTN == index) {
  509. set_response_msg("This partition doesn't exist");
  510. return false;
  511. }
  512. ptn = partition_get_offset(index);
  513. if (ptn == (unsigned long long)(-1)) {
  514. set_response_msg("partition offset is wrong");
  515. return false;
  516. }
  517. dprintf(ALWAYS, "partition(%s) index is %d, ptn is 0x%llx\n", arg, index, ptn);
  518. #if (defined(MTK_UFS_SUPPORT) || defined(MTK_NEW_COMBO_EMMC_SUPPORT))
  519. part_id = partition_get_region(index);
  520. #endif
  521. size = partition_get_size(index);
  522. /* Read and skip over sparse image header */
  523. sparse_header = (sparse_header_t *) data;
  524. dprintf(ALWAYS, "Image size span 0x%llx, partition size 0x%llx\n", (unsigned long long)sparse_header->total_blks*sparse_header->blk_sz, size);
  525. if ((unsigned long long)sparse_header->total_blks*sparse_header->blk_sz > size) {
  526. set_response_msg("sparse image size span overflow.");
  527. return false;
  528. }
  529. if(sparse_header->file_hdr_sz > sz)
  530. {
  531. set_response_msg("sparse image header invalid.");
  532. return false;
  533. }
  534. data += sizeof(sparse_header_t);
  535. if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) {
  536. /* Skip the remaining bytes in a header that is longer than
  537. * we expected.
  538. */
  539. data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
  540. }
  541. dprintf (ALWAYS, "=== Sparse Image Header ===\n");
  542. dprintf (ALWAYS, "magic: 0x%x\n", sparse_header->magic);
  543. dprintf (ALWAYS, "major_version: 0x%x\n", sparse_header->major_version);
  544. dprintf (ALWAYS, "minor_version: 0x%x\n", sparse_header->minor_version);
  545. dprintf (ALWAYS, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
  546. dprintf (ALWAYS, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
  547. dprintf (ALWAYS, "blk_sz: %d\n", sparse_header->blk_sz);
  548. dprintf (ALWAYS, "total_blks: %d\n", sparse_header->total_blks);
  549. dprintf (ALWAYS, "total_chunks: %d\n", sparse_header->total_chunks);
  550. display_info("\nWriting Flash ... ");
  551. /* Start processing chunks */
  552. for (chunk=0; chunk<sparse_header->total_chunks; chunk++) {
  553. /* Read and skip over chunk header */
  554. chunk_header = (chunk_header_t *)data;
  555. data += sizeof(chunk_header_t);
  556. //dprintf (ALWAYS, "=== Chunk Header ===\n");
  557. //dprintf (ALWAYS, "chunk_type: 0x%x\n", chunk_header->chunk_type);
  558. //dprintf (ALWAYS, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
  559. //dprintf (ALWAYS, "total_size: 0x%x\n", chunk_header->total_sz);
  560. if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) {
  561. /* Skip the remaining bytes in a header that is longer than
  562. * we expected.
  563. */
  564. data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
  565. }
  566. if(size/sparse_header->blk_sz < chunk_header->chunk_sz)
  567. {
  568. set_response_msg("sparse chunk size is too big.");
  569. return false;
  570. }
  571. chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
  572. //check chunk bounadary
  573. if(sparse_header->blk_sz * ((unsigned long long)chunk_header->chunk_sz + total_blocks) > size)
  574. {
  575. set_response_msg("sparse chunk size overflow.");
  576. return false;
  577. }
  578. if((sparse_header->total_blks - total_blocks) < chunk_header->chunk_sz)
  579. {
  580. set_response_msg("sparse chunk blocks bigger than total blocks.");
  581. return false;
  582. }
  583. switch (chunk_header->chunk_type) {
  584. case CHUNK_TYPE_RAW:
  585. {
  586. if (chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
  587. chunk_data_sz)) {
  588. set_response_msg("Bogus chunk size for chunk type Raw");
  589. return false;
  590. }
  591. dprintf (ALWAYS, "Raw: start block addr: 0x%x\n", total_blocks);
  592. #ifdef EMMC_TYPE
  593. #if (defined(MTK_UFS_SUPPORT) || defined(MTK_NEW_COMBO_EMMC_SUPPORT))
  594. //dprintf (ALWAYS, "partid %d, addr 0x%llx, partsz 0x%x\n", part_id, ptn + ((unsigned long long)total_blocks*sparse_header->blk_sz) , chunk_data_sz);
  595. if (emmc_write(part_id, ptn + ((unsigned long long)total_blocks*sparse_header->blk_sz) , data, chunk_data_sz) != chunk_data_sz)
  596. #else
  597. if (emmc_write(ptn + ((unsigned long long)total_blocks*sparse_header->blk_sz) , data, chunk_data_sz) != chunk_data_sz)
  598. #endif
  599. #endif
  600. #ifdef NAND_TYPE
  601. #if defined(MTK_MLC_NAND_SUPPORT) || defined(MTK_TLC_NAND_SUPPORT)
  602. if (nand_write_img((u64)(ptn + ((unsigned long long)total_blocks*sparse_header->blk_sz)), (char*)data, chunk_data_sz,(u64)size,get_nand_image_type(arg)))
  603. #else
  604. if (nand_write_img((u32)(ptn + ((unsigned long long)total_blocks*sparse_header->blk_sz)), (char*)data, chunk_data_sz,(u32)size,get_nand_image_type(arg)))
  605. #endif
  606. #endif
  607. {
  608. set_response_msg("flash write failure");
  609. return false;
  610. }
  611. total_blocks += chunk_header->chunk_sz;
  612. data += chunk_data_sz;
  613. break;
  614. }
  615. case CHUNK_TYPE_DONT_CARE:
  616. {
  617. dprintf (ALWAYS, "!!Blank: start: 0x%x offset: 0x%x\n", total_blocks, chunk_header->chunk_sz);
  618. total_blocks += chunk_header->chunk_sz;
  619. break;
  620. }
  621. case CHUNK_TYPE_FILL:
  622. {
  623. dprintf (ALWAYS, "%s %d: CHUNK_TYPE_FILL=0x%x size=%d chunk_data_sz=%d\n", __FUNCTION__, __LINE__, *(uint32_t *)data, ROUNDUP(sparse_header->blk_sz, CACHE_LINE), chunk_data_sz);
  624. if (chunk_header->total_sz != (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) {
  625. set_response_msg("Bogus chunk size for chunk type FILL");
  626. return false;
  627. }
  628. #define FILL_BUF_BLOCK_CNT ((4*1024*1024)/sparse_header->blk_sz)
  629. #define FILL_BUF_LEN (FILL_BUF_BLOCK_CNT * sparse_header->blk_sz)
  630. //fill_buf = (uint32_t *)memalign(CACHE_LINE, FILL_BUF_LEN);
  631. if(fill_buf == NULL)
  632. {
  633. fill_buf = special_heap_alloc(FILL_BUF_LEN);
  634. }
  635. if (!fill_buf) {
  636. set_response_msg("Malloc failed for: CHUNK_TYPE_FILL");
  637. return false;
  638. }
  639. fill_val = *(uint32_t *)data;
  640. data = (char *) data + sizeof(uint32_t);
  641. chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
  642. unsigned int int_cnt = FILL_BUF_LEN / sizeof(fill_val);
  643. for (i = 0; i < int_cnt; i++) {
  644. fill_buf[i] = fill_val;
  645. }
  646. unsigned int left = chunk_blk_cnt;
  647. while (left > 0) {
  648. unsigned int todo = (left > FILL_BUF_BLOCK_CNT) ? FILL_BUF_BLOCK_CNT : left;
  649. #if (defined(MTK_UFS_SUPPORT) || defined(MTK_NEW_COMBO_EMMC_SUPPORT))
  650. size_wrote = emmc_write(part_id, ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
  651. fill_buf, (u64)todo * sparse_header->blk_sz);
  652. #else
  653. size_wrote = emmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
  654. fill_buf, (u64)todo * sparse_header->blk_sz);
  655. #endif
  656. if (size_wrote != (unsigned long long)todo * sparse_header->blk_sz) {
  657. set_response_msg("CHUNK_TYPE_FILL flash write failure");
  658. //free(fill_buf);
  659. return false;
  660. }
  661. total_blocks += todo;
  662. left -= todo;
  663. }
  664. //free(fill_buf);
  665. break;
  666. }
  667. case CHUNK_TYPE_CRC:
  668. {
  669. if (chunk_header->total_sz != sparse_header->chunk_hdr_sz) {
  670. set_response_msg("Bogus chunk size for chunk type Dont Care");
  671. return false;
  672. }
  673. total_blocks += chunk_header->chunk_sz;
  674. data += chunk_data_sz;
  675. break;
  676. }
  677. default:
  678. set_response_msg("Unknown chunk type");
  679. return false;
  680. }
  681. display_progress("\rWrite Data", total_blocks*sparse_header->blk_sz, sparse_header->total_blks*sparse_header->blk_sz);
  682. }
  683. dprintf(ALWAYS, "Wrote %d blocks, expected to write %d blocks\n",
  684. total_blocks, sparse_header->total_blks);
  685. if (total_blocks != sparse_header->total_blks) {
  686. set_response_msg("sparse image write failure");
  687. return false;
  688. } else {
  689. display_info("\n\nOK");
  690. fastboot_okay("");
  691. }
  692. return true;
  693. }
  694. extern int mboot_recovery_load_raw_part_offset(char *part_name, unsigned long *addr, unsigned long offset, unsigned int size) __attribute__((weak));
  695. int _virtual_partition_support(const char *arg, void *data, unsigned int* p_sz, char **modified_arg)
  696. {
  697. #define TMP_DOWNLOAD_SIZE 52*1024*1024 //52MB, because kernel size(64M) - pagetable max size (8M) - ramdisk size(Estimate 4M)
  698. unsigned t_bootimg_addr = ((u32)data);
  699. void* new_data =(void *)((u32)data + SCRATCH_SIZE - TMP_DOWNLOAD_SIZE);
  700. bool _is_zimage = false;
  701. struct bootimg_hdr *p_boot_hdr;
  702. unsigned int t_kernel_addr;
  703. unsigned int t_ramdisk_addr;
  704. unsigned int sz = *p_sz;
  705. char* tmp = (void *)arg;
  706. int ret;
  707. if (!memcmp(arg, "zimage", strlen("zimage"))) {
  708. _is_zimage = true;
  709. dprintf(INFO,"Get zimage\n");
  710. } else if (!memcmp(arg, "ramdisk", strlen("ramdisk"))) {
  711. dprintf(INFO,"Get ramdisk\n");
  712. } else {
  713. return 0;
  714. }
  715. /* check size first */
  716. if (sz > TMP_DOWNLOAD_SIZE)
  717. return -ENOMEM;
  718. /* copy ori data to temp addr */
  719. memcpy(new_data, data, sz);
  720. /* copy bootimg header content */
  721. //clean space for img_hdr make the debug easier, 8k is enough for most case (page size < 8k)
  722. memset(data, 0, 0x2000);
  723. #ifdef MTK_GPT_SCHEME_SUPPORT
  724. ret = mboot_recovery_load_raw_part_offset("boot", (void *)t_bootimg_addr, 0, sizeof(struct bootimg_hdr));
  725. #else
  726. ret = mboot_recovery_load_raw_part_offset(PART_BOOTIMG, (void *)t_bootimg_addr, 0, sizeof(struct bootimg_hdr));
  727. #endif
  728. if (ret < 0) {
  729. return ret;
  730. }
  731. p_boot_hdr = (void *)t_bootimg_addr;
  732. dprintf(INFO,"ori kernel_sz: %x\n", p_boot_hdr->kernel_sz);
  733. dprintf(INFO,"ori ramdisk_sz: %x\n", p_boot_hdr->ramdisk_sz);
  734. t_kernel_addr = t_bootimg_addr + p_boot_hdr->page_sz;
  735. if (_is_zimage == true) {
  736. /* copy kernel content from new_data*/
  737. memcpy((void *)t_kernel_addr, new_data, sz);
  738. /* copy ramdisk content from flash */
  739. t_ramdisk_addr = ROUNDUP(t_kernel_addr + sz, p_boot_hdr->page_sz);
  740. #ifdef MTK_GPT_SCHEME_SUPPORT
  741. ret = mboot_recovery_load_raw_part_offset("boot", (void *)t_ramdisk_addr,
  742. ROUNDUP(p_boot_hdr->kernel_sz + p_boot_hdr->page_sz, p_boot_hdr->page_sz), p_boot_hdr->ramdisk_sz);
  743. #else
  744. ret = mboot_recovery_load_raw_part_offset(PART_BOOTIMG, (void *)t_ramdisk_addr,
  745. ROUNDUP(p_boot_hdr->kernel_sz + p_boot_hdr->page_sz, p_boot_hdr->page_sz), p_boot_hdr->ramdisk_sz);
  746. #endif
  747. if (ret < 0) {
  748. return ret;
  749. }
  750. /* update header */
  751. p_boot_hdr->kernel_sz = sz;
  752. dprintf(INFO,"->kernel_sz %x\n", sz);
  753. } else {
  754. /* copy kernel content from flash*/
  755. #ifdef MTK_GPT_SCHEME_SUPPORT
  756. ret = mboot_recovery_load_raw_part_offset("boot", (void *)t_kernel_addr, p_boot_hdr->page_sz, p_boot_hdr->kernel_sz);
  757. #else
  758. ret = mboot_recovery_load_raw_part_offset(PART_BOOTIMG, (void *)t_kernel_addr, p_boot_hdr->page_sz, p_boot_hdr->kernel_sz);
  759. #endif
  760. if (ret < 0) {
  761. return ret;
  762. }
  763. t_ramdisk_addr = ROUNDUP(t_kernel_addr + p_boot_hdr->kernel_sz, p_boot_hdr->page_sz);
  764. /* copy ramdisk content from new_data*/
  765. memcpy((void *)t_ramdisk_addr, new_data, sz);
  766. /* update header */
  767. p_boot_hdr->ramdisk_sz = sz;
  768. dprintf(INFO,"->ramdisk_sz %x\n", sz);
  769. }
  770. /* overwrite arg*/
  771. *p_sz = p_boot_hdr->page_sz + ROUNDUP(p_boot_hdr->kernel_sz, p_boot_hdr->page_sz)+ ROUNDUP(p_boot_hdr->ramdisk_sz, p_boot_hdr->page_sz);
  772. /*
  773. * FIXME or LIMITATION:
  774. * tmp is an argument passed all the way from the handle() function. The handle() function
  775. * is a legacy of the official little kernel release. If the length of the destination
  776. * buffer "tmp" is needed, the function prototype of handle() needs to have one more
  777. * argument. In other words, all the functions registered in fastboot need to be altered.
  778. */
  779. /* 4 for the string, "boot", 1 for terminater, '\0'*/
  780. //if (strlen(tmp) >= 5) /*tmp was 'ramdisk' or 'zimage'. Add check to avoid "Out-of-bound access" coverity scan.*/
  781. // strncpy(tmp, "boot", 4 + 1);
  782. if (strlen(tmp) >= 5 && modified_arg != 0)
  783. *modified_arg = "boot";
  784. return 0;
  785. }
  786. /******************************************************************************
  787. * The mkimage tool will round up the image size to 16-byte alignment boundary
  788. * by padding zero to the tail of the image before a 512-byte mkimage header is
  789. * "pre-pended" to it. The dsize field in the mkimage header is the real size
  790. * of the image content before padding is applied, and it does not include the
  791. * size of the header.
  792. *
  793. * The preloader image does not have a mkimage header by default. To make the
  794. * flow consistent, a mkimage header is "pre-pended" to the preloader before it
  795. * is concatenated to the "single boot loader". Please note that the header
  796. * along with the image content of an image (except for the preloader image)
  797. * needs to be flashed to the storage. In other words, do not flash the mkimage
  798. * header of the preloader to the storage. It is fine to flash the padding bytes
  799. * to the storage.
  800. ******************************************************************************/
  801. bool cmd_flash_mmc_standard(const char *arg, void *data, unsigned sz);
  802. void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
  803. {
  804. #ifdef MTK_ULTRA_FLASH
  805. bool result = false;
  806. if(g_ultra_flash_enable && (partition_name_history_len!=0) && (!is_special_partition(partition_name_history)))
  807. {
  808. fastboot_info(partition_name_history);
  809. fastboot_okay("");
  810. result = true;
  811. }
  812. else
  813. {
  814. result = flash_storage(arg, data, sz);
  815. }
  816. if(!result)
  817. {
  818. //restore the default max download size.
  819. switch_max_download_len(false);
  820. }
  821. #else
  822. flash_storage(arg, data, sz);
  823. #endif
  824. return;
  825. }
  826. #define UNI_BOOTLOADER_MAGIC 0xEF77FD33
  827. #define MAX_BOOTLOADER_IMAGE 16
  828. struct partition_cell
  829. {
  830. char name[32];
  831. unsigned int size;
  832. };
  833. struct uni_bootloader_hdr
  834. {
  835. unsigned int magic; //0xEF77FD33
  836. unsigned int version; //1
  837. unsigned int header_size; // total header's size
  838. unsigned int image_cnt; //images count
  839. struct partition_cell part_list[MAX_BOOTLOADER_IMAGE];
  840. };
  841. bool flash_storage(const char* arg, void* data, unsigned sz)
  842. {
  843. bool result = false;
  844. if (strcmp(arg, "singlebootloader") != 0 && strcmp(arg, "bootloader") != 0)
  845. {
  846. result = cmd_flash_mmc_standard(arg, data, sz);
  847. if (result)
  848. {
  849. fastboot_okay("");
  850. }
  851. else
  852. {
  853. fastboot_fail(error_msg);
  854. }
  855. return result;
  856. }
  857. dprintf(ALWAYS, "@download singlebootloader.\n");
  858. dprintf(ALWAYS, "@size 0x%x.\n", sz);
  859. struct uni_bootloader_hdr* hdr = data;
  860. if(hdr->magic != UNI_BOOTLOADER_MAGIC
  861. || hdr->image_cnt > MAX_BOOTLOADER_IMAGE
  862. || hdr->header_size != sizeof(struct uni_bootloader_hdr))
  863. {
  864. fastboot_fail("NOT UNI_BOOTLOADER_MAGIC OR COUNT OVERFLOW.");
  865. return false;
  866. }
  867. void* img_data = data + hdr->header_size;
  868. unsigned int idx = 0;
  869. for (;idx < hdr->image_cnt; ++idx)
  870. {
  871. dprintf(ALWAYS, "@split download %s.\n", hdr->part_list[idx].name);
  872. bool result = cmd_flash_mmc_standard(hdr->part_list[idx].name, img_data,
  873. hdr->part_list[idx].size);
  874. if (!result)
  875. {
  876. fastboot_fail(error_msg);
  877. return false;
  878. }
  879. img_data += hdr->part_list[idx].size;
  880. }
  881. fastboot_okay("");
  882. return result;
  883. }
  884. bool cmd_flash_mmc_standard(const char *arg, void *data, unsigned sz)
  885. {
  886. bool result = false;
  887. #ifdef MTK_SECURITY_SW_SUPPORT
  888. unsigned int permitted = 0;
  889. char msg[64];
  890. #endif
  891. #ifdef MTK_GPT_SCHEME_SUPPORT
  892. #if defined(PLATFORM_FASTBOOT_EMPTY_STORAGE) || defined(MTK_GPT_UPDATE_SUPPORT)
  893. int err;
  894. #endif
  895. #endif
  896. union mkimg_hdr *part_hdr;
  897. bool single_boot_loader = false;
  898. bool flash_tee2 = false; // If ATF was found, flash both tee1 and tee2.
  899. if (sz == 0) {
  900. //fastboot_okay("");
  901. return true;
  902. }
  903. if (!strcmp(arg, "singlebootloader"))
  904. single_boot_loader = true;
  905. u32 remaining_len = sz;
  906. u32 counter = 5;
  907. while (remaining_len && (counter > 0)) {
  908. part_hdr = (union mkimg_hdr *)data;
  909. if (single_boot_loader) {
  910. if (flash_tee2) {
  911. arg = "tee2";
  912. flash_tee2 = false;
  913. } else {
  914. arg = part_hdr->info.name;
  915. if ((!strncmp(arg, "lk", 3)) || (!strncmp(arg, "LK", 3))) {
  916. arg = "lk";
  917. sz = sizeof(union mkimg_hdr);
  918. }
  919. else if ((!strncmp(arg, "logo", 5)) || (!strncmp(arg, "LOGO", 5))) {
  920. arg = "logo";
  921. sz = sizeof(union mkimg_hdr);
  922. }
  923. else if ((!strncmp(arg, "atf", 4)) || (!strncmp(arg, "ATF", 4))) {
  924. arg = "tee1";
  925. sz = sizeof(union mkimg_hdr);
  926. flash_tee2 = true;
  927. }
  928. else if ((!strncmp(arg, "preloader", 10)) || (!strncmp(arg, "PRELOADER", 10))) {
  929. arg = "preloader";
  930. sz = 0;
  931. data += sizeof(union mkimg_hdr);
  932. if (remaining_len < sizeof(union mkimg_hdr)) {
  933. set_response_msg("not enough remaining length");
  934. return false;
  935. }
  936. remaining_len -= sizeof(union mkimg_hdr);
  937. }
  938. else {
  939. set_response_msg("unknown data");
  940. return false;
  941. }
  942. sz += ROUNDUP(part_hdr->info.dsz, 16);
  943. if (remaining_len < sz) {
  944. set_response_msg("not enough remaining length");
  945. return false;
  946. }
  947. }
  948. }
  949. // security check here.
  950. // ret = decrypt_scm((uint32 **) &data, &sz);
  951. #ifdef MTK_SECURITY_SW_SUPPORT
  952. if (sec_dl_permission_chk(arg, &permitted)) {
  953. if(snprintf(msg, sizeof(msg), "failed to get download permission for partition '%s'\n", arg) < 0)
  954. {
  955. fastboot_fail("ghost snprintf error.");
  956. return false;
  957. }
  958. set_response_msg(msg);
  959. return false;
  960. }
  961. if (0 == permitted) {
  962. if(snprintf(msg, sizeof(msg), "download for partition '%s' is not allowed\n", arg) < 0)
  963. {
  964. fastboot_fail("ghost snprintf error.");
  965. return false;
  966. }
  967. set_response_msg(msg);
  968. return false;
  969. }
  970. #endif
  971. #ifdef MTK_GPT_SCHEME_SUPPORT
  972. #if defined(PLATFORM_FASTBOOT_EMPTY_STORAGE) || defined(MTK_GPT_UPDATE_SUPPORT)
  973. if (!strcmp(arg, "gpt")) { //if arg == "gpt" , update pmbr, pgpt, and sgpt. Then re-init partition table for fastboot.
  974. err = gpt_partition_table_update(arg, data, sz);
  975. if (err) {
  976. set_response_msg("failed to update gpt partition");
  977. return false;
  978. }
  979. read_gpt(partition); //re-init partition table afster update pmbor, pgpt and sgpt
  980. register_partition_var();
  981. //fastboot_okay("");
  982. return true;
  983. }
  984. #endif
  985. #endif
  986. TIME_START;
  987. char *modified_arg = 0;
  988. if (_virtual_partition_support(arg, data, &sz, &modified_arg) < 0) {
  989. set_response_msg("virtual partition write fail");
  990. return false;
  991. }
  992. else { /* is boot virtual partition, ex:ramdisk */
  993. if (remaining_len < sz)
  994. remaining_len = sz;
  995. }
  996. if(modified_arg != 0)
  997. arg = modified_arg;
  998. sparse_header_t *sparse_header = (sparse_header_t *) data;
  999. if (sparse_header->magic != SPARSE_HEADER_MAGIC)
  1000. result = cmd_flash_mmc_img(arg, data, sz);
  1001. else
  1002. #ifdef NAND_TYPE
  1003. #ifdef MNTL_SUPPORT
  1004. {
  1005. if (is_mntl_partition(arg))
  1006. {
  1007. dprintf(CRITICAL, "%s: mntl partition %s\n",
  1008. __FUNCTION__, arg);
  1009. result = mntl_flash_img(arg, data, sz);
  1010. if (!result)
  1011. {
  1012. set_response_msg("mntl flash image err.");
  1013. return false;
  1014. }
  1015. }
  1016. else
  1017. {
  1018. result = cmd_flash_nand_sparse_img(arg, data, sz);
  1019. }
  1020. }
  1021. #else
  1022. {
  1023. result = cmd_flash_nand_sparse_img(arg, data, sz);
  1024. }
  1025. #endif
  1026. #endif
  1027. #ifdef EMMC_TYPE
  1028. {
  1029. result = cmd_flash_mmc_sparse_img(arg, data, sz);
  1030. }
  1031. #endif
  1032. if(!result)
  1033. {
  1034. return result;
  1035. }
  1036. if (flash_tee2) {
  1037. counter--;
  1038. continue; // Preserve "data" and "sz" for reuse.
  1039. }
  1040. remaining_len -= sz;
  1041. data += sz;
  1042. counter--;
  1043. }
  1044. return result;
  1045. }
  1046. void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
  1047. {
  1048. #if (defined(MTK_UFS_SUPPORT) || defined(MTK_NEW_COMBO_EMMC_SUPPORT))
  1049. unsigned int part_id;
  1050. #endif
  1051. unsigned long long ptn = 0;
  1052. unsigned long long size = 0;
  1053. int index = INVALID_PTN;
  1054. int erase_ret = MMC_ERR_NONE;
  1055. #ifdef MTK_SECURITY_SW_SUPPORT
  1056. unsigned int permitted = 0;
  1057. char msg[64];
  1058. #endif
  1059. dprintf (ALWAYS, "cmd_erase_mmc\n");
  1060. #ifdef MTK_SPI_NOR_SUPPORT
  1061. if (!strcmp(arg, "NOR")) {
  1062. ptn = 0;
  1063. size = nor_get_device_capacity();
  1064. part_id = 0;/* NOR_PART_BOOT1; */
  1065. goto erase_nor_part;
  1066. } else
  1067. #endif
  1068. #ifdef FASTBOOT_WHOLE_FLASH_SUPPORT
  1069. if (!strcmp(arg, "boot0")) {
  1070. ptn = 0;
  1071. size = mmc_get_region_size(EMMC_PART_BOOT1);
  1072. part_id = EMMC_PART_BOOT1;
  1073. } else if (!strcmp(arg, "boot1")) {
  1074. ptn = 0;
  1075. size = mmc_get_region_size(EMMC_PART_BOOT2);
  1076. part_id = EMMC_PART_BOOT2;
  1077. } else if (!strcmp(arg, "partition")) {
  1078. ptn = 0;
  1079. size = mmc_get_region_size(EMMC_PART_USER);
  1080. part_id = EMMC_PART_USER;
  1081. } else
  1082. #endif
  1083. {
  1084. index = partition_get_index(arg);
  1085. if (index == -1) {
  1086. fastboot_fail_wrapper("Partition table doesn't exist");
  1087. return;
  1088. }
  1089. #ifdef MTK_SECURITY_SW_SUPPORT
  1090. if (sec_format_permission_chk(arg, &permitted)) {
  1091. if(snprintf(msg, sizeof(msg), "failed to get format permission for partition '%s'\n", arg) < 0)
  1092. {
  1093. fastboot_fail("ghost snprintf error.");
  1094. return;
  1095. }
  1096. fastboot_fail(msg);
  1097. return;
  1098. }
  1099. if (0 == permitted) {
  1100. if(snprintf(msg, sizeof(msg), "format for partition '%s' is not allowed\n", arg) < 0)
  1101. {
  1102. fastboot_fail("ghost snprintf error.");
  1103. return;
  1104. }
  1105. fastboot_fail(msg);
  1106. return;
  1107. }
  1108. #endif
  1109. #if (defined(MTK_UFS_SUPPORT) || defined(MTK_NEW_COMBO_EMMC_SUPPORT))
  1110. part_id = partition_get_region(index);
  1111. #endif
  1112. ptn = partition_get_offset(index);
  1113. size = partition_get_size(index);
  1114. }
  1115. TIME_START;
  1116. #ifdef EMMC_TYPE
  1117. #if (defined(MTK_UFS_SUPPORT) || defined(MTK_NEW_COMBO_EMMC_SUPPORT))
  1118. erase_ret = emmc_erase(part_id, ptn, size);
  1119. #else
  1120. erase_ret = emmc_erase(ptn, size);
  1121. #endif
  1122. #endif
  1123. #ifdef NAND_TYPE
  1124. #ifdef MNTL_SUPPORT
  1125. if (is_mntl_partition(arg)) {
  1126. dprintf(ALWAYS, "%s: mntl partition %s\n",
  1127. __FUNCTION__, arg);
  1128. erase_ret = mntl_erase(arg, data, sz);
  1129. } else
  1130. erase_ret = nand_erase(ptn,size);
  1131. #else
  1132. erase_ret = nand_erase(ptn,size);
  1133. #endif
  1134. #endif
  1135. if (erase_ret == MMC_ERR_NONE) {
  1136. fastboot_ok_wrapper("OK", size);
  1137. } else {
  1138. fastboot_fail_wrapper("Erase error.");
  1139. }
  1140. return;
  1141. #ifdef NOR_TYPE
  1142. erase_nor_part:
  1143. dprintf (ALWAYS, "partid %d, addr 0x%llx, size 0x%llx\n", part_id, ptn, size);
  1144. erase_ret = nor_erase_part(part_id, ptn, size);
  1145. if (erase_ret == 0/* NOR_ERR_NONE */) { /* NOR_ERR_NONE suppose to be zero */
  1146. fastboot_ok_wrapper("OK", size);
  1147. } else {
  1148. fastboot_fail_wrapper("Erase SPI NOR flash error.");
  1149. }
  1150. return;
  1151. #endif
  1152. }
  1153. /****************NEW CACHE WRITE*************************/
  1154. #if defined(NAND_TYPE)
  1155. typedef struct data_cache {
  1156. unsigned char* base;
  1157. unsigned int len;
  1158. unsigned int offset;
  1159. unsigned int blocks_per_cache;
  1160. bool is_first_write;
  1161. bool valid_data;
  1162. } data_cache_t;
  1163. data_cache_t* cache;
  1164. static void memfill(unsigned int* addr, unsigned int value, unsigned int count)
  1165. {
  1166. unsigned int i = 0;
  1167. for (; i<count; ++i) {
  1168. addr[i] = value;
  1169. }
  1170. return;
  1171. }
  1172. typedef struct nand_arg {
  1173. unsigned long long ptn;
  1174. unsigned int img_type;
  1175. unsigned long long size; //partition size
  1176. } nand_arg_t;
  1177. extern int nand_get_alignment();
  1178. unsigned int get_cache_size()
  1179. {
  1180. return (unsigned int)nand_get_alignment();
  1181. }
  1182. extern int nand_img_read(unsigned long long source, unsigned char * dst, int size);
  1183. static bool cached_write_data(unsigned int* start_blocks, char* data, unsigned long long data_sz,unsigned int chunk_type, unsigned int sparse_blk_sz, void* arg)
  1184. {
  1185. unsigned long long byte_to_process = data_sz;
  1186. unsigned int byte_processed = 0;
  1187. bool skip_zero_copy = false; //only affect CHUNK_TYPE_DONT_CARE
  1188. unsigned int total_blocks = *start_blocks;
  1189. nand_arg_t* nand = (nand_arg_t*)arg;
  1190. while (byte_to_process != 0) {
  1191. unsigned int slot_len = (byte_to_process > cache->len - cache->offset) ? cache->len - cache->offset :byte_to_process;
  1192. if (chunk_type == CHUNK_TYPE_RAW ||chunk_type == CHUNK_TYPE_FILL) {
  1193. if (cache->is_first_write && (!cache->valid_data) && cache->offset!=0) {
  1194. #if defined(MTK_MLC_NAND_SUPPORT)
  1195. nand_img_read((u64)(nand->ptn + ((unsigned long long)total_blocks*sparse_blk_sz)), (char*)cache->base, cache->len);
  1196. #else
  1197. nand_img_read((u32)(nand->ptn + ((unsigned long long)total_blocks*sparse_blk_sz)), (char*)cache->base, cache->len);
  1198. #endif
  1199. dprintf(ALWAYS, "Read ptn = 0x%llx, Address 0x%x = 0x%x\n", nand->ptn, total_blocks,*(int*)cache->base);
  1200. cache->is_first_write = false;
  1201. }
  1202. }
  1203. if (chunk_type == CHUNK_TYPE_RAW) {
  1204. memcpy(cache->base+cache->offset, data+byte_processed, slot_len);
  1205. cache->valid_data = true;
  1206. } else if (chunk_type == CHUNK_TYPE_DONT_CARE ) {
  1207. if (!cache->valid_data) {
  1208. skip_zero_copy = true;
  1209. }
  1210. if (!skip_zero_copy)
  1211. memset(cache->base+cache->offset, 0xFF, slot_len);
  1212. } else if (chunk_type == CHUNK_TYPE_FILL) {
  1213. memfill((unsigned int*)(cache->base+cache->offset), *(unsigned int*)data, slot_len/sizeof(unsigned int));
  1214. cache->valid_data = true;
  1215. }
  1216. cache->offset += slot_len;
  1217. if (cache->offset == cache->len) { //cache full
  1218. dprintf(ALWAYS, "@@@@write At block(x4K) 0x%x\n", total_blocks);
  1219. if (!skip_zero_copy) {
  1220. #if defined(MTK_MLC_NAND_SUPPORT)
  1221. dprintf(ALWAYS, "Address 0x%x = 0x%x\n", total_blocks,*(int*)cache->base);
  1222. if (nand_write_img((u64)(nand->ptn + ((unsigned long long)total_blocks*sparse_blk_sz)), (char*)cache->base, cache->len,nand->size,nand->img_type))
  1223. #else
  1224. if (nand_write_img((u32)(nand->ptn + ((unsigned long long)total_blocks*sparse_blk_sz)), (char*)cache->base, cache->len,nand->size,nand->img_type))
  1225. #endif
  1226. {
  1227. dprintf(ALWAYS, "@@@@write failed\n");
  1228. return false;
  1229. }
  1230. }
  1231. cache->offset = 0; //start new package.
  1232. byte_to_process -= slot_len;
  1233. byte_processed += slot_len;
  1234. total_blocks += cache->blocks_per_cache;
  1235. cache->valid_data = false;
  1236. } else {
  1237. //can not full cache.
  1238. break;
  1239. }
  1240. if (chunk_type == CHUNK_TYPE_DONT_CARE) {
  1241. skip_zero_copy = true;
  1242. }
  1243. }
  1244. *start_blocks = total_blocks;
  1245. return true;
  1246. }
  1247. bool cmd_flash_nand_sparse_img(const char* arg, void* data, unsigned sz)
  1248. {
  1249. unsigned int chunk;
  1250. unsigned long long chunk_data_sz;
  1251. sparse_header_t *sparse_header;
  1252. chunk_header_t *chunk_header;
  1253. uint32_t total_blocks = 0;
  1254. unsigned long long ptn = 0;
  1255. unsigned long long size = 0;
  1256. int index = INVALID_PTN;
  1257. nand_arg_t sto_arg;
  1258. sto_arg.img_type = get_nand_image_type(arg);
  1259. unsigned int crc_value;
  1260. unsigned int fill_value;
  1261. data_cache_t cache_inst;
  1262. cache_inst.len = get_cache_size();
  1263. cache_inst.base = (unsigned char*)memalign(128, cache_inst.len);
  1264. cache_inst.offset = 0;
  1265. cache_inst.is_first_write = true;
  1266. cache_inst.valid_data = false;
  1267. cache = &cache_inst;
  1268. index = partition_get_index(arg);
  1269. sto_arg.ptn = partition_get_offset(index);
  1270. if (sto_arg.ptn == (unsigned long long)(-1)) {
  1271. set_response_msg("partition table doesn't exist");
  1272. goto exit;
  1273. }
  1274. sto_arg.size = partition_get_size(index);
  1275. /* Read and skip over sparse image header */
  1276. sparse_header = (sparse_header_t *) data;
  1277. dprintf(ALWAYS, "Image size span 0x%llx, partition size 0x%llx\n", (unsigned long long)sparse_header->total_blks*sparse_header->blk_sz, sto_arg.size);
  1278. if ((unsigned long long)sparse_header->total_blks*sparse_header->blk_sz > sto_arg.size) {
  1279. set_response_msg("sparse image size span overflow.");
  1280. goto exit;
  1281. }
  1282. data += sparse_header->file_hdr_sz;
  1283. if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) {
  1284. /* Skip the remaining bytes in a header that is longer than
  1285. * we expected.
  1286. */
  1287. data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
  1288. }
  1289. cache->blocks_per_cache = (cache->len/sparse_header->blk_sz);
  1290. dprintf (ALWAYS, "=== Sparse Image Header ===\n");
  1291. dprintf (ALWAYS, "magic: 0x%x\n", sparse_header->magic);
  1292. dprintf (ALWAYS, "major_version: 0x%x\n", sparse_header->major_version);
  1293. dprintf (ALWAYS, "minor_version: 0x%x\n", sparse_header->minor_version);
  1294. dprintf (ALWAYS, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
  1295. dprintf (ALWAYS, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
  1296. dprintf (ALWAYS, "blk_sz: %d\n", sparse_header->blk_sz);
  1297. dprintf (ALWAYS, "total_blks: %d\n", sparse_header->total_blks);
  1298. dprintf (ALWAYS, "total_chunks: %d\n", sparse_header->total_chunks);
  1299. display_info("\nWriting Flash ... ");
  1300. /* Start processing chunks */
  1301. for (chunk=0; chunk<sparse_header->total_chunks; chunk++) {
  1302. /* Read and skip over chunk header */
  1303. chunk_header = (chunk_header_t *) data;
  1304. data += sizeof(chunk_header_t);
  1305. //dprintf (ALWAYS, "=== Chunk Header ===\n");
  1306. //dprintf (ALWAYS, "chunk_type: 0x%x\n", chunk_header->chunk_type);
  1307. //dprintf (ALWAYS, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
  1308. //dprintf (ALWAYS, "total_size: 0x%x\n", chunk_header->total_sz);
  1309. if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) {
  1310. /* Skip the remaining bytes in a header that is longer than
  1311. * we expected.
  1312. */
  1313. data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
  1314. }
  1315. chunk_data_sz = (unsigned long long)(sparse_header->blk_sz&0xFFFFFFFF);
  1316. chunk_data_sz = chunk_data_sz * chunk_header->chunk_sz;
  1317. switch (chunk_header->chunk_type) {
  1318. case CHUNK_TYPE_RAW:
  1319. if (chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
  1320. chunk_data_sz)) {
  1321. //fastboot_fail("Bogus chunk size for chunk type Raw");
  1322. goto exit;
  1323. }
  1324. if (!cached_write_data(&total_blocks, data, chunk_data_sz, CHUNK_TYPE_RAW, sparse_header->blk_sz, (void*)&sto_arg)) {
  1325. set_response_msg("storage write failed.");
  1326. goto exit;
  1327. }
  1328. data += chunk_data_sz;
  1329. break;
  1330. case CHUNK_TYPE_DONT_CARE:
  1331. if (!cached_write_data(&total_blocks, 0, chunk_data_sz, CHUNK_TYPE_DONT_CARE, sparse_header->blk_sz, (void*)&sto_arg)) {
  1332. set_response_msg("storage write failed.");
  1333. goto exit;
  1334. }
  1335. break;
  1336. case CHUNK_TYPE_CRC:
  1337. crc_value = *(unsigned int*)data;
  1338. data += 4;
  1339. break;
  1340. case CHUNK_TYPE_FILL:
  1341. fill_value = *(unsigned int*)data;
  1342. if (!cached_write_data(&total_blocks, (char*)&fill_value, chunk_data_sz, CHUNK_TYPE_FILL, sparse_header->blk_sz, (void*)&sto_arg)) {
  1343. set_response_msg("storage write failed.");
  1344. goto exit;
  1345. }
  1346. data += 4; //data length is 4.
  1347. break;
  1348. default:
  1349. set_response_msg("Unknown chunk type");
  1350. goto exit;
  1351. }
  1352. display_progress("\rWrite Data", total_blocks*sparse_header->blk_sz, sparse_header->total_blks*sparse_header->blk_sz);
  1353. }
  1354. /******************/
  1355. //last cache
  1356. if (cache->offset != 0) {
  1357. #if defined(MTK_MLC_NAND_SUPPORT)
  1358. if (nand_write_img((u64)(sto_arg.ptn + ((unsigned long long)total_blocks*sparse_header->blk_sz)), (char*)cache->base, cache->offset,sto_arg.size,sto_arg.img_type))
  1359. #else
  1360. if (nand_write_img((u32)(sto_arg.ptn + ((unsigned long long)total_blocks*sparse_header->blk_sz)), (char*)cache->base, cache->offset,sto_arg.size,sto_arg.img_type))
  1361. #endif
  1362. total_blocks += cache->offset/sparse_header->blk_sz;
  1363. }
  1364. /******************/
  1365. dprintf(ALWAYS, "Wrote %d blocks, expected to write %d blocks\n",
  1366. total_blocks, sparse_header->total_blks);
  1367. if (total_blocks != sparse_header->total_blks) {
  1368. set_response_msg("sparse image write failure");
  1369. } else {
  1370. display_info("\n\nOK");
  1371. //fastboot_okay("");
  1372. if (cache->base) free(cache->base);
  1373. return true;
  1374. }
  1375. exit:
  1376. if (cache->base) free(cache->base);
  1377. return false;
  1378. }
  1379. #endif
  1380. /**********END********/
  1381. #ifdef MTK_ULTRA_FLASH
  1382. #define MAX_SPECIAL_P 17
  1383. char* special_partition[MAX_SPECIAL_P] = {
  1384. "preloader", "boot", "boot0", "boot1", "tee", "tee1", "bootloader"
  1385. , "tee2", "lk", "logo", "atf", "recovery", "partition", "singlebootloader", "gpt", "pgpt","mbr"
  1386. };
  1387. bool is_special_partition(const char* partition)
  1388. {
  1389. int idx = 0;
  1390. for(; idx<MAX_SPECIAL_P; ++idx)
  1391. {
  1392. if (strnicmp(special_partition[idx], partition, 32) == 0)
  1393. {
  1394. return true;
  1395. }
  1396. }
  1397. return false;
  1398. }
  1399. char* trim_space(const char *str)
  1400. {
  1401. char* p = (char*)str;
  1402. if(p == NULL)return NULL;
  1403. while(*p && (*p==' ' || *p == '\t'))
  1404. {
  1405. ++p;
  1406. }
  1407. return p;
  1408. }
  1409. extern void fastboot_update_var(const char *name, const char *value);
  1410. extern const char * fastboot_get_var(const char *name);
  1411. void cmd_oem_ultra_flash(const char *arg, void *data, unsigned sz)
  1412. {
  1413. char response[MAX_RSP_SIZE];
  1414. if(!g_ultra_flash_enable)
  1415. {
  1416. if(snprintf(response, MAX_RSP_SIZE, "Ultra-flash Disabled. [%s]", fastboot_get_var("max-download-size")) < 0)
  1417. {
  1418. fastboot_fail("ghost sprintf error.");
  1419. return;
  1420. }
  1421. fastboot_info(response);
  1422. fastboot_okay("");
  1423. return;
  1424. }
  1425. char* part = trim_space(arg);
  1426. if(part == 0)
  1427. {
  1428. fastboot_fail("ultra flash arg is null.");
  1429. return;
  1430. }
  1431. if(strnlen(part, MAX_PARTITION_NAME_LEN+3) >= MAX_PARTITION_NAME_LEN)
  1432. {
  1433. fastboot_fail("name too long.");
  1434. return;
  1435. }
  1436. memset(partition_name_history, 0, MAX_PARTITION_NAME_LEN);
  1437. partition_name_history_len = strnlen(part, MAX_PARTITION_NAME_LEN-1);
  1438. strncpy(partition_name_history, part, MAX_PARTITION_NAME_LEN-1);
  1439. partition_name_history[MAX_PARTITION_NAME_LEN-1] = 0;
  1440. switch_max_download_len((partition_name_history_len != 0) && (!is_special_partition(partition_name_history)));
  1441. if(snprintf(response, MAX_RSP_SIZE, "%s. [%s]", partition_name_history, fastboot_get_var("max-download-size")) < 0)
  1442. {
  1443. fastboot_fail("name too long.");
  1444. return;
  1445. }
  1446. fastboot_info(response);
  1447. fastboot_okay("");
  1448. }
  1449. void switch_max_download_len(bool ultra_en)
  1450. {
  1451. static const char* dl_max_str_history = NULL;
  1452. static const char* dl_max_str = "0x200000000";
  1453. if(dl_max_str_history == NULL)
  1454. {
  1455. dl_max_str_history = fastboot_get_var("max-download-size");
  1456. }
  1457. fastboot_update_var("max-download-size", ultra_en? dl_max_str:dl_max_str_history);
  1458. }
  1459. void cmd_oem_ultra_flash_en(const char *arg, void *data, unsigned sz)
  1460. {
  1461. char response[MAX_RSP_SIZE];
  1462. if (!strncmp(arg, " 1", strlen(" 1")))
  1463. {
  1464. //turn ultra flash on
  1465. fastboot_info("Enable ultra-flash.");
  1466. g_ultra_flash_enable = true;
  1467. }
  1468. else if (!strncmp(arg, " 0", strlen(" 0")))
  1469. {
  1470. //turn ultra flash off
  1471. fastboot_info("Disable ultra-flash.");
  1472. g_ultra_flash_enable = false;
  1473. } else {
  1474. if(snprintf(response, MAX_RSP_SIZE, "\tCurrent ultra-flash setting:%s",
  1475. g_ultra_flash_enable ? "Enabled" : "Disabled") < 0)
  1476. {
  1477. fastboot_fail("ghost snprintf error.");
  1478. return;
  1479. }
  1480. fastboot_info(response);
  1481. }
  1482. fastboot_okay("");
  1483. }
  1484. extern char * strncpy(char *dest, char const *src, size_t count);
  1485. bool cmd_download_ultra(const char *arg, void *data, unsigned sz)
  1486. {
  1487. int index = INVALID_PTN;
  1488. status_t status = STATUS_OK;
  1489. char response[MAX_RSP_SIZE];
  1490. uint64 len = hex2uint64(arg);
  1491. if (!power_check())
  1492. {
  1493. fastboot_fail("low power, need battery charging.");
  1494. return false;
  1495. }
  1496. init_display_xy();
  1497. if(snprintf(response, MAX_RSP_SIZE, "DATA%08llx", len) < 0)
  1498. {
  1499. fastboot_fail("ghost snprintf error.");
  1500. return false;
  1501. }
  1502. if (usb_write(response, strlen(response)) < 0) {
  1503. return false;
  1504. }
  1505. display_info("USB Bulk Transferring... ");
  1506. LOGI("[%s]", partition_name_history);
  1507. struct partition_info_struct part_info;
  1508. memset(&part_info, 0, sizeof(struct partition_info_struct));
  1509. index = partition_get_index(partition_name_history);
  1510. if (index == INVALID_PTN)
  1511. {
  1512. fastboot_fail("This partition doesn't exist");
  1513. return false;
  1514. }
  1515. strncpy((char*)part_info.name, partition_name_history, MAX_PARTITION_NAME_LEN-1);
  1516. part_info.base_addr = partition_get_offset(index);
  1517. if (part_info.base_addr == (unsigned long long)(-1))
  1518. {
  1519. fastboot_fail("partition table doesn't exist");
  1520. return false;
  1521. }
  1522. part_info.part_id = partition_get_region(index);
  1523. part_info.max_size = partition_get_size(index);
  1524. LOGI("download part_name[%s] base_addr[0x%llx] part_max_sz[0x%llx] down_len [0x%llx]\n",
  1525. part_info.name, part_info.base_addr, part_info.max_size, len);
  1526. TIME_START;
  1527. status = download_data(len, &part_info);
  1528. if(FAIL(status))
  1529. {
  1530. if(snprintf(response, MAX_RSP_SIZE, "Transmission FAIL: [%d]", status) < 0)
  1531. {
  1532. fastboot_fail("ghost snprintf error.");
  1533. return false;
  1534. }
  1535. fastboot_fail_wrapper(response);
  1536. fastboot_state = STATE_ERROR;
  1537. return false;
  1538. }
  1539. fastboot_ok_wrapper("Transmission OK", len);
  1540. return true;
  1541. }
  1542. void* get_available_ram_base()
  1543. {
  1544. return download_base;
  1545. }
  1546. #endif
  1547. /*LXO: END!Download related command*/