netkexec.c 10.0 KB

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