fb_command.c 8.4 KB

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