better support for running on real hardware

This commit is contained in:
nakst 2021-09-22 19:01:29 +01:00
parent 34f86b3f91
commit 0319f08a1a
10 changed files with 113 additions and 37 deletions

View File

@ -5,6 +5,15 @@
#define K_PAGE_SIZE (4096)
#define K_PAGE_BITS (12)
typedef struct VideoModeInformation {
uint8_t valid : 1, edidValid : 1;
uint8_t bitsPerPixel;
uint16_t widthPixels, heightPixels;
uint16_t bytesPerScanlineLinear;
uint64_t bufferPhysical;
uint8_t edid[128];
} VideoModeInformation;
typedef struct ElfHeader {
uint32_t magicNumber; // 0x7F followed by 'ELF'
uint8_t bits; // 1 = 32 bit, 2 = 64 bit
@ -79,13 +88,28 @@ void ZeroMemory(void *pointer, uint64_t size) {
}
}
void Error(WCHAR *message) {
void Print(WCHAR *message) {
uefi_call_wrapper(systemTable->ConOut->OutputString, 2, systemTable->ConOut, message);
}
void Error(WCHAR *message) {
Print(message);
while (1);
}
void PrintHex(uint64_t value) {
const WCHAR *hexChars = L"0123456789ABCDEF";
for (uintptr_t i = 0; i < 16; i++) {
WCHAR b[2] = { hexChars[(value >> (60 - i * 4)) & 0xF], 0 };
Print((WCHAR *) b);
}
Print(L", ");
}
EFI_STATUS EFIAPI efi_main(EFI_HANDLE imageHandle, EFI_SYSTEM_TABLE *_systemTable) {
UINTN mapKey;
UINTN mapKey = 0;
systemTable = _systemTable;
uint32_t *framebuffer, horizontalResolution, verticalResolution, pixelsPerScanline;
ElfHeader *header;
@ -95,12 +119,14 @@ EFI_STATUS EFIAPI efi_main(EFI_HANDLE imageHandle, EFI_SYSTEM_TABLE *_systemTabl
EFI_PHYSICAL_ADDRESS address = 0x100000;
if (EFI_SUCCESS != uefi_call_wrapper(systemTable->BootServices->AllocatePages, 4, AllocateAddress, EfiLoaderData, 0x200, &address)) {
Error(L"Error: Could not map 0x100000 -> 0x180000.\n");
Error(L"Error: Could not allocate 1MB->3MB.\n");
}
}
// Find the RSDP.
{
uint8_t foundRSDP = 0;
for (uintptr_t i = 0; i < systemTable->NumberOfTableEntries; i++) {
EFI_CONFIGURATION_TABLE *entry = systemTable->ConfigurationTable + i;
if (entry->VendorGuid.Data1 == 0x8868E871 && entry->VendorGuid.Data2 == 0xE4F1 && entry->VendorGuid.Data3 == 0x11D3
@ -108,9 +134,14 @@ EFI_STATUS EFIAPI efi_main(EFI_HANDLE imageHandle, EFI_SYSTEM_TABLE *_systemTabl
&& entry->VendorGuid.Data4[3] == 0x80 && entry->VendorGuid.Data4[4] == 0xC7 && entry->VendorGuid.Data4[5] == 0x3C
&& entry->VendorGuid.Data4[6] == 0x88 && entry->VendorGuid.Data4[7] == 0x81) {
*((uint64_t *) 0x107FE8) = (uint64_t) entry->VendorTable;
// Print(L"The RSDP can be found at 0x%x.\n", entry->VendorTable);
foundRSDP = 1;
break;
}
}
if (!foundRSDP) {
Error(L"Error: Could not find the RSDP.\n");
}
}
// Read the kernel, IID and loader files.
@ -125,13 +156,15 @@ EFI_STATUS EFIAPI efi_main(EFI_HANDLE imageHandle, EFI_SYSTEM_TABLE *_systemTabl
UINTN size;
if (EFI_SUCCESS != uefi_call_wrapper(systemTable->BootServices->OpenProtocol, 6, imageHandle, &loadedImageProtocolGUID, (void **) &loadedImageProtocol, imageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL)) {
if (EFI_SUCCESS != uefi_call_wrapper(systemTable->BootServices->OpenProtocol, 6, imageHandle, &loadedImageProtocolGUID,
(void **) &loadedImageProtocol, imageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL)) {
Error(L"Error: Could not open protocol 1.\n");
}
EFI_HANDLE deviceHandle = loadedImageProtocol->DeviceHandle;
if (EFI_SUCCESS != uefi_call_wrapper(systemTable->BootServices->OpenProtocol, 6, deviceHandle, &simpleFilesystemProtocolGUID, (void **) &simpleFilesystemProtocol, imageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL)) {
if (EFI_SUCCESS != uefi_call_wrapper(systemTable->BootServices->OpenProtocol, 6, deviceHandle, &simpleFilesystemProtocolGUID,
(void **) &simpleFilesystemProtocol, imageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL)) {
Error(L"Error: Could not open procotol 2.\n");
}
@ -152,7 +185,7 @@ EFI_STATUS EFIAPI efi_main(EFI_HANDLE imageHandle, EFI_SYSTEM_TABLE *_systemTabl
// Print(L"Kernel size: %d bytes\n", size);
if (size == KERNEL_BUFFER_SIZE) {
Error(L"Kernel too large to fit into buffer.\n");
Error(L"Error: Kernel too large to fit into buffer.\n");
}
if (EFI_SUCCESS != uefi_call_wrapper(filesystemRoot->Open, 5, filesystemRoot, &iidFile, L"esiid.dat", EFI_FILE_MODE_READ, 0)) {
@ -178,6 +211,7 @@ EFI_STATUS EFIAPI efi_main(EFI_HANDLE imageHandle, EFI_SYSTEM_TABLE *_systemTabl
// Get the graphics mode information.
// TODO Mode picking.
// TODO Get EDID information, if available.
{
EFI_GRAPHICS_OUTPUT_PROTOCOL *graphicsOutputProtocol;
EFI_GUID graphicsOutputProtocolGUID = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
@ -197,7 +231,8 @@ EFI_STATUS EFIAPI efi_main(EFI_HANDLE imageHandle, EFI_SYSTEM_TABLE *_systemTabl
UINTN descriptorSize, size = MEMORY_MAP_BUFFER_SIZE;
UINT32 descriptorVersion;
if (EFI_SUCCESS != uefi_call_wrapper(systemTable->BootServices->GetMemoryMap, 5, &size, (EFI_MEMORY_DESCRIPTOR *) memoryMapBuffer, &mapKey, &descriptorSize, &descriptorVersion)) {
if (EFI_SUCCESS != uefi_call_wrapper(systemTable->BootServices->GetMemoryMap, 5, &size,
(EFI_MEMORY_DESCRIPTOR *) memoryMapBuffer, &mapKey, &descriptorSize, &descriptorVersion)) {
Error(L"Error: Could not get memory map.\n");
}
@ -228,14 +263,14 @@ EFI_STATUS EFIAPI efi_main(EFI_HANDLE imageHandle, EFI_SYSTEM_TABLE *_systemTabl
uint64_t *paging = (uint64_t *) 0x140000;
ZeroMemory(paging, 0x5000);
paging[0x1FE] = 0x140003;
paging[0x000] = 0x141003;
paging[0x200] = 0x142003;
paging[0x400] = 0x143003;
paging[0x1FE] = 0x140003; // Recursive
paging[0x000] = 0x141003; // L4
paging[0x200] = 0x142003; // L3
paging[0x400] = 0x143003; // L2
paging[0x401] = 0x144003;
for (uintptr_t i = 0; i < 0x400; i++) {
paging[0x600 + i] = (i * 0x1000) | 3;
paging[0x600 + i] = (i * 0x1000) | 3; // L1
}
}
@ -249,17 +284,16 @@ EFI_STATUS EFIAPI efi_main(EFI_HANDLE imageHandle, EFI_SYSTEM_TABLE *_systemTabl
}
// Copy the graphics information across.
// TODO.
#if 0
{
struct VESAVideoModeInformation *destination = (struct VESAVideoModeInformation *) (0x107000);
VideoModeInformation *destination = (VideoModeInformation *) (0x107000);
destination->widthPixels = horizontalResolution;
destination->heightPixels = verticalResolution;
destination->bufferPhysical = (uintptr_t) framebuffer; // TODO 64-bit framebuffers.
destination->bufferPhysical = (uint64_t) framebuffer;
destination->bytesPerScanlineLinear = pixelsPerScanline * 4;
destination->bitsPerPixel = 32;
destination->valid = 1;
destination->edidValid = 0;
}
#endif
// Allocate and map memory for the kernel.
{

View File

@ -7,7 +7,6 @@ vbe_init:
jmp vbe_bad
%endif
; Get EDID information.
mov ax,0x4F15
mov bl,1

View File

@ -787,11 +787,11 @@ static void DeviceAttach(KDevice *parentDevice) {
ACPIInitialise2();
#endif
KDeviceAttachByName(acpi.computer, "PCI");
if (!acpi.vgaControllerUnavailable) {
KDeviceAttachByName(acpi.computer, "SVGA");
}
KDeviceAttachByName(acpi.computer, "PCI");
}
KDriver driverACPI = {

View File

@ -103,12 +103,17 @@ void KRegisterGraphicsTarget(KGraphicsTarget *target) {
graphics.frameBuffer.Resize(graphics.width, graphics.height);
#ifdef START_DEBUG_OUTPUT
StartDebugOutput();
EsPrint("Hello\n");
#else
windowManager.Initialise();
EsMessage m;
EsMemoryZero(&m, sizeof(EsMessage));
m.type = ES_MSG_SET_SCREEN_RESOLUTION;
desktopProcess->messageQueue.SendMessage(nullptr, &m);
#endif
}
bool Surface::Resize(size_t newResX, size_t newResY, uint32_t clearColor, bool copyOldBits) {
@ -580,6 +585,11 @@ void GraphicsDebugPutBlock32(uintptr_t x, uintptr_t y, bool toggle,
void GraphicsDebugClearScreen32(unsigned screenWidth, unsigned screenHeight, unsigned stride, volatile uint8_t *linearBuffer) {
for (uintptr_t i = 0; i < screenHeight; i++) {
for (uintptr_t j = 0; j < screenWidth * 4; j += 4) {
linearBuffer[i * stride + j + 2] = 0x18;
linearBuffer[i * stride + j + 1] = 0x7E;
linearBuffer[i * stride + j + 0] = 0xCF;
#if 0
if (graphics.debuggerActive) {
linearBuffer[i * stride + j + 2] = 0x18;
linearBuffer[i * stride + j + 1] = 0x7E;
@ -589,6 +599,7 @@ void GraphicsDebugClearScreen32(unsigned screenWidth, unsigned screenHeight, uns
linearBuffer[i * stride + j + 1] >>= 1;
linearBuffer[i * stride + j + 0] >>= 1;
}
#endif
}
}
}

View File

@ -104,6 +104,8 @@ InterruptContext *ArchInitialiseThread(uintptr_t kernelStack, uintptr_t kernelSt
uintptr_t startAddress, uintptr_t argument1, uintptr_t argument2,
bool userland, uintptr_t stack, uintptr_t userStackSize);
void StartDebugOutput();
uint64_t timeStampTicksPerMs;
EsUniqueIdentifier installationID; // The identifier of this OS installation, given to us by the bootloader.

View File

@ -22,13 +22,9 @@ void DebugWriteCharacter(uintptr_t character);
bool printToDebugger = false;
uintptr_t terminalPosition = 80;
#define EARLY_DEBUGGING
// #define VGA_TEXT_MODE
#ifdef EARLY_DEBUGGING
char kernelLog[262144];
#define KERNEL_LOG_SIZE (262144)
char kernelLog[KERNEL_LOG_SIZE];
uintptr_t kernelLogPosition;
#endif
static void TerminalCallback(int character, void *) {
if (!character) return;
@ -36,13 +32,11 @@ static void TerminalCallback(int character, void *) {
KSpinlockAcquire(&terminalLock);
EsDefer(KSpinlockRelease(&terminalLock));
#ifdef EARLY_DEBUGGING
{
if (sizeof(kernelLog)) {
kernelLog[kernelLogPosition] = character;
kernelLogPosition++;
if (kernelLogPosition == sizeof(kernelLog)) kernelLogPosition = 0;
}
#endif
#ifdef VGA_TEXT_MODE
{
@ -139,7 +133,7 @@ void DebugWriteCharacter(uintptr_t character) {
}
void StartDebugOutput() {
if (graphics.target && graphics.target->debugClearScreen && graphics.target->debugPutBlock) {
if (graphics.target && graphics.target->debugClearScreen && graphics.target->debugPutBlock && !printToDebugger) {
debugRows = (graphics.height - 1) / VGA_FONT_HEIGHT;
debugColumns = (graphics.width - 1) / VGA_FONT_WIDTH - 2;
debugCurrentRow = debugCurrentColumn = 0;
@ -156,6 +150,7 @@ void KDebugKeyPressed() {
KernelPanic("Debug key pressed.\n");
}
#ifdef POST_PANIC_DEBUGGING
uintptr_t DebugReadNumber() {
uintptr_t value = 0;
@ -188,6 +183,7 @@ uintptr_t DebugReadNumber() {
return value;
}
#endif
void KernelPanic(const char *format, ...) {
ProcessorDisableInterrupts();
@ -201,7 +197,7 @@ void KernelPanic(const char *format, ...) {
DriversDumpState();
}
if (!printToDebugger) StartDebugOutput();
StartDebugOutput();
EsPrint("\n--- System Error ---\n>> ");
@ -251,7 +247,7 @@ void KernelPanic(const char *format, ...) {
}
}
#ifdef EARLY_DEBUGGING
#ifdef POST_PANIC_DEBUGGING
uintptr_t kernelLogEnd = kernelLogPosition;
EsPrint("Press 'D' to enter debugger.\n");
while (KWaitKey() != ES_SCANCODE_D);
@ -279,8 +275,6 @@ void KernelPanic(const char *format, ...) {
uintptr_t position = 0, nextPosition = 0;
uintptr_t x = 0, y = 0;
#ifdef VGA_TEXT_MODE
for (uintptr_t i = 0; i < 80 * 25; i++) {
TERMINAL_ADDRESS[i] = 0x0700;

View File

@ -286,6 +286,8 @@ Option options[] = {
{ "Flag.CHECK_FOR_NOT_RESPONDING", OPTION_TYPE_BOOL, { .b = true } },
{ "Flag._ALWAYS_USE_VBE", OPTION_TYPE_BOOL, { .b = false } },
{ "Flag.COM_OUTPUT", OPTION_TYPE_BOOL, { .b = true } },
{ "Flag.POST_PANIC_DEBUGGING", OPTION_TYPE_BOOL, { .b = false } },
{ "Flag.START_DEBUG_OUTPUT", OPTION_TYPE_BOOL, { .b = false } },
{ "Dependency.ACPICA", OPTION_TYPE_BOOL, { .b = true } },
{ "Dependency.stb_image", OPTION_TYPE_BOOL, { .b = true } },
{ "Dependency.stb_image_write", OPTION_TYPE_BOOL, { .b = true } },
@ -308,6 +310,7 @@ Option options[] = {
{ "Emulator.VBoxEFI", OPTION_TYPE_BOOL, { .b = false } },
{ "Emulator.QemuEFI", OPTION_TYPE_BOOL, { .b = false } },
{ "BuildCore.Verbose", OPTION_TYPE_BOOL, { .b = false } },
{ "BuildCore.DeletePOSIXBeforeImport", OPTION_TYPE_BOOL, { .b = false } },
{ "General.first_application", OPTION_TYPE_STRING, { .s = NULL } },
{ "General.wallpaper", OPTION_TYPE_STRING, { .s = NULL } },
{ "General.installation_state", OPTION_TYPE_STRING, { .s = "0" } },

View File

@ -156,7 +156,7 @@ char kernelAssemblyFlags[4096] = " -felf64 -Fdwarf ";
bool verbose;
bool useColoredOutput;
bool forEmulator, bootUseVBE;
bool forEmulator, bootUseVBE, deletePOSIXBeforeImport;
bool systemBuild;
bool convertFonts = true;
EsINIState *fontLines;
@ -1169,6 +1169,12 @@ void Install(const char *driveFile, uint64_t partitionSize, const char *partitio
_partitionOffset = 1048576;
Format(partitionSize - _partitionOffset, partitionLabel, installationIdentifier, kernel, kernelBytes);
#ifndef OS_ESSENCE
if (deletePOSIXBeforeImport) {
system("rm -r root/Applications/POSIX");
}
#endif
Log("Copying files to the drive... ");
ImportNode root = {};
@ -1302,6 +1308,8 @@ int main(int argc, char **argv) {
bootUseVBE = !!atoi(s.value);
} else if (0 == strcmp(s.key, "Flag.COM_OUTPUT") && atoi(s.value)) {
strcat(kernelAssemblyFlags, " -DCOM_OUTPUT ");
} else if (0 == strcmp(s.key, "BuildCore.DeletePOSIXBeforeImport")) {
deletePOSIXBeforeImport = !!atoi(s.value);
} else if (0 == memcmp(s.key, "General.", 8)) {
EsINIState s2 = s;
s2.key += 8, s2.keyBytes -= 8;

View File

@ -16,7 +16,7 @@ LINK="clang -target x86_64-unknown-windows -nostdlib -Wl,-entry:efi_main -Wl,-su
$CC -o bin/uefi.o boot/x86/uefi.c
$LINK -o bin/uefi bin/uefi.o
mkdir mount
mkdir -p mount
sudo losetup --offset `fdisk -l bin/uefi_drive | grep 'EFI System' | awk '{print 512*$2}'` --sizelimit `fdisk -l bin/uefi_drive | grep 'EFI System' | awk '{print 512*$4}'` /dev/loop0 bin/uefi_drive
# sudo mkfs.fat /dev/loop0
sudo mount /dev/loop0 mount

25
util/uefi_to_device.sh Executable file
View File

@ -0,0 +1,25 @@
# Run as root.
# Pass device file of ESP as first argument.
# Pass device file of EsFS partition as second argument.
set -e
mkdir -p mount
mount $1 mount
mkdir -p mount/EFI/BOOT
cp bin/uefi mount/EFI/BOOT/BOOTX64.EFI
cp bin/Kernel.esx mount/eskernel.esx
cp bin/uefi_loader mount/esloader.bin
cp bin/iid.dat mount/esiid.dat
umount $1
rmdir mount
SOURCE_OFFSET=`fdisk -l bin/drive | grep 'Linux' | awk '{print $3}'`
SOURCE_COUNT=`fdisk -l bin/drive | grep 'Linux' | awk '{print $5}'`
DESTINATION_COUNT=`blockdev --getsz $2`
if [ "$SOURCE_COUNT" -gt "$DESTINATION_COUNT" ]; then
echo Please set Emulator.PrimaryDriveMB lower than $DESTINATION_COUNT.
exit 1
fi
dd if=bin/drive of=$2 bs=512 count=$SOURCE_COUNT skip=$SOURCE_OFFSET conv=notrunc