fb_command.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // SPDX-License-Identifier: BSD-2-Clause
  2. /*
  3. * Copyright (C) 2016 The Android Open Source Project
  4. */
  5. #include <common.h>
  6. #include <command.h>
  7. #include <env.h>
  8. #include <fastboot.h>
  9. #include <fastboot-internal.h>
  10. #include <fb_mmc.h>
  11. #include <fb_nand.h>
  12. #include <part.h>
  13. #include <stdlib.h>
  14. #define BLOCK_SIZE 512
  15. /**
  16. * image_size - final fastboot image size
  17. */
  18. static u32 image_size;
  19. /**
  20. * fastboot_bytes_received - number of bytes received in the current download
  21. */
  22. static u32 fastboot_bytes_received;
  23. /**
  24. * fastboot_bytes_expected - number of bytes expected in the current download
  25. */
  26. static u32 fastboot_bytes_expected;
  27. static void okay(char *, char *);
  28. static void getvar(char *, char *);
  29. static void download(char *, char *);
  30. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
  31. static void flash(char *, char *);
  32. static void erase(char *, char *);
  33. #endif
  34. static void reboot_bootloader(char *, char *);
  35. #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
  36. static void oem_format(char *, char *);
  37. #endif
  38. static void oem_command(char *, char *);
  39. static const struct {
  40. const char *command;
  41. void (*dispatch)(char *cmd_parameter, char *response);
  42. } commands[FASTBOOT_COMMAND_COUNT] = {
  43. [FASTBOOT_COMMAND_GETVAR] = {
  44. .command = "getvar",
  45. .dispatch = getvar
  46. },
  47. [FASTBOOT_COMMAND_DOWNLOAD] = {
  48. .command = "download",
  49. .dispatch = download
  50. },
  51. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
  52. [FASTBOOT_COMMAND_FLASH] = {
  53. .command = "flash",
  54. .dispatch = flash
  55. },
  56. [FASTBOOT_COMMAND_ERASE] = {
  57. .command = "erase",
  58. .dispatch = erase
  59. },
  60. #endif
  61. [FASTBOOT_COMMAND_BOOT] = {
  62. .command = "boot",
  63. .dispatch = okay
  64. },
  65. [FASTBOOT_COMMAND_CONTINUE] = {
  66. .command = "continue",
  67. .dispatch = okay
  68. },
  69. [FASTBOOT_COMMAND_REBOOT] = {
  70. .command = "reboot",
  71. .dispatch = okay
  72. },
  73. [FASTBOOT_COMMAND_REBOOT_BOOTLOADER] = {
  74. .command = "reboot-bootloader",
  75. .dispatch = reboot_bootloader
  76. },
  77. [FASTBOOT_COMMAND_SET_ACTIVE] = {
  78. .command = "set_active",
  79. .dispatch = okay
  80. },
  81. #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
  82. [FASTBOOT_COMMAND_OEM_FORMAT] = {
  83. .command = "oem format",
  84. .dispatch = oem_format,
  85. },
  86. #endif
  87. [FASTBOOT_COMMAND_OEM_COMMAND] = {
  88. .command = "oem command",
  89. .dispatch = oem_command,
  90. },
  91. };
  92. /**
  93. * fastboot_handle_command - Handle fastboot command
  94. *
  95. * @cmd_string: Pointer to command string
  96. * @response: Pointer to fastboot response buffer
  97. *
  98. * Return: Executed command, or -1 if not recognized
  99. */
  100. int fastboot_handle_command(char *cmd_string, char *response)
  101. {
  102. int i;
  103. char *cmd_parameter;
  104. cmd_parameter = cmd_string;
  105. strsep(&cmd_parameter, ":");
  106. for (i = 0; i < FASTBOOT_COMMAND_COUNT; i++) {
  107. if (!strcmp(commands[i].command, cmd_string)) {
  108. if (commands[i].dispatch) {
  109. commands[i].dispatch(cmd_parameter,
  110. response);
  111. return i;
  112. } else {
  113. break;
  114. }
  115. }
  116. }
  117. pr_err("command %s not recognized.\n", cmd_string);
  118. fastboot_fail("unrecognized command", response);
  119. return -1;
  120. }
  121. /**
  122. * okay() - Send bare OKAY response
  123. *
  124. * @cmd_parameter: Pointer to command parameter
  125. * @response: Pointer to fastboot response buffer
  126. *
  127. * Send a bare OKAY fastboot response. This is used where the command is
  128. * valid, but all the work is done after the response has been sent (e.g.
  129. * boot, reboot etc.)
  130. */
  131. static void okay(char *cmd_parameter, char *response)
  132. {
  133. fastboot_okay(NULL, response);
  134. }
  135. /**
  136. * getvar() - Read a config/version variable
  137. *
  138. * @cmd_parameter: Pointer to command parameter
  139. * @response: Pointer to fastboot response buffer
  140. */
  141. static void getvar(char *cmd_parameter, char *response)
  142. {
  143. fastboot_getvar(cmd_parameter, response);
  144. }
  145. /**
  146. * fastboot_download() - Start a download transfer from the client
  147. *
  148. * @cmd_parameter: Pointer to command parameter
  149. * @response: Pointer to fastboot response buffer
  150. */
  151. static void download(char *cmd_parameter, char *response)
  152. {
  153. char *tmp;
  154. if (!cmd_parameter) {
  155. fastboot_fail("Expected command parameter", response);
  156. return;
  157. }
  158. fastboot_bytes_received = 0;
  159. fastboot_bytes_expected = simple_strtoul(cmd_parameter, &tmp, 16);
  160. if (fastboot_bytes_expected == 0) {
  161. fastboot_fail("Expected nonzero image size", response);
  162. return;
  163. }
  164. /*
  165. * Nothing to download yet. Response is of the form:
  166. * [DATA|FAIL]$cmd_parameter
  167. *
  168. * where cmd_parameter is an 8 digit hexadecimal number
  169. */
  170. if (fastboot_bytes_expected > fastboot_buf_size) {
  171. fastboot_fail(cmd_parameter, response);
  172. } else {
  173. printf("Starting download of %d bytes\n",
  174. fastboot_bytes_expected);
  175. fastboot_response("DATA", response, "%s", cmd_parameter);
  176. }
  177. }
  178. /**
  179. * fastboot_data_remaining() - return bytes remaining in current transfer
  180. *
  181. * Return: Number of bytes left in the current download
  182. */
  183. u32 fastboot_data_remaining(void)
  184. {
  185. return fastboot_bytes_expected - fastboot_bytes_received;
  186. }
  187. /**
  188. * fastboot_data_download() - Copy image data to fastboot_buf_addr.
  189. *
  190. * @fastboot_data: Pointer to received fastboot data
  191. * @fastboot_data_len: Length of received fastboot data
  192. * @response: Pointer to fastboot response buffer
  193. *
  194. * Copies image data from fastboot_data to fastboot_buf_addr. Writes to
  195. * response. fastboot_bytes_received is updated to indicate the number
  196. * of bytes that have been transferred.
  197. *
  198. * On completion sets image_size and ${filesize} to the total size of the
  199. * downloaded image.
  200. */
  201. void fastboot_data_download(const void *fastboot_data,
  202. unsigned int fastboot_data_len,
  203. char *response)
  204. {
  205. #define BYTES_PER_DOT 0x20000
  206. u32 pre_dot_num, now_dot_num;
  207. if (fastboot_data_len == 0 ||
  208. (fastboot_bytes_received + fastboot_data_len) >
  209. fastboot_bytes_expected) {
  210. fastboot_fail("Received invalid data length",
  211. response);
  212. return;
  213. }
  214. /* Download data to fastboot_buf_addr */
  215. memcpy(fastboot_buf_addr + fastboot_bytes_received,
  216. fastboot_data, fastboot_data_len);
  217. pre_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
  218. fastboot_bytes_received += fastboot_data_len;
  219. now_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
  220. if (pre_dot_num != now_dot_num) {
  221. putc('.');
  222. if (!(now_dot_num % 74))
  223. putc('\n');
  224. }
  225. *response = '\0';
  226. }
  227. /**
  228. * fastboot_data_complete() - Mark current transfer complete
  229. *
  230. * @response: Pointer to fastboot response buffer
  231. *
  232. * Set image_size and ${filesize} to the total size of the downloaded image.
  233. */
  234. void fastboot_data_complete(char *response)
  235. {
  236. /* Download complete. Respond with "OKAY" */
  237. fastboot_okay(NULL, response);
  238. printf("\ndownloading of %d bytes finished\n", fastboot_bytes_received);
  239. image_size = fastboot_bytes_received;
  240. env_set_hex("filesize", image_size);
  241. fastboot_bytes_expected = 0;
  242. fastboot_bytes_received = 0;
  243. }
  244. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
  245. /**
  246. * flash() - write the downloaded image to the indicated partition.
  247. *
  248. * @cmd_parameter: Pointer to partition name
  249. * @response: Pointer to fastboot response buffer
  250. *
  251. * Writes the previously downloaded image to the partition indicated by
  252. * cmd_parameter. Writes to response.
  253. */
  254. static void flash(char *cmd_parameter, char *response)
  255. {
  256. #ifdef THEAD_LIGHT_FASTBOOT
  257. char cmdbuf[32];
  258. u32 block_cnt;
  259. struct blk_desc *dev_desc;
  260. if (strcmp(cmd_parameter, "uboot") == 0) {
  261. dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
  262. if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
  263. fastboot_fail("invalid mmc device", response);
  264. return;
  265. }
  266. run_command("mmc partconf 0 1 0 1", 0);
  267. block_cnt = image_size / BLOCK_SIZE;
  268. if (image_size % BLOCK_SIZE) {
  269. block_cnt = block_cnt +1;
  270. }
  271. sprintf(cmdbuf, "mmc write 0x%p 0 %x", fastboot_buf_addr, block_cnt);
  272. run_command(cmdbuf, 0);
  273. run_command("mmc partconf 0 1 0 0", 0);
  274. } else if ((strcmp(cmd_parameter, "fw") == 0)) {
  275. memcpy((void *)LIGHT_FW_ADDR, fastboot_buf_addr, image_size);
  276. } else if ((strcmp(cmd_parameter, "uImage") == 0)) {
  277. memcpy((void *)LIGHT_KERNEL_ADDR, fastboot_buf_addr, image_size);
  278. } else if ((strcmp(cmd_parameter, "dtb") == 0)) {
  279. memcpy((void *)LIGHT_DTB_ADDR, fastboot_buf_addr, image_size);
  280. } else if ((strcmp(cmd_parameter, "rootfs") == 0)) {
  281. memcpy((void *)LIGHT_ROOTFS_ADDR, fastboot_buf_addr, image_size);
  282. } else if ((strcmp(cmd_parameter, "aon") == 0)) {
  283. memcpy((void *)LIGHT_AON_FW_ADDR, fastboot_buf_addr, image_size);
  284. } else if ((strcmp(cmd_parameter, TF_PART_NAME) == 0)) {
  285. memcpy((void *)LIGHT_TF_FW_ADDR, fastboot_buf_addr, image_size);
  286. } else if ((strcmp(cmd_parameter, TEE_PART_NAME) == 0)) {
  287. memcpy((void *)LIGHT_TEE_FW_ADDR, fastboot_buf_addr, image_size);
  288. }
  289. if(strcmp(cmd_parameter, "uboot") == 0 || (strcmp(cmd_parameter, "fw") == 0) ||
  290. (strcmp(cmd_parameter, "uImage") == 0) || (strcmp(cmd_parameter, "dtb") == 0) ||
  291. (strcmp(cmd_parameter, "rootfs") == 0) || (strcmp(cmd_parameter, "aon") == 0)) {
  292. fastboot_okay(NULL, response);
  293. return;
  294. }
  295. #endif
  296. #if CONFIG_IS_ENABLED(LIGHT_SEC_UPGRADE)
  297. if(strcmp(cmd_parameter, TF_IMG_UPD_NAME) == 0) {
  298. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
  299. /* tee/tf/uboot image must be written into stash partition */
  300. sprintf(cmdbuf, "%s", STASH_PART_NAME);
  301. fastboot_mmc_flash_write(cmdbuf, fastboot_buf_addr, image_size, response);
  302. #endif
  303. /* Send ACK to host */
  304. fastboot_okay(NULL, response);
  305. /* set secure upgrade flag to indicate it is TF image upgrade*/
  306. sprintf(cmdbuf,"env set sec_upgrade_mode 0x%x", TF_SEC_UPGRADE_FLAG);
  307. run_command(cmdbuf, 0);
  308. run_command("saveenv", 0);
  309. run_command("reset", 0);
  310. return;
  311. } else if (strcmp(cmd_parameter, TEE_IMG_UPD_NAME) == 0) {
  312. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
  313. /* tee/tf/uboot image must be written into stash partition */
  314. sprintf(cmdbuf, "%s", STASH_PART_NAME);
  315. fastboot_mmc_flash_write(cmdbuf, fastboot_buf_addr, image_size, response);
  316. #endif
  317. /* Send ACK to host */
  318. fastboot_okay(NULL, response);
  319. /* set secure upgrade flag to indicate it is TEE image upgrade*/
  320. sprintf(cmdbuf,"env set sec_upgrade_mode 0x%x", TEE_SEC_UPGRADE_FLAG);
  321. run_command(cmdbuf, 0);
  322. run_command("saveenv", 0);
  323. run_command("reset", 0);
  324. return;
  325. } else if (strcmp(cmd_parameter, UBOOT_IMG_UPD_NAME) == 0) {
  326. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
  327. env_set_hex("ubootupdsize", image_size);
  328. /* tee/tf/uboot image must be written into stash partition */
  329. sprintf(cmdbuf, "%s", STASH_PART_NAME);
  330. fastboot_mmc_flash_write(cmdbuf, fastboot_buf_addr, image_size, response);
  331. #endif
  332. /* Send ACK to host */
  333. fastboot_okay(NULL, response);
  334. /* set secure upgrade flag to indicate it is UBOOT image upgrade*/
  335. sprintf(cmdbuf,"env set sec_upgrade_mode 0x%x", UBOOT_SEC_UPGRADE_FLAG);
  336. run_command(cmdbuf, 0);
  337. run_command("saveenv", 0);
  338. run_command("reset", 0);
  339. return;
  340. }
  341. #endif
  342. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
  343. printf("cmd_parameter: %s, imagesize: %d\n", cmd_parameter, image_size);
  344. fastboot_mmc_flash_write(cmd_parameter, fastboot_buf_addr, image_size,
  345. response);
  346. #endif
  347. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
  348. fastboot_nand_flash_write(cmd_parameter, fastboot_buf_addr, image_size,
  349. response);
  350. #endif
  351. }
  352. /**
  353. * erase() - erase the indicated partition.
  354. *
  355. * @cmd_parameter: Pointer to partition name
  356. * @response: Pointer to fastboot response buffer
  357. *
  358. * Erases the partition indicated by cmd_parameter (clear to 0x00s). Writes
  359. * to response.
  360. */
  361. static void erase(char *cmd_parameter, char *response)
  362. {
  363. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
  364. fastboot_mmc_erase(cmd_parameter, response);
  365. #endif
  366. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
  367. fastboot_nand_erase(cmd_parameter, response);
  368. #endif
  369. }
  370. #endif
  371. /**
  372. * reboot_bootloader() - Sets reboot bootloader flag.
  373. *
  374. * @cmd_parameter: Pointer to command parameter
  375. * @response: Pointer to fastboot response buffer
  376. */
  377. static void reboot_bootloader(char *cmd_parameter, char *response)
  378. {
  379. if (fastboot_set_reboot_flag())
  380. fastboot_fail("Cannot set reboot flag", response);
  381. else
  382. fastboot_okay(NULL, response);
  383. }
  384. #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
  385. /**
  386. * oem_format() - Execute the OEM format command
  387. *
  388. * @cmd_parameter: Pointer to command parameter
  389. * @response: Pointer to fastboot response buffer
  390. */
  391. static void oem_format(char *cmd_parameter, char *response)
  392. {
  393. char cmdbuf[32];
  394. if (!env_get("partitions")) {
  395. fastboot_fail("partitions not set", response);
  396. } else {
  397. sprintf(cmdbuf, "gpt write mmc %x $partitions",
  398. CONFIG_FASTBOOT_FLASH_MMC_DEV);
  399. if (run_command(cmdbuf, 0))
  400. fastboot_fail("", response);
  401. else
  402. fastboot_okay(NULL, response);
  403. }
  404. }
  405. #endif
  406. /**
  407. * oem_command() - Execute the OEM command
  408. *
  409. * @cmd_parameter: Pointer to command parameter
  410. * @response: Pointer to fastboot response buffer
  411. */
  412. static void oem_command(char *cmd_parameter, char *response)
  413. {
  414. if (run_command(cmd_parameter, 0))
  415. fastboot_fail("", response);
  416. else
  417. fastboot_okay(NULL, response);
  418. }