From 179b2fcaeb7d1ce1710c4957c2b9848f3f60bbb8 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sat, 4 Jan 2025 12:04:32 +0100 Subject: ADD: AHCI Metadata support, and working towards I/O support. Signed-off-by: Amlal El Mahrouss --- dev/Boot/amd64-desktop.make | 4 +- dev/Boot/src/HEL/AMD64/BootMain.cc | 322 --------------------------- dev/Boot/src/HEL/AMD64/EFIBootStartup.cc | 324 ++++++++++++++++++++++++++++ dev/Boot/src/HEL/ARM64/BootMain.cc | 76 ------- dev/Boot/src/HEL/ARM64/CoreBootStartup.S | 36 ++++ dev/Boot/src/HEL/ARM64/EFIBootStartup.cc | 76 +++++++ dev/Kernel/FSKit/NeFS.h | 1 + dev/Kernel/HALKit/AMD64/Storage/AHCI-DMA.cc | 319 --------------------------- dev/Kernel/HALKit/AMD64/Storage/ATA-PIO.cc | 2 +- dev/Kernel/HALKit/AMD64/Storage/SATA-DMA.cc | 276 ++++++++++++++++++++++++ dev/Kernel/src/DriveMgr.cc | 4 +- dev/Kernel/src/HardwareThreadScheduler.cc | 1 - dev/Kernel/src/UserProcessScheduler.cc | 1 - dev/Mod/AHCI/AHCI.h | 2 +- 14 files changed, 718 insertions(+), 726 deletions(-) delete mode 100644 dev/Boot/src/HEL/AMD64/BootMain.cc create mode 100644 dev/Boot/src/HEL/AMD64/EFIBootStartup.cc delete mode 100644 dev/Boot/src/HEL/ARM64/BootMain.cc create mode 100644 dev/Boot/src/HEL/ARM64/CoreBootStartup.S create mode 100644 dev/Boot/src/HEL/ARM64/EFIBootStartup.cc delete mode 100644 dev/Kernel/HALKit/AMD64/Storage/AHCI-DMA.cc create mode 100644 dev/Kernel/HALKit/AMD64/Storage/SATA-DMA.cc (limited to 'dev') diff --git a/dev/Boot/amd64-desktop.make b/dev/Boot/amd64-desktop.make index d56d5d78..e1d6b113 100644 --- a/dev/Boot/amd64-desktop.make +++ b/dev/Boot/amd64-desktop.make @@ -83,7 +83,7 @@ compile-amd64: .PHONY: run-efi-amd64-ahci run-efi-amd64-ahci: - $(EMU) $(EMU_FLAGS) -hdd $(IMG) + $(EMU) $(EMU_FLAGS) -hda $(IMG) .PHONY: run-efi-amd64-ata run-efi-amd64-ata: @@ -93,8 +93,6 @@ run-efi-amd64-ata: .PHONY: epm-img epm-img: qemu-img create -f raw $(IMG) 4G - qemu-img create -f raw $(IMG_2) 4G - qemu-img create -f raw $(IMG_3) 4G .PHONY: download-edk download-edk: diff --git a/dev/Boot/src/HEL/AMD64/BootMain.cc b/dev/Boot/src/HEL/AMD64/BootMain.cc deleted file mode 100644 index f2d6276b..00000000 --- a/dev/Boot/src/HEL/AMD64/BootMain.cc +++ /dev/null @@ -1,322 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024, Theater Quality Corp, all rights reserved. - -------------------------------------------- */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -// Makes the compiler shut up. -#ifndef kMachineModel -#define kMachineModel "ZkaOS" -#endif // !kMachineModel - -#ifndef kExpectedWidth -#define kExpectedWidth (1280) -#endif - -#ifndef kExpectedHeight -#define kExpectedHeight (720) -#endif - -/** Graphics related. */ - -STATIC EfiGraphicsOutputProtocol* kGop = nullptr; -STATIC UInt16 kGopStride = 0U; -STATIC EfiGUID kGopGuid; - -EXTERN_C Void rt_reset_hardware(); - -EXTERN EfiBootServices* BS; - -/** - @brief Finds and stores the GOP object. -*/ -STATIC Bool boot_init_fb() noexcept -{ - kGopGuid = EfiGUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); - kGop = nullptr; - - if (BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*)&kGop) != kEfiOk) - return No; - - kGopStride = 4; - - for (SizeT i = 0; i < kGop->Mode->MaxMode; ++i) - { - EfiGraphicsOutputProtocolModeInformation* infoPtr = nullptr; - UInt32 sz = 0U; - - kGop->QueryMode(kGop, i, &sz, &infoPtr); - - if (infoPtr->HorizontalResolution == kExpectedWidth && - infoPtr->VerticalResolution == kExpectedHeight) - { - kGop->SetMode(kGop, i); - return Yes; - } - } - - return No; -} - -EXTERN_C VoidPtr boot_read_cr3(); -EXTERN_C Void boot_write_cr3(VoidPtr new_cr3); - -EXTERN EfiBootServices* BS; - -/// @brief Main EFI entrypoint. -/// @param image_handle Handle of this image. -/// @param sys_table The system table of it. -/// @return nothing, never returns. -EFI_EXTERN_C EFI_API Int32 Main(EfiHandlePtr image_handle, - EfiSystemTable* sys_table) -{ - InitEFI(sys_table); ///! Init the EFI library. - - HEL::BootInfoHeader* handover_hdr = - new HEL::BootInfoHeader(); - - UInt32 map_key = 0; - UInt32 size_struct_ptr = sizeof(EfiMemoryDescriptor); - EfiMemoryDescriptor* struct_ptr = nullptr; - UInt32 sz_desc = sizeof(EfiMemoryDescriptor); - UInt32 rev_desc = 0; - -#ifdef ZBA_USE_FB - if (!boot_init_fb()) - return 1; ///! Init the GOP. - - for (SizeT index_vt = 0; index_vt < sys_table->NumberOfTableEntries; - ++index_vt) - { - Char* vendor_table = reinterpret_cast( - sys_table->ConfigurationTable[index_vt].VendorTable); - - // ACPI's 'RSD PTR', which contains the ACPI SDT (MADT, FACP...) - if (vendor_table[0] == 'R' && vendor_table[1] == 'S' && - vendor_table[2] == 'D' && vendor_table[3] == ' ' && - vendor_table[4] == 'P' && vendor_table[5] == 'T' && - vendor_table[6] == 'R' && vendor_table[7] == ' ') - { - handover_hdr->f_HardwareTables.f_VendorPtr = (VoidPtr)vendor_table; - break; - } - } - - // ------------------------------------------ // - // draw background color. - // ------------------------------------------ // - - handover_hdr->f_GOP.f_The = kGop->Mode->FrameBufferBase; - handover_hdr->f_GOP.f_Width = kGop->Mode->Info->VerticalResolution; - handover_hdr->f_GOP.f_Height = kGop->Mode->Info->HorizontalResolution; - handover_hdr->f_GOP.f_PixelPerLine = kGop->Mode->Info->PixelsPerScanLine; - handover_hdr->f_GOP.f_PixelFormat = kGop->Mode->Info->PixelFormat; - handover_hdr->f_GOP.f_Size = kGop->Mode->FrameBufferSize; -#endif // ZBA_USE_FB - - // ------------------------------------------- // - // Grab MP services, extended to runtime. // - // ------------------------------------------- // - - EfiGUID guid_mp = EfiGUID(EFI_MP_SERVICES_PROTOCOL_GUID); - EfiMpServicesProtocol* mp = nullptr; - - BS->LocateProtocol(&guid_mp, nullptr, reinterpret_cast(&mp)); - - handover_hdr->f_HardwareTables.f_MpPtr = reinterpret_cast(mp); - - kHandoverHeader = handover_hdr; - - fb_init(); - - UI::fb_clear_video(); - - FBDrawBitMapInRegion(zka_disk, ZKA_DISK_WIDTH, ZKA_DISK_HEIGHT, (kHandoverHeader->f_GOP.f_Width - ZKA_DISK_WIDTH) / 2, (kHandoverHeader->f_GOP.f_Height - ZKA_DISK_HEIGHT) / 2); - - fb_clear(); - - UInt32 cnt_enabled = 0; - UInt32 cnt_disabled = 0; - - mp->GetNumberOfProcessors(mp, &cnt_disabled, &cnt_enabled); - - handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = cnt_enabled > 1; - // Fill handover header now. - - // ---------------------------------------------------- // - // The following checks for an exisiting partition - // inside the disk, if it doesn't have one, - // format the disk. - // ---------------------------------------------------- // - - BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); - - struct_ptr = new EfiMemoryDescriptor[sz_desc]; - - BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); - - auto kDefaultMemoryMap = 0; // Grab any usable entries. - - //-----------------------------------------------------------// - // A simple loop which finds a usable memory region for us. - //-----------------------------------------------------------// - - SizeT lookup_index = 0UL; - - for (; struct_ptr[lookup_index].Kind != EfiMemoryType::EfiConventionalMemory; ++lookup_index) - { - ZKA_UNUSED(0); - } - - kDefaultMemoryMap = lookup_index; - - //-------------------------------------------------------------// - // Update handover file specific table and phyiscal start field. - //-------------------------------------------------------------// - - handover_hdr->f_BitMapStart = nullptr; /* Start of bitmap. */ - handover_hdr->f_BitMapSize = kHandoverBitMapSz; /* Size of bitmap. */ - - while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, &handover_hdr->f_BitMapStart) != kEfiOk) - { - if (handover_hdr->f_BitMapStart) - { - BS->FreePool(handover_hdr->f_BitMapStart); - handover_hdr->f_BitMapStart = nullptr; - } - } - - handover_hdr->f_FirmwareCustomTables[0] = (VoidPtr)BS; - handover_hdr->f_FirmwareCustomTables[1] = (VoidPtr)ST; - - Boot::BFileReader reader_syschk(L"syschk.sys", image_handle); - reader_syschk.ReadAll(0); - - Boot::BootThread* syschk_thread = nullptr; - - // ------------------------------------------ // - // If we succeed in reading the blob, then execute it. - // ------------------------------------------ // - - if (reader_syschk.Blob()) - { - syschk_thread = new Boot::BootThread(reader_syschk.Blob()); - syschk_thread->SetName("BootZ: System Recovery Check"); - } - - Boot::BDiskFormatFactory partition_factory; - - if (syschk_thread->Start(handover_hdr, NO) != kEfiOk) - { - if (partition_factory.IsPartitionValid() == NO) - { - Boot::BDiskFormatFactory::BFileDescriptor root{}; - - root.fFileName[0] = kNeFSRoot[0]; - root.fFileName[1] = 0; - - root.fKind = kNeFSCatalogKindDir; - - const auto kFSName = "SSD"; - - partition_factory.Format(kFSName, &root, 1); - - fb_init(); - - UI::fb_clear_video(); - - FBDrawBitMapInRegion(zka_has_disk, ZKA_HAS_DISK_WIDTH, ZKA_HAS_DISK_HEIGHT, (kHandoverHeader->f_GOP.f_Width - ZKA_HAS_DISK_WIDTH) / 2, (kHandoverHeader->f_GOP.f_Height - ZKA_HAS_DISK_HEIGHT) / 2); - - fb_clear(); - } - } - - // ------------------------------------------ // - // null these fields, to avoid being reused later. - // ------------------------------------------ // - - handover_hdr->f_FirmwareCustomTables[0] = nullptr; - handover_hdr->f_FirmwareCustomTables[1] = nullptr; - - handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); - - handover_hdr->f_Magic = kHandoverMagic; - handover_hdr->f_Version = kHandoverVersion; - - // Provide fimware vendor name. - - Boot::BCopyMem(handover_hdr->f_FirmwareVendorName, sys_table->FirmwareVendor, - handover_hdr->f_FirmwareVendorLen); - - handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); - - // Assign to global 'kHandoverHeader'. - - Boot::BFileReader reader_kernel(L"minoskrnl.exe", image_handle); - - reader_kernel.ReadAll(0); - - Boot::BootThread* kernel_thread = nullptr; - - // ------------------------------------------ // - // If we succeed in reading the blob, then execute it. - // ------------------------------------------ // - - if (reader_kernel.Blob()) - { - kernel_thread = new Boot::BootThread(reader_kernel.Blob()); - kernel_thread->SetName("BootZ: MicroKernel."); - - handover_hdr->f_KernelImage = reader_kernel.Blob(); - } - else - { - fb_init(); - FBDrawBitMapInRegion(zka_no_disk, ZKA_NO_DISK_WIDTH, ZKA_NO_DISK_HEIGHT, (kHandoverHeader->f_GOP.f_Width - ZKA_NO_DISK_WIDTH) / 2, (kHandoverHeader->f_GOP.f_Height - ZKA_NO_DISK_HEIGHT) / 2); - - EFI::Stop(); - } - - Boot::BFileReader ttf_font(L"TQ\\OSFont.ttf", image_handle); - - ttf_font.ReadAll(0); - - if (ttf_font.Blob()) - { - handover_hdr->f_KernelImage = reader_kernel.Blob(); - handover_hdr->f_KernelSz = reader_kernel.Size(); - handover_hdr->f_FontImage = ttf_font.Blob(); - handover_hdr->f_FontSz = ttf_font.Size(); - } - else - { - fb_init(); - FBDrawBitMapInRegion(zka_no_disk, ZKA_NO_DISK_HEIGHT, ZKA_NO_DISK_WIDTH, (kHandoverHeader->f_GOP.f_Width - ZKA_NO_DISK_WIDTH) / 2, (kHandoverHeader->f_GOP.f_Height - ZKA_NO_DISK_HEIGHT) / 2); - - EFI::Stop(); - } - - EFI::ExitBootServices(map_key, image_handle); - - // ---------------------------------------------------- // - // Finally load the OS kernel. - // ---------------------------------------------------- // - - kernel_thread->Start(handover_hdr, YES); - - CANT_REACH(); -} diff --git a/dev/Boot/src/HEL/AMD64/EFIBootStartup.cc b/dev/Boot/src/HEL/AMD64/EFIBootStartup.cc new file mode 100644 index 00000000..5b05e1bb --- /dev/null +++ b/dev/Boot/src/HEL/AMD64/EFIBootStartup.cc @@ -0,0 +1,324 @@ +/* ------------------------------------------- + + Copyright (C) 2024, Theater Quality Corp, all rights reserved. + +------------------------------------------- */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// Makes the compiler shut up. +#ifndef kMachineModel +#define kMachineModel "ZkaOS" +#endif // !kMachineModel + +#ifndef kExpectedWidth +#define kExpectedWidth (1280) +#endif + +#ifndef kExpectedHeight +#define kExpectedHeight (720) +#endif + +/** Graphics related. */ + +STATIC EfiGraphicsOutputProtocol* kGop = nullptr; +STATIC UInt16 kGopStride = 0U; +STATIC EfiGUID kGopGuid; + +EXTERN_C Void rt_reset_hardware(); + +EXTERN EfiBootServices* BS; + +/** + @brief Finds and stores the GOP object. +*/ +STATIC Bool boot_init_fb() noexcept +{ + kGopGuid = EfiGUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); + kGop = nullptr; + + if (BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*)&kGop) != kEfiOk) + return No; + + kGopStride = 4; + + for (SizeT i = 0; i < kGop->Mode->MaxMode; ++i) + { + EfiGraphicsOutputProtocolModeInformation* infoPtr = nullptr; + UInt32 sz = 0U; + + kGop->QueryMode(kGop, i, &sz, &infoPtr); + + if (infoPtr->HorizontalResolution == kExpectedWidth && + infoPtr->VerticalResolution == kExpectedHeight) + { + kGop->SetMode(kGop, i); + return Yes; + } + } + + return No; +} + +EXTERN_C VoidPtr boot_read_cr3(); +EXTERN_C Void boot_write_cr3(VoidPtr new_cr3); + +EXTERN EfiBootServices* BS; + +/// @brief Main EFI entrypoint. +/// @param image_handle Handle of this image. +/// @param sys_table The system table of it. +/// @return nothing, never returns. +EFI_EXTERN_C EFI_API Int32 Main(EfiHandlePtr image_handle, + EfiSystemTable* sys_table) +{ + InitEFI(sys_table); ///! Init the EFI library. + + HEL::BootInfoHeader* handover_hdr = + new HEL::BootInfoHeader(); + + UInt32 map_key = 0; + UInt32 size_struct_ptr = sizeof(EfiMemoryDescriptor); + EfiMemoryDescriptor* struct_ptr = nullptr; + UInt32 sz_desc = sizeof(EfiMemoryDescriptor); + UInt32 rev_desc = 0; + +#ifdef ZBA_USE_FB + if (!boot_init_fb()) + return 1; ///! Init the GOP. + + for (SizeT index_vt = 0; index_vt < sys_table->NumberOfTableEntries; + ++index_vt) + { + Char* vendor_table = reinterpret_cast( + sys_table->ConfigurationTable[index_vt].VendorTable); + + // ACPI's 'RSD PTR', which contains the ACPI SDT (MADT, FACP...) + if (vendor_table[0] == 'R' && vendor_table[1] == 'S' && + vendor_table[2] == 'D' && vendor_table[3] == ' ' && + vendor_table[4] == 'P' && vendor_table[5] == 'T' && + vendor_table[6] == 'R' && vendor_table[7] == ' ') + { + handover_hdr->f_HardwareTables.f_VendorPtr = (VoidPtr)vendor_table; + break; + } + } + + // ------------------------------------------ // + // draw background color. + // ------------------------------------------ // + + handover_hdr->f_GOP.f_The = kGop->Mode->FrameBufferBase; + handover_hdr->f_GOP.f_Width = kGop->Mode->Info->VerticalResolution; + handover_hdr->f_GOP.f_Height = kGop->Mode->Info->HorizontalResolution; + handover_hdr->f_GOP.f_PixelPerLine = kGop->Mode->Info->PixelsPerScanLine; + handover_hdr->f_GOP.f_PixelFormat = kGop->Mode->Info->PixelFormat; + handover_hdr->f_GOP.f_Size = kGop->Mode->FrameBufferSize; +#endif // ZBA_USE_FB + + // ------------------------------------------- // + // Grab MP services, extended to runtime. // + // ------------------------------------------- // + + EfiGUID guid_mp = EfiGUID(EFI_MP_SERVICES_PROTOCOL_GUID); + EfiMpServicesProtocol* mp = nullptr; + + BS->LocateProtocol(&guid_mp, nullptr, reinterpret_cast(&mp)); + + handover_hdr->f_HardwareTables.f_MpPtr = reinterpret_cast(mp); + + kHandoverHeader = handover_hdr; + + fb_init(); + + UI::fb_clear_video(); + + FBDrawBitMapInRegion(zka_disk, ZKA_DISK_WIDTH, ZKA_DISK_HEIGHT, (kHandoverHeader->f_GOP.f_Width - ZKA_DISK_WIDTH) / 2, (kHandoverHeader->f_GOP.f_Height - ZKA_DISK_HEIGHT) / 2); + + fb_clear(); + + UInt32 cnt_enabled = 0; + UInt32 cnt_disabled = 0; + + mp->GetNumberOfProcessors(mp, &cnt_disabled, &cnt_enabled); + + handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = cnt_enabled > 1; + // Fill handover header now. + + // ---------------------------------------------------- // + // The following checks for an exisiting partition + // inside the disk, if it doesn't have one, + // format the disk. + // ---------------------------------------------------- // + + BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); + + struct_ptr = new EfiMemoryDescriptor[sz_desc]; + + BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); + + auto kDefaultMemoryMap = 0; // Grab any usable entries. + + //-----------------------------------------------------------// + // A simple loop which finds a usable memory region for us. + //-----------------------------------------------------------// + + SizeT lookup_index = 0UL; + + for (; struct_ptr[lookup_index].Kind != EfiMemoryType::EfiConventionalMemory; ++lookup_index) + { + ZKA_UNUSED(0); + } + + kDefaultMemoryMap = lookup_index; + + //-------------------------------------------------------------// + // Update handover file specific table and phyiscal start field. + //-------------------------------------------------------------// + + handover_hdr->f_BitMapStart = nullptr; /* Start of bitmap. */ + handover_hdr->f_BitMapSize = kHandoverBitMapSz; /* Size of bitmap. */ + + while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, &handover_hdr->f_BitMapStart) != kEfiOk) + { + if (handover_hdr->f_BitMapStart) + { + BS->FreePool(handover_hdr->f_BitMapStart); + handover_hdr->f_BitMapStart = nullptr; + } + } + + handover_hdr->f_FirmwareCustomTables[0] = (VoidPtr)BS; + handover_hdr->f_FirmwareCustomTables[1] = (VoidPtr)ST; + + Boot::BFileReader reader_syschk(L"syschk.sys", image_handle); + reader_syschk.ReadAll(0); + + Boot::BootThread* syschk_thread = nullptr; + + // ------------------------------------------ // + // If we succeed in reading the blob, then execute it. + // ------------------------------------------ // + + if (reader_syschk.Blob()) + { + syschk_thread = new Boot::BootThread(reader_syschk.Blob()); + syschk_thread->SetName("BootZ: System Recovery Check"); + } + +#if 0 + Boot::BDiskFormatFactory partition_factory; + + if (syschk_thread->Start(handover_hdr, NO) != kEfiOk) + { + if (partition_factory.IsPartitionValid() == NO) + { + Boot::BDiskFormatFactory::BFileDescriptor root{}; + + root.fFileName[0] = kNeFSRoot[0]; + root.fFileName[1] = 0; + + root.fKind = kNeFSCatalogKindDir; + + const auto kFSName = "SSD"; + + partition_factory.Format(kFSName, &root, 1); + + fb_init(); + + UI::fb_clear_video(); + + FBDrawBitMapInRegion(zka_has_disk, ZKA_HAS_DISK_WIDTH, ZKA_HAS_DISK_HEIGHT, (kHandoverHeader->f_GOP.f_Width - ZKA_HAS_DISK_WIDTH) / 2, (kHandoverHeader->f_GOP.f_Height - ZKA_HAS_DISK_HEIGHT) / 2); + + fb_clear(); + } + } +#endif + + // ------------------------------------------ // + // null these fields, to avoid being reused later. + // ------------------------------------------ // + + handover_hdr->f_FirmwareCustomTables[0] = nullptr; + handover_hdr->f_FirmwareCustomTables[1] = nullptr; + + handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); + + handover_hdr->f_Magic = kHandoverMagic; + handover_hdr->f_Version = kHandoverVersion; + + // Provide fimware vendor name. + + Boot::BCopyMem(handover_hdr->f_FirmwareVendorName, sys_table->FirmwareVendor, + handover_hdr->f_FirmwareVendorLen); + + handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); + + // Assign to global 'kHandoverHeader'. + + Boot::BFileReader reader_kernel(L"minoskrnl.exe", image_handle); + + reader_kernel.ReadAll(0); + + Boot::BootThread* kernel_thread = nullptr; + + // ------------------------------------------ // + // If we succeed in reading the blob, then execute it. + // ------------------------------------------ // + + if (reader_kernel.Blob()) + { + kernel_thread = new Boot::BootThread(reader_kernel.Blob()); + kernel_thread->SetName("BootZ: MicroKernel."); + + handover_hdr->f_KernelImage = reader_kernel.Blob(); + } + else + { + fb_init(); + FBDrawBitMapInRegion(zka_no_disk, ZKA_NO_DISK_WIDTH, ZKA_NO_DISK_HEIGHT, (kHandoverHeader->f_GOP.f_Width - ZKA_NO_DISK_WIDTH) / 2, (kHandoverHeader->f_GOP.f_Height - ZKA_NO_DISK_HEIGHT) / 2); + + EFI::Stop(); + } + + Boot::BFileReader ttf_font(L"TQ\\OSFont.ttf", image_handle); + + ttf_font.ReadAll(0); + + if (ttf_font.Blob()) + { + handover_hdr->f_KernelImage = reader_kernel.Blob(); + handover_hdr->f_KernelSz = reader_kernel.Size(); + handover_hdr->f_FontImage = ttf_font.Blob(); + handover_hdr->f_FontSz = ttf_font.Size(); + } + else + { + fb_init(); + FBDrawBitMapInRegion(zka_no_disk, ZKA_NO_DISK_HEIGHT, ZKA_NO_DISK_WIDTH, (kHandoverHeader->f_GOP.f_Width - ZKA_NO_DISK_WIDTH) / 2, (kHandoverHeader->f_GOP.f_Height - ZKA_NO_DISK_HEIGHT) / 2); + + EFI::Stop(); + } + + EFI::ExitBootServices(map_key, image_handle); + + // ---------------------------------------------------- // + // Finally load the OS kernel. + // ---------------------------------------------------- // + + kernel_thread->Start(handover_hdr, YES); + + CANT_REACH(); +} diff --git a/dev/Boot/src/HEL/ARM64/BootMain.cc b/dev/Boot/src/HEL/ARM64/BootMain.cc deleted file mode 100644 index 4d303c33..00000000 --- a/dev/Boot/src/HEL/ARM64/BootMain.cc +++ /dev/null @@ -1,76 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024, Theater Quality Corp, all rights reserved. - -------------------------------------------- */ - -#include -#include -#include -#include - -#ifndef kExpectedWidth -#define kExpectedWidth (1280) -#endif - -#ifndef kExpectedHeight -#define kExpectedHeight (720) -#endif - -EXTERN EfiBootServices* BS; - -STATIC EfiGraphicsOutputProtocol* kGop = nullptr; -STATIC UInt16 kGopStride = 0U; -STATIC EfiGUID kGopGuid; - -/// @brief Main EFI entrypoint. -/// @param ImageHandle Handle of this image. -/// @param SystemTable The system table of it. -/// @return nothing, never returns. -EFI_EXTERN_C EFI_API Int32 Main(EfiHandlePtr ImageHandle, - EfiSystemTable* SystemTable) -{ - InitEFI(SystemTable); - - kGopGuid = EfiGUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); - kGop = nullptr; - - BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*)&kGop); - - kGopStride = 4; - - Boot::BTextWriter writer; - - for (SizeT i = 0; i < kGop->Mode->MaxMode; ++i) - { - EfiGraphicsOutputProtocolModeInformation* infoPtr = nullptr; - UInt32 sz = 0U; - - kGop->QueryMode(kGop, i, &sz, &infoPtr); - - writer.Write(infoPtr->HorizontalResolution); - writer.Write(infoPtr->VerticalResolution); - writer.Write("\r"); - - if (infoPtr->HorizontalResolution == kExpectedWidth && - infoPtr->VerticalResolution == kExpectedHeight) - { - kGop->SetMode(kGop, i); - break; - } - } - - Boot::BFileReader reader_kernel(L"minoskrnl.exe", ImageHandle); - - reader_kernel.ReadAll(0); - - if (reader_kernel.Blob()) - { - auto kernel_thread = Boot::BootThread(reader_kernel.Blob()); - - if (kernel_thread.IsValid()) - kernel_thread.Start(nullptr, YES); - } - - CANT_REACH(); -} diff --git a/dev/Boot/src/HEL/ARM64/CoreBootStartup.S b/dev/Boot/src/HEL/ARM64/CoreBootStartup.S new file mode 100644 index 00000000..ee2dd3ec --- /dev/null +++ b/dev/Boot/src/HEL/ARM64/CoreBootStartup.S @@ -0,0 +1,36 @@ +/* ------------------------------------------- + + Copyright (C) 2024, Theater Quality Corp, all rights reserved. + +------------------------------------------- */ + +.section .boot_hdr +.align 4 + +/* BootZ boot header begin */ + +boot_hdr_mag: + .ascii "CB" +boot_hdr_name: + // it has to match ten bytes. + .asciz "zbaosldr\0\0" +boot_hdr_ver: + .word 0x104 +boot_hdr_proc: + .long bootloader_start + +/* BootZ boot header end */ + +.extern bootloader_main +.extern bootloader_stack + +.globl bootloader_start +bootloader_start: + adr x0, bootloader_stack + ldr x1, =bootloader_start + sub x0, x0, x1 + ldr x0, [x0] + mov sp, x0 + + bl bootloader_main + ret \ No newline at end of file diff --git a/dev/Boot/src/HEL/ARM64/EFIBootStartup.cc b/dev/Boot/src/HEL/ARM64/EFIBootStartup.cc new file mode 100644 index 00000000..4d303c33 --- /dev/null +++ b/dev/Boot/src/HEL/ARM64/EFIBootStartup.cc @@ -0,0 +1,76 @@ +/* ------------------------------------------- + + Copyright (C) 2024, Theater Quality Corp, all rights reserved. + +------------------------------------------- */ + +#include +#include +#include +#include + +#ifndef kExpectedWidth +#define kExpectedWidth (1280) +#endif + +#ifndef kExpectedHeight +#define kExpectedHeight (720) +#endif + +EXTERN EfiBootServices* BS; + +STATIC EfiGraphicsOutputProtocol* kGop = nullptr; +STATIC UInt16 kGopStride = 0U; +STATIC EfiGUID kGopGuid; + +/// @brief Main EFI entrypoint. +/// @param ImageHandle Handle of this image. +/// @param SystemTable The system table of it. +/// @return nothing, never returns. +EFI_EXTERN_C EFI_API Int32 Main(EfiHandlePtr ImageHandle, + EfiSystemTable* SystemTable) +{ + InitEFI(SystemTable); + + kGopGuid = EfiGUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); + kGop = nullptr; + + BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*)&kGop); + + kGopStride = 4; + + Boot::BTextWriter writer; + + for (SizeT i = 0; i < kGop->Mode->MaxMode; ++i) + { + EfiGraphicsOutputProtocolModeInformation* infoPtr = nullptr; + UInt32 sz = 0U; + + kGop->QueryMode(kGop, i, &sz, &infoPtr); + + writer.Write(infoPtr->HorizontalResolution); + writer.Write(infoPtr->VerticalResolution); + writer.Write("\r"); + + if (infoPtr->HorizontalResolution == kExpectedWidth && + infoPtr->VerticalResolution == kExpectedHeight) + { + kGop->SetMode(kGop, i); + break; + } + } + + Boot::BFileReader reader_kernel(L"minoskrnl.exe", ImageHandle); + + reader_kernel.ReadAll(0); + + if (reader_kernel.Blob()) + { + auto kernel_thread = Boot::BootThread(reader_kernel.Blob()); + + if (kernel_thread.IsValid()) + kernel_thread.Start(nullptr, YES); + } + + CANT_REACH(); +} diff --git a/dev/Kernel/FSKit/NeFS.h b/dev/Kernel/FSKit/NeFS.h index 78177e07..5431225c 100644 --- a/dev/Kernel/FSKit/NeFS.h +++ b/dev/Kernel/FSKit/NeFS.h @@ -112,6 +112,7 @@ default. #define kNeFSForkNameLen (200) #define kNeFSFrameworkExt ".fwrk" +#define kNeFSStepsExt ".step" #define kNeFSApplicationExt ".app" #define kNeFSJournalExt ".jrnl" diff --git a/dev/Kernel/HALKit/AMD64/Storage/AHCI-DMA.cc b/dev/Kernel/HALKit/AMD64/Storage/AHCI-DMA.cc deleted file mode 100644 index 2f8ea774..00000000 --- a/dev/Kernel/HALKit/AMD64/Storage/AHCI-DMA.cc +++ /dev/null @@ -1,319 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024, Theater Quality Corp, all rights reserved. - -------------------------------------------- */ - -/** - * @file AHCI.cc - * @author Amlal EL Mahrouss (amlalelmahrouss@icloud.com) - * @brief AHCI driver. - * @version 0.1 - * @date 2024-02-02 - * - * @Copyright (C) 2024, Theater Quality Corp, all rights reserved. - * - */ - -#include -#include - -#include -#include -#include -#include -#include - -#ifdef __AHCI__ - -#define HBA_ERR_TFE (1 << 30) -#define HBA_PxCMD_ST 0x0001 -#define HBA_PxCMD_FRE 0x0010 -#define HBA_PxCMD_FR 0x4000 -#define HBA_PxCMD_CR 0x8000 - -#define kAhciStartAddress mib_cast(4) - -#define kAhciLBAMode (1 << 6) - -#define kAhciMaxPoll (100000U) - -#define kCmdOrCtrlCmd 1 -#define kCmdOrCtrlCtrl 0 - -#define kAhciSRBsy 0x80 -#define kAhciSRDrq 0x08 - -enum -{ - kSATAProgIfAHCI = 0x01, - kSATASubClass = 0x06, - kSATABar5 = 0x24, -}; - -STATIC Kernel::PCI::Device kAhciDevice; -STATIC HbaPort* kAhciPort = nullptr; -STATIC Kernel::Lba kCurrentDiskSectorCount = 0UL; - -Kernel::Void drv_calculate_disk_geometry() -{ - kCurrentDiskSectorCount = 0UL; - kcout << "Highest AHCI LBA (48-bit): " << Kernel::number(kCurrentDiskSectorCount) << endl; -} - -/// @brief Initializes an AHCI disk. -/// @param PortsImplemented the amount of kAhciPort that have been detected. -/// @return if the disk was successfully initialized or not. -Kernel::Boolean drv_std_init(Kernel::UInt16& PortsImplemented) -{ - using namespace Kernel; - - PCI::Iterator iterator(Types::PciDeviceKind::MassStorageController); - - for (SizeT device_index = 0; device_index < ZKA_BUS_COUNT; ++device_index) - { - kAhciDevice = iterator[device_index].Leak(); // And then leak the reference. - - // if SATA and then interface is AHCI... - if (kAhciDevice.Subclass() == kSATASubClass && - kAhciDevice.ProgIf() == kSATAProgIfAHCI) - { - kAhciDevice.EnableMmio(0x24); // Enable the memory index_byte/o for this ahci device. - kAhciDevice.BecomeBusMaster(0x24); // Become bus master for this ahci device, so that we can control it. - - HbaMem* mem_ahci = (HbaMem*)kAhciDevice.Bar(0x24); - - Kernel::UInt32 ports_implemented = mem_ahci->Pi; - Kernel::UInt16 ahci_index = 0; - - const Kernel::UInt16 kMaxPortsImplemented = 32; - const Kernel::UInt32 kSATASignature = 0x00000101; - const Kernel::UInt8 kAhciPresent = 0x03; - const Kernel::UInt8 kAhciIPMActive = 0x01; - - Kernel::Boolean detected = false; - - while (ahci_index < kMaxPortsImplemented) - { - if (ports_implemented) - { - kcout << "Port is implemented by host.\r"; - - Kernel::UInt8 ipm = (mem_ahci->Ports[ahci_index].Ssts >> 8) & 0x0F; - Kernel::UInt8 det = mem_ahci->Ports[ahci_index].Ssts & 0x0F; - - if (mem_ahci->Ports[ahci_index].Sig == kSATASignature) - { - kcout << "Port is AHCI controller.\r"; - - detected = true; - - kAhciPort = &mem_ahci->Ports[ahci_index]; - - kAhciPort->Cmd &= ~HBA_PxCMD_FRE; - - // Clear FRE (bit4) - kAhciPort->Cmd &= ~HBA_PxCMD_ST; - - // Wait until FR (bit14), CR (bit15) are cleared - while (YES) - { - if (kAhciPort->Cmd & HBA_PxCMD_CR) - continue; - - if (kAhciPort->Cmd & HBA_PxCMD_FR) - continue; - break; - } - - // when it's stopped. - - // do in-between - - kAhciPort->Clb = kAhciStartAddress + (ahci_index << 10); - kAhciPort->Clbu = 0; - rt_set_memory((Kernel::VoidPtr)((Kernel::UIntPtr)kAhciPort->Clb + kAhciPort->Clbu), 0, 1024); - - // FIS offset: 32K+256*ahci_index - // FIS entry size = 256 bytes per port - kAhciPort->Fb = kAhciStartAddress + (32 << 10) + (ahci_index << 8); - kAhciPort->Fbu = 0; - rt_set_memory((Kernel::VoidPtr)((Kernel::UIntPtr)kAhciPort->Fb + kAhciPort->Fbu), 0, 256); - - // Command table offset: 40K + 8K*ahci_index - // Command table size = 256*32 = 8K per port - HbaCmdHeader* cmd_header = (HbaCmdHeader*)((Kernel::UIntPtr)kAhciPort->Clb + kAhciPort->Clbu); - - for (SizeT i = 0; i < 32; i++) - { - cmd_header[i].Prdtl = 8; // 8 prdt entries per command table - // 256 bytes per command table, 64+16+48+16*8 - // Command table offset: 40K + 8K*ahci_index + cmdheader_index*256 - cmd_header[i].Ctba = kAhciStartAddress + (40 << 10) + (ahci_index << 13) + (i << 8); - cmd_header[i].Ctbau = 0; - - rt_set_memory((VoidPtr)((Kernel::UIntPtr)cmd_header[i].Ctba + cmd_header[i].Ctbau), 0, 256); - } - - // when it's starting - - // check for bits again, to start it again. - while (YES) - { - if (kAhciPort->Cmd & HBA_PxCMD_FR) - continue; - - break; - } - - kAhciPort->Cmd |= HBA_PxCMD_FRE; - kAhciPort->Cmd |= HBA_PxCMD_ST; - - drv_calculate_disk_geometry(); - - break; - } - } - - ports_implemented >>= 1; - ++ahci_index; - } - - return detected; - } - } - - return No; -} - -Kernel::Boolean drv_std_detected(Kernel::Void) -{ - return kAhciDevice.DeviceId() != 0xFFFF; -} - -Kernel::Void drv_std_write(Kernel::UInt64 lba, Kernel::Char* buffer, Kernel::SizeT sector_cnt, Kernel::SizeT size_buffer) -{ -} - -Kernel::Void drv_std_read(Kernel::UInt64 lba, Kernel::Char* buffer, Kernel::SizeT sector_cnt, Kernel::SizeT size_buffer) -{ - kAhciPort->Is = -1; - - Kernel::SizeT port = 0; - Kernel::UInt32 slots = (kAhciPort->Sact | kAhciPort->Ci); - - for (; port < slots; ++port) - { - if ((slots & 1) == 0) - break; - - slots >>= 1; - } - - HbaCmdHeader* cmd_hdr = (HbaCmdHeader*)((Kernel::UIntPtr)kAhciPort->Clb + kAhciPort->Clbu); - - cmd_hdr += port; - cmd_hdr->Cfl = sizeof(FisRegH2D) / sizeof(Kernel::UInt32); - cmd_hdr->Write = NO; - cmd_hdr->Prdtl = (Kernel::UInt16)((sector_cnt - 1) >> 4) + 1; - - HbaCmdTbl* cmd_tbl = (HbaCmdTbl*)((Kernel::UIntPtr)cmd_hdr->Ctba + cmd_hdr->Ctbau); - Kernel::rt_set_memory(cmd_tbl, 0, (cmd_hdr->Prdtl - 1) * sizeof(HbaPrdtEntry)); - - Kernel::SizeT i = 0; - - for (Kernel::SizeT i = 0; i < cmd_hdr->Prdtl - 1; i++) - { - cmd_tbl->PrdtEntries[i].Dba = (Kernel::UInt32)(Kernel::UInt64)buffer; - cmd_tbl->PrdtEntries[i].Dbau = (Kernel::UInt32)((Kernel::UInt64)(buffer) >> 32); - cmd_tbl->PrdtEntries[i].Dbc = size_buffer - 1; // 8K bytes (this value should always be set to 1 less than the actual value) - cmd_tbl->PrdtEntries[i].InterruptBit = 1; - } - - cmd_tbl->PrdtEntries[i].Dba = (Kernel::UInt32)(Kernel::UInt64)buffer; - cmd_tbl->PrdtEntries[i].Dbau = (Kernel::UInt32)((Kernel::UInt64)(buffer) >> 32); - cmd_tbl->PrdtEntries[i].Dbc = size_buffer - 1; // 8K bytes (this value should always be set to 1 less than the actual value) - cmd_tbl->PrdtEntries[i].InterruptBit = 1; - - FisRegH2D* cmd_fis = (FisRegH2D*)(&cmd_tbl->Cfis); - - cmd_fis->FisType = kFISTypeRegH2D; - cmd_fis->CmdOrCtrl = YES; // Command - cmd_fis->Command = kAHCICmdReadDmaEx; - - cmd_fis->Lba0 = (Kernel::UInt8)(Kernel::UInt32)lba & 0xFF; - cmd_fis->Lba1 = (Kernel::UInt8)((Kernel::UInt32)lba >> 8); - cmd_fis->Lba2 = (Kernel::UInt8)((Kernel::UInt32)lba >> 16); - cmd_fis->Device = kAhciLBAMode; // LBA mode - - cmd_fis->Lba3 = (Kernel::UInt8)((Kernel::UInt32)lba >> 24); - cmd_fis->Lba4 = (Kernel::UInt8)(lba >> 32); - cmd_fis->Lba5 = (Kernel::UInt8)((lba >> 32) >> 8); - - cmd_fis->CountLow = sector_cnt & 0xFF; - cmd_fis->CountHigh = (sector_cnt >> 8) & 0xFF; - - Kernel::UInt64 spin = 0UL; - - // The below loop waits until the port is no longer busy before issuing a new command - while ((kAhciPort->Tfd & (kAhciSRBsy | kAhciSRDrq)) && spin < kAhciMaxPoll) - { - spin++; - } - if (spin == 1000000) - { - kcout << "AHCI: Port is hung.\r"; - return; - } - - kAhciPort->Ci = 1 << port; // Issue command - - // Wait for completion - while (YES) - { - // In some longer duration reads, it may be helpful to spin on the DPS bit - // in the PxIS port field as well (1 << 5) - if ((kAhciPort->Ci & (1 << port)) == 0) - break; - if (kAhciPort->Is & HBA_ERR_TFE) // Task file error - { - using namespace Kernel; - kcout << ("AHCI: Read disk error.\r"); - - err_global_get() = kErrorUnrecoverableDisk; - - return; - } - } - - // Check again for the last time. - if (kAhciPort->Is & HBA_ERR_TFE) // task file error status - { - using namespace Kernel; - - kcout << ("AHCI: Read disk error.\r"); - *buffer = 0; - - err_global_get() = kErrorUnrecoverableDisk; - - return; - } -} - -/*** - @brief Gets the number of sectors inside the drive. - @return Sector size in bytes. - */ -Kernel::SizeT drv_get_sector_count() -{ - return kCurrentDiskSectorCount; -} - -/// @brief Get the drive size. -/// @return Disk size in bytes. -Kernel::SizeT drv_get_size() -{ - return drv_get_sector_count() * kAHCISectorSize; -} - -#endif // ifdef __AHCI__ diff --git a/dev/Kernel/HALKit/AMD64/Storage/ATA-PIO.cc b/dev/Kernel/HALKit/AMD64/Storage/ATA-PIO.cc index 1d02ca7b..b9da351a 100644 --- a/dev/Kernel/HALKit/AMD64/Storage/ATA-PIO.cc +++ b/dev/Kernel/HALKit/AMD64/Storage/ATA-PIO.cc @@ -189,7 +189,7 @@ Kernel::SizeT drv_get_sector_count() /// @brief Get the drive size. Kernel::SizeT drv_get_size() { - return drv_get_sector_count() * kATASectorSize; + return (drv_get_sector_count()) * kATASectorSize; } #endif /* ifdef __ATA_PIO__ */ diff --git a/dev/Kernel/HALKit/AMD64/Storage/SATA-DMA.cc b/dev/Kernel/HALKit/AMD64/Storage/SATA-DMA.cc new file mode 100644 index 00000000..a3c1474d --- /dev/null +++ b/dev/Kernel/HALKit/AMD64/Storage/SATA-DMA.cc @@ -0,0 +1,276 @@ +/* ------------------------------------------- + + Copyright (C) 2024, Theater Quality Corp, all rights reserved. + +------------------------------------------- */ + +/** + * @file AHCI.cc + * @author Amlal EL Mahrouss (amlalelmahrouss@icloud.com) + * @brief AHCI driver. + * @version 0.1 + * @date 2024-02-02 + * + * @Copyright (C) 2024, Theater Quality Corp, all rights reserved. + * + */ + +#include +#include + +#include +#include +#include +#include +#include + +#ifdef __AHCI__ + +#define HBA_ERR_TFE (1 << 30) +#define HBA_PxCMD_ST 0x0001 +#define HBA_PxCMD_FRE 0x0010 +#define HBA_PxCMD_FR 0x4000 +#define HBA_PxCMD_CR 0x8000 + +#define kAhciStartAddress mib_cast(4) + +#define kAhciLBAMode (1 << 6) + +#define kAhciMaxPoll (100000U) + +#define kCmdOrCtrlCmd 1 +#define kCmdOrCtrlCtrl 0 + +#define kAhciSRBsy 0x80 +#define kAhciSRDrq 0x08 + +enum +{ + kSATAProgIfAHCI = 0x01, + kSATASubClass = 0x06, + kSATABar5 = 0x24, +}; + +STATIC Kernel::PCI::Device kAhciDevice; +STATIC HbaPort* kAhciPort = nullptr; +STATIC Kernel::Lba kCurrentDiskSectorCount = 0UL; + +template +static Kernel::Void drv_std_input_output(Kernel::UInt64 lba, Kernel::UInt8* buffer, Kernel::SizeT sector_cnt, Kernel::SizeT size_buffer); + +static Kernel::Void drv_calculate_disk_geometry() noexcept; + +static Kernel::Void drv_calculate_disk_geometry() noexcept +{ + kCurrentDiskSectorCount = 0UL; + + Kernel::UInt8 identify_data[kib_cast(4)] = {}; + + drv_std_input_output(0, identify_data, 0, kib_cast(8)); + + kCurrentDiskSectorCount = (identify_data[61] << 16) | identify_data[60]; + + kcout << "Disk Size: " << Kernel::number(drv_get_size()) << endl; + kcout << "Highest AHCI LBA: " << Kernel::number(kCurrentDiskSectorCount) << endl; +} + +/// @brief Initializes an AHCI disk. +/// @param PortsImplemented the amount of kAhciPort that have been detected. +/// @return if the disk was successfully initialized or not. +Kernel::Boolean drv_std_init(Kernel::UInt16& PortsImplemented) +{ + using namespace Kernel; + + PCI::Iterator iterator(Types::PciDeviceKind::MassStorageController); + + for (SizeT device_index = 0; device_index < ZKA_BUS_COUNT; ++device_index) + { + kAhciDevice = iterator[device_index].Leak(); // And then leak the reference. + + // if SATA and then interface is AHCI... + if (kAhciDevice.Subclass() == kSATASubClass && + kAhciDevice.ProgIf() == kSATAProgIfAHCI) + { + kAhciDevice.EnableMmio(0x24); // Enable the memory index_byte/o for this ahci device. + kAhciDevice.BecomeBusMaster(0x24); // Become bus master for this ahci device, so that we can control it. + + HbaMem* mem_ahci = (HbaMem*)kAhciDevice.Bar(0x24); + + Kernel::UInt32 ports_implemented = mem_ahci->Pi; + Kernel::UInt16 ahci_index = 0; + + const Kernel::UInt16 kMaxPortsImplemented = 32; + const Kernel::UInt32 kSATASignature = 0x00000101; + const Kernel::UInt8 kAhciPresent = 0x03; + const Kernel::UInt8 kAhciIPMActive = 0x01; + + Kernel::Boolean detected = false; + + while (ahci_index < kMaxPortsImplemented) + { + if (ports_implemented) + { + kcout << "Port is implemented by host.\r"; + + Kernel::UInt8 ipm = (mem_ahci->Ports[ahci_index].Ssts >> 8) & 0x0F; + Kernel::UInt8 det = mem_ahci->Ports[ahci_index].Ssts & 0x0F; + + if (mem_ahci->Ports[ahci_index].Sig == kSATASignature) + { + kcout << "Port is AHCI controller.\r"; + + detected = true; + + kAhciPort = &mem_ahci->Ports[ahci_index]; + + kAhciPort->Cmd &= ~HBA_PxCMD_FRE; + + // Clear FRE (bit4) + kAhciPort->Cmd &= ~HBA_PxCMD_ST; + + // Wait until FR (bit14), CR (bit15) are cleared + while (YES) + { + if (kAhciPort->Cmd & HBA_PxCMD_CR) + continue; + + if (kAhciPort->Cmd & HBA_PxCMD_FR) + continue; + break; + } + + kAhciPort->Cmd |= HBA_PxCMD_FRE; + kAhciPort->Cmd |= HBA_PxCMD_ST; + + drv_calculate_disk_geometry(); + + break; + } + } + + ports_implemented >>= 1; + ++ahci_index; + } + + return detected; + } + } + + return No; +} + +Kernel::Boolean drv_std_detected(Kernel::Void) +{ + return kAhciDevice.DeviceId() != 0xFFFF; +} + +Kernel::Void drv_std_write(Kernel::UInt64 lba, Kernel::Char* buffer, Kernel::SizeT sector_cnt, Kernel::SizeT size_buffer) +{ + drv_std_input_output(lba, (Kernel::UInt8*)buffer, sector_cnt, size_buffer); +} + +Kernel::Void drv_std_read(Kernel::UInt64 lba, Kernel::Char* buffer, Kernel::SizeT sector_cnt, Kernel::SizeT size_buffer) +{ + drv_std_input_output(lba, (Kernel::UInt8*)buffer, sector_cnt, size_buffer); +} + +template +static Kernel::Void drv_std_input_output(Kernel::UInt64 lba, Kernel::UInt8* buffer, Kernel::SizeT sector_cnt, Kernel::SizeT size_buffer) +{ + Kernel::SizeT port = 0; + Kernel::UInt32 slots = (kAhciPort->Sact | kAhciPort->Ci); + + for (; port < slots; ++port) + { + if ((slots & 1) == 0) + break; + + slots >>= 1; + } + + if (slots == 0) + return; + + Kernel::UInt32* command_list = new Kernel::UInt32[kib_cast(1)]; + Kernel::UInt32* command_table = new Kernel::UInt32[kib_cast(4)]; + + Kernel::rt_set_memory(command_list, 0, kib_cast(1)); + Kernel::rt_set_memory(command_table, 0, kib_cast(4)); + + auto command_table_deref = *(Kernel::UInt32*)&command_table; + + command_table[0] = *(Kernel::UInt32*)&buffer; + + command_list[0] = command_table_deref; + command_list[1] = size_buffer; + + volatile Kernel::UInt32* command_header = (Kernel::UInt32*)kAhciPort + 0x10; + + auto command_list_deref = *(Kernel::UInt32*)&command_list; + + command_header[0] = 5 | 0 | (Write ? kAHCICmdWriteDma : kAHCICmdReadDma) | 0 | 00000000 | 1 | 00000000000; + command_header[0] = command_header[0] | (lba & __UINT32_MAX__); + + command_header[1] = command_list_deref; + + Kernel::UInt32* fis = command_list; + + fis[0] = kFISTypeRegH2D; + fis[1] = CommandOrCTRL; + + fis[2] = Identify ? kAHCICmdIdentify : kAHCICmdReadDmaEx; + + if (Write) + fis[2] = kAHCICmdWriteDmaEx; + + // 5. Issue the command + // Write command issue bit + kAhciPort->Ci |= (1 << 0); // Command Issue + + while (kAhciPort->Ci & (1 << 0)) + { + if (kAhciPort->Is & HBA_ERR_TFE) // Task file error + { + kcout << "AHCI: Read disk error.\r"; + + err_global_get() = Kernel::kErrorUnrecoverableDisk; + + return; + } + } + + // Check again for the last time. + if (kAhciPort->Is & HBA_ERR_TFE) // task file error status + { + using namespace Kernel; + + kcout << "AHCI: Read disk error.\r"; + *buffer = 0; + + err_global_get() = kErrorUnrecoverableDisk; + + return; + } + + delete[] command_table; + + command_table = nullptr; +} + +/*** + @brief Gets the number of sectors inside the drive. + @return Sector size in bytes. + */ +Kernel::SizeT drv_get_sector_count() +{ + return kCurrentDiskSectorCount; +} + +/// @brief Get the drive size. +/// @return Disk size in bytes. +Kernel::SizeT drv_get_size() +{ + return (drv_get_sector_count()) * kAHCISectorSize; +} + +#endif // ifdef __AHCI__ diff --git a/dev/Kernel/src/DriveMgr.cc b/dev/Kernel/src/DriveMgr.cc index b588bb0a..4c279d0f 100644 --- a/dev/Kernel/src/DriveMgr.cc +++ b/dev/Kernel/src/DriveMgr.cc @@ -176,7 +176,7 @@ namespace Kernel trait->fPacket.fPacketReadOnly = NO; trait->fKind = kMassStorageDisc | kEPMDrive; - kcout << "Formatted Disc is EPM (and Mass Storage too.)\r"; + kcout << "Formatted Disk is EPM (and Mass Storage too.)\r"; } else { @@ -186,7 +186,7 @@ namespace Kernel kcout << "Scheme Found: " << block_struct.Name << endl; if (block_struct.Name[0] == 0) - kcout << "Disc partition is unknown (Was set to Read Only.)\r"; + kcout << "Disk partition is unknown (Was set to Read Only.)\r"; } rt_copy_memory((VoidPtr) "*/*", trait->fPacket.fPacketMime, diff --git a/dev/Kernel/src/HardwareThreadScheduler.cc b/dev/Kernel/src/HardwareThreadScheduler.cc index 91800ab7..a94ad64c 100644 --- a/dev/Kernel/src/HardwareThreadScheduler.cc +++ b/dev/Kernel/src/HardwareThreadScheduler.cc @@ -4,7 +4,6 @@ ------------------------------------------- */ -#include "NewKit/Macros.h" #include #include #include diff --git a/dev/Kernel/src/UserProcessScheduler.cc b/dev/Kernel/src/UserProcessScheduler.cc index 91001b05..85915e10 100644 --- a/dev/Kernel/src/UserProcessScheduler.cc +++ b/dev/Kernel/src/UserProcessScheduler.cc @@ -12,7 +12,6 @@ /// @brief Low level/Ring-3 process scheduler. /***********************************************************************************/ -#include "NewKit/Macros.h" #include #include #include diff --git a/dev/Mod/AHCI/AHCI.h b/dev/Mod/AHCI/AHCI.h index 9b86dc81..f7211267 100644 --- a/dev/Mod/AHCI/AHCI.h +++ b/dev/Mod/AHCI/AHCI.h @@ -18,7 +18,7 @@ /// @note Forward declarations of structs. -#define kAHCISectorSize (512U) +#define kAHCISectorSize (512) struct HbaPort; struct FisData; -- cgit v1.2.3