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 / gpt_write.c

172 lines · 6178 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/* gpt_write.c -- hosted GPT image writer (see gpt_write.h). libc only. */
18#define _GNU_SOURCE
19#include "gpt_write.h"
20#include <fcntl.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25#include "disk_part.h" /* ck_crc32 only */
26
27const uint8_t gw_linux_type[16] = {0xaf, 0x3d, 0xc6, 0x0f, 0x83, 0x84, 0x72, 0x47,
28 0x8e, 0x79, 0x3d, 0x69, 0xd8, 0x47, 0x7d, 0xe4};
29const uint8_t gw_esp_type[16] = {0x28, 0x73, 0x2a, 0xc1, 0x1f, 0xf8, 0xd2, 0x11,
30 0xba, 0x4b, 0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b};
31
32static void put32(uint8_t *p, uint32_t v)
33{
34 for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i));
35}
36static void put64(uint8_t *p, uint64_t v)
37{
38 for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i));
39}
40
41uint64_t gw_entry_blocks(uint32_t bs)
42{
43 return ((uint64_t)GW_ENTRIES * GW_ESIZE + bs - 1u) / bs;
44}
45
46static int pw(int fd, const void *b, size_t n, uint64_t off)
47{
48 const uint8_t *p = b;
49 while (n) {
50 ssize_t w = pwrite(fd, p, n, (off_t)off);
51 if (w <= 0) return -1;
52 p += w;
53 n -= (size_t)w;
54 off += (uint64_t)w;
55 }
56 return 0;
57}
58
59static void header(uint8_t *h, uint64_t my, uint64_t alt, uint64_t fu, uint64_t lu, uint64_t ent_lba,
60 uint32_t ent_crc)
61{
62 static const uint8_t disk_guid[16] = {0x41, 0x49, 0x45, 0x4e, 0x2d, 0x47, 0x50, 0x54,
63 0x2d, 0x54, 0x45, 0x53, 0x54, 0x2d, 0x30, 0x31}; /* "AIEN-GPT-TEST-01" */
64 memcpy(h, "EFI PART", 8);
65 put32(h + 8, 0x00010000u);
66 put32(h + 12, 92);
67 put32(h + 16, 0);
68 put64(h + 24, my);
69 put64(h + 32, alt);
70 put64(h + 40, fu);
71 put64(h + 48, lu);
72 memcpy(h + 56, disk_guid, 16);
73 put64(h + 72, ent_lba);
74 put32(h + 80, GW_ENTRIES);
75 put32(h + 84, GW_ESIZE);
76 put32(h + 88, ent_crc);
77 put32(h + 16, ck_crc32(0, h, 92));
78}
79
80int gw_write(const char *path, uint32_t bs, uint64_t blocks, const gw_part *parts, int nparts, unsigned flags)
81{
82 if ((bs != 512 && bs != 4096) || blocks < 64 || nparts < 0 || nparts > GW_MAX_PARTS) {
83 fprintf(stderr, "gw_write: bad geometry\n");
84 return -1;
85 }
86 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
87 if (fd < 0 || ftruncate(fd, (off_t)(blocks * bs)) != 0) {
88 fprintf(stderr, "gw_write: cannot create %s\n", path);
89 if (fd >= 0) close(fd);
90 return -1;
91 }
92 int rc = 0;
93 /* Partition fills (sentinels): fill ^ low byte of the LBA, so every block differs. */
94 enum { CHUNK = 256 };
95 uint8_t *buf = malloc((size_t)CHUNK * bs);
96 if (!buf) rc = -1;
97 for (int i = 0; !rc && i < nparts; i++) {
98 if (!parts[i].fill) continue;
99 uint64_t f = parts[i].first, l = parts[i].last < blocks ? parts[i].last : blocks - 1u;
100 for (uint64_t lba = f; !rc && lba <= l;) {
101 uint64_t n = l - lba + 1u < CHUNK ? l - lba + 1u : CHUNK;
102 for (uint64_t k = 0; k < n; k++) memset(buf + k * bs, parts[i].fill ^ (uint8_t)(lba + k), bs);
103 rc = pw(fd, buf, (size_t)(n * bs), lba * bs);
104 lba += n;
105 }
106 }
107 free(buf);
108 if (rc || (flags & GW_NO_GPT)) {
109 close(fd);
110 if (rc) fprintf(stderr, "gw_write: write failed\n");
111 return rc;
112 }
113
114 uint64_t last = blocks - 1u, eb = gw_entry_blocks(bs);
115 uint64_t fu = 2u + eb, lu = last - 1u - eb, bent = last - eb;
116 size_t ebytes = (size_t)GW_ENTRIES * GW_ESIZE;
117 uint8_t *ent = calloc(1, (size_t)eb * bs);
118 uint8_t *h = calloc(1, bs), *mbr = calloc(1, bs);
119 if (!ent || !h || !mbr) rc = -1;
120 for (int i = 0; !rc && i < nparts; i++) {
121 uint8_t *e = ent + (size_t)i * GW_ESIZE;
122 memcpy(e, parts[i].type, 16);
123 memcpy(e + 16, "AIEN-UNIQUE-PT", 14); /* unique GUID: fixed text + index */
124 e[30] = (uint8_t)'0';
125 e[31] = (uint8_t)('0' + i);
126 put64(e + 32, parts[i].first);
127 put64(e + 40, parts[i].last);
128 for (size_t c = 0; parts[i].name && parts[i].name[c] && c < 36; c++) e[56 + 2 * c] = (uint8_t)parts[i].name[c];
129 }
130 uint32_t ecrc = ck_crc32(0, ent, ebytes);
131 if (!rc && !(flags & GW_NO_PMBR)) {
132 uint8_t *r = mbr + 446;
133 r[1] = 0x00; r[2] = 0x02; r[3] = 0x00; /* CHS start 0/0/2 */
134 r[4] = 0xee;
135 r[5] = 0xff; r[6] = 0xff; r[7] = 0xff;
136 put32(r + 8, 1);
137 put32(r + 12, last > 0xffffffffu ? 0xffffffffu : (uint32_t)last);
138 mbr[510] = 0x55;
139 mbr[511] = 0xaa;
140 rc = pw(fd, mbr, bs, 0);
141 }
142 /* Primary. */
143 if (!rc) {
144 header(h, 1, last, fu, lu, 2, ecrc);
145 if (flags & GW_BAD_PRIMARY_CRC) h[16] ^= 0x01;
146 rc = pw(fd, h, bs, 1u * bs);
147 }
148 if (!rc) {
149 if (flags & GW_BAD_PRIMARY_ENT) ent[56] ^= 0x20; /* first entry's name, after the CRC */
150 rc = pw(fd, ent, (size_t)eb * bs, 2u * bs);
151 if (flags & GW_BAD_PRIMARY_ENT) ent[56] ^= 0x20;
152 }
153 /* Backup. */
154 if (!rc) {
155 if (flags & GW_BAD_BACKUP_ENT) ent[56] ^= 0x20;
156 rc = pw(fd, ent, (size_t)eb * bs, bent * bs);
157 if (flags & GW_BAD_BACKUP_ENT) ent[56] ^= 0x20;
158 }
159 if (!rc) {
160 memset(h, 0, bs);
161 header(h, last, 1, fu, lu, bent, ecrc);
162 if (flags & GW_BAD_BACKUP_CRC) h[16] ^= 0x01;
163 rc = pw(fd, h, bs, last * bs);
164 }
165 if (rc) fprintf(stderr, "gw_write: write failed\n");
166 free(ent);
167 free(h);
168 free(mbr);
169 if (fsync(fd) != 0) rc = -1;
170 close(fd);
171 return rc;
172}