netkexec_send.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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_DONE 1
  28. #define ACK_INSTALLED 3
  29. #define ACK_INSTALL_FAIL 4
  30. static uint32_t rd32(const unsigned char *p)
  31. {
  32. return (uint32_t)p[0] | ((uint32_t)p[1] << 8) |
  33. ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
  34. }
  35. static void wr32(unsigned char *p, uint32_t v)
  36. {
  37. p[0] = (unsigned char)(v & 0xFF);
  38. p[1] = (unsigned char)((v >> 8) & 0xFF);
  39. p[2] = (unsigned char)((v >> 16) & 0xFF);
  40. p[3] = (unsigned char)((v >> 24) & 0xFF);
  41. }
  42. static int send_packet(int fd, const struct sockaddr_in *dst,
  43. const unsigned char *buf, size_t len)
  44. {
  45. return sendto(fd, buf, len, 0, (const struct sockaddr *)dst,
  46. sizeof(*dst)) == (ssize_t)len ? 0 : -1;
  47. }
  48. /* Wait for an ACK ("KXNA" next_offset status). Returns next_offset, or -1
  49. * on timeout. */
  50. static long wait_ack(int fd, unsigned int timeout_ms, uint32_t *status)
  51. {
  52. struct timeval tv = { timeout_ms / 1000, (timeout_ms % 1000) * 1000 };
  53. unsigned char buf[512];
  54. uint32_t next_offset;
  55. setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
  56. for (;;) {
  57. ssize_t n = recv(fd, buf, sizeof(buf), 0);
  58. if (n < 0)
  59. return -1; /* timeout */
  60. if (n < 12 || rd32(buf) != MAGIC_ACK)
  61. continue; /* not ours */
  62. next_offset = rd32(buf + 4);
  63. *status = rd32(buf + 8);
  64. return (long)next_offset;
  65. }
  66. }
  67. static unsigned char *read_file(const char *path, long *size)
  68. {
  69. FILE *f = fopen(path, "rb");
  70. unsigned char *data;
  71. long n;
  72. if (!f) {
  73. fprintf(stderr, "netkexec_send: %s: %s\n", path, strerror(errno));
  74. exit(1);
  75. }
  76. fseek(f, 0, SEEK_END);
  77. n = ftell(f);
  78. fseek(f, 0, SEEK_SET);
  79. if (n <= 0) {
  80. fprintf(stderr, "netkexec_send: %s: empty file\n", path);
  81. fclose(f);
  82. exit(1);
  83. }
  84. data = malloc((size_t)n);
  85. if (!data || fread(data, 1, (size_t)n, f) != (size_t)n) {
  86. fprintf(stderr, "netkexec_send: %s: read failed\n", path);
  87. fclose(f);
  88. free(data);
  89. exit(1);
  90. }
  91. fclose(f);
  92. *size = n;
  93. return data;
  94. }
  95. int main(int argc, char **argv)
  96. {
  97. const char *ip, *kernel_path, *pack_path = 0;
  98. unsigned int port, entry;
  99. unsigned char *kdata, *pdata = 0, *data, *pkt;
  100. long ksize, psize = 0, size;
  101. unsigned int offset = 0, sum = 0;
  102. int fd, i, fails = 0;
  103. int payload = 0, install = 0, installed = 0, install_failed = 0;
  104. const char *install_target = 0;
  105. struct sockaddr_in dst;
  106. /* --payload: replace the command pack (/ramfs) instead of the kernel. */
  107. if (argc > 1 && strcmp(argv[1], "--payload") == 0) {
  108. if (argc != 5) {
  109. fprintf(stderr, "usage: %s --payload <board-ip> <port> <file>\n",
  110. argv[0]);
  111. return 2;
  112. }
  113. payload = 1;
  114. ip = argv[2];
  115. port = (unsigned int)strtoul(argv[3], NULL, 0);
  116. kernel_path = argv[4];
  117. entry = 0;
  118. } else if (argc > 1 && strcmp(argv[1], "--install") == 0) {
  119. /* Persist kernel + NBROOT1 rootfs image to a NebOS disk. */
  120. if (argc != 8) {
  121. fprintf(stderr,
  122. "usage: %s --install <target|auto> <board-ip> "
  123. "<port> <entry-hex> <kernel> <rootfs-image>\n",
  124. argv[0]);
  125. return 2;
  126. }
  127. install = 1;
  128. install_target = argv[2];
  129. ip = argv[3];
  130. port = (unsigned int)strtoul(argv[4], NULL, 0);
  131. entry = (unsigned int)strtoul(argv[5], NULL, 16);
  132. kernel_path = argv[6];
  133. pack_path = argv[7];
  134. } else {
  135. if (argc != 5 && argc != 6) {
  136. fprintf(stderr,
  137. "usage: %s <board-ip> <port> <entry-hex> "
  138. "<kernel> [pack]\n", argv[0]);
  139. return 2;
  140. }
  141. ip = argv[1];
  142. port = (unsigned int)strtoul(argv[2], NULL, 0);
  143. entry = (unsigned int)strtoul(argv[3], NULL, 16);
  144. kernel_path = argv[4];
  145. if (argc == 6)
  146. pack_path = argv[5];
  147. }
  148. kdata = read_file(kernel_path, &ksize);
  149. if (pack_path)
  150. pdata = read_file(pack_path, &psize);
  151. size = ksize + psize;
  152. if (size <= 0 || size > 0x400000) { /* matches kexec.h KEXEC_MAX */
  153. fprintf(stderr, "netkexec_send: bad total size %ld\n", size);
  154. return 1;
  155. }
  156. data = malloc((size_t)size);
  157. if (!data) {
  158. fprintf(stderr, "netkexec_send: out of memory\n");
  159. return 1;
  160. }
  161. memcpy(data, kdata, (size_t)ksize);
  162. if (psize)
  163. memcpy(data + ksize, pdata, (size_t)psize);
  164. for (i = 0; i < size; i++)
  165. sum += data[i];
  166. fd = socket(AF_INET, SOCK_DGRAM, 0);
  167. if (fd < 0) {
  168. perror("socket");
  169. return 1;
  170. }
  171. {
  172. int on = 1;
  173. /* A board with no working unicast RX (or no DHCP lease) can
  174. * still receive broadcasts; 255.255.255.255 then pushes a
  175. * debug kernel over the normal LAN. */
  176. setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
  177. }
  178. memset(&dst, 0, sizeof(dst));
  179. dst.sin_family = AF_INET;
  180. dst.sin_port = htons((uint16_t)port);
  181. if (inet_pton(AF_INET, ip, &dst.sin_addr) != 1) {
  182. fprintf(stderr, "netkexec_send: bad address %s\n", ip);
  183. return 1;
  184. }
  185. pkt = malloc(36 + DATA_MAX);
  186. if (payload)
  187. printf("netkexec: %s:%u, %ld bytes, command pack for /ramfs\n",
  188. ip, port, size);
  189. else if (install)
  190. printf("netkexec: %s:%u, persist to %s: kernel %ld + "
  191. "rootfs %ld bytes\n", ip, port, install_target,
  192. ksize, psize);
  193. else
  194. printf("netkexec: %s:%u, kernel %ld + rootfs %ld bytes, "
  195. "entry 0x%08x\n", ip, port, ksize, psize, entry);
  196. /* Header first: the board answers with the first expected offset. */
  197. wr32(pkt, payload ? MAGIC_PAY : (install ? MAGIC_INST : MAGIC_HDR));
  198. wr32(pkt + 4, entry);
  199. if (payload) {
  200. wr32(pkt + 8, (uint32_t)size);
  201. wr32(pkt + 12, (uint32_t)sum);
  202. } else {
  203. wr32(pkt + 8, (uint32_t)ksize);
  204. wr32(pkt + 12, (uint32_t)psize);
  205. wr32(pkt + 16, (uint32_t)sum);
  206. if (install) {
  207. size_t tl = strlen(install_target);
  208. if (tl > 15)
  209. tl = 15;
  210. memset(pkt + 20, 0, 16);
  211. memcpy(pkt + 20, install_target, tl);
  212. }
  213. }
  214. for (i = 0; i < 20; i++) {
  215. uint32_t status;
  216. long ack;
  217. if (send_packet(fd, &dst, pkt,
  218. payload ? 16 : (install ? 36 : 20)) != 0) {
  219. perror("sendto");
  220. return 1;
  221. }
  222. ack = wait_ack(fd, 500, &status);
  223. if (ack == 0)
  224. break;
  225. if (ack > 0) {
  226. fprintf(stderr, "netkexec: unexpected ack offset %ld\n", ack);
  227. return 1;
  228. }
  229. }
  230. if (i == 20) {
  231. fprintf(stderr, "netkexec: no ack from the board (is it running and idle?)\n");
  232. return 1;
  233. }
  234. /* Stop-and-wait data transfer (kernel bytes, then rootfs bytes). */
  235. while (offset < (unsigned int)size) {
  236. unsigned int chunk = (unsigned int)size - offset;
  237. uint32_t status = 0;
  238. long ack;
  239. if (chunk > DATA_MAX)
  240. chunk = DATA_MAX;
  241. wr32(pkt, MAGIC_DATA);
  242. wr32(pkt + 4, offset);
  243. wr32(pkt + 8, chunk);
  244. memcpy(pkt + 12, data + offset, chunk);
  245. if (send_packet(fd, &dst, pkt, 12 + chunk) != 0) {
  246. perror("sendto");
  247. return 1;
  248. }
  249. ack = wait_ack(fd, 500, &status);
  250. if (ack < 0) {
  251. if (++fails > 40) {
  252. fprintf(stderr, "\nnetkexec: giving up at offset %u\n", offset);
  253. return 1;
  254. }
  255. continue; /* retransmit */
  256. }
  257. fails = 0;
  258. if ((unsigned int)ack == (unsigned int)size &&
  259. status == ACK_INSTALLED) {
  260. offset = (unsigned int)ack;
  261. installed = 1;
  262. break;
  263. }
  264. if ((unsigned int)ack == (unsigned int)size &&
  265. status == ACK_INSTALL_FAIL) {
  266. install_failed = 1;
  267. break;
  268. }
  269. if (status == ACK_DONE && (unsigned int)ack == (unsigned int)size) {
  270. offset = (unsigned int)ack;
  271. break;
  272. }
  273. if (ack > (long)offset && (unsigned int)ack <= (unsigned int)size)
  274. offset = (unsigned int)ack;
  275. printf("\rnetkexec: %3u%% (%u/%ld bytes)", offset * 100 / (unsigned)size,
  276. offset, size);
  277. fflush(stdout);
  278. }
  279. if (payload)
  280. printf("\rnetkexec: 100%% (%ld bytes) - pack accepted, /ramfs replaced\n",
  281. size);
  282. else if (install) {
  283. uint32_t status = 0;
  284. long ack;
  285. int tries;
  286. if (install_failed) {
  287. fprintf(stderr, "netkexec: board reported install failure\n");
  288. return 1;
  289. }
  290. printf("\rnetkexec: 100%% (%ld bytes) - board is writing the disk...\n",
  291. size);
  292. fflush(stdout);
  293. /* The board may still have an ACK_CONTINUE/ACK_DONE in flight
  294. * from the receive loop; drain those and wait for the final
  295. * install status. */
  296. for (tries = 0; !installed && tries < 6; tries++) {
  297. ack = wait_ack(fd, 10000, &status);
  298. if (ack < 0)
  299. break;
  300. if (status == ACK_INSTALLED) {
  301. installed = 1;
  302. break;
  303. }
  304. if (status == ACK_INSTALL_FAIL) {
  305. fprintf(stderr,
  306. "netkexec: board reported install failure\n");
  307. return 1;
  308. }
  309. /* ACK_CONTINUE, ACK_DONE or ACK_BADSUM: keep draining. */
  310. }
  311. if (!installed) {
  312. fprintf(stderr, "netkexec: no install status from the board\n");
  313. return 1;
  314. }
  315. printf("netkexec: installed to %s; board is jumping\n",
  316. install_target);
  317. } else
  318. printf("\rnetkexec: 100%% (%ld bytes) - kernel+rootfs accepted, board is jumping\n",
  319. size);
  320. close(fd);
  321. free(pkt);
  322. free(data);
  323. if (pdata)
  324. free(pdata);
  325. free(kdata);
  326. return 0;
  327. }