netkexec_send.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* Push a kernel + rootfs pack to a NebOS board over UDP (the board's WM
  2. * loop auto-detects the header and jumps into the new kernel).
  3. *
  4. * netkexec_send <board-ip> <port> <entry-hex> <kernel> [pack]
  5. * netkexec_send --payload <board-ip> <port> <pack>
  6. *
  7. * Protocol: see netkexec.c. Stop-and-wait: every datagram (except the
  8. * header) is acked with the next expected offset; lost datagrams are
  9. * retransmitted, so a slow or busy board just slows the transfer down.
  10. */
  11. #include <arpa/inet.h>
  12. #include <errno.h>
  13. #include <netinet/in.h>
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <sys/socket.h>
  19. #include <sys/time.h>
  20. #include <unistd.h>
  21. #define DATA_MAX 1400
  22. #define MAGIC_HDR 0x324E584Bu /* "KXN2" (kernel + rootfs) */
  23. #define MAGIC_PAY 0x504E584Bu /* "KXNP" (runtime pack replacement) */
  24. #define MAGIC_INST 0x3249584Bu /* "KXI2" (persist kernel + rootfs) */
  25. #define MAGIC_DATA 0x444E584Bu /* "KXND" */
  26. #define MAGIC_ACK 0x414E584Bu /* "KXNA" */
  27. #define ACK_CONTINUE 0
  28. #define ACK_DONE 1
  29. #define ACK_BADSUM 2
  30. #define ACK_INSTALLED 3
  31. #define ACK_INSTALL_FAIL 4
  32. static uint32_t rd32(const unsigned char *p)
  33. {
  34. return (uint32_t)p[0] | ((uint32_t)p[1] << 8) |
  35. ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
  36. }
  37. static void wr32(unsigned char *p, uint32_t v)
  38. {
  39. p[0] = (unsigned char)(v & 0xFF);
  40. p[1] = (unsigned char)((v >> 8) & 0xFF);
  41. p[2] = (unsigned char)((v >> 16) & 0xFF);
  42. p[3] = (unsigned char)((v >> 24) & 0xFF);
  43. }
  44. static int send_packet(int fd, const struct sockaddr_in *dst,
  45. const unsigned char *buf, size_t len)
  46. {
  47. return sendto(fd, buf, len, 0, (const struct sockaddr *)dst,
  48. sizeof(*dst)) == (ssize_t)len ? 0 : -1;
  49. }
  50. /* Milliseconds for the no-progress guard: a stalled transfer must time out
  51. * even while the board keeps sending stale ACKs that do not advance the
  52. * offset. */
  53. static unsigned long long now_ms(void)
  54. {
  55. struct timeval tv;
  56. gettimeofday(&tv, NULL);
  57. return (unsigned long long)tv.tv_sec * 1000ULL +
  58. (unsigned long long)tv.tv_usec / 1000ULL;
  59. }
  60. /* Wait for an ACK ("KXNA" next_offset status). Returns next_offset, or -1
  61. * on timeout. */
  62. static long wait_ack(int fd, unsigned int timeout_ms, uint32_t *status)
  63. {
  64. struct timeval tv = { timeout_ms / 1000, (timeout_ms % 1000) * 1000 };
  65. unsigned char buf[512];
  66. uint32_t next_offset;
  67. setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
  68. for (;;) {
  69. ssize_t n = recv(fd, buf, sizeof(buf), 0);
  70. if (n < 0)
  71. return -1; /* timeout */
  72. if (n < 12 || rd32(buf) != MAGIC_ACK)
  73. continue; /* not ours */
  74. next_offset = rd32(buf + 4);
  75. *status = rd32(buf + 8);
  76. return (long)next_offset;
  77. }
  78. }
  79. static unsigned char *read_file(const char *path, long *size)
  80. {
  81. FILE *f = fopen(path, "rb");
  82. unsigned char *data;
  83. long n;
  84. if (!f) {
  85. fprintf(stderr, "netkexec_send: %s: %s\n", path, strerror(errno));
  86. exit(1);
  87. }
  88. fseek(f, 0, SEEK_END);
  89. n = ftell(f);
  90. fseek(f, 0, SEEK_SET);
  91. if (n <= 0) {
  92. fprintf(stderr, "netkexec_send: %s: empty file\n", path);
  93. fclose(f);
  94. exit(1);
  95. }
  96. data = malloc((size_t)n);
  97. if (!data || fread(data, 1, (size_t)n, f) != (size_t)n) {
  98. fprintf(stderr, "netkexec_send: %s: read failed\n", path);
  99. fclose(f);
  100. free(data);
  101. exit(1);
  102. }
  103. fclose(f);
  104. *size = n;
  105. return data;
  106. }
  107. int main(int argc, char **argv)
  108. {
  109. const char *ip, *kernel_path, *pack_path = 0;
  110. unsigned int port, entry;
  111. unsigned char *kdata, *pdata = 0, *data, *pkt;
  112. long ksize, psize = 0, size;
  113. unsigned int offset = 0, sum = 0;
  114. unsigned long long progress_ms = 0;
  115. int fd, i, fails = 0;
  116. int payload = 0, install = 0, installed = 0, install_failed = 0;
  117. const char *install_target = 0;
  118. struct sockaddr_in dst;
  119. /* --payload: replace the command pack (/ramfs) instead of the kernel. */
  120. if (argc > 1 && strcmp(argv[1], "--payload") == 0) {
  121. if (argc != 5) {
  122. fprintf(stderr, "usage: %s --payload <board-ip> <port> <file>\n",
  123. argv[0]);
  124. return 2;
  125. }
  126. payload = 1;
  127. ip = argv[2];
  128. port = (unsigned int)strtoul(argv[3], NULL, 0);
  129. kernel_path = argv[4];
  130. entry = 0;
  131. } else if (argc > 1 && strcmp(argv[1], "--install") == 0) {
  132. /* Persist kernel + NBROOT1 rootfs image to a NebOS disk. */
  133. if (argc != 8) {
  134. fprintf(stderr,
  135. "usage: %s --install <target|auto> <board-ip> "
  136. "<port> <entry-hex> <kernel> <rootfs-image>\n",
  137. argv[0]);
  138. return 2;
  139. }
  140. install = 1;
  141. install_target = argv[2];
  142. ip = argv[3];
  143. port = (unsigned int)strtoul(argv[4], NULL, 0);
  144. entry = (unsigned int)strtoul(argv[5], NULL, 16);
  145. kernel_path = argv[6];
  146. pack_path = argv[7];
  147. } else {
  148. if (argc != 5 && argc != 6) {
  149. fprintf(stderr,
  150. "usage: %s <board-ip> <port> <entry-hex> "
  151. "<kernel> [pack]\n", argv[0]);
  152. return 2;
  153. }
  154. ip = argv[1];
  155. port = (unsigned int)strtoul(argv[2], NULL, 0);
  156. entry = (unsigned int)strtoul(argv[3], NULL, 16);
  157. kernel_path = argv[4];
  158. if (argc == 6)
  159. pack_path = argv[5];
  160. }
  161. kdata = read_file(kernel_path, &ksize);
  162. if (pack_path)
  163. pdata = read_file(pack_path, &psize);
  164. size = ksize + psize;
  165. if (size <= 0 || size > 0x800000) { /* matches kexec.h KEXEC_MAX */
  166. fprintf(stderr, "netkexec_send: bad total size %ld\n", size);
  167. return 1;
  168. }
  169. data = malloc((size_t)size);
  170. if (!data) {
  171. fprintf(stderr, "netkexec_send: out of memory\n");
  172. return 1;
  173. }
  174. memcpy(data, kdata, (size_t)ksize);
  175. if (psize)
  176. memcpy(data + ksize, pdata, (size_t)psize);
  177. for (i = 0; i < size; i++)
  178. sum += data[i];
  179. fd = socket(AF_INET, SOCK_DGRAM, 0);
  180. if (fd < 0) {
  181. perror("socket");
  182. return 1;
  183. }
  184. {
  185. int on = 1;
  186. /* A board with no working unicast RX (or no DHCP lease) can
  187. * still receive broadcasts; 255.255.255.255 then pushes a
  188. * debug kernel over the normal LAN. */
  189. setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
  190. }
  191. memset(&dst, 0, sizeof(dst));
  192. dst.sin_family = AF_INET;
  193. dst.sin_port = htons((uint16_t)port);
  194. if (inet_pton(AF_INET, ip, &dst.sin_addr) != 1) {
  195. fprintf(stderr, "netkexec_send: bad address %s\n", ip);
  196. return 1;
  197. }
  198. pkt = malloc(36 + DATA_MAX);
  199. if (payload)
  200. printf("netkexec: %s:%u, %ld bytes, command pack for /ramfs\n",
  201. ip, port, size);
  202. else if (install)
  203. printf("netkexec: %s:%u, persist to %s: kernel %ld + "
  204. "rootfs %ld bytes\n", ip, port, install_target,
  205. ksize, psize);
  206. else
  207. printf("netkexec: %s:%u, kernel %ld + rootfs %ld bytes, "
  208. "entry 0x%08x\n", ip, port, ksize, psize, entry);
  209. /* Header first: the board answers with the first expected offset. */
  210. wr32(pkt, payload ? MAGIC_PAY : (install ? MAGIC_INST : MAGIC_HDR));
  211. wr32(pkt + 4, entry);
  212. if (payload) {
  213. wr32(pkt + 8, (uint32_t)size);
  214. wr32(pkt + 12, (uint32_t)sum);
  215. } else {
  216. wr32(pkt + 8, (uint32_t)ksize);
  217. wr32(pkt + 12, (uint32_t)psize);
  218. wr32(pkt + 16, (uint32_t)sum);
  219. if (install) {
  220. size_t tl = strlen(install_target);
  221. if (tl > 15)
  222. tl = 15;
  223. memset(pkt + 20, 0, 16);
  224. memcpy(pkt + 20, install_target, tl);
  225. }
  226. }
  227. for (i = 0; i < 20; i++) {
  228. uint32_t status;
  229. long ack;
  230. if (send_packet(fd, &dst, pkt,
  231. payload ? 16 : (install ? 36 : 20)) != 0) {
  232. perror("sendto");
  233. return 1;
  234. }
  235. ack = wait_ack(fd, 500, &status);
  236. if (ack == 0)
  237. break;
  238. if (ack > 0) {
  239. fprintf(stderr, "netkexec: unexpected ack offset %ld\n", ack);
  240. return 1;
  241. }
  242. }
  243. if (i == 20) {
  244. fprintf(stderr, "netkexec: no ack from the board (is it running and idle?)\n");
  245. return 1;
  246. }
  247. /* Stop-and-wait data transfer (kernel bytes, then rootfs bytes). */
  248. progress_ms = now_ms();
  249. while (offset < (unsigned int)size) {
  250. unsigned int chunk = (unsigned int)size - offset;
  251. uint32_t status = 0;
  252. long ack;
  253. /* An ACK that does not advance the offset (the board keeps
  254. * repeating its last offset while it receives nothing) is
  255. * not progress. Bound the time without it so a lost ARP
  256. * entry or a dead link reports a readable error instead of
  257. * retransmitting for as long as the ACKs keep coming. */
  258. if (now_ms() - progress_ms > 10000ULL) {
  259. fprintf(stderr,
  260. "\nnetkexec: no forward progress at offset %u/%ld (board unreachable?)\n",
  261. offset, size);
  262. return 1;
  263. }
  264. if (chunk > DATA_MAX)
  265. chunk = DATA_MAX;
  266. wr32(pkt, MAGIC_DATA);
  267. wr32(pkt + 4, offset);
  268. wr32(pkt + 8, chunk);
  269. memcpy(pkt + 12, data + offset, chunk);
  270. if (send_packet(fd, &dst, pkt, 12 + chunk) != 0) {
  271. perror("sendto");
  272. return 1;
  273. }
  274. ack = wait_ack(fd, 500, &status);
  275. if (ack < 0) {
  276. if (++fails > 40) {
  277. fprintf(stderr, "\nnetkexec: giving up at offset %u\n", offset);
  278. return 1;
  279. }
  280. continue; /* retransmit */
  281. }
  282. fails = 0;
  283. if (status == ACK_BADSUM) {
  284. fprintf(stderr,
  285. "\nnetkexec: board rejected the checksum at offset %u\n",
  286. offset);
  287. return 1;
  288. }
  289. if ((unsigned int)ack == (unsigned int)size &&
  290. status == ACK_INSTALLED) {
  291. offset = (unsigned int)ack;
  292. installed = 1;
  293. break;
  294. }
  295. if ((unsigned int)ack == (unsigned int)size &&
  296. status == ACK_INSTALL_FAIL) {
  297. install_failed = 1;
  298. break;
  299. }
  300. if (status == ACK_DONE && (unsigned int)ack == (unsigned int)size) {
  301. offset = (unsigned int)ack;
  302. break;
  303. }
  304. /* Only a CONTINUE ACK that advances the offset is progress;
  305. * a duplicate or a repeated offset means retransmit the same
  306. * datagram, not that the transfer is healthy. */
  307. if (status == ACK_CONTINUE && (unsigned int)ack > offset &&
  308. (unsigned int)ack <= (unsigned int)size) {
  309. offset = (unsigned int)ack;
  310. progress_ms = now_ms();
  311. }
  312. printf("\rnetkexec: %3u%% (%u/%ld bytes)", offset * 100 / (unsigned)size,
  313. offset, size);
  314. fflush(stdout);
  315. }
  316. if (payload)
  317. printf("\rnetkexec: 100%% (%ld bytes) - pack accepted, /ramfs replaced\n",
  318. size);
  319. else if (install) {
  320. uint32_t status = 0;
  321. long ack;
  322. int tries;
  323. if (install_failed) {
  324. fprintf(stderr, "netkexec: board reported install failure\n");
  325. return 1;
  326. }
  327. printf("\rnetkexec: 100%% (%ld bytes) - board is writing the disk...\n",
  328. size);
  329. fflush(stdout);
  330. /* The board may still have an ACK_CONTINUE/ACK_DONE in flight
  331. * from the receive loop; drain those and wait for the final
  332. * install status. */
  333. for (tries = 0; !installed && tries < 6; tries++) {
  334. ack = wait_ack(fd, 10000, &status);
  335. if (ack < 0)
  336. break;
  337. if (status == ACK_INSTALLED) {
  338. installed = 1;
  339. break;
  340. }
  341. if (status == ACK_INSTALL_FAIL) {
  342. fprintf(stderr,
  343. "netkexec: board reported install failure\n");
  344. return 1;
  345. }
  346. /* ACK_CONTINUE, ACK_DONE or ACK_BADSUM: keep draining. */
  347. }
  348. if (!installed) {
  349. fprintf(stderr, "netkexec: no install status from the board\n");
  350. return 1;
  351. }
  352. printf("netkexec: installed to %s; board is jumping\n",
  353. install_target);
  354. } else
  355. printf("\rnetkexec: 100%% (%ld bytes) - kernel+rootfs accepted, board is jumping\n",
  356. size);
  357. close(fd);
  358. free(pkt);
  359. free(data);
  360. if (pdata)
  361. free(pdata);
  362. free(kdata);
  363. return 0;
  364. }