fb_command.c 7.6 KB

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