netkexec_send.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* Push a kernel + rootfs pack to a Nebs OS 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_DATA 0x444E584Bu /* "KXND" */
  25. #define MAGIC_ACK 0x414E584Bu /* "KXNA" */
  26. #define ACK_DONE 1
  27. static uint32_t rd32(const unsigned char *p)
  28. {
  29. return (uint32_t)p[0] | ((uint32_t)p[1] << 8) |
  30. ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
  31. }
  32. static void wr32(unsigned char *p, uint32_t v)
  33. {
  34. p[0] = (unsigned char)(v & 0xFF);
  35. p[1] = (unsigned char)((v >> 8) & 0xFF);
  36. p[2] = (unsigned char)((v >> 16) & 0xFF);
  37. p[3] = (unsigned char)((v >> 24) & 0xFF);
  38. }
  39. static int send_packet(int fd, const struct sockaddr_in *dst,
  40. const unsigned char *buf, size_t len)
  41. {
  42. return sendto(fd, buf, len, 0, (const struct sockaddr *)dst,
  43. sizeof(*dst)) == (ssize_t)len ? 0 : -1;
  44. }
  45. /* Wait for an ACK ("KXNA" next_offset status). Returns next_offset, or -1
  46. * on timeout. */
  47. static long wait_ack(int fd, unsigned int timeout_ms, uint32_t *status)
  48. {
  49. struct timeval tv = { timeout_ms / 1000, (timeout_ms % 1000) * 1000 };
  50. unsigned char buf[512];
  51. uint32_t next_offset;
  52. setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
  53. for (;;) {
  54. ssize_t n = recv(fd, buf, sizeof(buf), 0);
  55. if (n < 0)
  56. return -1; /* timeout */
  57. if (n < 12 || rd32(buf) != MAGIC_ACK)
  58. continue; /* not ours */
  59. next_offset = rd32(buf + 4);
  60. *status = rd32(buf + 8);
  61. return (long)next_offset;
  62. }
  63. }
  64. static unsigned char *read_file(const char *path, long *size)
  65. {
  66. FILE *f = fopen(path, "rb");
  67. unsigned char *data;
  68. long n;
  69. if (!f) {
  70. fprintf(stderr, "netkexec_send: %s: %s\n", path, strerror(errno));
  71. exit(1);
  72. }
  73. fseek(f, 0, SEEK_END);
  74. n = ftell(f);
  75. fseek(f, 0, SEEK_SET);
  76. if (n <= 0) {
  77. fprintf(stderr, "netkexec_send: %s: empty file\n", path);
  78. fclose(f);
  79. exit(1);
  80. }
  81. data = malloc((size_t)n);
  82. if (!data || fread(data, 1, (size_t)n, f) != (size_t)n) {
  83. fprintf(stderr, "netkexec_send: %s: read failed\n", path);
  84. fclose(f);
  85. free(data);
  86. exit(1);
  87. }
  88. fclose(f);
  89. *size = n;
  90. return data;
  91. }
  92. int main(int argc, char **argv)
  93. {
  94. const char *ip, *kernel_path, *pack_path = 0;
  95. unsigned int port, entry;
  96. unsigned char *kdata, *pdata = 0, *data, *pkt;
  97. long ksize, psize = 0, size;
  98. unsigned int offset = 0, sum = 0;
  99. int fd, i, fails = 0;
  100. int payload = 0;
  101. struct sockaddr_in dst;
  102. /* --payload: replace the command pack (/ramfs) instead of the kernel. */
  103. if (argc > 1 && strcmp(argv[1], "--payload") == 0) {
  104. if (argc != 5) {
  105. fprintf(stderr, "usage: %s --payload <board-ip> <port> <file>\n",
  106. argv[0]);
  107. return 2;
  108. }
  109. payload = 1;
  110. ip = argv[2];
  111. port = (unsigned int)strtoul(argv[3], NULL, 0);
  112. kernel_path = argv[4];
  113. entry = 0;
  114. } else {
  115. if (argc != 5 && argc != 6) {
  116. fprintf(stderr,
  117. "usage: %s <board-ip> <port> <entry-hex> "
  118. "<kernel> [pack]\n", argv[0]);
  119. return 2;
  120. }
  121. ip = argv[1];
  122. port = (unsigned int)strtoul(argv[2], NULL, 0);
  123. entry = (unsigned int)strtoul(argv[3], NULL, 16);
  124. kernel_path = argv[4];
  125. if (argc == 6)
  126. pack_path = argv[5];
  127. }
  128. kdata = read_file(kernel_path, &ksize);
  129. if (pack_path)
  130. pdata = read_file(pack_path, &psize);
  131. size = ksize + psize;
  132. if (size <= 0 || size > 0x400000) { /* matches kexec.h KEXEC_MAX */
  133. fprintf(stderr, "netkexec_send: bad total size %ld\n", size);
  134. return 1;
  135. }
  136. data = malloc((size_t)size);
  137. if (!data) {
  138. fprintf(stderr, "netkexec_send: out of memory\n");
  139. return 1;
  140. }
  141. memcpy(data, kdata, (size_t)ksize);
  142. if (psize)
  143. memcpy(data + ksize, pdata, (size_t)psize);
  144. for (i = 0; i < size; i++)
  145. sum += data[i];
  146. fd = socket(AF_INET, SOCK_DGRAM, 0);
  147. if (fd < 0) {
  148. perror("socket");
  149. return 1;
  150. }
  151. memset(&dst, 0, sizeof(dst));
  152. dst.sin_family = AF_INET;
  153. dst.sin_port = htons((uint16_t)port);
  154. if (inet_pton(AF_INET, ip, &dst.sin_addr) != 1) {
  155. fprintf(stderr, "netkexec_send: bad address %s\n", ip);
  156. return 1;
  157. }
  158. pkt = malloc(20 + DATA_MAX);
  159. if (payload)
  160. printf("netkexec: %s:%u, %ld bytes, command pack for /ramfs\n",
  161. ip, port, size);
  162. else
  163. printf("netkexec: %s:%u, kernel %ld + rootfs %ld bytes, "
  164. "entry 0x%08x\n", ip, port, ksize, psize, entry);
  165. /* Header first: the board answers with the first expected offset. */
  166. wr32(pkt, payload ? MAGIC_PAY : MAGIC_HDR);
  167. wr32(pkt + 4, entry);
  168. if (payload) {
  169. wr32(pkt + 8, (uint32_t)size);
  170. wr32(pkt + 12, (uint32_t)sum);
  171. } else {
  172. wr32(pkt + 8, (uint32_t)ksize);
  173. wr32(pkt + 12, (uint32_t)psize);
  174. wr32(pkt + 16, (uint32_t)sum);
  175. }
  176. for (i = 0; i < 20; i++) {
  177. uint32_t status;
  178. long ack;
  179. if (send_packet(fd, &dst, pkt, payload ? 16 : 20) != 0) {
  180. perror("sendto");
  181. return 1;
  182. }
  183. ack = wait_ack(fd, 500, &status);
  184. if (ack == 0)
  185. break;
  186. if (ack > 0) {
  187. fprintf(stderr, "netkexec: unexpected ack offset %ld\n", ack);
  188. return 1;
  189. }
  190. }
  191. if (i == 20) {
  192. fprintf(stderr, "netkexec: no ack from the board (is it running and idle?)\n");
  193. return 1;
  194. }
  195. /* Stop-and-wait data transfer (kernel bytes, then rootfs bytes). */
  196. while (offset < (unsigned int)size) {
  197. unsigned int chunk = (unsigned int)size - offset;
  198. uint32_t status = 0;
  199. long ack;
  200. if (chunk > DATA_MAX)
  201. chunk = DATA_MAX;
  202. wr32(pkt, MAGIC_DATA);
  203. wr32(pkt + 4, offset);
  204. wr32(pkt + 8, chunk);
  205. memcpy(pkt + 12, data + offset, chunk);
  206. if (send_packet(fd, &dst, pkt, 12 + chunk) != 0) {
  207. perror("sendto");
  208. return 1;
  209. }
  210. ack = wait_ack(fd, 500, &status);
  211. if (ack < 0) {
  212. if (++fails > 40) {
  213. fprintf(stderr, "\nnetkexec: giving up at offset %u\n", offset);
  214. return 1;
  215. }
  216. continue; /* retransmit */
  217. }
  218. fails = 0;
  219. if (status == ACK_DONE && (unsigned int)ack == (unsigned int)size) {
  220. offset = (unsigned int)ack;
  221. break;
  222. }
  223. if (ack > (long)offset && (unsigned int)ack <= (unsigned int)size)
  224. offset = (unsigned int)ack;
  225. printf("\rnetkexec: %3u%% (%u/%ld bytes)", offset * 100 / (unsigned)size,
  226. offset, size);
  227. fflush(stdout);
  228. }
  229. if (payload)
  230. printf("\rnetkexec: 100%% (%ld bytes) - pack accepted, /ramfs replaced\n",
  231. size);
  232. else
  233. printf("\rnetkexec: 100%% (%ld bytes) - kernel+rootfs accepted, board is jumping\n",
  234. size);
  235. close(fd);
  236. free(pkt);
  237. free(data);
  238. if (pdata)
  239. free(pdata);
  240. free(kdata);
  241. return 0;
  242. }