rumpk/libs/membrane/blk.nim

64 lines
1.9 KiB
Nim

# SPDX-License-Identifier: LSL-1.0
# Copyright (c) 2026 Markus Maiwald
# Stewardship: Self Sovereign Society Foundation
#
# This file is part of the Nexus Sovereign Core.
# See legal/LICENSE_SOVEREIGN.md for license terms.
## Nexus Membrane: Block I/O Client
# Membrane Block API (The Block Valve - Userland Side)
# Phase 37.2: Sovereign Storage Architecture
#
# This module provides raw sector access to userland filesystems.
# The kernel is just a valve - NO filesystem logic in kernel.
import libc
const
SECTOR_SIZE* = 512
# Block Valve Syscalls
SYS_BLK_READ* = 0x220
SYS_BLK_WRITE* = 0x221
SYS_BLK_SYNC* = 0x222
proc blk_read*(sector: uint64, buf: pointer): int =
## Read a single 512-byte sector from disk
## Returns: bytes read (512) or negative error
return syscall(SYS_BLK_READ, uint64(sector), cast[uint64](buf), 1'u64)
proc blk_write*(sector: uint64, buf: pointer): int =
## Write a single 512-byte sector to disk
## Returns: bytes written (512) or negative error
return syscall(SYS_BLK_WRITE, uint64(sector), cast[uint64](buf), 1'u64)
proc blk_sync*(): int =
## Flush all pending writes to disk
## Returns: 0 on success, negative error
return syscall(SYS_BLK_SYNC, 0'u64, 0'u64, 0'u64)
# --- Multi-Sector Helpers ---
proc blk_read_multi*(start_sector: uint64, buf: pointer, count: int): int =
## Read multiple contiguous sectors
var total = 0
for i in 0..<count:
let offset = i * SECTOR_SIZE
let res = blk_read(start_sector + uint64(i),
cast[pointer](cast[int](buf) + offset))
if res < 0: return res
total += res
return total
proc blk_write_multi*(start_sector: uint64, buf: pointer, count: int): int =
## Write multiple contiguous sectors
var total = 0
for i in 0..<count:
let offset = i * SECTOR_SIZE
let res = blk_write(start_sector + uint64(i),
cast[pointer](cast[int](buf) + offset))
if res < 0: return res
total += res
return total