payload.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* payload: replace the command pack (ramfs) over serial at runtime.
  2. *
  3. * Wire format (same framing as kexec.c, the entry field is unused):
  4. * 4 bytes: magic "KXNP" (LE)
  5. * 4 bytes: entry (LE, 0)
  6. * 4 bytes: size (LE) -- pack byte count
  7. * N bytes: pack data
  8. * 1 byte: checksum (sum of data bytes mod 256)
  9. *
  10. * The pack is received into page-allocated memory and validated before the
  11. * swap (ramfs_replace), so a truncated or corrupt transfer leaves the running
  12. * commands untouched; the previous received copy is freed on success. The WM
  13. * idle loop detects the magic like it does for kexec, so `make cmds-dev` on
  14. * the host alone triggers it. */
  15. #include "payload.h"
  16. #include "io.h"
  17. #include "kexec.h"
  18. #include "mm.h"
  19. #include "ramfs.h"
  20. #include "serial.h"
  21. #include "vfs.h"
  22. #define PAYLOAD_MAGIC 0x504E584B /* "KXNP" little-endian */
  23. #define PAYLOAD_MAX 0x800000 /* raw pack; RAMFS_MAX_DECOMPRESSED, KEXEC_MAX */
  24. int payload_pending(void)
  25. {
  26. unsigned char b, x, n, p;
  27. /* Scan for the "KXNP" magic, draining bytes that cannot begin it (same
  28. * self-healing as kexec_pending). */
  29. for (;;) {
  30. if (!serial_ring_peek(0, &b))
  31. return 0;
  32. if (b != 'K') {
  33. (void)serial_ring_getc();
  34. continue;
  35. }
  36. if (serial_ring_peek(1, &x) && serial_ring_peek(2, &n) &&
  37. serial_ring_peek(3, &p)) {
  38. if (x == 'X' && n == 'N' && p == 'P')
  39. return 1;
  40. /* A kernel image ("KXE2", formerly "KXEC")
  41. * belongs to kexec_pending(): leave it untouched
  42. * (the WM checks this detector first, so consuming
  43. * the 'K' here would eat the reload stream). */
  44. if (x == 'X' && n == 'E' && (p == '2' || p == 'C'))
  45. return 0;
  46. (void)serial_ring_getc();
  47. continue;
  48. }
  49. return 0;
  50. }
  51. }
  52. static unsigned char header[12];
  53. static void free_span(unsigned int base, unsigned int size)
  54. {
  55. unsigned int i, pages = (size + 0xFFFu) >> 12;
  56. for (i = 0; i < pages; i++)
  57. mm_free_page(base + i * 0x1000);
  58. }
  59. static int payload_recv(unsigned int *size, unsigned int *buf)
  60. {
  61. unsigned int i, n, magic = 0;
  62. unsigned char c, sum = 0, chk;
  63. volatile unsigned char *dst;
  64. for (i = 0; i < sizeof(header); i++) {
  65. if (!serial_ring_wait(5000, &header[i]))
  66. goto fail_header;
  67. if (i < 4)
  68. magic |= (unsigned int)header[i] << (8 * i);
  69. }
  70. if (magic != PAYLOAD_MAGIC)
  71. goto fail_magic;
  72. *size = (unsigned int)header[8] | ((unsigned int)header[9] << 8) |
  73. ((unsigned int)header[10] << 16) | ((unsigned int)header[11] << 24);
  74. if (*size == 0 || *size > PAYLOAD_MAX)
  75. goto fail_size;
  76. *buf = mm_alloc_pages((*size + 0xFFFu) >> 12);
  77. if (!*buf)
  78. goto fail_mem;
  79. dst = (volatile unsigned char *)*buf;
  80. for (n = 0; n < *size; n++) {
  81. if (!serial_ring_wait(5000, &c)) {
  82. free_span(*buf, *size);
  83. goto fail_data;
  84. }
  85. dst[n] = c;
  86. sum += c;
  87. }
  88. if (!serial_ring_wait(5000, &chk)) {
  89. free_span(*buf, *size);
  90. goto fail_sum;
  91. }
  92. if (chk != sum) {
  93. free_span(*buf, *size);
  94. goto fail_csum;
  95. }
  96. return 0;
  97. fail_header:
  98. io_putstr("payload: timeout (header)\n");
  99. return -1;
  100. fail_magic:
  101. io_putstr("payload: bad magic\n");
  102. return -1;
  103. fail_size:
  104. io_putstr("payload: bad size\n");
  105. return -1;
  106. fail_mem:
  107. io_putstr("payload: out of memory\n");
  108. return -1;
  109. fail_data:
  110. io_putstr("payload: timeout (data)\n");
  111. return -1;
  112. fail_sum:
  113. io_putstr("payload: timeout (checksum)\n");
  114. return -1;
  115. fail_csum:
  116. io_putstr("payload: checksum mismatch\n");
  117. return -1;
  118. }
  119. void program_payload_main(void)
  120. {
  121. unsigned int size = 0, buf = 0;
  122. io_putstr("payload: waiting for command pack over serial...\n");
  123. if (payload_recv(&size, &buf) != 0) {
  124. io_putstr("payload: aborted (pack unchanged)\n");
  125. kexec_busy = 0;
  126. return;
  127. }
  128. io_putstr("payload: received ");
  129. io_puthex(size);
  130. io_putstr(" bytes, ");
  131. if (ramfs_replace((const void *)buf, size) != 0) {
  132. io_putstr("not a valid pack (unchanged)\n");
  133. free_span(buf, size);
  134. } else {
  135. vfs_refresh_ramfs();
  136. io_putstr("pack installed (");
  137. io_puthex(ramfs_count());
  138. io_putstr(" files)\n");
  139. }
  140. kexec_busy = 0;
  141. }