This commit is contained in:
Alvin-Zilverstand committed 2026-10-08 23:36:34 +02:00
1 parent 78dd619293
commit 6dc841f956
109 files changed
+18192 -1907

No files matched your search

+193
View File
@@ -0,0 +1,193 @@
/* Build a small freestanding C program into bob64's versioned B64E format. */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#ifdef _WIN32
#include <direct.h>
#define make_dir(path) _mkdir(path)
#define APP_EXE ".exe"
#else
#include <sys/stat.h>
#define make_dir(path) mkdir(path,0777)
#define APP_EXE ""
#endif
#define APP_BASE 0x0000004000000000ULL
#define APP_LIMIT (16ULL*1024ULL*1024ULL)
#define PAGE_SIZE 4096u
#define HEADER_SIZE 64u
#define APP_ABI_VERSION 1u
#define EXEC_VERSION 1u
static uint16_t read16(const unsigned char *p) {
return (uint16_t)(p[0]|((uint16_t)p[1]<<8));
}
static uint32_t read32(const unsigned char *p) {
return (uint32_t)p[0]|((uint32_t)p[1]<<8)|((uint32_t)p[2]<<16)|((uint32_t)p[3]<<24);
}
static uint64_t read64(const unsigned char *p) {
return (uint64_t)read32(p)|((uint64_t)read32(p+4)<<32);
}
static void write16(unsigned char *p,uint16_t v) {
p[0]=(unsigned char)v;p[1]=(unsigned char)(v>>8);
}
static void write32(unsigned char *p,uint32_t v) {
for(unsigned i=0;i<4;i++)p[i]=(unsigned char)(v>>(i*8));
}
static void write64(unsigned char *p,uint64_t v) {
write32(p,(uint32_t)v);write32(p+4,(uint32_t)(v>>32));
}
static uint32_t crc32(const unsigned char *p,size_t n) {
uint32_t value=0xffffffffu;
for(size_t i=0;i<n;i++) {
value^=p[i];
for(unsigned bit=0;bit<8;bit++)
value=(value>>1)^(0xedb88320u&-(value&1u));
}
return ~value;
}
static unsigned char *read_file(const char *path,size_t *length) {
FILE *f=fopen(path,"rb");
unsigned char *bytes;
long size;
if(!f){perror(path);return NULL;}
if(fseek(f,0,SEEK_END)||(size=ftell(f))<0||size>64*1024*1024L||
fseek(f,0,SEEK_SET)) {fclose(f);fprintf(stderr,"bob64cc: invalid input size\n");return NULL;}
bytes=(unsigned char *)malloc((size_t)size? (size_t)size:1);
if(!bytes){fclose(f);fprintf(stderr,"bob64cc: out of memory\n");return NULL;}
if(fread(bytes,1,(size_t)size,f)!=(size_t)size||fclose(f)) {
free(bytes);fprintf(stderr,"bob64cc: cannot read %s\n",path);return NULL;
}
*length=(size_t)size;return bytes;
}
static int write_b64e(const char *path,const unsigned char *pe,size_t pe_size,
const unsigned char *payload,size_t payload_size) {
uint32_t pe_offset,entry_rva,section_alignment,size_of_image;
uint64_t image_base,lowest_rva=UINT64_MAX,highest_virtual=0,code_end=0;
uint64_t entry_offset,code_size,memory_size;
uint16_t section_count,optional_size;
size_t section_table;
int executable_at_lowest=0;
unsigned char *image;
FILE *out;
if(pe_size<64||pe[0]!='M'||pe[1]!='Z')goto bad_pe;
pe_offset=read32(pe+0x3c);
if((uint64_t)pe_offset+24>pe_size||memcmp(pe+pe_offset,"PE\0\0",4))goto bad_pe;
const unsigned char *coff=pe+pe_offset+4;
if(read16(coff)!=0x8664)goto bad_pe;
section_count=read16(coff+2);optional_size=read16(coff+16);
if(!section_count||section_count>32||optional_size<112||
(uint64_t)pe_offset+24+optional_size>pe_size)goto bad_pe;
const unsigned char *optional=coff+20;
if(read16(optional)!=0x20b)goto bad_pe;
entry_rva=read32(optional+16);image_base=read64(optional+24);
section_alignment=read32(optional+32);size_of_image=read32(optional+56);
if(section_alignment!=PAGE_SIZE||!entry_rva||
!size_of_image||size_of_image>APP_LIMIT+PAGE_SIZE)goto bad_pe;
if((read16(optional+70)&0x40u)||optional_size<160||
read32(optional+112+5*8)||read32(optional+112+5*8+4))goto bad_pe;
section_table=(size_t)pe_offset+24+optional_size;
if(section_count>(pe_size-section_table)/40)goto bad_pe;
for(uint16_t i=0;i<section_count;i++) {
const unsigned char *section=pe+section_table+(size_t)i*40;
uint32_t virtual_size=read32(section+8),rva=read32(section+12);
uint32_t raw_size=read32(section+16),raw_offset=read32(section+20);
uint32_t characteristics=read32(section+36);
uint64_t span=virtual_size>raw_size?virtual_size:raw_size;
if((uint64_t)rva+span>APP_LIMIT+PAGE_SIZE||
(raw_size&&(uint64_t)raw_offset+raw_size>pe_size))goto bad_pe;
if(span&&rva<lowest_rva)lowest_rva=rva;
if(span&&(uint64_t)rva+virtual_size>highest_virtual)
highest_virtual=(uint64_t)rva+virtual_size;
if(characteristics&0x20000000u) {
if(rva<lowest_rva)lowest_rva=rva;
if((uint64_t)rva+span>code_end)code_end=(uint64_t)rva+span;
if(rva==lowest_rva)executable_at_lowest=1;
}
}
if(lowest_rva==UINT64_MAX||image_base>UINT64_MAX-lowest_rva||
image_base+lowest_rva!=APP_BASE||
!executable_at_lowest||entry_rva<lowest_rva||
entry_rva>=code_end||code_end<=lowest_rva)goto bad_pe;
entry_offset=(uint64_t)entry_rva-lowest_rva;
code_size=(code_end-lowest_rva+PAGE_SIZE-1)&~(uint64_t)(PAGE_SIZE-1);
memory_size=(highest_virtual-lowest_rva+PAGE_SIZE-1)&~(uint64_t)(PAGE_SIZE-1);
if(payload_size>memory_size)memory_size=(payload_size+PAGE_SIZE-1)&~(uint64_t)(PAGE_SIZE-1);
if(!code_size||code_size>payload_size||payload_size>APP_LIMIT||
memory_size>APP_LIMIT||entry_offset>=code_size)goto bad_pe;
image=(unsigned char *)calloc(1,HEADER_SIZE+payload_size);
if(!image){fprintf(stderr,"bob64cc: out of memory\n");return -1;}
image[0]='B';image[1]='6';image[2]='4';image[3]='E';
write16(image+4,EXEC_VERSION);write16(image+6,HEADER_SIZE);
write32(image+8,APP_ABI_VERSION);write64(image+16,payload_size);
write64(image+24,memory_size);write64(image+32,entry_offset);
write64(image+40,code_size);memcpy(image+HEADER_SIZE,payload,payload_size);
write32(image+48,crc32(image+HEADER_SIZE,payload_size));
out=fopen(path,"wb");
if(!out){perror(path);free(image);return -1;}
int failed=fwrite(image,1,HEADER_SIZE+payload_size,out)!=HEADER_SIZE+payload_size;
if(fclose(out))failed=1;
free(image);
if(failed){fprintf(stderr,"bob64cc: failed writing %s\n",path);return -1;}
printf("Built B64E v1: entry=0x%llx code=%llu file=%llu memory=%llu bytes\n",
(unsigned long long)entry_offset,(unsigned long long)code_size,
(unsigned long long)payload_size,(unsigned long long)memory_size);
return 0;
bad_pe:
fprintf(stderr,"bob64cc: compiler output is not a supported bob64 app image\n");
return -1;
}
static int quote(char *out,size_t capacity,const char *value) {
size_t length=strlen(value);
if(length+3>capacity||strchr(value,'"'))return -1;
out[0]='"';memcpy(out+1,value,length);out[length+1]='"';out[length+2]=0;
return 0;
}
int main(int argc,char **argv) {
const char *compiler=getenv("BOB64_CC"),*objcopy=getenv("BOB64_OBJCOPY");
char q_source[4096],q_output[4096];
char command[16384],exe_path[256],bin_path[256];
size_t pe_size=0,payload_size=0;
unsigned char *pe=NULL,*payload=NULL;
int result=1;
if(argc!=3) {
fprintf(stderr,"Usage: bob64cc SOURCE.c OUTPUT.b64e\n");return 2;
}
if(!strcmp(argv[1],argv[2])) {
fprintf(stderr,"bob64cc: source and output paths must be different\n");return 2;
}
if(!compiler||!*compiler)compiler="x86_64-w64-mingw32-gcc";
if(!objcopy||!*objcopy)objcopy="objcopy";
if(quote(q_source,sizeof(q_source),argv[1])||
quote(q_output,sizeof(q_output),argv[2])||strchr(compiler,'"')||
strchr(objcopy,'"')) {
fprintf(stderr,"bob64cc: invalid or overly long path\n");return 2;
}
if(make_dir("build")&&errno!=EEXIST) {perror("build");return 1;}
if(make_dir("build/bob64-app")&&errno!=EEXIST) {perror("build/bob64-app");return 1;}
snprintf(exe_path,sizeof(exe_path),"build/bob64-app/image%s",APP_EXE);
snprintf(bin_path,sizeof(bin_path),"build/bob64-app/image.bin");
char q_exe[512],q_bin[512],q_entry[512];
if(quote(q_exe,sizeof(q_exe),exe_path)||quote(q_bin,sizeof(q_bin),bin_path)||
quote(q_entry,sizeof(q_entry),"bob64/app_entry.S")) {
fprintf(stderr,"bob64cc: temporary path too long\n");return 2;
}
snprintf(command,sizeof(command),
"%s -I. -DBOB64_UEFI_ABI -std=c11 -O2 -Wall -Wextra -Werror -ffreestanding "
"-fno-builtin -fno-stack-protector -fno-stack-check -fno-unwind-tables "
"-fno-asynchronous-unwind-tables -fPIC -mno-red-zone -nostdlib bob64/libc.c %s %s "
"-Wl,--image-base,0x3ffffff000 -Wl,--disable-dynamicbase "
"-Wl,--disable-reloc-section -Wl,-e,bob64_app_entry -o %s",
compiler,q_entry,q_source,q_exe);
if(system(command)!=0)goto done;
snprintf(command,sizeof(command),"%s -O binary %s %s",objcopy,q_exe,q_bin);
if(system(command)!=0)goto done;
pe=read_file(exe_path,&pe_size);payload=read_file(bin_path,&payload_size);
if(!pe||!payload||write_b64e(argv[2],pe,pe_size,payload,payload_size))goto done;
result=0;
done:
free(pe);free(payload);remove(exe_path);remove(bin_path);
return result;
}
+2 -1
View File
@@ -1315,12 +1315,13 @@ static void expr(Node *n) {
else if(!strcmp(n->name,"bob_run_image")){vector=12;expected=1;}
else if(!strcmp(n->name,"bob_run_native")){vector=13;expected=1;}
else if(!strcmp(n->name,"bob_import_image")){vector=14;expected=1;}
else if(!strcmp(n->name,"bob_os_service") || !strcmp(n->name,"bob_fs_register")){vector=15;expected=1;}
else if(!strcmp(n->name,"bob_key"))vector=8;
else if(!strcmp(n->name,"bob_terminal"))vector=9;
else if(!strcmp(n->name,"bob_columns"))vector=10;
else if(!strcmp(n->name,"bob_rows"))vector=11;
if(vector>=0) {if(count!=expected)error("wrong argument count for %s",n->name);
int first_type=(vector==2 || vector==12)?pointer_to_const(4):vector==3?pointer_to(4):vector==7 || vector==13 || vector==14?pointer_to(0):0;
int first_type=(vector==2 || vector==12)?pointer_to_const(4):vector==3?pointer_to(4):vector==7 || vector==13 || vector==14 || vector==15?pointer_to(0):0;
if(count)assignment_compatible(first_type,n->a);
if(expected==2)assignment_compatible(0,n->a->next);
if(expected==2){expr(n->a->next);push(0);expr(n->a);pop(1);}else if(count)expr(n->a);
+56 -2
View File
@@ -8,11 +8,15 @@
#include <direct.h>
#define EXE ".exe"
#define BUILD_DIR "build\\"
#define BOB64_BOOT_PATH "build\\bob64\\EFI\\BOOT\\BOOTX64.EFI"
#define BOB64_HANDOFF_BOOT_PATH "build\\bob64-handoff\\EFI\\BOOT\\BOOTX64.EFI"
#define BOB_COMMAND "bob.exe"
#define BOB32_COMMAND "bob32.exe"
#else
#define EXE ""
#define BUILD_DIR "./build/"
#define BOB64_BOOT_PATH "build/bob64/EFI/BOOT/BOOTX64.EFI"
#define BOB64_HANDOFF_BOOT_PATH "build/bob64-handoff/EFI/BOOT/BOOTX64.EFI"
#define BOB_COMMAND "./bob"
#define BOB32_COMMAND "./bob32"
#endif
@@ -20,9 +24,50 @@
static void run(const char *command) {
if(system(command)!=0){fprintf(stderr,"Build failed: %s\n",command);exit(1);}
}
static void ensure_directory(const char *path) {
#ifdef _WIN32
if(_mkdir(path) && errno!=EEXIST){perror(path);exit(1);}
#else
if(mkdir(path,0777) && errno!=EEXIST){perror(path);exit(1);}
#endif
}
static void build_bob64(int handoff) {
const char *compiler=getenv("BOB64_CC");
const char *output_path=handoff?BOB64_HANDOFF_BOOT_PATH:BOB64_BOOT_PATH;
const char *handoff_define=handoff?"-DBOB64_ENABLE_HANDOFF ":"";
char command[2048];
ensure_directory("build");
ensure_directory("build/bob64-app");
run("gcc -std=c11 -O2 -Wall -Wextra -Werror tools/bob64cc.c -o build/bob64cc" EXE);
run(BUILD_DIR "bob64cc" EXE " apps/bob64_smoke.c build/bob64-app/smoke.b64e");
run(BUILD_DIR "bob64cc" EXE " apps/bob64_display.c build/bob64-app/display.b64e");
run(BUILD_DIR "bob64cc" EXE " apps/bob64_gui.c build/bob64-app/gui.b64e");
ensure_directory(handoff?"build/bob64-handoff":"build/bob64");
if(handoff) {
ensure_directory("build/bob64-handoff/EFI");
ensure_directory("build/bob64-handoff/EFI/BOOT");
} else {
ensure_directory("build/bob64/EFI");
ensure_directory("build/bob64/EFI/BOOT");
}
if(!compiler || !*compiler) {
#ifdef _WIN32
compiler="gcc";
#else
compiler="x86_64-w64-mingw32-gcc";
#endif
}
if(snprintf(command,sizeof(command),"%s -std=c11 -O2 -Wall -Wextra -Werror -DBOB64_UEFI_ABI %s-ffreestanding -fno-stack-protector -fno-stack-check -fshort-wchar -mno-red-zone -nostdlib -Wl,--subsystem,10 -Wl,-e,efi_main -Wl,--image-base,0x100000 -Wl,--enable-reloc-section -Wl,--dynamicbase -Wl,--file-alignment,0x200 -Wl,--section-alignment,0x1000 bob64/boot.c bob64/memory.c bob64/paging.c bob64/cpu.c bob64/descriptors.c bob64/interrupts.c bob64/interrupts.S bob64/syscall.c bob64/window_server.c bob64/bootstrap.c bob64/kernel.c bob64/console.c bob64/heap.c bob64/keyboard.c bob64/mouse.c bob64/shell.c bob64/filesystem.c bob64/snapshot.c bob64/exec.c bob64/process.c bob64/compiler.c bob64/embedded_app.S bob64/handoff.S -o %s",compiler,handoff_define,output_path)>=(int)sizeof(command)) {
fprintf(stderr,"bob64 compiler command is too long\n");exit(1);
}
run(command);
}
int main(int argc,char **argv) {
if(argc>2||(argc==2&&strcmp(argv[1],"--run")&&strcmp(argv[1],"--run32")&&strcmp(argv[1],"--test"))) {
fprintf(stderr,"Usage: build [--run | --run32 | --test] (from repository root)\n");return 1;
int bob64Handoff=argc==2&&(!strcmp(argv[1],"--bob64-handoff")||!strcmp(argv[1],"--bob64-handoff-test"));
int bob64Only=argc==2&&(!strcmp(argv[1],"--bob64")||!strcmp(argv[1],"--bob64-test")||bob64Handoff);
int bob64Test=argc==2&&(!strcmp(argv[1],"--bob64-test")||!strcmp(argv[1],"--bob64-handoff-test"));
if(argc>2||(argc==2&&!bob64Only&&strcmp(argv[1],"--run")&&strcmp(argv[1],"--run32")&&strcmp(argv[1],"--test"))) {
fprintf(stderr,"Usage: build [--run | --run32 | --test | --bob64 | --bob64-test | --bob64-handoff | --bob64-handoff-test] (from repository root)\n");return 1;
}
#ifdef _WIN32
int directory_result=_mkdir("build");
@@ -40,6 +85,15 @@ int main(int argc,char **argv) {
if(!is_directory){
fprintf(stderr,"build must be a directory\n");return 1;
}
if(bob64Only) {
build_bob64(bob64Handoff);
if(bob64Test) {
run("gcc -std=c11 -O2 -Wall -Wextra -Werror tests/test_bob64.c bob64/libc.c bob64/compiler.c bob64/memory.c bob64/paging.c bob64/cpu.c bob64/descriptors.c bob64/interrupts.c bob64/interrupts.S bob64/syscall.c bob64/window_server.c bob64/bootstrap.c bob64/console.c bob64/heap.c bob64/keyboard.c bob64/mouse.c bob64/shell.c bob64/filesystem.c bob64/snapshot.c bob64/exec.c bob64/process.c -o build/test_bob64" EXE);
if(bob64Handoff) run(BUILD_DIR "test_bob64" EXE " " BOB64_HANDOFF_BOOT_PATH " build/bob64-app/smoke.b64e build/bob64-app/display.b64e build/bob64-app/gui.b64e");
else run(BUILD_DIR "test_bob64" EXE " " BOB64_BOOT_PATH " build/bob64-app/smoke.b64e build/bob64-app/display.b64e build/bob64-app/gui.b64e");
}
return 0;
}
run("gcc -std=gnu11 -O2 -Wall -Wextra -Werror main.c -o bob" EXE);
run("gcc -std=gnu11 -O2 -Wall -Wextra -Werror main.c -o bob32" EXE);
run("gcc -std=c11 -O2 -Wall -Wextra -Werror tools/bobcc.c -o build/bobcc" EXE);
+104
View File
@@ -0,0 +1,104 @@
param(
[Parameter(Mandatory = $true)]
[string]$QemuPath,
[Parameter(Mandatory = $true)]
[string]$OvmfCodePath,
[string]$ImageRoot = (Join-Path (Split-Path -Parent $PSScriptRoot) 'build\bob64-handoff'),
[int]$TimeoutSeconds = 45
)
$ErrorActionPreference = 'Stop'
$requiredMarkers = @(
'bob64: long mode active',
'bob64: exiting UEFI boot services',
'bob64 kernel: ring-3 bob! app passed',
'bob64 kernel: native streaming app passed',
'bob64 kernel: read-only code protection passed',
'bob64 kernel: non-executable data protection passed',
'bob64 kernel: memory checks passed',
'bob64 kernel: timer IRQ enabled',
'bob64 kernel: keyboard IRQ enabled',
'bob64 kernel: PS/2 mouse IRQ enabled',
'bob64! kernel shell; type help'
)
foreach ($path in @($QemuPath, $OvmfCodePath, $ImageRoot)) {
if (-not (Test-Path -LiteralPath $path)) {
throw "Required QEMU runtime path does not exist: $path"
}
}
$efiPath = Join-Path $ImageRoot 'EFI\BOOT\BOOTX64.EFI'
if (-not (Test-Path -LiteralPath $efiPath)) {
throw "The handoff image is missing: $efiPath"
}
$qemuDirectory = Split-Path -Parent (Resolve-Path -LiteralPath $QemuPath).Path
$shareDirectory = Join-Path $qemuDirectory 'share'
if (-not (Test-Path -LiteralPath $shareDirectory -PathType Container)) {
throw "QEMU's share directory does not exist: $shareDirectory"
}
$imagePath = (Resolve-Path -LiteralPath $ImageRoot).Path.Replace('\', '/')
$firmwarePath = (Resolve-Path -LiteralPath $OvmfCodePath).Path
$serialPath = Join-Path ([IO.Path]::GetTempPath()) ("bob64-qemu-{0}.log" -f [guid]::NewGuid())
$portProbe = [Net.Sockets.TcpListener]::new([Net.IPAddress]::Loopback, 0)
$portProbe.Start()
$monitorPort = ([Net.IPEndPoint]$portProbe.LocalEndpoint).Port
$portProbe.Stop()
$arguments = @(
'-machine', 'q35', '-m', '512M', '-cpu', 'max', '-L', $shareDirectory,
'-drive', "if=pflash,format=raw,unit=0,file=$firmwarePath,readonly=on",
'-drive', "format=raw,file=fat:rw:$imagePath",
'-boot', 'order=c', '-display', 'none', '-serial', "file:$serialPath",
'-monitor', "tcp:127.0.0.1:$monitorPort,server=on,wait=off", '-no-reboot'
)
$startInfo = [Diagnostics.ProcessStartInfo]::new()
$startInfo.FileName = (Resolve-Path -LiteralPath $QemuPath).Path
$startInfo.UseShellExecute = $false
$startInfo.CreateNoWindow = $true
$startInfo.Arguments = (($arguments | ForEach-Object {
'"' + ([string]$_).Replace('"', '\"') + '"'
}) -join ' ')
$process = $null
try {
$process = [Diagnostics.Process]::Start($startInfo)
$deadline = [DateTime]::UtcNow.AddSeconds($TimeoutSeconds)
$serialText = ''
while ([DateTime]::UtcNow -lt $deadline) {
if (Test-Path -LiteralPath $serialPath) {
$serialText = [string](Get-Content -LiteralPath $serialPath -Raw)
$allPresent = $true
foreach ($marker in $requiredMarkers) {
if ([string]::IsNullOrEmpty($serialText) -or -not $serialText.Contains($marker)) {
$allPresent = $false
break
}
}
if ($allPresent) {
Write-Output 'UEFI runtime checks passed:'
foreach ($marker in $requiredMarkers) {
Write-Output " $marker"
}
exit 0
}
}
if ($process.HasExited) {
throw "QEMU exited before the kernel reached its shell (exit code $($process.ExitCode)).`n$serialText"
}
Start-Sleep -Milliseconds 250
}
throw "Timed out waiting for the UEFI kernel smoke checks.`n$serialText"
}
finally {
if ($process -and -not $process.HasExited) {
$process.Kill()
$process.WaitForExit()
}
if (Test-Path -LiteralPath $serialPath) {
Remove-Item -LiteralPath $serialPath -Force
}
}