| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- /* 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 <board-ip> <port> <entry-hex> <kernel> [pack]
- * netkexec_send --payload <board-ip> <port> <pack>
- *
- * 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 <arpa/inet.h>
- #include <errno.h>
- #include <netinet/in.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/time.h>
- #include <unistd.h>
- #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_DONE 1
- #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;
- }
- /* 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;
- 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 <board-ip> <port> <file>\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 <target|auto> <board-ip> "
- "<port> <entry-hex> <kernel> <rootfs-image>\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 <board-ip> <port> <entry-hex> "
- "<kernel> [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 > 0x400000) { /* 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). */
- while (offset < (unsigned int)size) {
- unsigned int chunk = (unsigned int)size - offset;
- uint32_t status = 0;
- long ack;
- 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 ((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;
- }
- if (ack > (long)offset && (unsigned int)ack <= (unsigned int)size)
- offset = (unsigned int)ack;
- 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;
- }
|