From 1cae0d2cde6e775ecb1625aa93a5d65d996efad0 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 25 Apr 2024 09:38:24 +0200 Subject: MHR-16: Load NewLoader correctly. Signed-off-by: Amlal El Mahrouss --- Private/HALKit/AMD64/HalProcessor.cpp | 2 +- Private/HALKit/AMD64/Storage/AHCI.cxx | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'Private/HALKit') diff --git a/Private/HALKit/AMD64/HalProcessor.cpp b/Private/HALKit/AMD64/HalProcessor.cpp index 4fea4e7e..61d98c8c 100644 --- a/Private/HALKit/AMD64/HalProcessor.cpp +++ b/Private/HALKit/AMD64/HalProcessor.cpp @@ -8,7 +8,7 @@ /** * @file Processor.cpp - * @brief This file is about processor specific functions (in/out...) + * @brief This file is about processor specific functions (in/out/cli/std...) */ namespace NewOS::HAL { diff --git a/Private/HALKit/AMD64/Storage/AHCI.cxx b/Private/HALKit/AMD64/Storage/AHCI.cxx index 5e6b3348..c9db540a 100644 --- a/Private/HALKit/AMD64/Storage/AHCI.cxx +++ b/Private/HALKit/AMD64/Storage/AHCI.cxx @@ -33,11 +33,11 @@ NewOS::Boolean drv_std_init(NewOS::UInt16& PortsImplemented) { for (SizeT devIndex = 0; devIndex < NEWOS_BUS_COUNT; ++devIndex) { if (iterator[devIndex].Leak().Subclass() == kSATASubClass && iterator[devIndex].Leak().ProgIf() == kSATAProgIfAHCI) { - iterator[devIndex].Leak().EnableMmio(); - kAhciDevice = iterator[devIndex].Leak(); + iterator[devIndex].Leak().EnableMmio(); /// enable the memory i/o for this ahci device. + kAhciDevice = iterator[devIndex].Leak(); /// and then leak the reference. kcout << "NewKernel: [PCI] Found AHCI controller.\r\n"; - + return true; } } @@ -54,4 +54,4 @@ NewOS::Void drv_std_read(NewOS::UInt64 Lba, NewOS::Char* Buf, NewOS::Void drv_std_write(NewOS::UInt64 Lba, NewOS::Char* Buf, NewOS::SizeT SectorSz, NewOS::SizeT Size) {} -#endif // __AHCI__ \ No newline at end of file +#endif // __AHCI__ -- cgit v1.2.3 From 3b4a4a290b32fdb4a749b3e581afae450eb543c8 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 26 Apr 2024 08:13:03 +0200 Subject: MHR-16: NewBoot - BDiskFormatter and QR. Signed-off-by: Amlal El Mahrouss --- Private/FSKit/NewFS.hxx | 9 +- Private/HALKit/AMD64/HalNewBoot.asm | 14 +- Private/HALKit/AMD64/Storage/ATA-PIO.cxx | 2 +- Private/HALKit/POWER/HalStartSequence.s | 8 +- Private/Linker/16x0.json | 2 +- Private/Linker/32x0.json | 2 +- Private/Linker/64x0.json | 2 +- Private/NewBoot/BootKit/BootKit.hxx | 149 ++++++++++++++++++--- Private/NewBoot/BootKit/HW/ATA.hxx | 2 + Private/NewBoot/Source/HEL/AMD64/BootATA.cxx | 26 +--- Private/NewBoot/Source/HEL/AMD64/BootJump.S | 9 ++ Private/NewBoot/Source/HEL/AMD64/BootMain.cxx | 94 ++++++++----- .../NewBoot/Source/HEL/AMD64/BootTextWriter.cxx | 2 +- Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx | 11 ++ Private/NewBoot/Source/makefile | 13 +- Private/NewLoader/ReadMe.md | 9 -- Private/NewLoader/Source/.gitkeep | 0 Private/makefile | 10 +- 18 files changed, 253 insertions(+), 111 deletions(-) create mode 100644 Private/NewBoot/Source/HEL/AMD64/BootJump.S delete mode 100644 Private/NewLoader/ReadMe.md delete mode 100644 Private/NewLoader/Source/.gitkeep (limited to 'Private/HALKit') diff --git a/Private/FSKit/NewFS.hxx b/Private/FSKit/NewFS.hxx index cc43f51b..d3b72803 100644 --- a/Private/FSKit/NewFS.hxx +++ b/Private/FSKit/NewFS.hxx @@ -38,7 +38,7 @@ default. #define kNewFSIdentLen 8 #define kNewFSIdent " NewFS" -#define kNewFSPadLen 16 +#define kNewFSPadLen 408 /// @brief Partition GUID on EPM and GPT disks. #define kNewFSUUID "@{DD997393-9CCE-4288-A8D5-C0FDE3908DBE}" @@ -83,6 +83,7 @@ default. /// Start After the PM headers, pad 1024 bytes. #define kNewFSAddressAsLba (1024) +#define kNewFSCatalogStartAddress (kNewFSAddressAsLba + sizeof(NewPartitionBlock)) #define kResourceTypeDialog 10 #define kResourceTypeString 11 @@ -139,6 +140,9 @@ struct PACKED NewFork final { NewOS::Lba NextSibling; NewOS::Lba PreviousSibling; + + /// To make a perfect sector. + NewOS::Char Padding[200]; }; /// @brief Partition block type @@ -174,6 +178,9 @@ enum { kNewFSSubDriveCount, }; +/// \brief Resource fork kind. +enum { kNewFSRsrcForkKind = 0, kNewFSDataForkKind = 1 }; + /// /// \name NewFSParser /// \brief NewFS parser class. (catalog creation, remove removal, root, diff --git a/Private/HALKit/AMD64/HalNewBoot.asm b/Private/HALKit/AMD64/HalNewBoot.asm index 995a848c..11b11c0c 100644 --- a/Private/HALKit/AMD64/HalNewBoot.asm +++ b/Private/HALKit/AMD64/HalNewBoot.asm @@ -23,22 +23,16 @@ HandoverMagic: dq kHandoverMagic HandoverType: dw kTypeKernel HandoverArch: dw kArchAmd64 ;; This NewBootStart points to Main. -HandoverStart: dq Main +HandoverStart: dq __ImageStart section .text -global Main +global __ImageStart extern hal_init_platform ;; Just a simple setup, we'd also need to tell some before -Main: - push rax +__ImageStart: push rcx call hal_init_platform pop rcx - pop rax -;; Go to sleep. -MainLoop: - cli - hlt - jmp $ + ret diff --git a/Private/HALKit/AMD64/Storage/ATA-PIO.cxx b/Private/HALKit/AMD64/Storage/ATA-PIO.cxx index 3cbba6f7..652aa6f4 100644 --- a/Private/HALKit/AMD64/Storage/ATA-PIO.cxx +++ b/Private/HALKit/AMD64/Storage/ATA-PIO.cxx @@ -58,7 +58,7 @@ Void drv_std_select(UInt16 Bus) { Boolean drv_std_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster) { - if (drv_std_detected()) return false; + if (drv_std_detected()) return true; UInt16 IO = Bus; diff --git a/Private/HALKit/POWER/HalStartSequence.s b/Private/HALKit/POWER/HalStartSequence.s index f50a4d4f..fd018304 100644 --- a/Private/HALKit/POWER/HalStartSequence.s +++ b/Private/HALKit/POWER/HalStartSequence.s @@ -4,11 +4,11 @@ ------------------------------------------- */ -.globl Main -.extern HalKernelMain +.globl __ImageStart +.extern hal_init_platform .align 4 .text -Main: - bl HalKernelMain +__ImageStart: + bl hal_init_platform blr diff --git a/Private/Linker/16x0.json b/Private/Linker/16x0.json index 1dd7abbd..5851a2d3 100644 --- a/Private/Linker/16x0.json +++ b/Private/Linker/16x0.json @@ -1,6 +1,6 @@ { "executable_type": "kernel", "output_name": "NewKernel.exe", - "start_proc": "Main", + "start_proc": "__ImageStart", "format": "PEF" } diff --git a/Private/Linker/32x0.json b/Private/Linker/32x0.json index 1dd7abbd..5851a2d3 100644 --- a/Private/Linker/32x0.json +++ b/Private/Linker/32x0.json @@ -1,6 +1,6 @@ { "executable_type": "kernel", "output_name": "NewKernel.exe", - "start_proc": "Main", + "start_proc": "__ImageStart", "format": "PEF" } diff --git a/Private/Linker/64x0.json b/Private/Linker/64x0.json index 1dd7abbd..5851a2d3 100644 --- a/Private/Linker/64x0.json +++ b/Private/Linker/64x0.json @@ -1,6 +1,6 @@ { "executable_type": "kernel", "output_name": "NewKernel.exe", - "start_proc": "Main", + "start_proc": "__ImageStart", "format": "PEF" } diff --git a/Private/NewBoot/BootKit/BootKit.hxx b/Private/NewBoot/BootKit/BootKit.hxx index 15700f54..6272e4c9 100644 --- a/Private/NewBoot/BootKit/BootKit.hxx +++ b/Private/NewBoot/BootKit/BootKit.hxx @@ -6,7 +6,7 @@ /***********************************************************************************/ /// @file Boot.hxx -/// @brief Bootloader API. +/// @brief Bootloader Programming Interface. /***********************************************************************************/ #pragma once @@ -25,7 +25,7 @@ /***********************************************************************************/ #include -#include "Builtins/ATA/ATA.hxx" +#include /***********************************************************************************/ /// Framebuffer helpers. @@ -49,6 +49,11 @@ typedef Char *PEImagePtr; typedef WideChar CharacterTypeUTF16; typedef Char CharacterTypeUTF8; +namespace EFI { + extern void ThrowError(const CharacterTypeUTF16 *ErrorCode, + const CharacterTypeUTF16 *Reason) noexcept; +} + /** * @brief BootKit Text Writer class * Writes to UEFI StdOut. @@ -58,7 +63,7 @@ class BTextWriter final { public: BTextWriter &Write(const Long &num); - BTextWriter &Write(const UChar *str); + BTextWriter &Write(const Char *str); BTextWriter &Write(const CharacterTypeUTF16 *str); BTextWriter &WriteCharacter(CharacterTypeUTF16 c); @@ -171,14 +176,19 @@ static inline const UInt32 kRgbBlue = 0x00FF0000; static inline const UInt32 kRgbBlack = 0x00000000; static inline const UInt32 kRgbWhite = 0x00FFFFFF; +#define kBKBootFileMime "boot-x/file" +#define kBKBootDirMime "boot-x/dir" + /// @brief BootKit Disk Formatter. template class BDiskFormatFactory final { public: /// @brief File entry for **BDiskFormatFactory**. struct BFileDescriptor final { - Char fFilename[255]; - Char fForkName[255]; + Char fFileName[kNewFSNodeNameLen]; + Char fForkName[kNewFSNodeNameLen]; + + UInt32 fKind; VoidPtr fBlob; SizeT fBlobSz; @@ -190,6 +200,7 @@ public: public: explicit BDiskFormatFactory() = default; explicit BDiskFormatFactory(BootDev dev) : fDiskDev(dev) {} + ~BDiskFormatFactory() = default; NEWOS_COPY_DELETE(BDiskFormatFactory); @@ -204,7 +215,101 @@ public: private: /// @brief Write all of the requested catalogs into the filesystem. - Boolean _AppendCatalogList(BFileDescriptor* fileBlobs, SizeT blobCount) { + Boolean WriteContent(BFileDescriptor* fileBlobs, SizeT blobCount, + SizeT sectorSz, NewPartitionBlock& partBlock) { + if (sectorSz != BootDev::kSectorSize) return false; + + BFileDescriptor* blob = fileBlobs; + Lba startLba = partBlock.FreeCatalog; + + BTextWriter writer; + while (blob) { + NewCatalog catalogKind{ 0 }; + + /// Fill catalog kind. + catalogKind.Kind = blob->fKind; + + /// Allocate fork for blob. + catalogKind.FirstFork = (startLba + sizeof(NewCatalog)); + catalogKind.LastFork = catalogKind.FirstFork; + + NewFork forkKind{ 0 }; + + memcpy(forkKind.Name, blob->fForkName, strlen(blob->fForkName)); + forkKind.Kind = (forkKind.Name[0] == kNewFSDataFork[0]) ? kNewFSDataForkKind : kNewFSRsrcForkKind; + forkKind.Flags = kNewFSFlagCreated; + + /// We don't know. + forkKind.ResourceFlags = 0; + forkKind.ResourceId = 0; + forkKind.ResourceKind = 0; + + /// We're the only fork here. + forkKind.NextSibling = catalogKind.FirstFork; + forkKind.PreviousSibling = catalogKind.FirstFork; + + forkKind.DataOffset = (startLba + sizeof(NewCatalog) + sizeof(NewFork)); + forkKind.DataSize = blob->fBlobSz; + + Lba lbaStart = forkKind.DataOffset; + SizeT cur = 0UL; + + while (cur < forkKind.DataSize) { + this->fDiskDev.Leak().mSize = BootDev::kSectorSize; + this->fDiskDev.Leak().mBase = forkKind.DataOffset / BootDev::kSectorSize; + + this->fDiskDev.Write((Char*)(blob->fBlob) + cur, BootDev::kSectorSize); + + cur += BootDev::kSectorSize; + lbaStart += BootDev::kSectorSize; + } + + writer.Write((catalogKind.Kind == kNewFSCatalogKindFile) ? L"New Boot: Write-File: " : + L"New Boot: Write-Directory: " ).Write(blob->fFileName).Write(L"\r\n"); + + /// Set disk cursor here. + + fDiskDev.Leak().mBase = startLba + sizeof(NewCatalog) / sectorSz; + fDiskDev.Leak().mSize = sizeof(NewFork); + + fDiskDev.Write((Char*)&forkKind, 1); + + /// Fork is done. + + catalogKind.Kind = blob->fKind; + catalogKind.Flags |= kNewFSFlagCreated; + + Lba catalogLba = (sizeof(NewCatalog) - startLba); + + //// Now write catalog as well.. + + catalogKind.PrevSibling = startLba; + catalogKind.NextSibling = (sizeof(NewCatalog) + blob->fBlobSz); + + /// this mime only applies to file. + if (catalogKind.Kind == kNewFSCatalogKindFile) { + memcpy(catalogKind.Mime, kBKBootFileMime, strlen(kBKBootFileMime)); + } else { + memcpy(catalogKind.Mime, kBKBootDirMime, strlen(kBKBootDirMime)); + } + + memcpy(catalogKind.Name, blob->fFileName, strlen(blob->fFileName)); + + fDiskDev.Leak().mBase = startLba / sectorSz; + fDiskDev.Leak().mSize = sizeof(NewCatalog); + + fDiskDev.Write((Char*)&catalogKind, sectorSz); + + startLba += (sizeof(NewCatalog) + blob->fBlobSz); + + --partBlock.FreeCatalog; + --partBlock.FreeSectors; + + ++partBlock.CatalogCount; + + blob = blob->fNext; + } + return true; } @@ -222,28 +327,40 @@ private: template inline Boolean BDiskFormatFactory::Format(const char* partName, BDiskFormatFactory::BFileDescriptor* fileBlobs, SizeT blobCount) { - // if (!fileBlobs || !blobCount) return false; + if (!fileBlobs || !blobCount) return false; /// sanity check - static_assert(kNewFSMinimumSectorSz == kATASectorSize, "Sector size doesn't match!"); + /// convert the sector into something that the disk understands. + SizeT sectorSz = BootDev::kSectorSize; + Char buf[BootDev::kSectorSize] = { 0 }; - Char buf[kNewFSMinimumSectorSz] = { 0 }; NewPartitionBlock* partBlock = reinterpret_cast(buf); + memcpy(partBlock->Ident, kNewFSIdent, kNewFSIdentLen-1); memcpy(partBlock->PartitionName, partName, strlen(partName)); /// @note A catalog roughly equal to a sector. - partBlock->CatalogCount = blobCount; + partBlock->CatalogCount = 0UL; partBlock->Kind = kNewFSHardDrive; - partBlock->SectorCount = kNewFSMinimumSectorSz; + partBlock->SectorSize = sectorSz; partBlock->FreeCatalog = fDiskDev.GetSectorsCount() - partBlock->CatalogCount; partBlock->SectorCount = fDiskDev.GetSectorsCount(); partBlock->FreeSectors = fDiskDev.GetSectorsCount() - partBlock->CatalogCount; - partBlock->StartCatalog = kNewFSAddressAsLba + sizeof(NewPartitionBlock); + partBlock->StartCatalog = kNewFSCatalogStartAddress; - fDiskDev.Leak().mBase = (kNewFSAddressAsLba / kNewFSMinimumSectorSz); - fDiskDev.Leak().mSize = kNewFSMinimumSectorSz; - fDiskDev.Write(buf, kNewFSMinimumSectorSz); + if (this->WriteContent(fileBlobs, blobCount, sectorSz, *partBlock)) { + fDiskDev.Leak().mBase = (kNewFSAddressAsLba / sectorSz); + fDiskDev.Leak().mSize = BootDev::kSectorSize; + + fDiskDev.Write(buf, sectorSz); + + BTextWriter writer; + writer.Write(L"New Boot: Write-Partition, OK.\r\n"); + + return true; + } else { + EFI::ThrowError(L"Filesystem-Failure-Part", L"Filesystem couldn't be partitioned."); + } - return this->_AppendCatalogList(fileBlobs, blobCount); + return false; } diff --git a/Private/NewBoot/BootKit/HW/ATA.hxx b/Private/NewBoot/BootKit/HW/ATA.hxx index c557aeb8..4b96e9fa 100644 --- a/Private/NewBoot/BootKit/HW/ATA.hxx +++ b/Private/NewBoot/BootKit/HW/ATA.hxx @@ -23,6 +23,8 @@ class BootDeviceATA final : public Device { NEWOS_COPY_DEFAULT(BootDeviceATA); + enum { kSectorSize = kATASectorSize }; + struct ATATrait final : public Device::Trait { UInt16 mBus{kPrimary}; UInt8 mMaster{0}; diff --git a/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx b/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx index 10d4272d..8d923a50 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx @@ -132,7 +132,7 @@ ATAInit_Retry: Void boot_ata_read(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, SizeT SectorSz, SizeT Size) { - UInt8 Command = (!Master ? 0xE0 : 0xF0); + UInt8 Command = ((!Master) ? 0xE0 : 0xF0); boot_ata_wait_io(IO); @@ -146,27 +146,15 @@ Void boot_ata_read(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, Out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO); - while ((In8(ATA_COMMAND(IO))) & ATA_SR_BSY) boot_ata_wait_io(IO); - - UInt16 byte = In16(IO + ATA_REG_DATA); - SizeT IndexOff = 0UL; - Buf[IndexOff] = byte; - - while (byte != 0xFF) { - if (IndexOff > Size) break; - - ++IndexOff; - - while ((In8(ATA_COMMAND(IO))) & ATA_SR_BSY) boot_ata_wait_io(IO); - - byte = In16(IO + ATA_REG_DATA); - Buf[IndexOff] = byte; + for (SizeT IndexOff = 0; IndexOff < Size; ++IndexOff) { + boot_ata_wait_io(IO); + Buf[IndexOff] = In16(IO + ATA_REG_DATA); } } Void boot_ata_write(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, SizeT SectorSz, SizeT Size) { - UInt8 Command = (!Master ? 0xE0 : 0xF0); + UInt8 Command = ((!Master) ? 0xE0 : 0xF0); boot_ata_wait_io(IO); @@ -209,10 +197,6 @@ BootDeviceATA::BootDeviceATA() noexcept { boot_ata_init(ATA_SECONDARY_IO, true, this->Leak().mBus, this->Leak().mMaster)) { kATADetected = true; - - BTextWriter writer; - - writer.Write(L"New Boot: Drive is OnLine.\r\n"); } } /** diff --git a/Private/NewBoot/Source/HEL/AMD64/BootJump.S b/Private/NewBoot/Source/HEL/AMD64/BootJump.S new file mode 100644 index 00000000..af278cc7 --- /dev/null +++ b/Private/NewBoot/Source/HEL/AMD64/BootJump.S @@ -0,0 +1,9 @@ +.global rt_jump_to_address +.text + +.code64 +.intel_syntax noprefix + +rt_jump_to_address: + jmp rcx + ret diff --git a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx index 356db88b..ea3fcbf0 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx @@ -9,8 +9,15 @@ #include #include #include -#include +#include #include +#include +#include + +/// make the compiler shut up. +#ifndef kMachineModel +#define kMachineModel "NeWS HD" +#endif // !kMachineModel /** Graphics related. */ @@ -18,6 +25,8 @@ STATIC EfiGraphicsOutputProtocol* kGop = nullptr; STATIC UInt16 kStride = 0U; STATIC EfiGUID kGopGuid; +EXTERN_C Void rt_jump_to_address(VoidPtr blob); + /** @brief Finds and stores the GOP. */ @@ -166,45 +175,66 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, } } - BFileReader kernelFile(L"NewLoader.exe", ImageHandle); - kernelFile.ReadAll(MIB(1), 4096); + /// + /// The following checks for an exisiting partition + /// inside the disk, if it doesn't have one, + /// format the disk. + // - if (kernelFile.Blob()) { - PEFContainer* containerKind = - reinterpret_cast(kernelFile.Blob()); + /// need this as well, to invoke BExecutableLoader. + BFileReader readerKernel(L"NewKernel.exe", ImageHandle); + readerKernel.ReadAll(MIB(1), 4096); - if (containerKind->Magic[0] == kPefMagic[0] && - containerKind->Magic[1] == kPefMagic[1] && - containerKind->Magic[2] == kPefMagic[2] && - containerKind->Magic[3] == kPefMagic[3] && - containerKind->Magic[4] == kPefMagic[4]) { - if (containerKind->Abi != kPefAbi || - containerKind->Cpu != kPefArchAMD64) { - EFI::ThrowError(L"Bad-Architecture", - L"New Boot can't run this architecture."); - } + BDiskFormatFactory diskFormatter; - NewOS::HEL::BootMainKind main = - reinterpret_cast(containerKind + - containerKind->Start); + BDiskFormatFactory::BFileDescriptor rootDesc{0}; - if (!main) { - EFI::ThrowError(L"Bad-Exec", - L"New Boot can't recognize this executable."); - } + memcpy(rootDesc.fFileName, "/", strlen("/")); + memcpy(rootDesc.fForkName, kNewFSResourceFork, strlen(kNewFSResourceFork)); - EFI::ExitBootServices(MapKey, ImageHandle); + rootDesc.fBlobSz = strlen(kMachineModel " startup disk."); + rootDesc.fBlob = new Char[rootDesc.fBlobSz]; - main(handoverHdrPtr); + memcpy(rootDesc.fBlob, kMachineModel " startup disk.", + strlen(kMachineModel " startup disk.")); - EFI::Stop(); + rootDesc.fKind = kNewFSCatalogKindDir; - CANT_REACH(); - } - } + BDiskFormatFactory::BFileDescriptor bootDesc{0}; + + bootDesc.fKind = kNewFSCatalogKindDir; + + memcpy(bootDesc.fFileName, "/Boot", strlen("/Boot")); + memcpy(bootDesc.fForkName, kNewFSResourceFork, strlen(kNewFSResourceFork)); + memcpy(bootDesc.fBlob, kMachineModel " startup folder.", + strlen(kMachineModel " startup folder.")); + + bootDesc.fBlobSz = strlen(kMachineModel " startup folder."); + bootDesc.fBlob = new Char[bootDesc.fBlobSz]; + + BDiskFormatFactory::BFileDescriptor kernelDesc{0}; + + kernelDesc.fKind = kNewFSCatalogKindFile; + + memcpy(kernelDesc.fFileName, "/Boot/NewKernel", strlen("/Boot/NewKernel")); + memcpy(kernelDesc.fForkName, kNewFSDataFork, strlen(kNewFSDataFork)); + + kernelDesc.fBlob = readerKernel.Blob(); + kernelDesc.fBlobSz = readerKernel.Size(); + + rootDesc.fNext = &bootDesc; + rootDesc.fNext->fPrev = &rootDesc; + + rootDesc.fNext->fNext = &kernelDesc; + rootDesc.fNext->fNext->fPrev = &bootDesc; + + diskFormatter.Format(kMachineModel, &rootDesc, 3); + + EFI::ExitBootServices(MapKey, ImageHandle); + + rt_jump_to_address(readerKernel.Blob()); - EFI::ThrowError(L"Invalid-PEF-Executable", - L"PEF executable expected. Got something else."); + EFI::Stop(); - return kEfiFail; + CANT_REACH(); } diff --git a/Private/NewBoot/Source/HEL/AMD64/BootTextWriter.cxx b/Private/NewBoot/Source/HEL/AMD64/BootTextWriter.cxx index 81aa6c9d..ff30ff95 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootTextWriter.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootTextWriter.cxx @@ -32,7 +32,7 @@ BTextWriter &BTextWriter::Write(const CharacterTypeUTF16 *str) { return *this; } -BTextWriter &BTextWriter::Write(const UChar *str) { +BTextWriter &BTextWriter::Write(const Char *str) { #ifdef __DEBUG__ if (!str || *str == 0) return *this; diff --git a/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx b/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx index 77552ef8..01911c43 100644 --- a/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx @@ -20,6 +20,17 @@ void* operator new(size_t sz) return buf; } +/// @brief Allocates a new object. +/// @param sz the size. +/// @return +void* operator new[](size_t sz) +{ + void* buf = nullptr; + BS->AllocatePool(EfiMemoryType::EfiLoaderData, sz, &buf); + + return buf; +} + /// @brief Deletes the object. /// @param buf the object. void operator delete(void* buf) diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index 93bb5e28..92128b02 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -18,6 +18,10 @@ else EMU=qemu-system-x86_64 endif +ifeq ($(NEWS_MODEL), ) +NEWOS_MODEL=-DkMachineModel="\"Generic NeWS HD\"" +endif + IMG=epm.img IMG_2=epm-slave.img EMU_FLAGS=-net none -smp 4 -m 8G -M q35 -bios OVMF.fd -device piix3-ide,id=ide -drive id=disk,file=$(IMG),format=raw,if=none -device ide-hd,drive=disk,bus=ide.0 -drive file=fat:rw:CDROM,index=2,format=raw -d int -hdd epm-slave.img @@ -46,13 +50,10 @@ KERNEL=NewKernel.exe .PHONY: all all: compile-amd64 mkdir -p CDROM/EFI/BOOT - $(LD_GNU) $(OBJ) $(LD_FLAGS) -o $(KERNEL_OBJ) - $(IMG_CREATE) create -f raw $(BOOT_LOADER) $(MAX_KERNEL_SIZE) - $(DD) if=$(KERNEL_OBJ) of=$(BOOT_LOADER) bs=1 seek=0 conv=notrunc + $(LD_GNU) $(OBJ) $(LD_FLAGS) -o $(BOOT_LOADER) $(COPY) $(BOOT_LOADER) CDROM/EFI/BOOT/BOOTX64.EFI $(COPY) $(BOOT_LOADER) CDROM/EFI/BOOT/NEWBOOT.EFI $(COPY) ../../$(KERNEL) CDROM/$(KERNEL) - $(COPY) $(BOOT_LOADER) ../../Root/Boot/$(BOOT_LOADER) ifneq ($(DEBUG_SUPPORT), ) DEBUG = -D__DEBUG__ @@ -61,7 +62,7 @@ endif .PHONY: compile-amd64 compile-amd64: $(WINDRES) BootloaderRsrc.rsrc -O coff -o BootloaderRsrc.o - $(CC_GNU) $(FLAG_GNU) $(DEBUG) $(wildcard HEL/AMD64/*.cxx) $(wildcard *.cxx) + $(CC_GNU) $(NEWOS_MODEL) $(FLAG_GNU) $(DEBUG) $(wildcard HEL/AMD64/*.cxx) $(wildcard HEL/AMD64/*.S) $(wildcard *.cxx) .PHONY: run-efi-amd64 run-efi-amd64: @@ -69,7 +70,7 @@ run-efi-amd64: .PHONY: epm-img epm-img: - qemu-img create -f qcow2 $(IMG) 256M + qemu-img create -f qcow2 $(IMG) 512M qemu-img create -f qcow2 $(IMG_2) 512M .PHONY: download-edk diff --git a/Private/NewLoader/ReadMe.md b/Private/NewLoader/ReadMe.md deleted file mode 100644 index 955f0651..00000000 --- a/Private/NewLoader/ReadMe.md +++ /dev/null @@ -1,9 +0,0 @@ -# NewLoader - -## New OS Stage 2 loader. - -Loads and runs the NewKernel and it's components. - -- Loads: NewKernel.exe, a MicroKernel in C++ (PEF) -- Loads: Bonjour.exe, zeroconf driver. (PEF) -- Loads: MahroussUpdate.exe, mahrouss system update driver. (PEF) diff --git a/Private/NewLoader/Source/.gitkeep b/Private/NewLoader/Source/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/Private/makefile b/Private/makefile index 673a4b56..977e33ac 100644 --- a/Private/makefile +++ b/Private/makefile @@ -5,7 +5,7 @@ CC = x86_64-w64-mingw32-gcc LD = x86_64-w64-mingw32-ld -CCFLAGS = -c -ffreestanding -fPIE -fPIC -D__NEWOS_AMD64__ -mno-red-zone -fno-rtti -fno-exceptions -std=c++20 -D__FSKIT_NEWFS__ -D__KERNEL__ -D__HAVE_MAHROUSS_APIS__ -D__MAHROUSS__ -I../ -I./ +CCFLAGS = -c -ffreestanding -fPIC -D__NEWOS_AMD64__ -mno-red-zone -fno-rtti -fno-exceptions -std=c++20 -D__FSKIT_NEWFS__ -D__KERNEL__ -D__HAVE_MAHROUSS_APIS__ -D__MAHROUSS__ -I../ -I./ ASM = nasm ifneq ($(ATA_PIO_SUPPORT), ) @@ -30,7 +30,7 @@ COPY = cp ASMFLAGS = -f win64 # NewOS subsystem is 17. -LDFLAGS = -e Main --subsystem=17 +LDFLAGS = -e __ImageStart --subsystem=17 LDOBJ = Objects/*.obj # This file is the kernel, responsible of task management and memory. @@ -59,17 +59,13 @@ newos-amd64-epm: clean OBJCOPY=x86_64-w64-mingw32-objcopy -KERNEL_OBJ=kernel.bin DD=dd IMG_CREATE=qemu-img MAX_KERNEL_SIZE=1024K .PHONY: link-amd64-epm link-amd64-epm: - $(LD) $(LDFLAGS) $(LDOBJ) -o $(KERNEL_OBJ) - $(IMG_CREATE) create -f raw $(KERNEL) $(MAX_KERNEL_SIZE) - $(DD) if=$(KERNEL_OBJ) of=$(KERNEL) bs=1 seek=0 conv=notrunc - $(COPY) $(KERNEL) Root/Boot + $(LD) $(LDFLAGS) $(LDOBJ) -o $(KERNEL) .PHONY: all all: newos-amd64-epm link-amd64-epm -- cgit v1.2.3 From 4afe2bd4c6e95f2ca4a5b9bdacf51ef5f91fd0d7 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 26 Apr 2024 08:28:10 +0200 Subject: MHR-16: Fix kernel driver as well. Signed-off-by: Amlal El Mahrouss --- Private/HALKit/AMD64/Storage/ATA-PIO.cxx | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'Private/HALKit') diff --git a/Private/HALKit/AMD64/Storage/ATA-PIO.cxx b/Private/HALKit/AMD64/Storage/ATA-PIO.cxx index 652aa6f4..f2ae01a4 100644 --- a/Private/HALKit/AMD64/Storage/ATA-PIO.cxx +++ b/Private/HALKit/AMD64/Storage/ATA-PIO.cxx @@ -132,15 +132,14 @@ ATAInit_Retry: Void drv_std_read(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorSz, SizeT Size) { - UInt8 Command = (!Master ? 0xE0 : 0xF0); + UInt8 Command = ((!Master )? 0xE0 : 0xF0); Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0xF)); - Out8(IO + ATA_REG_SEC_COUNT0, SectorSz); + Out8(IO + ATA_REG_SEC_COUNT0, 1); Out8(IO + ATA_REG_LBA0, (Lba)); Out8(IO + ATA_REG_LBA1, (Lba) >> 8); Out8(IO + ATA_REG_LBA2, (Lba) >> 16); - Out8(IO + ATA_REG_LBA3, (Lba) >> 24); Out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO); @@ -155,15 +154,14 @@ Void drv_std_read(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, Void drv_std_write(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorSz, SizeT Size) { - UInt8 Command = (!Master ? 0xE0 : 0xF0); + UInt8 Command = ((!Master) ? 0xE0 : 0xF0); Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0xF)); - Out8(IO + ATA_REG_SEC_COUNT0, SectorSz); + Out8(IO + ATA_REG_SEC_COUNT0, 1); Out8(IO + ATA_REG_LBA0, (Lba)); Out8(IO + ATA_REG_LBA1, (Lba) >> 8); Out8(IO + ATA_REG_LBA2, (Lba) >> 16); - Out8(IO + ATA_REG_LBA3, (Lba) >> 24); Out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO); -- cgit v1.2.3 From ba80f513dd5cd82feae0b8f794b3367caf9ce8bb Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 26 Apr 2024 10:20:01 +0200 Subject: MHR-16: a lot of fixes. Regarding the disk driver and the filesystem class BDiskFormatFactory. Signed-off-by: Amlal El Mahrouss --- Private/Builtins/ATA/ATA.hxx | 3 +- Private/HALKit/AMD64/Storage/ATA-PIO.cxx | 6 +- Private/NewBoot/BootKit/BootKit.hxx | 110 +++++++++++++++----------- Private/NewBoot/BootKit/HW/ATA.hxx | 1 + Private/NewBoot/Source/HEL/AMD64/BootATA.cxx | 67 +++++++--------- Private/NewBoot/Source/HEL/AMD64/BootMain.cxx | 13 ++- Private/NewBoot/Source/makefile | 2 +- 7 files changed, 110 insertions(+), 92 deletions(-) (limited to 'Private/HALKit') diff --git a/Private/Builtins/ATA/ATA.hxx b/Private/Builtins/ATA/ATA.hxx index 66856d8a..a2ce79ba 100644 --- a/Private/Builtins/ATA/ATA.hxx +++ b/Private/Builtins/ATA/ATA.hxx @@ -102,7 +102,8 @@ #define ATA_PRIMARY 0x00 #define ATA_SECONDARY 0x01 -#define ATA_CYL_LOW 4 +#define ATA_CYL_LOW 3 +#define ATA_CYL_MID 4 #define ATA_CYL_HIGH 5 ///! IO Direction diff --git a/Private/HALKit/AMD64/Storage/ATA-PIO.cxx b/Private/HALKit/AMD64/Storage/ATA-PIO.cxx index f2ae01a4..beb113d3 100644 --- a/Private/HALKit/AMD64/Storage/ATA-PIO.cxx +++ b/Private/HALKit/AMD64/Storage/ATA-PIO.cxx @@ -134,12 +134,13 @@ Void drv_std_read(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorSz, SizeT Size) { UInt8 Command = ((!Master )? 0xE0 : 0xF0); - Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0xF)); + Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); Out8(IO + ATA_REG_SEC_COUNT0, 1); Out8(IO + ATA_REG_LBA0, (Lba)); Out8(IO + ATA_REG_LBA1, (Lba) >> 8); Out8(IO + ATA_REG_LBA2, (Lba) >> 16); + Out8(IO + ATA_REG_LBA4, (Lba) >> 24); Out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO); @@ -156,12 +157,13 @@ Void drv_std_write(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorSz, SizeT Size) { UInt8 Command = ((!Master) ? 0xE0 : 0xF0); - Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0xF)); + Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); Out8(IO + ATA_REG_SEC_COUNT0, 1); Out8(IO + ATA_REG_LBA0, (Lba)); Out8(IO + ATA_REG_LBA1, (Lba) >> 8); Out8(IO + ATA_REG_LBA2, (Lba) >> 16); + Out8(IO + ATA_REG_LBA4, (Lba) >> 24); Out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO); diff --git a/Private/NewBoot/BootKit/BootKit.hxx b/Private/NewBoot/BootKit/BootKit.hxx index ffa3af15..b3a81160 100644 --- a/Private/NewBoot/BootKit/BootKit.hxx +++ b/Private/NewBoot/BootKit/BootKit.hxx @@ -214,14 +214,25 @@ public: Boolean Format(const char* partName, BFileDescriptor* fileBlobs, SizeT blobCount); operator bool() noexcept { - fDiskDev.Leak().mBase = (kNewFSAddressAsLba / BootDev::kSectorSize); + fDiskDev.Leak().mBase = (kNewFSAddressAsLba); fDiskDev.Leak().mSize = BootDev::kSectorSize; Char buf[BootDev::kSectorSize] = { 0 }; fDiskDev.Read(buf, BootDev::kSectorSize); - return buf[0] == kNewFSIdent[0]; + NewPartitionBlock* blockPart = reinterpret_cast(buf); + + for (SizeT indexMag = 0UL; indexMag < kNewFSIdentLen; ++indexMag) { + if (blockPart->Ident[indexMag] != kNewFSIdent[indexMag]) + return false; + } + + BTextWriter writer; + + writer.Write(L"Disk Partition: ").Write(blockPart->PartitionName).Write(L" is okay.\r\n"); + + return true; } private: @@ -231,43 +242,55 @@ private: if (sectorSz != BootDev::kSectorSize) return false; BFileDescriptor* blob = fileBlobs; - Lba startLba = partBlock.FreeCatalog; - + Lba startLba = partBlock.StartCatalog; BTextWriter writer; + while (blob) { - NewCatalog catalogKind{ 0 }; + NewCatalog* catalogKind = new NewCatalog(); + memset(catalogKind, 0, sizeof(NewCatalog)); /// Fill catalog kind. - catalogKind.Kind = blob->fKind; + catalogKind->Kind = blob->fKind; /// Allocate fork for blob. - catalogKind.FirstFork = (startLba + sizeof(NewCatalog)); - catalogKind.LastFork = catalogKind.FirstFork; + catalogKind->FirstFork = (startLba + sizeof(NewCatalog)); + catalogKind->LastFork = catalogKind->FirstFork; - NewFork forkKind{ 0 }; + NewFork* forkKind = new NewFork(); + memset(forkKind, 0, sizeof(NewFork)); - memcpy(forkKind.Name, blob->fForkName, strlen(blob->fForkName)); - forkKind.Kind = (forkKind.Name[0] == kNewFSDataFork[0]) ? kNewFSDataForkKind : kNewFSRsrcForkKind; - forkKind.Flags = kNewFSFlagCreated; + memcpy(forkKind->Name, blob->fForkName, strlen(blob->fForkName)); + forkKind->Kind = (forkKind->Name[0] == kNewFSDataFork[0]) ? kNewFSDataForkKind : kNewFSRsrcForkKind; + forkKind->Flags = kNewFSFlagCreated; /// We don't know. - forkKind.ResourceFlags = 0; - forkKind.ResourceId = 0; - forkKind.ResourceKind = 0; + forkKind->ResourceFlags = 0; + forkKind->ResourceId = 0; + forkKind->ResourceKind = 0; /// We're the only fork here. - forkKind.NextSibling = catalogKind.FirstFork; - forkKind.PreviousSibling = catalogKind.FirstFork; + forkKind->NextSibling = catalogKind->FirstFork; + forkKind->PreviousSibling = catalogKind->FirstFork; - forkKind.DataOffset = (startLba + sizeof(NewCatalog) + sizeof(NewFork)); - forkKind.DataSize = blob->fBlobSz; + forkKind->DataOffset = (startLba + sizeof(NewCatalog) + sizeof(NewFork)); + forkKind->DataSize = blob->fBlobSz; - Lba lbaStart = forkKind.DataOffset; + Lba lbaStart = forkKind->DataOffset; SizeT cur = 0UL; - while (cur < forkKind.DataSize) { + writer.Write((catalogKind->Kind == kNewFSCatalogKindFile) ? L"New Boot: Write-File: " : + L"New Boot: Write-Directory: " ).Write(blob->fFileName).Write(L"\r\n"); + + /// Set disk cursor here. + + fDiskDev.Leak().mBase = startLba + sizeof(NewCatalog); + fDiskDev.Leak().mSize = sizeof(NewFork); + + fDiskDev.Write((Char*)forkKind, sizeof(NewFork)); + + while (cur < forkKind->DataSize) { this->fDiskDev.Leak().mSize = BootDev::kSectorSize; - this->fDiskDev.Leak().mBase = forkKind.DataOffset / BootDev::kSectorSize; + this->fDiskDev.Leak().mBase = (forkKind->DataOffset + cur); this->fDiskDev.Write((Char*)(blob->fBlob) + cur, BootDev::kSectorSize); @@ -275,48 +298,40 @@ private: lbaStart += BootDev::kSectorSize; } - writer.Write((catalogKind.Kind == kNewFSCatalogKindFile) ? L"New Boot: Write-File: " : - L"New Boot: Write-Directory: " ).Write(blob->fFileName).Write(L"\r\n"); - - /// Set disk cursor here. - - fDiskDev.Leak().mBase = startLba + sizeof(NewCatalog) / sectorSz; - fDiskDev.Leak().mSize = sizeof(NewFork); - - fDiskDev.Write((Char*)&forkKind, 1); /// Fork is done. - catalogKind.Kind = blob->fKind; - catalogKind.Flags |= kNewFSFlagCreated; + catalogKind->Kind = blob->fKind; + catalogKind->Flags |= kNewFSFlagCreated; Lba catalogLba = (sizeof(NewCatalog) - startLba); //// Now write catalog as well.. - catalogKind.PrevSibling = startLba; - catalogKind.NextSibling = (sizeof(NewCatalog) + blob->fBlobSz); + catalogKind->PrevSibling = startLba; + catalogKind->NextSibling = (sizeof(NewCatalog) + blob->fBlobSz); /// this mime only applies to file. - if (catalogKind.Kind == kNewFSCatalogKindFile) { - memcpy(catalogKind.Mime, kBKBootFileMime, strlen(kBKBootFileMime)); + if (catalogKind->Kind == kNewFSCatalogKindFile) { + memcpy(catalogKind->Mime, kBKBootFileMime, strlen(kBKBootFileMime)); } else { - memcpy(catalogKind.Mime, kBKBootDirMime, strlen(kBKBootDirMime)); + memcpy(catalogKind->Mime, kBKBootDirMime, strlen(kBKBootDirMime)); } - memcpy(catalogKind.Name, blob->fFileName, strlen(blob->fFileName)); + memcpy(catalogKind->Name, blob->fFileName, strlen(blob->fFileName)); - fDiskDev.Leak().mBase = startLba / sectorSz; + fDiskDev.Leak().mBase = startLba; fDiskDev.Leak().mSize = sizeof(NewCatalog); - fDiskDev.Write((Char*)&catalogKind, sectorSz); + fDiskDev.Write((Char*)catalogKind, sizeof(NewCatalog)); - startLba += (sizeof(NewCatalog) + blob->fBlobSz); + startLba += (sizeof(NewCatalog) + sizeof(NewFork) + blob->fBlobSz); --partBlock.FreeCatalog; --partBlock.FreeSectors; - ++partBlock.CatalogCount; + delete forkKind; + delete catalogKind; blob = blob->fNext; } @@ -346,22 +361,23 @@ inline Boolean BDiskFormatFactory::Format(const char* partName, NewPartitionBlock* partBlock = reinterpret_cast(buf); - memcpy(partBlock->Ident, kNewFSIdent, kNewFSIdentLen-1); + memcpy(partBlock->Ident, kNewFSIdent, kNewFSIdentLen - 1); memcpy(partBlock->PartitionName, partName, strlen(partName)); /// @note A catalog roughly equal to a sector. - partBlock->CatalogCount = 0UL; + partBlock->CatalogCount = blobCount; partBlock->Kind = kNewFSHardDrive; partBlock->SectorSize = sectorSz; partBlock->FreeCatalog = fDiskDev.GetSectorsCount() - partBlock->CatalogCount; partBlock->SectorCount = fDiskDev.GetSectorsCount(); partBlock->FreeSectors = fDiskDev.GetSectorsCount() - partBlock->CatalogCount; partBlock->StartCatalog = kNewFSCatalogStartAddress; + partBlock->DiskSize = fDiskDev.GetDiskSize(); if (this->WriteContent(fileBlobs, blobCount, sectorSz, *partBlock)) { - fDiskDev.Leak().mBase = (kNewFSAddressAsLba / sectorSz); - fDiskDev.Leak().mSize = BootDev::kSectorSize; + fDiskDev.Leak().mBase = (kNewFSAddressAsLba); + fDiskDev.Leak().mSize = sectorSz; fDiskDev.Write(buf, sectorSz); diff --git a/Private/NewBoot/BootKit/HW/ATA.hxx b/Private/NewBoot/BootKit/HW/ATA.hxx index 4b96e9fa..5bdcec56 100644 --- a/Private/NewBoot/BootKit/HW/ATA.hxx +++ b/Private/NewBoot/BootKit/HW/ATA.hxx @@ -37,6 +37,7 @@ class BootDeviceATA final : public Device { operator bool(); SizeT GetSectorsCount() noexcept; + SizeT GetDiskSize() noexcept; BootDeviceATA& Read(Char* Buf, const SizeT& SecCount) override; BootDeviceATA& Write(Char* Buf, const SizeT& SecCount) override; diff --git a/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx b/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx index bb634fbf..41ea04ac 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx @@ -88,44 +88,13 @@ ATAInit_Retry: boot_ata_wait_io(IO); for (SizeT indexData = 0ul; indexData < kATADataLen; ++indexData) { - kATAData[indexData] = In16(IO + ATA_REG_DATA); + kATAData[indexData] = In8(IO + ATA_REG_DATA); } OutBus = (Bus == ATA_PRIMARY_IO) ? BootDeviceATA::kPrimary : BootDeviceATA::kSecondary; - OutMaster = (Bus == ATA_PRIMARY_IO) ? ATA_MASTER : ATA_SLAVE; - - Out8(Bus + ATA_REG_HDDEVSEL, 0xA0 | ATA_MASTER << 4); - - In8(Bus + ATA_REG_CONTROL); - In8(Bus + ATA_REG_CONTROL); - In8(Bus + ATA_REG_CONTROL); - In8(Bus + ATA_REG_CONTROL); - - unsigned cl = In8(Bus + ATA_CYL_LOW); /* get the "signature bytes" */ - unsigned ch = In8(Bus + ATA_CYL_HIGH); - - /* differentiate ATA, ATAPI, SATA and SATAPI */ - if (cl == 0x14 && ch == 0xEB) { - writer.Write(L"New Boot: PATAPI drive detected.\r\n"); - kATADeviceType = kATADevicePATA_PI; - } - if (cl == 0x69 && ch == 0x96) { - writer.Write(L"New Boot: SATAPI drive detected.\r\n"); - kATADeviceType = kATADeviceSATA_PI; - } - - if (cl == 0x0 && ch == 0x0) { - writer.Write(L"New Boot: PATA drive detected.\r\n"); - kATADeviceType = kATADevicePATA; - } - - if (cl == 0x3c && ch == 0xc3) { - writer.Write(L"New Boot: SATA drive detected.\r\n"); - kATADeviceType = kATADeviceSATA; - } - Out8(IO + ATA_REG_CONTROL, 0x02); + OutMaster = (Bus == ATA_PRIMARY_IO) ? ATA_MASTER : ATA_SLAVE; return true; } @@ -137,12 +106,13 @@ Void boot_ata_read(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, boot_ata_wait_io(IO); boot_ata_select(IO); - Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0xF)); + Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); Out8(IO + ATA_REG_SEC_COUNT0, 1); Out8(IO + ATA_REG_LBA0, (Lba)); Out8(IO + ATA_REG_LBA1, (Lba) >> 8); Out8(IO + ATA_REG_LBA2, (Lba) >> 16); + Out8(IO + ATA_REG_LBA4, (Lba) >> 24); Out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO); @@ -159,12 +129,13 @@ Void boot_ata_write(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, boot_ata_wait_io(IO); boot_ata_select(IO); - Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0xF)); + Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); Out8(IO + ATA_REG_SEC_COUNT0, 1); Out8(IO + ATA_REG_LBA0, (Lba)); Out8(IO + ATA_REG_LBA1, (Lba) >> 8); Out8(IO + ATA_REG_LBA2, (Lba) >> 16); + Out8(IO + ATA_REG_LBA4, (Lba) >> 24); Out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO); @@ -217,7 +188,9 @@ BootDeviceATA& BootDeviceATA::Read(CharacterTypeUTF8* Buf, const SizeT& SectorSz if (!Buf || SectorSz < 1) return *this; - boot_ata_read(this->Leak().mBase, this->Leak().mBus, this->Leak().mMaster, + auto lba = this->Leak().mBase / BootDeviceATA::kSectorSize; + + boot_ata_read(lba, this->Leak().mBus, this->Leak().mMaster, Buf, SectorSz, this->Leak().mSize); return *this; @@ -238,7 +211,9 @@ BootDeviceATA& BootDeviceATA::Write(CharacterTypeUTF8* Buf, const SizeT& SectorS if (!Buf || SectorSz < 1) return *this; - boot_ata_write(this->Leak().mBase, this->Leak().mBus, this->Leak().mMaster, + auto lba = this->Leak().mBase / BootDeviceATA::kSectorSize; + + boot_ata_write(lba, this->Leak().mBus, this->Leak().mMaster, Buf, SectorSz, this->Leak().mSize); return *this; @@ -256,3 +231,21 @@ BootDeviceATA::ATATrait& BootDeviceATA::Leak() { return mTrait; } SizeT BootDeviceATA::GetSectorsCount() noexcept { return kATAData[60] + kATAData[61]; } + +SizeT BootDeviceATA::GetDiskSize() noexcept { + Out8(this->Leak().mBus + ATA_REG_COMMAND, 0xF8); + + boot_ata_wait_io(this->Leak().mBus); + + SizeT result = 0; + + result += In8(this->Leak().mBus + ATA_CYL_LOW); + result += In8(this->Leak().mBus + ATA_CYL_MID) << 8; + result += In8(this->Leak().mBus + ATA_CYL_HIGH) << 16; + result += In8(this->Leak().mBus + ATA_CYL_HIGH) << 24; + + BTextWriter writer; + writer.Write(L"Disk-Size: ").Write(result).Write(L"\r\n"); + + return result; +} diff --git a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx index 5e9e2355..a928a41a 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx @@ -193,9 +193,11 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, memcpy(rootDesc.fFileName, "/", strlen("/")); memcpy(rootDesc.fForkName, kNewFSResourceFork, strlen(kNewFSResourceFork)); - rootDesc.fBlobSz = strlen(kMachineModel " startup disk."); + rootDesc.fBlobSz = BootDeviceATA::kSectorSize; rootDesc.fBlob = new Char[rootDesc.fBlobSz]; + memset(rootDesc.fBlob, 0, rootDesc.fBlobSz); + memcpy(rootDesc.fBlob, kMachineModel " startup disk.", strlen(kMachineModel " startup disk.")); @@ -207,12 +209,15 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, memcpy(bootDesc.fFileName, "/Boot", strlen("/Boot")); memcpy(bootDesc.fForkName, kNewFSResourceFork, strlen(kNewFSResourceFork)); + + bootDesc.fBlobSz = BootDeviceATA::kSectorSize; + bootDesc.fBlob = new Char[rootDesc.fBlobSz]; + + memset(bootDesc.fBlob, 0, bootDesc.fBlobSz); + memcpy(bootDesc.fBlob, kMachineModel " startup folder.", strlen(kMachineModel " startup folder.")); - bootDesc.fBlobSz = strlen(kMachineModel " startup folder."); - bootDesc.fBlob = new Char[bootDesc.fBlobSz]; - BDiskFormatFactory::BFileDescriptor kernelDesc{0}; kernelDesc.fKind = kNewFSCatalogKindFile; diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index 92128b02..4d8e1100 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -70,7 +70,7 @@ run-efi-amd64: .PHONY: epm-img epm-img: - qemu-img create -f qcow2 $(IMG) 512M + qemu-img create -f qcow2 $(IMG) 1G qemu-img create -f qcow2 $(IMG_2) 512M .PHONY: download-edk -- cgit v1.2.3 From 313c303fab092b1c45e615f960826375e7eef093 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 26 Apr 2024 14:23:37 +0200 Subject: MHR-16: Last commit on branch, merging now. - boot manager and micro kernel have been unified, as the USA design states. Signed-off-by: Amlal El Mahrouss --- Private/HALKit/AMD64/HalNewBoot.asm | 3 +++ Private/NewBoot/Source/CDROM/SplashScreen.fmt | 5 +++-- Private/NewBoot/Source/HEL/AMD64/BootMain.cxx | 25 ++++------------------- Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx | 9 ++++++++ Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx | 4 ++++ Private/NewBoot/Source/HEL/AMD64/Support.cxx | 5 ++++- Private/NewBoot/Source/makefile | 5 ++--- Private/Source/Utils.cxx | 9 ++++++-- 8 files changed, 36 insertions(+), 29 deletions(-) (limited to 'Private/HALKit') diff --git a/Private/HALKit/AMD64/HalNewBoot.asm b/Private/HALKit/AMD64/HalNewBoot.asm index 11b11c0c..e9034f05 100644 --- a/Private/HALKit/AMD64/HalNewBoot.asm +++ b/Private/HALKit/AMD64/HalNewBoot.asm @@ -28,9 +28,12 @@ HandoverStart: dq __ImageStart section .text global __ImageStart +global __NewBootJumpProc + extern hal_init_platform ;; Just a simple setup, we'd also need to tell some before +__NewBootJumpProc: __ImageStart: push rcx call hal_init_platform diff --git a/Private/NewBoot/Source/CDROM/SplashScreen.fmt b/Private/NewBoot/Source/CDROM/SplashScreen.fmt index 339b94e7..857c63a1 100644 --- a/Private/NewBoot/Source/CDROM/SplashScreen.fmt +++ b/Private/NewBoot/Source/CDROM/SplashScreen.fmt @@ -1,6 +1,7 @@ Welcome to NeWS. -Brought to you by: -* MicroKernel, Bootloader: Amlal EL Mahrouss. +Brought to you by: Amlal EL Mahrouss. +* NewBoot, NewKernel: Amlal EL Mahrouss. +This copy can boot directly to NewKernel (Unified System). Copyright Mahrouss-Logic, all rights reserved. diff --git a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx index 1c969b28..68762089 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx @@ -21,6 +21,8 @@ /** Graphics related. */ +EXTERN_C Void hal_init_platform(HEL::HandoverInformationHeader* HIH); + STATIC EfiGraphicsOutputProtocol* kGop = nullptr; STATIC UInt16 kStride = 0U; STATIC EfiGUID kGopGuid; @@ -181,12 +183,6 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, /// format the disk. // - constexpr auto binarySize = KIB(512); - - /// need this as well, to invoke BExecutableLoader. - BFileReader readerKernel(L"NewKernel.exe", ImageHandle); - readerKernel.ReadAll(binarySize, BootDeviceATA::kSectorSize); - BDiskFormatFactory diskFormatter; if (!diskFormatter) { @@ -220,28 +216,15 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, memcpy(bootDesc.fBlob, kMachineModel " startup folder.", strlen(kMachineModel " startup folder.")); - BDiskFormatFactory::BFileDescriptor kernelDesc{0}; - - kernelDesc.fKind = kNewFSCatalogKindFile; - - memcpy(kernelDesc.fFileName, "/Boot/NewKernel", strlen("/Boot/NewKernel")); - memcpy(kernelDesc.fForkName, kNewFSDataFork, strlen(kNewFSDataFork)); - - kernelDesc.fBlob = readerKernel.Blob(); - kernelDesc.fBlobSz = readerKernel.Size(); - rootDesc.fNext = &bootDesc; rootDesc.fNext->fPrev = &rootDesc; - rootDesc.fNext->fNext = &kernelDesc; - rootDesc.fNext->fNext->fPrev = &bootDesc; - - diskFormatter.Format(kMachineModel, &rootDesc, 3); + diskFormatter.Format(kMachineModel, &rootDesc, 2); } EFI::ExitBootServices(MapKey, ImageHandle); - rt_jump_to_address(readerKernel.Blob()); + hal_init_platform(kHandoverHeader); EFI::Stop(); diff --git a/Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx b/Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx index f47e3532..2ac90dd8 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx @@ -7,6 +7,9 @@ #include #include #include +#include "HALKit/AMD64/Processor.hpp" + +#if 0 EXTERN_C void rt_hlt() { asm volatile("hlt"); } @@ -50,3 +53,9 @@ EXTERN_C UInt32 In32(UInt16 port) { return value; } + +#else + +void rt_hlt() { NewOS::HAL::rt_halt(); } + +#endif // 0 diff --git a/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx b/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx index 01911c43..909ccca6 100644 --- a/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx @@ -9,6 +9,8 @@ #include #include /* Since we're using GCC for this EFI program. */ +#if 0 + /// @brief Allocates a new object. /// @param sz the size. /// @return @@ -45,3 +47,5 @@ void operator delete(void* buf, size_t size) { BS->FreePool(buf); } + +#endif // Inactive diff --git a/Private/NewBoot/Source/HEL/AMD64/Support.cxx b/Private/NewBoot/Source/HEL/AMD64/Support.cxx index 0508d491..a8e2c275 100644 --- a/Private/NewBoot/Source/HEL/AMD64/Support.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/Support.cxx @@ -8,6 +8,8 @@ #include #include +#if 0 + /// @brief memset definition in C++. /// @param dst destination pointer. /// @param byte value to fill in. @@ -41,6 +43,7 @@ EXTERN_C size_t strlen(const char *whatToCheck) { return len; } - /// @brief somthing specific to the microsoft ABI, regarding checking the stack. EXTERN_C void ___chkstk_ms(void) {} + +#endif diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index 92128b02..8306f844 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -28,7 +28,7 @@ EMU_FLAGS=-net none -smp 4 -m 8G -M q35 -bios OVMF.fd -device piix3-ide,id=ide - LD_FLAGS=-e Main --subsystem=10 -OBJ=*.o +OBJ=*.o ../../Objects/*.obj REM=rm REM_FLAG=-f @@ -53,7 +53,6 @@ all: compile-amd64 $(LD_GNU) $(OBJ) $(LD_FLAGS) -o $(BOOT_LOADER) $(COPY) $(BOOT_LOADER) CDROM/EFI/BOOT/BOOTX64.EFI $(COPY) $(BOOT_LOADER) CDROM/EFI/BOOT/NEWBOOT.EFI - $(COPY) ../../$(KERNEL) CDROM/$(KERNEL) ifneq ($(DEBUG_SUPPORT), ) DEBUG = -D__DEBUG__ @@ -61,7 +60,7 @@ endif .PHONY: compile-amd64 compile-amd64: - $(WINDRES) BootloaderRsrc.rsrc -O coff -o BootloaderRsrc.o + # $(WINDRES) BootloaderRsrc.rsrc -O coff -o BootloaderRsrc.o $(CC_GNU) $(NEWOS_MODEL) $(FLAG_GNU) $(DEBUG) $(wildcard HEL/AMD64/*.cxx) $(wildcard HEL/AMD64/*.S) $(wildcard *.cxx) .PHONY: run-efi-amd64 diff --git a/Private/Source/Utils.cxx b/Private/Source/Utils.cxx index e12041c8..fcdddc4a 100644 --- a/Private/Source/Utils.cxx +++ b/Private/Source/Utils.cxx @@ -169,10 +169,15 @@ char *rt_string_frofchar(char *str, const char chr) { } } // namespace NewOS -extern "C" void memset(void *dst, char src, size_t len) { +EXTERN_C void memset(void *dst, char src, size_t len) { NewOS::rt_set_memory(dst, src, len); } -extern "C" void memcpy(void *dst, void *src, size_t len) { +EXTERN_C void memcpy(void *dst, void *src, size_t len) { NewOS::rt_copy_memory(src, dst, len); } + +/// @brief strlen definition in C++. +EXTERN_C size_t strlen(const char *whatToCheck) { + return NewOS::rt_string_len(whatToCheck); +} -- cgit v1.2.3