boot: one hand-off contract, defined once

Groundwork for the unified loader: before the two paths can share code,
they have to agree on what they are producing, and until now they only
agreed by coincidence.

Every field of the loader/kernel hand-off was #defined in whichever file
happened to need it -- kernel/core/main.c, kernel/dev/vga.c,
kernel/arch/x86_64/e820.c, boot/bios/cdboot.S, boot/bios/stage2main.c and
boot/uefi/main.c, six copies of the same addresses. schboot.h existed for
exactly this and held only the entry magic and the RSDP slot, with a
comment describing the rest of the block it did not define.

The whole block lives there now: the VBE info fields, the verbose and
media flags (with the real-mode segment:offset spelling cdboot.S needs),
the e820 count, map and cap, and the RSDP. Every one of those files
includes it instead of restating it.

This is not a tidy-up. It is the same hazard as defining the entry magic
twice, only quieter -- nothing breaks until one copy moves, and the two
loaders diverging is precisely what produced the EFER.SCE and RSDP bugs,
where a fix on one path silently did not exist on the other.

Verified on both firmwares: BIOS boots to schinit with the same e820,
ACPI and VGA lines as before; UEFI reaches cpu0 with 1018 MB in 7 regions
and its own 1280x800 mode. `make iso` still builds. bootcheck 108 [ok].

Next: the loaders become starters that gather firmware-specific facts and
hand to one common stage. That needs the BIOS side to climb to long mode
first, since common code can only be compiled for one CPU mode -- which
is the risky part, and is why the contract went first.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Nexoniarz 2026-08-10 21:37:36 +02:00
commit 0500e4382d
7 changed files with 89 additions and 56 deletions

View file

@ -18,9 +18,7 @@
# info block at 0x30000 that stage2.S fills in a moment from now.
#define STAGE2_ADDR 0x7E00
#define BOOT_MEDIA_FLAG_SEG 0x3000
#define BOOT_MEDIA_FLAG_OFF 0x101
#define BOOT_MEDIA_CD 1
#include "../../include/schboot.h"
.code16
.global start
@ -34,10 +32,10 @@ start:
# "You were booted from a CD." stage2main.c reads this to decide between
# ATA and ATAPI; without it the loader would read the kernel off whatever
# happens to be sitting at 0x1F0, which on a CD boot is usually nothing.
movw $BOOT_MEDIA_FLAG_SEG, %ax
movw $SCHBOOT_MEDIA_SEG, %ax
movw %ax, %es
movw $BOOT_MEDIA_FLAG_OFF, %di
movb $BOOT_MEDIA_CD, %al
movw $SCHBOOT_MEDIA_OFF, %di
movb $SCHBOOT_MEDIA_CD, %al
movb %al, %es:(%di)
# stage2.S expects zeroed segments and an inherited stack, same as it gets

View file

