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

87 lines · 3715 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/* pi5_store_locate.c -- see pi5_store_locate.h. Hosted. */
18#include <stdio.h>
19#include <string.h>
20#include "ck.h"
21#include "disk.h"
22#include "disk_file.h"
23#include "disk_layout.h"
24#include "store_v1.h"
25#include "store_engine.h"
26#include "pi5_store_locate.h"
27#include "../core/pi5_marker.h"
28
29static st_workspace g_ws;
30static st_store g_st;
31
32static int fail(char *err, size_t n, const char *msg)
33{
34 if (err && n) snprintf(err, n, "%s", msg);
35 return -1;
36}
37
38int ck_pi5_locate_keyslot(const char *path, ck_pi5_ksm_loc *out, char *err, size_t errn)
39{
40 disk_file f;
41 disk_dev d;
42 memset(out, 0, sizeof *out);
43 if (disk_file_open(&f, &d, path, 512, 0, 0)) return fail(err, errn, "cannot open the partition image");
44 const uint64_t bpu = CK_LAYOUT_UNIT / DISK_SECTOR_BYTES;
45 if (d.block_count < CK_PI5_MARKER_SECS + bpu) {
46 disk_file_close(&f);
47 return fail(err, errn, "image too small");
48 }
49 /* the Store view: the partition minus the marker tail, in whole units (ck_pi5_store_view) */
50 uint64_t store_blocks = (d.block_count - CK_PI5_MARKER_SECS) / bpu * bpu;
51 disk_dev sv = d;
52 sv.block_count = store_blocks;
53 uint64_t base_lba = (uint64_t)CK_LAYOUT_ANCHOR_UNITS * bpu;
54 uint64_t units = store_blocks / bpu - CK_LAYOUT_ANCHOR_UNITS - CK_LAYOUT_PROBE_UNITS;
55 st_disk sd;
56 st_dev sdev;
57 int rc = st_disk_bind(&sd, &sv, base_lba, units, &sdev);
58 if (rc) { disk_file_close(&f); return fail(err, errn, "st_disk_bind refused the geometry"); }
59 rc = st_open(&g_st, &sdev, &g_ws);
60 if (rc) { disk_file_close(&f); return fail(err, errn, "st_open (unkeyed) refused: not an intact Store"); }
61 uint32_t n = 0;
62 const sv1_entry *e = st_catalog(&g_st, &n);
63 int at = -1;
64 for (uint32_t i = 0; i < n; i++)
65 if (e[i].kind == 21 && e[i].version == 1) {
66 out->keyslot_objects++;
67 at = (int)i;
68 }
69 if (out->keyslot_objects != 1) {
70 disk_file_close(&f);
71 return fail(err, errn, "need exactly one KeySlotManifest object in the catalog");
72 }
73 out->first_unit = e[at].first_unit;
74 out->byte_length = e[at].byte_length;
75 out->part_sector = base_lba + e[at].first_unit * bpu;
76 out->part_byte_offset = out->part_sector * DISK_SECTOR_BYTES;
77 out->wrapped_kvol_offset = out->part_byte_offset + CK_PI5_KSM_RECOVERY_SLOT_OFF + CK_PI5_KSM_WRAPPED_KVOL_OFF;
78 out->wrap_tag_offset = out->part_byte_offset + CK_PI5_KSM_RECOVERY_SLOT_OFF + CK_PI5_KSM_WRAP_TAG_OFF;
79 /* read the raw sector back: it must start with the manifest magic "AIENKSL1" (m5_ksm_encode) */
80 uint8_t raw[512] __attribute__((aligned(64)));
81 rc = disk_read(&d, out->part_sector, 1, raw);
82 disk_file_close(&f);
83 if (rc != DISK_OK) return fail(err, errn, "raw read of the manifest sector failed");
84 if (memcmp(raw, "AIENKSL1", 8) != 0) return fail(err, errn, "raw sector at the located place is not a KeySlotManifest");
85 if (out->byte_length != 640) return fail(err, errn, "manifest length is not the 640-byte raw-secret profile");
86 return 0;
87}