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 / boot / slot_update.h

79 lines · 4181 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.h -- write a signed candidate image into the INACTIVE slot
18 * (native v1 plan P11 cut 3, gate N7; docs/NATIVE_IMAGE_SLOTS.md section 9).
19 *
20 * One routine, shared by the service kernel (svc/slot_service.c, operator
21 * verb `update`) and the host tool (ck_slot_tool, its self-test injects an
22 * I/O error at every write). Source: the update inbox partition
23 * (CK_SLOT_INBOX_TYPE, one slot's shape: manifest unit, then the image).
24 *
25 * Order, and what an interruption at each point leaves behind:
26 * 1. refuse unless: control record present, no candidate staged, the
27 * booted slot is the active one; target = the other slot. Nothing read.
28 * 2. read the inbox manifest and image, ck_slot_verify against the image's
29 * trusted keys (signature over the manifest, SHA-512 of the image), and
30 * dry-run ck_slot_stage_candidate (generation above the floor) on a copy
31 * of the control record. Any refusal: NOTHING written.
32 * 3. overwrite the target's manifest unit with zeros, flush. From here the
33 * target slot is INVALID until step 5 completes; the control record is
34 * untouched, so the loader keeps booting the active slot.
35 * 4. write the image units, flush.
36 * 5. write the manifest unit, flush; read the whole slot back and verify it
37 * again (signature + digest from the disk bytes). A failed read-back
38 * zeroes the manifest unit again (best effort) and refuses.
39 * 6. stage: one control record commit (ck_slot_control_commit), candidate =
40 * target, tries = CK_SLOT_MAX_TRIES. Only now does the loader try it.
41 * Every write lies inside the target slot's unit range [M, M + CAP) (checked
42 * before each write); the active slot and the control region (except the one
43 * commit of step 6) are never written. Freestanding, no allocation: the
44 * caller provides the image buffer. */
45#ifndef AIENOS_CK_SLOT_UPDATE_H
46#define AIENOS_CK_SLOT_UPDATE_H
47#include "slot_control.h"
48#include "../disk/disk.h"
49
50struct ck_slot_update_io {
51 const disk_dev *slots; /* the slots partition (bounded view) */
52 const disk_dev *inbox; /* the update inbox partition (read only) */
53 ts_region *control; /* opened and recovered over the slots partition */
54 const struct ck_slot_control *ctl; /* the recovered control record (NULL: refused) */
55 unsigned booted_slot; /* the slot this kernel was started from */
56 const uint8_t (*keys)[32]; /* trusted manifest signers */
57 unsigned nkeys;
58 uint8_t *buf; /* image buffer, buf_cap bytes, 4096-aligned length */
59 uint64_t buf_cap;
60 /* Optional progress hook: step name, units done, units total. A nonzero
61 * return stops the update there (the hosts's interruption point). */
62 int (*progress)(void *ctx, const char *step, uint64_t done, uint64_t total);
63 void *ctx;
64};
65
66struct ck_slot_update_result {
67 unsigned target; /* the slot written (0 or 1) */
68 uint64_t generation; /* of the staged image */
69 uint64_t image_len;
70 uint64_t units_written; /* inside the target slot */
71 uint64_t seq; /* control sequence after staging */
72 int wrote; /* 1 once step 3 wrote anything */
73};
74
75/* 0 = staged (res filled). -1 = refused with *why (res->wrote says whether
76 * the target slot was touched; the control record never was). */
77int ck_slot_update(const struct ck_slot_update_io *io, struct ck_slot_update_result *res, const char **why);
78
79#endif