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/slot_update.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 / slot_update.c` 164 lines ยท 7352 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 /* slot_update.c -- the inactive-slot updater (contract in slot_update.h). */ 18 #include "slot_update.h" 19 20 static void zero(uint8_t *p, uint64_t n) 21 { 22 for (uint64_t i = 0; i < n; i++) 23 p[i] = 0; 24 } 25 26 static int refuse(const char **why, const char *w) 27 { 28 if (why) 29 *why = w; 30 return -1; 31 } 32 33 /* Write count units starting at unit u of the slots partition, refusing any 34 * unit outside the target slot [lo, hi). */ 35 static int put_units(const struct ck_slot_update_io *io, uint64_t lo, uint64_t hi, uint64_t u, uint64_t count, 36 const uint8_t *src) 37 { 38 uint32_t bpu = CK_SLOT_UNIT / io->slots->block_size; 39 if (u < lo || count > hi - u || count == 0) 40 return -2; 41 return disk_write(io->slots, u * bpu, (uint32_t)(count * bpu), src) == DISK_OK ? 0 : -1; 42 } 43 44 static int get_units(const disk_dev *d, uint64_t u, uint64_t count, uint8_t *dst) 45 { 46 uint32_t bpu = CK_SLOT_UNIT / d->block_size; 47 return disk_read(d, u * bpu, (uint32_t)(count * bpu), dst) == DISK_OK ? 0 : -1; 48 } 49 50 static int step(const struct ck_slot_update_io *io, const char *name, uint64_t done, uint64_t total) 51 { 52 return io->progress ? io->progress(io->ctx, name, done, total) : 0; 53 } 54 55 /* Units per write call: bounded so one call never asks the backend for more 56 * than 64 units (256 KiB); disk_write splits further by max_blocks_per_io. */ 57 #define CHUNK_UNITS 64u 58 59 int ck_slot_update(const struct ck_slot_update_io *io, struct ck_slot_update_result *res, const char **why) 60 { 61 static struct ck_slot_manifest man, back; 62 static struct ck_slot_control staged; 63 struct ck_slot_eval ev; 64 const char *w = 0; 65 if (!res) 66 return refuse(why, "no result"); 67 zero((uint8_t *)res, sizeof *res); 68 if (!io || !io->slots || !io->inbox || !io->control || !io->buf || !io->keys) 69 return refuse(why, "update arguments"); 70 if (io->nkeys == 0) 71 return refuse(why, "this image trusts no slot signing key"); 72 const struct ck_slot_control *ctl = io->ctl; 73 if (!ctl || ck_slot_control_check(ctl, &w) != 0) 74 return refuse(why, ctl ? w : "no control record on disk"); 75 if (ctl->candidate_slot != CK_SLOT_NONE) 76 return refuse(why, "a candidate is already staged: boot it (promote or abandon) first"); 77 if (io->booted_slot >= CK_SLOT_COUNT || io->booted_slot != ctl->active_slot) 78 return refuse(why, "the booted slot is not the active slot"); 79 for (const disk_dev *d = io->slots;; d = io->inbox) { 80 if (disk_check(d) != DISK_OK || (d->block_size != 512 && d->block_size != 4096)) 81 return refuse(why, "partition geometry"); 82 if (d == io->inbox) 83 break; 84 } 85 unsigned t = 1u - ctl->active_slot; 86 uint64_t units = io->slots->block_count / (CK_SLOT_UNIT / io->slots->block_size); 87 uint64_t cap = ck_slot_cap_units(units); 88 uint64_t in_units = io->inbox->block_count / (CK_SLOT_UNIT / io->inbox->block_size); 89 if (cap < CK_SLOT_MIN_CAP_UNITS || in_units < 2) 90 return refuse(why, "slots or inbox partition too small"); 91 uint64_t lo = ck_slot_manifest_unit(t, cap), hi = lo + cap; 92 res->target = t; 93 94 /* 2. Read and verify; nothing is written before this passes. */ 95 if (get_units(io->inbox, 0, 1, (uint8_t *)&man) != 0) 96 return refuse(why, "inbox manifest read failed"); 97 if (ck_slot_manifest_check(&man, &w) != 0) 98 return refuse(why, w); 99 uint64_t len = man.image_len, n = (len + CK_SLOT_UNIT - 1) / CK_SLOT_UNIT; 100 if (n > cap - 1) 101 return refuse(why, "image larger than the slot"); 102 if (n > in_units - 1) 103 return refuse(why, "image longer than the inbox (truncated package)"); 104 if (n * CK_SLOT_UNIT > io->buf_cap) 105 return refuse(why, "image larger than the update buffer"); 106 for (uint64_t u = 0; u < n; u += CHUNK_UNITS) { 107 uint64_t c = n - u < CHUNK_UNITS ? n - u : CHUNK_UNITS; 108 if (get_units(io->inbox, 1 + u, c, io->buf + u * CK_SLOT_UNIT) != 0) 109 return refuse(why, "inbox image read failed"); 110 } 111 if (!ck_slot_verify(&man, io->buf, (cap - 1) * CK_SLOT_UNIT, io->keys, io->nkeys, &ev)) 112 return refuse(why, ev.why ? ev.why : "inbox image refused"); 113 for (uint64_t i = 0; i < sizeof staged; i++) 114 ((uint8_t *)&staged)[i] = ((const uint8_t *)ctl)[i]; 115 if (ck_slot_stage_candidate(&staged, t, man.generation, &w) != 0) 116 return refuse(why, w); 117 if (step(io, "verified", 0, n)) 118 return refuse(why, "stopped before writing"); 119 120 /* 3. Invalidate the target first. */ 121 static uint8_t zu[CK_SLOT_UNIT]; 122 res->wrote = 1; 123 if (put_units(io, lo, hi, lo, 1, zu) != 0 || disk_flush(io->slots) != DISK_OK) 124 return refuse(why, "invalidating the target slot failed"); 125 if (step(io, "invalidated", 0, n)) 126 return refuse(why, "stopped after invalidating the target slot"); 127 128 /* 4. Image units. */ 129 for (uint64_t u = 0; u < n; u += CHUNK_UNITS) { 130 uint64_t c = n - u < CHUNK_UNITS ? n - u : CHUNK_UNITS; 131 if (put_units(io, lo, hi, lo + 1 + u, c, io->buf + u * CK_SLOT_UNIT) != 0) 132 return refuse(why, "image write failed (target slot left invalid)"); 133 res->units_written += c; 134 if (step(io, "image", u + c, n)) 135 return refuse(why, "stopped while writing the image (target slot left invalid)"); 136 } 137 if (disk_flush(io->slots) != DISK_OK) 138 return refuse(why, "image flush failed (target slot left invalid)"); 139 140 /* 5. Manifest last, then read everything back and verify again. */ 141 if (put_units(io, lo, hi, lo, 1, (const uint8_t *)&man) != 0 || disk_flush(io->slots) != DISK_OK) 142 return refuse(why, "manifest write failed (target slot left invalid)"); 143 res->units_written++; 144 if (step(io, "manifest", n, n)) 145 return refuse(why, "stopped after the manifest (slot valid, not staged)"); 146 int ok = get_units(io->slots, lo, 1, (uint8_t *)&back) == 0; 147 for (uint64_t u = 0; ok && u < n; u += CHUNK_UNITS) { 148 uint64_t c = n - u < CHUNK_UNITS ? n - u : CHUNK_UNITS; 149 ok = get_units(io->slots, lo + 1 + u, c, io->buf + u * CK_SLOT_UNIT) == 0; 150 } 151 if (!ok || !ck_slot_verify(&back, io->buf, (cap - 1) * CK_SLOT_UNIT, io->keys, io->nkeys, &ev)) { 152 (void)put_units(io, lo, hi, lo, 1, zu); 153 (void)disk_flush(io->slots); 154 return refuse(why, "read-back verification of the written slot failed (slot invalidated)"); 155 } 156 157 /* 6. Stage. */ 158 int crc = ck_slot_control_commit(io->control, &staged, &res->seq); 159 if (crc != TS_OK) 160 return refuse(why, "staging commit failed (slot written and valid, not staged)"); 161 res->generation = man.generation; 162 res->image_len = len; 163 return 0; 164 }