aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / boot / netboot_proto.h
| 1 | /* netboot_proto.h -- the AIENOS lab netboot wire format (NETLDR.EFI <-> host |
| 2 | * tools/ck_imac_netboot.c). Freestanding C, no heap, no I/O; the same object |
| 3 | * is linked into the UEFI loader and the host server. |
| 4 | * |
| 5 | * Purpose: put a new test kernel on the iMac without touching the USB stick. |
| 6 | * NETLDR.EFI stays on the stick as EFI/BOOT/BOOTX64.EFI, asks the LAN for a |
| 7 | * kernel, verifies it (ck_slot_verify: Ed25519 manifest signature by a key |
| 8 | * baked in at build time, SHA-512 of the image) and only then starts it. |
| 9 | * Nothing in these messages is trusted: the transfer only moves bytes, the |
| 10 | * signature decides. Anyone on the LAN can read the transfer (the kernels are |
| 11 | * open source) or disturb it (a denial of service, never a takeover). |
| 12 | * |
| 13 | * Transport: UDP over IPv4, Ethernet broadcast both ways, so neither side |
| 14 | * needs an address, ARP or DHCP. The loader sends from 0.0.0.0:NB_PORT_LOADER |
| 15 | * to 255.255.255.255:NB_PORT_SERVER; the server answers to the subnet |
| 16 | * broadcast address, port NB_PORT_LOADER. TTL 1: this network only. |
| 17 | * |
| 18 | * Message (all integers big-endian), NB_HDR + 8 fixed body bytes minimum: |
| 19 | * 0 magic "ANB1" 4 type u8 5 reserved[3] = 0 8 nonce[16] |
| 20 | * HELLO 1 L->S 24 mac[6] 30 reserved[2] (32 bytes) |
| 21 | * OFFER 2 S->L 24 total u32 28 chunk u16 = NB_CHUNK 30 rsvd u16 (32) |
| 22 | * WAIT 3 S->L 24 seconds u16 (1..NB_WAIT_MAX) 26 reserved[6] (32) |
| 23 | * GET 4 L->S 24 offset u32 28 count u16 (1..NB_WINDOW) 30 rsvd (32) |
| 24 | * DATA 5 S->L 24 offset u32 28 len u16 30 rsvd u16 32 data[len] (32 + len) |
| 25 | * DONE 6 L->S 24 status u8 (0 verified, booting; 1 refused) 25 rsvd[7] |
| 26 | * 32 text[n], n <= NB_TEXT_MAX printable ASCII (32 + n) |
| 27 | * The nonce is chosen by the loader for each attempt and echoed by the server; |
| 28 | * it pairs replies with requests and is not a secret. |
| 29 | * The object served is a signed slot manifest (CK_SLOT_UNIT bytes, |
| 30 | * slot_format.h) followed by the image: total = CK_SLOT_UNIT + image_len. |
| 31 | * DATA offsets are multiples of NB_CHUNK; every chunk but the last is full. */ |
| 32 | #ifndef AIENOS_NETBOOT_PROTO_H |
| 33 | #define AIENOS_NETBOOT_PROTO_H |
| 34 | #include <stddef.h> |
| 35 | #include <stdint.h> |
| 36 | |
| 37 | #define NB_MAGIC "ANB1" |
| 38 | #define NB_PORT_SERVER 47032u |
| 39 | #define NB_PORT_LOADER 47033u |
| 40 | #define NB_HDR 24u |
| 41 | #define NB_MIN 32u |
| 42 | #define NB_CHUNK 1024u |
| 43 | #define NB_WINDOW 16u |
| 44 | #define NB_WAIT_MAX 60u |
| 45 | #define NB_TEXT_MAX 96u |
| 46 | #define NB_OBJECT_MIN (4096u + 1u) |
| 47 | #define NB_OBJECT_MAX (64u << 20) |
| 48 | #define NB_MSG_MAX (NB_MIN + NB_CHUNK) |
| 49 | |
| 50 | enum nb_type { NB_HELLO = 1, NB_OFFER, NB_WAIT, NB_GET, NB_DATA, NB_DONE }; |
| 51 | |
| 52 | struct nb_msg { |
| 53 | uint8_t type; |
| 54 | uint8_t nonce[16]; |
| 55 | uint8_t mac[6]; /* HELLO */ |
| 56 | uint32_t total; /* OFFER */ |
| 57 | uint16_t seconds; /* WAIT */ |
| 58 | uint32_t offset; /* GET, DATA */ |
| 59 | uint16_t count; /* GET */ |
| 60 | uint16_t len; /* DATA bytes, DONE text bytes */ |
| 61 | uint8_t status; /* DONE */ |
| 62 | const uint8_t *data; /* DATA payload or DONE text: a view into the parsed buffer, or the bytes to send */ |
| 63 | }; |
| 64 | |
| 65 | /* Encode m into out. Returns the message length, or 0 if m breaks a rule |
| 66 | * above or cap is too small. */ |
| 67 | size_t nb_build(const struct nb_msg *m, uint8_t *out, size_t cap); |
| 68 | /* Decode and check one message. Returns 0 and fills *m, or -1 for anything |
| 69 | * malformed (wrong magic, nonzero reserved bytes, unknown type, wrong length, |
| 70 | * out-of-range field, unprintable text). */ |
| 71 | int nb_parse(const uint8_t *b, size_t len, struct nb_msg *m); |
| 72 | /* Number of NB_CHUNK pieces in an object of total bytes. */ |
| 73 | static inline uint32_t nb_chunks(uint32_t total) { return (total + NB_CHUNK - 1u) / NB_CHUNK; } |
| 74 | |
| 75 | /* What the loader hands the kernel it starts, as that image's LoadOptions |
| 76 | * (EFI_LOADED_IMAGE_PROTOCOL, UEFI 2.10 section 9.1): the processor clock |
| 77 | * (TSC, counting since power-on) when the loader began and when it started |
| 78 | * the kernel, and, for a network kernel, where the verified object (4096-byte |
| 79 | * signed manifest + image) lies, in the loader's EfiLoaderData pages, which |
| 80 | * the kernel's frame allocator never hands out. The kernel installs that |
| 81 | * object on its disk's boot section (kernel/dev/fat_boot.h). */ |
| 82 | #define NB_PASS_MAGIC "AIENNLP1" |
| 83 | struct nb_pass { |
| 84 | uint8_t magic[8]; |
| 85 | uint64_t loader_tsc; /* the loader's first instruction */ |
| 86 | uint64_t start_tsc; /* just before StartImage */ |
| 87 | uint64_t object; /* physical address of the verified object; 0: none (a local kernel) */ |
| 88 | uint64_t object_len; /* manifest + image bytes */ |
| 89 | }; |
| 90 | |
| 91 | /* Length of a DONE text: bytes before the first NUL, at most NB_TEXT_MAX. |
| 92 | * Reads no byte past text[NB_TEXT_MAX - 1], so text need not be terminated |
| 93 | * within the cap. */ |
| 94 | static inline size_t nb_text_len(const char *text) |
| 95 | { |
| 96 | size_t n = 0; |
| 97 | while (n < NB_TEXT_MAX && text[n]) |
| 98 | n++; |
| 99 | return n; |
| 100 | } |
| 101 | |
| 102 | #endif /* AIENOS_NETBOOT_PROTO_H */ |