fb_command.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 <flash.h>
  13. #include <part.h>
  14. #include <stdlib.h>
  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. static void reboot_fastbootd(char *, char *);
  36. static void reboot_recovery(char *, char *);
  37. #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
  38. static void oem_format(char *, char *);
  39. #endif
  40. static const struct {
  41. const char *command;
  42. void (*dispatch)(char *cmd_parameter, char *response);
  43. } commands[FASTBOOT_COMMAND_COUNT] = {
  44. [FASTBOOT_COMMAND_GETVAR] = {
  45. .command = "getvar",
  46. .dispatch = getvar
  47. },
  48. [FASTBOOT_COMMAND_DOWNLOAD] = {
  49. .command = "download",
  50. .dispatch = download
  51. },
  52. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
  53. [FASTBOOT_COMMAND_FLASH] = {
  54. .command = "flash",
  55. .dispatch = flash
  56. },
  57. [FASTBOOT_COMMAND_ERASE] = {
  58. .command = "erase",
  59. .dispatch = erase
  60. },
  61. #endif
  62. [FASTBOOT_COMMAND_BOOT] = {
  63. .command = "boot",
  64. .dispatch = okay
  65. },
  66. [FASTBOOT_COMMAND_CONTINUE] = {
  67. .command = "continue",
  68. .dispatch = okay
  69. },
  70. [FASTBOOT_COMMAND_REBOOT] = {
  71. .command = "reboot",
  72. .dispatch = okay
  73. },
  74. [FASTBOOT_COMMAND_REBOOT_BOOTLOADER] = {
  75. .command = "reboot-bootloader",
  76. .dispatch = reboot_bootloader
  77. },
  78. [FASTBOOT_COMMAND_REBOOT_FASTBOOTD] = {
  79. .command = "reboot-fastboot",
  80. .dispatch = reboot_fastbootd
  81. },
  82. [FASTBOOT_COMMAND_REBOOT_RECOVERY] = {
  83. .command = "reboot-recovery",
  84. .dispatch = reboot_recovery
  85. },
  86. [FASTBOOT_COMMAND_SET_ACTIVE] = {
  87. .command = "set_active",
  88. .dispatch = okay
  89. },
  90. #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
  91. [FASTBOOT_COMMAND_OEM_FORMAT] = {
  92. .command = "oem format",
  93. .dispatch = oem_format,
  94. },
  95. #endif
  96. };
  97. /**
  98. * fastboot_handle_command - Handle fastboot command
  99. *
  100. * @cmd_string: Pointer to command string
  101. * @response: Pointer to fastboot response buffer
  102. *
  103. * Return: Executed command, or -1 if not recognized
  104. */
  105. int fastboot_handle_command(char *cmd_string, char *response)
  106. {
  107. int i;
  108. char *cmd_parameter;
  109. cmd_parameter = cmd_string;
  110. strsep(&cmd_parameter, ":");
  111. for (i = 0; i < FASTBOOT_COMMAND_COUNT; i++) {
  112. if (!strcmp(commands[i].command, cmd_string)) {
  113. if (commands[i].dispatch) {
  114. commands[i].dispatch(cmd_parameter,
  115. response);
  116. return i;
  117. } else {
  118. break;
  119. }
  120. }
  121. }
  122. pr_err("command %s not recognized.\n", cmd_string);
  123. fastboot_fail("unrecognized command", response);
  124. return -1;
  125. }
  126. /**
  127. * okay() - Send bare OKAY response
  128. *
  129. * @cmd_parameter: Pointer to command parameter
  130. * @response: Pointer to fastboot response buffer
  131. *
  132. * Send a bare OKAY fastboot response. This is used where the command is
  133. * valid, but all the work is done after the response has been sent (e.g.
  134. * boot, reboot etc.)
  135. */
  136. static void okay(char *cmd_parameter, char *response)
  137. {
  138. fastboot_okay(NULL, response);
  139. }
  140. /**
  141. * getvar() - Read a config/version variable
  142. *
  143. * @cmd_parameter: Pointer to command parameter
  144. * @response: Pointer to fastboot response buffer
  145. */
  146. static void getvar(char *cmd_parameter, char *response)
  147. {
  148. fastboot_getvar(cmd_parameter, response);
  149. }
  150. /**
  151. * fastboot_download() - Start a download transfer from the client
  152. *
  153. * @cmd_parameter: Pointer to command parameter
  154. * @response: Pointer to fastboot response buffer
  155. */
  156. static void download(char *cmd_parameter, char *response)
  157. {
  158. char *tmp;
  159. if (!cmd_parameter) {
  160. fastboot_fail("Expected command parameter", response);
  161. return;
  162. }
  163. fastboot_bytes_received = 0;
  164. fastboot_bytes_expected = simple_strtoul(cmd_parameter, &tmp, 16);
  165. if (fastboot_bytes_expected == 0) {
  166. fastboot_fail("Expected nonzero image size", response);
  167. return;
  168. }
  169. /*
  170. * Nothing to download yet. Response is of the form:
  171. * [DATA|FAIL]$cmd_parameter
  172. *
  173. * where cmd_parameter is an 8 digit hexadecimal number
  174. */
  175. if (fastboot_bytes_expected > fastboot_buf_size) {
  176. fastboot_fail(cmd_parameter, response);
  177. } else {
  178. printf("Starting download of %d bytes\n",
  179. fastboot_bytes_expected);
  180. fastboot_response("DATA", response, "%s", cmd_parameter);
  181. }
  182. }
  183. /**
  184. * fastboot_data_remaining() - return bytes remaining in current transfer
  185. *
  186. * Return: Number of bytes left in the current download
  187. */
  188. u32 fastboot_data_remaining(void)
  189. {
  190. return fastboot_bytes_expected - fastboot_bytes_received;
  191. }
  192. /**
  193. * fastboot_data_download() - Copy image data to fastboot_buf_addr.
  194. *
  195. * @fastboot_data: Pointer to received fastboot data
  196. * @fastboot_data_len: Length of received fastboot data
  197. * @response: Pointer to fastboot response buffer
  198. *
  199. * Copies image data from fastboot_data to fastboot_buf_addr. Writes to
  200. * response. fastboot_bytes_received is updated to indicate the number
  201. * of bytes that have been transferred.
  202. *
  203. * On completion sets image_size and ${filesize} to the total size of the
  204. * downloaded image.
  205. */
  206. void fastboot_data_download(const void *fastboot_data,
  207. unsigned int fastboot_data_len,
  208. char *response)
  209. {
  210. #define BYTES_PER_DOT 0x20000
  211. u32 pre_dot_num, now_dot_num;
  212. if (fastboot_data_len == 0 ||
  213. (fastboot_bytes_received + fastboot_data_len) >
  214. fastboot_bytes_expected) {
  215. fastboot_fail("Received invalid data length",
  216. response);
  217. return;
  218. }
  219. /* Download data to fastboot_buf_addr */
  220. memcpy(fastboot_buf_addr + fastboot_bytes_received,
  221. fastboot_data, fastboot_data_len);
  222. pre_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
  223. fastboot_bytes_received += fastboot_data_len;
  224. now_dot_num = fastboot_bytes_received / BYTES_PER_DOT;
  225. if (pre_dot_num != now_dot_num) {
  226. putc('.');
  227. if (!(now_dot_num % 74))
  228. putc('\n');
  229. }
  230. *response = '\0';
  231. }
  232. /**
  233. * fastboot_data_complete() - Mark current transfer complete
  234. *
  235. * @response: Pointer to fastboot response buffer
  236. *
  237. * Set image_size and ${filesize} to the total size of the downloaded image.
  238. */
  239. void fastboot_data_complete(char *response)
  240. {
  241. /* Download complete. Respond with "OKAY" */
  242. fastboot_okay(NULL, response);
  243. printf("\ndownloading of %d bytes finished\n", fastboot_bytes_received);
  244. image_size = fastboot_bytes_received;
  245. env_set_hex("filesize", image_size);
  246. fastboot_bytes_expected = 0;
  247. fastboot_bytes_received = 0;
  248. }
  249. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
  250. /**
  251. * flash() - write the downloaded image to the indicated partition.
  252. *
  253. * @cmd_parameter: Pointer to partition name
  254. * @response: Pointer to fastboot response buffer
  255. *
  256. * Writes the previously downloaded image to the partition indicated by
  257. * cmd_parameter. Writes to response.
  258. */
  259. static void flash(char *cmd_parameter, char *response)
  260. {
  261. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
  262. fastboot_mmc_flash_write(cmd_parameter, fastboot_buf_addr, image_size,
  263. response);
  264. #endif
  265. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
  266. fastboot_nand_flash_write(cmd_parameter, fastboot_buf_addr, image_size,
  267. response);
  268. #endif
  269. }
  270. /**
  271. * erase() - erase the indicated partition.
  272. *
  273. * @cmd_parameter: Pointer to partition name
  274. * @response: Pointer to fastboot response buffer
  275. *
  276. * Erases the partition indicated by cmd_parameter (clear to 0x00s). Writes
  277. * to response.
  278. */
  279. static void erase(char *cmd_parameter, char *response)
  280. {
  281. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
  282. fastboot_mmc_erase(cmd_parameter, response);
  283. #endif
  284. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
  285. fastboot_nand_erase(cmd_parameter, response);
  286. #endif
  287. }
  288. #endif
  289. /**
  290. * reboot_bootloader() - Sets reboot bootloader flag.
  291. *
  292. * @cmd_parameter: Pointer to command parameter
  293. * @response: Pointer to fastboot response buffer
  294. */
  295. static void reboot_bootloader(char *cmd_parameter, char *response)
  296. {
  297. if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_BOOTLOADER))
  298. fastboot_fail("Cannot set reboot flag", response);
  299. else
  300. fastboot_okay(NULL, response);
  301. }
  302. /**
  303. * reboot_fastbootd() - Sets reboot fastboot flag.
  304. *
  305. * @cmd_parameter: Pointer to command parameter
  306. * @response: Pointer to fastboot response buffer
  307. */
  308. static void reboot_fastbootd(char *cmd_parameter, char *response)
  309. {
  310. if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_FASTBOOTD))
  311. fastboot_fail("Cannot set fastboot flag", response);
  312. else
  313. fastboot_okay(NULL, response);
  314. }
  315. /**
  316. * reboot_recovery() - Sets reboot recovery flag.
  317. *
  318. * @cmd_parameter: Pointer to command parameter
  319. * @response: Pointer to fastboot response buffer
  320. */
  321. static void reboot_recovery(char *cmd_parameter, char *response)
  322. {
  323. if (fastboot_set_reboot_flag(FASTBOOT_REBOOT_REASON_RECOVERY))
  324. fastboot_fail("Cannot set recovery flag", response);
  325. else
  326. fastboot_okay(NULL, response);
  327. }
  328. #if CONFIG_IS_ENABLED(FASTBOOT_CMD_OEM_FORMAT)
  329. /**
  330. * oem_format() - Execute the OEM format command
  331. *
  332. * @cmd_parameter: Pointer to command parameter
  333. * @response: Pointer to fastboot response buffer
  334. */
  335. static void oem_format(char *cmd_parameter, char *response)
  336. {
  337. char cmdbuf[32];
  338. if (!env_get("partitions")) {
  339. fastboot_fail("partitions not set", response);
  340. } else {
  341. sprintf(cmdbuf, "gpt write mmc %x $partitions",
  342. CONFIG_FASTBOOT_FLASH_MMC_DEV);
  343. if (run_command(cmdbuf, 0))
  344. fastboot_fail("", response);
  345. else
  346. fastboot_okay(NULL, response);
  347. }
  348. }
  349. #endif