fastboot.c 8.2 KB

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