/* Push a kernel + rootfs pack to a NebOS board over UDP (the board's WM * loop auto-detects the header and jumps into the new kernel). * * netkexec_send [pack] * netkexec_send --payload * * Protocol: see netkexec.c. Stop-and-wait: every datagram (except the * header) is acked with the next expected offset; lost datagrams are * retransmitted, so a slow or busy board just slows the transfer down. */ #include #include #include #include #include #include #include #include #include #include #define DATA_MAX 1400 #define MAGIC_HDR 0x324E584Bu /* "KXN2" (kernel + rootfs) */ #define MAGIC_PAY 0x504E584Bu /* "KXNP" (runtime pack replacement) */ #define MAGIC_INST 0x3249584Bu /* "KXI2" (persist kernel + rootfs) */ #define MAGIC_DATA 0x444E584Bu /* "KXND" */ #define MAGIC_ACK 0x414E584Bu /* "KXNA" */ #define ACK_CONTINUE 0 #define ACK_DONE 1 #define ACK_BADSUM 2 #define ACK_INSTALLED 3 #define ACK_INSTALL_FAIL 4 static uint32_t rd32(const unsigned char *p) { return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); } static void wr32(unsigned char *p, uint32_t v) { p[0] = (unsigned char)(v & 0xFF); p[1] = (unsigned char)((v >> 8) & 0xFF); p[2] = (unsigned char)((v >> 16) & 0xFF); p[3] = (unsigned char)((v >> 24) & 0xFF); } static int send_packet(int fd, const struct sockaddr_in *dst, const unsigned char *buf, size_t len) { return sendto(fd, buf, len, 0, (const struct sockaddr *)dst, sizeof(*dst)) == (ssize_t)len ? 0 : -1; } /* Milliseconds for the no-progress guard: a stalled transfer must time out * even while the board keeps sending stale ACKs that do not advance the * offset. */ static unsigned long long now_ms(void) { struct timeval tv; gettimeofday(&tv, NULL); return (unsigned long long)tv.tv_sec * 1000ULL + (unsigned long long)tv.tv_usec / 1000ULL; } /* Wait for an ACK ("KXNA" next_offset status). Returns next_offset, or -1 * on timeout. */ static long wait_ack(int fd, unsigned int timeout_ms, uint32_t *status) { struct timeval tv = { timeout_ms / 1000, (timeout_ms % 1000) * 1000 }; unsigned char buf[512]; uint32_t next_offset; setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); for (;;) { ssize_t n = recv(fd, buf, sizeof(buf), 0); if (n < 0) return -1; /* timeout */ if (n < 12 || rd32(buf) != MAGIC_ACK) continue; /* not ours */ next_offset = rd32(buf + 4); *status = rd32(buf + 8); return (long)next_offset; } } static unsigned char *read_file(const char *path, long *size) { FILE *f = fopen(path, "rb"); unsigned char *data; long n; if (!f) { fprintf(stderr, "netkexec_send: %s: %s\n", path, strerror(errno)); exit(1); } fseek(f, 0, SEEK_END); n = ftell(f); fseek(f, 0, SEEK_SET); if (n <= 0) { fprintf(stderr, "netkexec_send: %s: empty file\n", path); fclose(f); exit(1); } data = malloc((size_t)n); if (!data || fread(data, 1, (size_t)n, f) != (size_t)n) { fprintf(stderr, "netkexec_send: %s: read failed\n", path); fclose(f); free(data); exit(1); } fclose(f); *size = n; return data; } int main(int argc, char **argv) { const char *ip, *kernel_path, *pack_path = 0; unsigned int port, entry; unsigned char *kdata, *pdata = 0, *data, *pkt; long ksize, psize = 0, size; unsigned int offset = 0, sum = 0; unsigned long long progress_ms = 0; int fd, i, fails = 0; int payload = 0, install = 0, installed = 0, install_failed = 0; const char *install_target = 0; struct sockaddr_in dst; /* --payload: replace the command pack (/ramfs) instead of the kernel. */ if (argc > 1 && strcmp(argv[1], "--payload") == 0) { if (argc != 5) { fprintf(stderr, "usage: %s --payload \n", argv[0]); return 2; } payload = 1; ip = argv[2]; port = (unsigned int)strtoul(argv[3], NULL, 0); kernel_path = argv[4]; entry = 0; } else if (argc > 1 && strcmp(argv[1], "--install") == 0) { /* Persist kernel + NBROOT1 rootfs image to a NebOS disk. */ if (argc != 8) { fprintf(stderr, "usage: %s --install " " \n", argv[0]); return 2; } install = 1; install_target = argv[2]; ip = argv[3]; port = (unsigned int)strtoul(argv[4], NULL, 0); entry = (unsigned int)strtoul(argv[5], NULL, 16); kernel_path = argv[6]; pack_path = argv[7]; } else { if (argc != 5 && argc != 6) { fprintf(stderr, "usage: %s " " [pack]\n", argv[0]); return 2; } ip = argv[1]; port = (unsigned int)strtoul(argv[2], NULL, 0); entry = (unsigned int)strtoul(argv[3], NULL, 16); kernel_path = argv[4]; if (argc == 6) pack_path = argv[5]; } kdata = read_file(kernel_path, &ksize); if (pack_path) pdata = read_file(pack_path, &psize); size = ksize + psize; if (size <= 0 || size > 0x800000) { /* matches kexec.h KEXEC_MAX */ fprintf(stderr, "netkexec_send: bad total size %ld\n", size); return 1; } data = malloc((size_t)size); if (!data) { fprintf(stderr, "netkexec_send: out of memory\n"); return 1; } memcpy(data, kdata, (size_t)ksize); if (psize) memcpy(data + ksize, pdata, (size_t)psize); for (i = 0; i < size; i++) sum += data[i]; fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { perror("socket"); return 1; } { int on = 1; /* A board with no working unicast RX (or no DHCP lease) can * still receive broadcasts; 255.255.255.255 then pushes a * debug kernel over the normal LAN. */ setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)); } memset(&dst, 0, sizeof(dst)); dst.sin_family = AF_INET; dst.sin_port = htons((uint16_t)port); if (inet_pton(AF_INET, ip, &dst.sin_addr) != 1) { fprintf(stderr, "netkexec_send: bad address %s\n", ip); return 1; } pkt = malloc(36 + DATA_MAX); if (payload) printf("netkexec: %s:%u, %ld bytes, command pack for /ramfs\n", ip, port, size); else if (install) printf("netkexec: %s:%u, persist to %s: kernel %ld + " "rootfs %ld bytes\n", ip, port, install_target, ksize, psize); else printf("netkexec: %s:%u, kernel %ld + rootfs %ld bytes, " "entry 0x%08x\n", ip, port, ksize, psize, entry); /* Header first: the board answers with the first expected offset. */ wr32(pkt, payload ? MAGIC_PAY : (install ? MAGIC_INST : MAGIC_HDR)); wr32(pkt + 4, entry); if (payload) { wr32(pkt + 8, (uint32_t)size); wr32(pkt + 12, (uint32_t)sum); } else { wr32(pkt + 8, (uint32_t)ksize); wr32(pkt + 12, (uint32_t)psize); wr32(pkt + 16, (uint32_t)sum); if (install) { size_t tl = strlen(install_target); if (tl > 15) tl = 15; memset(pkt + 20, 0, 16); memcpy(pkt + 20, install_target, tl); } } for (i = 0; i < 20; i++) { uint32_t status; long ack; if (send_packet(fd, &dst, pkt, payload ? 16 : (install ? 36 : 20)) != 0) { perror("sendto"); return 1; } ack = wait_ack(fd, 500, &status); if (ack == 0) break; if (ack > 0) { fprintf(stderr, "netkexec: unexpected ack offset %ld\n", ack); return 1; } } if (i == 20) { fprintf(stderr, "netkexec: no ack from the board (is it running and idle?)\n"); return 1; } /* Stop-and-wait data transfer (kernel bytes, then rootfs bytes). */ progress_ms = now_ms(); while (offset < (unsigned int)size) { unsigned int chunk = (unsigned int)size - offset; uint32_t status = 0; long ack; /* An ACK that does not advance the offset (the board keeps * repeating its last offset while it receives nothing) is * not progress. Bound the time without it so a lost ARP * entry or a dead link reports a readable error instead of * retransmitting for as long as the ACKs keep coming. */ if (now_ms() - progress_ms > 10000ULL) { fprintf(stderr, "\nnetkexec: no forward progress at offset %u/%ld (board unreachable?)\n", offset, size); return 1; } if (chunk > DATA_MAX) chunk = DATA_MAX; wr32(pkt, MAGIC_DATA); wr32(pkt + 4, offset); wr32(pkt + 8, chunk); memcpy(pkt + 12, data + offset, chunk); if (send_packet(fd, &dst, pkt, 12 + chunk) != 0) { perror("sendto"); return 1; } ack = wait_ack(fd, 500, &status); if (ack < 0) { if (++fails > 40) { fprintf(stderr, "\nnetkexec: giving up at offset %u\n", offset); return 1; } continue; /* retransmit */ } fails = 0; if (status == ACK_BADSUM) { fprintf(stderr, "\nnetkexec: board rejected the checksum at offset %u\n", offset); return 1; } if ((unsigned int)ack == (unsigned int)size && status == ACK_INSTALLED) { offset = (unsigned int)ack; installed = 1; break; } if ((unsigned int)ack == (unsigned int)size && status == ACK_INSTALL_FAIL) { install_failed = 1; break; } if (status == ACK_DONE && (unsigned int)ack == (unsigned int)size) { offset = (unsigned int)ack; break; } /* Only a CONTINUE ACK that advances the offset is progress; * a duplicate or a repeated offset means retransmit the same * datagram, not that the transfer is healthy. */ if (status == ACK_CONTINUE && (unsigned int)ack > offset && (unsigned int)ack <= (unsigned int)size) { offset = (unsigned int)ack; progress_ms = now_ms(); } printf("\rnetkexec: %3u%% (%u/%ld bytes)", offset * 100 / (unsigned)size, offset, size); fflush(stdout); } if (payload) printf("\rnetkexec: 100%% (%ld bytes) - pack accepted, /ramfs replaced\n", size); else if (install) { uint32_t status = 0; long ack; int tries; if (install_failed) { fprintf(stderr, "netkexec: board reported install failure\n"); return 1; } printf("\rnetkexec: 100%% (%ld bytes) - board is writing the disk...\n", size); fflush(stdout); /* The board may still have an ACK_CONTINUE/ACK_DONE in flight * from the receive loop; drain those and wait for the final * install status. */ for (tries = 0; !installed && tries < 6; tries++) { ack = wait_ack(fd, 10000, &status); if (ack < 0) break; if (status == ACK_INSTALLED) { installed = 1; break; } if (status == ACK_INSTALL_FAIL) { fprintf(stderr, "netkexec: board reported install failure\n"); return 1; } /* ACK_CONTINUE, ACK_DONE or ACK_BADSUM: keep draining. */ } if (!installed) { fprintf(stderr, "netkexec: no install status from the board\n"); return 1; } printf("netkexec: installed to %s; board is jumping\n", install_target); } else printf("\rnetkexec: 100%% (%ld bytes) - kernel+rootfs accepted, board is jumping\n", size); close(fd); free(pkt); free(data); if (pdata) free(pdata); free(kdata); return 0; }