netkexec.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 "KXN2" entry kernel_size pack_size csum (20 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. * status 3 = persisted, 4 = persist failed
  13. *
  14. * The runtime pack replacement keeps the old 16-byte "KXNP" header
  15. * (magic, entry, size, csum) with the raw NBPAK1 pack as data.
  16. *
  17. * The persistent install uses "KXI2": the KXN2 header plus a 16-byte target
  18. * name, and a full NBROOT1 rootfs image (header + gzip pack) instead of the
  19. * raw gzip payload. After the receive the board writes P2/P1, acks
  20. * ACK_INSTALLED/ACK_INSTALL_FAIL and then jumps with the gzip part.
  21. *
  22. * The board auto-detects the header from the WM loop (netkexec_pending());
  23. * program_netkexec_main() then runs stop-and-wait: every datagram is acked
  24. * with the next expected offset, so a lost or duplicated datagram costs one
  25. * retransmit. Datagrams addressed to the broadcast IP are accepted too, so
  26. * the host can push without knowing whether the board is up yet.
  27. */
  28. #include "install.h"
  29. #include "io.h"
  30. #include "kexec.h"
  31. #include "net.h"
  32. #include "netcore.h"
  33. #include "netdev.h"
  34. #include "tcp.h"
  35. #include "process.h"
  36. #include "serial.h"
  37. #include "timer.h"
  38. #include "netkexec.h"
  39. #include "mm.h"
  40. #include "ramfs.h"
  41. #include "rootfs.h"
  42. #include "vfs.h"
  43. #define NETKEXEC_PORT 19288
  44. #define NETKEXEC_DATA_MAX 1400
  45. #define X_MAGIC_HDR 0x324E584Bu /* "KXN2": kernel + rootfs */
  46. #define X_MAGIC_PAY 0x504E584Bu /* "KXNP": a command pack for /ramfs */
  47. #define X_MAGIC_INST 0x3249584Bu /* "KXI2": persist kernel + rootfs */
  48. #define X_MAGIC_DATA 0x444E584Bu /* "KXND" */
  49. #define X_MAGIC_ACK 0x414E584Bu /* "KXNA" */
  50. #define ACK_CONTINUE 0
  51. #define ACK_DONE 1
  52. #define ACK_BADSUM 2
  53. #define ACK_INSTALLED 3
  54. #define ACK_INSTALL_FAIL 4
  55. static unsigned char rxbuf[1520]; /* one full Ethernet frame */
  56. static unsigned char replybuf[1520];
  57. static unsigned char ackbuf[12];
  58. /* Page-allocated staging for a payload transfer (freed after the swap). */
  59. static unsigned int pay_buf;
  60. static unsigned int pay_size;
  61. static int net_payload_alloc(unsigned int size)
  62. {
  63. pay_size = size;
  64. pay_buf = mm_alloc_pages((size + 0xFFFu) >> 12);
  65. return pay_buf != 0;
  66. }
  67. static void net_payload_free(void)
  68. {
  69. unsigned int i;
  70. for (i = 0; i * 0x1000 < pay_size; i++)
  71. mm_free_page(pay_buf + i * 0x1000);
  72. pay_buf = 0;
  73. }
  74. /* Pending transfer (filled by netkexec_pending). */
  75. static int have_header;
  76. static int payload_mode; /* header was KXNP: replace the ramfs instead */
  77. static int install_mode; /* header was KXI2: persist to a block device */
  78. static char x_install_target[16];
  79. static unsigned int x_entry, x_size, x_csum;
  80. static unsigned int x_kernel_size, x_pack_size;
  81. static unsigned char peer_mac[6], peer_ip[4];
  82. static unsigned int peer_port;
  83. static unsigned int rd32(const unsigned char *p)
  84. {
  85. return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
  86. ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
  87. }
  88. static void wr32(unsigned char *p, unsigned int v)
  89. {
  90. p[0] = (unsigned char)(v & 0xFF);
  91. p[1] = (unsigned char)((v >> 8) & 0xFF);
  92. p[2] = (unsigned char)((v >> 16) & 0xFF);
  93. p[3] = (unsigned char)((v >> 24) & 0xFF);
  94. }
  95. /* Parse `f` as a UDP datagram to NETKEXEC_PORT; fill payload + peer address.
  96. * The management link may have a different address from the active test NIC
  97. * (ASIX 192.168.50.2 while e1000e is 192.168.178.240), so accept any
  98. * destination IP here: the KXN2/KXND magic and checksum are the gate.
  99. * Returns 1 on match. */
  100. static int parse_udp(const unsigned char *f, unsigned int len,
  101. const unsigned char **payload, unsigned int *plen,
  102. unsigned char p_mac[6], unsigned char p_ip[4],
  103. unsigned int *p_port)
  104. {
  105. const unsigned char *ip = f + 14;
  106. const unsigned char *udp;
  107. unsigned int i, ihl, ip_len;
  108. if (len < 14 + 20 + 8)
  109. return 0;
  110. if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
  111. return 0;
  112. if ((ip[0] >> 4) != 4)
  113. return 0;
  114. ihl = (unsigned int)(ip[0] & 0x0F) * 4;
  115. if (ihl < 20 || ip[9] != IPPROTO_UDP)
  116. return 0;
  117. ip_len = ((unsigned int)ip[2] << 8) | ip[3];
  118. if (ip_len < ihl + 8 || 14 + ip_len > len)
  119. return 0;
  120. udp = ip + ihl;
  121. if ((((unsigned int)udp[2] << 8) | udp[3]) != NETKEXEC_PORT)
  122. return 0;
  123. for (i = 0; i < 6; i++)
  124. p_mac[i] = f[6 + i];
  125. for (i = 0; i < 4; i++)
  126. p_ip[i] = ip[12 + i];
  127. *p_port = ((unsigned int)udp[0] << 8) | udp[1];
  128. *payload = udp + 8;
  129. *plen = ip_len - ihl - 8;
  130. return *plen > 0;
  131. }
  132. static int netkexec_poll_nd(const struct netdev *nd, int capture)
  133. {
  134. unsigned int n;
  135. for (n = 0; n < 8; n++) {
  136. int len = nd->rx(rxbuf, sizeof(rxbuf));
  137. if (len < 0)
  138. break;
  139. if (capture && netkexec_capture_frame(rxbuf,
  140. (unsigned int)len))
  141. return 1;
  142. /* Not a kexec datagram: stay reachable by answering ARP and
  143. * ICMP echo requests for our IP (so the board can be pinged,
  144. * and a host can resolve its MAC, without running a
  145. * command). Until an address is configured there is nothing
  146. * to answer for. */
  147. if (net_is_configured()) {
  148. int rlen = net_build_reply(rxbuf, (unsigned int)len,
  149. replybuf,
  150. sizeof(replybuf));
  151. if (rlen > 0)
  152. nd->tx(replybuf, (unsigned int)rlen);
  153. }
  154. }
  155. return 0;
  156. }
  157. int netkexec_pending(void)
  158. {
  159. const struct netdev *mgmt, *act;
  160. if (kexec_busy || netdev_in_use() || tcp_client_count() != 0 ||
  161. net_stack_busy())
  162. return 0;
  163. if (have_header)
  164. return 1;
  165. mgmt = netdev_mgmt();
  166. act = netdev_active();
  167. if (!mgmt && !act)
  168. return 0;
  169. /* KXN2/KXNP headers and the netmon console use the management
  170. * link (ASIX when present), so switching the active driver to
  171. * e1000e for testing does not cut the upload/debug path. The
  172. * active NIC still gets its ARP/ICMP responder polled. */
  173. if (mgmt && netkexec_poll_nd(mgmt, 1))
  174. return 1;
  175. if (act && act != mgmt)
  176. netkexec_poll_nd(act, 0);
  177. return 0;
  178. }
  179. /* Validate one received frame as a KXN2/KXNP header and stash the transfer.
  180. * Returns 1 when it was captured. The WebDAV server calls this while it
  181. * owns the receiver, so `make net-dev` hands the NIC back without waiting
  182. * for the host's header retransmits. */
  183. int netkexec_capture_frame(const unsigned char *frame, unsigned int len)
  184. {
  185. const unsigned char *payload;
  186. unsigned int plen, port, magic, i;
  187. unsigned char p_mac[6], p_ip[4];
  188. if (have_header)
  189. return 0;
  190. if (!parse_udp(frame, len, &payload, &plen, p_mac, p_ip, &port))
  191. return 0;
  192. if (plen < 16)
  193. return 0;
  194. magic = rd32(payload);
  195. if (magic != X_MAGIC_HDR && magic != X_MAGIC_PAY &&
  196. magic != X_MAGIC_INST)
  197. return 0;
  198. x_entry = rd32(payload + 4);
  199. payload_mode = 0;
  200. install_mode = 0;
  201. if (magic == X_MAGIC_PAY) {
  202. x_size = rd32(payload + 8);
  203. x_csum = rd32(payload + 12);
  204. x_kernel_size = 0;
  205. x_pack_size = 0;
  206. payload_mode = 1;
  207. } else {
  208. if (plen < (magic == X_MAGIC_INST ? 36u : 20u))
  209. return 0;
  210. x_kernel_size = rd32(payload + 8);
  211. x_pack_size = rd32(payload + 12);
  212. x_csum = rd32(payload + 16);
  213. x_size = x_kernel_size + x_pack_size;
  214. if (x_kernel_size == 0 || x_pack_size > KEXEC_MAX ||
  215. x_size < x_kernel_size || x_size > KEXEC_MAX)
  216. return 0;
  217. if (magic == X_MAGIC_INST) {
  218. unsigned int t;
  219. for (t = 0; t < sizeof(x_install_target) - 1; t++) {
  220. char c = (char)payload[20 + t];
  221. if (c == '\0')
  222. break;
  223. x_install_target[t] = c;
  224. }
  225. x_install_target[t] = '\0';
  226. if (!x_install_target[0])
  227. return 0;
  228. install_mode = 1;
  229. }
  230. }
  231. if (x_size == 0 || x_size > KEXEC_MAX)
  232. return 0;
  233. for (i = 0; i < 6; i++)
  234. peer_mac[i] = p_mac[i];
  235. for (i = 0; i < 4; i++)
  236. peer_ip[i] = p_ip[i];
  237. peer_port = port;
  238. have_header = 1;
  239. return 1;
  240. }
  241. static void send_ack(unsigned int next_offset, unsigned int status)
  242. {
  243. const struct netdev *nd = netdev_mgmt();
  244. unsigned char src[4];
  245. if (!nd)
  246. return;
  247. net_get_ip(src);
  248. wr32(ackbuf, X_MAGIC_ACK);
  249. wr32(ackbuf + 4, next_offset);
  250. wr32(ackbuf + 8, status);
  251. net_udp_send_from_dev(nd, src, peer_mac, peer_ip, NETKEXEC_PORT,
  252. peer_port, ackbuf, sizeof(ackbuf));
  253. }
  254. static int netkexec_recv(unsigned int stage_base)
  255. {
  256. volatile unsigned char *stage = (volatile unsigned char *)stage_base;
  257. const struct netdev *nd = netdev_mgmt();
  258. unsigned int offset = 0, retries = 0, sum = 0;
  259. unsigned int deadline = get_ticks_ms() + 3000;
  260. unsigned int hard_deadline = get_ticks_ms() + 60000;
  261. if (!nd)
  262. return -1;
  263. kexec_total = x_size;
  264. kexec_progress = 0;
  265. send_ack(0, ACK_CONTINUE); /* header ok, send from offset 0 */
  266. while (offset < x_size) {
  267. const unsigned char *payload;
  268. unsigned int plen, port, doff, dlen;
  269. int len, i;
  270. /* A host that trickles one datagram at a time can keep the
  271. * per-packet deadline alive forever, holding the NIC (and the
  272. * background ARP/ICMP responder) hostage. Cap the whole
  273. * transfer as well. */
  274. if ((int)(get_ticks_ms() - hard_deadline) > 0)
  275. return -1;
  276. if ((int)(get_ticks_ms() - deadline) > 0) {
  277. if (++retries > 10)
  278. return -1; /* host gave up */
  279. send_ack(offset, ACK_CONTINUE);
  280. deadline = get_ticks_ms() + 1000;
  281. continue;
  282. }
  283. len = nd->rx(rxbuf, sizeof(rxbuf));
  284. if (len < 0) {
  285. /* Yield so the WM and terminal keep running while we
  286. * wait for the next datagram; this used to busy-spin
  287. * with the BKL held and froze the UI. */
  288. process_sleep_ms(1);
  289. continue;
  290. }
  291. if (!parse_udp(rxbuf, (unsigned int)len, &payload, &plen,
  292. peer_mac, peer_ip, &port)) {
  293. process_sleep_ms(1);
  294. continue;
  295. }
  296. if (plen < 12 || rd32(payload) != X_MAGIC_DATA) {
  297. process_sleep_ms(1);
  298. continue;
  299. }
  300. doff = rd32(payload + 4);
  301. dlen = rd32(payload + 8);
  302. if (dlen > NETKEXEC_DATA_MAX || plen < 12 + dlen) {
  303. process_sleep_ms(1);
  304. continue;
  305. }
  306. if (doff == offset) {
  307. for (i = 0; i < (int)dlen; i++) {
  308. stage[offset + i] = payload[12 + i];
  309. sum += payload[12 + i];
  310. }
  311. offset += dlen;
  312. kexec_progress = offset;
  313. retries = 0;
  314. deadline = get_ticks_ms() + 1000;
  315. if ((offset & 0x3FFF) == 0)
  316. io_putchar('.');
  317. }
  318. if (offset < x_size)
  319. send_ack(offset, ACK_CONTINUE); /* also acks duplicates */
  320. }
  321. if (sum != x_csum) {
  322. send_ack(offset, ACK_BADSUM);
  323. return -1;
  324. }
  325. send_ack(x_size, ACK_DONE);
  326. return 0;
  327. }
  328. void program_netkexec_main(void)
  329. {
  330. if (!have_header) {
  331. io_putstr("netkexec: no transfer pending\n");
  332. kexec_busy = 0;
  333. return;
  334. }
  335. if (tcp_client_count() != 0) {
  336. io_putstr("netkexec: NIC busy (close network clients first)\n");
  337. kexec_busy = 0;
  338. return;
  339. }
  340. have_header = 0;
  341. netdev_acquire();
  342. io_putstr("netkexec: receiving ");
  343. io_puthex(x_size);
  344. io_putstr(" bytes over UDP...\n");
  345. if (payload_mode) {
  346. unsigned int buf = 0;
  347. if (!net_payload_alloc(x_size)) {
  348. io_putstr("netkexec: out of memory (staging)\n");
  349. netdev_release();
  350. kexec_busy = 0;
  351. return;
  352. }
  353. buf = pay_buf;
  354. if (netkexec_recv(buf) != 0) {
  355. io_putstr("\nnetkexec: aborted (timeout or checksum)\n");
  356. net_payload_free();
  357. netdev_release();
  358. kexec_busy = 0;
  359. return;
  360. }
  361. io_putstr("\nnetkexec: received ");
  362. io_puthex(x_size);
  363. io_putstr(" bytes, ");
  364. if (ramfs_replace((const void *)buf, x_size) != 0) {
  365. io_putstr("not a valid pack (unchanged)\n");
  366. net_payload_free(); /* the ramfs kept the old pack */
  367. } else {
  368. vfs_refresh_ramfs();
  369. io_putstr("pack installed (");
  370. io_puthex(ramfs_count());
  371. io_putstr(" files)\n");
  372. pay_buf = 0; /* ownership moved to the ramfs */
  373. pay_size = 0;
  374. }
  375. netdev_release();
  376. kexec_busy = 0;
  377. return;
  378. }
  379. if (!kexec_stage_alloc(x_size)) {
  380. io_putstr("netkexec: out of memory (staging)\n");
  381. netdev_release();
  382. kexec_busy = 0;
  383. return;
  384. }
  385. if (netkexec_recv(kexec_stage) != 0) {
  386. io_putstr("\nnetkexec: aborted (timeout or checksum)\n");
  387. kexec_stage_free();
  388. netdev_release();
  389. kexec_busy = 0;
  390. return;
  391. }
  392. io_putstr("\nnetkexec: received kernel ");
  393. io_puthex(x_kernel_size);
  394. io_putstr(" bytes, rootfs ");
  395. io_puthex(x_pack_size);
  396. io_putstr(" bytes, entry ");
  397. io_puthex(x_entry);
  398. io_putchar('\n');
  399. if (install_mode) {
  400. const struct rootfs_hdr *rh =
  401. (const struct rootfs_hdr *)(kexec_stage + x_kernel_size);
  402. int r;
  403. if (x_pack_size < ROOTFS_HDR_SIZE ||
  404. rh->pack_bytes > x_pack_size - ROOTFS_HDR_SIZE) {
  405. io_putstr("netinstall: bad rootfs image\n");
  406. send_ack(x_size, ACK_INSTALL_FAIL);
  407. kexec_stage_free();
  408. netdev_release();
  409. kexec_busy = 0;
  410. return;
  411. }
  412. r = install_ram_image(x_install_target,
  413. (const void *)kexec_stage, x_kernel_size,
  414. rh, x_pack_size);
  415. if (r != 0) {
  416. io_putstr("netinstall: failed; disk not updated\n");
  417. send_ack(x_size, ACK_INSTALL_FAIL);
  418. kexec_stage_free();
  419. netdev_release();
  420. kexec_busy = 0;
  421. return;
  422. }
  423. send_ack(x_size, ACK_INSTALLED);
  424. io_putstr("netinstall: persisted to ");
  425. io_putstr(x_install_target);
  426. io_putstr(" -> jumping\n");
  427. kexec_jump(x_entry, x_kernel_size,
  428. (unsigned int)rh + ROOTFS_HDR_SIZE,
  429. rh->pack_bytes);
  430. }
  431. io_putstr(" -> jumping\n");
  432. kexec_jump(x_entry, x_kernel_size, kexec_stage + x_kernel_size,
  433. x_pack_size); /* never returns */
  434. }