netkexec_send.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* Push kernel.bin to a Nebs OS board over UDP (the board's WM loop
  2. * auto-detects the header and jumps into the new kernel).
  3. *
  4. * netkexec_send <board-ip> <port> <entry-hex> <file>
  5. *
  6. * Protocol: see netkexec.c. Stop-and-wait: every datagram (except the
  7. * header) is acked with the next expected offset; lost datagrams are
  8. * retransmitted, so a slow or busy board just slows the transfer down.
  9. */
  10. #include <arpa/inet.h>
  11. #include <errno.h>
  12. #include <netinet/in.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <sys/socket.h>
  18. #include <sys/time.h>
  19. #include <unistd.h>
  20. #define DATA_MAX 1400
  21. #define MAGIC_HDR 0x554E584Bu /* "KXNU" */
  22. #define MAGIC_PAY 0x504E584Bu /* "KXNP" */
  23. #define MAGIC_DATA 0x444E584Bu /* "KXND" */
  24. #define MAGIC_ACK 0x414E584Bu /* "KXNA" */
  25. #define ACK_DONE 1
  26. static uint32_t rd32(const unsigned char *p)
  27. {
  28. return (uint32_t)p[0] | ((uint32_t)p[1] << 8) |
  29. ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
  30. }
  31. static void wr32(unsigned char *p, uint32_t v)
  32. {
  33. p[0] = (unsigned char)(v & 0xFF);
  34. p[1] = (unsigned char)((v >> 8) & 0xFF);
  35. p[2] = (unsigned char)((v >> 16) & 0xFF);
  36. p[3] = (unsigned char)((v >> 24) & 0xFF);
  37. }
  38. static int send_packet(int fd, const struct sockaddr_in *dst,
  39. const unsigned char *buf, size_t len)
  40. {
  41. return sendto(fd, buf, len, 0, (const struct sockaddr *)dst,
  42. sizeof(*dst)) == (ssize_t)len ? 0 : -1;
  43. }
  44. /* Wait for an ACK ("KXNA" next_offset status). Returns next_offset, or -1
  45. * on timeout. */
  46. static long wait_ack(int fd, unsigned int timeout_ms, uint32_t *status)
  47. {
  48. struct timeval tv = { timeout_ms / 1000, (timeout_ms % 1000) * 1000 };
  49. unsigned char buf[512];
  50. uint32_t next_offset;
  51. setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
  52. for (;;) {
  53. ssize_t n = recv(fd, buf, sizeof(buf), 0);
  54. if (n < 0)
  55. return -1; /* timeout */
  56. if (n < 12 || rd32(buf) != MAGIC_ACK)
  57. continue; /* not ours */
  58. next_offset = rd32(buf + 4);
  59. *status = rd32(buf + 8);
  60. return (long)next_offset;
  61. }
  62. }
  63. int main(int argc, char **argv)
  64. {
  65. const char *ip, *path;
  66. unsigned int port;
  67. unsigned int entry;
  68. unsigned char *data, *pkt;
  69. long size;
  70. FILE *f;
  71. unsigned int offset = 0, sum = 0;
  72. int fd, i, fails = 0;
  73. int payload = 0;
  74. struct sockaddr_in dst;
  75. /* --payload: replace the command pack (/ramfs) instead of the kernel. */
  76. if (argc > 1 && strcmp(argv[1], "--payload") == 0) {
  77. if (argc != 5) {
  78. fprintf(stderr, "usage: %s --payload <board-ip> <port> <file>\n",
  79. argv[0]);
  80. return 2;
  81. }
  82. payload = 1;
  83. ip = argv[2];
  84. port = (unsigned int)strtoul(argv[3], NULL, 0);
  85. path = argv[4];
  86. entry = 0;
  87. } else {
  88. if (argc != 5) {
  89. fprintf(stderr,
  90. "usage: %s <board-ip> <port> <entry-hex> <file>\n",
  91. argv[0]);
  92. return 2;
  93. }
  94. ip = argv[1];
  95. port = (unsigned int)strtoul(argv[2], NULL, 0);
  96. entry = (unsigned int)strtoul(argv[3], NULL, 16);
  97. path = argv[4];
  98. }
  99. f = fopen(path, "rb");
  100. if (!f) {
  101. fprintf(stderr, "netkexec_send: %s: %s\n", path, strerror(errno));
  102. return 1;
  103. }
  104. fseek(f, 0, SEEK_END);
  105. size = ftell(f);
  106. fseek(f, 0, SEEK_SET);
  107. if (size <= 0 || size > 0x400000) { /* matches kexec.h KEXEC_MAX */
  108. fprintf(stderr, "netkexec_send: bad size %ld\n", size);
  109. fclose(f);
  110. return 1;
  111. }
  112. data = malloc((size_t)size);
  113. if (!data || fread(data, 1, (size_t)size, f) != (size_t)size) {
  114. fprintf(stderr, "netkexec_send: read failed\n");
  115. fclose(f);
  116. return 1;
  117. }
  118. fclose(f);
  119. for (i = 0; i < size; i++)
  120. sum += data[i];
  121. fd = socket(AF_INET, SOCK_DGRAM, 0);
  122. if (fd < 0) {
  123. perror("socket");
  124. return 1;
  125. }
  126. memset(&dst, 0, sizeof(dst));
  127. dst.sin_family = AF_INET;
  128. dst.sin_port = htons((uint16_t)port);
  129. if (inet_pton(AF_INET, ip, &dst.sin_addr) != 1) {
  130. fprintf(stderr, "netkexec_send: bad address %s\n", ip);
  131. return 1;
  132. }
  133. pkt = malloc(12 + DATA_MAX);
  134. if (payload)
  135. printf("netkexec: %s:%u, %ld bytes, command pack for /ramfs\n",
  136. ip, port, size);
  137. else
  138. printf("netkexec: %s:%u, %ld bytes, entry 0x%08x\n",
  139. ip, port, size, entry);
  140. /* Header first: the board answers with the first expected offset. */
  141. wr32(pkt, payload ? MAGIC_PAY : MAGIC_HDR);
  142. wr32(pkt + 4, entry);
  143. wr32(pkt + 8, (uint32_t)size);
  144. wr32(pkt + 12, (uint32_t)sum);
  145. for (i = 0; i < 20; i++) {
  146. uint32_t status;
  147. long ack;
  148. if (send_packet(fd, &dst, pkt, 16) != 0) {
  149. perror("sendto");
  150. return 1;
  151. }
  152. ack = wait_ack(fd, 500, &status);
  153. if (ack == 0)
  154. break;
  155. if (ack > 0) {
  156. fprintf(stderr, "netkexec: unexpected ack offset %ld\n", ack);
  157. return 1;
  158. }
  159. }
  160. if (i == 20) {
  161. fprintf(stderr, "netkexec: no ack from the board (is it running and idle?)\n");
  162. return 1;
  163. }
  164. /* Stop-and-wait data transfer. */
  165. while (offset < (unsigned int)size) {
  166. unsigned int chunk = (unsigned int)size - offset;
  167. uint32_t status = 0;
  168. long ack;
  169. if (chunk > DATA_MAX)
  170. chunk = DATA_MAX;
  171. wr32(pkt, MAGIC_DATA);
  172. wr32(pkt + 4, offset);
  173. wr32(pkt + 8, chunk);
  174. memcpy(pkt + 12, data + offset, chunk);
  175. if (send_packet(fd, &dst, pkt, 12 + chunk) != 0) {
  176. perror("sendto");
  177. return 1;
  178. }
  179. ack = wait_ack(fd, 500, &status);
  180. if (ack < 0) {
  181. if (++fails > 40) {
  182. fprintf(stderr, "\nnetkexec: giving up at offset %u\n", offset);
  183. return 1;
  184. }
  185. continue; /* retransmit */
  186. }
  187. fails = 0;
  188. if (status == ACK_DONE && (unsigned int)ack == (unsigned int)size) {
  189. offset = (unsigned int)ack;
  190. break;
  191. }
  192. if (ack > (long)offset && (unsigned int)ack <= (unsigned int)size)
  193. offset = (unsigned int)ack;
  194. printf("\rnetkexec: %3u%% (%u/%ld bytes)", offset * 100 / (unsigned)size,
  195. offset, size);
  196. fflush(stdout);
  197. }
  198. if (payload)
  199. printf("\rnetkexec: 100%% (%ld bytes) - pack accepted, /ramfs replaced\n",
  200. size);
  201. else
  202. printf("\rnetkexec: 100%% (%ld bytes) - image accepted, board is jumping\n",
  203. size);
  204. close(fd);
  205. free(pkt);
  206. free(data);
  207. return 0;
  208. }