fb_command.c 8.4 KB

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