fb_command.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. /**
  15. * image_size - final fastboot image size
  16. */
  17. static u32 image_size;
  18. /**
  19. * fastboot_bytes_received - number of bytes received in the current download
  20. */
  21. static u32 fastboot_bytes_received;
  22. /**
  23. * fastboot_bytes_expected - number of bytes expected in the current download
  24. */
  25. static u32 fastboot_bytes_expected;
  26. static void okay(char *, char *);
  27. static void getvar(char *, char *);
  28. static void download(char *, char *);
  29. static void flash(char *, char *);
  30. static void erase(char *, char *);
  31. static void reboot_bootloader(char *, char *);
  32. static void reboot_fastbootd(char *, char *);
  33. static void reboot_recovery(char *, char *);
  34. static void oem_format(char *, char *);
  35. static void oem_partconf(char *, char *);
  36. static void oem_bootbus(char *, char *);
  37. static void run_ucmd(char *, char *);
  38. static void run_acmd(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. [FASTBOOT_COMMAND_FLASH] = {
  52. .command = "flash",
  53. .dispatch = CONFIG_IS_ENABLED(FASTBOOT_FLASH, (flash), (NULL))
  54. },
  55. [FASTBOOT_COMMAND_ERASE] = {
  56. .command = "erase",
  57. .dispatch = CONFIG_IS_ENABLED(FASTBOOT_FLASH, (erase), (NULL))
  58. },
  59. [FASTBOOT_COMMAND_BOOT] = {
  60. .command = "boot",
  61. .dispatch = okay
  62. },
  63. [FASTBOOT_COMMAND_CONTINUE] = {
  64. .command = "continue",
  65. .dispatch = okay
  66. },
  67. [FASTBOOT_COMMAND_REBOOT] = {
  68. .command = "reboot",
  69. .dispatch = okay
  70. },
  71. [FASTBOOT_COMMAND_REBOOT_BOOTLOADER] = {
  72. .command = "reboot-bootloader",
  73. .dispatch = reboot_bootloader
  74. },
  75. [FASTBOOT_COMMAND_REBOOT_FASTBOOTD] = {
  76. .command = "reboot-fastboot",
  77. .dispatch = reboot_fastbootd
  78. },
  79. [FASTBOOT_COMMAND_REBOOT_RECOVERY] = {
  80. .command = "reboot-recovery",
  81. .dispatch = reboot_recovery
  82. },
  83. [FASTBOOT_COMMAND_SET_ACTIVE] = {
  84. .command = "set_active",
  85. .dispatch = okay
  86. },
  87. [FASTBOOT_COMMAND_OEM_FORMAT] = {
  88. .command = "oem format",
  89. .dispatch = CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT, (oem_format), (NULL))
  90. },
  91. [FASTBOOT_COMMAND_OEM_PARTCONF] = {
  92. .command = "oem partconf",
  93. .dispatch = CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_PARTCONF, (oem_partconf), (NULL))
  94. },
  95. [FASTBOOT_COMMAND_OEM_BOOTBUS] = {
  96. .command = "oem bootbus",
  97. .dispatch = CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_BOOTBUS, (oem_bootbus), (NULL))
  98. },
  99. [FASTBOOT_COMMAND_OEM_RUN] = {
  100. .command = "oem run",
  101. .dispatch = CONFIG_IS_ENABLED(FASTBOOT_OEM_RUN, (run_ucmd), (NULL))
  102. },
  103. [FASTBOOT_COMMAND_UCMD] = {
  104. .command = "UCmd",
  105. .dispatch = CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT, (run_ucmd), (NULL))
  106. },
  107. [FASTBOOT_COMMAND_ACMD] = {
  108. .command = "ACmd",
  109. .dispatch = CONFIG_IS_ENABLED(FASTBOOT_UUU_SUPPORT, (run_acmd), (NULL))
  110. },
  111. };
  112. /**
  113. * fastboot_handle_command - Handle fastboot command
  114. *
  115. * @cmd_string: Pointer to command string
  116. * @response: Pointer to fastboot response buffer
  117. *
  118. * Return: Executed command, or -1 if not recognized
  119. */
  120. int fastboot_handle_command(char *cmd_string, char *response)
  121. {
  122. int i;
  123. char *cmd_parameter;
  124. cmd_parameter = cmd_string;
  125. strsep(&cmd_parameter, ":");
  126. for (i = 0; i < FASTBOOT_COMMAND_COUNT; i++) {
  127. if (!strcmp(commands[i].command, cmd_string)) {
  128. if (commands[i].dispatch) {
  129. commands[i].dispatch(cmd_parameter,
  130. response);
  131. return i;
  132. } else {
  133. pr_err("command %s not supported.\n", cmd_string);
  134. fastboot_fail("Unsupported command", response);
  135. return -1;
  136. }
  137. }
  138. }
  139. pr_err("command %s not recognized.\n", cmd_string);
  140. fastboot_fail("unrecognized command", response);
  141. return -1;
  142. }
  143. /**
  144. * okay() - Send bare OKAY response
  145. *
  146. * @cmd_parameter: Pointer to command parameter
  147. * @response: Pointer to fastboot response buffer
  148. *
  149. * Send a bare OKAY fastboot response. This is used where the command is
  150. * valid, but all the work is done after the response has been sent (e.g.
  151. * boot, reboot etc.)
  152. */
  153. static void okay(char *cmd_parameter, char *response)
  154. {
  155. fastboot_okay(NULL, response);
  156. }
  157. /**
  158. * getvar() - Read a config/version variable
  159. *
  160. * @cmd_parameter: Pointer to command parameter
  161. * @response: Pointer to fastboot response buffer
  162. */
  163. static void getvar(char *cmd_parameter, char *response)
  164. {
  165. fastboot_getvar(cmd_parameter, response);
  166. }
  167. /**
  168. * fastboot_download() - Start a download transfer from the client
  169. *
  170. * @cmd_parameter: Pointer to command parameter
  171. * @response: Pointer to fastboot response buffer
  172. */
  173. static void download(char *cmd_parameter, char *response)
  174. {
  175. char *tmp;
  176. if (!cmd_parameter) {
  177. fastboot_fail("Expected command parameter", response);
  178. return;
  179. }
  180. fastboot_bytes_received = 0;
  181. fastboot_bytes_expected = hextoul(cmd_parameter, &tmp);
  182. if (fastboot_bytes_expected == 0) {
  183. fastboot_fail("Expected nonzero image size", response);
  184. return;
  185. }
  186. /*
  187. * Nothing to download yet. Response is of the form:
  188. * [DATA|FAIL]$cmd_parameter
  189. *
  190. * where cmd_parameter is an 8 digit hexadecimal number
  191. */
  192. if (fastboot_bytes_expected > fastboot_buf_size) {
  193. fastboot_fail(cmd_parameter, response);
  194. } else {
  195. printf("Starting download of %d bytes\n",
  196. fastboot_bytes_expected);
  197. fastboot_response("DATA", response, "%s", cmd_parameter);
  198. }
  199. }
  200. /**
  201. * fastboot_data_remaining() - return bytes remaining in current transfer
  202. *
  203. * Return: Number of bytes left in the current download
  204. */
  205. u32 fastboot_data_remaining(void)
  206. {
  207. return fastboot_bytes_expected - fastboot_bytes_received;
  208. }
  209. /**
  210. * fastboot_data_download() - Copy image data to fastboot_buf_addr.
  211. *
  212. * @fastboot_data: Pointer to received fastboot data
  213. * @fastboot_data_len: Length of received fastboot data
  214. * @response: Pointer to fastboot response buffer
  215. *
  216. * Copies image data from fastboot_data to fastboot_buf_addr. Writes to
  217. * response. fastboot_bytes_received is updated to indicate the number
  218. * of bytes that have been transferred.
  219. *
  220. * On completion sets image_size and ${filesize} to the total size of the
  221. * downloaded image.
  222. */
  223. void fastboot_data_download(const void *fastboot_data,
  224. unsigned int fastboot_data_len,
  225. char *response)
  226. {
  227. #define BYTES_PER_DOT 0x20000
  228. u32 pre_dot_num, now_dot_num;
  229. if (fastboot_data_len == 0 ||
  230. (fastboot_bytes_received + fastboot_data_len) >
  231. fastboot_bytes_expected) {
  232. fastboot_fail("Received invalid data length",
  233. response);
  234. return;
  235. }
  236. /* Download data to fastboot_buf_addr */
  237. memcpy(fastboot_buf_addr + fastboot_bytes_received,
  238. fastboot_data, fastboot_data_len);
  239. pre_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
  240. fastboot_bytes_received += fastboot_data_len;
  241. now_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
  242. if (pre_dot_num != now_dot_num) {
  243. putc('.');
  244. if (!(now_dot_num % 74))
  245. putc('\n');
  246. }
  247. *response = '\0';
  248. }
  249. /**
  250. * fastboot_data_complete() - Mark current transfer complete
  251. *
  252. * @response: Pointer to fastboot response buffer
  253. *
  254. * Set image_size and ${filesize} to the total size of the downloaded image.
  255. */
  256. void fastboot_data_complete(char *response)
  257. {
  258. /* Download complete. Respond with "OKAY" */
  259. fastboot_okay(NULL, response);
  260. printf("\ndownloading of %d bytes finished\n", fastboot_bytes_received);
  261. image_size = fastboot_bytes_received;
  262. env_set_hex("filesize", image_size);
  263. fastboot_bytes_expected = 0;
  264. fastboot_bytes_received = 0;
  265. }
  266. /**
  267. * flash() - write the downloaded image to the indicated partition.
  268. *
  269. * @cmd_parameter: Pointer to partition name
  270. * @response: Pointer to fastboot response buffer
  271. *
  272. * Writes the previously downloaded image to the partition indicated by
  273. * cmd_parameter. Writes to response.
  274. */
  275. static void __maybe_unused flash(char *cmd_parameter, char *response)
  276. {
  277. if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC))
  278. fastboot_mmc_flash_write(cmd_parameter, fastboot_buf_addr,
  279. image_size, response);
  280. if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_NAND))
  281. fastboot_nand_flash_write(cmd_parameter, fastboot_buf_addr,
  282. image_size, response);
  283. }
  284. /**
  285. * erase() - erase the indicated partition.
  286. *
  287. * @cmd_parameter: Pointer to partition name
  288. * @response: Pointer to fastboot response buffer
  289. *
  290. * Erases the partition indicated by cmd_parameter (clear to 0x00s). Writes
  291. * to response.
  292. */
  293. static void __maybe_unused erase(char *cmd_parameter, char *response)
  294. {
  295. if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC))
  296. fastboot_mmc_erase(cmd_parameter, response);
  297. if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_NAND))
  298. fastboot_nand_erase(cmd_parameter, response);
  299. }
  300. /**
  301. * run_ucmd() - Execute the UCmd command
  302. *
  303. * @cmd_parameter: Pointer to command parameter
  304. * @response: Pointer to fastboot response buffer
  305. */
  306. static void __maybe_unused run_ucmd(char *cmd_parameter, char *response)
  307. {
  308. if (!cmd_parameter) {
  309. pr_err("missing slot suffix\n");
  310. fastboot_fail("missing command", response);
  311. return;
  312. }
  313. if (run_command(cmd_parameter, 0))
  314. fastboot_fail("", response);
  315. else
  316. fastboot_okay(NULL, response);
  317. }
  318. static char g_a_cmd_buff[64];
  319. void fastboot_acmd_complete(void)
  320. {
  321. run_command(g_a_cmd_buff, 0);
  322. }
  323. /**
  324. * run_acmd() - Execute the ACmd command
  325. *
  326. * @cmd_parameter: Pointer to command parameter
  327. * @response: Pointer to fastboot response buffer
  328. */
  329. static void __maybe_unused run_acmd(char *cmd_parameter, char *response)
  330. {
  331. if (!cmd_parameter) {
  332. pr_err("missing slot suffix\n");
  333. fastboot_fail("missing command", response);
  334. return;
  335. }
  336. if (strlen(cmd_parameter) > sizeof(g_a_cmd_buff)) {
  337. pr_err("too long command\n");
  338. fastboot_fail("too long command", response);
  339. return;
  340. }
  341. strcpy(g_a_cmd_buff, cmd_parameter);
  342. fastboot_okay(NULL, response);
  343. }
  344. /**
  345. * reboot_bootloader() - Sets reboot bootloader flag.
  346. *
  347. * @cmd_parameter: Pointer to command parameter
  348. * @response: Pointer to fastboot response buffer
  349. */
  350. static void reboot_bootloader(char *cmd_parameter, char *response)
  351. {
  352. if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_BOOTLOADER))
  353. fastboot_fail("Cannot set reboot flag", response);
  354. else
  355. fastboot_okay(NULL, response);
  356. }
  357. /**
  358. * reboot_fastbootd() - Sets reboot fastboot flag.
  359. *
  360. * @cmd_parameter: Pointer to command parameter
  361. * @response: Pointer to fastboot response buffer
  362. */
  363. static void reboot_fastbootd(char *cmd_parameter, char *response)
  364. {
  365. if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_FASTBOOTD))
  366. fastboot_fail("Cannot set fastboot flag", response);
  367. else
  368. fastboot_okay(NULL, response);
  369. }
  370. /**
  371. * reboot_recovery() - Sets reboot recovery flag.
  372. *
  373. * @cmd_parameter: Pointer to command parameter
  374. * @response: Pointer to fastboot response buffer
  375. */
  376. static void reboot_recovery(char *cmd_parameter, char *response)
  377. {
  378. if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_RECOVERY))
  379. fastboot_fail("Cannot set recovery flag", response);
  380. else
  381. fastboot_okay(NULL, response);
  382. }
  383. /**
  384. * oem_format() - Execute the OEM format command
  385. *
  386. * @cmd_parameter: Pointer to command parameter
  387. * @response: Pointer to fastboot response buffer
  388. */
  389. static void __maybe_unused oem_format(char *cmd_parameter, char *response)
  390. {
  391. char cmdbuf[32];
  392. const int mmc_dev = config_opt_enabled(CONFIG_FASTBOOT_FLASH_MMC,
  393. CONFIG_FASTBOOT_FLASH_MMC_DEV, -1);
  394. if (!env_get("partitions")) {
  395. fastboot_fail("partitions not set", response);
  396. } else {
  397. sprintf(cmdbuf, "gpt write mmc %x $partitions", mmc_dev);
  398. if (run_command(cmdbuf, 0))
  399. fastboot_fail("", response);
  400. else
  401. fastboot_okay(NULL, response);
  402. }
  403. }
  404. /**
  405. * oem_partconf() - Execute the OEM partconf command
  406. *
  407. * @cmd_parameter: Pointer to command parameter
  408. * @response: Pointer to fastboot response buffer
  409. */
  410. static void __maybe_unused oem_partconf(char *cmd_parameter, char *response)
  411. {
  412. char cmdbuf[32];
  413. const int mmc_dev = config_opt_enabled(CONFIG_FASTBOOT_FLASH_MMC,
  414. CONFIG_FASTBOOT_FLASH_MMC_DEV, -1);
  415. if (!cmd_parameter) {
  416. fastboot_fail("Expected command parameter", response);
  417. return;
  418. }
  419. /* execute 'mmc partconfg' command with cmd_parameter arguments*/
  420. snprintf(cmdbuf, sizeof(cmdbuf), "mmc partconf %x %s 0", mmc_dev, cmd_parameter);
  421. printf("Execute: %s\n", cmdbuf);
  422. if (run_command(cmdbuf, 0))
  423. fastboot_fail("Cannot set oem partconf", response);
  424. else
  425. fastboot_okay(NULL, response);
  426. }
  427. /**
  428. * oem_bootbus() - Execute the OEM bootbus command
  429. *
  430. * @cmd_parameter: Pointer to command parameter
  431. * @response: Pointer to fastboot response buffer
  432. */
  433. static void __maybe_unused oem_bootbus(char *cmd_parameter, char *response)
  434. {
  435. char cmdbuf[32];
  436. const int mmc_dev = config_opt_enabled(CONFIG_FASTBOOT_FLASH_MMC,
  437. CONFIG_FASTBOOT_FLASH_MMC_DEV, -1);
  438. if (!cmd_parameter) {
  439. fastboot_fail("Expected command parameter", response);
  440. return;
  441. }
  442. /* execute 'mmc bootbus' command with cmd_parameter arguments*/
  443. snprintf(cmdbuf, sizeof(cmdbuf), "mmc bootbus %x %s", mmc_dev, cmd_parameter);
  444. printf("Execute: %s\n", cmdbuf);
  445. if (run_command(cmdbuf, 0))
  446. fastboot_fail("Cannot set oem bootbus", response);
  447. else
  448. fastboot_okay(NULL, response);
  449. }