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/kernel/tools/ck_gpt_image.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 / kernel / tools / ck_gpt_image.c` 263 lines ยท 13708 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* ck_gpt_image -- host tool: GPT boot-disk images for the C kernel's QEMU 18 * gates (scripts/qemu_ck_store_test.sh, qemu_ck_net_test.sh, 19 * qemu_ck_artifact_test.sh, qemu_ck_disk_layout_test.sh). Hosted C, test 20 * tooling only, never part of an image. No Python, no sgdisk. 21 * 22 * ck_gpt_image create IMG BS MIB LAYOUT [CORRUPTION...] 23 * BS 512|4096, MIB >= 16. LAYOUT (MiB offsets, M = MIB): 24 * aienos-middle sentinel-a [1,9) Linux type, filled with a pattern; 25 * AIENOS [9,M-7) all zero; sentinel-b [M-7,M-1) filled 26 * no-aienos same, middle partition has the Linux type 27 * two-aienos same, sentinel-b also has the AIENOS type 28 * aienos-outside sentinel-a, AIENOS [9, M+1): ends past the disk 29 * aienos-overlap sentinel-a [1,9), AIENOS [5,M-7), sentinel-b 30 * one-old sentinel-a only; the rest unused (Allen memory gate) 31 * one-whole one empty Linux partition [1, M-1): no unused gap 32 * no-gpt sentinel fills only: no protective MBR, no GPT 33 * esp-model ESP_MIB MODEL_FILE ESP + model in 3 out-of-order 34 * extents (see esp_model below; model ingest gate) 35 * slots one AIENOS image slots partition [1, M-1) (type 36 * CK_SLOT_PART_TYPE_TEXT, native/boot/slot_format.h), left 37 * zero; prints "gpt_image bs=B blocks=N slots_first_lba=F 38 * slots_last_lba=L" (slot loader gate) 39 * aienos-slots AIENOS [1,41) all zero + slots [41,M-1) (MIB >= 48; 40 * P11 cut 2b service boot disk); prints both ranges 41 * esp-model-aienos ESP_MIB MODEL_FILE STORE_MIB esp-model plus an 42 * all-zero AIENOS partition of STORE_MIB at the end: 43 * one disk with ESP, model and Store (service gate) 44 * aienos-slots-inbox AIENOS [1,41) + slots [41,M-17) + update inbox 45 * [M-17,M-1) (MIB >= 64; P11 cut 3); prints all three ranges 46 * CORRUPTION: bad-primary-crc bad-backup-crc bad-primary-entries 47 * bad-backup-entries no-pmbr 48 * Prints one line: "gpt_image bs=B blocks=N aienos_first_lba=F 49 * aienos_last_lba=L" (the AIENOS range as WRITTEN, from this tool's own 50 * layout, not from the kernel parser; 0 0 when the layout has none). 51 * ck_gpt_image find IMG BS 52 * Runs the kernel's own parser (dev/disk_part.c) read-only; prints 53 * "aienos first_lba=F last_lba=L blocks=K" (exit 0) or 54 * "refused: rc=N" (exit 1). */ 55 #define _GNU_SOURCE 56 #include 57 #include 58 #include 59 #include 60 #include 61 #include "disk_file.h" 62 #include "disk_part.h" 63 #include "gpt_write.h" 64 #include "../../boot/slot_format.h" 65 66 static int usage(void) 67 { 68 fprintf(stderr, "usage: ck_gpt_image create IMG BS MIB LAYOUT [CORRUPTION...] | find IMG BS\n"); 69 return 2; 70 } 71 72 /* esp-model (model ingest gate, scripts/qemu_ck_infer_test.sh --disk): 73 * ck_gpt_image create IMG BS MIB esp-model ESP_MIB MODEL_FILE 74 * p0 = EFI System Partition [1, 1+ESP_MIB) MiB, left zero (the script formats 75 * it with mkfs.vfat --offset and fills it with mtools); p1 = Linux type 76 * [1+ESP_MIB, MIB-1) MiB holding MODEL_FILE in THREE extents written OUT OF 77 * FILE ORDER with 1 MiB gaps (disk order C, A, B for file thirds A, B, C), so a 78 * reader that ignores the map order or the gaps cannot produce the right 79 * bytes. Prints: "gpt_image bs=B blocks=N esp_first_lba=F esp_last_lba=L 80 * model_len=M model_extents=LBA:COUNT,LBA:COUNT,LBA:COUNT" (file order). */ 81 static int esp_model(int argc, char **argv, uint32_t bs, uint64_t blocks, uint64_t per, unsigned flags) 82 { 83 /* esp-model-aienos (argc 9): the model-home partition ends STORE_MIB 84 * earlier and an all-zero AIENOS partition takes [M-1-STORE_MIB, M-1). */ 85 int with_store = argc == 9; 86 if (argc != 8 && !with_store) return usage(); 87 uint64_t esp_mib = strtoull(argv[6], NULL, 10); 88 const char *mpath = argv[7]; 89 uint64_t mib = blocks / per; 90 uint64_t store_mib = with_store ? strtoull(argv[8], NULL, 10) : 0; 91 if (with_store && store_mib < 16) return usage(); 92 if (esp_mib < 16 || esp_mib + 4 + store_mib >= mib) return usage(); 93 FILE *mf = fopen(mpath, "rb"); 94 if (!mf) { fprintf(stderr, "cannot open %s\n", mpath); return 1; } 95 fseeko(mf, 0, SEEK_END); 96 uint64_t mlen = (uint64_t)ftello(mf); 97 fseeko(mf, 0, SEEK_SET); 98 uint64_t sectors = (mlen + bs - 1) / bs, gap = (1u << 20) / bs; 99 uint64_t a = sectors / 3, b = sectors / 3, c = sectors - a - b; 100 uint64_t p1_first = (1 + esp_mib) * per, p1_last = (mib - 1u - store_mib) * per - 1u; 101 if (p1_first + 3 * gap + sectors > p1_last + 1) { 102 fprintf(stderr, "image too small for the model: need %llu more MiB\n", 103 (unsigned long long)((p1_first + 3 * gap + sectors - p1_last) / per + 1)); 104 fclose(mf); 105 return 1; 106 } 107 /* disk order: C, A, B */ 108 uint64_t lba_c = p1_first + gap, lba_a = lba_c + c + gap, lba_b = lba_a + a + gap; 109 gw_part p[3]; 110 p[0] = (gw_part){gw_esp_type, 1 * per, (1 + esp_mib) * per - 1u, "ESP", 0}; 111 p[1] = (gw_part){gw_linux_type, p1_first, p1_last, "model-home", 0}; 112 p[2] = (gw_part){ck_aienos_part_type, p1_last + 1u, (mib - 1u) * per - 1u, "AIENOS", 0}; 113 if (gw_write(argv[2], bs, blocks, p, with_store ? 3 : 2, flags)) { fclose(mf); return 1; } 114 int fd = open(argv[2], O_WRONLY); 115 if (fd < 0) { fclose(mf); fprintf(stderr, "cannot reopen %s\n", argv[2]); return 1; } 116 struct { uint64_t lba, count; } ext[3] = {{lba_a, a}, {lba_b, b}, {lba_c, c}}; 117 static uint8_t buf[1u << 20]; 118 int rc = 0; 119 for (int i = 0; i < 3 && !rc; i++) { 120 uint64_t left = ext[i].count * bs, off = ext[i].lba * bs; 121 while (left && !rc) { 122 size_t want = left < sizeof buf ? (size_t)left : sizeof buf; 123 memset(buf, 0, want); 124 size_t got = fread(buf, 1, want, mf); /* short only on the final partial sector */ 125 if (got == 0) { rc = 1; break; } 126 if (pwrite(fd, buf, want, (off_t)off) != (ssize_t)want) rc = 1; 127 left -= want; 128 off += want; 129 } 130 } 131 close(fd); 132 fclose(mf); 133 if (rc) { fprintf(stderr, "model write failed\n"); return 1; } 134 printf("gpt_image bs=%u blocks=%llu esp_first_lba=%llu esp_last_lba=%llu model_len=%llu " 135 "model_extents=%llu:%llu,%llu:%llu,%llu:%llu\n", 136 bs, (unsigned long long)blocks, (unsigned long long)p[0].first, (unsigned long long)p[0].last, 137 (unsigned long long)mlen, (unsigned long long)lba_a, (unsigned long long)a, 138 (unsigned long long)lba_b, (unsigned long long)b, (unsigned long long)lba_c, (unsigned long long)c); 139 if (with_store) 140 printf("gpt_image aienos_first_lba=%llu aienos_last_lba=%llu\n", (unsigned long long)p[2].first, 141 (unsigned long long)p[2].last); 142 return 0; 143 } 144 145 static int cmd_create(int argc, char **argv) 146 { 147 if (argc < 6) return usage(); 148 uint32_t bs = (uint32_t)strtoul(argv[3], NULL, 10); 149 uint64_t mib = strtoull(argv[4], NULL, 10); 150 if ((bs != 512 && bs != 4096) || mib < 16 || mib > `1048576`) return usage(); 151 uint64_t per = (1024u * 1024u) / bs, blocks = mib * per; 152 const char *lay = argv[5]; 153 unsigned flags = 0; 154 if (!strcmp(lay, "esp-model") && argc == 8) return esp_model(argc, argv, bs, blocks, per, flags); 155 if (!strcmp(lay, "esp-model-aienos") && argc == 9) return esp_model(argc, argv, bs, blocks, per, flags); 156 if (!strcmp(lay, "slots")) { 157 static const uint8_t slots_type[16] = CK_SLOT_PART_TYPE_BYTES; 158 gw_part sp = {slots_type, 1 * per, (mib - 1u) * per - 1u, "AIENOS-SLOTS", 0}; 159 if (argc != 6) return usage(); 160 if (gw_write(argv[2], bs, blocks, &sp, 1, flags)) return 1; 161 printf("gpt_image bs=%u blocks=%llu slots_first_lba=%llu slots_last_lba=%llu\n", bs, 162 (unsigned long long)blocks, (unsigned long long)sp.first, (unsigned long long)sp.last); 163 return 0; 164 } 165 if (!strcmp(lay, "aienos-slots")) { 166 /* P11 cut 2b: the service boot disk with its image slots (one NVMe disk). */ 167 static const uint8_t slots_type[16] = CK_SLOT_PART_TYPE_BYTES; 168 gw_part sp[2] = {{ck_aienos_part_type, 1 * per, 41 * per - 1u, "AIENOS", 0}, 169 {slots_type, 41 * per, (mib - 1u) * per - 1u, "AIENOS-SLOTS", 0}}; 170 if (argc != 6 || mib < 48) return usage(); 171 if (gw_write(argv[2], bs, blocks, sp, 2, flags)) return 1; 172 printf("gpt_image bs=%u blocks=%llu aienos_first_lba=%llu aienos_last_lba=%llu slots_first_lba=%llu " 173 "slots_last_lba=%llu\n", 174 bs, (unsigned long long)blocks, (unsigned long long)sp[0].first, (unsigned long long)sp[0].last, 175 (unsigned long long)sp[1].first, (unsigned long long)sp[1].last); 176 return 0; 177 } 178 if (!strcmp(lay, "aienos-slots-inbox")) { 179 /* P11 cut 3: the same disk plus the update inbox partition (slot_update.h). */ 180 static const uint8_t slots_type[16] = CK_SLOT_PART_TYPE_BYTES, inbox_type[16] = CK_SLOT_INBOX_TYPE_BYTES; 181 gw_part sp[3] = {{ck_aienos_part_type, 1 * per, 41 * per - 1u, "AIENOS", 0}, 182 {slots_type, 41 * per, (mib - 17u) * per - 1u, "AIENOS-SLOTS", 0}, 183 {inbox_type, (mib - 17u) * per, (mib - 1u) * per - 1u, "AIENOS-INBOX", 0}}; 184 if (argc != 6 || mib < 64) return usage(); 185 if (gw_write(argv[2], bs, blocks, sp, 3, flags)) return 1; 186 printf("gpt_image bs=%u blocks=%llu aienos_first_lba=%llu aienos_last_lba=%llu slots_first_lba=%llu " 187 "slots_last_lba=%llu inbox_first_lba=%llu inbox_last_lba=%llu\n", 188 bs, (unsigned long long)blocks, (unsigned long long)sp[0].first, (unsigned long long)sp[0].last, 189 (unsigned long long)sp[1].first, (unsigned long long)sp[1].last, (unsigned long long)sp[2].first, 190 (unsigned long long)sp[2].last); 191 return 0; 192 } 193 for (int i = 6; i < argc; i++) { 194 if (!strcmp(argv[i], "bad-primary-crc")) flags |= GW_BAD_PRIMARY_CRC; 195 else if (!strcmp(argv[i], "bad-backup-crc")) flags |= GW_BAD_BACKUP_CRC; 196 else if (!strcmp(argv[i], "bad-primary-entries")) flags |= GW_BAD_PRIMARY_ENT; 197 else if (!strcmp(argv[i], "bad-backup-entries")) flags |= GW_BAD_BACKUP_ENT; 198 else if (!strcmp(argv[i], "no-pmbr")) flags |= GW_NO_PMBR; 199 else return usage(); 200 } 201 gw_part p[3]; 202 int n = 3; 203 p[0] = (gw_part){gw_linux_type, 1 * per, 9 * per - 1u, "sentinel-a", 0xa5}; 204 p[1] = (gw_part){ck_aienos_part_type, 9 * per, (mib - 7u) * per - 1u, "AIENOS", 0}; 205 p[2] = (gw_part){gw_linux_type, (mib - 7u) * per, (mib - 1u) * per - 1u, "sentinel-b", 0x5a}; 206 if (!strcmp(lay, "aienos-middle")) { 207 } else if (!strcmp(lay, "no-aienos")) { 208 p[1].type = gw_linux_type; 209 } else if (!strcmp(lay, "two-aienos")) { 210 p[2].type = ck_aienos_part_type; 211 } else if (!strcmp(lay, "aienos-outside")) { 212 p[1].last = (mib + 1u) * per - 1u; 213 n = 2; 214 } else if (!strcmp(lay, "aienos-overlap")) { 215 p[1].first = 5 * per; 216 } else if (!strcmp(lay, "one-old")) { 217 n = 1; /* sentinel-a only: the rest of the disk unused */ 218 } else if (!strcmp(lay, "one-whole")) { 219 p[0] = (gw_part){gw_linux_type, 1 * per, (mib - 1u) * per - 1u, "whole", 0}; 220 n = 1; 221 } else if (!strcmp(lay, "no-gpt")) { 222 flags |= GW_NO_GPT; 223 } else { 224 return usage(); 225 } 226 if (gw_write(argv[2], bs, blocks, p, n, flags)) return 1; 227 int has = strcmp(lay, "no-aienos") && strcmp(lay, "one-old") && strcmp(lay, "one-whole"); 228 printf("gpt_image bs=%u blocks=%llu aienos_first_lba=%llu aienos_last_lba=%llu\n", bs, 229 (unsigned long long)blocks, has ? (unsigned long long)p[1].first : 0ull, 230 has ? (unsigned long long)p[1].last : 0ull); 231 return 0; 232 } 233 234 static int cmd_find(int argc, char **argv) 235 { 236 if (argc != 4) return usage(); 237 uint32_t bs = (uint32_t)strtoul(argv[3], NULL, 10); 238 if (bs != 512 && bs != 4096) return usage(); 239 disk_file f; 240 disk_dev d; 241 if (disk_file_open(&f, &d, argv[2], bs, 0, 0)) { 242 fprintf(stderr, "cannot open %s\n", argv[2]); 243 return 2; 244 } 245 static ck_part p; 246 ck_gpt_info gi; 247 int rc = ck_gpt_find_aienos(&d, &p, &gi); 248 if (rc) 249 printf("refused: %s rc=%d\n", ck_gpt_strerror(rc), rc); 250 else 251 printf("aienos first_lba=%llu last_lba=%llu blocks=%llu\n", (unsigned long long)p.first_lba, 252 (unsigned long long)(p.first_lba + p.blocks - 1u), (unsigned long long)p.blocks); 253 disk_file_close(&f); 254 return rc ? 1 : 0; 255 } 256 257 int main(int argc, char **argv) 258 { 259 if (argc < 2) return usage(); 260 if (!strcmp(argv[1], "create")) return cmd_create(argc, argv); 261 if (!strcmp(argv[1], "find")) return cmd_find(argc, argv); 262 return usage(); 263 }