kexec_send.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. if (argc != 5) {
  37. fprintf(stderr, "Usage: %s <device> <baud> <entry_hex> <file>\n", argv[0]);
  38. return 1;
  39. }
  40. entry = (uint32_t)strtoul(argv[3], NULL, 16);
  41. fp = fopen(argv[4], "rb");
  42. if (!fp) {
  43. perror("fopen");
  44. return 1;
  45. }
  46. fseek(fp, 0, SEEK_END);
  47. size = ftell(fp);
  48. fseek(fp, 0, SEEK_SET);
  49. data = malloc((size_t)size);
  50. if (!data) {
  51. perror("malloc");
  52. fclose(fp);
  53. return 1;
  54. }
  55. if (fread(data, 1, (size_t)size, fp) != (size_t)size) {
  56. perror("fread");
  57. fclose(fp);
  58. free(data);
  59. return 1;
  60. }
  61. fclose(fp);
  62. for (long i = 0; i < size; i++)
  63. cksum += data[i];
  64. cksum &= 0xFF;
  65. fd = open(argv[1], O_RDWR | O_NOCTTY);
  66. if (fd < 0) {
  67. perror("open");
  68. free(data);
  69. return 1;
  70. }
  71. memset(&tty, 0, sizeof(tty));
  72. if (tcgetattr(fd, &tty) != 0) {
  73. perror("tcgetattr");
  74. free(data);
  75. close(fd);
  76. return 1;
  77. }
  78. cfsetispeed(&tty, (speed_t)atoi(argv[2]));
  79. cfsetospeed(&tty, (speed_t)atoi(argv[2]));
  80. tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
  81. tty.c_cflag |= CLOCAL | CREAD;
  82. tty.c_cflag &= ~(PARENB | PARODD | CRTSCTS | CSTOPB);
  83. tty.c_iflag &= ~(IXON | IXOFF | IXANY | IGNBRK | BRKINT | ICRNL | INLCR);
  84. tty.c_oflag &= ~OPOST;
  85. tty.c_lflag = 0;
  86. tty.c_cc[VMIN] = 1;
  87. tty.c_cc[VTIME] = 0;
  88. if (tcsetattr(fd, TCSANOW, &tty) != 0) {
  89. perror("tcsetattr");
  90. free(data);
  91. close(fd);
  92. return 1;
  93. }
  94. fprintf(stderr, "Sending %ld bytes (entry 0x%08x)...\n", size, entry);
  95. memcpy(hdr, "KXEC", 4);
  96. put32le(hdr + 4, entry);
  97. put32le(hdr + 8, (uint32_t)size);
  98. if (write(fd, hdr, 12) != 12) {
  99. perror("write header");
  100. free(data);
  101. close(fd);
  102. return 1;
  103. }
  104. tcdrain(fd);
  105. if (write(fd, data, (size_t)size) != size) {
  106. perror("write data");
  107. free(data);
  108. close(fd);
  109. return 1;
  110. }
  111. tcdrain(fd);
  112. hdr[0] = (unsigned char)cksum;
  113. if (write(fd, hdr, 1) != 1) {
  114. perror("write checksum");
  115. free(data);
  116. close(fd);
  117. return 1;
  118. }
  119. tcdrain(fd);
  120. fprintf(stderr, "Done.\n");
  121. free(data);
  122. close(fd);
  123. return 0;
  124. }