kexec_send.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Host-side sender for nebos `kexec` / bootstrap.
  2. *
  3. * Protocol: send immediately (no handshake; the kernel auto-detects the magic):
  4. * 4 bytes: magic "KXEC" (LE)
  5. * 4 bytes: entry address (LE)
  6. * 4 bytes: size (LE)
  7. * N bytes: kernel.bin data
  8. * 1 byte: checksum (sum of data bytes mod 256)
  9. *
  10. * Usage: kexec_send <device> <baud> <entry_hex> <file>
  11. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <fcntl.h>
  16. #include <unistd.h>
  17. #include <termios.h>
  18. #include <stdint.h>
  19. static void put32le(unsigned char *p, uint32_t v)
  20. {
  21. p[0] = (unsigned char)(v & 0xFF);
  22. p[1] = (unsigned char)((v >> 8) & 0xFF);
  23. p[2] = (unsigned char)((v >> 16) & 0xFF);
  24. p[3] = (unsigned char)((v >> 24) & 0xFF);
  25. }
  26. int main(int argc, char *argv[])
  27. {
  28. int fd;
  29. FILE *fp;
  30. unsigned char *data;
  31. long size;
  32. unsigned int cksum = 0;
  33. unsigned char hdr[12];
  34. struct termios tty;
  35. uint32_t entry;
  36. const char *dev, *baud, *file;
  37. int payload = 0;
  38. if (argc > 1 && strcmp(argv[1], "--payload") == 0) {
  39. if (argc != 5) {
  40. fprintf(stderr, "Usage: %s --payload <device> <baud> <file>\n",
  41. argv[0]);
  42. return 1;
  43. }
  44. payload = 1;
  45. dev = argv[2];
  46. baud = argv[3];
  47. file = argv[4];
  48. entry = 0;
  49. } else {
  50. if (argc != 5) {
  51. fprintf(stderr, "Usage: %s <device> <baud> <entry_hex> <file>\n",
  52. argv[0]);
  53. return 1;
  54. }
  55. dev = argv[1];
  56. baud = argv[2];
  57. entry = (uint32_t)strtoul(argv[3], NULL, 16);
  58. file = argv[4];
  59. }
  60. fp = fopen(file, "rb");
  61. if (!fp) {
  62. perror("fopen");
  63. return 1;
  64. }
  65. fseek(fp, 0, SEEK_END);
  66. size = ftell(fp);
  67. fseek(fp, 0, SEEK_SET);
  68. data = malloc((size_t)size);
  69. if (!data) {
  70. perror("malloc");
  71. fclose(fp);
  72. return 1;
  73. }
  74. if (fread(data, 1, (size_t)size, fp) != (size_t)size) {
  75. perror("fread");
  76. fclose(fp);
  77. free(data);
  78. return 1;
  79. }
  80. fclose(fp);
  81. for (long i = 0; i < size; i++)
  82. cksum += data[i];
  83. cksum &= 0xFF;
  84. fd = open(dev, O_RDWR | O_NOCTTY);
  85. if (fd < 0) {
  86. perror("open");
  87. free(data);
  88. return 1;
  89. }
  90. memset(&tty, 0, sizeof(tty));
  91. if (tcgetattr(fd, &tty) != 0) {
  92. perror("tcgetattr");
  93. free(data);
  94. close(fd);
  95. return 1;
  96. }
  97. cfsetispeed(&tty, (speed_t)atoi(baud));
  98. cfsetospeed(&tty, (speed_t)atoi(baud));
  99. tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
  100. tty.c_cflag |= CLOCAL | CREAD;
  101. tty.c_cflag &= ~(PARENB | PARODD | CRTSCTS | CSTOPB);
  102. tty.c_iflag &= ~(IXON | IXOFF | IXANY | IGNBRK | BRKINT | ICRNL | INLCR);
  103. tty.c_oflag &= ~OPOST;
  104. tty.c_lflag = 0;
  105. tty.c_cc[VMIN] = 1;
  106. tty.c_cc[VTIME] = 0;
  107. if (tcsetattr(fd, TCSANOW, &tty) != 0) {
  108. perror("tcsetattr");
  109. free(data);
  110. close(fd);
  111. return 1;
  112. }
  113. if (payload)
  114. fprintf(stderr, "Sending %ld bytes (command pack)...\n", size);
  115. else
  116. fprintf(stderr, "Sending %ld bytes (entry 0x%08x)...\n", size, entry);
  117. memcpy(hdr, payload ? "KXNP" : "KXEC", 4);
  118. put32le(hdr + 4, entry);
  119. put32le(hdr + 8, (uint32_t)size);
  120. if (write(fd, hdr, 12) != 12) {
  121. perror("write header");
  122. free(data);
  123. close(fd);
  124. return 1;
  125. }
  126. tcdrain(fd);
  127. if (write(fd, data, (size_t)size) != size) {
  128. perror("write data");
  129. free(data);
  130. close(fd);
  131. return 1;
  132. }
  133. tcdrain(fd);
  134. hdr[0] = (unsigned char)cksum;
  135. if (write(fd, hdr, 1) != 1) {
  136. perror("write checksum");
  137. free(data);
  138. close(fd);
  139. return 1;
  140. }
  141. tcdrain(fd);
  142. fprintf(stderr, "Done.\n");
  143. free(data);
  144. close(fd);
  145. return 0;
  146. }