netkexec.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* Kernel hot-reload over UDP: the host pushes kernel.bin as datagrams, the
  2. * board writes them into the kexec staging buffer, verifies the checksum and
  3. * jumps (kexec.c has the serial variant).
  4. *
  5. * Protocol (little-endian; UDP to NETKEXEC_PORT; the board replies to the
  6. * sender's IP/MAC/port):
  7. *
  8. * host -> board header "KXNU" entry size csum (16 bytes)
  9. * host -> board data "KXND" offset len payload... (12 + len)
  10. * board -> host ack "KXNA" next_offset status (12 bytes)
  11. * status 0 = keep going, 1 = done, 2 = bad checksum
  12. *
  13. * The board auto-detects the header from the WM loop (netkexec_pending());
  14. * program_netkexec_main() then runs stop-and-wait: every datagram is acked
  15. * with the next expected offset, so a lost or duplicated datagram costs one
  16. * retransmit. Datagrams addressed to the broadcast IP are accepted too, so
  17. * the host can push without knowing whether the board is up yet.
  18. */
  19. #include "io.h"
  20. #include "kexec.h"
  21. #include "net.h"
  22. #include "netdev.h"
  23. #include "serial.h"
  24. #include "timer.h"
  25. #include "netkexec.h"
  26. #define NETKEXEC_PORT 19288
  27. #define NETKEXEC_DATA_MAX 1400
  28. #define X_MAGIC_HDR 0x554E584Bu /* "KXNU" */
  29. #define X_MAGIC_DATA 0x444E584Bu /* "KXND" */
  30. #define X_MAGIC_ACK 0x414E584Bu /* "KXNA" */
  31. #define ACK_CONTINUE 0
  32. #define ACK_DONE 1
  33. #define ACK_BADSUM 2
  34. static unsigned char rxbuf[1520]; /* one full Ethernet frame */
  35. static unsigned char replybuf[1520];
  36. static unsigned char ackbuf[12];
  37. /* Pending transfer (filled by netkexec_pending). */
  38. static int have_header;
  39. static unsigned int x_entry, x_size, x_csum;
  40. static unsigned char peer_mac[6], peer_ip[4];
  41. static unsigned int peer_port;
  42. static unsigned int rd32(const unsigned char *p)
  43. {
  44. return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
  45. ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
  46. }
  47. static void wr32(unsigned char *p, unsigned int v)
  48. {
  49. p[0] = (unsigned char)(v & 0xFF);
  50. p[1] = (unsigned char)((v >> 8) & 0xFF);
  51. p[2] = (unsigned char)((v >> 16) & 0xFF);
  52. p[3] = (unsigned char)((v >> 24) & 0xFF);
  53. }
  54. static int ip_eq(const unsigned char *a, const unsigned char *b)
  55. {
  56. return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3];
  57. }
  58. static int ip_is_bcast(const unsigned char *ip)
  59. {
  60. return ip[0] == 0xFF && ip[1] == 0xFF && ip[2] == 0xFF && ip[3] == 0xFF;
  61. }
  62. /* Parse `f` as a UDP datagram to NETKEXEC_PORT; fill payload + peer address.
  63. * Returns 1 on match. */
  64. static int parse_udp(const unsigned char *f, unsigned int len,
  65. const unsigned char **payload, unsigned int *plen,
  66. unsigned char p_mac[6], unsigned char p_ip[4],
  67. unsigned int *p_port)
  68. {
  69. const unsigned char *ip = f + 14;
  70. const unsigned char *udp;
  71. unsigned char my_ip[4];
  72. unsigned int i, ihl, ip_len;
  73. if (len < 14 + 20 + 8)
  74. return 0;
  75. if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
  76. return 0;
  77. if ((ip[0] >> 4) != 4)
  78. return 0;
  79. ihl = (unsigned int)(ip[0] & 0x0F) * 4;
  80. if (ihl < 20 || ip[9] != IPPROTO_UDP)
  81. return 0;
  82. ip_len = ((unsigned int)ip[2] << 8) | ip[3];
  83. if (ip_len < ihl + 8 || 14 + ip_len > len)
  84. return 0;
  85. net_get_ip(my_ip);
  86. if (!ip_eq(ip + 16, my_ip) && !ip_is_bcast(ip + 16))
  87. return 0;
  88. udp = ip + ihl;
  89. if ((((unsigned int)udp[2] << 8) | udp[3]) != NETKEXEC_PORT)
  90. return 0;
  91. for (i = 0; i < 6; i++)
  92. p_mac[i] = f[6 + i];
  93. for (i = 0; i < 4; i++)
  94. p_ip[i] = ip[12 + i];
  95. *p_port = ((unsigned int)udp[0] << 8) | udp[1];
  96. *payload = udp + 8;
  97. *plen = ip_len - ihl - 8;
  98. return *plen > 0;
  99. }
  100. int netkexec_pending(void)
  101. {
  102. const struct netdev *nd;
  103. const unsigned char *payload;
  104. unsigned int plen, port;
  105. int len;
  106. if (kexec_busy || netdev_in_use())
  107. return 0;
  108. nd = netdev_active();
  109. if (!nd)
  110. return 0;
  111. /* One frame per call; anything else is dropped (this also keeps the RX
  112. * ring drained while the system idles at a prompt). */
  113. len = nd->rx(rxbuf, sizeof(rxbuf));
  114. if (len < 0)
  115. return 0;
  116. if (!parse_udp(rxbuf, (unsigned int)len, &payload, &plen,
  117. peer_mac, peer_ip, &port)) {
  118. /* Not a kexec datagram: stay reachable by answering ARP and ICMP
  119. * echo requests for our IP (so the board can be pinged, and a
  120. * host can resolve its MAC, without running a command). Until
  121. * an address is configured there is nothing to answer for. */
  122. int rlen;
  123. if (!net_is_configured())
  124. return 0;
  125. rlen = net_build_reply(rxbuf, (unsigned int)len,
  126. replybuf, sizeof(replybuf));
  127. if (rlen > 0)
  128. nd->tx(replybuf, (unsigned int)rlen);
  129. return 0;
  130. }
  131. if (plen < 16 || rd32(payload) != X_MAGIC_HDR)
  132. return 0;
  133. if (plen < 16 || rd32(payload) != X_MAGIC_HDR)
  134. return 0;
  135. x_entry = rd32(payload + 4);
  136. x_size = rd32(payload + 8);
  137. x_csum = rd32(payload + 12);
  138. if (x_size == 0 || x_size > KEXEC_MAX)
  139. return 0;
  140. peer_port = port;
  141. have_header = 1;
  142. return 1;
  143. }
  144. static void send_ack(unsigned int next_offset, unsigned int status)
  145. {
  146. wr32(ackbuf, X_MAGIC_ACK);
  147. wr32(ackbuf + 4, next_offset);
  148. wr32(ackbuf + 8, status);
  149. net_udp_send(peer_mac, peer_ip, NETKEXEC_PORT, peer_port,
  150. ackbuf, sizeof(ackbuf));
  151. }
  152. static int netkexec_recv(void)
  153. {
  154. volatile unsigned char *stage = (volatile unsigned char *)kexec_stage;
  155. unsigned int offset = 0, retries = 0, sum = 0;
  156. unsigned int deadline = get_ticks_ms() + 3000;
  157. kexec_total = x_size;
  158. kexec_progress = 0;
  159. send_ack(0, ACK_CONTINUE); /* header ok, send from offset 0 */
  160. while (offset < x_size) {
  161. const unsigned char *payload;
  162. unsigned int plen, port, doff, dlen;
  163. int len, i;
  164. if ((int)(get_ticks_ms() - deadline) > 0) {
  165. if (++retries > 10)
  166. return -1; /* host gave up */
  167. send_ack(offset, ACK_CONTINUE);
  168. deadline = get_ticks_ms() + 1000;
  169. continue;
  170. }
  171. len = netdev_active()->rx(rxbuf, sizeof(rxbuf));
  172. if (len < 0)
  173. continue;
  174. if (!parse_udp(rxbuf, (unsigned int)len, &payload, &plen,
  175. peer_mac, peer_ip, &port))
  176. continue;
  177. if (plen < 12 || rd32(payload) != X_MAGIC_DATA)
  178. continue;
  179. doff = rd32(payload + 4);
  180. dlen = rd32(payload + 8);
  181. if (dlen > NETKEXEC_DATA_MAX || plen < 12 + dlen)
  182. continue;
  183. if (doff == offset) {
  184. for (i = 0; i < (int)dlen; i++) {
  185. stage[offset + i] = payload[12 + i];
  186. sum += payload[12 + i];
  187. }
  188. offset += dlen;
  189. kexec_progress = offset;
  190. retries = 0;
  191. deadline = get_ticks_ms() + 1000;
  192. if ((offset & 0x3FFF) == 0)
  193. io_putchar('.');
  194. }
  195. send_ack(offset, ACK_CONTINUE); /* also acks duplicates */
  196. }
  197. if (sum != x_csum) {
  198. send_ack(offset, ACK_BADSUM);
  199. return -1;
  200. }
  201. send_ack(x_size, ACK_DONE);
  202. return 0;
  203. }
  204. void program_netkexec_main(void)
  205. {
  206. if (!have_header) {
  207. io_putstr("netkexec: no transfer pending\n");
  208. kexec_busy = 0;
  209. return;
  210. }
  211. have_header = 0;
  212. netdev_acquire();
  213. io_putstr("netkexec: receiving ");
  214. io_puthex(x_size);
  215. io_putstr(" bytes over UDP...\n");
  216. if (!kexec_stage_alloc(x_size)) {
  217. io_putstr("netkexec: out of memory (staging)\n");
  218. netdev_release();
  219. kexec_busy = 0;
  220. return;
  221. }
  222. if (netkexec_recv() != 0) {
  223. io_putstr("\nnetkexec: aborted (timeout or checksum)\n");
  224. kexec_stage_free();
  225. netdev_release();
  226. kexec_busy = 0;
  227. return;
  228. }
  229. io_putstr("\nnetkexec: received ");
  230. io_puthex(x_size);
  231. io_putstr(" bytes, entry ");
  232. io_puthex(x_entry);
  233. io_putstr(" -> jumping\n");
  234. kexec_jump(x_entry, x_size); /* never returns */
  235. }