fastboot.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 <fastboot.h>
  8. #include <net.h>
  9. #include <net/fastboot.h>
  10. enum {
  11. FASTBOOT_ERROR = 0,
  12. FASTBOOT_QUERY = 1,
  13. FASTBOOT_INIT = 2,
  14. FASTBOOT_FASTBOOT = 3,
  15. };
  16. struct __packed fastboot_header {
  17. uchar id;
  18. uchar flags;
  19. unsigned short seq;
  20. };
  21. #define PACKET_SIZE 1024
  22. #define DATA_SIZE (PACKET_SIZE - sizeof(struct fastboot_header))
  23. /* Sequence number sent for every packet */
  24. static unsigned short sequence_number = 1;
  25. static const unsigned short packet_size = PACKET_SIZE;
  26. static const unsigned short udp_version = 1;
  27. /* Keep track of last packet for resubmission */
  28. static uchar last_packet[PACKET_SIZE];
  29. static unsigned int last_packet_len;
  30. static struct in_addr fastboot_remote_ip;
  31. /* The UDP port at their end */
  32. static int fastboot_remote_port;
  33. /* The UDP port at our end */
  34. static int fastboot_our_port;
  35. static void boot_downloaded_image(void);
  36. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
  37. /**
  38. * fastboot_udp_send_info() - Send an INFO packet during long commands.
  39. *
  40. * @msg: String describing the reason for waiting
  41. */
  42. static void fastboot_udp_send_info(const char *msg)
  43. {
  44. uchar *packet;
  45. uchar *packet_base;
  46. int len = 0;
  47. char response[FASTBOOT_RESPONSE_LEN] = {0};
  48. struct fastboot_header response_header = {
  49. .id = FASTBOOT_FASTBOOT,
  50. .flags = 0,
  51. .seq = htons(sequence_number)
  52. };
  53. ++sequence_number;
  54. packet = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
  55. packet_base = packet;
  56. /* Write headers */
  57. memcpy(packet, &response_header, sizeof(response_header));
  58. packet += sizeof(response_header);
  59. /* Write response */
  60. fastboot_response("INFO", response, "%s", msg);
  61. memcpy(packet, response, strlen(response));
  62. packet += strlen(response);
  63. len = packet - packet_base;
  64. /* Save packet for retransmitting */
  65. last_packet_len = len;
  66. memcpy(last_packet, packet_base, last_packet_len);
  67. net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
  68. fastboot_remote_port, fastboot_our_port, len);
  69. }
  70. /**
  71. * fastboot_timed_send_info() - Send INFO packet every 30 seconds
  72. *
  73. * @msg: String describing the reason for waiting
  74. *
  75. * Send an INFO packet during long commands based on timer. An INFO packet
  76. * is sent if the time is 30 seconds after start. Else, noop.
  77. */
  78. static void fastboot_timed_send_info(const char *msg)
  79. {
  80. static ulong start;
  81. /* Initialize timer */
  82. if (start == 0)
  83. start = get_timer(0);
  84. ulong time = get_timer(start);
  85. /* Send INFO packet to host every 30 seconds */
  86. if (time >= 30000) {
  87. start = get_timer(0);
  88. fastboot_udp_send_info(msg);
  89. }
  90. }
  91. #endif
  92. /**
  93. * fastboot_send() - Sends a packet in response to received fastboot packet
  94. *
  95. * @header: Header for response packet
  96. * @fastboot_data: Pointer to received fastboot data
  97. * @fastboot_data_len: Length of received fastboot data
  98. * @retransmit: Nonzero if sending last sent packet
  99. */
  100. static void fastboot_send(struct fastboot_header header, char *fastboot_data,
  101. unsigned int fastboot_data_len, uchar retransmit)
  102. {
  103. uchar *packet;
  104. uchar *packet_base;
  105. int len = 0;
  106. const char *error_msg = "An error occurred.";
  107. short tmp;
  108. struct fastboot_header response_header = header;
  109. static char command[FASTBOOT_COMMAND_LEN];
  110. static int cmd = -1;
  111. static bool pending_command;
  112. char response[FASTBOOT_RESPONSE_LEN] = {0};
  113. /*
  114. * We will always be sending some sort of packet, so
  115. * cobble together the packet headers now.
  116. */
  117. packet = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
  118. packet_base = packet;
  119. /* Resend last packet */
  120. if (retransmit) {
  121. memcpy(packet, last_packet, last_packet_len);
  122. net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
  123. fastboot_remote_port, fastboot_our_port,
  124. last_packet_len);
  125. return;
  126. }
  127. response_header.seq = htons(response_header.seq);
  128. memcpy(packet, &response_header, sizeof(response_header));
  129. packet += sizeof(response_header);
  130. switch (header.id) {
  131. case FASTBOOT_QUERY:
  132. tmp = htons(sequence_number);
  133. memcpy(packet, &tmp, sizeof(tmp));
  134. packet += sizeof(tmp);
  135. break;
  136. case FASTBOOT_INIT:
  137. tmp = htons(udp_version);
  138. memcpy(packet, &tmp, sizeof(tmp));
  139. packet += sizeof(tmp);
  140. tmp = htons(packet_size);
  141. memcpy(packet, &tmp, sizeof(tmp));
  142. packet += sizeof(tmp);
  143. break;
  144. case FASTBOOT_ERROR:
  145. memcpy(packet, error_msg, strlen(error_msg));
  146. packet += strlen(error_msg);
  147. break;
  148. case FASTBOOT_FASTBOOT:
  149. if (cmd == FASTBOOT_COMMAND_DOWNLOAD) {
  150. if (!fastboot_data_len && !fastboot_data_remaining()) {
  151. fastboot_data_complete(response);
  152. } else {
  153. fastboot_data_download(fastboot_data,
  154. fastboot_data_len,
  155. response);
  156. }
  157. } else if (!pending_command) {
  158. strlcpy(command, fastboot_data,
  159. min((size_t)fastboot_data_len + 1,
  160. sizeof(command)));
  161. pending_command = true;
  162. } else {
  163. cmd = fastboot_handle_command(command, response);
  164. pending_command = false;
  165. }
  166. /*
  167. * Sent some INFO packets, need to update sequence number in
  168. * header
  169. */
  170. if (header.seq != sequence_number) {
  171. response_header.seq = htons(sequence_number);
  172. memcpy(packet_base, &response_header,
  173. sizeof(response_header));
  174. }
  175. /* Write response to packet */
  176. memcpy(packet, response, strlen(response));
  177. packet += strlen(response);
  178. break;
  179. default:
  180. pr_err("ID %d not implemented.\n", header.id);
  181. return;
  182. }
  183. len = packet - packet_base;
  184. /* Save packet for retransmitting */
  185. last_packet_len = len;
  186. memcpy(last_packet, packet_base, last_packet_len);
  187. net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
  188. fastboot_remote_port, fastboot_our_port, len);
  189. /* Continue boot process after sending response */
  190. if (!strncmp("OKAY", response, 4)) {
  191. switch (cmd) {
  192. case FASTBOOT_COMMAND_BOOT:
  193. boot_downloaded_image();
  194. break;
  195. case FASTBOOT_COMMAND_CONTINUE:
  196. net_set_state(NETLOOP_SUCCESS);
  197. break;
  198. case FASTBOOT_COMMAND_REBOOT:
  199. case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
  200. case FASTBOOT_COMMAND_REBOOT_FASTBOOTD:
  201. case FASTBOOT_COMMAND_REBOOT_RECOVERY:
  202. do_reset(NULL, 0, 0, NULL);
  203. break;
  204. }
  205. }
  206. if (!strncmp("OKAY", response, 4) || !strncmp("FAIL", response, 4))
  207. cmd = -1;
  208. }
  209. /**
  210. * boot_downloaded_image() - Boots into downloaded image.
  211. */
  212. static void boot_downloaded_image(void)
  213. {
  214. fastboot_boot();
  215. net_set_state(NETLOOP_SUCCESS);
  216. }
  217. /**
  218. * fastboot_handler() - Incoming UDP packet handler.
  219. *
  220. * @packet: Pointer to incoming UDP packet
  221. * @dport: Destination UDP port
  222. * @sip: Source IP address
  223. * @sport: Source UDP port
  224. * @len: Packet length
  225. */
  226. static void fastboot_handler(uchar *packet, unsigned int dport,
  227. struct in_addr sip, unsigned int sport,
  228. unsigned int len)
  229. {
  230. struct fastboot_header header;
  231. char fastboot_data[DATA_SIZE] = {0};
  232. unsigned int fastboot_data_len = 0;
  233. if (dport != fastboot_our_port)
  234. return;
  235. fastboot_remote_ip = sip;
  236. fastboot_remote_port = sport;
  237. if (len < sizeof(struct fastboot_header) || len > PACKET_SIZE)
  238. return;
  239. memcpy(&header, packet, sizeof(header));
  240. header.flags = 0;
  241. header.seq = ntohs(header.seq);
  242. packet += sizeof(header);
  243. len -= sizeof(header);
  244. switch (header.id) {
  245. case FASTBOOT_QUERY:
  246. fastboot_send(header, fastboot_data, 0, 0);
  247. break;
  248. case FASTBOOT_INIT:
  249. case FASTBOOT_FASTBOOT:
  250. fastboot_data_len = len;
  251. if (len > 0)
  252. memcpy(fastboot_data, packet, len);
  253. if (header.seq == sequence_number) {
  254. fastboot_send(header, fastboot_data,
  255. fastboot_data_len, 0);
  256. sequence_number++;
  257. } else if (header.seq == sequence_number - 1) {
  258. /* Retransmit last sent packet */
  259. fastboot_send(header, fastboot_data,
  260. fastboot_data_len, 1);
  261. }
  262. break;
  263. default:
  264. pr_err("ID %d not implemented.\n", header.id);
  265. header.id = FASTBOOT_ERROR;
  266. fastboot_send(header, fastboot_data, 0, 0);
  267. break;
  268. }
  269. }
  270. void fastboot_start_server(void)
  271. {
  272. printf("Using %s device\n", eth_get_name());
  273. printf("Listening for fastboot command on %pI4\n", &net_ip);
  274. fastboot_our_port = CONFIG_UDP_FUNCTION_FASTBOOT_PORT;
  275. #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
  276. fastboot_set_progress_callback(fastboot_timed_send_info);
  277. #endif
  278. net_set_udp_handler(fastboot_handler);
  279. /* zero out server ether in case the server ip has changed */
  280. memset(net_server_ethaddr, 0, 6);
  281. }