From 1b4d5fb6a46e3fd43631b8acea349103bab6a8eb Mon Sep 17 00:00:00 2001 From: pepepper Date: Fri, 9 Apr 2021 18:13:55 +0900 Subject: [PATCH] some modify --- .gitignore | 4 ++ LICENSE | 0 README.md | 2 +- elf.c | 30 ++++++++ elf.h | 3 + fb.c | 0 fb.h | 0 font.h | 0 gdt.c | 0 gdt.h | 0 int_handler.s | 0 interrupt.c | 91 ++++++++++++++++++------- interrupt.h | 0 io.c | 30 ++++++++ io.h | 0 keycode.h | 40 +++++++++++ main.c | 174 +++++++++++++++++++++++++++++++++++++++++------ memory.c | 5 ++ mkramfs/Makefile | 8 +++ mkramfs/main.cpp | 37 ++++++++++ note.md | 0 pci.c | 51 ++++++++++++++ ramfs.c | 56 +++++++++++++++ ramfs.h | 15 ++++ sprinkleos.dec | 0 sprinkleos.dsc | 6 +- sprinkleos.inf | 8 ++- testapp/main.c | 3 + text.c | 55 ++++++++++++++- text.h | 6 +- 30 files changed, 569 insertions(+), 55 deletions(-) mode change 100644 => 100755 .gitignore mode change 100644 => 100755 LICENSE mode change 100644 => 100755 README.md create mode 100755 elf.c create mode 100755 elf.h mode change 100644 => 100755 fb.c mode change 100644 => 100755 fb.h mode change 100644 => 100755 font.h mode change 100644 => 100755 gdt.c mode change 100644 => 100755 gdt.h mode change 100644 => 100755 int_handler.s mode change 100644 => 100755 interrupt.c mode change 100644 => 100755 interrupt.h mode change 100644 => 100755 io.c mode change 100644 => 100755 io.h create mode 100755 keycode.h mode change 100644 => 100755 main.c create mode 100755 memory.c create mode 100755 mkramfs/Makefile create mode 100755 mkramfs/main.cpp mode change 100644 => 100755 note.md create mode 100755 pci.c create mode 100755 ramfs.c create mode 100755 ramfs.h mode change 100644 => 100755 sprinkleos.dec mode change 100644 => 100755 sprinkleos.dsc mode change 100644 => 100755 sprinkleos.inf create mode 100755 testapp/main.c mode change 100644 => 100755 text.c mode change 100644 => 100755 text.h diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 index c6f9a44..65596bd --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ .vscode/settings.json +**/a.out +mkramfs/ramfs.img +mkramfs/mkramfs +FAT/ diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 10af839..fffb378 --- a/README.md +++ b/README.md @@ -18,4 +18,4 @@ Challenge of handmade OS. Conf/target.txtのACTIVE_PLATFORMをsprinkleos/sprinkleos.dscに TARGET_ARCHをX64に TOOL_CHAIN_TAGをGCC5に ```build``` 出来上がったBuild/SprinkleOSX64/DEBUG_GCC5/X64/sprinkleos.efiを上手いこと起動 ---> \ No newline at end of file +--> diff --git a/elf.c b/elf.c new file mode 100755 index 0000000..4cbde4a --- /dev/null +++ b/elf.c @@ -0,0 +1,30 @@ +#include "elf.h" +#include "text.h" +#include + +void run(char *elfdata) { + if (elfdata[EI_CLASS] == ELFCLASS32) { + Elf32_Ehdr elf32header; + CopyMem(&elf32header, elfdata, sizeof(elf32header)); + if (!IS_ELF(elf32header)) { + print("This is not ELF file!"); + } + if (elf32header.e_ident[EI_OSABI] != ELFOSABI_LINUX && + elf32header.e_ident[EI_OSABI] != ELFOSABI_NONE) { + print("This ELF file is not valid OS ABI!"); + } + if (elf32header.e_machine != EM_386 && + elf32header.e_machine != EM_486 && + elf32header.e_machine != EM_X86_64) { + print("This ELF file is not for x86 or x86_64!"); + } + if (elf32header.e_type == ET_REL) { + } else if (elf32header.e_type == ET_EXEC) { + } else if (elf32header.e_type == ET_DYN) { + } + + } else if (elfdata[EI_CLASS] == ELFCLASS64) { + Elf64_Ehdr elf64header; + CopyMem(&elf64header, elfdata, sizeof(elf64header)); + } +} \ No newline at end of file diff --git a/elf.h b/elf.h new file mode 100755 index 0000000..a9d744e --- /dev/null +++ b/elf.h @@ -0,0 +1,3 @@ +#include <../BaseTools/Source/C/GenFw/elf_common.h> +#include <../BaseTools/Source/C/GenFw/elf32.h> +#include <../BaseTools/Source/C/GenFw/elf64.h> diff --git a/fb.c b/fb.c old mode 100644 new mode 100755 diff --git a/fb.h b/fb.h old mode 100644 new mode 100755 diff --git a/font.h b/font.h old mode 100644 new mode 100755 diff --git a/gdt.c b/gdt.c old mode 100644 new mode 100755 diff --git a/gdt.h b/gdt.h old mode 100644 new mode 100755 diff --git a/int_handler.s b/int_handler.s old mode 100644 new mode 100755 diff --git a/interrupt.c b/interrupt.c old mode 100644 new mode 100755 index 7e9c4f6..46643bd --- a/interrupt.c +++ b/interrupt.c @@ -1,6 +1,7 @@ #include #include "io.h" +#include "keycode.h" #include "text.h" #define SPECIAL_SHIFT_L 0b00000001 #define SPECIAL_SHIFT_R 0b00000010 @@ -82,41 +83,81 @@ void interrupt_init() { void keyboard_int_proc() { UINT8 data; - // static char keyboard_special_keys = 0; + static BOOLEAN special_flag = FALSE; + static char keyboard_special_keys = 0; data = mapped_io_read(0x64); if ((data & 0b00000001) == 0) // output buffer is not full goto eoi; data = mapped_io_read(0x60); - /* - if (data & 0b10000000){ // key is broken + if (data == 0xE0) { + special_flag = TRUE; + goto eoi; + } + + if (data & 0b10000000) { // key is broken switch (data & 0b01111111) { - case 44: + case 0x2A: keyboard_special_keys &= ~SPECIAL_SHIFT_L; - return; - case 57: + goto eoi; + case 0x36: keyboard_special_keys &= ~SPECIAL_SHIFT_R; - return; - case 58: - keyboard_special_keys &= ~SPECIAL_CTRL_L; - return; - case 64: - keyboard_special_keys &= ~SPECIAL_CTRL_R; - return; - case 60: - keyboard_special_keys &= ~SPECIAL_ALT_L; - return; - case 62: - keyboard_special_keys &= ~SPECIAL_ALT_R; - return; - case : - keyboard_special_keys &= ~SPECIAL_CTRL_L; - return; + goto eoi; + case 0x1D: + if (!special_flag) + keyboard_special_keys &= ~SPECIAL_CTRL_L; + else + keyboard_special_keys &= ~SPECIAL_CTRL_R; + goto eoi; + case 0x38: + if (!special_flag) + keyboard_special_keys &= ~SPECIAL_ALT_L; + else + keyboard_special_keys &= ~SPECIAL_ALT_R; + goto eoi; + case 0x5B: + if (special_flag) keyboard_special_keys &= ~SPECIAL_SUPER_L; + goto eoi; + case 0x5C: + if (special_flag) keyboard_special_keys &= ~SPECIAL_SUPER_L; + goto eoi; + } + } else { // key is make + switch (data & 0b01111111) { + case 0x2A: + keyboard_special_keys |= SPECIAL_SHIFT_L; + goto eoi; + case 0x36: + keyboard_special_keys |= SPECIAL_SHIFT_R; + goto eoi; + case 0x1D: + if (!special_flag) + keyboard_special_keys |= SPECIAL_CTRL_L; + else + keyboard_special_keys |= SPECIAL_CTRL_R; + goto eoi; + case 0x38: + if (!special_flag) + keyboard_special_keys |= SPECIAL_ALT_L; + else + keyboard_special_keys |= SPECIAL_ALT_R; + goto eoi; + case 0x5B: + if (special_flag) keyboard_special_keys |= SPECIAL_SUPER_L; + goto eoi; + case 0x5C: + if (special_flag) keyboard_special_keys |= SPECIAL_SUPER_L; + goto eoi; default: - return; + if ((keyboard_special_keys & SPECIAL_SHIFT_L) || + (keyboard_special_keys & SPECIAL_SHIFT_R)) { + if (keycode_to_ascii_shift[data]) + textout(keycode_to_ascii_shift[data]); + } else if (keycode_to_ascii[data]) + textout(keycode_to_ascii[data]); + + goto eoi; } } - */ - textout('0'); eoi: end_of_interrupt(33); } \ No newline at end of file diff --git a/interrupt.h b/interrupt.h old mode 100644 new mode 100755 diff --git a/io.c b/io.c old mode 100644 new mode 100755 index 91b1c0a..40f6e3c --- a/io.c +++ b/io.c @@ -8,3 +8,33 @@ UINT8 mapped_io_read(UINT16 addr) { void mapped_io_write(UINT16 addr, UINT8 data) { asm volatile("outb %0,%1" ::"a"(data), "d"(addr)); } + +UINT16 mapped_io_read_word(UINT16 addr) { + UINT16 data; + asm volatile("inw %1,%0" : "=a"(data) : "d"(addr)); + return data; +} + +void mapped_io_write_word(UINT16 addr, UINT16 data) { + asm volatile("outw %0,%1" ::"a"(data), "d"(addr)); +} + +UINT32 mapped_io_read_dword(UINT16 addr) { + UINT32 data; + asm volatile("inl %1,%0" : "=a"(data) : "d"(addr)); + return data; +} + +void mapped_io_write_dword(UINT16 addr, UINT32 data) { + asm volatile("outl %0,%1" ::"a"(data), "d"(addr)); +} + +UINT64 mapped_io_read_qword(UINT16 addr) { + UINT64 data; + asm volatile("inq %1,%0" : "=a"(data) : "d"(addr)); + return data; +} + +void mapped_io_write_qword(UINT16 addr, UINT64 data) { + asm volatile("outq %0,%1" ::"a"(data), "d"(addr)); +} diff --git a/io.h b/io.h old mode 100644 new mode 100755 diff --git a/keycode.h b/keycode.h new file mode 100755 index 0000000..4f40b2c --- /dev/null +++ b/keycode.h @@ -0,0 +1,40 @@ +char keycode_to_ascii[] = { + 0, 0, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '^', + 0, 0, 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '@', '[', + '\n', 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', ':', 0, + 0, ']', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, '*', + 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, '\\', 0, 0, 0, 0, 0, 0, 0, 0, 0, '\\', + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0}; + +char keycode_to_ascii_shift[] = { + 0, 0, '!', '"', '#', '$', '%', '&', '\'', '(', ')', 0, '=', '~', 0, + 0, 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '`', '{', 0, 0, + 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', '+', '*', 0, 0, '}', 'Z', + 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 0, '*', 0, ' ', 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', + '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '_', 0, 0, 0, 0, + 0, 0, 0, 0, 0, '|', 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0}; \ No newline at end of file diff --git a/main.c b/main.c old mode 100644 new mode 100755 index 9c6fbbf..40359b5 --- a/main.c +++ b/main.c @@ -9,20 +9,68 @@ #include #include */ - +#include #include #include #include "fb.h" #include "gdt.h" #include "interrupt.h" +#include "ramfs.h" #include "text.h" +#pragma pack(1) +typedef struct { + EFI_ACPI_DESCRIPTION_HEADER header; + EFI_ACPI_DESCRIPTION_HEADER *entry[0]; +} XSDT; + +typedef struct { + EFI_ACPI_DESCRIPTION_HEADER header; + UINT32 EventTimerBlockID; + EFI_ACPI_2_0_GENERIC_ADDRESS_STRUCTURE BaseAddress; + UINT8 HPETNumber; + UINT16 MinimumTick; + UINT8 Flags; +} HPET; +#pragma pack() + +EFI_ACPI_DESCRIPTION_HEADER *Get_SDT(EFI_SYSTEM_TABLE *SystemTable, + UINT64 Signature) { + EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER *ACPI_Table; + EFI_GUID gEfiAcpiTableGuid = EFI_ACPI_TABLE_GUID; + for (UINTN i = 0; i < SystemTable->NumberOfTableEntries; i++) { + EFI_CONFIGURATION_TABLE *table = SystemTable->ConfigurationTable + i; + if (table->VendorGuid.Data1 == gEfiAcpiTableGuid.Data1 && + table->VendorGuid.Data2 == gEfiAcpiTableGuid.Data2 && + table->VendorGuid.Data3 == gEfiAcpiTableGuid.Data3 && + table->VendorGuid.Data4[0] == gEfiAcpiTableGuid.Data4[0] && + table->VendorGuid.Data4[1] == gEfiAcpiTableGuid.Data4[1] && + table->VendorGuid.Data4[2] == gEfiAcpiTableGuid.Data4[2] && + table->VendorGuid.Data4[3] == gEfiAcpiTableGuid.Data4[3] && + table->VendorGuid.Data4[4] == gEfiAcpiTableGuid.Data4[4] && + table->VendorGuid.Data4[5] == gEfiAcpiTableGuid.Data4[5] && + table->VendorGuid.Data4[6] == gEfiAcpiTableGuid.Data4[6] && + table->VendorGuid.Data4[7] == gEfiAcpiTableGuid.Data4[7]) { + ACPI_Table = (EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER *) + table->VendorTable; + } + } + XSDT *xsdt = (XSDT *)ACPI_Table->XsdtAddress; + + for (int i = 0; + i < (xsdt->header.Length - sizeof(EFI_ACPI_DESCRIPTION_HEADER)) / + sizeof(EFI_ACPI_DESCRIPTION_HEADER *); + i++) { + if (xsdt->entry[i]->Signature == Signature) return xsdt->entry[i]; + } + return NULL; +} + EFI_STATUS EFIAPI UefiMain(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable) { EFI_STATUS Status; EFI_GRAPHICS_OUTPUT_PROTOCOL *gop; - EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fsp; // EFI_MP_SERVICES_PROTOCOL *mps; SystemTable->BootServices->SetWatchdogTimer( @@ -37,14 +85,6 @@ EFI_STATUS EFIAPI UefiMain(IN EFI_HANDLE ImageHandle, return Status; } - Status = SystemTable->BootServices->LocateProtocol( - &gEfiSimpleFileSystemProtocolGuid, NULL, (VOID **)&fsp); - if (EFI_ERROR(Status)) { - Print(L"Error:Failed to locate EFI Simple FileSystem Protocol - %r", - Status); - return Status; - } - /* Status = SystemTable->BootServices->LocateProtocol( &gEfiMpServiceProtocolGuid, NULL, @@ -54,22 +94,119 @@ EFI_STATUS EFIAPI UefiMain(IN EFI_HANDLE ImageHandle, Status); return Status; } */ - + // HPET *hpet_table = (HPET *)Get_SDT( + // SystemTable, EFI_ACPI_3_0_HIGH_PRECISION_EVENT_TIMER_TABLE_SIGNATURE); FrameBuffer_init(gop); + ramfsinit(SystemTable); UINTN MemoryMapSize = 1, MapKey, DescriptorSize; UINT32 DescriptorVersion; + EFI_MEMORY_DESCRIPTOR *mem_desc; do { - EFI_MEMORY_DESCRIPTOR *mem_desc = - (EFI_MEMORY_DESCRIPTOR *)AllocatePool(MemoryMapSize); + Status = SystemTable->BootServices->AllocatePool( + EfiLoaderData, MemoryMapSize, (void **)&mem_desc); + if (EFI_ERROR(Status)) { + Print(L"Error:Failed to allocate memory map memory - %r\n", Status); + continue; + } Status = SystemTable->BootServices->GetMemoryMap( &MemoryMapSize, mem_desc, &MapKey, &DescriptorSize, &DescriptorVersion); - if (EFI_ERROR(Status)) continue; + if (EFI_ERROR(Status)) { + Print(L"Error:Failed to get memory map - %r\n", Status); + SystemTable->BootServices->FreePool(mem_desc); + continue; + } Status = SystemTable->BootServices->ExitBootServices(ImageHandle, MapKey); - } while (EFI_ERROR(Status)); + if (EFI_ERROR(Status)) { + Print(L"Error:Failed to exit boot service - %r\n", Status); + SystemTable->BootServices->FreePool(mem_desc); + continue; + } + break; + } while (TRUE); + for (int y = 0; y < buffer.VerticalRes; y++) { + for (int x = 0; x < buffer.HorizontalRes; x++) { + EFI_GRAPHICS_OUTPUT_BLT_PIXEL *pixel = + (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)buffer.BaseAddress + + (buffer.HorizontalRes * y) + x; + RGBtoPixel(pixel, 0x000000); + } + } + for (UINTN i = (UINTN)mem_desc; i < ((UINTN)mem_desc + MemoryMapSize); + i += DescriptorSize) { + ((EFI_MEMORY_DESCRIPTOR *)i)->VirtualStart = + ((EFI_MEMORY_DESCRIPTOR *)i)->PhysicalStart; + } + SystemTable->RuntimeServices->SetVirtualAddressMap( + MemoryMapSize, DescriptorSize, DescriptorVersion, mem_desc); + for (UINTN i = (UINTN)mem_desc; i < ((UINTN)mem_desc + MemoryMapSize); + i += DescriptorSize) { + print("Type:"); + switch (((EFI_MEMORY_DESCRIPTOR *)i)->Type) { + case EfiReservedMemoryType: + print("EfiReservedMemoryType"); + break; + case EfiLoaderCode: + print("EfiLoaderCode"); + break; + case EfiLoaderData: + print("EfiLoaderData"); + break; + case EfiBootServicesCode: + print("EfiBootServicesCode"); + break; + case EfiBootServicesData: + print("EfiBootServicesData"); + break; + case EfiRuntimeServicesCode: + print("EfiRuntimeServicesCode"); + break; + case EfiRuntimeServicesData: + print("EfiRuntimeServicesData"); + break; + case EfiConventionalMemory: + print("EfiConventionalMemory"); + break; + case EfiUnusableMemory: + print("EfiUnusableMemory"); + break; + case EfiACPIReclaimMemory: + print("EfiACPIReclaimMemory"); + break; + case EfiACPIMemoryNVS: + print("EfiACPIMemoryNVS"); + break; + case EfiMemoryMappedIO: + print("EfiMemoryMappedIO"); + break; + case EfiMemoryMappedIOPortSpace: + print("EfiMemoryMappedIOPortSpace"); + break; + case EfiPalCode: + print("EfiPalCode"); + break; + case EfiPersistentMemory: + print("EfiPersistentMemory"); + break; + case EfiMaxMemoryType: + print("EfiMaxMemoryType"); + break; + } + print(" Physical:"); + printhex(((EFI_MEMORY_DESCRIPTOR *)i)->PhysicalStart, 0); + print(" Virtual:"); + printhex(((EFI_MEMORY_DESCRIPTOR *)i)->VirtualStart, 0); + print(" Pages:"); + printhex(((EFI_MEMORY_DESCRIPTOR *)i)->NumberOfPages, 0); + print(" Attribute:"); + printhex(((EFI_MEMORY_DESCRIPTOR *)i)->Attribute, 0); + print("\n"); + } + GDT_init(); interrupt_init(); + /* for (int y = 0; y < buffer.VerticalRes; y++) { for (int x = 0; x < buffer.HorizontalRes; x++) { EFI_GRAPHICS_OUTPUT_BLT_PIXEL *pixel = @@ -77,12 +214,7 @@ EFI_STATUS EFIAPI UefiMain(IN EFI_HANDLE ImageHandle, (buffer.HorizontalRes * y) + x; RGBtoPixel(pixel, 0xFF0000); } - } - textout('A'); - textout('p'); - textout('p'); - textout('l'); - textout('e'); + }*/ while (1) asm volatile("hlt"); return Status; } \ No newline at end of file diff --git a/memory.c b/memory.c new file mode 100755 index 0000000..39c0b31 --- /dev/null +++ b/memory.c @@ -0,0 +1,5 @@ +#include +#include +#include + +void VMemInit() { UINTN *PageTable = (UINTN *)AsmReadCr3(); } \ No newline at end of file diff --git a/mkramfs/Makefile b/mkramfs/Makefile new file mode 100755 index 0000000..ac3e08f --- /dev/null +++ b/mkramfs/Makefile @@ -0,0 +1,8 @@ +CC := g++ +CFLAGS := -std=c++17 + + +mkramfs: main.cpp + $(CC) $(CFLAGS) -o mkramfs main.cpp + +clean: $(RM) mkramfs \ No newline at end of file diff --git a/mkramfs/main.cpp b/mkramfs/main.cpp new file mode 100755 index 0000000..25ff13a --- /dev/null +++ b/mkramfs/main.cpp @@ -0,0 +1,37 @@ +#include + +#include +#include +#include +typedef struct { + int Start; + int Size; + char name[256]; +} FILE_DESC; + +int main(int argc, char **argv) { + if (argc == 1) { + std::cout << "Usage:mkramfs ..." << std::endl; + return -1; + } + FILE_DESC descriptor[256] = {0}; + std::ofstream ramfs("ramfs.img", std::ios::binary | std::ios::ate); + for (int i = 1; i < argc; i++) { + if (!std::filesystem::exists(std::filesystem::path(argv[i]))) { + std::cout << "file does not exist! - " << argv[i] << std::endl; + continue; + } + descriptor[i].Start = descriptor[i - 1].Start + descriptor[i - 1].Size; + descriptor[i].Size = + std::filesystem::file_size(std::filesystem::path(argv[i])); + strncpy(descriptor[i].name, + std::filesystem::path(argv[i]).filename().c_str(), 255); + } + ramfs.write(reinterpret_cast(descriptor), sizeof(descriptor)); + for (int i = 1; i < argc; i++) { + std::ifstream file(argv[i], std::ios::binary); + ramfs << file.rdbuf(); + file.close(); + } + ramfs.close(); +} \ No newline at end of file diff --git a/note.md b/note.md old mode 100644 new mode 100755 diff --git a/pci.c b/pci.c new file mode 100755 index 0000000..7e2518f --- /dev/null +++ b/pci.c @@ -0,0 +1,51 @@ +#include +#include +#include + +typedef struct { + BOOLEAN isUsing; + BOOLEAN isIDE; // T:IDE F:AHCI + UINT8 bus; + UINT8 device; + UINT8 function; +} DiskController; + +DiskController controllers[5]; + +void init_pci() { + for (UINT8 bus = 0; bus < 256; bus++) { + for (UINT8 device = 0; device < 32; device++) { + for (UINT8 function = 0; function < 8; function++) { + UINT32 ConfigData = + PciCf8Read32(PCI_CF8_LIB_ADDRESS(bus, device, function, 0)); + if (ConfigData != 0xffffffff) { + ConfigData = PciCf8Read32( + PCI_CF8_LIB_ADDRESS(bus, device, function, 8)); + if ((ConfigData >> 16) == 0x0101) { // IDE Controller + int n = 0; + while (controllers[n].isUsing) { + n++; + } + controllers[n].isUsing = TRUE; + controllers[n].isIDE = TRUE; + controllers[n].bus = bus; + controllers[n].device = device; + controllers[n].function = function; + return; + } else if ((ConfigData >> 8) == + 0x010601) { // AHCI Controller + int n = 0; + while (controllers[n].isUsing) { + n++; + } + controllers[n].isUsing = TRUE; + controllers[n].isIDE = FALSE; + controllers[n].bus = bus; + controllers[n].device = device; + controllers[n].function = function; + } + } + } + } + } +} \ No newline at end of file diff --git a/ramfs.c b/ramfs.c new file mode 100755 index 0000000..2981ffd --- /dev/null +++ b/ramfs.c @@ -0,0 +1,56 @@ +#include "ramfs.h" + +#include +#include +#include +#include +#include +#include + +#include "text.h" +RAMFS_DATA *data; +void ramfsinit( + EFI_SYSTEM_TABLE *SystemTable) { // must call before ExitBootServices + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fsp; + EFI_FILE_PROTOCOL *Root, *ramfs; + EFI_FILE_INFO info; + UINTN infosize = sizeof(info), datasize; + + EFI_STATUS Status = SystemTable->BootServices->LocateProtocol( + &gEfiSimpleFileSystemProtocolGuid, NULL, (VOID **)&fsp); + if (EFI_ERROR(Status)) { + Print(L"Error:Failed to locate EFI Simple FileSystem Protocol - %r", + Status); + return; + } + + fsp->OpenVolume(fsp, &Root); + Status = Root->Open(Root, &ramfs, L"ramfs.img", EFI_FILE_MODE_READ, 0); + if (EFI_ERROR(Status)) { + Print(L"Error:Failed to open ramfs file - %r\n", Status); + return; + } + ramfs->GetInfo(ramfs, &gEfiFileInfoGuid, &infosize, &info); + Status = SystemTable->BootServices->AllocatePool( + EfiLoaderData, info.FileSize, (void **)&data); + if (EFI_ERROR(Status)) { + Print(L"Error:Failed to allocate ramfs memory - %r\n", Status); + return; + } + datasize = info.FileSize; + ramfs->Read(ramfs, &datasize, data); +} + +void getfilesize(char *filename, UINTN *size) { + int index; + for (index = 0; index < 256; index++) + if (!AsciiStrCmp(data->descriptor[index].name, filename)) break; + *size = data->descriptor[index].Size; +} + +void readfile(char *filename, UINTN buffersize, void *buffer) { + int index; + for (index = 0; index < 256; index++) + if (!AsciiStrCmp(data->descriptor[index].name, filename)) break; + CopyMem(buffer, data->data + data->descriptor[index].Start, buffersize); +} \ No newline at end of file diff --git a/ramfs.h b/ramfs.h new file mode 100755 index 0000000..4d1b732 --- /dev/null +++ b/ramfs.h @@ -0,0 +1,15 @@ +#include +typedef struct { + int Start; + int Size; + char name[256]; +} FILE_DESC; + +typedef struct { + FILE_DESC descriptor[256]; + char data[]; +} RAMFS_DATA; + +void ramfsinit(EFI_SYSTEM_TABLE *SystemTable); +void getfilesize(char *filename, UINTN *size); +void readfile(char *filename, UINTN buffersize, void *buffer); \ No newline at end of file diff --git a/sprinkleos.dec b/sprinkleos.dec old mode 100644 new mode 100755 diff --git a/sprinkleos.dsc b/sprinkleos.dsc old mode 100644 new mode 100755 index 262c516..8d13717 --- a/sprinkleos.dsc +++ b/sprinkleos.dsc @@ -30,6 +30,8 @@ MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf - + PciCf8Lib|MdePkg/Library/BasePciCf8Lib/BasePciCf8Lib.inf + IoLib|MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf + RegisterFilterLib|MdePkg/Library/RegisterFilterLibNull/RegisterFilterLibNull.inf [Components] - sprinkleos/sprinkleos.inf \ No newline at end of file + sprinkleos/sprinkleos.inf diff --git a/sprinkleos.inf b/sprinkleos.inf old mode 100644 new mode 100755 index 017a210..a91abdd --- a/sprinkleos.inf +++ b/sprinkleos.inf @@ -14,10 +14,16 @@ io.c text.c int_handler.s + ramfs.c + elf.c [Packages] MdePkg/MdePkg.dec [LibraryClasses] UefiLib - UefiApplicationEntryPoint \ No newline at end of file + UefiApplicationEntryPoint + PciCf8Lib + +[Guids] + gEfiFileInfoGuid \ No newline at end of file diff --git a/testapp/main.c b/testapp/main.c new file mode 100755 index 0000000..1d20be4 --- /dev/null +++ b/testapp/main.c @@ -0,0 +1,3 @@ +int main(){ +return 0; +} diff --git a/text.c b/text.c old mode 100644 new mode 100755 index b38780f..6cc4700 --- a/text.c +++ b/text.c @@ -57,6 +57,11 @@ void textinit(EFI_SIMPLE_FILE_SYSTEM_PROTOCOL* fsp) { void textout(UINT8 code) { static UINTN x = 0, y = 0; + if (code == '\n') { + x = 0; + y++; + return; + } for (int row = 0; row < 13; row++) { for (int column = 0; column < 8; column++) { EFI_GRAPHICS_OUTPUT_BLT_PIXEL *pixel = @@ -64,7 +69,7 @@ void textout(UINT8 code) { y * 13 * buffer.HorizontalRes + row * buffer.HorizontalRes + 8 * x + column; if ((font[code][row] >> (7 - column)) & 0x1) - RGBtoPixel(pixel, 0x00FF00); + RGBtoPixel(pixel, 0xFFFFFF); } } if (x < (buffer.HorizontalRes / 8) - 1) @@ -73,4 +78,50 @@ void textout(UINT8 code) { x = 0; y++; } -} \ No newline at end of file +} + +void print(char *string) { + do { + textout(*string); + string++; + } while (*string != '\0'); +} + +UINT64 pow(UINT64 base, UINT64 exponent) { + UINT64 ans = 1; + for (UINT64 i = 0; i < exponent; i++) { + ans *= base; + } + return ans; +} + +void printhex(UINT64 num, UINT8 digit) { + if (digit == 0) { + UINT8 maxdigit = 0; + while (num >= pow(16, maxdigit)) maxdigit++; + print("0x"); + if (maxdigit == 0) textout('0' + num); + while (maxdigit) { + UINT64 weight = pow(16, maxdigit - 1); + UINT64 ans = num / weight; + if (ans < 10) + textout('0' + ans); + else + textout('A' + ans - 10); + num -= ans * weight; + maxdigit--; + } + } else { + print("0x"); + while (digit) { + UINT64 weight = pow(16, digit - 1); + UINT64 ans = num / weight; + if (ans < 10) + textout('0' + ans); + else + textout('A' + ans - 10); + num -= ans * weight; + digit--; + } + } +} diff --git a/text.h b/text.h old mode 100644 new mode 100755 index 605f875..d598c39 --- a/text.h +++ b/text.h @@ -1,6 +1,6 @@ -#include #include #include -// extern char keycode[]; -void textout(UINT8 code); \ No newline at end of file +void textout(UINT8 code); +void print(char *string); +void printhex(UINT64 num, UINT8 digit); \ No newline at end of file