netkexec.c 11 KB

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