changeset 8:c4198b641a84

kernel is correctly loaded on 0x40000000 CPU priviledge is wrong
author kono
date Fri, 19 Jan 2024 17:30:15 +0900
parents f422d46482db
children 33d10c470c6e
files gnu-efi-3.0.12/apps/bootloader.c
diffstat 1 files changed, 50 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/gnu-efi-3.0.12/apps/bootloader.c	Fri Jan 19 15:48:18 2024 +0900
+++ b/gnu-efi-3.0.12/apps/bootloader.c	Fri Jan 19 17:30:15 2024 +0900
@@ -4,15 +4,37 @@
 #include <efi.h>
 #include <efilib.h>
 
+struct rsdp {
+  CHAR8 sig[8];
+  UINT8 Checksum;
+  CHAR8 OEMID[6];
+  UINT8 Revision;
+  UINT32 RsdtAddress;
+  UINT32 Len;
+  UINT64 XsdtAddress;
+  UINT8 ExtChecksum;
+  UINT8 reserved[3];
+};
+
+struct __attribute((packed)) GraphicConfig {
+  UINT64 frame_base;
+  UINT64 frame_size;
+  UINT64 horizontal_resolution;
+  UINT64 vertical_resolution;
+  UINT64 pixels_per_scan_line;
+};
+
 struct __attribute__((packed)) BootParam {
   UINT64 kernel_entry;
   UINT64 rsdp_addr;
-  // struct GraphicConfig graphic_config;
+  struct GraphicConfig graphic_config;
 //  struct gdt bootstrap_gdt[3];
 //  struct gdt_desc bootstrap_gdt_desc;
   UINT64 kernel_addr;
 };
 
+
+
 struct MemoryMap {
   UINTN BufferSize;
   VOID *Buffer;
@@ -83,14 +105,25 @@
             UINTN FileSize = FileInfo->FileSize;
             Print(L"FileSize = %d\n", FileSize);
             *FilePageSize = (FileSize + 4095) / 4096;
-            *FileAddr = 0;
-            Status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData, *FilePageSize, FileAddr);
+            *FileAddr = 0x40000000;
+            Status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAddress, EfiLoaderData, *FilePageSize, FileAddr);
             if (EFI_ERROR(Status)) {
                 Print(L"Failed to allocate pages.\n");
                 FreePool(FileInfo);
                 return Status;
             }
 
+            // skip header
+            FileSize = 0x1000;
+            Status = uefi_call_wrapper(File->Read, 3, File, &FileSize, (VOID *)*FileAddr);
+            if (EFI_ERROR(Status)) {
+                Print(L"Failed to load file.\n");
+                FreePool(FileInfo);
+                return Status;
+            }
+
+            // load binary part
+            FileSize = FileInfo->FileSize-0x1000;
             Status = uefi_call_wrapper(File->Read, 3, File, &FileSize, (VOID *)*FileAddr);
             if (EFI_ERROR(Status)) {
                 Print(L"Failed to load file.\n");
@@ -129,7 +162,6 @@
     Print(L"Boot 1\n");
 
     struct BootParam boot_param;
-    KernelBaseAddr  = (EFI_PHYSICAL_ADDRESS ) ((UINT64)KernelBaseAddr + 0x1000);
     boot_param.kernel_addr = KernelBaseAddr ;
     boot_param.kernel_entry = boot_param.kernel_addr ;
     // GetGraphicMode(ImageHandle, &(boot_param.graphic_config));
@@ -137,15 +169,21 @@
     typedef unsigned long (EntryPoint)(struct BootParam*);
     EntryPoint *Entry = (EntryPoint*)(KernelBaseAddr);
 
-    // VOID *vendor_table;
-    // Status = SystemTable->BootServices->GetSystemConfigurationTable(&gEfiAcpiTableGuid, &vendor_table);
-    // if (EFI_ERROR(Status)) {
-    //     Print(L"Failed to get system configuration table.\n");
-    //     return Status;
-    // }
-    // boot_param.rsdp_addr = (UINT64)vendor_table;
     Print(L"Boot 2\n");
 
+    void **rsdp = NULL;
+    EFI_GUID AcpiTableGuid = ACPI_20_TABLE_GUID; // Use ACPI 2.0 table GUID
+
+    Status = LibGetSystemConfigurationTable(&AcpiTableGuid, (void **)&rsdp);
+    if (EFI_ERROR(Status) || rsdp == NULL) {
+        Print(L"Error getting ACPI table: %r\n", Status);
+        return Status;
+    } else {
+        boot_param.rsdp_addr = (UINT64)rsdp;
+    }
+
+    Print(L"Boot 3\n");
+
     struct MemoryMap MemoryMap = {4096, NULL, 4096, 0, 0, 0};
     Status = uefi_call_wrapper(SystemTable->BootServices->AllocatePool, 3, EfiLoaderData, MemoryMap.BufferSize, &MemoryMap.Buffer);
     if (EFI_ERROR(Status)) {
@@ -158,7 +196,7 @@
         Print(L"Failed to get memory map.\n");
         return Status;
     }
-    Print(L"Boot 3\n");
+    Print(L"Boot r4\n");
 
     Print(L"Kernel Entry: %llx\n",boot_param.kernel_entry);
     Print(L"RSDP Address: %llx\n",boot_param.rsdp_addr);