fb_command.c 8.4 KB

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