From d48cbe75ef29a9c67c9d176bf58e56ea6448fb9e Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 21 Oct 2024 20:23:36 +0200 Subject: IMP: Major refactor of header and source files extensions. Signed-off-by: Amlal El Mahrouss --- dev/zba/BootKit/BitManip.h | 20 ++ dev/zba/BootKit/BitManip.hxx | 20 -- dev/zba/BootKit/BootKit.h | 385 +++++++++++++++++++++++++++++++++ dev/zba/BootKit/BootKit.hxx | 385 --------------------------------- dev/zba/BootKit/Device.h | 36 +++ dev/zba/BootKit/Device.hxx | 36 --- dev/zba/BootKit/EPM.h | 9 + dev/zba/BootKit/EPM.hxx | 9 - dev/zba/BootKit/HW/ATA.h | 58 +++++ dev/zba/BootKit/HW/ATA.hxx | 58 ----- dev/zba/BootKit/HW/SATA.h | 46 ++++ dev/zba/BootKit/HW/SATA.hxx | 46 ---- dev/zba/BootKit/Platform.h | 32 +++ dev/zba/BootKit/Platform.hxx | 32 --- dev/zba/BootKit/Protocol.h | 10 + dev/zba/BootKit/Protocol.hxx | 10 - dev/zba/BootKit/STB.h | 27 +++ dev/zba/BootKit/STB.hxx | 27 --- dev/zba/BootKit/Support.h | 165 ++++++++++++++ dev/zba/BootKit/Support.hxx | 165 -------------- dev/zba/BootKit/Thread.h | 42 ++++ dev/zba/BootKit/Thread.hxx | 42 ---- dev/zba/Modules/NetBoot/Module.cc | 16 ++ dev/zba/Modules/NetBoot/Module.cxx | 16 -- dev/zba/Modules/NetBoot/NetBoot.h | 26 +++ dev/zba/Modules/NetBoot/NetBoot.hxx | 26 --- dev/zba/Modules/NetBoot/build.json | 2 +- dev/zba/Modules/SysChk/Module.cc | 22 ++ dev/zba/Modules/SysChk/Module.cxx | 22 -- dev/zba/Modules/SysChk/build.json | 2 +- dev/zba/amd64-efi.make | 4 +- dev/zba/arm64-efi.make | 4 +- dev/zba/src/BootFileReader.cc | 203 +++++++++++++++++ dev/zba/src/BootFileReader.cxx | 203 ----------------- dev/zba/src/BootString.cc | 92 ++++++++ dev/zba/src/BootString.cxx | 92 -------- dev/zba/src/BootSupport.cc | 82 +++++++ dev/zba/src/BootSupport.cxx | 82 ------- dev/zba/src/BootTextWriter.cc | 169 +++++++++++++++ dev/zba/src/BootTextWriter.cxx | 169 --------------- dev/zba/src/BootloaderRsrc.rsrc | 2 +- dev/zba/src/HEL/AMD64/BootAHCI.cc | 20 ++ dev/zba/src/HEL/AMD64/BootAHCI.cxx | 20 -- dev/zba/src/HEL/AMD64/BootATA.cc | 278 ++++++++++++++++++++++++ dev/zba/src/HEL/AMD64/BootATA.cxx | 278 ------------------------ dev/zba/src/HEL/AMD64/BootMain.cc | 324 +++++++++++++++++++++++++++ dev/zba/src/HEL/AMD64/BootMain.cxx | 324 --------------------------- dev/zba/src/HEL/AMD64/BootPlatform.cc | 106 +++++++++ dev/zba/src/HEL/AMD64/BootPlatform.cxx | 106 --------- dev/zba/src/HEL/ARM64/BootMain.cc | 76 +++++++ dev/zba/src/HEL/ARM64/BootMain.cxx | 76 ------- dev/zba/src/HEL/ARM64/BootPlatform.cc | 37 ++++ dev/zba/src/HEL/ARM64/BootPlatform.cxx | 37 ---- dev/zba/src/New+Delete.cc | 60 +++++ dev/zba/src/New+Delete.cxx | 60 ----- dev/zba/src/Thread.cc | 195 +++++++++++++++++ dev/zba/src/Thread.cxx | 195 ----------------- 57 files changed, 2543 insertions(+), 2543 deletions(-) create mode 100644 dev/zba/BootKit/BitManip.h delete mode 100644 dev/zba/BootKit/BitManip.hxx create mode 100644 dev/zba/BootKit/BootKit.h delete mode 100644 dev/zba/BootKit/BootKit.hxx create mode 100644 dev/zba/BootKit/Device.h delete mode 100644 dev/zba/BootKit/Device.hxx create mode 100644 dev/zba/BootKit/EPM.h delete mode 100644 dev/zba/BootKit/EPM.hxx create mode 100644 dev/zba/BootKit/HW/ATA.h delete mode 100644 dev/zba/BootKit/HW/ATA.hxx create mode 100644 dev/zba/BootKit/HW/SATA.h delete mode 100644 dev/zba/BootKit/HW/SATA.hxx create mode 100644 dev/zba/BootKit/Platform.h delete mode 100644 dev/zba/BootKit/Platform.hxx create mode 100644 dev/zba/BootKit/Protocol.h delete mode 100644 dev/zba/BootKit/Protocol.hxx create mode 100644 dev/zba/BootKit/STB.h delete mode 100644 dev/zba/BootKit/STB.hxx create mode 100644 dev/zba/BootKit/Support.h delete mode 100644 dev/zba/BootKit/Support.hxx create mode 100644 dev/zba/BootKit/Thread.h delete mode 100644 dev/zba/BootKit/Thread.hxx create mode 100644 dev/zba/Modules/NetBoot/Module.cc delete mode 100644 dev/zba/Modules/NetBoot/Module.cxx create mode 100644 dev/zba/Modules/NetBoot/NetBoot.h delete mode 100644 dev/zba/Modules/NetBoot/NetBoot.hxx create mode 100644 dev/zba/Modules/SysChk/Module.cc delete mode 100644 dev/zba/Modules/SysChk/Module.cxx create mode 100644 dev/zba/src/BootFileReader.cc delete mode 100644 dev/zba/src/BootFileReader.cxx create mode 100644 dev/zba/src/BootString.cc delete mode 100644 dev/zba/src/BootString.cxx create mode 100644 dev/zba/src/BootSupport.cc delete mode 100644 dev/zba/src/BootSupport.cxx create mode 100644 dev/zba/src/BootTextWriter.cc delete mode 100644 dev/zba/src/BootTextWriter.cxx create mode 100644 dev/zba/src/HEL/AMD64/BootAHCI.cc delete mode 100644 dev/zba/src/HEL/AMD64/BootAHCI.cxx create mode 100644 dev/zba/src/HEL/AMD64/BootATA.cc delete mode 100644 dev/zba/src/HEL/AMD64/BootATA.cxx create mode 100644 dev/zba/src/HEL/AMD64/BootMain.cc delete mode 100644 dev/zba/src/HEL/AMD64/BootMain.cxx create mode 100644 dev/zba/src/HEL/AMD64/BootPlatform.cc delete mode 100644 dev/zba/src/HEL/AMD64/BootPlatform.cxx create mode 100644 dev/zba/src/HEL/ARM64/BootMain.cc delete mode 100644 dev/zba/src/HEL/ARM64/BootMain.cxx create mode 100644 dev/zba/src/HEL/ARM64/BootPlatform.cc delete mode 100644 dev/zba/src/HEL/ARM64/BootPlatform.cxx create mode 100644 dev/zba/src/New+Delete.cc delete mode 100644 dev/zba/src/New+Delete.cxx create mode 100644 dev/zba/src/Thread.cc delete mode 100644 dev/zba/src/Thread.cxx (limited to 'dev/zba') diff --git a/dev/zba/BootKit/BitManip.h b/dev/zba/BootKit/BitManip.h new file mode 100644 index 00000000..ad893b02 --- /dev/null +++ b/dev/zba/BootKit/BitManip.h @@ -0,0 +1,20 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#ifndef __BITMANIP_H__ +#define __BITMANIP_H__ + +/// Name: Bits API. +/// Purpose: Bit manip helpers, based on CoreBoot header. + +#define bk_set_bit(X, O) X = (1 << O) | X +#define bk_clear_bit(X, O) X = ~(1 << O) & X +#define bk_toogle(X, O) X = (1 << O) ^ X +#define bk_lsb(X) X = X & -X +#define bk_msb(X) X = -(mp_lsb(X)) & X +#define bk_look_for_bit(X, O) (1 << O) | X + +#endif // ifndef __BITMANIP_H__ diff --git a/dev/zba/BootKit/BitManip.hxx b/dev/zba/BootKit/BitManip.hxx deleted file mode 100644 index ad893b02..00000000 --- a/dev/zba/BootKit/BitManip.hxx +++ /dev/null @@ -1,20 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#ifndef __BITMANIP_H__ -#define __BITMANIP_H__ - -/// Name: Bits API. -/// Purpose: Bit manip helpers, based on CoreBoot header. - -#define bk_set_bit(X, O) X = (1 << O) | X -#define bk_clear_bit(X, O) X = ~(1 << O) & X -#define bk_toogle(X, O) X = (1 << O) ^ X -#define bk_lsb(X) X = X & -X -#define bk_msb(X) X = -(mp_lsb(X)) & X -#define bk_look_for_bit(X, O) (1 << O) | X - -#endif // ifndef __BITMANIP_H__ diff --git a/dev/zba/BootKit/BootKit.h b/dev/zba/BootKit/BootKit.h new file mode 100644 index 00000000..dfba9728 --- /dev/null +++ b/dev/zba/BootKit/BootKit.h @@ -0,0 +1,385 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +/***********************************************************************************/ +/// @file Boot.h +/// @brief Bootloader Programming Interface. +/***********************************************************************************/ + +#pragma once + +#include +#include +#include + +/// include NeFS header and Support header as well. + +#include +#include + +/***********************************************************************************/ +/// Include other APIs. +/***********************************************************************************/ + +#include +#include + +#include + +/***********************************************************************************/ +/// Framebuffer helpers. +/***********************************************************************************/ + +namespace EFI +{ + extern void ThrowError(const WideChar* errorCode, + const WideChar* reason) noexcept; +} // namespace EFI + +namespace Boot +{ + class BTextWriter; + class BFileReader; + class BThread; + class BVersionString; + + typedef Char* PEFImagePtr; + typedef Char* PEImagePtr; + + typedef WideChar CharacterTypeUTF16; + typedef Char CharacterTypeUTF8; + + using namespace Kernel; + + /** + * @brief BootKit Text Writer class + * Writes to UEFI StdOut. + */ + class BTextWriter final + { + BTextWriter& _Write(const Long& num); + + public: + BTextWriter& Write(const Long& num); + BTextWriter& Write(const Char* str); + BTextWriter& Write(const CharacterTypeUTF16* str); + BTextWriter& WriteCharacter(CharacterTypeUTF16 c); + BTextWriter& Write(const UChar* str); + + public: + explicit BTextWriter() = default; + ~BTextWriter() = default; + + public: + BTextWriter& operator=(const BTextWriter&) = default; + BTextWriter(const BTextWriter&) = default; + }; + + Kernel::SizeT BCopyMem(CharacterTypeUTF16* dest, CharacterTypeUTF16* src, const Kernel::SizeT len); + + Kernel::SizeT BSetMem(CharacterTypeUTF8* src, const CharacterTypeUTF8 byte, const Kernel::SizeT len); + + /// String length functions. + + /// @brief get string length. + Kernel::SizeT BStrLen(const CharacterTypeUTF16* ptr); + + /// @brief set memory with custom value. + Kernel::SizeT BSetMem(CharacterTypeUTF16* src, const CharacterTypeUTF16 byte, const Kernel::SizeT len); + + /** + * @brief BootKit File Reader class + * Reads the Firmware Boot partition and filesystem. + */ + class BFileReader final + { + public: + explicit BFileReader(const CharacterTypeUTF16* path, + EfiHandlePtr ImageHandle); + ~BFileReader(); + + public: + Void ReadAll(SizeT until, SizeT chunk = kib_cast(4), UIntPtr out_address = 0UL); + + enum + { + kOperationOkay, + kNotSupported, + kEmptyDirectory, + kNoSuchEntry, + kIsDirectory, + kTooSmall, + kCount, + }; + + /// @brief error code getter. + /// @return the error code. + Int32& Error(); + + /// @brief blob getter. + /// @return the blob. + VoidPtr Blob(); + + /// @breif Size getter. + /// @return the size of the file. + UInt64& Size(); + + public: + BFileReader& operator=(const BFileReader&) = default; + BFileReader(const BFileReader&) = default; + + private: + Int32 mErrorCode{kOperationOkay}; + VoidPtr mBlob{nullptr}; + CharacterTypeUTF16 mPath[kPathLen]; + BTextWriter mWriter; + EfiFileProtocol* mFile{nullptr}; + UInt64 mSizeFile{0}; + EfiFileProtocol* mRootFs; + }; + + typedef UInt8* BlobType; + + class BVersionString final + { + public: + static const CharacterTypeUTF8* The() + { + return BOOTLOADER_VERSION; + } + }; + + /***********************************************************************************/ + /// Provide some useful processor features. + /***********************************************************************************/ + +#ifdef __EFI_x86_64__ + + /*** + * Common processor instructions. + */ + + EXTERN_C void Out8(UInt16 port, UInt8 value); + EXTERN_C void Out16(UInt16 port, UInt16 value); + EXTERN_C void Out32(UInt16 port, UInt32 value); + EXTERN_C UInt8 In8(UInt16 port); + EXTERN_C UInt16 In16(UInt16 port); + EXTERN_C UInt32 In32(UInt16 port); + + EXTERN_C void rt_hlt(); + EXTERN_C void rt_cli(); + EXTERN_C void rt_sti(); + EXTERN_C void rt_cld(); + EXTERN_C void rt_std(); + +#endif // __EFI_x86_64__ + + static inline const UInt32 kRgbRed = 0x000000FF; + static inline const UInt32 kRgbGreen = 0x0000FF00; + 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 Drive Formatter. + template + class BDiskFormatFactory final + { + public: + /// @brief File entry for **BDiskFormatFactory**. + struct BFileDescriptor final + { + Char fFileName[kNeFSNodeNameLen]; + Int32 fKind; + }; + + public: + explicit BDiskFormatFactory() = default; + explicit BDiskFormatFactory(BootDev dev) + : fDiskDev(dev) + { + } + + ~BDiskFormatFactory() = default; + + ZKA_COPY_DELETE(BDiskFormatFactory); + + /// @brief Format disk. + /// @param Partition Name + /// @param Blobs. + /// @param Number of blobs. + /// @retval True disk has been formatted. + /// @retval False failed to format. + Boolean Format(const Char* partName, BFileDescriptor* fileBlobs, SizeT blobCount); + + /// @brief check if partition is good. + Bool IsPartitionValid() noexcept + { + fDiskDev.Leak().mBase = (kNeFSRootCatalogStartAddress); + fDiskDev.Leak().mSize = BootDev::kSectorSize; + + Char buf[BootDev::kSectorSize] = {0}; + + fDiskDev.Read(buf, BootDev::kSectorSize); + + NFS_ROOT_PARTITION_BLOCK* blockPart = reinterpret_cast(buf); + + BTextWriter writer; + + for (SizeT indexMag = 0UL; indexMag < kNeFSIdentLen; ++indexMag) + { + if (blockPart->Ident[indexMag] != kNeFSIdent[indexMag]) + return false; + } + + if (blockPart->DiskSize != this->fDiskDev.GetDiskSize() || + blockPart->DiskSize < 1 || + blockPart->SectorSize != BootDev::kSectorSize || + blockPart->Version != kNeFSVersionInteger || + blockPart->StartCatalog == 0) + { + return false; + } + else if (blockPart->PartitionName[0] == 0) + { + return false; + } + + writer.Write(L"NEWOSLDR: Partition: ").Write(blockPart->PartitionName).Write(L" is healthy.\r"); + + return true; + } + + private: + /// @brief Write all of the requested catalogs into the filesystem. + /// @param fileBlobs the blobs. + /// @param blobCount the number of blobs to write. + /// @param partBlock the NeFS partition block. + Boolean WriteRootCatalog(BFileDescriptor* fileBlobs, SizeT blobCount, NFS_ROOT_PARTITION_BLOCK& partBlock) + { + BFileDescriptor* blob = fileBlobs; + Lba startLba = partBlock.StartCatalog; + BTextWriter writer; + + NFS_CATALOG_STRUCT catalogKind{0}; + + constexpr auto cNeFSCatalogPadding = 4; + + catalogKind.PrevSibling = startLba; + catalogKind.NextSibling = (startLba + sizeof(NFS_CATALOG_STRUCT) * cNeFSCatalogPadding); + + /// Fill catalog kind. + catalogKind.Kind = blob->fKind; + catalogKind.Flags = kNeFSFlagCreated; + + --partBlock.FreeCatalog; + --partBlock.FreeSectors; + + CopyMem(catalogKind.Name, blob->fFileName, StrLen(blob->fFileName)); + + fDiskDev.Leak().mBase = startLba; + fDiskDev.Leak().mSize = sizeof(NFS_CATALOG_STRUCT); + + fDiskDev.Write((Char*)&catalogKind, sizeof(NFS_CATALOG_STRUCT)); + + writer.Write(L"NEWOSLDR: Wrote directory: ").Write(blob->fFileName).Write(L"\r"); + + return true; + } + + private: + BootDev fDiskDev; + }; + + /// @brief Format disk. + /// @param Partition Name + /// @param Blobs. + /// @param Number of blobs. + /// @retval True disk has been formatted. + /// @retval False failed to format. + template + inline Boolean BDiskFormatFactory::Format(const Char* partName, + BDiskFormatFactory::BFileDescriptor* fileBlobs, + SizeT blobCount) + { + if (!fileBlobs || !blobCount) + return false; /// sanity check + + /// convert the sector into something that the disk understands. + SizeT sectorSz = BootDev::kSectorSize; + + /// @note A catalog roughly equal to a sector. + + constexpr auto cMinimumDiskSize = kNeFSMinimumDiskSize; // at minimum. + + /// @note also look at EPM headers, for free part blocks. + + if (fDiskDev.GetDiskSize() < cMinimumDiskSize) + { + EFI::ThrowError(L"Drive-Too-Tiny", L"Can't format a New Filesystem partition here."); + return false; + } + + NFS_ROOT_PARTITION_BLOCK partBlock{0}; + + CopyMem(partBlock.Ident, kNeFSIdent, kNeFSIdentLen - 1); + CopyMem(partBlock.PartitionName, partName, strlen(partName)); + + partBlock.Version = kNeFSVersionInteger; + partBlock.CatalogCount = blobCount; + partBlock.Kind = kNeFSHardDrive; + partBlock.SectorSize = sectorSz; + partBlock.FreeCatalog = fDiskDev.GetSectorsCount() / sizeof(NFS_CATALOG_STRUCT); + partBlock.SectorCount = fDiskDev.GetSectorsCount(); + partBlock.FreeSectors = fDiskDev.GetSectorsCount(); + partBlock.StartCatalog = kNeFSCatalogStartAddress; + partBlock.DiskSize = fDiskDev.GetDiskSize(); + partBlock.Flags = kNeFSPartitionTypeBoot | kNeFSPartitionTypeStandard; + + fDiskDev.Leak().mBase = kNeFSRootCatalogStartAddress; + fDiskDev.Leak().mSize = sectorSz; + + fDiskDev.Write((Char*)&partBlock, sectorSz); + + BOOT_BLOCK_STRUCT epmBoot{0}; + + constexpr auto cFsName = "NeFS"; + constexpr auto cBlockName = "ZKA:"; + + CopyMem(epmBoot.Fs, reinterpret_cast(const_cast(cFsName)), StrLen(cFsName)); + + epmBoot.FsVersion = kNeFSVersionInteger; + epmBoot.LbaStart = kNeFSRootCatalogStartAddress; + epmBoot.SectorSz = partBlock.SectorSize; + epmBoot.Kind = kEPMNewOS; + epmBoot.NumBlocks = partBlock.CatalogCount; + + CopyMem(epmBoot.Name, reinterpret_cast(const_cast(cBlockName)), StrLen(cBlockName)); + CopyMem(epmBoot.Magic, reinterpret_cast(const_cast(kEPMMagic)), StrLen(kEPMMagic)); + + fDiskDev.Leak().mBase = 1; // always always resies at zero block. + fDiskDev.Leak().mSize = BootDev::kSectorSize; + + fDiskDev.Write((Char*)&epmBoot, sectorSz); + + /// if we can write a root catalog, then write the partition block. + if (this->WriteRootCatalog(fileBlobs, blobCount, partBlock)) + { + BTextWriter writer; + writer.Write(L"NEWOSLDR: Drive formatted.\r"); + + return true; + } + else + { + EFI::ThrowError(L"Filesystem-Failure-Part", L"Filesystem couldn't be partitioned."); + } + + return false; + } +} // namespace Boot diff --git a/dev/zba/BootKit/BootKit.hxx b/dev/zba/BootKit/BootKit.hxx deleted file mode 100644 index f9a94c6e..00000000 --- a/dev/zba/BootKit/BootKit.hxx +++ /dev/null @@ -1,385 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -/***********************************************************************************/ -/// @file Boot.hxx -/// @brief Bootloader Programming Interface. -/***********************************************************************************/ - -#pragma once - -#include -#include -#include - -/// include NeFS header and Support header as well. - -#include -#include - -/***********************************************************************************/ -/// Include other APIs. -/***********************************************************************************/ - -#include -#include - -#include - -/***********************************************************************************/ -/// Framebuffer helpers. -/***********************************************************************************/ - -namespace EFI -{ - extern void ThrowError(const WideChar* errorCode, - const WideChar* reason) noexcept; -} // namespace EFI - -namespace Boot -{ - class BTextWriter; - class BFileReader; - class BThread; - class BVersionString; - - typedef Char* PEFImagePtr; - typedef Char* PEImagePtr; - - typedef WideChar CharacterTypeUTF16; - typedef Char CharacterTypeUTF8; - - using namespace Kernel; - - /** - * @brief BootKit Text Writer class - * Writes to UEFI StdOut. - */ - class BTextWriter final - { - BTextWriter& _Write(const Long& num); - - public: - BTextWriter& Write(const Long& num); - BTextWriter& Write(const Char* str); - BTextWriter& Write(const CharacterTypeUTF16* str); - BTextWriter& WriteCharacter(CharacterTypeUTF16 c); - BTextWriter& Write(const UChar* str); - - public: - explicit BTextWriter() = default; - ~BTextWriter() = default; - - public: - BTextWriter& operator=(const BTextWriter&) = default; - BTextWriter(const BTextWriter&) = default; - }; - - Kernel::SizeT BCopyMem(CharacterTypeUTF16* dest, CharacterTypeUTF16* src, const Kernel::SizeT len); - - Kernel::SizeT BSetMem(CharacterTypeUTF8* src, const CharacterTypeUTF8 byte, const Kernel::SizeT len); - - /// String length functions. - - /// @brief get string length. - Kernel::SizeT BStrLen(const CharacterTypeUTF16* ptr); - - /// @brief set memory with custom value. - Kernel::SizeT BSetMem(CharacterTypeUTF16* src, const CharacterTypeUTF16 byte, const Kernel::SizeT len); - - /** - * @brief BootKit File Reader class - * Reads the Firmware Boot partition and filesystem. - */ - class BFileReader final - { - public: - explicit BFileReader(const CharacterTypeUTF16* path, - EfiHandlePtr ImageHandle); - ~BFileReader(); - - public: - Void ReadAll(SizeT until, SizeT chunk = kib_cast(4), UIntPtr out_address = 0UL); - - enum - { - kOperationOkay, - kNotSupported, - kEmptyDirectory, - kNoSuchEntry, - kIsDirectory, - kTooSmall, - kCount, - }; - - /// @brief error code getter. - /// @return the error code. - Int32& Error(); - - /// @brief blob getter. - /// @return the blob. - VoidPtr Blob(); - - /// @breif Size getter. - /// @return the size of the file. - UInt64& Size(); - - public: - BFileReader& operator=(const BFileReader&) = default; - BFileReader(const BFileReader&) = default; - - private: - Int32 mErrorCode{kOperationOkay}; - VoidPtr mBlob{nullptr}; - CharacterTypeUTF16 mPath[kPathLen]; - BTextWriter mWriter; - EfiFileProtocol* mFile{nullptr}; - UInt64 mSizeFile{0}; - EfiFileProtocol* mRootFs; - }; - - typedef UInt8* BlobType; - - class BVersionString final - { - public: - static const CharacterTypeUTF8* The() - { - return BOOTLOADER_VERSION; - } - }; - - /***********************************************************************************/ - /// Provide some useful processor features. - /***********************************************************************************/ - -#ifdef __EFI_x86_64__ - - /*** - * Common processor instructions. - */ - - EXTERN_C void Out8(UInt16 port, UInt8 value); - EXTERN_C void Out16(UInt16 port, UInt16 value); - EXTERN_C void Out32(UInt16 port, UInt32 value); - EXTERN_C UInt8 In8(UInt16 port); - EXTERN_C UInt16 In16(UInt16 port); - EXTERN_C UInt32 In32(UInt16 port); - - EXTERN_C void rt_hlt(); - EXTERN_C void rt_cli(); - EXTERN_C void rt_sti(); - EXTERN_C void rt_cld(); - EXTERN_C void rt_std(); - -#endif // __EFI_x86_64__ - - static inline const UInt32 kRgbRed = 0x000000FF; - static inline const UInt32 kRgbGreen = 0x0000FF00; - 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 Drive Formatter. - template - class BDiskFormatFactory final - { - public: - /// @brief File entry for **BDiskFormatFactory**. - struct BFileDescriptor final - { - Char fFileName[kNeFSNodeNameLen]; - Int32 fKind; - }; - - public: - explicit BDiskFormatFactory() = default; - explicit BDiskFormatFactory(BootDev dev) - : fDiskDev(dev) - { - } - - ~BDiskFormatFactory() = default; - - ZKA_COPY_DELETE(BDiskFormatFactory); - - /// @brief Format disk. - /// @param Partition Name - /// @param Blobs. - /// @param Number of blobs. - /// @retval True disk has been formatted. - /// @retval False failed to format. - Boolean Format(const Char* partName, BFileDescriptor* fileBlobs, SizeT blobCount); - - /// @brief check if partition is good. - Bool IsPartitionValid() noexcept - { - fDiskDev.Leak().mBase = (kNeFSRootCatalogStartAddress); - fDiskDev.Leak().mSize = BootDev::kSectorSize; - - Char buf[BootDev::kSectorSize] = {0}; - - fDiskDev.Read(buf, BootDev::kSectorSize); - - NFS_ROOT_PARTITION_BLOCK* blockPart = reinterpret_cast(buf); - - BTextWriter writer; - - for (SizeT indexMag = 0UL; indexMag < kNeFSIdentLen; ++indexMag) - { - if (blockPart->Ident[indexMag] != kNeFSIdent[indexMag]) - return false; - } - - if (blockPart->DiskSize != this->fDiskDev.GetDiskSize() || - blockPart->DiskSize < 1 || - blockPart->SectorSize != BootDev::kSectorSize || - blockPart->Version != kNeFSVersionInteger || - blockPart->StartCatalog == 0) - { - return false; - } - else if (blockPart->PartitionName[0] == 0) - { - return false; - } - - writer.Write(L"NEWOSLDR: Partition: ").Write(blockPart->PartitionName).Write(L" is healthy.\r"); - - return true; - } - - private: - /// @brief Write all of the requested catalogs into the filesystem. - /// @param fileBlobs the blobs. - /// @param blobCount the number of blobs to write. - /// @param partBlock the NeFS partition block. - Boolean WriteRootCatalog(BFileDescriptor* fileBlobs, SizeT blobCount, NFS_ROOT_PARTITION_BLOCK& partBlock) - { - BFileDescriptor* blob = fileBlobs; - Lba startLba = partBlock.StartCatalog; - BTextWriter writer; - - NFS_CATALOG_STRUCT catalogKind{0}; - - constexpr auto cNeFSCatalogPadding = 4; - - catalogKind.PrevSibling = startLba; - catalogKind.NextSibling = (startLba + sizeof(NFS_CATALOG_STRUCT) * cNeFSCatalogPadding); - - /// Fill catalog kind. - catalogKind.Kind = blob->fKind; - catalogKind.Flags = kNeFSFlagCreated; - - --partBlock.FreeCatalog; - --partBlock.FreeSectors; - - CopyMem(catalogKind.Name, blob->fFileName, StrLen(blob->fFileName)); - - fDiskDev.Leak().mBase = startLba; - fDiskDev.Leak().mSize = sizeof(NFS_CATALOG_STRUCT); - - fDiskDev.Write((Char*)&catalogKind, sizeof(NFS_CATALOG_STRUCT)); - - writer.Write(L"NEWOSLDR: Wrote directory: ").Write(blob->fFileName).Write(L"\r"); - - return true; - } - - private: - BootDev fDiskDev; - }; - - /// @brief Format disk. - /// @param Partition Name - /// @param Blobs. - /// @param Number of blobs. - /// @retval True disk has been formatted. - /// @retval False failed to format. - template - inline Boolean BDiskFormatFactory::Format(const Char* partName, - BDiskFormatFactory::BFileDescriptor* fileBlobs, - SizeT blobCount) - { - if (!fileBlobs || !blobCount) - return false; /// sanity check - - /// convert the sector into something that the disk understands. - SizeT sectorSz = BootDev::kSectorSize; - - /// @note A catalog roughly equal to a sector. - - constexpr auto cMinimumDiskSize = kNeFSMinimumDiskSize; // at minimum. - - /// @note also look at EPM headers, for free part blocks. - - if (fDiskDev.GetDiskSize() < cMinimumDiskSize) - { - EFI::ThrowError(L"Drive-Too-Tiny", L"Can't format a New Filesystem partition here."); - return false; - } - - NFS_ROOT_PARTITION_BLOCK partBlock{0}; - - CopyMem(partBlock.Ident, kNeFSIdent, kNeFSIdentLen - 1); - CopyMem(partBlock.PartitionName, partName, strlen(partName)); - - partBlock.Version = kNeFSVersionInteger; - partBlock.CatalogCount = blobCount; - partBlock.Kind = kNeFSHardDrive; - partBlock.SectorSize = sectorSz; - partBlock.FreeCatalog = fDiskDev.GetSectorsCount() / sizeof(NFS_CATALOG_STRUCT); - partBlock.SectorCount = fDiskDev.GetSectorsCount(); - partBlock.FreeSectors = fDiskDev.GetSectorsCount(); - partBlock.StartCatalog = kNeFSCatalogStartAddress; - partBlock.DiskSize = fDiskDev.GetDiskSize(); - partBlock.Flags = kNeFSPartitionTypeBoot | kNeFSPartitionTypeStandard; - - fDiskDev.Leak().mBase = kNeFSRootCatalogStartAddress; - fDiskDev.Leak().mSize = sectorSz; - - fDiskDev.Write((Char*)&partBlock, sectorSz); - - BOOT_BLOCK_STRUCT epmBoot{0}; - - constexpr auto cFsName = "NeFS"; - constexpr auto cBlockName = "ZKA:"; - - CopyMem(epmBoot.Fs, reinterpret_cast(const_cast(cFsName)), StrLen(cFsName)); - - epmBoot.FsVersion = kNeFSVersionInteger; - epmBoot.LbaStart = kNeFSRootCatalogStartAddress; - epmBoot.SectorSz = partBlock.SectorSize; - epmBoot.Kind = kEPMNewOS; - epmBoot.NumBlocks = partBlock.CatalogCount; - - CopyMem(epmBoot.Name, reinterpret_cast(const_cast(cBlockName)), StrLen(cBlockName)); - CopyMem(epmBoot.Magic, reinterpret_cast(const_cast(kEPMMagic)), StrLen(kEPMMagic)); - - fDiskDev.Leak().mBase = 1; // always always resies at zero block. - fDiskDev.Leak().mSize = BootDev::kSectorSize; - - fDiskDev.Write((Char*)&epmBoot, sectorSz); - - /// if we can write a root catalog, then write the partition block. - if (this->WriteRootCatalog(fileBlobs, blobCount, partBlock)) - { - BTextWriter writer; - writer.Write(L"NEWOSLDR: Drive formatted.\r"); - - return true; - } - else - { - EFI::ThrowError(L"Filesystem-Failure-Part", L"Filesystem couldn't be partitioned."); - } - - return false; - } -} // namespace Boot diff --git a/dev/zba/BootKit/Device.h b/dev/zba/BootKit/Device.h new file mode 100644 index 00000000..f6352b7b --- /dev/null +++ b/dev/zba/BootKit/Device.h @@ -0,0 +1,36 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include + +using namespace Kernel; + +/// @brief Device type. +class Device +{ +public: + explicit Device() = default; + virtual ~Device() = default; + + ZKA_MOVE_DEFAULT(Device); + + struct Trait + { + SizeT mBase{1024}; + SizeT mSize{1024}; + }; + + virtual Trait& Leak() = 0; + + virtual Device& Read(Char* Buf, const SizeT& SecCount) = 0; + virtual Device& Write(Char* Buf, const SizeT& SecCount) = 0; +}; + +typedef Device BootDevice; +typedef Device NetworkDevice; +typedef Device DiskDevice; diff --git a/dev/zba/BootKit/Device.hxx b/dev/zba/BootKit/Device.hxx deleted file mode 100644 index 7c6d9c4e..00000000 --- a/dev/zba/BootKit/Device.hxx +++ /dev/null @@ -1,36 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include - -using namespace Kernel; - -/// @brief Device type. -class Device -{ -public: - explicit Device() = default; - virtual ~Device() = default; - - ZKA_MOVE_DEFAULT(Device); - - struct Trait - { - SizeT mBase{1024}; - SizeT mSize{1024}; - }; - - virtual Trait& Leak() = 0; - - virtual Device& Read(Char* Buf, const SizeT& SecCount) = 0; - virtual Device& Write(Char* Buf, const SizeT& SecCount) = 0; -}; - -typedef Device BootDevice; -typedef Device NetworkDevice; -typedef Device DiskDevice; diff --git a/dev/zba/BootKit/EPM.h b/dev/zba/BootKit/EPM.h new file mode 100644 index 00000000..df9754f1 --- /dev/null +++ b/dev/zba/BootKit/EPM.h @@ -0,0 +1,9 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include diff --git a/dev/zba/BootKit/EPM.hxx b/dev/zba/BootKit/EPM.hxx deleted file mode 100644 index 0dacba8b..00000000 --- a/dev/zba/BootKit/EPM.hxx +++ /dev/null @@ -1,9 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include diff --git a/dev/zba/BootKit/HW/ATA.h b/dev/zba/BootKit/HW/ATA.h new file mode 100644 index 00000000..0bed397f --- /dev/null +++ b/dev/zba/BootKit/HW/ATA.h @@ -0,0 +1,58 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include +#include + +using namespace Kernel; + +class BootDeviceATA final : public Device +{ +public: + enum + { + kPrimary = ATA_PRIMARY_IO, + kSecondary = ATA_SECONDARY_IO, + }; + + explicit BootDeviceATA() noexcept; + ~BootDeviceATA() = default; + + ZKA_COPY_DELETE(BootDeviceATA); + + enum + { + kSectorSize = kATASectorSize + }; + + struct ATATrait final : public Device::Trait + { + UInt16 mBus{kPrimary}; + UInt8 mMaster{0}; + Boolean mErr{false}; + + operator bool() + { + return !mErr; + } + }; + +public: + operator bool(); + + SizeT GetSectorsCount() noexcept; + SizeT GetDiskSize() noexcept; + + BootDeviceATA& Read(Char* Buf, const SizeT& SecCount) override; + BootDeviceATA& Write(Char* Buf, const SizeT& SecCount) override; + + ATATrait& Leak() override; + +private: + ATATrait mTrait; +}; diff --git a/dev/zba/BootKit/HW/ATA.hxx b/dev/zba/BootKit/HW/ATA.hxx deleted file mode 100644 index 84d03835..00000000 --- a/dev/zba/BootKit/HW/ATA.hxx +++ /dev/null @@ -1,58 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include -#include - -using namespace Kernel; - -class BootDeviceATA final : public Device -{ -public: - enum - { - kPrimary = ATA_PRIMARY_IO, - kSecondary = ATA_SECONDARY_IO, - }; - - explicit BootDeviceATA() noexcept; - ~BootDeviceATA() = default; - - ZKA_COPY_DELETE(BootDeviceATA); - - enum - { - kSectorSize = kATASectorSize - }; - - struct ATATrait final : public Device::Trait - { - UInt16 mBus{kPrimary}; - UInt8 mMaster{0}; - Boolean mErr{false}; - - operator bool() - { - return !mErr; - } - }; - -public: - operator bool(); - - SizeT GetSectorsCount() noexcept; - SizeT GetDiskSize() noexcept; - - BootDeviceATA& Read(Char* Buf, const SizeT& SecCount) override; - BootDeviceATA& Write(Char* Buf, const SizeT& SecCount) override; - - ATATrait& Leak() override; - -private: - ATATrait mTrait; -}; diff --git a/dev/zba/BootKit/HW/SATA.h b/dev/zba/BootKit/HW/SATA.h new file mode 100644 index 00000000..3154e99b --- /dev/null +++ b/dev/zba/BootKit/HW/SATA.h @@ -0,0 +1,46 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include +#include + +class BootDeviceSATA final +{ +public: + explicit BootDeviceSATA() noexcept; + ~BootDeviceSATA() = default; + + ZKA_COPY_DEFAULT(BootDeviceSATA); + + struct SATATrait final + { + Kernel::SizeT mBase{1024}; + Kernel::Boolean mErr{false}; + Kernel::Boolean mDetected{false}; + + operator bool() + { + return !this->mErr; + } + }; + + operator bool() + { + return this->Leak().mDetected; + } + + BootDeviceSATA& Read(Kernel::WideChar* Buf, const Kernel::SizeT& SecCount); + BootDeviceSATA& Write(Kernel::WideChar* Buf, const Kernel::SizeT& SecCount); + + SATATrait& Leak(); + +private: + SATATrait mTrait; +}; + +#define kAHCISectorSz 4096 diff --git a/dev/zba/BootKit/HW/SATA.hxx b/dev/zba/BootKit/HW/SATA.hxx deleted file mode 100644 index d0f7a3b5..00000000 --- a/dev/zba/BootKit/HW/SATA.hxx +++ /dev/null @@ -1,46 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include -#include - -class BootDeviceSATA final -{ -public: - explicit BootDeviceSATA() noexcept; - ~BootDeviceSATA() = default; - - ZKA_COPY_DEFAULT(BootDeviceSATA); - - struct SATATrait final - { - Kernel::SizeT mBase{1024}; - Kernel::Boolean mErr{false}; - Kernel::Boolean mDetected{false}; - - operator bool() - { - return !this->mErr; - } - }; - - operator bool() - { - return this->Leak().mDetected; - } - - BootDeviceSATA& Read(Kernel::WideChar* Buf, const Kernel::SizeT& SecCount); - BootDeviceSATA& Write(Kernel::WideChar* Buf, const Kernel::SizeT& SecCount); - - SATATrait& Leak(); - -private: - SATATrait mTrait; -}; - -#define kAHCISectorSz 4096 diff --git a/dev/zba/BootKit/Platform.h b/dev/zba/BootKit/Platform.h new file mode 100644 index 00000000..e778223b --- /dev/null +++ b/dev/zba/BootKit/Platform.h @@ -0,0 +1,32 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +/** + @file Platform.h + @brief Platform specific code. +*/ + +#ifdef __x86_64__ + +#ifdef __cplusplus +#ifndef EXTERN_C +#define EXTERN_C extern "C" +#endif +#else +#ifndef EXTERN_C +#define EXTERN_C extern +#endif +#endif // __cplusplus + +EXTERN_C void rt_halt(); +EXTERN_C void rt_cli(); +EXTERN_C void rt_sti(); +EXTERN_C void rt_cld(); +EXTERN_C void rt_std(); + +#endif /* ifdef __x86_64__ */ diff --git a/dev/zba/BootKit/Platform.hxx b/dev/zba/BootKit/Platform.hxx deleted file mode 100644 index 0ed6d047..00000000 --- a/dev/zba/BootKit/Platform.hxx +++ /dev/null @@ -1,32 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -/** - @file Platform.hxx - @brief Platform specific code. -*/ - -#ifdef __x86_64__ - -#ifdef __cplusplus -#ifndef EXTERN_C -#define EXTERN_C extern "C" -#endif -#else -#ifndef EXTERN_C -#define EXTERN_C extern -#endif -#endif // __cplusplus - -EXTERN_C void rt_halt(); -EXTERN_C void rt_cli(); -EXTERN_C void rt_sti(); -EXTERN_C void rt_cld(); -EXTERN_C void rt_std(); - -#endif /* ifdef __x86_64__ */ diff --git a/dev/zba/BootKit/Protocol.h b/dev/zba/BootKit/Protocol.h new file mode 100644 index 00000000..82ef1140 --- /dev/null +++ b/dev/zba/BootKit/Protocol.h @@ -0,0 +1,10 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include +#include diff --git a/dev/zba/BootKit/Protocol.hxx b/dev/zba/BootKit/Protocol.hxx deleted file mode 100644 index 537fea94..00000000 --- a/dev/zba/BootKit/Protocol.hxx +++ /dev/null @@ -1,10 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include -#include diff --git a/dev/zba/BootKit/STB.h b/dev/zba/BootKit/STB.h new file mode 100644 index 00000000..7b8d6f3d --- /dev/null +++ b/dev/zba/BootKit/STB.h @@ -0,0 +1,27 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#define STBI_NO_STDIO 1 +#define STBI_NO_SIMD 1 +#define STBI_NO_THREAD_LOCALS 1 +#define STBI_NO_LINEAR 1 + +#define STBI_ONLY_PNG 1 +#define STBI_ONLY_JPEG 1 +#define STBI_ONLY_BMP 1 +#define STBI_ONLY_GIF 1 + +#define STBI_ASSERT(x) MUST_PASS(x) +#define STBI_MALLOC(x) Kernel::mm_new_heap(x, true, true) +#define STBI_REALLOC(p, x) Kernel::mm_realloc_heap(p, x); +#define STBI_FREE(x) Kernel::mm_delete_heap(x) +#define STB_IMAGE_IMPLEMENTATION 1 + +#include + +#include diff --git a/dev/zba/BootKit/STB.hxx b/dev/zba/BootKit/STB.hxx deleted file mode 100644 index 1f47fdfb..00000000 --- a/dev/zba/BootKit/STB.hxx +++ /dev/null @@ -1,27 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#define STBI_NO_STDIO 1 -#define STBI_NO_SIMD 1 -#define STBI_NO_THREAD_LOCALS 1 -#define STBI_NO_LINEAR 1 - -#define STBI_ONLY_PNG 1 -#define STBI_ONLY_JPEG 1 -#define STBI_ONLY_BMP 1 -#define STBI_ONLY_GIF 1 - -#define STBI_ASSERT(x) MUST_PASS(x) -#define STBI_MALLOC(x) Kernel::mm_new_heap(x, true, true) -#define STBI_REALLOC(p, x) Kernel::mm_realloc_heap(p, x); -#define STBI_FREE(x) Kernel::mm_delete_heap(x) -#define STB_IMAGE_IMPLEMENTATION 1 - -#include - -#include diff --git a/dev/zba/BootKit/Support.h b/dev/zba/BootKit/Support.h new file mode 100644 index 00000000..99fe0cb0 --- /dev/null +++ b/dev/zba/BootKit/Support.h @@ -0,0 +1,165 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +/// @file Support.h +/// @brief Purpose of this file is to help port libs into the bootloader. + +#define cLongMax ((long)(~0UL >> 1)) +#define cLongMin (~cLongMax) + +#ifdef __NEWOSLDR__ + +/// @brief memset definition in C++. +/// @param dst destination pointer. +/// @param byte value to fill in. +/// @param len length of of src. +EXTERN_C void* memset(void* dst, int byte, long long unsigned int len); + +/// @brief memcpy definition in C++. +/// @param dst destination pointer. +/// @param src source pointer. +/// @param len length of of src. +EXTERN_C void* memcpy(void* dst, const void* src, long long unsigned int len); + +/// @brief strlen definition in C++. +EXTERN_C size_t strlen(const char* whatToCheck); + +/// @brief strcmp definition in C++. +EXTERN_C int strcmp(const char* whatToCheck, const char* whatToCheckRight); + +#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) +#define StrLen(src) strlen(src) +#define StrCmp(dst, src) strcmp(dst, src) + +#endif // __NEWOSLDR__ + +inline int IsSpace(int c) +{ + return c == ' '; +} + +inline int StringNCompare(const char* destination, const char* source, long length) +{ + long err = 0; + + for (long i = 0UL; i < length; ++i) + { + if (source[i] != destination[i]) + ++err; + } + + return err; +} + +inline long StringToLong(const char* nptr, char** endptr, int base) +{ + const char *p = nptr, *endp; + bool is_neg = 0, overflow = 0; + + /* Need unsigned so (-cLongMin) can fit in these: */ + unsigned long n = 0UL, cutoff; + int cutlim; + + if (base < 0 || base == 1 || base > 36) + { + return 0L; + } + + endp = nptr; + + while (IsSpace(*p)) + p++; + + if (*p == '+') + { + p++; + } + else if (*p == '-') + { + is_neg = 1, p++; + } + if (*p == '0') + { + p++; + /* For strtol(" 0xZ", &endptr, 16), endptr should point to 'x'; + * pointing to ' ' or '0' is non-compliant. + * (Many implementations do this wrong.) */ + endp = p; + if (base == 16 && (*p == 'X' || *p == 'x')) + { + p++; + } + else if (base == 2 && (*p == 'B' || *p == 'b')) + { + /* C23 standard supports "0B" and "0b" prefixes. */ + p++; + } + else if (base == 0) + { + if (*p == 'X' || *p == 'x') + { + base = 16, p++; + } + else if (*p == 'B' || *p == 'b') + { + base = 2, p++; + } + else + { + base = 8; + } + } + } + else if (base == 0) + { + base = 10; + } + + cutoff = (is_neg) ? -(cLongMin / base) : cLongMax / base; + cutlim = (is_neg) ? -(cLongMin % base) : cLongMax % base; + + while (1) + { + int c; + if (*p >= 'A') + c = ((*p - 'A') & (~('a' ^ 'A'))) + 10; + else if (*p <= '9') + c = *p - '0'; + else + break; + if (c < 0 || c >= base) + break; + endp = ++p; + if (overflow) + { + /* endptr should go forward and point to the non-digit character + * (of the given base); required by ANSI standard. */ + if (endptr) + continue; + break; + } + if (n > cutoff || (n == cutoff && c > cutlim)) + { + overflow = 1; + continue; + } + n = n * base + c; + } + + if (endptr) + *endptr = (char*)endp; + + if (overflow) + { + return ((is_neg) ? cLongMin : cLongMax); + } + + return (long)((is_neg) ? -n : n); +} diff --git a/dev/zba/BootKit/Support.hxx b/dev/zba/BootKit/Support.hxx deleted file mode 100644 index 44ba5299..00000000 --- a/dev/zba/BootKit/Support.hxx +++ /dev/null @@ -1,165 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -/// @file Support.hxx -/// @brief Purpose of this file is to help port libs into the bootloader. - -#define cLongMax ((long)(~0UL >> 1)) -#define cLongMin (~cLongMax) - -#ifdef __NEWOSLDR__ - -/// @brief memset definition in C++. -/// @param dst destination pointer. -/// @param byte value to fill in. -/// @param len length of of src. -EXTERN_C void* memset(void* dst, int byte, long long unsigned int len); - -/// @brief memcpy definition in C++. -/// @param dst destination pointer. -/// @param src source pointer. -/// @param len length of of src. -EXTERN_C void* memcpy(void* dst, const void* src, long long unsigned int len); - -/// @brief strlen definition in C++. -EXTERN_C size_t strlen(const char* whatToCheck); - -/// @brief strcmp definition in C++. -EXTERN_C int strcmp(const char* whatToCheck, const char* whatToCheckRight); - -#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) -#define StrLen(src) strlen(src) -#define StrCmp(dst, src) strcmp(dst, src) - -#endif // __NEWOSLDR__ - -inline int IsSpace(int c) -{ - return c == ' '; -} - -inline int StringNCompare(const char* destination, const char* source, long length) -{ - long err = 0; - - for (long i = 0UL; i < length; ++i) - { - if (source[i] != destination[i]) - ++err; - } - - return err; -} - -inline long StringToLong(const char* nptr, char** endptr, int base) -{ - const char *p = nptr, *endp; - bool is_neg = 0, overflow = 0; - - /* Need unsigned so (-cLongMin) can fit in these: */ - unsigned long n = 0UL, cutoff; - int cutlim; - - if (base < 0 || base == 1 || base > 36) - { - return 0L; - } - - endp = nptr; - - while (IsSpace(*p)) - p++; - - if (*p == '+') - { - p++; - } - else if (*p == '-') - { - is_neg = 1, p++; - } - if (*p == '0') - { - p++; - /* For strtol(" 0xZ", &endptr, 16), endptr should point to 'x'; - * pointing to ' ' or '0' is non-compliant. - * (Many implementations do this wrong.) */ - endp = p; - if (base == 16 && (*p == 'X' || *p == 'x')) - { - p++; - } - else if (base == 2 && (*p == 'B' || *p == 'b')) - { - /* C23 standard supports "0B" and "0b" prefixes. */ - p++; - } - else if (base == 0) - { - if (*p == 'X' || *p == 'x') - { - base = 16, p++; - } - else if (*p == 'B' || *p == 'b') - { - base = 2, p++; - } - else - { - base = 8; - } - } - } - else if (base == 0) - { - base = 10; - } - - cutoff = (is_neg) ? -(cLongMin / base) : cLongMax / base; - cutlim = (is_neg) ? -(cLongMin % base) : cLongMax % base; - - while (1) - { - int c; - if (*p >= 'A') - c = ((*p - 'A') & (~('a' ^ 'A'))) + 10; - else if (*p <= '9') - c = *p - '0'; - else - break; - if (c < 0 || c >= base) - break; - endp = ++p; - if (overflow) - { - /* endptr should go forward and point to the non-digit character - * (of the given base); required by ANSI standard. */ - if (endptr) - continue; - break; - } - if (n > cutoff || (n == cutoff && c > cutlim)) - { - overflow = 1; - continue; - } - n = n * base + c; - } - - if (endptr) - *endptr = (char*)endp; - - if (overflow) - { - return ((is_neg) ? cLongMin : cLongMax); - } - - return (long)((is_neg) ? -n : n); -} diff --git a/dev/zba/BootKit/Thread.h b/dev/zba/BootKit/Thread.h new file mode 100644 index 00000000..6923854b --- /dev/null +++ b/dev/zba/BootKit/Thread.h @@ -0,0 +1,42 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include +#include +#include + +namespace Boot +{ + using namespace Kernel; + + class BThread; + + /// @brief Program loader class + /// @package nl.zeta.boot.api + class BThread final + { + public: + explicit BThread() = delete; + ~BThread() = default; + + explicit BThread(Kernel::VoidPtr blob); + + BThread& operator=(const BThread&) = default; + BThread(const BThread&) = default; + + void Start(HEL::HANDOVER_INFO_HEADER* handover); + const char* GetName(); + void SetName(const char* name); + bool IsValid(); + + private: + Char fBlobName[255] = {"BootThread"}; + VoidPtr fStartAddress{nullptr}; + VoidPtr fBlob{nullptr}; + }; +} // namespace Boot diff --git a/dev/zba/BootKit/Thread.hxx b/dev/zba/BootKit/Thread.hxx deleted file mode 100644 index 25a3b544..00000000 --- a/dev/zba/BootKit/Thread.hxx +++ /dev/null @@ -1,42 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include -#include -#include - -namespace Boot -{ - using namespace Kernel; - - class BThread; - - /// @brief Program loader class - /// @package nl.zeta.boot.api - class BThread final - { - public: - explicit BThread() = delete; - ~BThread() = default; - - explicit BThread(Kernel::VoidPtr blob); - - BThread& operator=(const BThread&) = default; - BThread(const BThread&) = default; - - void Start(HEL::HANDOVER_INFO_HEADER* handover); - const char* GetName(); - void SetName(const char* name); - bool IsValid(); - - private: - Char fBlobName[255] = {"BootThread"}; - VoidPtr fStartAddress{nullptr}; - VoidPtr fBlob{nullptr}; - }; -} // namespace Boot diff --git a/dev/zba/Modules/NetBoot/Module.cc b/dev/zba/Modules/NetBoot/Module.cc new file mode 100644 index 00000000..15e63f83 --- /dev/null +++ b/dev/zba/Modules/NetBoot/Module.cc @@ -0,0 +1,16 @@ +/* + * ======================================================== + * + * NetBoot + * Copyright ZKA Web Services Co., all rights reserved. + * + * ======================================================== + */ + +#include +#include + +EXTERN_C Int32 main(Kernel::HEL::HANDOVER_INFO_HEADER* Handover) +{ + return kEfiOk; +} diff --git a/dev/zba/Modules/NetBoot/Module.cxx b/dev/zba/Modules/NetBoot/Module.cxx deleted file mode 100644 index 3b75b3b2..00000000 --- a/dev/zba/Modules/NetBoot/Module.cxx +++ /dev/null @@ -1,16 +0,0 @@ -/* - * ======================================================== - * - * NetBoot - * Copyright ZKA Web Services Co., all rights reserved. - * - * ======================================================== - */ - -#include -#include - -EXTERN_C Int32 main(Kernel::HEL::HANDOVER_INFO_HEADER* Handover) -{ - return kEfiOk; -} diff --git a/dev/zba/Modules/NetBoot/NetBoot.h b/dev/zba/Modules/NetBoot/NetBoot.h new file mode 100644 index 00000000..8645a822 --- /dev/null +++ b/dev/zba/Modules/NetBoot/NetBoot.h @@ -0,0 +1,26 @@ +/* + * ======================================================== + * + * NetBoot + * Copyright ZKA Web Services Co., all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include + +/// @brief the internet header is used to download updates OTA. +typedef struct NetBootInternetHeader +{ + Kernel::Char NB1; /// magic char 1 'N' + Kernel::Char NB2; /// magic char 2 'E' + Kernel::Char NB3; /// magic char 3 'T' + Kernel::Char NB4; /// magic char 4 'B' + + Kernel::Char PatchName[255]; /// example: ColdChoco + Kernel::Int32 PatchLength; /// the patch length. + Kernel::Char PatchTarget[255]; /// the target file. + Kernel::Boolean ImpliesROM; /// does it imply an EEPROM reprogram? +} NetBootInternetHeader; diff --git a/dev/zba/Modules/NetBoot/NetBoot.hxx b/dev/zba/Modules/NetBoot/NetBoot.hxx deleted file mode 100644 index bb0127a2..00000000 --- a/dev/zba/Modules/NetBoot/NetBoot.hxx +++ /dev/null @@ -1,26 +0,0 @@ -/* - * ======================================================== - * - * NetBoot - * Copyright ZKA Web Services Co., all rights reserved. - * - * ======================================================== - */ - -#pragma once - -#include - -/// @brief the internet header is used to download updates OTA. -typedef struct NetBootInternetHeader -{ - Kernel::Char NB1; /// magic char 1 'N' - Kernel::Char NB2; /// magic char 2 'E' - Kernel::Char NB3; /// magic char 3 'T' - Kernel::Char NB4; /// magic char 4 'B' - - Kernel::Char PatchName[255]; /// example: ColdChoco - Kernel::Int32 PatchLength; /// the patch length. - Kernel::Char PatchTarget[255]; /// the target file. - Kernel::Boolean ImpliesROM; /// does it imply an EEPROM reprogram? -} NetBootInternetHeader; diff --git a/dev/zba/Modules/NetBoot/build.json b/dev/zba/Modules/NetBoot/build.json index f03fc06c..dfa39ac5 100644 --- a/dev/zba/Modules/NetBoot/build.json +++ b/dev/zba/Modules/NetBoot/build.json @@ -2,7 +2,7 @@ "compiler_path": "x86_64-w64-mingw32-g++", "compiler_std": "c++20", "headers_path": ["../", "../../", "../../../zka", "../../../", "./"], - "sources_path": ["*.cxx", "*.S"], + "sources_path": [".cc", "*.S"], "output_name": "netboot.sys", "compiler_flags": [ "-ffreestanding", diff --git a/dev/zba/Modules/SysChk/Module.cc b/dev/zba/Modules/SysChk/Module.cc new file mode 100644 index 00000000..74f7c661 --- /dev/null +++ b/dev/zba/Modules/SysChk/Module.cc @@ -0,0 +1,22 @@ +/* + * ======================================================== + * + * NetBoot + * Copyright ZKA Web Services Co., all rights reserved. + * + * ======================================================== + */ + +#include + +EXTERN_C Int32 ModuleMain(Kernel::HEL::HANDOVER_INFO_HEADER* Handover) +{ + EfiSystemTable* cST = (EfiSystemTable*)Handover->f_FirmwareCustomTables[1]; + + cST->ConOut->ClearScreen(cST->ConOut); + + cST->ConOut->OutputString(cST->ConOut, L"SYSCHK: CHECKING FOR VALID NEFS OR HPFS PARTITIONS...\r\n"); + cST->ConOut->OutputString(cST->ConOut, L"SYSCHK: GOOD TO GO!\r\n"); + + return kEfiOk; +} diff --git a/dev/zba/Modules/SysChk/Module.cxx b/dev/zba/Modules/SysChk/Module.cxx deleted file mode 100644 index f8770686..00000000 --- a/dev/zba/Modules/SysChk/Module.cxx +++ /dev/null @@ -1,22 +0,0 @@ -/* - * ======================================================== - * - * NetBoot - * Copyright ZKA Web Services Co., all rights reserved. - * - * ======================================================== - */ - -#include - -EXTERN_C Int32 ModuleMain(Kernel::HEL::HANDOVER_INFO_HEADER* Handover) -{ - EfiSystemTable* cST = (EfiSystemTable*)Handover->f_FirmwareCustomTables[1]; - - cST->ConOut->ClearScreen(cST->ConOut); - - cST->ConOut->OutputString(cST->ConOut, L"SYSCHK: CHECKING FOR VALID NEFS OR HPFS PARTITIONS...\r\n"); - cST->ConOut->OutputString(cST->ConOut, L"SYSCHK: GOOD TO GO!\r\n"); - - return kEfiOk; -} diff --git a/dev/zba/Modules/SysChk/build.json b/dev/zba/Modules/SysChk/build.json index 477a3ae5..7587d0a2 100644 --- a/dev/zba/Modules/SysChk/build.json +++ b/dev/zba/Modules/SysChk/build.json @@ -2,7 +2,7 @@ "compiler_path": "x86_64-w64-mingw32-g++", "compiler_std": "c++20", "headers_path": ["../", "../../", "../../../zka", "../../../", "./"], - "sources_path": ["*.cxx", "*.S"], + "sources_path": [".cc", "*.S"], "output_name": "syschk.sys", "compiler_flags": [ "-ffreestanding", diff --git a/dev/zba/amd64-efi.make b/dev/zba/amd64-efi.make index 9ebc8972..4d028b8e 100644 --- a/dev/zba/amd64-efi.make +++ b/dev/zba/amd64-efi.make @@ -83,9 +83,9 @@ endif compile-amd64: $(RESCMD) $(CC_GNU) $(ZKA_MODEL) $(STANDALONE_MACRO) $(FLAG_GNU) $(DEBUG) \ - $(wildcard src/HEL/AMD64/*.cxx) \ + $(wildcard src/HEL/AMD64/*.cc) \ $(wildcard src/HEL/AMD64/*.S) \ - $(wildcard src/*.cxx) + $(wildcard src/*.cc) .PHONY: run-efi-amd64 run-efi-amd64: diff --git a/dev/zba/arm64-efi.make b/dev/zba/arm64-efi.make index 638efb12..2735d757 100644 --- a/dev/zba/arm64-efi.make +++ b/dev/zba/arm64-efi.make @@ -77,9 +77,9 @@ endif compile-amd64: $(RESCMD) $(CC_GNU) $(ZKA_MODEL) $(STANDALONE_MACRO) $(FLAG_GNU) $(DEBUG) \ - $(wildcard src/HEL/ARM64/*.cxx) \ + $(wildcard src/HEL/ARM64/*.cc) \ $(wildcard src/HEL/ARM64/*.S) \ - $(wildcard src/*.cxx) + $(wildcard src/*.cc) .PHONY: run-efi-amd64 run-efi-amd64: diff --git a/dev/zba/src/BootFileReader.cc b/dev/zba/src/BootFileReader.cc new file mode 100644 index 00000000..ef1f3492 --- /dev/null +++ b/dev/zba/src/BootFileReader.cc @@ -0,0 +1,203 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + + File: FileReader.cxx + Purpose: New Boot FileReader, + Read complete file and store it in a buffer. + +------------------------------------------- */ + +#include +#include +#include +#include +#include +#include + +/// @file BootFileReader +/// @brief Bootloader File reader. +/// BUGS: 0 + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// +/// +/// @name BFileReader class +/// @brief Reads the file as a blob. +/// +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/*** + @brief File Reader constructor. +*/ +Boot::BFileReader::BFileReader(const CharacterTypeUTF16* path, + EfiHandlePtr ImageHandle) +{ + if (path != nullptr) + { + SizeT index = 0UL; + for (; path[index] != L'\0'; ++index) + { + mPath[index] = path[index]; + } + + mPath[index] = 0; + } + + /// Load protocols with their GUIDs. + + EfiGUID guidEfp = EfiGUID(EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID); + + EfiSimpleFilesystemProtocol* efp = nullptr; + + EfiLoadImageProtocol* img = nullptr; + EfiGUID guidImg = EfiGUID(EFI_LOADED_IMAGE_PROTOCOL_GUID); + + if (BS->HandleProtocol(ImageHandle, &guidImg, (void**)&img) != kEfiOk) + { + mWriter.Write(L"NEWOSLDR: Handle-Protocol: No-Such-Protocol").Write(L"\r"); + this->mErrorCode = kNotSupported; + } + + if (BS->HandleProtocol(img->DeviceHandle, &guidEfp, (void**)&efp) != kEfiOk) + { + mWriter.Write(L"NEWOSLDR: Handle-Protocol: No-Such-Protocol").Write(L"\r"); + this->mErrorCode = kNotSupported; + return; + } + + /// Start doing disk I/O + + if (efp->OpenVolume(efp, &mRootFs) != kEfiOk) + { + mWriter.Write(L"NEWOSLDR: Fetch-Protocol: No-Such-Volume").Write(L"\r"); + this->mErrorCode = kNotSupported; + return; + } + + EfiFileProtocol* KernelFile = nullptr; + + if (mRootFs->Open(mRootFs, &KernelFile, mPath, kEFIFileRead, kEFIReadOnly) != + kEfiOk) + { + mWriter.Write(L"NEWOSLDR: Fetch-Protocol: No-Such-Path: ") + .Write(mPath) + .Write(L"\r"); + this->mErrorCode = kNotSupported; + + CGDrawString("NEWOSLDR: PLEASE RECOVER YOUR MINKRNL INSTALL.", 40, 10, RGB(0xFF, 0xFF, 0xFF)); + + mRootFs->Close(mRootFs); + + return; + } + + mSizeFile = 0; + mFile = KernelFile; + mErrorCode = kOperationOkay; +} + +Boot::BFileReader::~BFileReader() +{ + if (this->mFile) + { + this->mFile->Close(this->mFile); + this->mFile = nullptr; + } + + if (this->mRootFs) + { + this->mRootFs->Close(this->mRootFs); + this->mRootFs = nullptr; + } + + if (this->mBlob) + { + BS->FreePool(this->mBlob); + this->mBlob = nullptr; + } + + BSetMem(this->mPath, 0, kPathLen); +} + +/** + @brief Reads all of the file into a buffer. + @param **readUntil** size of file + @param **chunkToRead** chunk to read each time. +*/ +Void Boot::BFileReader::ReadAll(SizeT readUntil, SizeT chunkToRead, UIntPtr out_address) +{ + if (mBlob == nullptr) + { + EfiFileInfo newPtrInfo; + UInt32 szInfo = 0; + + EfiGUID cFileInfoGUID = EFI_FILE_INFO_GUID; + + if (mFile->GetInfo(mFile, &cFileInfoGUID, &szInfo, &newPtrInfo) == kEfiOk) + { + if (newPtrInfo.FileSize < readUntil) + readUntil = newPtrInfo.FileSize; + else if (readUntil < 1) + readUntil = newPtrInfo.FileSize; + + mWriter.Write(L"NEWOSLDR: FileSize: ").Write(readUntil).Write("\r"); + } + + if (!out_address) + { + if (auto err = BS->AllocatePool(EfiLoaderCode, readUntil, (VoidPtr*)&mBlob) != + kEfiOk) + { + mWriter.Write(L"*** error: ").Write(err).Write(L" ***\r"); + EFI::ThrowError(L"OutOfMemory", L"Out of memory."); + } + } + else + { + mBlob = (VoidPtr)out_address; + } + } + + mErrorCode = kNotSupported; + + UInt64 bufSize = chunkToRead; + UInt64 szCnt = 0UL; + + while (szCnt < readUntil) + { + auto res = mFile->Read(mFile, &bufSize, (VoidPtr)(&((Char*)mBlob)[szCnt])); + + szCnt += bufSize; + + if (res == kBufferTooSmall) + { + bufSize = chunkToRead; + } + } + + mSizeFile = szCnt; + mErrorCode = kOperationOkay; +} + +/// @brief error code getter. +/// @return the error code. +Int32& Boot::BFileReader::Error() +{ + return mErrorCode; +} + +/// @brief blob getter. +/// @return the blob. +VoidPtr Boot::BFileReader::Blob() +{ + return mBlob; +} + +/// @breif Size getter. +/// @return the size of the file. +UInt64& Boot::BFileReader::Size() +{ + return mSizeFile; +} diff --git a/dev/zba/src/BootFileReader.cxx b/dev/zba/src/BootFileReader.cxx deleted file mode 100644 index b1969761..00000000 --- a/dev/zba/src/BootFileReader.cxx +++ /dev/null @@ -1,203 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - - File: FileReader.cxx - Purpose: New Boot FileReader, - Read complete file and store it in a buffer. - -------------------------------------------- */ - -#include -#include -#include -#include -#include -#include - -/// @file BootFileReader -/// @brief Bootloader File reader. -/// BUGS: 0 - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// -/// -/// @name BFileReader class -/// @brief Reads the file as a blob. -/// -/// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -/*** - @brief File Reader constructor. -*/ -Boot::BFileReader::BFileReader(const CharacterTypeUTF16* path, - EfiHandlePtr ImageHandle) -{ - if (path != nullptr) - { - SizeT index = 0UL; - for (; path[index] != L'\0'; ++index) - { - mPath[index] = path[index]; - } - - mPath[index] = 0; - } - - /// Load protocols with their GUIDs. - - EfiGUID guidEfp = EfiGUID(EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID); - - EfiSimpleFilesystemProtocol* efp = nullptr; - - EfiLoadImageProtocol* img = nullptr; - EfiGUID guidImg = EfiGUID(EFI_LOADED_IMAGE_PROTOCOL_GUID); - - if (BS->HandleProtocol(ImageHandle, &guidImg, (void**)&img) != kEfiOk) - { - mWriter.Write(L"NEWOSLDR: Handle-Protocol: No-Such-Protocol").Write(L"\r"); - this->mErrorCode = kNotSupported; - } - - if (BS->HandleProtocol(img->DeviceHandle, &guidEfp, (void**)&efp) != kEfiOk) - { - mWriter.Write(L"NEWOSLDR: Handle-Protocol: No-Such-Protocol").Write(L"\r"); - this->mErrorCode = kNotSupported; - return; - } - - /// Start doing disk I/O - - if (efp->OpenVolume(efp, &mRootFs) != kEfiOk) - { - mWriter.Write(L"NEWOSLDR: Fetch-Protocol: No-Such-Volume").Write(L"\r"); - this->mErrorCode = kNotSupported; - return; - } - - EfiFileProtocol* KernelFile = nullptr; - - if (mRootFs->Open(mRootFs, &KernelFile, mPath, kEFIFileRead, kEFIReadOnly) != - kEfiOk) - { - mWriter.Write(L"NEWOSLDR: Fetch-Protocol: No-Such-Path: ") - .Write(mPath) - .Write(L"\r"); - this->mErrorCode = kNotSupported; - - CGDrawString("NEWOSLDR: PLEASE RECOVER YOUR MINKRNL INSTALL.", 40, 10, RGB(0xFF, 0xFF, 0xFF)); - - mRootFs->Close(mRootFs); - - return; - } - - mSizeFile = 0; - mFile = KernelFile; - mErrorCode = kOperationOkay; -} - -Boot::BFileReader::~BFileReader() -{ - if (this->mFile) - { - this->mFile->Close(this->mFile); - this->mFile = nullptr; - } - - if (this->mRootFs) - { - this->mRootFs->Close(this->mRootFs); - this->mRootFs = nullptr; - } - - if (this->mBlob) - { - BS->FreePool(this->mBlob); - this->mBlob = nullptr; - } - - BSetMem(this->mPath, 0, kPathLen); -} - -/** - @brief Reads all of the file into a buffer. - @param **readUntil** size of file - @param **chunkToRead** chunk to read each time. -*/ -Void Boot::BFileReader::ReadAll(SizeT readUntil, SizeT chunkToRead, UIntPtr out_address) -{ - if (mBlob == nullptr) - { - EfiFileInfo newPtrInfo; - UInt32 szInfo = 0; - - EfiGUID cFileInfoGUID = EFI_FILE_INFO_GUID; - - if (mFile->GetInfo(mFile, &cFileInfoGUID, &szInfo, &newPtrInfo) == kEfiOk) - { - if (newPtrInfo.FileSize < readUntil) - readUntil = newPtrInfo.FileSize; - else if (readUntil < 1) - readUntil = newPtrInfo.FileSize; - - mWriter.Write(L"NEWOSLDR: FileSize: ").Write(readUntil).Write("\r"); - } - - if (!out_address) - { - if (auto err = BS->AllocatePool(EfiLoaderCode, readUntil, (VoidPtr*)&mBlob) != - kEfiOk) - { - mWriter.Write(L"*** error: ").Write(err).Write(L" ***\r"); - EFI::ThrowError(L"OutOfMemory", L"Out of memory."); - } - } - else - { - mBlob = (VoidPtr)out_address; - } - } - - mErrorCode = kNotSupported; - - UInt64 bufSize = chunkToRead; - UInt64 szCnt = 0UL; - - while (szCnt < readUntil) - { - auto res = mFile->Read(mFile, &bufSize, (VoidPtr)(&((Char*)mBlob)[szCnt])); - - szCnt += bufSize; - - if (res == kBufferTooSmall) - { - bufSize = chunkToRead; - } - } - - mSizeFile = szCnt; - mErrorCode = kOperationOkay; -} - -/// @brief error code getter. -/// @return the error code. -Int32& Boot::BFileReader::Error() -{ - return mErrorCode; -} - -/// @brief blob getter. -/// @return the blob. -VoidPtr Boot::BFileReader::Blob() -{ - return mBlob; -} - -/// @breif Size getter. -/// @return the size of the file. -UInt64& Boot::BFileReader::Size() -{ - return mSizeFile; -} diff --git a/dev/zba/src/BootString.cc b/dev/zba/src/BootString.cc new file mode 100644 index 00000000..97ccbc43 --- /dev/null +++ b/dev/zba/src/BootString.cc @@ -0,0 +1,92 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + + File: String.cxx + Purpose: NewBoot string library + + Revision History: + + + +------------------------------------------- */ + +#include +#include +#include + +/// BUGS: 0 + +///////////////////////////////////////////////////////////////////////////////////////////////////////// + +Kernel::SizeT Boot::BCopyMem(CharacterTypeUTF16* dest, CharacterTypeUTF16* src, const Kernel::SizeT len) +{ + if (!dest || !src) + return 0; + + SizeT index = 0UL; + for (; index < len; ++index) + { + dest[index] = src[index]; + } + + return index; +} + +Kernel::SizeT Boot::BStrLen(const CharacterTypeUTF16* ptr) +{ + if (!ptr) + return 0; + + Kernel::SizeT cnt = 0; + + while (*ptr != (CharacterTypeUTF16)0) + { + ++ptr; + ++cnt; + } + + return cnt; +} + +Kernel::SizeT Boot::BSetMem(CharacterTypeUTF16* src, const CharacterTypeUTF16 byte, const Kernel::SizeT len) +{ + if (!src) + return 0; + + Kernel::SizeT cnt = 0UL; + + while (*src != 0) + { + if (cnt > len) + break; + + *src = byte; + ++src; + + ++cnt; + } + + return cnt; +} + +Kernel::SizeT Boot::BSetMem(CharacterTypeUTF8* src, const CharacterTypeUTF8 byte, const Kernel::SizeT len) +{ + if (!src) + return 0; + + Kernel::SizeT cnt = 0UL; + + while (*src != 0) + { + if (cnt > len) + break; + + *src = byte; + ++src; + + ++cnt; + } + + return cnt; +} diff --git a/dev/zba/src/BootString.cxx b/dev/zba/src/BootString.cxx deleted file mode 100644 index 695f9d52..00000000 --- a/dev/zba/src/BootString.cxx +++ /dev/null @@ -1,92 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - - File: String.cxx - Purpose: NewBoot string library - - Revision History: - - - -------------------------------------------- */ - -#include -#include -#include - -/// BUGS: 0 - -///////////////////////////////////////////////////////////////////////////////////////////////////////// - -Kernel::SizeT Boot::BCopyMem(CharacterTypeUTF16* dest, CharacterTypeUTF16* src, const Kernel::SizeT len) -{ - if (!dest || !src) - return 0; - - SizeT index = 0UL; - for (; index < len; ++index) - { - dest[index] = src[index]; - } - - return index; -} - -Kernel::SizeT Boot::BStrLen(const CharacterTypeUTF16* ptr) -{ - if (!ptr) - return 0; - - Kernel::SizeT cnt = 0; - - while (*ptr != (CharacterTypeUTF16)0) - { - ++ptr; - ++cnt; - } - - return cnt; -} - -Kernel::SizeT Boot::BSetMem(CharacterTypeUTF16* src, const CharacterTypeUTF16 byte, const Kernel::SizeT len) -{ - if (!src) - return 0; - - Kernel::SizeT cnt = 0UL; - - while (*src != 0) - { - if (cnt > len) - break; - - *src = byte; - ++src; - - ++cnt; - } - - return cnt; -} - -Kernel::SizeT Boot::BSetMem(CharacterTypeUTF8* src, const CharacterTypeUTF8 byte, const Kernel::SizeT len) -{ - if (!src) - return 0; - - Kernel::SizeT cnt = 0UL; - - while (*src != 0) - { - if (cnt > len) - break; - - *src = byte; - ++src; - - ++cnt; - } - - return cnt; -} diff --git a/dev/zba/src/BootSupport.cc b/dev/zba/src/BootSupport.cc new file mode 100644 index 00000000..0953398b --- /dev/null +++ b/dev/zba/src/BootSupport.cc @@ -0,0 +1,82 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#include +#include +#include +#include +#include +#include +#include + +#ifdef __STANDALONE__ + +/// @brief memset definition in C++. +/// @param dst destination pointer. +/// @param byte value to fill in. +/// @param len length of of src. +EXTERN_C VoidPtr memset(void* dst, int byte, long long unsigned int len) +{ + for (size_t i = 0UL; i < len; ++i) + { + ((int*)dst)[i] = byte; + } + + return dst; +} + +/// @brief memcpy definition in C++. +/// @param dst destination pointer. +/// @param src source pointer. +/// @param len length of of src. +EXTERN_C VoidPtr memcpy(void* dst, const void* src, long long unsigned int len) +{ + for (size_t i = 0UL; i < len; ++i) + { + ((int*)dst)[i] = ((int*)src)[i]; + } + + return dst; +} + +/// @brief strlen definition in C++. +EXTERN_C size_t strlen(const char* whatToCheck) +{ + SizeT len = 0; + + while (whatToCheck[len] != 0) + { + ++len; + } + + return len; +} + +/// @brief strcmp definition in C++. +EXTERN_C int strcmp(const char* whatToCheck, const char* whatToCheckRight) +{ + if (!whatToCheck || *whatToCheck == 0) + return 0; + + SizeT len = 0; + + while (whatToCheck[len] == whatToCheckRight[len]) + { + if (whatToCheck[len] == 0) + return 0; + + ++len; + } + + return len; +} + +/// @brief something specific to the Microsoft's ABI, When the stack grows too big. +EXTERN_C void ___chkstk_ms(void) +{ +} + +#endif diff --git a/dev/zba/src/BootSupport.cxx b/dev/zba/src/BootSupport.cxx deleted file mode 100644 index bda7ee38..00000000 --- a/dev/zba/src/BootSupport.cxx +++ /dev/null @@ -1,82 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#include -#include -#include -#include -#include -#include -#include - -#ifdef __STANDALONE__ - -/// @brief memset definition in C++. -/// @param dst destination pointer. -/// @param byte value to fill in. -/// @param len length of of src. -EXTERN_C VoidPtr memset(void* dst, int byte, long long unsigned int len) -{ - for (size_t i = 0UL; i < len; ++i) - { - ((int*)dst)[i] = byte; - } - - return dst; -} - -/// @brief memcpy definition in C++. -/// @param dst destination pointer. -/// @param src source pointer. -/// @param len length of of src. -EXTERN_C VoidPtr memcpy(void* dst, const void* src, long long unsigned int len) -{ - for (size_t i = 0UL; i < len; ++i) - { - ((int*)dst)[i] = ((int*)src)[i]; - } - - return dst; -} - -/// @brief strlen definition in C++. -EXTERN_C size_t strlen(const char* whatToCheck) -{ - SizeT len = 0; - - while (whatToCheck[len] != 0) - { - ++len; - } - - return len; -} - -/// @brief strcmp definition in C++. -EXTERN_C int strcmp(const char* whatToCheck, const char* whatToCheckRight) -{ - if (!whatToCheck || *whatToCheck == 0) - return 0; - - SizeT len = 0; - - while (whatToCheck[len] == whatToCheckRight[len]) - { - if (whatToCheck[len] == 0) - return 0; - - ++len; - } - - return len; -} - -/// @brief something specific to the Microsoft's ABI, When the stack grows too big. -EXTERN_C void ___chkstk_ms(void) -{ -} - -#endif diff --git a/dev/zba/src/BootTextWriter.cc b/dev/zba/src/BootTextWriter.cc new file mode 100644 index 00000000..1446250c --- /dev/null +++ b/dev/zba/src/BootTextWriter.cc @@ -0,0 +1,169 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + + File: String.cxx + Purpose: NewBoot string library + + Revision History: + + + +------------------------------------------- */ + +#include +#include +#include +#include + +/// BUGS: 0 + +///////////////////////////////////////////////////////////////////////////////////////////////////////// + +/** +@brief puts wrapper over EFI ConOut. +*/ +Boot::BTextWriter& Boot::BTextWriter::Write(const CharacterTypeUTF16* str) +{ +#ifdef __DEBUG__ + if (!str || *str == 0) + return *this; + + CharacterTypeUTF16 strTmp[2]; + strTmp[1] = 0; + + for (size_t i = 0; str[i] != 0; i++) + { + if (str[i] == '\r') + { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + + strTmp[0] = '\n'; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } + else + { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } + } +#endif // ifdef __DEBUG__ + + return *this; +} + +/// @brief UTF-8 equivalent of Write (UTF-16). +/// @param str the input string. +Boot::BTextWriter& Boot::BTextWriter::Write(const Char* str) +{ +#ifdef __DEBUG__ + if (!str || *str == 0) + return *this; + + CharacterTypeUTF16 strTmp[2]; + strTmp[1] = 0; + + for (size_t i = 0; str[i] != 0; i++) + { + if (str[i] == '\r') + { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + + strTmp[0] = '\n'; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } + else + { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } + } +#endif // ifdef __DEBUG__ + + return *this; +} + +Boot::BTextWriter& Boot::BTextWriter::Write(const UChar* str) +{ +#ifdef __DEBUG__ + if (!str || *str == 0) + return *this; + + CharacterTypeUTF16 strTmp[2]; + strTmp[1] = 0; + + for (size_t i = 0; str[i] != 0; i++) + { + if (str[i] == '\r') + { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + + strTmp[0] = '\n'; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } + else + { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } + } +#endif // ifdef __DEBUG__ + + return *this; +} + +/** +@brief putc wrapper over EFI ConOut. +*/ +Boot::BTextWriter& Boot::BTextWriter::WriteCharacter(CharacterTypeUTF16 c) +{ +#ifdef __DEBUG__ + EfiCharType str[2]; + + str[0] = c; + str[1] = 0; + ST->ConOut->OutputString(ST->ConOut, str); +#endif // ifdef __DEBUG__ + + return *this; +} + +Boot::BTextWriter& Boot::BTextWriter::Write(const Long& x) +{ +#ifdef __DEBUG__ + this->_Write(x); + this->Write("h"); +#endif // ifdef __DEBUG__ + + return *this; +} + +Boot::BTextWriter& Boot::BTextWriter::_Write(const Long& x) +{ +#ifdef __DEBUG__ + UInt64 y = (x > 0 ? x : -x) / 16; + UInt64 h = (x > 0 ? x : -x) % 16; + + if (y) + this->_Write(y); + + /* fail if the hex number is not base-16 */ + if (h > 16) + { + this->WriteCharacter('?'); + return *this; + } + + if (y < 0) + y = -y; + + const char cNumbers[] = "0123456789ABCDEF"; + + this->WriteCharacter(cNumbers[h]); +#endif // ifdef __DEBUG__ + + return *this; +} diff --git a/dev/zba/src/BootTextWriter.cxx b/dev/zba/src/BootTextWriter.cxx deleted file mode 100644 index e904f2f9..00000000 --- a/dev/zba/src/BootTextWriter.cxx +++ /dev/null @@ -1,169 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - - File: String.cxx - Purpose: NewBoot string library - - Revision History: - - - -------------------------------------------- */ - -#include -#include -#include -#include - -/// BUGS: 0 - -///////////////////////////////////////////////////////////////////////////////////////////////////////// - -/** -@brief puts wrapper over EFI ConOut. -*/ -Boot::BTextWriter& Boot::BTextWriter::Write(const CharacterTypeUTF16* str) -{ -#ifdef __DEBUG__ - if (!str || *str == 0) - return *this; - - CharacterTypeUTF16 strTmp[2]; - strTmp[1] = 0; - - for (size_t i = 0; str[i] != 0; i++) - { - if (str[i] == '\r') - { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); - - strTmp[0] = '\n'; - ST->ConOut->OutputString(ST->ConOut, strTmp); - } - else - { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); - } - } -#endif // ifdef __DEBUG__ - - return *this; -} - -/// @brief UTF-8 equivalent of Write (UTF-16). -/// @param str the input string. -Boot::BTextWriter& Boot::BTextWriter::Write(const Char* str) -{ -#ifdef __DEBUG__ - if (!str || *str == 0) - return *this; - - CharacterTypeUTF16 strTmp[2]; - strTmp[1] = 0; - - for (size_t i = 0; str[i] != 0; i++) - { - if (str[i] == '\r') - { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); - - strTmp[0] = '\n'; - ST->ConOut->OutputString(ST->ConOut, strTmp); - } - else - { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); - } - } -#endif // ifdef __DEBUG__ - - return *this; -} - -Boot::BTextWriter& Boot::BTextWriter::Write(const UChar* str) -{ -#ifdef __DEBUG__ - if (!str || *str == 0) - return *this; - - CharacterTypeUTF16 strTmp[2]; - strTmp[1] = 0; - - for (size_t i = 0; str[i] != 0; i++) - { - if (str[i] == '\r') - { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); - - strTmp[0] = '\n'; - ST->ConOut->OutputString(ST->ConOut, strTmp); - } - else - { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); - } - } -#endif // ifdef __DEBUG__ - - return *this; -} - -/** -@brief putc wrapper over EFI ConOut. -*/ -Boot::BTextWriter& Boot::BTextWriter::WriteCharacter(CharacterTypeUTF16 c) -{ -#ifdef __DEBUG__ - EfiCharType str[2]; - - str[0] = c; - str[1] = 0; - ST->ConOut->OutputString(ST->ConOut, str); -#endif // ifdef __DEBUG__ - - return *this; -} - -Boot::BTextWriter& Boot::BTextWriter::Write(const Long& x) -{ -#ifdef __DEBUG__ - this->_Write(x); - this->Write("h"); -#endif // ifdef __DEBUG__ - - return *this; -} - -Boot::BTextWriter& Boot::BTextWriter::_Write(const Long& x) -{ -#ifdef __DEBUG__ - UInt64 y = (x > 0 ? x : -x) / 16; - UInt64 h = (x > 0 ? x : -x) % 16; - - if (y) - this->_Write(y); - - /* fail if the hex number is not base-16 */ - if (h > 16) - { - this->WriteCharacter('?'); - return *this; - } - - if (y < 0) - y = -y; - - const char cNumbers[] = "0123456789ABCDEF"; - - this->WriteCharacter(cNumbers[h]); -#endif // ifdef __DEBUG__ - - return *this; -} diff --git a/dev/zba/src/BootloaderRsrc.rsrc b/dev/zba/src/BootloaderRsrc.rsrc index 25c343e1..23a0bf09 100644 --- a/dev/zba/src/BootloaderRsrc.rsrc +++ b/dev/zba/src/BootloaderRsrc.rsrc @@ -1,4 +1,4 @@ -#include "../../Kernel/CompilerKit/Version.hxx" +#include "../../Kernel/CompilerKit/Version.h" 1 VERSIONINFO FILEVERSION 1,0,0,0 diff --git a/dev/zba/src/HEL/AMD64/BootAHCI.cc b/dev/zba/src/HEL/AMD64/BootAHCI.cc new file mode 100644 index 00000000..5899f177 --- /dev/null +++ b/dev/zba/src/HEL/AMD64/BootAHCI.cc @@ -0,0 +1,20 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +/** + * @file AHCI.cxx + * @author Amlal El Mahrouss (amlalelmahrouss@icloud.com) + * @brief AHCI driver. + * @version 0.1 + * @date 2024-02-02 + * + * @copyright Copyright (c) ZKA Web Services Co + * + */ + +#include +#include +#include diff --git a/dev/zba/src/HEL/AMD64/BootAHCI.cxx b/dev/zba/src/HEL/AMD64/BootAHCI.cxx deleted file mode 100644 index 0c4eb953..00000000 --- a/dev/zba/src/HEL/AMD64/BootAHCI.cxx +++ /dev/null @@ -1,20 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -/** - * @file AHCI.cxx - * @author Amlal El Mahrouss (amlalelmahrouss@icloud.com) - * @brief AHCI driver. - * @version 0.1 - * @date 2024-02-02 - * - * @copyright Copyright (c) ZKA Web Services Co - * - */ - -#include -#include -#include diff --git a/dev/zba/src/HEL/AMD64/BootATA.cc b/dev/zba/src/HEL/AMD64/BootATA.cc new file mode 100644 index 00000000..78792175 --- /dev/null +++ b/dev/zba/src/HEL/AMD64/BootATA.cc @@ -0,0 +1,278 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +/** + * @file ATA.cxx + * @author Amlal El Mahrouss (amlalelmahrouss@icloud.com) + * @brief ATA driver. + * @version 0.1 + * @date 2024-02-02 + * + * @copyright Copyright (c) ZKA Web Services Co + * + */ + +#include +#include +#include + +/// bugs: 0 + +using namespace Boot; + +#define kATADataLen 256 + +static Boolean kATADetected = false; +static Int32 kATADeviceType = kATADeviceCount; +static UInt16 kATAData[kATADataLen] = {0}; + +Boolean boot_ata_detected(Void); + +STATIC Boolean boot_ata_wait_io(UInt16 IO) +{ + for (int i = 0; i < 400; i++) + In8(IO + ATA_REG_STATUS); + +ATAWaitForIO_Retry: + auto statRdy = In8(IO + ATA_REG_STATUS); + + if ((statRdy & ATA_SR_BSY)) + goto ATAWaitForIO_Retry; + +ATAWaitForIO_Retry2: + statRdy = In8(IO + ATA_REG_STATUS); + + if (statRdy & ATA_SR_ERR) + return false; + + if (!(statRdy & ATA_SR_DRDY)) + goto ATAWaitForIO_Retry2; + + return true; +} + +Void boot_ata_select(UInt16 Bus) +{ + if (Bus == ATA_PRIMARY_IO) + Out8(Bus + ATA_REG_HDDEVSEL, ATA_PRIMARY_SEL); + else + Out8(Bus + ATA_REG_HDDEVSEL, ATA_SECONDARY_SEL); +} + +Boolean boot_ata_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster) +{ + if (boot_ata_detected()) + return true; + + BTextWriter writer; + + UInt16 IO = Bus; + + boot_ata_select(IO); + + // Bus init, NEIN bit. + Out8(IO + ATA_REG_NEIN, 1); + + // identify until it's good. +ATAInit_Retry: + auto statRdy = In8(IO + ATA_REG_STATUS); + + if (statRdy & ATA_SR_ERR) + { + writer.Write( + L"NEWOSLDR: ATA: error, not an IDE based hard-drive.\r"); + + return false; + } + + if ((statRdy & ATA_SR_BSY)) + goto ATAInit_Retry; + + Out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY); + + /// fetch serial info + /// model, speed, number of sectors... + + boot_ata_wait_io(IO); + + for (SizeT indexData = 0ul; indexData < kATADataLen; ++indexData) + { + kATAData[indexData] = In16(IO + ATA_REG_DATA); + } + + OutBus = + (Bus == ATA_PRIMARY_IO) ? BootDeviceATA::kPrimary : BootDeviceATA::kSecondary; + + OutMaster = (Bus == ATA_PRIMARY_IO) ? ATA_MASTER : ATA_SLAVE; + + return true; +} + +Void boot_ata_read(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, SizeT SectorSz, SizeT Size) +{ + Lba /= SectorSz; + + UInt8 Command = ((!Master) ? 0xE0 : 0xF0); + + boot_ata_wait_io(IO); + boot_ata_select(IO); + + Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); + + Out8(IO + ATA_REG_SEC_COUNT0, ((Size + SectorSz) / SectorSz)); + + Out8(IO + ATA_REG_LBA0, (Lba)&0xFF); + 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); + + 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); + } +} + +Void boot_ata_write(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, SizeT SectorSz, SizeT Size) +{ + Lba /= SectorSz; + + UInt8 Command = ((!Master) ? 0xE0 : 0xF0); + + boot_ata_wait_io(IO); + boot_ata_select(IO); + + Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); + + Out8(IO + ATA_REG_SEC_COUNT0, ((Size + (SectorSz)) / SectorSz)); + + Out8(IO + ATA_REG_LBA0, (Lba)&0xFF); + 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); + + 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); + } +} + +/// @check is ATA detected? +Boolean boot_ata_detected(Void) +{ + return kATADetected; +} + +/*** + * + * + * @brief ATA Device class. + * + * + */ + +/** + * @brief ATA Device constructor. + * @param void none. + */ +BootDeviceATA::BootDeviceATA() noexcept +{ + if (boot_ata_init(ATA_PRIMARY_IO, true, this->Leak().mBus, + this->Leak().mMaster) || + boot_ata_init(ATA_SECONDARY_IO, true, this->Leak().mBus, + this->Leak().mMaster)) + { + kATADetected = true; + } +} +/** + * @brief Is ATA detected? + */ +BootDeviceATA::operator bool() +{ + return boot_ata_detected(); +} + +/** + @brief Read Buf from disk + @param Sz Sector size + @param Buf buffer +*/ +BootDeviceATA& BootDeviceATA::Read(CharacterTypeUTF8* Buf, const SizeT& SectorSz) +{ + if (!boot_ata_detected()) + { + Leak().mErr = true; + return *this; + } + + this->Leak().mErr = false; + + if (!Buf || SectorSz < 1) + return *this; + + boot_ata_read(this->Leak().mBase, this->Leak().mBus, this->Leak().mMaster, + Buf, SectorSz, this->Leak().mSize); + + return *this; +} + +/** + @brief Write Buf into disk + @param Sz Sector size + @param Buf buffer +*/ +BootDeviceATA& BootDeviceATA::Write(CharacterTypeUTF8* Buf, const SizeT& SectorSz) +{ + if (!boot_ata_detected()) + { + Leak().mErr = true; + return *this; + } + + Leak().mErr = false; + + if (!Buf || SectorSz < 1) + return *this; + + boot_ata_write(this->Leak().mBase, this->Leak().mBus, this->Leak().mMaster, + Buf, SectorSz, this->Leak().mSize); + + return *this; +} + +/** + * @brief ATA trait getter. + * @return BootDeviceATA::ATATrait& the drive config. + */ +BootDeviceATA::ATATrait& BootDeviceATA::Leak() +{ + return mTrait; +} + +/*** + @brief Getter, gets the number of sectors inside the drive. +*/ +SizeT BootDeviceATA::GetSectorsCount() noexcept +{ + return (kATAData[61] << 16) | kATAData[60]; +} + +SizeT BootDeviceATA::GetDiskSize() noexcept +{ + return this->GetSectorsCount() * BootDeviceATA::kSectorSize; +} diff --git a/dev/zba/src/HEL/AMD64/BootATA.cxx b/dev/zba/src/HEL/AMD64/BootATA.cxx deleted file mode 100644 index 6561c150..00000000 --- a/dev/zba/src/HEL/AMD64/BootATA.cxx +++ /dev/null @@ -1,278 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -/** - * @file ATA.cxx - * @author Amlal El Mahrouss (amlalelmahrouss@icloud.com) - * @brief ATA driver. - * @version 0.1 - * @date 2024-02-02 - * - * @copyright Copyright (c) ZKA Web Services Co - * - */ - -#include -#include -#include - -/// bugs: 0 - -using namespace Boot; - -#define kATADataLen 256 - -static Boolean kATADetected = false; -static Int32 kATADeviceType = kATADeviceCount; -static UInt16 kATAData[kATADataLen] = {0}; - -Boolean boot_ata_detected(Void); - -STATIC Boolean boot_ata_wait_io(UInt16 IO) -{ - for (int i = 0; i < 400; i++) - In8(IO + ATA_REG_STATUS); - -ATAWaitForIO_Retry: - auto statRdy = In8(IO + ATA_REG_STATUS); - - if ((statRdy & ATA_SR_BSY)) - goto ATAWaitForIO_Retry; - -ATAWaitForIO_Retry2: - statRdy = In8(IO + ATA_REG_STATUS); - - if (statRdy & ATA_SR_ERR) - return false; - - if (!(statRdy & ATA_SR_DRDY)) - goto ATAWaitForIO_Retry2; - - return true; -} - -Void boot_ata_select(UInt16 Bus) -{ - if (Bus == ATA_PRIMARY_IO) - Out8(Bus + ATA_REG_HDDEVSEL, ATA_PRIMARY_SEL); - else - Out8(Bus + ATA_REG_HDDEVSEL, ATA_SECONDARY_SEL); -} - -Boolean boot_ata_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster) -{ - if (boot_ata_detected()) - return true; - - BTextWriter writer; - - UInt16 IO = Bus; - - boot_ata_select(IO); - - // Bus init, NEIN bit. - Out8(IO + ATA_REG_NEIN, 1); - - // identify until it's good. -ATAInit_Retry: - auto statRdy = In8(IO + ATA_REG_STATUS); - - if (statRdy & ATA_SR_ERR) - { - writer.Write( - L"NEWOSLDR: ATA: error, not an IDE based hard-drive.\r"); - - return false; - } - - if ((statRdy & ATA_SR_BSY)) - goto ATAInit_Retry; - - Out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY); - - /// fetch serial info - /// model, speed, number of sectors... - - boot_ata_wait_io(IO); - - for (SizeT indexData = 0ul; indexData < kATADataLen; ++indexData) - { - kATAData[indexData] = In16(IO + ATA_REG_DATA); - } - - OutBus = - (Bus == ATA_PRIMARY_IO) ? BootDeviceATA::kPrimary : BootDeviceATA::kSecondary; - - OutMaster = (Bus == ATA_PRIMARY_IO) ? ATA_MASTER : ATA_SLAVE; - - return true; -} - -Void boot_ata_read(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, SizeT SectorSz, SizeT Size) -{ - Lba /= SectorSz; - - UInt8 Command = ((!Master) ? 0xE0 : 0xF0); - - boot_ata_wait_io(IO); - boot_ata_select(IO); - - Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); - - Out8(IO + ATA_REG_SEC_COUNT0, ((Size + SectorSz) / SectorSz)); - - Out8(IO + ATA_REG_LBA0, (Lba)&0xFF); - 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); - - 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); - } -} - -Void boot_ata_write(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, SizeT SectorSz, SizeT Size) -{ - Lba /= SectorSz; - - UInt8 Command = ((!Master) ? 0xE0 : 0xF0); - - boot_ata_wait_io(IO); - boot_ata_select(IO); - - Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); - - Out8(IO + ATA_REG_SEC_COUNT0, ((Size + (SectorSz)) / SectorSz)); - - Out8(IO + ATA_REG_LBA0, (Lba)&0xFF); - 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); - - 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); - } -} - -/// @check is ATA detected? -Boolean boot_ata_detected(Void) -{ - return kATADetected; -} - -/*** - * - * - * @brief ATA Device class. - * - * - */ - -/** - * @brief ATA Device constructor. - * @param void none. - */ -BootDeviceATA::BootDeviceATA() noexcept -{ - if (boot_ata_init(ATA_PRIMARY_IO, true, this->Leak().mBus, - this->Leak().mMaster) || - boot_ata_init(ATA_SECONDARY_IO, true, this->Leak().mBus, - this->Leak().mMaster)) - { - kATADetected = true; - } -} -/** - * @brief Is ATA detected? - */ -BootDeviceATA::operator bool() -{ - return boot_ata_detected(); -} - -/** - @brief Read Buf from disk - @param Sz Sector size - @param Buf buffer -*/ -BootDeviceATA& BootDeviceATA::Read(CharacterTypeUTF8* Buf, const SizeT& SectorSz) -{ - if (!boot_ata_detected()) - { - Leak().mErr = true; - return *this; - } - - this->Leak().mErr = false; - - if (!Buf || SectorSz < 1) - return *this; - - boot_ata_read(this->Leak().mBase, this->Leak().mBus, this->Leak().mMaster, - Buf, SectorSz, this->Leak().mSize); - - return *this; -} - -/** - @brief Write Buf into disk - @param Sz Sector size - @param Buf buffer -*/ -BootDeviceATA& BootDeviceATA::Write(CharacterTypeUTF8* Buf, const SizeT& SectorSz) -{ - if (!boot_ata_detected()) - { - Leak().mErr = true; - return *this; - } - - Leak().mErr = false; - - if (!Buf || SectorSz < 1) - return *this; - - boot_ata_write(this->Leak().mBase, this->Leak().mBus, this->Leak().mMaster, - Buf, SectorSz, this->Leak().mSize); - - return *this; -} - -/** - * @brief ATA trait getter. - * @return BootDeviceATA::ATATrait& the drive config. - */ -BootDeviceATA::ATATrait& BootDeviceATA::Leak() -{ - return mTrait; -} - -/*** - @brief Getter, gets the number of sectors inside the drive. -*/ -SizeT BootDeviceATA::GetSectorsCount() noexcept -{ - return (kATAData[61] << 16) | kATAData[60]; -} - -SizeT BootDeviceATA::GetDiskSize() noexcept -{ - return this->GetSectorsCount() * BootDeviceATA::kSectorSize; -} diff --git a/dev/zba/src/HEL/AMD64/BootMain.cc b/dev/zba/src/HEL/AMD64/BootMain.cc new file mode 100644 index 00000000..1520dd1d --- /dev/null +++ b/dev/zba/src/HEL/AMD64/BootMain.cc @@ -0,0 +1,324 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// make the compiler shut up. +#ifndef kMachineModel +#define kMachineModel "ZKA SSD" +#endif // !kMachineModel + +#ifndef cExpectedWidth +#define cExpectedWidth 1280 +#endif + +#ifndef cExpectedHeight +#define cExpectedHeight 720 +#endif + +/** Graphics related. */ + +STATIC EfiGraphicsOutputProtocol* kGop = nullptr; +STATIC UInt16 kGopStride = 0U; +STATIC EfiGUID kGopGuid; + +EXTERN_C Void rt_reset_hardware(); + +/** + @brief Finds and stores the GOP. +*/ + +EXTERN EfiBootServices* BS; + +STATIC Void boot_init_fb() noexcept +{ + kGopGuid = EfiGUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); + kGop = nullptr; + + BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*)&kGop); + + 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 == cExpectedWidth && + infoPtr->VerticalResolution == cExpectedHeight) + { + kGop->SetMode(kGop, i); + break; + } + } +} + +EXTERN_C VoidPtr boot_read_cr3(); +EXTERN_C Void boot_write_cr3(VoidPtr new_cr3); + +EXTERN EfiBootServices* BS; + +/// @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); ///! Init the EFI library. + + HEL::HANDOVER_INFO_HEADER* handover_hdr = + new HEL::HANDOVER_INFO_HEADER(); + + UInt32 map_key = 0; + UInt32 size_struct_ptr = sizeof(EfiMemoryDescriptor); + EfiMemoryDescriptor* struct_ptr = nullptr; + UInt32 sz_desc = sizeof(EfiMemoryDescriptor); + UInt32 rev_desc = 0; + +#ifdef __ZKA_USE_FB__ + boot_init_fb(); ///! Init the GOP. + + for (SizeT indexVT = 0; indexVT < SystemTable->NumberOfTableEntries; + ++indexVT) + { + Char* vendor_table = reinterpret_cast( + SystemTable->ConfigurationTable[indexVT].VendorTable); + + /// ACPI's 'RSD PTR', which contains hardware tables (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 // __ZKA_USE_FB__ + + // ------------------------------------------- // + // Grab MP services, extended to runtime. // + // ------------------------------------------- // + + auto 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; + +#ifdef __ZKA_USE_FB__ + CGInit(); + CGDrawInRegion(CGColor(0xFF, 0x3A, 0x3A), handover_hdr->f_GOP.f_Height, handover_hdr->f_GOP.f_Width, 0, 0); + CGFini(); +#endif // __ZKA_USE_FB__ + + UInt32 cnt_enabled = 0; + UInt32 cnt_disabled = 0; + + mp->GetNumberOfProcessors(mp, &cnt_disabled, &cnt_enabled); + +#ifdef __ZKA_USE_FB__ + CGDrawString("NEWOSLDR (C) ZKA WEB SERVICES.", 10, 10, RGB(0xFF, 0xFF, 0xFF)); + CGDrawString((cnt_enabled > 1) ? "MULTIPLE PROCESSORS DETECTED." : "SINGLE PROCESSOR DETECTED.", 20, 10, RGB(0xFF, 0xFF, 0xFF)); +#endif // __ZKA_USE_FB__ + + handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = cnt_enabled > 1; + // Fill handover header now. + + Boot::BDiskFormatFactory partition_factory; + + // ---------------------------------------------------- // + // The following checks for an exisiting partition + // inside the disk, if it doesn't have one, + // format the disk. + // ---------------------------------------------------- // + +#ifdef __AHCI__ + if (!partition_factory.IsPartitionValid()) + { + Boot::BDiskFormatFactory::BFileDescriptor root; + root.fFileName[0] = kNeFSRoot[0]; + root.fFileName[1] = 0; + + root.fKind = kNeFSCatalogKindDir; + + partition_factory.Format("FileSystem (A:)", &root, 1); + + rt_reset_hardware(); + } +#endif + + 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 cDefaultMemoryMap = 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); + } + + cDefaultMemoryMap = 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", ImageHandle); + reader_syschk.ReadAll(0); + + Boot::BThread* syschk_thread = nullptr; + + // ------------------------------------------ // + // If we succeed in reading the blob, then execute it. + // ------------------------------------------ // + + if (reader_syschk.Blob()) + { + syschk_thread = new Boot::BThread(reader_syschk.Blob()); + syschk_thread->SetName("System Check."); + } + + syschk_thread->Start(handover_hdr); + + // nullify 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(SystemTable->FirmwareVendor); + + handover_hdr->f_Magic = kHandoverMagic; + handover_hdr->f_Version = kHandoverVersion; + + // Provide fimware vendor name. + + Boot::BCopyMem(handover_hdr->f_FirmwareVendorName, SystemTable->FirmwareVendor, + handover_hdr->f_FirmwareVendorLen); + + handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(SystemTable->FirmwareVendor); + + // Assign to global 'kHandoverHeader'. + + Boot::BFileReader reader_kernel(L"minoskrnl.exe", ImageHandle); + + reader_kernel.ReadAll(0); + + Boot::BThread* kernel_thread = nullptr; + + // ------------------------------------------ // + // If we succeed in reading the blob, then execute it. + // ------------------------------------------ // + + if (reader_kernel.Blob()) + { + kernel_thread = new Boot::BThread(reader_kernel.Blob()); + kernel_thread->SetName("Minimal Kernel."); + + handover_hdr->f_KernelImage = reader_kernel.Blob(); + } + else + { +#ifdef __ZKA_USE_FB__ + CGDrawString("NEWOSLDR: PLEASE RECOVER YOUR KERNEL IMAGE.", 30, 10, RGB(0xFF, 0xFF, 0xFF)); +#endif // __ZKA_USE_FB__ + } + + Boot::BFileReader chimeWav(L"zka\\startup.wav", ImageHandle); + Boot::BFileReader readerSysDrv(L"zka\\startup.sys", ImageHandle); + Boot::BFileReader urbanistTTF(L"zka\\urbanist.ttf", ImageHandle); + + readerSysDrv.ReadAll(0); + chimeWav.ReadAll(0); + urbanistTTF.ReadAll(0); + + if (readerSysDrv.Blob() && + chimeWav.Blob() && + urbanistTTF.Blob()) + { + handover_hdr->f_StartupChime = chimeWav.Blob(); + handover_hdr->f_ChimeSz = chimeWav.Size(); + handover_hdr->f_StartupImage = readerSysDrv.Blob(); + handover_hdr->f_StartupSz = readerSysDrv.Size(); + handover_hdr->f_KernelImage = reader_kernel.Blob(); + handover_hdr->f_KernelSz = reader_kernel.Size(); + handover_hdr->f_TTFallbackFont = urbanistTTF.Blob(); + handover_hdr->f_FontSz = urbanistTTF.Size(); + } + else + { +#ifdef __ZKA_USE_FB__ + CGDrawString("NEWOSLDR: ONE OR MORE SYSTEM COMPONENTS ARE MISSING, PLEASE REINSTALL THE OS.", 30, 10, RGB(0xFF, 0xFF, 0xFF)); +#endif // __ZKA_USE_FB__ + } + + EFI::ExitBootServices(map_key, ImageHandle); + +#ifdef __ZKA_USE_FB__ + CGDrawInRegion(CGColor(0xFF, 0x3A, 0x3A), handover_hdr->f_GOP.f_Height, handover_hdr->f_GOP.f_Width, 0, 0); + CGFini(); +#endif // __ZKA_USE_FB__ + + // ---------------------------------------------------- // + // Finally load Kernel, and the cr3 to it. + // ---------------------------------------------------- // + + kernel_thread->Start(handover_hdr); + + CANT_REACH(); +} diff --git a/dev/zba/src/HEL/AMD64/BootMain.cxx b/dev/zba/src/HEL/AMD64/BootMain.cxx deleted file mode 100644 index 6010d21b..00000000 --- a/dev/zba/src/HEL/AMD64/BootMain.cxx +++ /dev/null @@ -1,324 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -// make the compiler shut up. -#ifndef kMachineModel -#define kMachineModel "ZKA SSD" -#endif // !kMachineModel - -#ifndef cExpectedWidth -#define cExpectedWidth 1280 -#endif - -#ifndef cExpectedHeight -#define cExpectedHeight 720 -#endif - -/** Graphics related. */ - -STATIC EfiGraphicsOutputProtocol* kGop = nullptr; -STATIC UInt16 kGopStride = 0U; -STATIC EfiGUID kGopGuid; - -EXTERN_C Void rt_reset_hardware(); - -/** - @brief Finds and stores the GOP. -*/ - -EXTERN EfiBootServices* BS; - -STATIC Void boot_init_fb() noexcept -{ - kGopGuid = EfiGUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); - kGop = nullptr; - - BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*)&kGop); - - 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 == cExpectedWidth && - infoPtr->VerticalResolution == cExpectedHeight) - { - kGop->SetMode(kGop, i); - break; - } - } -} - -EXTERN_C VoidPtr boot_read_cr3(); -EXTERN_C Void boot_write_cr3(VoidPtr new_cr3); - -EXTERN EfiBootServices* BS; - -/// @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); ///! Init the EFI library. - - HEL::HANDOVER_INFO_HEADER* handover_hdr = - new HEL::HANDOVER_INFO_HEADER(); - - UInt32 map_key = 0; - UInt32 size_struct_ptr = sizeof(EfiMemoryDescriptor); - EfiMemoryDescriptor* struct_ptr = nullptr; - UInt32 sz_desc = sizeof(EfiMemoryDescriptor); - UInt32 rev_desc = 0; - -#ifdef __ZKA_USE_FB__ - boot_init_fb(); ///! Init the GOP. - - for (SizeT indexVT = 0; indexVT < SystemTable->NumberOfTableEntries; - ++indexVT) - { - Char* vendor_table = reinterpret_cast( - SystemTable->ConfigurationTable[indexVT].VendorTable); - - /// ACPI's 'RSD PTR', which contains hardware tables (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 // __ZKA_USE_FB__ - - // ------------------------------------------- // - // Grab MP services, extended to runtime. // - // ------------------------------------------- // - - auto 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; - -#ifdef __ZKA_USE_FB__ - CGInit(); - CGDrawInRegion(CGColor(0xFF, 0x3A, 0x3A), handover_hdr->f_GOP.f_Height, handover_hdr->f_GOP.f_Width, 0, 0); - CGFini(); -#endif // __ZKA_USE_FB__ - - UInt32 cnt_enabled = 0; - UInt32 cnt_disabled = 0; - - mp->GetNumberOfProcessors(mp, &cnt_disabled, &cnt_enabled); - -#ifdef __ZKA_USE_FB__ - CGDrawString("NEWOSLDR (C) ZKA WEB SERVICES.", 10, 10, RGB(0xFF, 0xFF, 0xFF)); - CGDrawString((cnt_enabled > 1) ? "MULTIPLE PROCESSORS DETECTED." : "SINGLE PROCESSOR DETECTED.", 20, 10, RGB(0xFF, 0xFF, 0xFF)); -#endif // __ZKA_USE_FB__ - - handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = cnt_enabled > 1; - // Fill handover header now. - - Boot::BDiskFormatFactory partition_factory; - - // ---------------------------------------------------- // - // The following checks for an exisiting partition - // inside the disk, if it doesn't have one, - // format the disk. - // ---------------------------------------------------- // - -#ifdef __AHCI__ - if (!partition_factory.IsPartitionValid()) - { - Boot::BDiskFormatFactory::BFileDescriptor root; - root.fFileName[0] = kNeFSRoot[0]; - root.fFileName[1] = 0; - - root.fKind = kNeFSCatalogKindDir; - - partition_factory.Format("FileSystem (A:)", &root, 1); - - rt_reset_hardware(); - } -#endif - - 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 cDefaultMemoryMap = 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); - } - - cDefaultMemoryMap = 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", ImageHandle); - reader_syschk.ReadAll(0); - - Boot::BThread* syschk_thread = nullptr; - - // ------------------------------------------ // - // If we succeed in reading the blob, then execute it. - // ------------------------------------------ // - - if (reader_syschk.Blob()) - { - syschk_thread = new Boot::BThread(reader_syschk.Blob()); - syschk_thread->SetName("System Check."); - } - - syschk_thread->Start(handover_hdr); - - // nullify 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(SystemTable->FirmwareVendor); - - handover_hdr->f_Magic = kHandoverMagic; - handover_hdr->f_Version = kHandoverVersion; - - // Provide fimware vendor name. - - Boot::BCopyMem(handover_hdr->f_FirmwareVendorName, SystemTable->FirmwareVendor, - handover_hdr->f_FirmwareVendorLen); - - handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(SystemTable->FirmwareVendor); - - // Assign to global 'kHandoverHeader'. - - Boot::BFileReader reader_kernel(L"minoskrnl.exe", ImageHandle); - - reader_kernel.ReadAll(0); - - Boot::BThread* kernel_thread = nullptr; - - // ------------------------------------------ // - // If we succeed in reading the blob, then execute it. - // ------------------------------------------ // - - if (reader_kernel.Blob()) - { - kernel_thread = new Boot::BThread(reader_kernel.Blob()); - kernel_thread->SetName("Minimal Kernel."); - - handover_hdr->f_KernelImage = reader_kernel.Blob(); - } - else - { -#ifdef __ZKA_USE_FB__ - CGDrawString("NEWOSLDR: PLEASE RECOVER YOUR KERNEL IMAGE.", 30, 10, RGB(0xFF, 0xFF, 0xFF)); -#endif // __ZKA_USE_FB__ - } - - Boot::BFileReader chimeWav(L"zka\\startup.wav", ImageHandle); - Boot::BFileReader readerSysDrv(L"zka\\startup.sys", ImageHandle); - Boot::BFileReader urbanistTTF(L"zka\\urbanist.ttf", ImageHandle); - - readerSysDrv.ReadAll(0); - chimeWav.ReadAll(0); - urbanistTTF.ReadAll(0); - - if (readerSysDrv.Blob() && - chimeWav.Blob() && - urbanistTTF.Blob()) - { - handover_hdr->f_StartupChime = chimeWav.Blob(); - handover_hdr->f_ChimeSz = chimeWav.Size(); - handover_hdr->f_StartupImage = readerSysDrv.Blob(); - handover_hdr->f_StartupSz = readerSysDrv.Size(); - handover_hdr->f_KernelImage = reader_kernel.Blob(); - handover_hdr->f_KernelSz = reader_kernel.Size(); - handover_hdr->f_TTFallbackFont = urbanistTTF.Blob(); - handover_hdr->f_FontSz = urbanistTTF.Size(); - } - else - { -#ifdef __ZKA_USE_FB__ - CGDrawString("NEWOSLDR: ONE OR MORE SYSTEM COMPONENTS ARE MISSING, PLEASE REINSTALL THE OS.", 30, 10, RGB(0xFF, 0xFF, 0xFF)); -#endif // __ZKA_USE_FB__ - } - - EFI::ExitBootServices(map_key, ImageHandle); - -#ifdef __ZKA_USE_FB__ - CGDrawInRegion(CGColor(0xFF, 0x3A, 0x3A), handover_hdr->f_GOP.f_Height, handover_hdr->f_GOP.f_Width, 0, 0); - CGFini(); -#endif // __ZKA_USE_FB__ - - // ---------------------------------------------------- // - // Finally load Kernel, and the cr3 to it. - // ---------------------------------------------------- // - - kernel_thread->Start(handover_hdr); - - CANT_REACH(); -} diff --git a/dev/zba/src/HEL/AMD64/BootPlatform.cc b/dev/zba/src/HEL/AMD64/BootPlatform.cc new file mode 100644 index 00000000..70b95734 --- /dev/null +++ b/dev/zba/src/HEL/AMD64/BootPlatform.cc @@ -0,0 +1,106 @@ + +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#include +#include +#include + +#ifdef __STANDALONE__ + +using namespace Boot; + +EXTERN_C void rt_hlt() +{ + asm volatile("hlt"); +} + +EXTERN_C void rt_cli() +{ + asm volatile("cli"); +} + +EXTERN_C void rt_sti() +{ + asm volatile("sti"); +} + +EXTERN_C void rt_cld() +{ + asm volatile("cld"); +} + +EXTERN_C void rt_std() +{ + asm volatile("std"); +} + +EXTERN_C void Out8(UInt16 port, UInt8 value) +{ + asm volatile("outb %%al, %1" + : + : "a"(value), "Nd"(port) + : "memory"); +} + +EXTERN_C void Out16(UInt16 port, UInt16 value) +{ + asm volatile("outw %%ax, %1" + : + : "a"(value), "Nd"(port) + : "memory"); +} + +EXTERN_C void Out32(UInt16 port, UInt32 value) +{ + asm volatile("outl %%eax, %1" + : + : "a"(value), "Nd"(port) + : "memory"); +} + +EXTERN_C UInt8 In8(UInt16 port) +{ + UInt8 value; + asm volatile("inb %1, %%al" + : "=a"(value) + : "Nd"(port) + : "memory"); + + return value; +} + +EXTERN_C UInt16 In16(UInt16 port) +{ + UInt16 value; + asm volatile("inw %%dx, %%ax" + : "=a"(value) + : "d"(port)); + + return value; +} + +EXTERN_C UInt32 In32(UInt16 port) +{ + UInt32 value; + asm volatile("inl %1, %%eax" + : "=a"(value) + : "Nd"(port) + : "memory"); + + return value; +} + +#else + +#include + +void rt_hlt() +{ + Kernel::HAL::rt_halt(); +} + +#endif // __STANDALONE__ diff --git a/dev/zba/src/HEL/AMD64/BootPlatform.cxx b/dev/zba/src/HEL/AMD64/BootPlatform.cxx deleted file mode 100644 index 8aad43ca..00000000 --- a/dev/zba/src/HEL/AMD64/BootPlatform.cxx +++ /dev/null @@ -1,106 +0,0 @@ - -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#include -#include -#include - -#ifdef __STANDALONE__ - -using namespace Boot; - -EXTERN_C void rt_hlt() -{ - asm volatile("hlt"); -} - -EXTERN_C void rt_cli() -{ - asm volatile("cli"); -} - -EXTERN_C void rt_sti() -{ - asm volatile("sti"); -} - -EXTERN_C void rt_cld() -{ - asm volatile("cld"); -} - -EXTERN_C void rt_std() -{ - asm volatile("std"); -} - -EXTERN_C void Out8(UInt16 port, UInt8 value) -{ - asm volatile("outb %%al, %1" - : - : "a"(value), "Nd"(port) - : "memory"); -} - -EXTERN_C void Out16(UInt16 port, UInt16 value) -{ - asm volatile("outw %%ax, %1" - : - : "a"(value), "Nd"(port) - : "memory"); -} - -EXTERN_C void Out32(UInt16 port, UInt32 value) -{ - asm volatile("outl %%eax, %1" - : - : "a"(value), "Nd"(port) - : "memory"); -} - -EXTERN_C UInt8 In8(UInt16 port) -{ - UInt8 value; - asm volatile("inb %1, %%al" - : "=a"(value) - : "Nd"(port) - : "memory"); - - return value; -} - -EXTERN_C UInt16 In16(UInt16 port) -{ - UInt16 value; - asm volatile("inw %%dx, %%ax" - : "=a"(value) - : "d"(port)); - - return value; -} - -EXTERN_C UInt32 In32(UInt16 port) -{ - UInt32 value; - asm volatile("inl %1, %%eax" - : "=a"(value) - : "Nd"(port) - : "memory"); - - return value; -} - -#else - -#include - -void rt_hlt() -{ - Kernel::HAL::rt_halt(); -} - -#endif // __STANDALONE__ diff --git a/dev/zba/src/HEL/ARM64/BootMain.cc b/dev/zba/src/HEL/ARM64/BootMain.cc new file mode 100644 index 00000000..dbdb7d0d --- /dev/null +++ b/dev/zba/src/HEL/ARM64/BootMain.cc @@ -0,0 +1,76 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#include +#include +#include +#include + +#ifndef cExpectedWidth +#define cExpectedWidth 844 +#endif + +#ifndef cExpectedHeight +#define cExpectedHeight 390 +#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 == cExpectedWidth && + infoPtr->VerticalResolution == cExpectedHeight) + { + 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::BThread(reader_kernel.Blob()); + + if (kernel_thread.IsValid()) + kernel_thread.Start(nullptr); + } + + CANT_REACH(); +} diff --git a/dev/zba/src/HEL/ARM64/BootMain.cxx b/dev/zba/src/HEL/ARM64/BootMain.cxx deleted file mode 100644 index 2dc940cd..00000000 --- a/dev/zba/src/HEL/ARM64/BootMain.cxx +++ /dev/null @@ -1,76 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#include -#include -#include -#include - -#ifndef cExpectedWidth -#define cExpectedWidth 844 -#endif - -#ifndef cExpectedHeight -#define cExpectedHeight 390 -#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 == cExpectedWidth && - infoPtr->VerticalResolution == cExpectedHeight) - { - 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::BThread(reader_kernel.Blob()); - - if (kernel_thread.IsValid()) - kernel_thread.Start(nullptr); - } - - CANT_REACH(); -} diff --git a/dev/zba/src/HEL/ARM64/BootPlatform.cc b/dev/zba/src/HEL/ARM64/BootPlatform.cc new file mode 100644 index 00000000..d63893a7 --- /dev/null +++ b/dev/zba/src/HEL/ARM64/BootPlatform.cc @@ -0,0 +1,37 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#include +#include +#include + +#ifdef __STANDALONE__ + +using namespace Boot; + +EXTERN_C void rt_hlt() +{ + while (Yes) + ; +} + +EXTERN_C void rt_cli() +{ +} + +EXTERN_C void rt_sti() +{ +} + +EXTERN_C void rt_cld() +{ +} + +EXTERN_C void rt_std() +{ +} + +#endif // __STANDALONE__ diff --git a/dev/zba/src/HEL/ARM64/BootPlatform.cxx b/dev/zba/src/HEL/ARM64/BootPlatform.cxx deleted file mode 100644 index b8c96a10..00000000 --- a/dev/zba/src/HEL/ARM64/BootPlatform.cxx +++ /dev/null @@ -1,37 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#include -#include -#include - -#ifdef __STANDALONE__ - -using namespace Boot; - -EXTERN_C void rt_hlt() -{ - while (Yes) - ; -} - -EXTERN_C void rt_cli() -{ -} - -EXTERN_C void rt_sti() -{ -} - -EXTERN_C void rt_cld() -{ -} - -EXTERN_C void rt_std() -{ -} - -#endif // __STANDALONE__ diff --git a/dev/zba/src/New+Delete.cc b/dev/zba/src/New+Delete.cc new file mode 100644 index 00000000..075accfa --- /dev/null +++ b/dev/zba/src/New+Delete.cc @@ -0,0 +1,60 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#include +#include +#include + +#ifdef __STANDALONE__ +EXTERN EfiBootServices* BS; + +/// @brief Allocates a new object. +/// @param sz the size. +/// @return +void* operator new(size_t sz) +{ + void* buf = nullptr; + + while (BS->AllocatePool(EfiMemoryType::EfiLoaderData, sz, &buf) == kBufferTooSmall) + BS->FreePool(buf); + + 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) +{ + BS->FreePool(buf); +} + +/// @brief Deletes the object. +/// @param buf the object. +void operator delete[](void* buf) +{ + BS->FreePool(buf); +} + +/// @brief Deletes the object (array specific). +/// @param buf the object. +/// @param size it's size. +void operator delete(void* buf, size_t size) +{ + BS->FreePool(buf); +} + +#endif // __STANDALONE__ diff --git a/dev/zba/src/New+Delete.cxx b/dev/zba/src/New+Delete.cxx deleted file mode 100644 index 932bae69..00000000 --- a/dev/zba/src/New+Delete.cxx +++ /dev/null @@ -1,60 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#include -#include -#include - -#ifdef __STANDALONE__ -EXTERN EfiBootServices* BS; - -/// @brief Allocates a new object. -/// @param sz the size. -/// @return -void* operator new(size_t sz) -{ - void* buf = nullptr; - - while (BS->AllocatePool(EfiMemoryType::EfiLoaderData, sz, &buf) == kBufferTooSmall) - BS->FreePool(buf); - - 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) -{ - BS->FreePool(buf); -} - -/// @brief Deletes the object. -/// @param buf the object. -void operator delete[](void* buf) -{ - BS->FreePool(buf); -} - -/// @brief Deletes the object (array specific). -/// @param buf the object. -/// @param size it's size. -void operator delete(void* buf, size_t size) -{ - BS->FreePool(buf); -} - -#endif // __STANDALONE__ diff --git a/dev/zba/src/Thread.cc b/dev/zba/src/Thread.cc new file mode 100644 index 00000000..6efe3242 --- /dev/null +++ b/dev/zba/src/Thread.cc @@ -0,0 +1,195 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +// External boot services symbol. +EXTERN EfiBootServices* BS; + +/// @note BThread doesn't parse the symbols so doesn't nullify them, .bss is though. + +namespace Boot +{ + BThread::BThread(VoidPtr blob) + : fBlob(blob), fStartAddress(nullptr) + { + // detect the format. + const Char* blob_bytes = reinterpret_cast(fBlob); + + BTextWriter writer; + + if (!blob_bytes) + { + // failed to provide a valid pointer. + return; + } + + if (blob_bytes[0] == kMagMz0 && + blob_bytes[1] == kMagMz1) + { + LDR_EXEC_HEADER_PTR header_ptr = ldr_find_exec_header(blob_bytes); + LDR_OPTIONAL_HEADER_PTR opt_header_ptr = ldr_find_opt_exec_header(blob_bytes); + + if (!header_ptr || !opt_header_ptr) + return; + +#ifdef __ZKA_AMD64__ + if (header_ptr->mMachine != kPeMachineAMD64 || + header_ptr->mSignature != kPeSignature) + { + writer.Write("NEWOSLDR: Not a PE32+ executable.\r"); + return; + } +#elif defined(__ZKA_ARM64__) + if (header_ptr->mMachine != kPeMachineAMD64 || + header_ptr->mSignature != kPeSignature) + { + writer.Write("NEWOSLDR: Not a PE32+ executable.\r"); + return; + } +#endif // __ZKA_AMD64__ || __ZKA_ARM64__ + + if (opt_header_ptr->mSubsystem != kZKASubsystem) + { + writer.Write("NEWOSLDR: Not a ZKA Subsystem executable.\r"); + return; + } + + writer.Write("NEWOSLDR: PE32+ executable detected (ZKA Subsystem).\r"); + + auto numSecs = header_ptr->mNumberOfSections; + + writer.Write("NEWOSLDR: Major Linker Ver: ").Write(opt_header_ptr->mMajorLinkerVersion).Write("\r"); + writer.Write("NEWOSLDR: Minor Linker Ver: ").Write(opt_header_ptr->mMinorLinkerVersion).Write("\r"); + writer.Write("NEWOSLDR: Major Subsystem Ver: ").Write(opt_header_ptr->mMajorSubsystemVersion).Write("\r"); + writer.Write("NEWOSLDR: Minor Subsystem Ver: ").Write(opt_header_ptr->mMinorSubsystemVersion).Write("\r"); + writer.Write("NEWOSLDR: Magic: ").Write(header_ptr->mSignature).Write("\r"); + + constexpr auto cPageSize = 512; + + EfiPhysicalAddress loadStartAddress = opt_header_ptr->mImageBase; + loadStartAddress += opt_header_ptr->mBaseOfData; + + writer.Write("NEWOSLDR: ImageBase: ").Write(loadStartAddress).Write("\r"); + + auto numPages = opt_header_ptr->mSizeOfImage / cPageSize; + BS->AllocatePages(AllocateAddress, EfiLoaderData, numPages, &loadStartAddress); + + LDR_SECTION_HEADER_PTR sectPtr = (LDR_SECTION_HEADER_PTR)(((Char*)opt_header_ptr) + header_ptr->mSizeOfOptionalHeader); + + constexpr auto sectionForCode = ".text"; + constexpr auto sectionForNewLdr = ".ldr"; + constexpr auto sectionForBSS = ".bss"; + + for (SizeT sectIndex = 0; sectIndex < numSecs; ++sectIndex) + { + LDR_SECTION_HEADER_PTR sect = §Ptr[sectIndex]; + + SetMem((VoidPtr)(loadStartAddress + sect->mVirtualAddress), 0, sect->mSizeOfRawData); + + if (StrCmp(sectionForCode, sect->mName) == 0) + { + fStartAddress = (VoidPtr)((UIntPtr)loadStartAddress + opt_header_ptr->mAddressOfEntryPoint); + writer.Write("NEWOSLDR: Executable entry address: ").Write((UIntPtr)fStartAddress).Write("\r"); + } + else if (StrCmp(sectionForNewLdr, sect->mName) == 0) + { + struct HANDOVER_INFORMATION_STUB + { + UInt64 HandoverMagic; + UInt32 HandoverType; + UInt32 HandoverPad; + UInt32 HandoverArch; + }* handover_struc = (struct HANDOVER_INFORMATION_STUB*)((UIntPtr)fBlob + sect->mPointerToRawData); + + if (handover_struc->HandoverMagic != kHandoverMagic && + handover_struc->HandoverType != HEL::kTypeKernel) + { +#ifdef __ZKA_AMD64__ + if (handover_struc->HandoverArch != HEL::kArchAMD64) + { + CGDrawString("NEWOSLDR: NOT AN HANDOVER IMAGE, BAD ARCHITECTURE...", 40, 10, RGB(0xFF, 0xFF, 0xFF)); + ::EFI::Stop(); + } +#endif + +#ifdef __ZKA_ARM64__ + if (handover_struc->HandoverArch != HEL::kArchARM64) + { + CGDrawString("NEWOSLDR: NOT AN HANDOVER IMAGE, BAD ARCHITECTURE...", 40, 10, RGB(0xFF, 0xFF, 0xFF)); + ::EFI::Stop(); + } +#endif + CGDrawString("NEWOSLDR: NOT AN HANDOVER IMAGE...", 40, 10, RGB(0xFF, 0xFF, 0xFF)); + + ::EFI::Stop(); + } + } + + writer.Write("NEWOSLDR: Raw offset: ").Write(sect->mPointerToRawData).Write(" of ").Write(sect->mName).Write("\r"); + + CopyMem((VoidPtr)(loadStartAddress + sect->mVirtualAddress), (VoidPtr)((UIntPtr)fBlob + sect->mPointerToRawData), sect->mSizeOfRawData); + } + } + else if (blob_bytes[0] == kPefMagic[0] && + blob_bytes[1] == kPefMagic[1] && + blob_bytes[2] == kPefMagic[2] && + blob_bytes[3] == kPefMagic[3]) + { + // ========================================= // + // PEF executable detected. + // ========================================= // + + fStartAddress = nullptr; + writer.Write("NEWOSLDR: PEF executable detected, won't load it.\r"); + writer.Write("NEWOSLDR: note: PEF executables aren't loadable by default.\r"); + } + else + { + writer.Write("NEWOSLDR: Invalid executable.\r"); + } + } + + /// @note handover header has to be valid! + Void BThread::Start(HEL::HANDOVER_INFO_HEADER* handover) + { + HEL::HandoverProc err_fn = [](HEL::HANDOVER_INFO_HEADER* rcx) -> void { + CGDrawString("NEWOSLDR: INVALID IMAGE! ABORTING...", 50, 10, RGB(0xFF, 0xFF, 0xFF)); + ::EFI::Stop(); + }; + + if (!fStartAddress) + { + err_fn(handover); + } + + reinterpret_cast(fStartAddress)(handover); + } + + const Char* BThread::GetName() + { + return fBlobName; + } + + Void BThread::SetName(const Char* name) + { + CopyMem(fBlobName, name, StrLen(name)); + } + + bool BThread::IsValid() + { + return fStartAddress != nullptr; + } +} // namespace Boot diff --git a/dev/zba/src/Thread.cxx b/dev/zba/src/Thread.cxx deleted file mode 100644 index 6d527fce..00000000 --- a/dev/zba/src/Thread.cxx +++ /dev/null @@ -1,195 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -// External boot services symbol. -EXTERN EfiBootServices* BS; - -/// @note BThread doesn't parse the symbols so doesn't nullify them, .bss is though. - -namespace Boot -{ - BThread::BThread(VoidPtr blob) - : fBlob(blob), fStartAddress(nullptr) - { - // detect the format. - const Char* blob_bytes = reinterpret_cast(fBlob); - - BTextWriter writer; - - if (!blob_bytes) - { - // failed to provide a valid pointer. - return; - } - - if (blob_bytes[0] == kMagMz0 && - blob_bytes[1] == kMagMz1) - { - LDR_EXEC_HEADER_PTR header_ptr = ldr_find_exec_header(blob_bytes); - LDR_OPTIONAL_HEADER_PTR opt_header_ptr = ldr_find_opt_exec_header(blob_bytes); - - if (!header_ptr || !opt_header_ptr) - return; - -#ifdef __ZKA_AMD64__ - if (header_ptr->mMachine != kPeMachineAMD64 || - header_ptr->mSignature != kPeSignature) - { - writer.Write("NEWOSLDR: Not a PE32+ executable.\r"); - return; - } -#elif defined(__ZKA_ARM64__) - if (header_ptr->mMachine != kPeMachineAMD64 || - header_ptr->mSignature != kPeSignature) - { - writer.Write("NEWOSLDR: Not a PE32+ executable.\r"); - return; - } -#endif // __ZKA_AMD64__ || __ZKA_ARM64__ - - if (opt_header_ptr->mSubsystem != kZKASubsystem) - { - writer.Write("NEWOSLDR: Not a ZKA Subsystem executable.\r"); - return; - } - - writer.Write("NEWOSLDR: PE32+ executable detected (ZKA Subsystem).\r"); - - auto numSecs = header_ptr->mNumberOfSections; - - writer.Write("NEWOSLDR: Major Linker Ver: ").Write(opt_header_ptr->mMajorLinkerVersion).Write("\r"); - writer.Write("NEWOSLDR: Minor Linker Ver: ").Write(opt_header_ptr->mMinorLinkerVersion).Write("\r"); - writer.Write("NEWOSLDR: Major Subsystem Ver: ").Write(opt_header_ptr->mMajorSubsystemVersion).Write("\r"); - writer.Write("NEWOSLDR: Minor Subsystem Ver: ").Write(opt_header_ptr->mMinorSubsystemVersion).Write("\r"); - writer.Write("NEWOSLDR: Magic: ").Write(header_ptr->mSignature).Write("\r"); - - constexpr auto cPageSize = 512; - - EfiPhysicalAddress loadStartAddress = opt_header_ptr->mImageBase; - loadStartAddress += opt_header_ptr->mBaseOfData; - - writer.Write("NEWOSLDR: ImageBase: ").Write(loadStartAddress).Write("\r"); - - auto numPages = opt_header_ptr->mSizeOfImage / cPageSize; - BS->AllocatePages(AllocateAddress, EfiLoaderData, numPages, &loadStartAddress); - - LDR_SECTION_HEADER_PTR sectPtr = (LDR_SECTION_HEADER_PTR)(((Char*)opt_header_ptr) + header_ptr->mSizeOfOptionalHeader); - - constexpr auto sectionForCode = ".text"; - constexpr auto sectionForNewLdr = ".ldr"; - constexpr auto sectionForBSS = ".bss"; - - for (SizeT sectIndex = 0; sectIndex < numSecs; ++sectIndex) - { - LDR_SECTION_HEADER_PTR sect = §Ptr[sectIndex]; - - SetMem((VoidPtr)(loadStartAddress + sect->mVirtualAddress), 0, sect->mSizeOfRawData); - - if (StrCmp(sectionForCode, sect->mName) == 0) - { - fStartAddress = (VoidPtr)((UIntPtr)loadStartAddress + opt_header_ptr->mAddressOfEntryPoint); - writer.Write("NEWOSLDR: Executable entry address: ").Write((UIntPtr)fStartAddress).Write("\r"); - } - else if (StrCmp(sectionForNewLdr, sect->mName) == 0) - { - struct HANDOVER_INFORMATION_STUB - { - UInt64 HandoverMagic; - UInt32 HandoverType; - UInt32 HandoverPad; - UInt32 HandoverArch; - }* handover_struc = (struct HANDOVER_INFORMATION_STUB*)((UIntPtr)fBlob + sect->mPointerToRawData); - - if (handover_struc->HandoverMagic != kHandoverMagic && - handover_struc->HandoverType != HEL::kTypeKernel) - { -#ifdef __ZKA_AMD64__ - if (handover_struc->HandoverArch != HEL::kArchAMD64) - { - CGDrawString("NEWOSLDR: NOT AN HANDOVER IMAGE, BAD ARCHITECTURE...", 40, 10, RGB(0xFF, 0xFF, 0xFF)); - ::EFI::Stop(); - } -#endif - -#ifdef __ZKA_ARM64__ - if (handover_struc->HandoverArch != HEL::kArchARM64) - { - CGDrawString("NEWOSLDR: NOT AN HANDOVER IMAGE, BAD ARCHITECTURE...", 40, 10, RGB(0xFF, 0xFF, 0xFF)); - ::EFI::Stop(); - } -#endif - CGDrawString("NEWOSLDR: NOT AN HANDOVER IMAGE...", 40, 10, RGB(0xFF, 0xFF, 0xFF)); - - ::EFI::Stop(); - } - } - - writer.Write("NEWOSLDR: Raw offset: ").Write(sect->mPointerToRawData).Write(" of ").Write(sect->mName).Write("\r"); - - CopyMem((VoidPtr)(loadStartAddress + sect->mVirtualAddress), (VoidPtr)((UIntPtr)fBlob + sect->mPointerToRawData), sect->mSizeOfRawData); - } - } - else if (blob_bytes[0] == kPefMagic[0] && - blob_bytes[1] == kPefMagic[1] && - blob_bytes[2] == kPefMagic[2] && - blob_bytes[3] == kPefMagic[3]) - { - // ========================================= // - // PEF executable detected. - // ========================================= // - - fStartAddress = nullptr; - writer.Write("NEWOSLDR: PEF executable detected, won't load it.\r"); - writer.Write("NEWOSLDR: note: PEF executables aren't loadable by default.\r"); - } - else - { - writer.Write("NEWOSLDR: Invalid executable.\r"); - } - } - - /// @note handover header has to be valid! - Void BThread::Start(HEL::HANDOVER_INFO_HEADER* handover) - { - HEL::HandoverProc err_fn = [](HEL::HANDOVER_INFO_HEADER* rcx) -> void { - CGDrawString("NEWOSLDR: INVALID IMAGE! ABORTING...", 50, 10, RGB(0xFF, 0xFF, 0xFF)); - ::EFI::Stop(); - }; - - if (!fStartAddress) - { - err_fn(handover); - } - - reinterpret_cast(fStartAddress)(handover); - } - - const Char* BThread::GetName() - { - return fBlobName; - } - - Void BThread::SetName(const Char* name) - { - CopyMem(fBlobName, name, StrLen(name)); - } - - bool BThread::IsValid() - { - return fStartAddress != nullptr; - } -} // namespace Boot -- cgit v1.2.3