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/dev/gpt_add.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 / dev / gpt_add.c` 255 lines ยท 8656 bytes Raw Blame History 1 /* gpt_add.c -- add the AIENOS partition in unused space; see gpt_add.h. */ 2 #include "gpt_add.h" 3 4 #include "disk_part.h" 5 #include "sha256.h" 6 7 #define ENT_MAX (64u << 10) 8 static uint8_t g_ent[ENT_MAX]; 9 static uint8_t g_ph[4096]; /* primary header block */ 10 static uint8_t g_bh[4096]; /* backup header block */ 11 static uint64_t g_f[CK_GPT_MAX_USED], g_l[CK_GPT_MAX_USED]; 12 13 /* UEFI 2.10 table 5-5 offsets, as in disk_part.c */ 14 enum { H_CRC = 16, H_SIZE = 12, H_GUID = 56, H_ENT_LBA = 72, H_ECRC = 88 }; 15 static const ck_gpt_new allen_memory = {ck_aienos_part_type, "AIENOS Allen memory", "aienos.allen.memory.v1"}; 16 17 static uint32_t get32(const uint8_t *p) 18 { 19 return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; 20 } 21 static uint64_t get64(const uint8_t *p) { return (uint64_t)get32(p) | (uint64_t)get32(p + 4) << 32; } 22 static void put32(uint8_t *p, uint32_t v) 23 { 24 for (int i = 0; i < 4; i++) 25 p[i] = (uint8_t)(v >> (8 * i)); 26 } 27 static void put64(uint8_t *p, uint64_t v) 28 { 29 put32(p, (uint32_t)v); 30 put32(p + 4, (uint32_t)(v >> 32)); 31 } 32 static int zero16(const uint8_t *p) 33 { 34 uint8_t d = 0; 35 for (int i = 0; i < 16; i++) 36 d |= p[i]; 37 return d == 0; 38 } 39 40 /* Reads both header blocks and the primary entry array (after the strict 41 * check passed far enough to fill info). */ 42 static int load(const disk_dev *d, const ck_gpt_info *gi, uint32_t *bytes, uint32_t *blocks) 43 { 44 *bytes = gi->entries * gi->entry_size; 45 if (*bytes == 0 || *bytes > ENT_MAX || d->block_size > sizeof g_ph) 46 return CK_GPT_EBIG; 47 *blocks = (*bytes + d->block_size - 1u) / d->block_size; 48 if (disk_read(d, 1, 1, g_ph) != DISK_OK || disk_read(d, gi->last_lba, 1, g_bh) != DISK_OK || 49 disk_read(d, get64(g_ph + H_ENT_LBA), *blocks, g_ent) != DISK_OK) 50 return CK_GPT_EIO; 51 return CK_GPT_OK; 52 } 53 54 int ck_gpt_list(const disk_dev *d, void (*fn)(void *ctx, const ck_gpt_entry *e), void *ctx) 55 { 56 ck_part p; 57 ck_gpt_info gi; 58 int rc = ck_gpt_find_aienos(d, &p, &gi); 59 if (rc != CK_GPT_OK && gi.entries == 0) 60 return rc; 61 uint32_t bytes, blocks, used = 0; 62 if ((rc = load(d, &gi, &bytes, &blocks))) 63 return rc; 64 for (uint32_t i = 0; i < gi.entries; i++) { 65 const uint8_t *e = g_ent + (uint64_t)i * gi.entry_size; 66 if (zero16(e)) 67 continue; 68 ck_gpt_entry out; 69 out.index = i; 70 for (int k = 0; k < 16; k++) { 71 out.type[k] = e[k]; 72 out.uguid[k] = e[16 + k]; 73 } 74 out.first = get64(e + 32); 75 out.last = get64(e + 40); 76 unsigned n = 0; 77 for (; n < 36; n++) { 78 uint16_t c = (uint16_t)(e[56 + 2 * n] | e[57 + 2 * n] << 8); 79 if (!c) 80 break; 81 out.name[n] = c >= 0x20 && c < 0x7f && c != '"' ? (char)c : '?'; 82 } 83 out.name[n] = 0; 84 used++; 85 if (fn) 86 fn(ctx, &out); 87 } 88 return (int)used; 89 } 90 91 static void header_crc(uint8_t *h, uint32_t ecrc) 92 { 93 put32(h + H_ECRC, ecrc); 94 put32(h + H_CRC, 0); 95 put32(h + H_CRC, ck_crc32(0, h, get32(h + H_SIZE))); 96 } 97 98 /* The unique GUID of a partition this file adds: SHA-256 over the label 99 * (with its terminator), the disk GUID and the first LBA, cut to 16 bytes and 100 * marked version 4 / variant 1. */ 101 static void unique_guid(const char *label, const uint8_t *disk_guid, uint64_t first, uint8_t out[16]) 102 { 103 uint8_t dig[32], lba8[8]; 104 size_t n = 0; 105 while (label[n]) 106 n++; 107 put64(lba8, first); 108 sha256_ctx h; 109 sha256_init(&h); 110 sha256_update(&h, (const uint8_t *)label, n + 1u); 111 sha256_update(&h, disk_guid, 16); 112 sha256_update(&h, lba8, 8); 113 sha256_final(&h, dig); 114 for (int i = 0; i < 16; i++) 115 out[i] = dig[i]; 116 out[7] = (uint8_t)((out[7] & 0x0f) | 0x40); 117 out[8] = (uint8_t)((out[8] & 0x3f) | 0x80); 118 } 119 120 int ck_gpt_is_ours(const disk_dev *d, const ck_gpt_new *spec, uint32_t index) 121 { 122 ck_part p; 123 ck_gpt_info gi; 124 int rc = ck_gpt_find_type(d, spec->type, &p, &gi); 125 if (rc != CK_GPT_OK) 126 return rc; 127 uint32_t bytes, blocks; 128 if (index != gi.part_index || (rc = load(d, &gi, &bytes, &blocks))) 129 return rc ? rc : 0; 130 const uint8_t *e = g_ent + (uint64_t)index * gi.entry_size; 131 uint8_t want[16]; 132 unique_guid(spec->label, g_ph + H_GUID, get64(e + 32), want); 133 for (int i = 0; i < 16; i++) 134 if (e[16 + i] != want[i]) 135 return 0; 136 for (unsigned i = 0; i < 36; i++) { 137 uint16_t c = (uint16_t)(e[56 + 2 * i] | e[57 + 2 * i] << 8); 138 if (c != (uint8_t)spec->name[i]) 139 return 0; 140 if (!c) 141 break; 142 } 143 return 1; 144 } 145 146 int ck_gpt_add_aienos(const disk_dev *d, uint64_t blocks, uint64_t align, ck_gpt_added *out) 147 { 148 return ck_gpt_add(d, &allen_memory, blocks, align, out); 149 } 150 151 int ck_gpt_add(const disk_dev *d, const ck_gpt_new *spec, uint64_t blocks, uint64_t align, ck_gpt_added *out) 152 { 153 ck_part p; 154 ck_gpt_info gi; 155 if (!out || !blocks || !align || !spec || !spec->type || !spec->name || !spec->label) 156 return CK_GPT_EGEOM; 157 size_t name_len = 0; 158 while (spec->name[name_len]) 159 name_len++; 160 if (name_len > 36) 161 return CK_GPT_EGEOM; 162 *out = (ck_gpt_added){0, 0, 0, 0}; 163 int rc = ck_gpt_find_type(d, spec->type, &p, &gi); 164 if (rc == CK_GPT_OK) { 165 out->first = p.first_lba; 166 out->last = p.first_lba + p.blocks - 1u; 167 out->index = gi.part_index; 168 return CK_GPT_OK; 169 } 170 if (rc != CK_GPT_ENOPART) 171 return rc; 172 uint32_t bytes, eblocks; 173 if ((rc = load(d, &gi, &bytes, &eblocks))) 174 return rc; 175 176 /* Used ranges, sorted by first LBA (the strict check proved they lie in 177 * the usable range and do not overlap). */ 178 uint32_t n = 0, slot = gi.entries; 179 for (uint32_t i = 0; i < gi.entries; i++) { 180 const uint8_t *e = g_ent + (uint64_t)i * gi.entry_size; 181 if (zero16(e)) { 182 if (slot == gi.entries) 183 slot = i; 184 continue; 185 } 186 if (n == CK_GPT_MAX_USED) 187 return CK_GPT_ETOOMANY; 188 uint64_t f = get64(e + 32), l = get64(e + 40); 189 uint32_t k = n++; 190 for (; k && g_f[k - 1] > f; k--) { 191 g_f[k] = g_f[k - 1]; 192 g_l[k] = g_l[k - 1]; 193 } 194 g_f[k] = f; 195 g_l[k] = l; 196 } 197 if (slot == gi.entries) 198 return CK_GPT_ENOSLOT; 199 uint64_t at = 0, cursor = gi.first_usable; 200 int found = 0; 201 for (uint32_t k = 0; k <= n && !found; k++) { 202 uint64_t gap_end = k < n ? g_f[k] - 1u : gi.last_usable; /* g_f[k] >= first_usable >= 34 */ 203 uint64_t start = (cursor + align - 1u) / align * align; 204 if (cursor <= gap_end && start >= cursor && start <= gap_end && gap_end - start >= blocks - 1u) { 205 at = start; 206 found = 1; 207 } 208 if (k < n && g_l[k] + 1u > cursor) 209 cursor = g_l[k] + 1u; 210 } 211 if (!found) 212 return CK_GPT_ENOSPACE; 213 214 /* The new entry, its unique GUID from unique_guid. */ 215 uint8_t *e = g_ent + (uint64_t)slot * gi.entry_size; 216 for (uint32_t i = 0; i < gi.entry_size; i++) 217 e[i] = 0; 218 for (int i = 0; i < 16; i++) 219 e[i] = spec->type[i]; 220 unique_guid(spec->label, g_ph + H_GUID, at, e + 16); 221 put64(e + 32, at); 222 put64(e + 40, at + blocks - 1u); 223 for (unsigned i = 0; i < name_len; i++) 224 e[56 + 2 * i] = (uint8_t)spec->name[i]; 225 226 uint32_t ecrc = ck_crc32(0, g_ent, bytes); 227 header_crc(g_ph, ecrc); 228 header_crc(g_bh, ecrc); 229 uint64_t bent = get64(g_bh + H_ENT_LBA), pent = get64(g_ph + H_ENT_LBA); 230 if (disk_write(d, bent, eblocks, g_ent) != DISK_OK || disk_write(d, gi.last_lba, 1, g_bh) != DISK_OK || 231 disk_flush(d) != DISK_OK || disk_write(d, pent, eblocks, g_ent) != DISK_OK || 232 disk_write(d, 1, 1, g_ph) != DISK_OK || disk_flush(d) != DISK_OK) 233 return CK_GPT_EWRITE; 234 235 rc = ck_gpt_find_type(d, spec->type, &p, &gi); 236 if (rc != CK_GPT_OK || p.first_lba != at || p.blocks != blocks || gi.part_index != slot) 237 return CK_GPT_EVERIFY; 238 out->first = at; 239 out->last = at + blocks - 1u; 240 out->index = slot; 241 out->created = 1; 242 return CK_GPT_OK; 243 } 244 245 const char *ck_gpt_add_strerror(int rc) 246 { 247 switch (rc) { 248 case CK_GPT_ENOSPACE: return "no unused space large enough"; 249 case CK_GPT_ENOSLOT: return "no free partition entry"; 250 case CK_GPT_EBIG: return "partition entry array too large"; 251 case CK_GPT_EWRITE: return "write failed"; 252 case CK_GPT_EVERIFY: return "partition not as written after the check"; 253 default: return ck_gpt_strerror(rc); 254 } 255 }