netkexec.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. const unsigned char *payload;
  125. unsigned int plen, port;
  126. int len;
  127. if (kexec_busy || netdev_in_use())
  128. return 0;
  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 (!parse_udp(rxbuf, (unsigned int)len, &payload, &plen,
  138. peer_mac, peer_ip, &port)) {
  139. /* Not a kexec datagram: stay reachable by answering ARP and ICMP
  140. * echo requests for our IP (so the board can be pinged, and a
  141. * host can resolve its MAC, without running a command). Until
  142. * an address is configured there is nothing to answer for. */
  143. int rlen;
  144. if (!net_is_configured())
  145. return 0;
  146. rlen = net_build_reply(rxbuf, (unsigned int)len,
  147. replybuf, sizeof(replybuf));
  148. if (rlen > 0)
  149. nd->tx(replybuf, (unsigned int)rlen);
  150. return 0;
  151. }
  152. if (plen < 16)
  153. return 0;
  154. {
  155. /* KXNU: kernel image (jump after the checksum); KXNP: a command
  156. * pack for /ramfs (swap it in and stay running). */
  157. unsigned int magic = rd32(payload);
  158. if (magic != X_MAGIC_HDR && magic != X_MAGIC_PAY)
  159. return 0;
  160. payload_mode = (magic == X_MAGIC_PAY);
  161. }
  162. x_entry = rd32(payload + 4);
  163. x_size = rd32(payload + 8);
  164. x_csum = rd32(payload + 12);
  165. if (x_size == 0 || x_size > KEXEC_MAX)
  166. return 0;
  167. peer_port = port;
  168. have_header = 1;
  169. return 1;
  170. }
  171. static void send_ack(unsigned int next_offset, unsigned int status)
  172. {
  173. wr32(ackbuf, X_MAGIC_ACK);
  174. wr32(ackbuf + 4, next_offset);
  175. wr32(ackbuf + 8, status);
  176. net_udp_send(peer_mac, peer_ip, NETKEXEC_PORT, peer_port,
  177. ackbuf, sizeof(ackbuf));
  178. }
  179. static int netkexec_recv(unsigned int stage_base)
  180. {
  181. volatile unsigned char *stage = (volatile unsigned char *)stage_base;
  182. unsigned int offset = 0, retries = 0, sum = 0;
  183. unsigned int deadline = get_ticks_ms() + 3000;
  184. kexec_total = x_size;
  185. kexec_progress = 0;
  186. send_ack(0, ACK_CONTINUE); /* header ok, send from offset 0 */
  187. while (offset < x_size) {
  188. const unsigned char *payload;
  189. unsigned int plen, port, doff, dlen;
  190. int len, i;
  191. if ((int)(get_ticks_ms() - deadline) > 0) {
  192. if (++retries > 10)
  193. return -1; /* host gave up */
  194. send_ack(offset, ACK_CONTINUE);
  195. deadline = get_ticks_ms() + 1000;
  196. continue;
  197. }
  198. len = netdev_active()->rx(rxbuf, sizeof(rxbuf));
  199. if (len < 0)
  200. continue;
  201. if (!parse_udp(rxbuf, (unsigned int)len, &payload, &plen,
  202. peer_mac, peer_ip, &port))
  203. continue;
  204. if (plen < 12 || rd32(payload) != X_MAGIC_DATA)
  205. continue;
  206. doff = rd32(payload + 4);
  207. dlen = rd32(payload + 8);
  208. if (dlen > NETKEXEC_DATA_MAX || plen < 12 + dlen)
  209. continue;
  210. if (doff == offset) {
  211. for (i = 0; i < (int)dlen; i++) {
  212. stage[offset + i] = payload[12 + i];
  213. sum += payload[12 + i];
  214. }
  215. offset += dlen;
  216. kexec_progress = offset;
  217. retries = 0;
  218. deadline = get_ticks_ms() + 1000;
  219. if ((offset & 0x3FFF) == 0)
  220. io_putchar('.');
  221. }
  222. send_ack(offset, ACK_CONTINUE); /* also acks duplicates */
  223. }
  224. if (sum != x_csum) {
  225. send_ack(offset, ACK_BADSUM);
  226. return -1;
  227. }
  228. send_ack(x_size, ACK_DONE);
  229. return 0;
  230. }
  231. void program_netkexec_main(void)
  232. {
  233. if (!have_header) {
  234. io_putstr("netkexec: no transfer pending\n");
  235. kexec_busy = 0;
  236. return;
  237. }
  238. have_header = 0;
  239. netdev_acquire();
  240. io_putstr("netkexec: receiving ");
  241. io_puthex(x_size);
  242. io_putstr(" bytes over UDP...\n");
  243. if (payload_mode) {
  244. unsigned int buf = 0;
  245. if (!net_payload_alloc(x_size)) {
  246. io_putstr("netkexec: out of memory (staging)\n");
  247. netdev_release();
  248. kexec_busy = 0;
  249. return;
  250. }
  251. buf = pay_buf;
  252. if (netkexec_recv(buf) != 0) {
  253. io_putstr("\nnetkexec: aborted (timeout or checksum)\n");
  254. net_payload_free();
  255. netdev_release();
  256. kexec_busy = 0;
  257. return;
  258. }
  259. io_putstr("\nnetkexec: received ");
  260. io_puthex(x_size);
  261. io_putstr(" bytes, ");
  262. if (ramfs_replace((const void *)buf, x_size) != 0) {
  263. io_putstr("not a valid pack (unchanged)\n");
  264. net_payload_free(); /* the ramfs kept the old pack */
  265. } else {
  266. vfs_refresh_ramfs();
  267. io_putstr("pack installed (");
  268. io_puthex(ramfs_count());
  269. io_putstr(" files)\n");
  270. pay_buf = 0; /* ownership moved to the ramfs */
  271. pay_size = 0;
  272. }
  273. netdev_release();
  274. kexec_busy = 0;
  275. return;
  276. }
  277. if (!kexec_stage_alloc(x_size)) {
  278. io_putstr("netkexec: out of memory (staging)\n");
  279. netdev_release();
  280. kexec_busy = 0;
  281. return;
  282. }
  283. if (netkexec_recv(kexec_stage) != 0) {
  284. io_putstr("\nnetkexec: aborted (timeout or checksum)\n");
  285. kexec_stage_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, entry ");
  293. io_puthex(x_entry);
  294. io_putstr(" -> jumping\n");
  295. kexec_jump(x_entry, x_size); /* never returns */
  296. }