mirror of
https://github.com/Alvin-Zilverstand/bob16.git
synced 2026-11-23 13:22:59 +01:00
d
d
This commit is contained in:
1 parent
3d06f94e48
commit
57787bfc7f
59 files changed
+9967
-410
No files matched your search
+47
-11
@@ -10,6 +10,9 @@
|
||||
#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 BOB64_STORAGE_TEST_BOOT_PATH "build\\bob64-handoff-storage-test\\EFI\\BOOT\\BOOTX64.EFI"
|
||||
#define BOB64_STORAGE_ACTIVE_TEST_BOOT_PATH "build\\bob64-handoff-storage-active-test\\EFI\\BOOT\\BOOTX64.EFI"
|
||||
#define BOB64_STORAGE64_TEST_BOOT_PATH "build\\bob64-handoff-storage64-test\\EFI\\BOOT\\BOOTX64.EFI"
|
||||
#define BOB_COMMAND "bob.exe"
|
||||
#define BOB32_COMMAND "bob32.exe"
|
||||
#define BOB64_TEST_THREAD_FLAGS ""
|
||||
@@ -18,6 +21,9 @@
|
||||
#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 BOB64_STORAGE_TEST_BOOT_PATH "build/bob64-handoff-storage-test/EFI/BOOT/BOOTX64.EFI"
|
||||
#define BOB64_STORAGE_ACTIVE_TEST_BOOT_PATH "build/bob64-handoff-storage-active-test/EFI/BOOT/BOOTX64.EFI"
|
||||
#define BOB64_STORAGE64_TEST_BOOT_PATH "build/bob64-handoff-storage64-test/EFI/BOOT/BOOTX64.EFI"
|
||||
#define BOB_COMMAND "./bob"
|
||||
#define BOB32_COMMAND "./bob32"
|
||||
#define BOB64_TEST_THREAD_FLAGS "-pthread "
|
||||
@@ -33,10 +39,19 @@ static void ensure_directory(const char *path) {
|
||||
if(mkdir(path,0777) && errno!=EEXIST){perror(path);exit(1);}
|
||||
#endif
|
||||
}
|
||||
static void build_bob64(int handoff) {
|
||||
static void build_bob64(int handoff,int storage_write_test,int active_disconnect_test,
|
||||
int storage_lba64_test) {
|
||||
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 ":"";
|
||||
const char *output_path=storage_lba64_test?BOB64_STORAGE64_TEST_BOOT_PATH:
|
||||
(active_disconnect_test?BOB64_STORAGE_ACTIVE_TEST_BOOT_PATH:
|
||||
(storage_write_test?BOB64_STORAGE_TEST_BOOT_PATH:
|
||||
(handoff?BOB64_HANDOFF_BOOT_PATH:BOB64_BOOT_PATH)));
|
||||
const char *handoff_define=storage_lba64_test?
|
||||
"-DBOB64_ENABLE_HANDOFF -DBOB64_STORAGE_LBA64_TEST ":
|
||||
(active_disconnect_test?
|
||||
"-DBOB64_ENABLE_HANDOFF -DBOB64_STORAGE_ACTIVE_DISCONNECT_TEST ":
|
||||
(storage_write_test?"-DBOB64_ENABLE_HANDOFF -DBOB64_STORAGE_WRITE_TEST ":
|
||||
(handoff?"-DBOB64_ENABLE_HANDOFF ":"")));
|
||||
char command[2048];
|
||||
ensure_directory("build");
|
||||
ensure_directory("build/bob64-app");
|
||||
@@ -52,10 +67,27 @@ static void build_bob64(int handoff) {
|
||||
run(BUILD_DIR "bob64cc" EXE " apps/bob64_info.c build/bob64-app/info.b64e");
|
||||
run(BUILD_DIR "bob64cc" EXE " apps/bob64_mouse_smoke.c build/bob64-app/mouse-smoke.b64e");
|
||||
run(BUILD_DIR "bob64cc" EXE " apps/bob64_launcher.c build/bob64-app/launcher.b64e");
|
||||
ensure_directory(handoff?"build/bob64-handoff":"build/bob64");
|
||||
ensure_directory(storage_lba64_test?"build/bob64-handoff-storage64-test":
|
||||
(active_disconnect_test?"build/bob64-handoff-storage-active-test":
|
||||
(storage_write_test?"build/bob64-handoff-storage-test":
|
||||
(handoff?"build/bob64-handoff":"build/bob64"))));
|
||||
if(handoff) {
|
||||
ensure_directory("build/bob64-handoff/EFI");
|
||||
ensure_directory("build/bob64-handoff/EFI/BOOT");
|
||||
const char *image_directory=storage_lba64_test?
|
||||
"build/bob64-handoff-storage64-test/EFI":
|
||||
(active_disconnect_test?
|
||||
"build/bob64-handoff-storage-active-test/EFI":
|
||||
(storage_write_test?
|
||||
"build/bob64-handoff-storage-test/EFI":
|
||||
"build/bob64-handoff/EFI"));
|
||||
ensure_directory(image_directory);
|
||||
const char *boot_directory=storage_lba64_test?
|
||||
"build/bob64-handoff-storage64-test/EFI/BOOT":
|
||||
(active_disconnect_test?
|
||||
"build/bob64-handoff-storage-active-test/EFI/BOOT":
|
||||
(storage_write_test?
|
||||
"build/bob64-handoff-storage-test/EFI/BOOT":
|
||||
"build/bob64-handoff/EFI/BOOT"));
|
||||
ensure_directory(boot_directory);
|
||||
} else {
|
||||
ensure_directory("build/bob64/EFI");
|
||||
ensure_directory("build/bob64/EFI/BOOT");
|
||||
@@ -67,17 +99,20 @@ static void build_bob64(int handoff) {
|
||||
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/lz4.c bob64/firmware_store.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)) {
|
||||
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/pci.c bob64/xhci.c bob64/usb.c bob64/block_device.c bob64/partition.c bob64/disk_store.c bob64/shell.c bob64/filesystem.c bob64/snapshot.c bob64/lz4.c bob64/firmware_store.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) {
|
||||
int bob64Handoff=argc==2&&(!strcmp(argv[1],"--bob64-handoff")||!strcmp(argv[1],"--bob64-handoff-test"));
|
||||
int bob64StorageWriteTest=argc==2&&!strcmp(argv[1],"--bob64-handoff-storage-test");
|
||||
int bob64StorageActiveTest=argc==2&&!strcmp(argv[1],"--bob64-handoff-storage-active-test");
|
||||
int bob64StorageLba64Test=argc==2&&!strcmp(argv[1],"--bob64-handoff-storage64-test");
|
||||
int bob64Handoff=argc==2&&(!strcmp(argv[1],"--bob64-handoff")||!strcmp(argv[1],"--bob64-handoff-test")||bob64StorageWriteTest||bob64StorageActiveTest||bob64StorageLba64Test);
|
||||
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;
|
||||
fprintf(stderr,"Usage: build [--run | --run32 | --test | --bob64 | --bob64-test | --bob64-handoff | --bob64-handoff-test | --bob64-handoff-storage-test | --bob64-handoff-storage-active-test | --bob64-handoff-storage64-test] (from repository root)\n");return 1;
|
||||
}
|
||||
#ifdef _WIN32
|
||||
int directory_result=_mkdir("build");
|
||||
@@ -96,9 +131,10 @@ int main(int argc,char **argv) {
|
||||
fprintf(stderr,"build must be a directory\n");return 1;
|
||||
}
|
||||
if(bob64Only) {
|
||||
build_bob64(bob64Handoff);
|
||||
build_bob64(bob64Handoff,bob64StorageWriteTest,bob64StorageActiveTest,
|
||||
bob64StorageLba64Test);
|
||||
if(bob64Test) {
|
||||
run("gcc -std=c11 -O2 -Wall -Wextra -Werror " BOB64_TEST_THREAD_FLAGS "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/lz4.c bob64/firmware_store.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);
|
||||
run("gcc -std=c11 -O2 -Wall -Wextra -Werror " BOB64_TEST_THREAD_FLAGS "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/lz4.c bob64/firmware_store.c bob64/heap.c bob64/keyboard.c bob64/mouse.c bob64/pci.c bob64/xhci.c bob64/usb.c bob64/block_device.c bob64/partition.c bob64/disk_store.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 build/bob64-app/nested-smoke.b64e build/bob64-app/echo.b64e build/bob64-app/ls.b64e build/bob64-app/cat.b64e build/bob64-app/notes.b64e build/bob64-app/info.b64e build/bob64-app/mouse-smoke.b64e build/bob64-app/launcher.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 build/bob64-app/nested-smoke.b64e build/bob64-app/echo.b64e build/bob64-app/ls.b64e build/bob64-app/cat.b64e build/bob64-app/notes.b64e build/bob64-app/info.b64e build/bob64-app/mouse-smoke.b64e build/bob64-app/launcher.b64e");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$Path,
|
||||
[ValidateRange(32, 2097154)]
|
||||
[int]$SizeMiB = 64,
|
||||
[long]$FirstPartitionLba = 2048,
|
||||
[switch]$Sparse,
|
||||
[switch]$Force
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$sectorSize = 512
|
||||
$sectorCount = [uint64]$SizeMiB * 1MB / $sectorSize
|
||||
$tableBytes = 128 * 128
|
||||
$tableSectors = [uint64][Math]::Ceiling($tableBytes / $sectorSize)
|
||||
if ($sectorCount -lt (4096 + 2 * $tableSectors + 2)) {
|
||||
throw 'The image is too small for the bob64 data partition and GPT metadata.'
|
||||
}
|
||||
|
||||
$fullPath = [IO.Path]::GetFullPath($Path)
|
||||
if ((Test-Path -LiteralPath $fullPath) -and -not $Force) {
|
||||
throw "Refusing to overwrite '$fullPath'; pass -Force to replace it."
|
||||
}
|
||||
$parent = Split-Path -Parent $fullPath
|
||||
if (-not (Test-Path -LiteralPath $parent -PathType Container)) {
|
||||
throw "Output directory does not exist: $parent"
|
||||
}
|
||||
|
||||
function Set-U16([byte[]]$Buffer,[int]$Offset,[uint16]$Value) {
|
||||
[Buffer]::BlockCopy([BitConverter]::GetBytes($Value),0,$Buffer,$Offset,2)
|
||||
}
|
||||
function Set-U32([byte[]]$Buffer,[int]$Offset,[uint32]$Value) {
|
||||
[Buffer]::BlockCopy([BitConverter]::GetBytes($Value),0,$Buffer,$Offset,4)
|
||||
}
|
||||
function Set-U64([byte[]]$Buffer,[int]$Offset,[uint64]$Value) {
|
||||
[Buffer]::BlockCopy([BitConverter]::GetBytes($Value),0,$Buffer,$Offset,8)
|
||||
}
|
||||
function Get-Crc32([byte[]]$Bytes) {
|
||||
[uint32]$crc = [uint32]::MaxValue
|
||||
foreach ($value in $Bytes) {
|
||||
$crc = $crc -bxor [uint32]$value
|
||||
for ($bit = 0; $bit -lt 8; $bit++) {
|
||||
if ($crc -band 1) { $crc = ($crc -shr 1) -bxor 0xedb88320 }
|
||||
else { $crc = $crc -shr 1 }
|
||||
}
|
||||
}
|
||||
return [uint32]($crc -bxor 0xffffffff)
|
||||
}
|
||||
function New-GptHeader([uint64]$CurrentLba,[uint64]$BackupLba,
|
||||
[uint64]$TableLba,[uint64]$FirstUsable,[uint64]$LastUsable,
|
||||
[byte[]]$DiskGuid,[uint32]$TableCrc) {
|
||||
$header = [byte[]]::new($sectorSize)
|
||||
[Text.Encoding]::ASCII.GetBytes('EFI PART').CopyTo($header,0)
|
||||
Set-U32 $header 8 0x00010000
|
||||
Set-U32 $header 12 92
|
||||
Set-U64 $header 24 $CurrentLba
|
||||
Set-U64 $header 32 $BackupLba
|
||||
Set-U64 $header 40 $FirstUsable
|
||||
Set-U64 $header 48 $LastUsable
|
||||
[Buffer]::BlockCopy($DiskGuid,0,$header,56,16)
|
||||
Set-U64 $header 72 $TableLba
|
||||
Set-U32 $header 80 128
|
||||
Set-U32 $header 84 128
|
||||
Set-U32 $header 88 $TableCrc
|
||||
$crcInput = [byte[]]::new(92)
|
||||
[Buffer]::BlockCopy($header,0,$crcInput,0,92)
|
||||
Set-U32 $header 16 (Get-Crc32 $crcInput)
|
||||
return ,$header
|
||||
}
|
||||
|
||||
$table = [byte[]]::new([int]($tableSectors * $sectorSize))
|
||||
$typeGuid = [byte[]](0x64,0x62,0x61,0x7b,0x30,0x30,0x34,0x30,
|
||||
0x9a,0x21,0x42,0x4f,0x42,0x36,0x34,0x01)
|
||||
$uniqueGuid = [Guid]::NewGuid().ToByteArray()
|
||||
$firstPartitionLba = [uint64]$FirstPartitionLba
|
||||
$lastUsableLba = $sectorCount - $tableSectors - 2
|
||||
if ($firstPartitionLba -lt 2048 -or $firstPartitionLba -gt $lastUsableLba) {
|
||||
throw 'The data partition start must be inside the GPT usable-LBA range.'
|
||||
}
|
||||
$lastPartitionLba = $sectorCount - $tableSectors - 2
|
||||
[Buffer]::BlockCopy($typeGuid,0,$table,0,16)
|
||||
[Buffer]::BlockCopy($uniqueGuid,0,$table,16,16)
|
||||
Set-U64 $table 32 $firstPartitionLba
|
||||
Set-U64 $table 40 $lastPartitionLba
|
||||
$name = [Text.Encoding]::Unicode.GetBytes('bob64 data')
|
||||
[Buffer]::BlockCopy($name,0,$table,56,$name.Length)
|
||||
$tableCrc = Get-Crc32 $table
|
||||
$diskGuid = [Guid]::NewGuid().ToByteArray()
|
||||
$primaryHeader = New-GptHeader 1 ($sectorCount - 1) 2 34 $lastUsableLba $diskGuid $tableCrc
|
||||
$backupTableLba = $sectorCount - $tableSectors - 1
|
||||
$backupHeader = New-GptHeader ($sectorCount - 1) 1 $backupTableLba 34 $lastUsableLba $diskGuid $tableCrc
|
||||
$mbr = [byte[]]::new($sectorSize)
|
||||
$mbr[446 + 4] = 0xee
|
||||
Set-U32 $mbr (446 + 8) 1
|
||||
Set-U32 $mbr (446 + 12) ([uint32][Math]::Min([double]($sectorCount - 1),[double][uint32]::MaxValue))
|
||||
$mbr[510] = 0x55
|
||||
$mbr[511] = 0xaa
|
||||
|
||||
$stream = [IO.File]::Open($fullPath,[IO.FileMode]::Create,
|
||||
[IO.FileAccess]::ReadWrite,[IO.FileShare]::None)
|
||||
try {
|
||||
if ($Sparse) {
|
||||
$stream.Dispose()
|
||||
& fsutil.exe sparse setflag $fullPath | Out-Null
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw 'Could not mark the GPT disk image as sparse.'
|
||||
}
|
||||
$stream = [IO.File]::Open($fullPath,[IO.FileMode]::Open,
|
||||
[IO.FileAccess]::ReadWrite,[IO.FileShare]::None)
|
||||
}
|
||||
$stream.SetLength([long]$sectorCount * $sectorSize)
|
||||
$stream.Position = 0; $stream.Write($mbr,0,$mbr.Length)
|
||||
$stream.Position = $sectorSize; $stream.Write($primaryHeader,0,$primaryHeader.Length)
|
||||
$stream.Position = 2 * $sectorSize; $stream.Write($table,0,$table.Length)
|
||||
$stream.Position = [long]$backupTableLba * $sectorSize
|
||||
$stream.Write($table,0,$table.Length)
|
||||
$stream.Position = [long]($sectorCount - 1) * $sectorSize
|
||||
$stream.Write($backupHeader,0,$backupHeader.Length)
|
||||
$stream.Flush($true)
|
||||
} finally {
|
||||
if ($stream) { $stream.Dispose() }
|
||||
}
|
||||
|
||||
$partitionBlocks = $lastPartitionLba - $firstPartitionLba + 1
|
||||
Write-Output "Created bob64 GPT disk image: $fullPath"
|
||||
Write-Output "Disk size: $SizeMiB MiB; data partition: LBA $firstPartitionLba, $partitionBlocks sectors"
|
||||
Write-Output 'Attach this raw image to the emulator as a USB mass-storage disk.'
|
||||
+1411
-45
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,134 @@
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$QemuPath,
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$OvmfCodePath,
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$OvmfVarsPath,
|
||||
[string]$ImageRoot = (Join-Path (Split-Path -Parent $PSScriptRoot) 'build\bob64-handoff'),
|
||||
[int]$TimeoutSeconds = 60
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
foreach ($path in @($QemuPath, $OvmfCodePath, $OvmfVarsPath, $ImageRoot)) {
|
||||
if (-not (Test-Path -LiteralPath $path)) {
|
||||
throw "Required Bob64 serial test 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"
|
||||
}
|
||||
|
||||
function Get-FreeLoopbackPort {
|
||||
$listener = [Net.Sockets.TcpListener]::new([Net.IPAddress]::Loopback, 0)
|
||||
$listener.Start()
|
||||
try { return ([Net.IPEndPoint]$listener.LocalEndpoint).Port }
|
||||
finally { $listener.Stop() }
|
||||
}
|
||||
|
||||
function Wait-SerialText([Net.Sockets.NetworkStream]$stream,
|
||||
[string]$needle,[int]$startOffset,[int]$timeoutSeconds) {
|
||||
$deadline = [DateTime]::UtcNow.AddSeconds($timeoutSeconds)
|
||||
$buffer = New-Object byte[] 4096
|
||||
while ([DateTime]::UtcNow -lt $deadline) {
|
||||
while ($stream.DataAvailable) {
|
||||
$count = $stream.Read($buffer, 0, $buffer.Length)
|
||||
if ($count -le 0) { break }
|
||||
[void]$script:serialTranscript.Append(
|
||||
[Text.Encoding]::ASCII.GetString($buffer, 0, $count))
|
||||
}
|
||||
$text = $script:serialTranscript.ToString()
|
||||
if ($text.Length -gt $startOffset -and
|
||||
$text.Substring($startOffset).Contains($needle)) { return $text }
|
||||
if ($script:qemuProcess.HasExited) {
|
||||
throw "QEMU exited before COM1 produced '$needle'.`n$text"
|
||||
}
|
||||
Start-Sleep -Milliseconds 20
|
||||
}
|
||||
throw "Timed out waiting for COM1 output '$needle'.`n$($script:serialTranscript.ToString())"
|
||||
}
|
||||
|
||||
$imagePath = (Resolve-Path -LiteralPath $ImageRoot).Path.Replace('\', '/')
|
||||
$firmwarePath = (Resolve-Path -LiteralPath $OvmfCodePath).Path
|
||||
$varsTemplatePath = (Resolve-Path -LiteralPath $OvmfVarsPath).Path
|
||||
$varsCopyPath = Join-Path ([IO.Path]::GetTempPath()) ("bob64-serial-vars-{0}.fd" -f [guid]::NewGuid())
|
||||
Copy-Item -LiteralPath $varsTemplatePath -Destination $varsCopyPath
|
||||
$serialPort = Get-FreeLoopbackPort
|
||||
$script:serialTranscript = [Text.StringBuilder]::new()
|
||||
$script:qemuProcess = $null
|
||||
$serialClient = $null
|
||||
|
||||
try {
|
||||
$qemuArguments = @(
|
||||
'-machine', 'q35', '-m', '512M', '-cpu', 'max',
|
||||
'-drive', "if=pflash,format=raw,unit=0,file=$firmwarePath,readonly=on",
|
||||
'-drive', "if=pflash,format=raw,unit=1,file=$varsCopyPath",
|
||||
'-drive', "format=raw,file=fat:rw:$imagePath",
|
||||
'-boot', 'order=c', '-display', 'none',
|
||||
'-serial', "tcp:127.0.0.1:$serialPort,server=on,wait=on",
|
||||
'-monitor', 'none'
|
||||
)
|
||||
$startInfo = [Diagnostics.ProcessStartInfo]::new()
|
||||
$startInfo.FileName = (Resolve-Path -LiteralPath $QemuPath).Path
|
||||
$startInfo.UseShellExecute = $false
|
||||
$startInfo.CreateNoWindow = $true
|
||||
$startInfo.Arguments = (($qemuArguments | ForEach-Object {
|
||||
'"' + ([string]$_).Replace('"', '\"') + '"'
|
||||
}) -join ' ')
|
||||
$script:qemuProcess = [Diagnostics.Process]::Start($startInfo)
|
||||
|
||||
$serialClient = $null
|
||||
$connectDeadline = [DateTime]::UtcNow.AddSeconds(10)
|
||||
while (-not $serialClient -and [DateTime]::UtcNow -lt $connectDeadline) {
|
||||
$candidate = [Net.Sockets.TcpClient]::new()
|
||||
try {
|
||||
$candidate.Connect('127.0.0.1', $serialPort)
|
||||
$serialClient = $candidate
|
||||
} catch {
|
||||
$candidate.Dispose()
|
||||
Start-Sleep -Milliseconds 100
|
||||
}
|
||||
}
|
||||
if (-not $serialClient) {
|
||||
throw 'Could not connect to QEMU COM1.'
|
||||
}
|
||||
$stream = $serialClient.GetStream()
|
||||
$stream.ReadTimeout = 100
|
||||
[void](Wait-SerialText $stream 'bob64> ' 0 $TimeoutSeconds)
|
||||
if (-not $script:serialTranscript.ToString().Contains(
|
||||
'bob64 kernel: serial RX IRQ enabled')) {
|
||||
throw "The kernel did not enable COM1 receive interrupts.`n$($script:serialTranscript.ToString())"
|
||||
}
|
||||
|
||||
$startOffset = $script:serialTranscript.Length
|
||||
$commandBytes = [Text.Encoding]::ASCII.GetBytes("version`r")
|
||||
$stream.Write($commandBytes, 0, $commandBytes.Length)
|
||||
$stream.Flush()
|
||||
[void](Wait-SerialText $stream 'bob64 kernel 0.1' $startOffset $TimeoutSeconds)
|
||||
|
||||
$startOffset = $script:serialTranscript.Length
|
||||
$commandBytes = [Text.Encoding]::ASCII.GetBytes("write serial.txt bob!`r")
|
||||
$stream.Write($commandBytes, 0, $commandBytes.Length)
|
||||
$stream.Flush()
|
||||
[void](Wait-SerialText $stream 'saved' $startOffset $TimeoutSeconds)
|
||||
|
||||
$startOffset = $script:serialTranscript.Length
|
||||
$commandBytes = [Text.Encoding]::ASCII.GetBytes("cat serial.txt`r")
|
||||
$stream.Write($commandBytes, 0, $commandBytes.Length)
|
||||
$stream.Flush()
|
||||
[void](Wait-SerialText $stream "bob!`r`nbob64> " $startOffset $TimeoutSeconds)
|
||||
|
||||
Write-Output 'COM1 IRQ runtime check passed: version, file write, and file read commands completed over serial.'
|
||||
}
|
||||
finally {
|
||||
if ($serialClient) { $serialClient.Dispose() }
|
||||
if ($script:qemuProcess -and -not $script:qemuProcess.HasExited) {
|
||||
$script:qemuProcess.Kill()
|
||||
$script:qemuProcess.WaitForExit()
|
||||
}
|
||||
if (Test-Path -LiteralPath $varsCopyPath) {
|
||||
Remove-Item -LiteralPath $varsCopyPath -Force
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user