netkexec_send.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_DATA 0x444E584Bu /* "KXND" */
  23. #define MAGIC_ACK 0x414E584Bu /* "KXNA" */
  24. #define ACK_DONE 1
  25. static uint32_t rd32(const unsigned char *p)
  26. {
  27. return (uint32_t)p[0] | ((uint32_t)p[1] << 8) |
  28. ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
  29. }
  30. static void wr32(unsigned char *p, uint32_t v)
  31. {
  32. p[0] = (unsigned char)(v & 0xFF);
  33. p[1] = (unsigned char)((v >> 8) & 0xFF);
  34. p[2] = (unsigned char)((v >> 16) & 0xFF);
  35. p[3] = (unsigned char)((v >> 24) & 0xFF);
  36. }
  37. static int send_packet(int fd, const struct sockaddr_in *dst,
  38. const unsigned char *buf, size_t len)
  39. {
  40. return sendto(fd, buf, len, 0, (const struct sockaddr *)dst,
  41. sizeof(*dst)) == (ssize_t)len ? 0 : -1;
  42. }
  43. /* Wait for an ACK ("KXNA" next_offset status). Returns next_offset, or -1
  44. * on timeout. */
  45. static long wait_ack(int fd, unsigned int timeout_ms, uint32_t *status)
  46. {
  47. struct timeval tv = { timeout_ms / 1000, (timeout_ms % 1000) * 1000 };
  48. unsigned char buf[512];
  49. uint32_t next_offset;
  50. setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
  51. for (;;) {
  52. ssize_t n = recv(fd, buf, sizeof(buf), 0);
  53. if (n < 0)
  54. return -1; /* timeout */
  55. if (n < 12 || rd32(buf) != MAGIC_ACK)
  56. continue; /* not ours */
  57. next_offset = rd32(buf + 4);
  58. *status = rd32(buf + 8);
  59. return (long)next_offset;
  60. }
  61. }
  62. int main(int argc, char **argv)
  63. {
  64. const char *ip, *path;
  65. unsigned int port;
  66. unsigned int entry;
  67. unsigned char *data, *pkt;
  68. long size;
  69. FILE *f;
  70. unsigned int offset = 0, sum = 0;
  71. int fd, i, fails = 0;
  72. struct sockaddr_in dst;
  73. if (argc != 5) {
  74. fprintf(stderr, "usage: %s <board-ip> <port> <entry-hex> <file>\n", argv[0]);
  75. return 2;
  76. }
  77. ip = argv[1];
  78. port = (unsigned int)strtoul(argv[2], NULL, 0);
  79. entry = (unsigned int)strtoul(argv[3], NULL, 16);
  80. path = argv[4];
  81. f = fopen(path, "rb");
  82. if (!f) {
  83. fprintf(stderr, "netkexec_send: %s: %s\n", path, strerror(errno));
  84. return 1;
  85. }
  86. fseek(f, 0, SEEK_END);
  87. size = ftell(f);
  88. fseek(f, 0, SEEK_SET);
  89. if (size <= 0 || size > 0x3C200) {
  90. fprintf(stderr, "netkexec_send: bad size %ld\n", size);
  91. fclose(f);
  92. return 1;
  93. }
  94. data = malloc((size_t)size);
  95. if (!data || fread(data, 1, (size_t)size, f) != (size_t)size) {
  96. fprintf(stderr, "netkexec_send: read failed\n");
  97. fclose(f);
  98. return 1;
  99. }
  100. fclose(f);
  101. for (i = 0; i < size; i++)
  102. sum += data[i];
  103. fd = socket(AF_INET, SOCK_DGRAM, 0);
  104. if (fd < 0) {
  105. perror("socket");
  106. return 1;
  107. }
  108. memset(&dst, 0, sizeof(dst));
  109. dst.sin_family = AF_INET;
  110. dst.sin_port = htons((uint16_t)port);
  111. if (inet_pton(AF_INET, ip, &dst.sin_addr) != 1) {
  112. fprintf(stderr, "netkexec_send: bad address %s\n", ip);
  113. return 1;
  114. }
  115. pkt = malloc(12 + DATA_MAX);
  116. printf("netkexec: %s:%u, %ld bytes, entry 0x%08x\n", ip, port, size, entry);
  117. /* Header first: the board answers with the first expected offset. */
  118. wr32(pkt, MAGIC_HDR);
  119. wr32(pkt + 4, entry);
  120. wr32(pkt + 8, (uint32_t)size);
  121. wr32(pkt + 12, (uint32_t)sum);
  122. for (i = 0; i < 20; i++) {
  123. uint32_t status;
  124. long ack;
  125. if (send_packet(fd, &dst, pkt, 16) != 0) {
  126. perror("sendto");
  127. return 1;
  128. }
  129. ack = wait_ack(fd, 500, &status);
  130. if (ack == 0)
  131. break;
  132. if (ack > 0) {
  133. fprintf(stderr, "netkexec: unexpected ack offset %ld\n", ack);
  134. return 1;
  135. }
  136. }
  137. if (i == 20) {
  138. fprintf(stderr, "netkexec: no ack from the board (is it running and idle?)\n");
  139. return 1;
  140. }
  141. /* Stop-and-wait data transfer. */
  142. while (offset < (unsigned int)size) {
  143. unsigned int chunk = (unsigned int)size - offset;
  144. uint32_t status = 0;
  145. long ack;
  146. if (chunk > DATA_MAX)
  147. chunk = DATA_MAX;
  148. wr32(pkt, MAGIC_DATA);
  149. wr32(pkt + 4, offset);
  150. wr32(pkt + 8, chunk);
  151. memcpy(pkt + 12, data + offset, chunk);
  152. if (send_packet(fd, &dst, pkt, 12 + chunk) != 0) {
  153. perror("sendto");
  154. return 1;
  155. }
  156. ack = wait_ack(fd, 500, &status);
  157. if (ack < 0) {
  158. if (++fails > 40) {
  159. fprintf(stderr, "\nnetkexec: giving up at offset %u\n", offset);
  160. return 1;
  161. }
  162. continue; /* retransmit */
  163. }
  164. fails = 0;
  165. if (status == ACK_DONE && (unsigned int)ack == (unsigned int)size) {
  166. offset = (unsigned int)ack;
  167. break;
  168. }
  169. if (ack > (long)offset && (unsigned int)ack <= (unsigned int)size)
  170. offset = (unsigned int)ack;
  171. printf("\rnetkexec: %3u%% (%u/%ld bytes)", offset * 100 / (unsigned)size,
  172. offset, size);
  173. fflush(stdout);
  174. }
  175. printf("\rnetkexec: 100%% (%ld bytes) - image accepted, board is jumping\n", size);
  176. close(fd);
  177. free(pkt);
  178. free(data);
  179. return 0;
  180. }