netkexec.c 11 KB

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