aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / store / store_disk.c
| 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 | /* |
| 18 | * store_disk.c -- ADR 0015 geometry adapter (device.rs twin): 4096-byte |
| 19 | * Store units over a native/disk disk_dev with 512- or 4096-byte blocks. |
| 20 | * Whole units only; never read-modify-write. |
| 21 | */ |
| 22 | #include "store_engine.h" |
| 23 | |
| 24 | static int sd_read(void *ctx, uint64_t unit, uint8_t out[SV1_UNIT]) |
| 25 | { |
| 26 | st_disk *a = ctx; |
| 27 | if (unit >= a->region_units) |
| 28 | return -1; |
| 29 | return disk_read(a->disk, a->base_lba + unit * a->blocks_per_unit, a->blocks_per_unit, out) |
| 30 | ? -1 |
| 31 | : 0; |
| 32 | } |
| 33 | |
| 34 | static int sd_write(void *ctx, uint64_t unit, const uint8_t in[SV1_UNIT]) |
| 35 | { |
| 36 | st_disk *a = ctx; |
| 37 | if (unit >= a->region_units) |
| 38 | return -1; |
| 39 | return disk_write(a->disk, a->base_lba + unit * a->blocks_per_unit, a->blocks_per_unit, in) |
| 40 | ? -1 |
| 41 | : 0; |
| 42 | } |
| 43 | |
| 44 | static int sd_flush(void *ctx) |
| 45 | { |
| 46 | st_disk *a = ctx; |
| 47 | return disk_flush(a->disk) ? -1 : 0; |
| 48 | } |
| 49 | |
| 50 | int st_disk_bind(st_disk *a, const disk_dev *disk, uint64_t base_lba, uint64_t region_units, |
| 51 | st_dev *out) |
| 52 | { |
| 53 | if (!a || !disk || !out) |
| 54 | return ST_E_ARG; |
| 55 | if (disk_check(disk)) |
| 56 | return ST_E_GEOMETRY; |
| 57 | uint32_t bpu; |
| 58 | if (disk->block_size == 512) |
| 59 | bpu = 8; |
| 60 | else if (disk->block_size == 4096) |
| 61 | bpu = 1; |
| 62 | else |
| 63 | return ST_E_GEOMETRY; /* GUARD:adapter-block-size */ |
| 64 | if (base_lba % bpu) return ST_E_GEOMETRY; /* GUARD:adapter-alignment */ |
| 65 | if (base_lba >= disk->block_count) |
| 66 | return ST_E_GEOMETRY; |
| 67 | uint64_t max_units = (disk->block_count - base_lba) / bpu; |
| 68 | if (region_units == 0) |
| 69 | region_units = max_units; |
| 70 | if (region_units > max_units) return ST_E_GEOMETRY; /* GUARD:adapter-region-end */ |
| 71 | a->disk = disk; |
| 72 | a->base_lba = base_lba; |
| 73 | a->blocks_per_unit = bpu; |
| 74 | a->region_units = region_units; |
| 75 | out->ctx = a; |
| 76 | out->region_units = region_units; |
| 77 | out->read_unit = sd_read; |
| 78 | out->write_unit = sd_write; |
| 79 | out->flush = sd_flush; |
| 80 | return 0; |
| 81 | } |