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