@ -34,7 +34,7 @@ _Static_assert(0x7E00 + STAGE2_SECTORS * 512 <= 0x10000,
// by stage2's own tail end. 0x30000 is clear of stage2, the multiboot
// kernel-search buffer (0x10000-0x14000), and the kernel's load address
// (0x100000).
#define VBE_INFO_ADDR 0x30000
#include "../../include/schboot.h" /* the hand-off block */
// Fixed low-memory scratch byte, read by kernel/core/main.c's kmain() before
// any other init: 1 if the user picked "verbose" from the boot menu. Placed
@ -44,21 +44,16 @@ _Static_assert(0x7E00 + STAGE2_SECTORS * 512 <= 0x10000,
// perspective (verbose is read before startothers() runs), so it wasn't an
// active bug, but reusing a documented-elsewhere address is exactly the
// kind of thing that turns into one later.
#define BOOT_VERBOSE_FLAG_ADDR (VBE_INFO_ADDR + 0x100)
// Set by boot/bios/cdboot.S when we were booted from optical media. One byte past
// the verbose flag, for the same reason that one sits where it does: clear of
// the 256-byte VBE mode info block at VBE_INFO_ADDR.
#define BOOT_MEDIA_FLAG_ADDR (VBE_INFO_ADDR + 0x101)
#define BOOT_MEDIA_CD 1
// the 256-byte VBE mode info block at SCHBOOT_VBE_INFO.
// E820 memory map, collected by stage2.S in real mode (the same firmware
// interface GRUB and Linux's boot stub use). Count at +0x200, then 24-byte
// entries: {uint64 base; uint64 length; uint32 type; uint32 acpi_ext}.
// The kernel can read the same addresses later; the bootloader uses it to
// report detected RAM like any grown-up loader does.
#define E820_COUNT_ADDR (VBE_INFO_ADDR + 0x200)
#define E820_MAP_ADDR (VBE_INFO_ADDR + 0x208)
#define E820_USABLE 1
// NOT include/types.h's uint64: that's `unsigned long`, which is 4 bytes
@ -76,8 +71,8 @@ struct e820entry {
static uint32
e820_usable_mb(void)
{
uint32 count = *(uint32*)E820_COUNT_ADDR;
struct e820entry *e = (struct e820entry*)E820_MAP_ADDR;
uint32 count = *(uint32*)SCHBOOT_E820_COUNT;
struct e820entry *e = (struct e820entry*)SCHBOOT_E820_MAP;
e820_u64 total = 0;
if (count > 32) return 0; // garbage count: BIOS didn't fill the map
for (uint32 i = 0; i < count; i++)
@ -142,11 +137,11 @@ static int g_have_fb;
static void
fb_init(void)
{
g_stride = *(ushort*)(VBE_INFO_ADDR + 16);
g_width = *(ushort*)(VBE_INFO_ADDR + 18);
g_height = *(ushort*)(VBE_INFO_ADDR + 20);
uchar bpp = *(uchar*)(VBE_INFO_ADDR + 25);
uint32 fbaddr = *(uint32*)(VBE_INFO_ADDR + 40);
g_stride = *(ushort*)(SCHBOOT_VBE_INFO + 16);
g_width = *(ushort*)(SCHBOOT_VBE_INFO + 18);
g_height = *(ushort*)(SCHBOOT_VBE_INFO + 20);
uchar bpp = *(uchar*)(SCHBOOT_VBE_INFO + 25);
uint32 fbaddr = *(uint32*)(SCHBOOT_VBE_INFO + 40);
g_fb = (uint8*)fbaddr;
g_have_fb = (bpp == 32 && fbaddr && g_width && g_height);
}
@ -587,7 +582,7 @@ stage2main(void)
// Sort out where the kernel is coming from before anything is drawn, so a
// disc we cannot read says so on the splash instead of failing silently
// several seconds later in the middle of the progress bar.
if (*(uchar*)BOOT_MEDIA_FLAG_ADDR == BOOT_MEDIA_CD) {
if (*(uchar*)SCHBOOT_MEDIA_FLAG == SCHBOOT_MEDIA_CD) {
if (atapi_find())
g_cd_base512 = atapi_boot_base() * (CD_SECTSIZE / SECTSIZE);
g_from_cd = (g_atapi_io != 0 && g_cd_base512 != 0);
@ -601,7 +596,7 @@ stage2main(void)
if (wait_for_f8(3000000))
verbose = boot_menu();
*(uchar*)BOOT_VERBOSE_FLAG_ADDR = verbose;
*(uchar*)SCHBOOT_VERBOSE_FLAG = verbose;
if (g_have_fb) {
fill_rect(0, 0, g_width, g_height, SCH_BG);
@ -615,7 +610,7 @@ stage2main(void)
// legible reason is the only useful thing left to do. This is drawn after
// the clear above on purpose: an earlier version put it on the splash,
// where it was wiped a moment later and nobody ever saw it.
if (*(uchar*)BOOT_MEDIA_FLAG_ADDR == BOOT_MEDIA_CD && !g_from_cd) {
if (*(uchar*)SCHBOOT_MEDIA_FLAG == SCHBOOT_MEDIA_CD && !g_from_cd) {
draw_centered(g_height / 2, "No readable boot disc found.", SCH_ACCENT);
draw_centered(g_height / 2 + 20, "The drive did not answer ATAPI.", SCH_DIM);
for (;;)
@ -660,7 +655,7 @@ stage2main(void)
// broke it silently (kernel received garbage instead of the real
// LFB address). Pin it down explicitly instead of hoping.
{
uint32 fbaddr = *(uint32*)(VBE_INFO_ADDR + 40);
uint32 fbaddr = *(uint32*)(SCHBOOT_VBE_INFO + 40);
uint32 entry = hdr->entry_addr;
asm volatile("jmp *%0" : : "r"(entry), "a"(fbaddr));
}

View file

@ -27,13 +27,9 @@
#include "efi.h"
#include "../../include/schboot.h"
// Where the kernel expects to find things. These are the BIOS loader's
// addresses and they are deliberately unchanged: the kernel should not be able
// to tell which firmware booted it.
#define VBE_INFO_ADDR 0x30000
#define E820_COUNT_ADDR (VBE_INFO_ADDR + 0x200)
#define E820_MAP_ADDR (VBE_INFO_ADDR + 0x208)
#define E820_MAXENTRIES 32
// Where the kernel expects to find things comes from schboot.h, included
// above: the hand-off is one contract and both loaders write the same one.
#define E820_MAXENTRIES SCHBOOT_E820_MAX
#define E820_USABLE 1
#define E820_RESERVED 2
#define E820_ACPI 3
@ -438,7 +434,7 @@ setup_framebuffer(void)
// block as the BIOS path leaves it, and the kernel takes exactly four
// fields out of it -- so those four are what get written, at the offsets
// it uses.
UINT8 *vbe = (UINT8 *)VBE_INFO_ADDR;
UINT8 *vbe = (UINT8 *)SCHBOOT_VBE_INFO;
zero(vbe, 512);
UINT32 bpp = 32; // GOP's 8-bit formats are 32bpp
@ -612,8 +608,8 @@ EfiMain(EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
__asm__ volatile("cli");
copy((void *)E820_MAP_ADDR, e820, nent * sizeof e820[0]);
*(UINT32 *)E820_COUNT_ADDR = nent;
copy((void *)SCHBOOT_E820_MAP, e820, nent * sizeof e820[0]);
*(UINT32 *)SCHBOOT_E820_COUNT = nent;
*(UINT64 *)SCHBOOT_RSDP_ADDR = rsdp;
// The page tables mboot_entry would have built. entry64low's first act is

View file

@ -23,6 +23,56 @@
#define SCH64_MAGIC0 0x36484353 /* "SCH6" */
#define SCH64_MAGIC1 0x4f4f4234 /* "4BOO" */
// ---------------------------------------------------------------------------
// The hand-off block.
//
// One page of low memory that a loader fills in and the kernel reads. Every
// field below was previously #defined in whichever file happened to need it --
// five of them, across two loaders and two kernel files -- which is the same
// hazard as defining the magic twice, only quieter: nothing breaks until one
// copy moves.
//
// 0x30000 is clear of stage2 (loaded at 0x7E00 and well under 0xA000), the
// multiboot kernel-search buffer at 0x10000-0x14000, and the kernel's own load
// address at 0x100000.
//
// Both loaders write it, so a field's meaning must not depend on which one did.
// Where they genuinely differ -- the BIOS path knows what medium it booted
// from, UEFI does not -- the field is defined as "0 when unknown" rather than
// left for the reader to guess.
#define SCHBOOT_BASE 0x30000
// A VBE mode info block, 256 bytes. The UEFI loader fills the same four fields
// out of GOP rather than translating a structure it never saw, so the kernel
// cannot tell which firmware booted it.
#define SCHBOOT_VBE_INFO (SCHBOOT_BASE + 0x000)
#define SCHBOOT_VBE_STRIDE (SCHBOOT_VBE_INFO + 16) /* uint16, bytes/line */
#define SCHBOOT_VBE_WIDTH (SCHBOOT_VBE_INFO + 18) /* uint16 */
#define SCHBOOT_VBE_HEIGHT (SCHBOOT_VBE_INFO + 20) /* uint16 */
#define SCHBOOT_VBE_BPP (SCHBOOT_VBE_INFO + 25) /* uint8 */
#define SCHBOOT_VBE_LFB (SCHBOOT_VBE_INFO + 40) /* uint32, physical */
// Non-zero if the loader was asked for a verbose boot.
#define SCHBOOT_VERBOSE_FLAG (SCHBOOT_BASE + 0x100)
// What the system booted from. SCHBOOT_MEDIA_CD matters because the ISO path
// reads through ATAPI rather than ATA; 0 means "not stated", which is what the
// UEFI loader leaves, since it does not read the kernel through either.
#define SCHBOOT_MEDIA_FLAG (SCHBOOT_BASE + 0x101)
#define SCHBOOT_MEDIA_CD 1
// Real-mode spelling of the same byte, for cdboot.S which has no 32-bit
// addressing yet. Segment and offset, not a flat address.
#define SCHBOOT_MEDIA_SEG 0x3000
#define SCHBOOT_MEDIA_OFF 0x101
// The E820 memory map: a uint32 count, then that many 24-byte entries. Capped
// at 32 so a pathological firmware cannot run past the buffer; the map ends at
// 0x508, which is what leaves 0x600 free below.
#define SCHBOOT_E820_COUNT (SCHBOOT_BASE + 0x200)
#define SCHBOOT_E820_MAP (SCHBOOT_BASE + 0x208)
#define SCHBOOT_E820_MAX 32
// Where the loader leaves the ACPI RSDP, as a little-endian 64-bit physical
// address. Zero means "not provided, go and look for it yourself".
//
@ -33,10 +83,7 @@
// the machine boots to a prompt on polled serial output and then ignores every
// key, which looks like a broken console rather than a missing table.
//
// 0x30000 is the same low-memory hand-off block the BIOS loader uses:
// +0x100 verbose flag, +0x200 e820 count, +0x208 the e820 map (32 entries of
// 24 bytes, ending at 0x508). 0x600 is clear of all of it.
#define SCHBOOT_RSDP_ADDR (0x30000 + 0x600)
#define SCHBOOT_RSDP_ADDR (SCHBOOT_BASE + 0x600)
#ifndef __ASSEMBLER__

View file

@ -23,13 +23,10 @@
// than in some new way.
uint64 phystop = PHYSTOP_FALLBACK;
// Where stage2 left the map. Must match E820_COUNT_ADDR / E820_MAP_ADDR in
// boot/bios/stage2main.c, which are themselves relative to VBE_INFO_ADDR.
#define E820_COUNT_ADDR (0x30000 + 0x200)
#define E820_MAP_ADDR (0x30000 + 0x208)
#include "schboot.h" /* the hand-off block */
#define E820_USABLE 1
#define E820_MAXENTRIES MEMREGION_MAX
#define E820_MAXENTRIES SCHBOOT_E820_MAX
// Every usable region, page aligned inwards and clipped to PHYSTOP_MAX.
// kinit1() frees these; see include/memlayout.h for why a list and not just a
@ -59,8 +56,8 @@ struct e820entry {
void
e820_init(void)
{
uint32 count = *(uint32 *)P2V(E820_COUNT_ADDR);
struct e820entry *e = (struct e820entry *)P2V(E820_MAP_ADDR);
uint32 count = *(uint32 *)P2V(SCHBOOT_E820_COUNT);
struct e820entry *e = (struct e820entry *)P2V(SCHBOOT_E820_MAP);
// A count of zero means the firmware declined; anything absurd means we are
// reading something that is not a memory map. Either way the constant is the

View file

@ -12,15 +12,15 @@ static void startothers(void);
static void mpmain(void) __attribute__((noreturn));
extern char end[]; // first address after kernel loaded from ELF file
// Set by stage2main.c (boot/bios/stage2main.c, BOOT_VERBOSE_FLAG_ADDR) when the
// Set by stage2main.c (boot/bios/stage2main.c, SCHBOOT_VERBOSE_FLAG) when the
// user picks "Boot SchOS (verbose)" from the F8 boot menu; read once here,
// before anything else, so subsystem init can log its own steps.
int boot_verbose;
// Must match BOOT_VERBOSE_FLAG_ADDR in boot/bios/stage2main.c exactly: just past
// Must match SCHBOOT_VERBOSE_FLAG in boot/bios/stage2main.c exactly: just past
// the VBE info block, not 0x7000 (startothers() below uses that address for
// the AP boot trampoline).
#define BOOT_VERBOSE_FLAG_ADDR (0x30000 + 0x100)
#include "schboot.h"
// Bootstrap processor starts running C code here.
// Allocate a real stack and switch to it, first
@ -28,7 +28,7 @@ int boot_verbose;
int
kmain(uint32 fb_addr)
{
boot_verbose = *(uchar*)P2V(BOOT_VERBOSE_FLAG_ADDR);
boot_verbose = *(uchar*)P2V(SCHBOOT_VERBOSE_FLAG);
uartearlyinit();
if (boot_verbose) cprintf("verbose boot: uartearlyinit done\n");

View file

@ -40,23 +40,23 @@ vga_set_bg(uint32 color)
}
// Written by boot/bios/stage2.S's VBE call and read by boot/bios/stage2main.c under
// the same name (VBE_INFO_ADDR) — not 0x9000, since stage2's own code now
// the same name (SCHBOOT_VBE_INFO) — not 0x9000, since stage2's own code now
// runs up to nearly 0x9000 once the boot-menu/progress-bar logic is
// included, which would otherwise clobber a mode info block placed there.
#define VBE_INFO_ADDR 0x30000
#include "schboot.h" /* the hand-off block */
void
vga_init(uint64 phys_addr) {
if (phys_addr == 0) {
phys_addr = *(uint32*)(P2V(VBE_INFO_ADDR) + 40);
phys_addr = *(uint32*)(P2V(SCHBOOT_VBE_LFB));
}
vga_mem = (volatile uchar*)P2V(phys_addr);
vga_phys = phys_addr;
vga_stride = *(ushort*)(P2V(VBE_INFO_ADDR) + 16);
vga_width = *(ushort*)(P2V(VBE_INFO_ADDR) + 18);
vga_height = *(ushort*)(P2V(VBE_INFO_ADDR) + 20);
vga_bpp = *(uchar*)(P2V(VBE_INFO_ADDR) + 25);
vga_stride = *(ushort*)(P2V(SCHBOOT_VBE_STRIDE));
vga_width = *(ushort*)(P2V(SCHBOOT_VBE_WIDTH));
vga_height = *(ushort*)(P2V(SCHBOOT_VBE_HEIGHT));
vga_bpp = *(uchar*)(P2V(SCHBOOT_VBE_BPP));
if (vga_width == 0) vga_width = 800;
if (vga_height == 0) vga_height = 600;