interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / tools / ck_imac_swap.c

196 lines · 7589 bytes Raw Blame History
1/* ck_imac_swap.c -- Spark-side sender: ship a SIGNED slot object to a running iMac
2 * over UDP port 47034 (the live-swap design in docs/IMAC_GPU_BENCH.md).
3 *
4 * Usage: ck_imac_swap push <SLOT_OBJECT> [--target <ip>]
5 *
6 * SLOT_OBJECT is a signed slot object: one 4096-byte manifest unit followed by the
7 * raw kernel PE. Make one with the signing tool, never with this file:
8 * ck_slot_tool make-manifest --image IMACKERN-LAB.EFI --generation N --out M.bin
9 * ck_slot_tool sign-test M.bin --out MS.bin (TEST key: QEMU only)
10 * cat MS.bin IMACKERN-LAB.EFI > SLOT_OBJECT
11 * (Owner keys: ck_slot_tool tbs / offline signer / attach-sig.) This tool does not
12 * sign and does not check signatures; the kernel verifies with ck_slot_verify and
13 * refuses anything that does not verify.
14 *
15 * STATUS: the kernel side of this protocol is NOT BUILT in this branch. swap_x86.c's
16 * UDP receiver is a stub, so no ACK comes back and this tool times out against a real
17 * iMac. The QEMU swap test stages the same slot object on a disk instead.
18 *
19 * C standard library only. Build: make -C native/kernel ck-imac-swap */
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <stdint.h>
24#include <errno.h>
25#include <time.h>
26#include <sys/types.h>
27#include <sys/socket.h>
28#include <netinet/in.h>
29#include <arpa/inet.h>
30#include <unistd.h>
31#include <fcntl.h>
32#include <sys/select.h>
33
34#include "../x86/swap_proto.h"
35
36/* Size of the slot manifest unit (slot_format.h: CK_SLOT_UNIT). */
37#define SLOT_UNIT 4096
38#define SLOT_MAGIC "AIENSLT1"
39
40static void die(const char *msg)
41{
42 perror(msg);
43 exit(1);
44}
45
46/* Read the whole file into a malloc'd buffer. Returns the length. */
47static size_t read_file(const char *path, uint8_t **out)
48{
49 FILE *f = fopen(path, "rb");
50 if (!f) { fprintf(stderr, "cannot open %s: %s\n", path, strerror(errno)); exit(1); }
51 fseek(f, 0, SEEK_END);
52 long sz = ftell(f);
53 rewind(f);
54 if (sz <= 0) { fclose(f); *out = 0; return 0; }
55 uint8_t *buf = malloc((size_t)sz);
56 if (!buf) die("malloc");
57 if ((long)fread(buf, 1, (size_t)sz, f) != sz) die("fread");
58 fclose(f);
59 *out = buf;
60 return (size_t)sz;
61}
62
63static void cmd_push(int argc, char **argv)
64{
65 const char *obj_path = NULL, *target_ip = "255.255.255.255";
66 for (int i = 0; i < argc; i++) {
67 if (!strcmp(argv[i], "--target") && i + 1 < argc) target_ip = argv[++i];
68 else if (!obj_path) obj_path = argv[i];
69 }
70 if (!obj_path) { fprintf(stderr, "usage: ck_imac_swap push <SLOT_OBJECT> [--target IP]\n"); exit(1); }
71
72 uint8_t *obj; size_t obj_len = read_file(obj_path, &obj);
73 if (obj_len < SLOT_UNIT || memcmp(obj, SLOT_MAGIC, 8) != 0) {
74 fprintf(stderr, "%s is not a signed slot object (no %s manifest unit)\n", obj_path, SLOT_MAGIC);
75 exit(1);
76 }
77 printf("swap: read %s (%zu bytes, manifest + image)\n", obj_path, obj_len);
78 printf("swap: sending %zu bytes to %s:%u\n", obj_len, target_ip, CK_X86_SWAP_PORT);
79
80 int sock = socket(AF_INET, SOCK_DGRAM, 0);
81 if (sock < 0) die("socket");
82 int bcast = 1;
83 setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &bcast, sizeof bcast);
84
85 struct sockaddr_in dst;
86 memset(&dst, 0, sizeof dst);
87 dst.sin_family = AF_INET;
88 dst.sin_port = htons(CK_X86_SWAP_PORT);
89 dst.sin_addr.s_addr = inet_addr(target_ip);
90
91 /* Session nonce, echoed by the kernel's ACKs. */
92 uint8_t nonce[8];
93 srand((unsigned)time(NULL));
94 for (int i = 0; i < 8; i++) nonce[i] = (uint8_t)rand();
95
96 /* Stop-and-wait: one DATA packet, wait up to 500 ms for an ACK, resend on timeout. */
97 uint32_t offset = 0;
98 uint32_t total = (uint32_t)obj_len;
99 uint8_t pkt[SWAP_PUSH_MIN + SWAP_CHUNK];
100 uint8_t ack[64];
101 unsigned chunks = (total + SWAP_CHUNK - 1) / SWAP_CHUNK;
102 unsigned chunk_no = 0;
103
104 while (offset < total) {
105 uint32_t len = total - offset;
106 if (len > SWAP_CHUNK) len = SWAP_CHUNK;
107
108 memset(pkt, 0, SWAP_PUSH_MIN);
109 memcpy(pkt, SWAP_MAGIC, 4);
110 pkt[4] = SWAP_VERSION;
111 pkt[5] = SWAP_TYPE_DATA;
112 memcpy(pkt + 8, nonce, 8);
113 swap_w32(pkt + 16, offset);
114 swap_w32(pkt + 20, total);
115 swap_w16(pkt + 24, (uint16_t)len);
116 memcpy(pkt + SWAP_PUSH_MIN, obj + offset, len);
117
118 size_t pkt_len = SWAP_PUSH_MIN + len;
119 if (sendto(sock, pkt, pkt_len, 0, (struct sockaddr *)&dst, sizeof dst) < 0)
120 die("sendto");
121
122 fd_set fds; FD_ZERO(&fds); FD_SET(sock, &fds);
123 struct timeval tv = {0, 500000};
124 int r = select(sock + 1, &fds, NULL, NULL, &tv);
125 if (r > 0) {
126 socklen_t al = sizeof dst;
127 ssize_t n = recvfrom(sock, ack, sizeof ack, 0, (struct sockaddr *)&dst, &al);
128 if (n >= (ssize_t)SWAP_ACK_MIN && !memcmp(ack, SWAP_MAGIC_ACK, 4)) {
129 uint8_t status = ack[5];
130 uint32_t want = swap_r32(ack + 16);
131 if (status == SWAP_ACK_DONE) {
132 printf("swap: kernel verified, jumping (offset %u/%u)\n", offset, total);
133 free(obj); close(sock); return;
134 } else if (status == SWAP_ACK_FAIL) {
135 char text[81] = {0};
136 if (n > (ssize_t)SWAP_ACK_MIN) {
137 size_t tlen = (size_t)(n - SWAP_ACK_MIN);
138 if (tlen > 80) tlen = 80;
139 memcpy(text, ack + SWAP_ACK_MIN, tlen);
140 }
141 fprintf(stderr, "swap: REJECTED by iMac: %s\n", text);
142 free(obj); close(sock); exit(1);
143 } else {
144 offset = want; /* the ACK advances the offset */
145 }
146 }
147 }
148 /* no ACK in time: the same chunk goes out again on the next pass */
149 chunk_no++;
150 if (chunk_no % 16 == 0)
151 printf("swap: sent chunk %u/%u (%.0f%%)\n", chunk_no, chunks,
152 100.0 * chunk_no / chunks);
153 }
154
155 memset(pkt, 0, SWAP_PUSH_MIN);
156 memcpy(pkt, SWAP_MAGIC, 4);
157 pkt[4] = SWAP_VERSION;
158 pkt[5] = SWAP_TYPE_END;
159 memcpy(pkt + 8, nonce, 8);
160 swap_w32(pkt + 20, total);
161 sendto(sock, pkt, SWAP_PUSH_MIN, 0, (struct sockaddr *)&dst, sizeof dst);
162 printf("swap: all %u chunks sent\n", chunks);
163
164 for (int i = 0; i < 30; i++) {
165 fd_set fds; FD_ZERO(&fds); FD_SET(sock, &fds);
166 struct timeval tv = {1, 0};
167 if (select(sock + 1, &fds, NULL, NULL, &tv) > 0) {
168 socklen_t al = sizeof dst;
169 ssize_t n = recvfrom(sock, ack, sizeof ack, 0, (struct sockaddr *)&dst, &al);
170 if (n >= (ssize_t)SWAP_ACK_MIN && !memcmp(ack, SWAP_MAGIC_ACK, 4)) {
171 uint8_t status = ack[5];
172 if (status == SWAP_ACK_DONE) {
173 printf("swap: done, new kernel jumping\n");
174 free(obj); close(sock); return;
175 } else if (status == SWAP_ACK_FAIL) {
176 fprintf(stderr, "swap: kernel rejected the image\n");
177 free(obj); close(sock); exit(1);
178 }
179 }
180 }
181 }
182 printf("swap: no final ACK (the iMac has no swap receiver in this build, or it is unreachable)\n");
183 free(obj); close(sock);
184 exit(1);
185}
186
187int main(int argc, char **argv)
188{
189 if (argc < 2) {
190 fprintf(stderr, "usage: ck_imac_swap push <SLOT_OBJECT> [--target IP]\n");
191 return 1;
192 }
193 if (!strcmp(argv[1], "push")) { cmd_push(argc - 2, argv + 2); return 0; }
194 fprintf(stderr, "unknown command: %s\n", argv[1]);
195 return 1;
196}