netkexec.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #include "mm.h"
  27. #include "ramfs.h"
  28. #include "vfs.h"
  29. #define NETKEXEC_PORT 19288
  30. #define NETKEXEC_DATA_MAX 1400
  31. #define X_MAGIC_HDR 0x554E584Bu /* "KXNU" */
  32. #define X_MAGIC_PAY 0x504E584Bu /* "KXNP": a command pack for /ramfs */
  33. #define X_MAGIC_DATA 0x444E584Bu /* "KXND" */
  34. #define X_MAGIC_ACK 0x414E584Bu /* "KXNA" */
  35. #define ACK_CONTINUE 0
  36. #define ACK_DONE 1
  37. #define ACK_BADSUM 2
  38. static unsigned char rxbuf[1520]; /* one full Ethernet frame */
  39. static unsigned char replybuf[1520];
  40. static unsigned char ackbuf[12];
  41. /* Page-allocated staging for a payload transfer (freed after the swap). */
  42. static unsigned int pay_buf;
  43. static unsigned int pay_size;
  44. static int net_payload_alloc(unsigned int size)
  45. {
  46. pay_size = size;
  47. pay_buf = mm_alloc_pages((size + 0xFFFu) >> 12);
  48. return pay_buf != 0;
  49. }
  50. static void net_payload_free(void)
  51. {
  52. unsigned int i;
  53. for (i = 0; i * 0x1000 < pay_size; i++)
  54. mm_free_page(pay_buf + i * 0x1000);
  55. pay_buf = 0;
  56. }
  57. /* Pending transfer (filled by netkexec_pending). */
  58. static int have_header;
  59. static int payload_mode; /* header was KXNP: replace the ramfs instead */
  60. static unsigned int x_entry, x_size, x_csum;
  61. static unsigned char peer_mac[6], peer_ip[4];
  62. static unsigned int peer_port;
  63. static unsigned int rd32(const unsigned char *p)
  64. {
  65. return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
  66. ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
  67. }
  68. static void wr32(unsigned char *p, unsigned int v)
  69. {
  70. p[0] = (unsigned char)(v & 0xFF);
  71. p[1] = (unsigned char)((v >> 8) & 0xFF);
  72. p[2] = (unsigned char)((v >> 16) & 0xFF);
  73. p[3] = (unsigned char)((v >> 24) & 0xFF);
  74. }
  75. static int ip_eq(const unsigned char *a, const unsigned char *b)
  76. {
  77. return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3];
  78. }
  79. static int ip_is_bcast(const unsigned char *ip)
  80. {
  81. return ip[0] == 0xFF && ip[1] == 0xFF && ip[2] == 0xFF && ip[3] == 0xFF;
  82. }
  83. /* Parse `f` as a UDP datagram to NETKEXEC_PORT; fill payload + peer address.
  84. * Returns 1 on match. */
  85. static int parse_udp(const unsigned char *f, unsigned int len,
  86. const unsigned char **payload, unsigned int *plen,
  87. unsigned char p_mac[6], unsigned char p_ip[4],
  88. unsigned int *p_port)
  89. {
  90. const unsigned char *ip = f + 14;
  91. const unsigned char *udp;
  92. unsigned char my_ip[4];
  93. unsigned int i, ihl, ip_len;
  94. if (len < 14 + 20 + 8)
  95. return 0;
  96. if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
  97. return 0;
  98. if ((ip[0] >> 4) != 4)
  99. return 0;
  100. ihl = (unsigned int)(ip[0] & 0x0F) * 4;
  101. if (ihl < 20 || ip[9] != IPPROTO_UDP)
  102. return 0;
  103. ip_len = ((unsigned int)ip[2] << 8) | ip[3];
  104. if (ip_len < ihl + 8 || 14 + ip_len > len)
  105. return 0;
  106. net_get_ip(my_ip);
  107. if (!ip_eq(ip + 16, my_ip) && !ip_is_bcast(ip + 16))
  108. return 0;
  109. udp = ip + ihl;
  110. if ((((unsigned int)udp[2] << 8) | udp[3]) != NETKEXEC_PORT)
  111. return 0;
  112. for (i = 0; i < 6; i++)
  113. p_mac[i] = f[6 + i];
  114. for (i = 0; i < 4; i++)
  115. p_ip[i] = ip[12 + i];
  116. *p_port = ((unsigned int)udp[0] << 8) | udp[1];
  117. *payload = udp + 8;
  118. *plen = ip_len - ihl - 8;
  119. return *plen > 0;
  120. }
  121. int netkexec_pending(void)
  122. {
  123. const struct netdev *nd;
  124. int len;
  125. if (kexec_busy || netdev_in_use())
  126. return 0;
  127. if (have_header)
  128. return 1;
  129. nd = netdev_active();
  130. if (!nd)
  131. return 0;
  132. /* One frame per call; anything else is dropped (this also keeps the RX
  133. * ring drained while the system idles at a prompt). */
  134. len = nd->rx(rxbuf, sizeof(rxbuf));
  135. if (len < 0)
  136. return 0;
  137. if (netkexec_capture_frame(rxbuf, (unsigned int)len))
  138. return 1;
  139. /* Not a kexec datagram: stay reachable by answering ARP and ICMP echo
  140. * requests for our IP (so the board can be pinged, and a host can
  141. * resolve its MAC, without running a command). Until an address is
  142. * configured there is nothing to answer for. */
  143. if (net_is_configured()) {
  144. int rlen = net_build_reply(rxbuf, (unsigned int)len,
  145. replybuf, sizeof(replybuf));
  146. if (rlen > 0)
  147. nd->tx(replybuf, (unsigned int)rlen);
  148. }
  149. return 0;
  150. }
  151. /* Validate one received frame as a KXNU/KXNP header and stash the transfer.
  152. * Returns 1 when it was captured. The WebDAV server calls this while it
  153. * owns the receiver, so `make net-dev` hands the NIC back without waiting
  154. * for the host's header retransmits. */
  155. int netkexec_capture_frame(const unsigned char *frame, unsigned int len)
  156. {
  157. const unsigned char *payload;
  158. unsigned int plen, port, magic, i;
  159. unsigned char p_mac[6], p_ip[4];
  160. if (have_header)
  161. return 0;
  162. if (!parse_udp(frame, len, &payload, &plen, p_mac, p_ip, &port))
  163. return 0;
  164. if (plen < 16)
  165. return 0;
  166. magic = rd32(payload);
  167. if (magic != X_MAGIC_HDR && magic != X_MAGIC_PAY)
  168. return 0;
  169. x_entry = rd32(payload + 4);
  170. x_size = rd32(payload + 8);
  171. x_csum = rd32(payload + 12);
  172. if (x_size == 0 || x_size > KEXEC_MAX)
  173. return 0;
  174. payload_mode = (magic == X_MAGIC_PAY);
  175. for (i = 0; i < 6; i++)
  176. peer_mac[i] = p_mac[i];
  177. for (i = 0; i < 4; i++)
  178. peer_ip[i] = p_ip[i];
  179. peer_port = port;
  180. have_header = 1;
  181. return 1;
  182. }
  183. static void send_ack(unsigned int next_offset, unsigned int status)
  184. {
  185. wr32(ackbuf, X_MAGIC_ACK);
  186. wr32(ackbuf + 4, next_offset);
  187. wr32(ackbuf + 8, status);
  188. net_udp_send(peer_mac, peer_ip, NETKEXEC_PORT, peer_port,
  189. ackbuf, sizeof(ackbuf));
  190. }
  191. static int netkexec_recv(unsigned int stage_base)
  192. {
  193. volatile unsigned char *stage = (volatile unsigned char *)stage_base;
  194. unsigned int offset = 0, retries = 0, sum = 0;
  195. unsigned int deadline = get_ticks_ms() + 3000;
  196. kexec_total = x_size;
  197. kexec_progress = 0;
  198. send_ack(0, ACK_CONTINUE); /* header ok, send from offset 0 */
  199. while (offset < x_size) {
  200. const unsigned char *payload;
  201. unsigned int plen, port, doff, dlen;
  202. int len, i;
  203. if ((int)(get_ticks_ms() - deadline) > 0) {
  204. if (++retries > 10)
  205. return -1; /* host gave up */
  206. send_ack(offset, ACK_CONTINUE);
  207. deadline = get_ticks_ms() + 1000;
  208. continue;
  209. }
  210. len = netdev_active()->rx(rxbuf, sizeof(rxbuf));
  211. if (len < 0)
  212. continue;
  213. if (!parse_udp(rxbuf, (unsigned int)len, &payload, &plen,
  214. peer_mac, peer_ip, &port))
  215. continue;
  216. if (plen < 12 || rd32(payload) != X_MAGIC_DATA)
  217. continue;
  218. doff = rd32(payload + 4);
  219. dlen = rd32(payload + 8);
  220. if (dlen > NETKEXEC_DATA_MAX || plen < 12 + dlen)
  221. continue;
  222. if (doff == offset) {
  223. for (i = 0; i < (int)dlen; i++) {
  224. stage[offset + i] = payload[12 + i];
  225. sum += payload[12 + i];
  226. }
  227. offset += dlen;
  228. kexec_progress = offset;
  229. retries = 0;
  230. deadline = get_ticks_ms() + 1000;
  231. if ((offset & 0x3FFF) == 0)
  232. io_putchar('.');
  233. }
  234. send_ack(offset, ACK_CONTINUE); /* also acks duplicates */
  235. }
  236. if (sum != x_csum) {
  237. send_ack(offset, ACK_BADSUM);
  238. return -1;
  239. }
  240. send_ack(x_size, ACK_DONE);
  241. return 0;
  242. }
  243. void program_netkexec_main(void)
  244. {
  245. if (!have_header) {
  246. io_putstr("netkexec: no transfer pending\n");
  247. kexec_busy = 0;
  248. return;
  249. }
  250. have_header = 0;
  251. netdev_acquire();
  252. io_putstr("netkexec: receiving ");
  253. io_puthex(x_size);
  254. io_putstr(" bytes over UDP...\n");
  255. if (payload_mode) {
  256. unsigned int buf = 0;
  257. if (!net_payload_alloc(x_size)) {
  258. io_putstr("netkexec: out of memory (staging)\n");
  259. netdev_release();
  260. kexec_busy = 0;
  261. return;
  262. }
  263. buf = pay_buf;
  264. if (netkexec_recv(buf) != 0) {
  265. io_putstr("\nnetkexec: aborted (timeout or checksum)\n");
  266. net_payload_free();
  267. netdev_release();
  268. kexec_busy = 0;
  269. return;
  270. }
  271. io_putstr("\nnetkexec: received ");
  272. io_puthex(x_size);
  273. io_putstr(" bytes, ");
  274. if (ramfs_replace((const void *)buf, x_size) != 0) {
  275. io_putstr("not a valid pack (unchanged)\n");
  276. net_payload_free(); /* the ramfs kept the old pack */
  277. } else {
  278. vfs_refresh_ramfs();
  279. io_putstr("pack installed (");
  280. io_puthex(ramfs_count());
  281. io_putstr(" files)\n");
  282. pay_buf = 0; /* ownership moved to the ramfs */
  283. pay_size = 0;
  284. }
  285. netdev_release();
  286. kexec_busy = 0;
  287. return;
  288. }
  289. if (!kexec_stage_alloc(x_size)) {
  290. io_putstr("netkexec: out of memory (staging)\n");
  291. netdev_release();
  292. kexec_busy = 0;
  293. return;
  294. }
  295. if (netkexec_recv(kexec_stage) != 0) {
  296. io_putstr("\nnetkexec: aborted (timeout or checksum)\n");
  297. kexec_stage_free();
  298. netdev_release();
  299. kexec_busy = 0;
  300. return;
  301. }
  302. io_putstr("\nnetkexec: received ");
  303. io_puthex(x_size);
  304. io_putstr(" bytes, entry ");
  305. io_puthex(x_entry);
  306. io_putstr(" -> jumping\n");
  307. kexec_jump(x_entry, x_size); /* never returns */
  308. }