kexec_send.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* Host-side sender for nebos `kexec` / `cmds-dev`.
  2. *
  3. * Kernel + rootfs protocol (serial, no handshake; the kernel auto-detects it):
  4. * 4 bytes: magic "KXE2" (LE)
  5. * 4 bytes: entry address (LE)
  6. * 4 bytes: kernel_size (LE)
  7. * 4 bytes: pack_size (LE) -- 0 keeps the running rootfs
  8. * N bytes: kernel.bin
  9. * M bytes: rootfs pack (cmds_pack_gz.bin; may be omitted)
  10. * 1 byte: checksum (sum of both payloads mod 256)
  11. *
  12. * Runtime pack replacement (`--payload`) keeps the old 12-byte framing:
  13. * "KXNP", 0, pack_size, pack data, checksum.
  14. *
  15. * Usage: kexec_send <device> <baud> <entry_hex> <kernel> [pack]
  16. * kexec_send --payload <device> <baud> <file>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <fcntl.h>
  22. #include <unistd.h>
  23. #include <termios.h>
  24. #include <stdint.h>
  25. static void put32le(unsigned char *p, uint32_t v)
  26. {
  27. p[0] = (unsigned char)(v & 0xFF);
  28. p[1] = (unsigned char)((v >> 8) & 0xFF);
  29. p[2] = (unsigned char)((v >> 16) & 0xFF);
  30. p[3] = (unsigned char)((v >> 24) & 0xFF);
  31. }
  32. static unsigned char *read_file(const char *path, long *size)
  33. {
  34. FILE *fp = fopen(path, "rb");
  35. unsigned char *data;
  36. long n;
  37. if (!fp) {
  38. perror(path);
  39. exit(1);
  40. }
  41. fseek(fp, 0, SEEK_END);
  42. n = ftell(fp);
  43. fseek(fp, 0, SEEK_SET);
  44. if (n <= 0) {
  45. fprintf(stderr, "%s: empty file\n", path);
  46. fclose(fp);
  47. exit(1);
  48. }
  49. data = malloc((size_t)n);
  50. if (!data || fread(data, 1, (size_t)n, fp) != (size_t)n) {
  51. fprintf(stderr, "%s: read failed\n", path);
  52. fclose(fp);
  53. free(data);
  54. exit(1);
  55. }
  56. fclose(fp);
  57. *size = n;
  58. return data;
  59. }
  60. int main(int argc, char *argv[])
  61. {
  62. int fd;
  63. unsigned char *kdata, *pdata = 0;
  64. long ksize, psize = 0;
  65. unsigned int cksum = 0;
  66. unsigned char hdr[16];
  67. struct termios tty;
  68. uint32_t entry;
  69. const char *dev, *baud, *file, *pack_file = 0;
  70. int payload = 0;
  71. if (argc > 1 && strcmp(argv[1], "--payload") == 0) {
  72. if (argc != 5) {
  73. fprintf(stderr, "Usage: %s --payload <device> <baud> <file>\n",
  74. argv[0]);
  75. return 1;
  76. }
  77. payload = 1;
  78. dev = argv[2];
  79. baud = argv[3];
  80. file = argv[4];
  81. entry = 0;
  82. } else {
  83. if (argc != 5 && argc != 6) {
  84. fprintf(stderr, "Usage: %s <device> <baud> <entry_hex> "
  85. "<kernel> [pack]\n", argv[0]);
  86. return 1;
  87. }
  88. dev = argv[1];
  89. baud = argv[2];
  90. entry = (uint32_t)strtoul(argv[3], NULL, 16);
  91. file = argv[4];
  92. if (argc == 6)
  93. pack_file = argv[5];
  94. }
  95. kdata = read_file(file, &ksize);
  96. if (pack_file)
  97. pdata = read_file(pack_file, &psize);
  98. for (long i = 0; i < ksize; i++)
  99. cksum += kdata[i];
  100. for (long i = 0; i < psize; i++)
  101. cksum += pdata[i];
  102. cksum &= 0xFF;
  103. fd = open(dev, O_RDWR | O_NOCTTY);
  104. if (fd < 0) {
  105. perror("open");
  106. if (pdata)
  107. free(pdata);
  108. free(kdata);
  109. return 1;
  110. }
  111. memset(&tty, 0, sizeof(tty));
  112. if (tcgetattr(fd, &tty) != 0) {
  113. perror("tcgetattr");
  114. if (pdata)
  115. free(pdata);
  116. free(kdata);
  117. close(fd);
  118. return 1;
  119. }
  120. cfsetispeed(&tty, (speed_t)atoi(baud));
  121. cfsetospeed(&tty, (speed_t)atoi(baud));
  122. tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
  123. tty.c_cflag |= CLOCAL | CREAD;
  124. tty.c_cflag &= ~(PARENB | PARODD | CRTSCTS | CSTOPB);
  125. tty.c_iflag &= ~(IXON | IXOFF | IXANY | IGNBRK | BRKINT | ICRNL | INLCR);
  126. tty.c_oflag &= ~OPOST;
  127. tty.c_lflag = 0;
  128. tty.c_cc[VMIN] = 1;
  129. tty.c_cc[VTIME] = 0;
  130. if (tcsetattr(fd, TCSANOW, &tty) != 0) {
  131. perror("tcsetattr");
  132. if (pdata)
  133. free(pdata);
  134. free(kdata);
  135. close(fd);
  136. return 1;
  137. }
  138. if (payload)
  139. fprintf(stderr, "Sending %ld bytes (command pack)...\n", ksize);
  140. else
  141. fprintf(stderr, "Sending kernel %ld + rootfs %ld bytes "
  142. "(entry 0x%08x)...\n", ksize, psize, entry);
  143. if (payload) {
  144. memcpy(hdr, "KXNP", 4);
  145. put32le(hdr + 4, 0);
  146. put32le(hdr + 8, (uint32_t)ksize);
  147. if (write(fd, hdr, 12) != 12) {
  148. perror("write header");
  149. goto fail;
  150. }
  151. } else {
  152. memcpy(hdr, "KXE2", 4);
  153. put32le(hdr + 4, entry);
  154. put32le(hdr + 8, (uint32_t)ksize);
  155. put32le(hdr + 12, (uint32_t)psize);
  156. if (write(fd, hdr, 16) != 16) {
  157. perror("write header");
  158. goto fail;
  159. }
  160. }
  161. tcdrain(fd);
  162. if (write(fd, kdata, (size_t)ksize) != ksize) {
  163. perror("write kernel");
  164. goto fail;
  165. }
  166. if (pdata && write(fd, pdata, (size_t)psize) != psize) {
  167. perror("write rootfs");
  168. goto fail;
  169. }
  170. tcdrain(fd);
  171. hdr[0] = (unsigned char)cksum;
  172. if (write(fd, hdr, 1) != 1) {
  173. perror("write checksum");
  174. goto fail;
  175. }
  176. tcdrain(fd);
  177. fprintf(stderr, "Done.\n");
  178. if (pdata)
  179. free(pdata);
  180. free(kdata);
  181. close(fd);
  182. return 0;
  183. fail:
  184. if (pdata)
  185. free(pdata);
  186. free(kdata);
  187. close(fd);
  188. return 1;
  189. }