Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/boot/netboot_proto.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / boot / netboot_proto.c` 161 lines ยท 4448 bytes Raw Blame History 1 /* netboot_proto.c -- encode and check AIENOS lab netboot messages 2 * (netboot_proto.h is the wire spec). Freestanding: no heap, no I/O. */ 3 #include "netboot_proto.h" 4 5 static void put16(uint8_t *p, uint16_t v) 6 { 7 p[0] = (uint8_t)(v >> 8); 8 p[1] = (uint8_t)v; 9 } 10 11 static void put32(uint8_t *p, uint32_t v) 12 { 13 p[0] = (uint8_t)(v >> 24); 14 p[1] = (uint8_t)(v >> 16); 15 p[2] = (uint8_t)(v >> 8); 16 p[3] = (uint8_t)v; 17 } 18 19 static uint16_t get16(const uint8_t *p) { return (uint16_t)(p[0] << 8 | p[1]); } 20 21 static uint32_t get32(const uint8_t *p) 22 { 23 return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]; 24 } 25 26 static int zero(const uint8_t *p, size_t n) 27 { 28 uint8_t a = 0; 29 for (size_t i = 0; i < n; i++) 30 a |= p[i]; 31 return a == 0; 32 } 33 34 static int printable(const uint8_t *p, size_t n) 35 { 36 for (size_t i = 0; i < n; i++) 37 if (p[i] < 0x20 || p[i] > 0x7e) 38 return 0; 39 return 1; 40 } 41 42 /* The field rules shared by build and parse. */ 43 static int fields_ok(const struct nb_msg *m) 44 { 45 switch (m->type) { 46 case NB_HELLO: 47 return 1; 48 case NB_OFFER: 49 return m->total >= NB_OBJECT_MIN && m->total <= NB_OBJECT_MAX; 50 case NB_WAIT: 51 return m->seconds >= 1 && m->seconds <= NB_WAIT_MAX; 52 case NB_GET: 53 return m->count >= 1 && m->count <= NB_WINDOW && m->offset % NB_CHUNK == 0 && m->offset < NB_OBJECT_MAX; 54 case NB_DATA: 55 return m->len >= 1 && m->len <= NB_CHUNK && m->offset % NB_CHUNK == 0 && m->offset < NB_OBJECT_MAX && 56 (m->data != 0); 57 case NB_DONE: 58 return m->status <= 1 && m->len <= NB_TEXT_MAX && (m->len == 0 || (m->data && printable(m->data, m->len))); 59 default: 60 return 0; 61 } 62 } 63 64 size_t nb_build(const struct nb_msg *m, uint8_t *out, size_t cap) 65 { 66 size_t n = NB_MIN + (m && (m->type == NB_DATA || m->type == NB_DONE) ? m->len : 0); 67 if (!m || !out || !fields_ok(m) || cap < n) 68 return 0; 69 for (size_t i = 0; i < NB_MIN; i++) 70 out[i] = 0; 71 for (size_t i = 0; i < 4; i++) 72 out[i] = (uint8_t)NB_MAGIC[i]; 73 out[4] = m->type; 74 for (size_t i = 0; i < 16; i++) 75 out[8 + i] = m->nonce[i]; 76 switch (m->type) { 77 case NB_HELLO: 78 for (size_t i = 0; i < 6; i++) 79 out[24 + i] = m->mac[i]; 80 break; 81 case NB_OFFER: 82 put32(out + 24, m->total); 83 put16(out + 28, NB_CHUNK); 84 break; 85 case NB_WAIT: 86 put16(out + 24, m->seconds); 87 break; 88 case NB_GET: 89 put32(out + 24, m->offset); 90 put16(out + 28, m->count); 91 break; 92 case NB_DATA: 93 put32(out + 24, m->offset); 94 put16(out + 28, m->len); 95 break; 96 case NB_DONE: 97 out[24] = m->status; 98 break; 99 } 100 for (size_t i = 0; i < n - NB_MIN; i++) 101 out[NB_MIN + i] = m->data[i]; 102 return n; 103 } 104 105 int nb_parse(const uint8_t *b, size_t len, struct nb_msg *m) 106 { 107 if (!b || !m || len < NB_MIN || len > NB_MSG_MAX) 108 return -1; 109 for (size_t i = 0; i < 4; i++) 110 if (b[i] != (uint8_t)NB_MAGIC[i]) 111 return -1; 112 if (!zero(b + 5, 3)) 113 return -1; 114 *m = (struct nb_msg){0}; 115 m->type = b[4]; 116 for (size_t i = 0; i < 16; i++) 117 m->nonce[i] = b[8 + i]; 118 switch (m->type) { 119 case NB_HELLO: 120 if (len != NB_MIN || !zero(b + 30, 2)) 121 return -1; 122 for (size_t i = 0; i < 6; i++) 123 m->mac[i] = b[24 + i]; 124 break; 125 case NB_OFFER: 126 if (len != NB_MIN || get16(b + 28) != NB_CHUNK || !zero(b + 30, 2)) 127 return -1; 128 m->total = get32(b + 24); 129 break; 130 case NB_WAIT: 131 if (len != NB_MIN || !zero(b + 26, 6)) 132 return -1; 133 m->seconds = get16(b + 24); 134 break; 135 case NB_GET: 136 if (len != NB_MIN || !zero(b + 30, 2)) 137 return -1; 138 m->offset = get32(b + 24); 139 m->count = get16(b + 28); 140 break; 141 case NB_DATA: 142 if (!zero(b + 30, 2)) 143 return -1; 144 m->offset = get32(b + 24); 145 m->len = get16(b + 28); 146 if (len != NB_MIN + (size_t)m->len) 147 return -1; 148 m->data = b + NB_MIN; 149 break; 150 case NB_DONE: 151 if (!zero(b + 25, 7)) 152 return -1; 153 m->status = b[24]; 154 m->len = (uint16_t)(len - NB_MIN); 155 m->data = b + NB_MIN; 156 break; 157 default: 158 return -1; 159 } 160 return fields_ok(m) ? 0 : -1; 161 }