diff options
| author | Amlal <amlalelmahrouss@icloud.com> | 2024-04-28 15:13:03 +0000 |
|---|---|---|
| committer | Amlal <amlalelmahrouss@icloud.com> | 2024-04-28 15:13:03 +0000 |
| commit | 14f10cc0b35155ddb19ec9069ebb884246e61dcf (patch) | |
| tree | a988617d1c511cf04eb2c2392829a37d82a59e2e /Private/NewBoot | |
| parent | db0681412191dcceb5aa99cf31fb8339d6bc4adb (diff) | |
| parent | 346558208d39a036effe3a4ec232fa5df5a3c8e7 (diff) | |
Merged in MHR-18 (pull request #8)
MHR-18: A lot of fixes and improvements, mostly related to disk I/O and kernel stability.
Diffstat (limited to 'Private/NewBoot')
| -rw-r--r-- | Private/NewBoot/BootKit/BootKit.hxx | 81 | ||||
| -rw-r--r-- | Private/NewBoot/BootKit/Vendor/Support.hxx | 6 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/BootATA.cxx | 22 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/BootMain.cxx | 28 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx | 2 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx | 2 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/Support.cxx | 16 | ||||
| -rw-r--r-- | Private/NewBoot/Source/compile_flags.txt | 1 | ||||
| -rw-r--r-- | Private/NewBoot/Source/makefile | 4 |
9 files changed, 94 insertions, 68 deletions
diff --git a/Private/NewBoot/BootKit/BootKit.hxx b/Private/NewBoot/BootKit/BootKit.hxx index e2e371dd..c3e438ae 100644 --- a/Private/NewBoot/BootKit/BootKit.hxx +++ b/Private/NewBoot/BootKit/BootKit.hxx @@ -38,9 +38,6 @@ class BFileReader; class BFileRunner; class BVersionString; -///! @note This address is reserved to NewKernel. -#define kBootVirtualAddress (0xfffffff80000000) - using namespace NewOS; typedef Char *PEFImagePtr; @@ -188,11 +185,15 @@ public: Char fFileName[kNewFSNodeNameLen]; Char fForkName[kNewFSNodeNameLen]; - UInt32 fKind; + Int32 fKind; + Int64 fLba; VoidPtr fBlob; SizeT fBlobSz; + bool IsCatalogValid() { return fLba != 0 && fLba >= kNewFSCatalogStartAddress; } + + struct BFileDescriptor* fParent; struct BFileDescriptor* fPrev; struct BFileDescriptor* fNext; }; @@ -214,7 +215,7 @@ public: Boolean Format(const char* partName, BFileDescriptor* fileBlobs, SizeT blobCount); /// @brief check if partition is good. - operator bool() noexcept { + Bool IsPartitionValid() noexcept { fDiskDev.Leak().mBase = (kNewFSAddressAsLba); fDiskDev.Leak().mSize = BootDev::kSectorSize; @@ -224,12 +225,13 @@ public: NewPartitionBlock* blockPart = reinterpret_cast<NewPartitionBlock*>(buf); + BTextWriter writer; + for (SizeT indexMag = 0UL; indexMag < kNewFSIdentLen; ++indexMag) { if (blockPart->Ident[indexMag] != kNewFSIdent[indexMag]) return false; } - BTextWriter writer; writer.Write(L"Device Size: ").Write(this->fDiskDev.GetDiskSize()).Write(L"\r\n"); if (blockPart->DiskSize != this->fDiskDev.GetDiskSize() || @@ -247,32 +249,46 @@ public: private: /// @brief Write all of the requested catalogs into the filesystem. - Boolean WriteContent(BFileDescriptor* fileBlobs, SizeT blobCount, - SizeT sectorSz, NewPartitionBlock& partBlock) { - if (sectorSz != BootDev::kSectorSize) return false; + /// @param fileBlobs the blobs. + /// @param blobCount the number of blobs to write. + /// @param partBlock the NewFS partition block. + Boolean FormatCatalog(BFileDescriptor* fileBlobs, SizeT blobCount, + NewPartitionBlock& partBlock) { + if (partBlock.SectorSize != BootDev::kSectorSize) return false; BFileDescriptor* blob = fileBlobs; Lba startLba = partBlock.StartCatalog; BTextWriter writer; - SizeT blobCounter = 0UL; + Char bufCatalog[sizeof(NewCatalog)] = { 0 }; + Char bufFork[sizeof(NewFork)] = { 0 }; while (blob) { - if (blobCounter > blobCount) break; - ++blobCounter; - - NewCatalog* catalogKind = new NewCatalog(); - memset(catalogKind, 0, sizeof(NewCatalog)); + NewCatalog* catalogKind = (NewCatalog*)bufCatalog; + + blob->fLba = startLba; + + if (!blob->fParent) + catalogKind->PrevSibling = startLba; + else { + if (blob->IsCatalogValid()) { + catalogKind->PrevSibling = blob->fParent->fLba; + } else { + EFI::ThrowError(L"Invalid-Catalog-Location", L"Invalid catalog location."); + } + } /// Fill catalog kind. catalogKind->Kind = blob->fKind; /// Allocate fork for blob. - catalogKind->FirstFork = (startLba + sizeof(NewCatalog)); - catalogKind->LastFork = catalogKind->FirstFork; + if (catalogKind->Kind == kNewFSDataForkKind) { + catalogKind->DataFork = (startLba + sizeof(NewCatalog)); + } else { + catalogKind->ResourceFork = (startLba + sizeof(NewCatalog)); + } - NewFork* forkKind = new NewFork(); - memset(forkKind, 0, sizeof(NewFork)); + NewFork* forkKind = (NewFork*)bufFork; memcpy(forkKind->Name, blob->fForkName, strlen(blob->fForkName)); forkKind->Kind = (forkKind->Name[0] == kNewFSDataFork[0]) ? kNewFSDataForkKind : kNewFSRsrcForkKind; @@ -284,8 +300,8 @@ private: forkKind->ResourceKind = 0; /// We're the only fork here. - forkKind->NextSibling = catalogKind->FirstFork; - forkKind->PreviousSibling = catalogKind->FirstFork; + forkKind->NextSibling = forkKind->Kind == kNewFSDataForkKind ? catalogKind->DataFork : catalogKind->ResourceFork; + forkKind->PreviousSibling = kNewFSDataForkKind ? catalogKind->DataFork : catalogKind->ResourceFork; forkKind->DataOffset = (startLba + sizeof(NewCatalog) + sizeof(NewFork)); forkKind->DataSize = blob->fBlobSz; @@ -300,7 +316,7 @@ private: fDiskDev.Leak().mBase = startLba + sizeof(NewCatalog); fDiskDev.Leak().mSize = sizeof(NewFork); - fDiskDev.Write((Char*)forkKind, sizeof(NewFork)); + fDiskDev.Write((Char*)bufFork, sizeof(NewFork)); do { this->fDiskDev.Leak().mSize = BootDev::kSectorSize; @@ -316,13 +332,8 @@ private: 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)); @@ -332,18 +343,20 @@ private: memcpy(catalogKind->Name, blob->fFileName, strlen(blob->fFileName)); + catalogKind->NextSibling = startLba + (sizeof(NewCatalog) + sizeof(NewFork) + blob->fBlobSz); + fDiskDev.Leak().mBase = startLba; fDiskDev.Leak().mSize = sizeof(NewCatalog); - fDiskDev.Write((Char*)catalogKind, sizeof(NewCatalog)); + fDiskDev.Write((Char*)bufCatalog, sizeof(NewCatalog)); startLba += (sizeof(NewCatalog) + sizeof(NewFork) + blob->fBlobSz); --partBlock.FreeCatalog; --partBlock.FreeSectors; - delete forkKind; - delete catalogKind; + memset(bufFork, 0, sizeof(NewFork)); + memset(bufCatalog, 0, sizeof(NewCatalog)); blob = blob->fNext; } @@ -381,14 +394,14 @@ inline Boolean BDiskFormatFactory<BootDev>::Format(const char* partName, partBlock->CatalogCount = blobCount; partBlock->Kind = kNewFSHardDrive; partBlock->SectorSize = sectorSz; - partBlock->FreeCatalog = fDiskDev.GetSectorsCount() - partBlock->CatalogCount; + partBlock->FreeCatalog = fDiskDev.GetSectorsCount(); partBlock->SectorCount = fDiskDev.GetSectorsCount(); - partBlock->FreeSectors = fDiskDev.GetSectorsCount() - partBlock->CatalogCount; + partBlock->FreeSectors = fDiskDev.GetSectorsCount(); partBlock->StartCatalog = kNewFSCatalogStartAddress; partBlock->DiskSize = fDiskDev.GetDiskSize(); - if (this->WriteContent(fileBlobs, blobCount, sectorSz, *partBlock)) { - fDiskDev.Leak().mBase = (kNewFSAddressAsLba); + if (this->FormatCatalog(fileBlobs, blobCount, *partBlock)) { + fDiskDev.Leak().mBase = kNewFSAddressAsLba; fDiskDev.Leak().mSize = sectorSz; fDiskDev.Write(buf, sectorSz); diff --git a/Private/NewBoot/BootKit/Vendor/Support.hxx b/Private/NewBoot/BootKit/Vendor/Support.hxx index 4d35b4cb..b4ba4f68 100644 --- a/Private/NewBoot/BootKit/Vendor/Support.hxx +++ b/Private/NewBoot/BootKit/Vendor/Support.hxx @@ -13,9 +13,9 @@ #define LONG_MAX ((long)(~0UL>>1)) #define LONG_MIN (~LONG_MAX) -#define SetMem(dst, c, sz) BSetMem((CharacterTypeUTF16 *)dst, c, sz) -#define MoveMem(dst, src, sz) BCopyMem((CharacterTypeUTF16 *)dst, (CharacterTypeUTF16 *)src, sz) -#define CopyMem(dst, src, sz) BCopyMem((CharacterTypeUTF16 *)dst, (CharacterTypeUTF16 *)src, sz) +#define SetMem(dst, c, sz) memset(dst, c, sz) +#define MoveMem(dst, src, sz) memcpy(dst, src, sz) +#define CopyMem(dst, src, sz) memcpy(dst,src, sz) inline int isspace(int c) { return c == ' '; } diff --git a/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx b/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx index 8ab7dc20..d6b5542c 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx @@ -88,7 +88,7 @@ ATAInit_Retry: boot_ata_wait_io(IO); for (SizeT indexData = 0ul; indexData < kATADataLen; ++indexData) { - kATAData[indexData] = In8(IO + ATA_REG_DATA); + kATAData[indexData] = In16(IO + ATA_REG_DATA); } OutBus = @@ -107,18 +107,22 @@ Void boot_ata_read(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, boot_ata_select(IO); Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); - Out8(IO + ATA_REG_SEC_COUNT0, 1); + + Out8(IO + ATA_REG_SEC_COUNT0, 2); 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_LBA3, (Lba) >> 24); Out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO); + boot_ata_wait_io(IO); + for (SizeT IndexOff = 0; IndexOff < Size; ++IndexOff) { boot_ata_wait_io(IO); Buf[IndexOff] = In16(IO + ATA_REG_DATA); + boot_ata_wait_io(IO); } } @@ -130,18 +134,22 @@ Void boot_ata_write(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, boot_ata_select(IO); Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); - Out8(IO + ATA_REG_SEC_COUNT0, 1); + + Out8(IO + ATA_REG_SEC_COUNT0, 2); 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_LBA3, (Lba) >> 24); Out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO); + boot_ata_wait_io(IO); + for (SizeT IndexOff = 0; IndexOff < Size; ++IndexOff) { boot_ata_wait_io(IO); Out16(IO + ATA_REG_DATA, Buf[IndexOff]); + boot_ata_wait_io(IO); } } @@ -188,7 +196,7 @@ BootDeviceATA& BootDeviceATA::Read(CharacterTypeUTF8* Buf, const SizeT& SectorSz if (!Buf || SectorSz < 1) return *this; - auto lba = this->Leak().mBase / BootDeviceATA::kSectorSize; + auto lba = this->Leak().mBase / SectorSz; boot_ata_read(lba, this->Leak().mBus, this->Leak().mMaster, Buf, SectorSz, this->Leak().mSize); @@ -211,7 +219,7 @@ BootDeviceATA& BootDeviceATA::Write(CharacterTypeUTF8* Buf, const SizeT& SectorS if (!Buf || SectorSz < 1) return *this; - auto lba = this->Leak().mBase / BootDeviceATA::kSectorSize; + auto lba = this->Leak().mBase / SectorSz; boot_ata_write(lba, this->Leak().mBus, this->Leak().mMaster, Buf, SectorSz, this->Leak().mSize); diff --git a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx index 558bf001..2939c182 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx @@ -12,6 +12,7 @@ #include <NewKit/Macros.hpp> #include <BootKit/BootKit.hxx> #include <NewKit/Ref.hpp> +#include <FirmwareKit/Handover.hxx> #include <cstring> /// make the compiler shut up. @@ -21,13 +22,11 @@ /** Graphics related. */ -EXTERN_C Void hal_init_platform(HEL::HandoverInformationHeader* HIH); - STATIC EfiGraphicsOutputProtocol* kGop = nullptr; STATIC UInt16 kStride = 0U; STATIC EfiGUID kGopGuid; -EXTERN_C Void rt_jump_to_address(VoidPtr blob); +EXTERN_C Void hal_init_platform(HEL::HandoverInformationHeader* HIH); /** @brief Finds and stores the GOP. @@ -66,7 +65,7 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, writer.Write(L"Mahrouss-Logic (R) New Boot: ") .Write(BVersionString::Shared()); - writer.Write(L"\r\nNewBoot: Firmware Vendor: ") + writer.Write(L"\r\nNew Boot: Firmware Vendor: ") .Write(SystemTable->FirmwareVendor) .Write(L"\r\n"); @@ -76,6 +75,7 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, UInt32* SzDesc = new UInt32(); UInt32* RevDesc = new UInt32(); + *MapKey = 0; *SizePtr = sizeof(EfiMemoryDescriptor); HEL::HandoverInformationHeader* handoverHdrPtr = new HEL::HandoverInformationHeader(); @@ -125,19 +125,12 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, ToolboxClearRsrc(); - EfiPhysicalAddress* whereAddress = - reinterpret_cast<EfiPhysicalAddress*>(kBootVirtualAddress); - BS->GetMemoryMap(SizePtr, Descriptor, MapKey, SzDesc, RevDesc); handoverHdrPtr->f_PhysicalStart = (VoidPtr)Descriptor->PhysicalStart; - handoverHdrPtr->f_FirmwareSpecific[0] = Descriptor->Attribute; - handoverHdrPtr->f_FirmwareSpecific[1] = Descriptor->Kind; - - - BS->AllocatePages(EfiAllocateType::AllocateAnyPages, - EfiMemoryType::EfiConventionalMemory, 1, whereAddress); + handoverHdrPtr->f_FirmwareSpecific[HEL::kHandoverSpecificAttrib] = Descriptor->Attribute; + handoverHdrPtr->f_FirmwareSpecific[HEL::kHandoverSpecificKind] = Descriptor->Kind; handoverHdrPtr->f_VirtualStart = (VoidPtr)Descriptor->VirtualStart; handoverHdrPtr->f_VirtualSize = Descriptor->NumberOfPages; /* # of pages */ @@ -170,7 +163,9 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, BDiskFormatFactory<BootDeviceATA> diskFormatter; - if (!diskFormatter) { + /// if not formated yet, then format it with the following folders: + /// /, /Boot, /Applications. + if (!diskFormatter.IsPartitionValid()) { BDiskFormatFactory<BootDeviceATA>::BFileDescriptor rootDesc{0}; memcpy(rootDesc.fFileName, "/", strlen("/")); @@ -178,6 +173,7 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, rootDesc.fBlobSz = BootDeviceATA::kSectorSize; rootDesc.fBlob = new Char[rootDesc.fBlobSz]; + rootDesc.fParent = &rootDesc; memset(rootDesc.fBlob, 0, rootDesc.fBlobSz); @@ -195,6 +191,7 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, bootDesc.fBlobSz = BootDeviceATA::kSectorSize; bootDesc.fBlob = new Char[bootDesc.fBlobSz]; + bootDesc.fParent = &rootDesc; memset(bootDesc.fBlob, 0, bootDesc.fBlobSz); @@ -213,6 +210,7 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, appDesc.fBlobSz = BootDeviceATA::kSectorSize; appDesc.fBlob = new Char[appDesc.fBlobSz]; + appDesc.fParent = &rootDesc; memset(appDesc.fBlob, 0, appDesc.fBlobSz); @@ -229,7 +227,7 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, EFI::ExitBootServices(*MapKey, ImageHandle); - hal_init_platform(kHandoverHeader); + hal_init_platform(handoverHdrPtr); EFI::Stop(); diff --git a/Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx b/Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx index 2ac90dd8..fa735142 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx @@ -9,7 +9,7 @@ #include <BootKit/BootKit.hxx> #include "HALKit/AMD64/Processor.hpp" -#if 0 +#ifdef __STANDALONE__ EXTERN_C void rt_hlt() { asm volatile("hlt"); } diff --git a/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx b/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx index 909ccca6..8d811bd9 100644 --- a/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx @@ -9,7 +9,7 @@ #include <BootKit/BootKit.hxx> #include <cstddef> /* Since we're using GCC for this EFI program. */ -#if 0 +#ifdef __STANDALONE__ /// @brief Allocates a new object. /// @param sz the size. diff --git a/Private/NewBoot/Source/HEL/AMD64/Support.cxx b/Private/NewBoot/Source/HEL/AMD64/Support.cxx index a8e2c275..3a6974bb 100644 --- a/Private/NewBoot/Source/HEL/AMD64/Support.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/Support.cxx @@ -8,7 +8,7 @@ #include <FirmwareKit/Handover.hxx> #include <BootKit/Vendor/Support.hxx> -#if 0 +#ifdef __STANDALONE__ /// @brief memset definition in C++. /// @param dst destination pointer. @@ -16,8 +16,11 @@ /// @param len length of of src. EXTERN_C VoidPtr memset(void *dst, int byte, long long unsigned int len) { - SetMem(dst, byte, len); - return dst; + for (size_t i = 0UL; i < len; ++i) { + ((int*)dst)[i] = byte; + } + + return dst; } /// @brief memcpy definition in C++. @@ -26,8 +29,11 @@ EXTERN_C VoidPtr memset(void *dst, int byte, /// @param len length of of src. EXTERN_C VoidPtr memcpy(void *dst, const void *src, long long unsigned int len) { - CopyMem(dst, src, len); - return dst; + for (size_t i = 0UL; i < len; ++i){ + ((int*)dst)[i] = ((int*)src)[i]; + } + + return dst; } /// @brief strlen definition in C++. diff --git a/Private/NewBoot/Source/compile_flags.txt b/Private/NewBoot/Source/compile_flags.txt index e58d7ab9..c74d22b2 100644 --- a/Private/NewBoot/Source/compile_flags.txt +++ b/Private/NewBoot/Source/compile_flags.txt @@ -1,3 +1,4 @@ -std=c++20 -I../ -I../../ +-D__NEWOS_AMD64__ diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index f3a292c5..9f70d903 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -75,8 +75,8 @@ run-efi-amd64: .PHONY: epm-img epm-img: - qemu-img create -f qcow2 $(IMG) 512M - qemu-img create -f qcow2 $(IMG_2) 512M + qemu-img create -f raw $(IMG) 512M + qemu-img create -f raw $(IMG_2) 512M .PHONY: download-edk download-edk: |
