From 9cf4007cc5a2d37c8a49cbc25a564cde5981e7b0 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 26 Apr 2024 08:26:54 +0200 Subject: MHR-16: Last fix and improvements. Signed-off-by: Amlal El Mahrouss --- Private/NewBoot/BootKit/BootKit.hxx | 11 ++++++ Private/NewBoot/Source/HEL/AMD64/BootATA.cxx | 12 +++--- Private/NewBoot/Source/HEL/AMD64/BootMain.cxx | 56 ++++++++++++++------------- 3 files changed, 45 insertions(+), 34 deletions(-) diff --git a/Private/NewBoot/BootKit/BootKit.hxx b/Private/NewBoot/BootKit/BootKit.hxx index 6272e4c9..ffa3af15 100644 --- a/Private/NewBoot/BootKit/BootKit.hxx +++ b/Private/NewBoot/BootKit/BootKit.hxx @@ -213,6 +213,17 @@ public: /// @retval False failed to format. Boolean Format(const char* partName, BFileDescriptor* fileBlobs, SizeT blobCount); + operator bool() noexcept { + fDiskDev.Leak().mBase = (kNewFSAddressAsLba / BootDev::kSectorSize); + fDiskDev.Leak().mSize = BootDev::kSectorSize; + + Char buf[BootDev::kSectorSize] = { 0 }; + + fDiskDev.Read(buf, BootDev::kSectorSize); + + return buf[0] == kNewFSIdent[0]; + } + private: /// @brief Write all of the requested catalogs into the filesystem. Boolean WriteContent(BFileDescriptor* fileBlobs, SizeT blobCount, diff --git a/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx b/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx index 8d923a50..bb634fbf 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx @@ -56,7 +56,7 @@ Void boot_ata_select(UInt16 Bus) { Boolean boot_ata_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster) { - if (boot_ata_detected()) return false; + if (boot_ata_detected()) return true; BTextWriter writer; @@ -135,14 +135,14 @@ Void boot_ata_read(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, UInt8 Command = ((!Master) ? 0xE0 : 0xF0); boot_ata_wait_io(IO); + boot_ata_select(IO); 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); @@ -157,14 +157,14 @@ Void boot_ata_write(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, UInt8 Command = ((!Master) ? 0xE0 : 0xF0); boot_ata_wait_io(IO); + boot_ata_select(IO); 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); @@ -190,8 +190,6 @@ Boolean boot_ata_detected(Void) { return kATADetected; } * @param void none. */ BootDeviceATA::BootDeviceATA() noexcept { - if (boot_ata_detected()) return; - if (boot_ata_init(ATA_PRIMARY_IO, true, this->Leak().mBus, this->Leak().mMaster) || boot_ata_init(ATA_SECONDARY_IO, true, this->Leak().mBus, diff --git a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx index ea3fcbf0..5e9e2355 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx @@ -187,48 +187,50 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, BDiskFormatFactory diskFormatter; - BDiskFormatFactory::BFileDescriptor rootDesc{0}; + if (!diskFormatter) { + BDiskFormatFactory::BFileDescriptor rootDesc{0}; - memcpy(rootDesc.fFileName, "/", strlen("/")); - memcpy(rootDesc.fForkName, kNewFSResourceFork, strlen(kNewFSResourceFork)); + memcpy(rootDesc.fFileName, "/", strlen("/")); + memcpy(rootDesc.fForkName, kNewFSResourceFork, strlen(kNewFSResourceFork)); - rootDesc.fBlobSz = strlen(kMachineModel " startup disk."); - rootDesc.fBlob = new Char[rootDesc.fBlobSz]; + rootDesc.fBlobSz = strlen(kMachineModel " startup disk."); + rootDesc.fBlob = new Char[rootDesc.fBlobSz]; - memcpy(rootDesc.fBlob, kMachineModel " startup disk.", - strlen(kMachineModel " startup disk.")); + memcpy(rootDesc.fBlob, kMachineModel " startup disk.", + strlen(kMachineModel " startup disk.")); - rootDesc.fKind = kNewFSCatalogKindDir; + rootDesc.fKind = kNewFSCatalogKindDir; - BDiskFormatFactory::BFileDescriptor bootDesc{0}; + BDiskFormatFactory::BFileDescriptor bootDesc{0}; - bootDesc.fKind = kNewFSCatalogKindDir; + 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.")); + 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]; + bootDesc.fBlobSz = strlen(kMachineModel " startup folder."); + bootDesc.fBlob = new Char[bootDesc.fBlobSz]; - BDiskFormatFactory::BFileDescriptor kernelDesc{0}; + BDiskFormatFactory::BFileDescriptor kernelDesc{0}; - kernelDesc.fKind = kNewFSCatalogKindFile; + kernelDesc.fKind = kNewFSCatalogKindFile; - memcpy(kernelDesc.fFileName, "/Boot/NewKernel", strlen("/Boot/NewKernel")); - memcpy(kernelDesc.fForkName, kNewFSDataFork, strlen(kNewFSDataFork)); + memcpy(kernelDesc.fFileName, "/Boot/NewKernel", strlen("/Boot/NewKernel")); + memcpy(kernelDesc.fForkName, kNewFSDataFork, strlen(kNewFSDataFork)); - kernelDesc.fBlob = readerKernel.Blob(); - kernelDesc.fBlobSz = readerKernel.Size(); + kernelDesc.fBlob = readerKernel.Blob(); + kernelDesc.fBlobSz = readerKernel.Size(); - rootDesc.fNext = &bootDesc; - rootDesc.fNext->fPrev = &rootDesc; + rootDesc.fNext = &bootDesc; + rootDesc.fNext->fPrev = &rootDesc; - rootDesc.fNext->fNext = &kernelDesc; - rootDesc.fNext->fNext->fPrev = &bootDesc; + rootDesc.fNext->fNext = &kernelDesc; + rootDesc.fNext->fNext->fPrev = &bootDesc; - diskFormatter.Format(kMachineModel, &rootDesc, 3); + diskFormatter.Format(kMachineModel, &rootDesc, 3); + } EFI::ExitBootServices(MapKey, ImageHandle); -- cgit v1.2.3