diff options
| author | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-05-05 21:10:18 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-05-05 21:10:18 +0200 |
| commit | f95d8bf159d10b5a9521dcaa0bc37aa0e9dfc02b (patch) | |
| tree | bf8186f1a0521a64983bb0bca4f7b54883542195 /Private/NewBoot | |
| parent | 5a903c1d8f80ca8d7bc5fbea0aea710ce0133f9d (diff) | |
MHR-23: Add run_format.sh, kernel patches.
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'Private/NewBoot')
20 files changed, 2212 insertions, 1846 deletions
diff --git a/Private/NewBoot/BootKit/BitManip.hxx b/Private/NewBoot/BootKit/BitManip.hxx index dcb786cf..08733840 100644 --- a/Private/NewBoot/BootKit/BitManip.hxx +++ b/Private/NewBoot/BootKit/BitManip.hxx @@ -10,11 +10,11 @@ /// 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_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/Private/NewBoot/BootKit/BootKit.hxx b/Private/NewBoot/BootKit/BootKit.hxx index faba02fb..2d72c2d2 100644 --- a/Private/NewBoot/BootKit/BootKit.hxx +++ b/Private/NewBoot/BootKit/BootKit.hxx @@ -40,106 +40,111 @@ class BVersionString; using namespace NewOS; -typedef Char *PEFImagePtr; -typedef Char *PEImagePtr; +typedef Char* PEFImagePtr; +typedef Char* PEImagePtr; typedef WideChar CharacterTypeUTF16; -typedef Char CharacterTypeUTF8; +typedef Char CharacterTypeUTF8; -namespace EFI { - extern void ThrowError(const CharacterTypeUTF16 *ErrorCode, - const CharacterTypeUTF16 *Reason) noexcept; +namespace EFI +{ + extern void ThrowError(const CharacterTypeUTF16* ErrorCode, + const CharacterTypeUTF16* Reason) noexcept; } /** * @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); - - public: - explicit BTextWriter() = default; - ~BTextWriter() = default; - - public: - BTextWriter &operator=(const BTextWriter &) = default; - BTextWriter(const BTextWriter &) = default; +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); + +public: + explicit BTextWriter() = default; + ~BTextWriter() = default; + +public: + BTextWriter& operator=(const BTextWriter&) = default; + BTextWriter(const BTextWriter&) = default; }; -NewOS::SizeT BCopyMem(CharacterTypeUTF16 *dest, CharacterTypeUTF16 *src, - const NewOS::SizeT len); +NewOS::SizeT BCopyMem(CharacterTypeUTF16* dest, CharacterTypeUTF16* src, const NewOS::SizeT len); -NewOS::SizeT BSetMem(CharacterTypeUTF8 *src, const CharacterTypeUTF8 byte, - const NewOS::SizeT len); +NewOS::SizeT BSetMem(CharacterTypeUTF8* src, const CharacterTypeUTF8 byte, const NewOS::SizeT len); /// String length functions. /// @brief get string length. -NewOS::SizeT BStrLen(const CharacterTypeUTF16 *ptr); +NewOS::SizeT BStrLen(const CharacterTypeUTF16* ptr); /// @brief set memory with custom value. -NewOS::SizeT BSetMem(CharacterTypeUTF16 *src, const CharacterTypeUTF16 byte, - const NewOS::SizeT len); +NewOS::SizeT BSetMem(CharacterTypeUTF16* src, const CharacterTypeUTF16 byte, const NewOS::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 = 4096); - - enum { - kOperationOkay, - kNotSupported, - kEmptyDirectory, - kNoSuchEntry, - kIsDirectory, - 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}; +class BFileReader final +{ +public: + explicit BFileReader(const CharacterTypeUTF16* path, + EfiHandlePtr ImageHandle); + ~BFileReader(); + +public: + Void ReadAll(SizeT until, SizeT chunk = 4096); + + enum + { + kOperationOkay, + kNotSupported, + kEmptyDirectory, + kNoSuchEntry, + kIsDirectory, + 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}; }; -typedef UInt8 *BlobType; +typedef UInt8* BlobType; -class BVersionString final { - public: - static const CharacterTypeUTF16 *Shared() { return BOOTLOADER_VERSION; } +class BVersionString final +{ +public: + static const CharacterTypeUTF16* Shared() + { + return BOOTLOADER_VERSION; + } }; /***********************************************************************************/ @@ -152,10 +157,10 @@ class BVersionString final { * 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 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); @@ -165,130 +170,139 @@ EXTERN_C void rt_sti(); EXTERN_C void rt_cld(); EXTERN_C void rt_std(); -#endif // __EFI_x86_64__ +#endif // __EFI_x86_64__ -static inline const UInt32 kRgbRed = 0x000000FF; +static inline const UInt32 kRgbRed = 0x000000FF; static inline const UInt32 kRgbGreen = 0x0000FF00; -static inline const UInt32 kRgbBlue = 0x00FF0000; +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" +#define kBKBootDirMime "boot-x/dir" /// @brief BootKit Disk Formatter. template <typename BootDev> -class BDiskFormatFactory final { +class BDiskFormatFactory final +{ public: - /// @brief File entry for **BDiskFormatFactory**. - struct BFileDescriptor final { - Char fFileName[kNewFSNodeNameLen]; - Int32 fKind; - }; + /// @brief File entry for **BDiskFormatFactory**. + struct BFileDescriptor final + { + Char fFileName[kNewFSNodeNameLen]; + Int32 fKind; + }; public: - explicit BDiskFormatFactory() = default; - explicit BDiskFormatFactory(BootDev dev) : fDiskDev(dev) {} + explicit BDiskFormatFactory() = default; + explicit BDiskFormatFactory(BootDev dev) + : fDiskDev(dev) + { + } - ~BDiskFormatFactory() = default; + ~BDiskFormatFactory() = default; - NEWOS_COPY_DELETE(BDiskFormatFactory); + NEWOS_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 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 = (kNewFSAddressAsLba); - fDiskDev.Leak().mSize = BootDev::kSectorSize; + /// @brief check if partition is good. + Bool IsPartitionValid() noexcept + { + fDiskDev.Leak().mBase = (kNewFSAddressAsLba); + fDiskDev.Leak().mSize = BootDev::kSectorSize; - Char buf[BootDev::kSectorSize] = { 0 }; + Char buf[BootDev::kSectorSize] = {0}; - fDiskDev.Read(buf, BootDev::kSectorSize); + fDiskDev.Read(buf, BootDev::kSectorSize); - NewPartitionBlock* blockPart = reinterpret_cast<NewPartitionBlock*>(buf); + NewPartitionBlock* blockPart = reinterpret_cast<NewPartitionBlock*>(buf); - BTextWriter writer; + BTextWriter writer; - for (SizeT indexMag = 0UL; indexMag < kNewFSIdentLen; ++indexMag) { - if (blockPart->Ident[indexMag] != kNewFSIdent[indexMag]) - return false; - } + for (SizeT indexMag = 0UL; indexMag < kNewFSIdentLen; ++indexMag) + { + if (blockPart->Ident[indexMag] != kNewFSIdent[indexMag]) + return false; + } - writer.Write(L"Device Size: ").Write(this->fDiskDev.GetDiskSize()).Write(L"\r"); + writer.Write(L"Device Size: ").Write(this->fDiskDev.GetDiskSize()).Write(L"\r"); - if (blockPart->DiskSize != this->fDiskDev.GetDiskSize() || - blockPart->DiskSize < 1 || - blockPart->SectorSize != BootDev::kSectorSize || - blockPart->Version != kNewFSVersionInteger) { - EFI::ThrowError(L"Invalid-Disk-Geometry", L"Invalid disk geometry."); - } else if (blockPart->PartitionName[0] == 0) { - EFI::ThrowError(L"Invalid-Partition-Name", L"Invalid disk partition."); - } + if (blockPart->DiskSize != this->fDiskDev.GetDiskSize() || + blockPart->DiskSize < 1 || + blockPart->SectorSize != BootDev::kSectorSize || + blockPart->Version != kNewFSVersionInteger) + { + EFI::ThrowError(L"Invalid-Disk-Geometry", L"Invalid disk geometry."); + } + else if (blockPart->PartitionName[0] == 0) + { + EFI::ThrowError(L"Invalid-Partition-Name", L"Invalid disk partition."); + } - writer.Write(L"Device Partition: ").Write(blockPart->PartitionName).Write(L" is healthy.\r"); + writer.Write(L"Device Partition: ").Write(blockPart->PartitionName).Write(L" is healthy.\r"); - return true; - } + 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 NewFS partition block. - Boolean WriteRootCatalog(BFileDescriptor* fileBlobs, SizeT blobCount, - NewPartitionBlock& partBlock) { - if (partBlock.SectorSize != BootDev::kSectorSize) return false; - - BFileDescriptor* blob = fileBlobs; - Lba startLba = partBlock.StartCatalog; - BTextWriter writer; + /// @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 NewFS partition block. + Boolean WriteRootCatalog(BFileDescriptor* fileBlobs, SizeT blobCount, NewPartitionBlock& partBlock) + { + if (partBlock.SectorSize != BootDev::kSectorSize) + return false; - Char bufCatalog[sizeof(NewCatalog)] = { 0 }; + BFileDescriptor* blob = fileBlobs; + Lba startLba = partBlock.StartCatalog; + BTextWriter writer; - constexpr auto cNewFSCatalogPadding = 4; + Char bufCatalog[sizeof(NewCatalog)] = {0}; - NewCatalog* catalogKind = (NewCatalog*)bufCatalog; - catalogKind->PrevSibling = startLba; - catalogKind->NextSibling = (startLba + (sizeof(NewCatalog) * cNewFSCatalogPadding)); + constexpr auto cNewFSCatalogPadding = 4; - /// Fill catalog kind. - catalogKind->Kind = blob->fKind; - catalogKind->Flags = kNewFSFlagCreated; + NewCatalog* catalogKind = (NewCatalog*)bufCatalog; + catalogKind->PrevSibling = startLba; + catalogKind->NextSibling = (startLba + (sizeof(NewCatalog) * cNewFSCatalogPadding)); - /// before going to forks, we must check for the catalog name first. - if (blob->fKind == kNewFSCatalogKindDir && - blob->fFileName[strlen(blob->fFileName) - 1] != '/') { - EFI::ThrowError(L"Developer-Error", L"This is caused by the developer of the bootloader."); - } + /// Fill catalog kind. + catalogKind->Kind = blob->fKind; + catalogKind->Flags = kNewFSFlagCreated; - writer.Write((catalogKind->Kind == kNewFSCatalogKindFile) ? L"New Boot: Write-File: " : - L"New Boot: Write-Directory: " ).Write(blob->fFileName).Write(L"\r"); + /// before going to forks, we must check for the catalog name first. + if (blob->fKind == kNewFSCatalogKindDir && + blob->fFileName[strlen(blob->fFileName) - 1] != '/') + { + EFI::ThrowError(L"Developer-Error", L"This is caused by the developer of the bootloader."); + } + writer.Write((catalogKind->Kind == kNewFSCatalogKindFile) ? L"New Boot: Write-File: " : L"New Boot: Write-Directory: ").Write(blob->fFileName).Write(L"\r"); - memcpy(catalogKind->Name, blob->fFileName, strlen(blob->fFileName)); + memcpy(catalogKind->Name, blob->fFileName, strlen(blob->fFileName)); - fDiskDev.Leak().mBase = startLba; - fDiskDev.Leak().mSize = sizeof(NewCatalog); + fDiskDev.Leak().mBase = startLba; + fDiskDev.Leak().mSize = sizeof(NewCatalog); - fDiskDev.Write((Char*)bufCatalog, sizeof(NewCatalog)); + fDiskDev.Write((Char*)bufCatalog, sizeof(NewCatalog)); - --partBlock.FreeCatalog; - --partBlock.FreeSectors; + --partBlock.FreeCatalog; + --partBlock.FreeSectors; - memset(bufCatalog, 0, sizeof(NewCatalog)); + memset(bufCatalog, 0, sizeof(NewCatalog)); - return true; - } + return true; + } private: - BootDev fDiskDev; - + BootDev fDiskDev; }; /// @brief Format disk. @@ -298,55 +312,62 @@ private: /// @retval True disk has been formatted. /// @retval False failed to format. template <typename BootDev> -inline Boolean BDiskFormatFactory<BootDev>::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; - Char buf[BootDev::kSectorSize] = { 0 }; - - NewPartitionBlock* partBlock = reinterpret_cast<NewPartitionBlock*>(buf); - - memcpy(partBlock->Ident, kNewFSIdent, kNewFSIdentLen - 1); - memcpy(partBlock->PartitionName, partName, strlen(partName)); - - /// @note A catalog roughly equal to a sector. - - constexpr auto cMinimumDiskSize = 10; // at minimum. - - /// @note also look at EPM headers, for free part blocks. - - if (GIB(fDiskDev.GetDiskSize()) < cMinimumDiskSize) { - EFI::ThrowError(L"Disk-Too-Tiny", L"Disk can't contain a New Filesystem partition."); - return false; - } - - partBlock->Version = kNewFSVersionInteger; - partBlock->CatalogCount = blobCount; - partBlock->Kind = kNewFSHardDrive; - partBlock->SectorSize = sectorSz; - partBlock->FreeCatalog = fDiskDev.GetSectorsCount() / sizeof(NewCatalog); - partBlock->SectorCount = fDiskDev.GetSectorsCount(); - partBlock->FreeSectors = fDiskDev.GetSectorsCount(); - partBlock->StartCatalog = kNewFSCatalogStartAddress; - partBlock->DiskSize = fDiskDev.GetDiskSize(); - partBlock->Flags |= kNewFSPartitionTypeBoot; - - /// if we can write a root catalog, then write the partition block. - if (this->WriteRootCatalog(fileBlobs, blobCount, *partBlock)) { - fDiskDev.Leak().mBase = kNewFSAddressAsLba; - fDiskDev.Leak().mSize = sectorSz; - - fDiskDev.Write(buf, sectorSz); - - BTextWriter writer; - writer.Write(L"New Boot: Write-Partition, OK.\r"); - - return true; - } else { - EFI::ThrowError(L"Filesystem-Failure-Part", L"Filesystem couldn't be partitioned."); - } - - return false; +inline Boolean BDiskFormatFactory<BootDev>::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; + Char buf[BootDev::kSectorSize] = {0}; + + NewPartitionBlock* partBlock = reinterpret_cast<NewPartitionBlock*>(buf); + + memcpy(partBlock->Ident, kNewFSIdent, kNewFSIdentLen - 1); + memcpy(partBlock->PartitionName, partName, strlen(partName)); + + /// @note A catalog roughly equal to a sector. + + constexpr auto cMinimumDiskSize = 10; // at minimum. + + /// @note also look at EPM headers, for free part blocks. + + if (GIB(fDiskDev.GetDiskSize()) < cMinimumDiskSize) + { + EFI::ThrowError(L"Disk-Too-Tiny", L"Disk can't contain a New Filesystem partition."); + return false; + } + + partBlock->Version = kNewFSVersionInteger; + partBlock->CatalogCount = blobCount; + partBlock->Kind = kNewFSHardDrive; + partBlock->SectorSize = sectorSz; + partBlock->FreeCatalog = fDiskDev.GetSectorsCount() / sizeof(NewCatalog); + partBlock->SectorCount = fDiskDev.GetSectorsCount(); + partBlock->FreeSectors = fDiskDev.GetSectorsCount(); + partBlock->StartCatalog = kNewFSCatalogStartAddress; + partBlock->DiskSize = fDiskDev.GetDiskSize(); + partBlock->Flags |= kNewFSPartitionTypeBoot; + + /// if we can write a root catalog, then write the partition block. + if (this->WriteRootCatalog(fileBlobs, blobCount, *partBlock)) + { + fDiskDev.Leak().mBase = kNewFSAddressAsLba; + fDiskDev.Leak().mSize = sectorSz; + + fDiskDev.Write(buf, sectorSz); + + BTextWriter writer; + writer.Write(L"New Boot: Write-Partition, OK.\r"); + + return true; + } + else + { + EFI::ThrowError(L"Filesystem-Failure-Part", L"Filesystem couldn't be partitioned."); + } + + return false; } diff --git a/Private/NewBoot/BootKit/Device.hxx b/Private/NewBoot/BootKit/Device.hxx index 45bc7e69..37c617b5 100644 --- a/Private/NewBoot/BootKit/Device.hxx +++ b/Private/NewBoot/BootKit/Device.hxx @@ -11,22 +11,24 @@ using namespace NewOS; /// @brief Device type. -class Device { - public: - Device() = default; - virtual ~Device() = default; +class Device +{ +public: + Device() = default; + virtual ~Device() = default; - NEWOS_MOVE_DEFAULT(Device); + NEWOS_MOVE_DEFAULT(Device); - struct Trait { - SizeT mBase{1024}; - SizeT mSize{1024}; -}; + struct Trait + { + SizeT mBase{1024}; + SizeT mSize{1024}; + }; - virtual Trait& Leak() = 0; + virtual Trait& Leak() = 0; - virtual Device& Read(Char* Buf, const SizeT& SecCount) = 0; - virtual Device& Write(Char* Buf, const SizeT& SecCount) = 0; + virtual Device& Read(Char* Buf, const SizeT& SecCount) = 0; + virtual Device& Write(Char* Buf, const SizeT& SecCount) = 0; }; typedef Device BootDevice; diff --git a/Private/NewBoot/BootKit/HW/ATA.hxx b/Private/NewBoot/BootKit/HW/ATA.hxx index 5bdcec56..6b61f7b2 100644 --- a/Private/NewBoot/BootKit/HW/ATA.hxx +++ b/Private/NewBoot/BootKit/HW/ATA.hxx @@ -11,39 +11,48 @@ using namespace NewOS; -class BootDeviceATA final : public Device { - public: - enum { - kPrimary = ATA_PRIMARY_IO, - kSecondary = ATA_SECONDARY_IO, - }; - - explicit BootDeviceATA() noexcept; - ~BootDeviceATA() = default; - - NEWOS_COPY_DEFAULT(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; +class BootDeviceATA final : public Device +{ +public: + enum + { + kPrimary = ATA_PRIMARY_IO, + kSecondary = ATA_SECONDARY_IO, + }; + + explicit BootDeviceATA() noexcept; + ~BootDeviceATA() = default; + + NEWOS_COPY_DEFAULT(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/Private/NewBoot/BootKit/HW/SATA.hxx b/Private/NewBoot/BootKit/HW/SATA.hxx index 24c08c01..171adecb 100644 --- a/Private/NewBoot/BootKit/HW/SATA.hxx +++ b/Private/NewBoot/BootKit/HW/SATA.hxx @@ -9,30 +9,38 @@ #include <CompilerKit/CompilerKit.hxx> #include <Builtins/AHCI/AHCI.hxx> -class BootDeviceSATA final { - public: - explicit BootDeviceSATA() noexcept; - ~BootDeviceSATA() = default; - - NEWOS_COPY_DEFAULT(BootDeviceSATA); - - struct SATATrait final { - NewOS::SizeT mBase{1024}; - NewOS::Boolean mErr{false}; - NewOS::Boolean mDetected{false}; - - operator bool() { return !this->mErr; } - }; - - operator bool() { return this->Leak().mDetected; } - - BootDeviceSATA& Read(NewOS::WideChar* Buf, const NewOS::SizeT& SecCount); - BootDeviceSATA& Write(NewOS::WideChar* Buf, const NewOS::SizeT& SecCount); - - SATATrait& Leak(); - - private: - SATATrait mTrait; +class BootDeviceSATA final +{ +public: + explicit BootDeviceSATA() noexcept; + ~BootDeviceSATA() = default; + + NEWOS_COPY_DEFAULT(BootDeviceSATA); + + struct SATATrait final + { + NewOS::SizeT mBase{1024}; + NewOS::Boolean mErr{false}; + NewOS::Boolean mDetected{false}; + + operator bool() + { + return !this->mErr; + } + }; + + operator bool() + { + return this->Leak().mDetected; + } + + BootDeviceSATA& Read(NewOS::WideChar* Buf, const NewOS::SizeT& SecCount); + BootDeviceSATA& Write(NewOS::WideChar* Buf, const NewOS::SizeT& SecCount); + + SATATrait& Leak(); + +private: + SATATrait mTrait; }; #define kAHCISectorSz 4096 diff --git a/Private/NewBoot/BootKit/Vendor/Qr.hxx b/Private/NewBoot/BootKit/Vendor/Qr.hxx index 982af6b5..1df0bdaf 100644 --- a/Private/NewBoot/BootKit/Vendor/Qr.hxx +++ b/Private/NewBoot/BootKit/Vendor/Qr.hxx @@ -13,817 +13,967 @@ #include <BootKit/Vendor/Support.hxx> #include <CompilerKit/Detail.hxx> -namespace qr { -inline uint8_t min_poly = - 0b11101, /* Minimal polynomial x^8 + x^4 + x^3 + x^2 + 1 */ - generator = 0b10; /* Generator of Galois field */ - -/// @brief galois finite field multiplication. -inline uint8_t gf_mul(uint8_t a, uint8_t b) { - uint8_t res = 0; - - for (; b; b >>= 1) { - if (b & 1) res ^= a; - if (a & 0x80) - a = (a << 1) ^ min_poly; - else - a <<= 1; - } - - return res; -} - -// Size of Ecc block with respect to level and version. 0 version is for -// padding. -constexpr int ECC_CODEWORDS_PER_BLOCK[4][41] = { - {0, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, - 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, - 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, - {0, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, - 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, - 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28}, - {0, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, - 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, - 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, - {0, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, - 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, - 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, -}; - -// Number of Ecc blocks with respect to level and version. 0 version is for -// padding. -constexpr int N_ECC_BLOCKS[4][41] = { - {0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, - 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, - 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25}, - {0, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, - 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, - 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49}, - {0, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, - 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, - 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68}, - {0, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, - 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, - 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81}, -}; - -// Positions of central modules of alignment patterns according to version. 0 -// version is for padding. -constexpr int ALIGN_POS[41][7] = { - {}, - {0}, - {6, 18}, - {6, 22}, - {6, 26}, - {6, 30}, - {6, 34}, - {6, 22, 38}, - {6, 24, 42}, - {6, 26, 46}, - {6, 28, 50}, - {6, 30, 54}, - {6, 32, 58}, - {6, 34, 62}, - {6, 26, 46, 66}, - {6, 26, 48, 70}, - {6, 26, 50, 74}, - {6, 30, 54, 78}, - {6, 30, 56, 82}, - {6, 30, 58, 86}, - {6, 34, 62, 90}, - {6, 28, 50, 72, 94}, - {6, 26, 50, 74, 98}, - {6, 30, 54, 78, 102}, - {6, 28, 54, 80, 106}, - {6, 32, 58, 84, 110}, - {6, 30, 58, 86, 114}, - {6, 34, 62, 90, 118}, - {6, 26, 50, 74, 98, 122}, - {6, 30, 54, 78, 102, 126}, - {6, 26, 52, 78, 104, 130}, - {6, 30, 56, 82, 108, 134}, - {6, 34, 60, 86, 112, 138}, - {6, 30, 58, 86, 114, 142}, - {6, 34, 62, 90, 118, 146}, - {6, 30, 54, 78, 102, 126, 150}, - {6, 24, 50, 76, 102, 128, 154}, - {6, 28, 54, 80, 106, 132, 158}, - {6, 32, 58, 84, 110, 136, 162}, - {6, 26, 54, 82, 110, 138, 166}, - {6, 30, 58, 86, 114, 142, 170}, -}; - -// Return n-th bit of arr starting from MSB. -constexpr uint8_t get_bit_r(uint8_t *arr, int n) { - return (arr[n >> 3] >> (7 - (n & 7))) & 1; -} - -// Add up to 16 bits to arr. Data starts from MSB as well as each byte of an -// array. -constexpr void add_bits(uint16_t data, int n, uint8_t *arr, size_t &pos) { - while (n--) { - arr[pos >> 3] |= ((data >> n) & 1) << (7 - (pos & 7)); - ++pos; - } -} - -// Translate char to alphanumeric encoding value, -constexpr int alphanumeric(char c) { - if (c >= '0' && c <= '9') return c - '0'; - - if (c >= 'A' && c <= 'Z') return c - 'A' + 10; - - switch (c) { - case ' ': - return 36; - case '$': - return 37; - case '%': - return 38; - case '*': - return 39; - case '+': - return 40; - case '-': - return 41; - case '.': - return 42; - case '/': - return 43; - case ':': - return 44; - } - return -1; -} - -// Check if string can be encoded in alphanumeric mode. -constexpr bool is_alphanumeric(const char *str, size_t len) { - for (size_t i = 0; i < len; ++i) - if (alphanumeric(str[i]) == -1) return false; - return true; -} - -// Check if string can be encoded in numeric mode. -constexpr bool is_numeric(const char *str, size_t len) { - for (size_t i = 0; i < len; ++i) - if (str[i] < '0' || str[i] > '9') return false; - return true; -} - -// Check if string can be encoded in kanji mode. -constexpr bool is_kanji(const char *str, size_t len) { - for (size_t i = 0; i < len; i += 2) { - uint16_t val = uint16_t(str[i]) | (uint16_t(str[i + 1]) << 8); - if (val < 0x8140 || val > 0xebbf || (val > 0x9ffc && val < 0xe040)) - return false; - } - return true; -} - -// Reed-Solomon Ecc generator polynomial for the given degree. -constexpr void gf_gen_poly(int degree, uint8_t *poly) { - SetMem(poly, 0, degree); - - uint8_t root = poly[degree - 1] = 1; - - for (int i = 0; i < degree; ++i) { - for (int j = 0; j < degree - 1; ++j) - poly[j] = gf_mul(poly[j], root) ^ poly[j + 1]; - poly[degree - 1] = gf_mul(poly[degree - 1], root); - root = (root << 1) ^ ((root >> 7) * 0x11d); - } -} - -// Polynomial division if Galois Field. -constexpr void gf_poly_div(uint8_t *dividend, size_t len, uint8_t *divisor, - int degree, uint8_t *result) { - SetMem(result, 0, degree); - - for (size_t i = 0; i < len; ++i) { - uint8_t factor = dividend[i] ^ result[0]; - MoveMem(&result[0], &result[1], degree - 1); - result[degree - 1] = 0; - for (int j = 0; j < degree; ++j) result[j] ^= gf_mul(divisor[j], factor); - } -} - -enum Ecc { - L, - M, - Q, - H, -}; - -enum Mode { - M_NUMERIC, - M_ALPHANUMERIC, - M_BYTE, - M_KANJI, -}; - -// Select appropriate encoding mode for string. -constexpr Mode select_mode(const char *str, size_t len) { - if (is_numeric(str, len)) return M_NUMERIC; - if (is_alphanumeric(str, len)) return M_ALPHANUMERIC; - if (is_kanji(str, len)) return M_KANJI; - return M_BYTE; -} - -// Return size of Character Control Indicator in bits for given version and -// mode. -constexpr int cci(int ver, Mode mode) { - constexpr int cnt[4][3] = { - {10, 12, 14}, - {9, 11, 13}, - {8, 16, 16}, - {8, 10, 12}, - }; - if (ver < 10) return cnt[mode][0]; - if (ver < 27) return cnt[mode][1]; - return cnt[mode][2]; -} - -template <int V> -struct Qr { -private: - friend class QrDelegate; - bool draw(int x, int y) noexcept; - -public: - constexpr auto side_size() const { return SIDE; } - constexpr auto data_size() const { return N_BITS; } - constexpr auto data_ptr() const { return code; } - - bool module(int x, int y); - bool encode(const char *str, size_t len, Ecc ecc, int mask = -1); - - private: - bool encode_data(const char *data, size_t len, Ecc ecc, uint8_t *out); - void encode_ecc(uint8_t *data, Ecc ecc, uint8_t *out); - - void add_data(uint8_t *data, uint8_t *patterns); - void add_patterns(); - void add_version(); - void add_format(Ecc ecc, int mask); - void reserve_patterns(uint8_t *out); - - template <bool Black> - void draw_rect(int y, int x, int height, int width, uint8_t *out); - template <bool Black> - void draw_bound(int y, int x, int height, int width, uint8_t *out); - - template <bool Horizontal> - int rule_1_3_score(); - int penalty_score(); - int select_mask(Ecc ecc, uint8_t *patterns); - void apply_mask(int mask, uint8_t *patterns); - - private: - static_assert(V >= 1 && V <= 40, "invalid version"); - static constexpr int SIDE = 17 + V * 4; - static constexpr int N_BITS = SIDE * SIDE; - static constexpr int N_ALIGN = V == 1 ? 0 : V / 7 + 2; - static constexpr int N_ALIGN_BITS = V > 1 ? (N_ALIGN *N_ALIGN - 3) * 25 : 0; - static constexpr int N_TIMING_BITS = - (SIDE - 16) * 2 - (10 * (V > 1 ? N_ALIGN - 2 : 0)); - static constexpr int N_VER_BITS = V > 6 ? 36 : 0; - static constexpr int N_DAT_BITS = - N_BITS - (192 + N_ALIGN_BITS + N_TIMING_BITS + 31 + N_VER_BITS); - static constexpr int N_BYTES = utl::bytes_in_bits(N_BITS); // Actual number of bytes_in_bits - // required to store whole Qr code - static constexpr int N_DAT_BYTES = utl::bytes_in_bits(N_DAT_BITS); // Actual number of bytes_in_bits required to store - // [data + ecc] - static constexpr int N_DAT_CAPACITY = - N_DAT_BITS >> 3; // Capacity of [data + ecc] without remainder bits - private: - /// @brief internal function to retrieve bit from a bitset. - uint8_t get_arr_bit(uint8_t *arr, unsigned bit) const { - return utl::get_arr_bit(arr, bit); - } - - /// @brief internal function to set bit from a bitset. - void set_arr_bit(uint8_t *arr, unsigned bit) { - utl::set_arr_bit(arr, bit); - } - - /// @brief internal function to clear bit from a bitset. - void clr_arr_bit(uint8_t *arr, unsigned bit) { - utl::clr_arr_bit(arr, bit); - } - - uint8_t code[N_BYTES] = {}; - - bool status = false; -}; - -// Get color of a module from left-to-right and top-to-bottom. Black is true. -template <int V> -bool Qr<V>::module(int x, int y) { - return get_arr_bit(code, y * SIDE + x); -} - -/// @brief draw a new QR code. -template <int V> -bool Qr<V>::draw(int whereX, int whereY) noexcept { - if (!this->status) return false; // it may be invalid. - - for (int y = 0; y < (this->side_size()); ++y) { - for (int x = 0; x < (this->side_size()); ++x) { - ToolboxDrawZone( - (this->module(x, y) ? RGB(00, 00, 00) : RGB(FF, FF, FF)), - 1, 1, - x + whereX, y + whereY); - } - } - - return false; -} - -// Create Qr code with given error correction level. If mask == -1, -// then best mask selected automatically. NOTE: Automatic mask is the -// most expensive operation. Takes about 95 % of all computation time. -template <int V> -bool Qr<V>::encode(const char *str, size_t len, Ecc ecc, int mask) { - uint8_t data[N_DAT_BYTES] = {}; - uint8_t data_with_ecc[N_DAT_BYTES] = {}; - uint8_t patterns[N_BYTES] = {}; - - if (!encode_data(str, len, ecc, data)) { - return status = false; - } - - encode_ecc(data, ecc, data_with_ecc); - - reserve_patterns(patterns); - CopyMem(code, patterns, N_BYTES); - - add_data(data_with_ecc, patterns); - add_patterns(); - add_version(); - - mask = mask != -1 ? mask & 7 : select_mask(ecc, patterns); - - add_format(ecc, mask); - apply_mask(mask, patterns); - - return status = true; -} - -template <int V> -bool Qr<V>::encode_data(const char *data, size_t len, Ecc ecc, uint8_t *out) { - Mode mode = select_mode(data, len); - - size_t n_bits = - (N_DAT_CAPACITY - ECC_CODEWORDS_PER_BLOCK[ecc][V] * N_ECC_BLOCKS[ecc][V]) - << 3; - size_t pos = 0; - - add_bits(1 << mode, 4, out, pos); - add_bits(len, cci(V, mode), out, pos); - - if (mode == M_NUMERIC) { - const size_t triplets = len / 3; - const size_t triplets_size = triplets * 3; - const size_t rem = len % 3; - const size_t rem_bits = rem == 2 ? 7 : rem == 1 ? 4 : 0; - const size_t total_size = 10 * triplets + rem_bits; - - if (pos + total_size > n_bits) return false; - - char buf[4] = {}; - - for (size_t i = 0; i < triplets_size; i += 3) { - buf[0] = data[i]; - buf[1] = data[i + 1]; - buf[2] = data[i + 2]; - - uint16_t num = StringToLong(buf, NULL, 10); - add_bits(num, 10, out, pos); - } - - if (rem) { - buf[0] = data[triplets_size]; - buf[1] = data[triplets_size + 1]; - buf[rem] = 0; - - uint16_t num = StringToLong(buf, NULL, 10); - add_bits(num, rem_bits, out, pos); - } - } else if (mode == M_ALPHANUMERIC) { - if (pos + 11 * (int(len & ~1ul) / 2) > n_bits) return false; - - for (int i = 0; i < int(len & ~1ul); i += 2) { - uint16_t num = alphanumeric(data[i]) * 45 + alphanumeric(data[i + 1]); - add_bits(num, 11, out, pos); - } - if (len & 1) { - if (pos + 6 > n_bits) return false; - - add_bits(alphanumeric(data[len - 1]), 6, out, pos); - } - - } else if (mode == M_BYTE) { - if (pos + len * 8 > n_bits) return false; - - for (size_t i = 0; i < len; ++i) add_bits(data[i], 8, out, pos); - - } else { - if (pos + 13 * (len / 2) > n_bits) return false; - - for (size_t i = 0; i < len; i += 2) { - uint16_t val = ((uint8_t)data[i]) | (((uint8_t)data[i + 1]) << 8); - uint16_t res = 0; - val -= val < 0x9FFC ? 0x8140 : 0xC140; - res += val & 0xff; - res += (val >> 8) * 0xc0; - add_bits(res, 13, out, pos); - } - } - - size_t padding = n_bits - pos; - size_t i = 0; - - add_bits(0, padding > 4 ? 4 : padding, out, pos); - - if (pos & 7) add_bits(0, (8 - pos) & 7, out, pos); - - while (pos < n_bits) add_bits(++i & 1 ? 0xec : 0x11, 8, out, pos); - - return true; -} - -template <int V> -void Qr<V>::encode_ecc(uint8_t *data, Ecc ecc, uint8_t *out) { - int n_blocks = N_ECC_BLOCKS[ecc][V]; - int ecc_len = ECC_CODEWORDS_PER_BLOCK[ecc][V]; - - int n_data_bytes = N_DAT_CAPACITY - ecc_len * n_blocks; - - int n_short_blocks = n_blocks - N_DAT_CAPACITY % n_blocks; - int short_len = N_DAT_CAPACITY / n_blocks - ecc_len; - - uint8_t gen_poly[30]; - uint8_t ecc_buf[30]; - - gf_gen_poly(ecc_len, gen_poly); - - uint8_t *data_ptr = data; - - for (int i = 0; i < n_blocks; ++i) { - int data_len = short_len; - - if (i >= n_short_blocks) ++data_len; - - gf_poly_div(data_ptr, data_len, gen_poly, ecc_len, ecc_buf); - - for (int j = 0, k = i; j < data_len; ++j, k += n_blocks) { - if (j == short_len) k -= n_short_blocks; - out[k] = data_ptr[j]; - } - for (int j = 0, k = n_data_bytes + i; j < ecc_len; ++j, k += n_blocks) - out[k] = ecc_buf[j]; - - data_ptr += data_len; - } -} - -template <int V> -void Qr<V>::add_data(uint8_t *data, uint8_t *patterns) { - int data_pos = 0; - - for (int x = SIDE - 1; x >= 1; x -= 2) { - if (x == 6) x = 5; - - for (int i = 0; i < SIDE; ++i) { - int y = !((x + 1) & 2) ? SIDE - 1 - i : i; - int coord = y * SIDE + x; - - if (!get_arr_bit(patterns, coord)) { - if (get_bit_r(data, data_pos)) set_arr_bit(code, coord); - - ++data_pos; - } - - if (!get_arr_bit(patterns, coord - 1)) { - if (get_bit_r(data, data_pos)) set_arr_bit(code, coord - 1); - - ++data_pos; - } - } - } -} - -template <int V> -void Qr<V>::add_patterns() { - // White bounds inside finders - draw_bound<false>(1, 1, 5, 5, code); - draw_bound<false>(1, SIDE - 6, 5, 5, code); - draw_bound<false>(SIDE - 6, 1, 5, 5, code); - - // Finish alignment patterns - for (int i = 0; i < N_ALIGN; ++i) { - for (int j = 0; j < N_ALIGN; ++j) { - if ((!i && !j) || (!i && j == N_ALIGN - 1) || (!j && i == N_ALIGN - 1)) - continue; - draw_bound<false>(ALIGN_POS[V][i] - 1, ALIGN_POS[V][j] - 1, 3, 3, code); - } - } - - // Draw white separators - draw_rect<false>(7, 0, 1, 8, code); - draw_rect<false>(0, 7, 8, 1, code); - draw_rect<false>(SIDE - 8, 0, 1, 8, code); - draw_rect<false>(SIDE - 8, 7, 8, 1, code); - draw_rect<false>(7, SIDE - 8, 1, 8, code); - draw_rect<false>(0, SIDE - 8, 8, 1, code); - - // Perforate timing patterns - for (int i = 7; i < SIDE - 7; i += 2) { - clr_arr_bit(code, 6 * SIDE + i); - clr_arr_bit(code, i * SIDE + 6); - } -} - -template <int V> -void Qr<V>::add_version() { - if (V < 7) return; - - uint32_t rem = V; - - for (uint8_t i = 0; i < 12; ++i) rem = (rem << 1) ^ ((rem >> 11) * 0x1F25); - - uint32_t data = V << 12 | rem; - - for (int x = 0; x < 6; ++x) { - for (int j = 0; j < 3; ++j) { - int y = SIDE - 11 + j; - - bool black = (data >> (x * 3 + j)) & 1; - - if (!black) { - clr_arr_bit(code, y * SIDE + x); - clr_arr_bit(code, y + SIDE * x); - } - } - } -} - -template <int V> -void Qr<V>::add_format(Ecc ecc, int mask) { - int data = (ecc ^ 1) << 3 | mask; - int rem = data; - - for (int i = 0; i < 10; i++) rem = (rem << 1) ^ ((rem >> 9) * 0b10100110111); - - int res = (data << 10 | rem) ^ 0b101010000010010; - - for (int i = 0; i < 6; ++i) { - if ((res >> i) & 1) { - set_arr_bit(code, SIDE * 8 + SIDE - 1 - i); - set_arr_bit(code, SIDE * i + 8); - } else { - clr_arr_bit(code, SIDE * 8 + SIDE - 1 - i); - clr_arr_bit(code, SIDE * i + 8); - } - } - - for (int i = 6; i < 8; ++i) { - if ((res >> i) & 1) { - set_arr_bit(code, SIDE * 8 + SIDE - 1 - i); - set_arr_bit(code, SIDE * (i + 1) + 8); - } else { - clr_arr_bit(code, SIDE * 8 + SIDE - 1 - i); - clr_arr_bit(code, SIDE * (i + 1) + 8); - } - } - - if ((res >> 8) & 1) { - set_arr_bit(code, SIDE * 8 + 7); - set_arr_bit(code, SIDE * (SIDE - 7) + 8); - } else { - clr_arr_bit(code, SIDE * 8 + 7); - clr_arr_bit(code, SIDE * (SIDE - 7) + 8); - } - - for (int i = 9, j = 5; i < 15; ++i, --j) { - if ((res >> i) & 1) { - set_arr_bit(code, SIDE * 8 + j); - set_arr_bit(code, SIDE * (SIDE - 1 - j) + 8); - } else { - clr_arr_bit(code, SIDE * 8 + j); - clr_arr_bit(code, SIDE * (SIDE - 1 - j) + 8); - } - } -} - -template <int V> -template <bool B> -void Qr<V>::draw_rect(int y, int x, int height, int width, uint8_t *out) { - if (B) { - for (int dy = y * SIDE; dy < (y + height) * SIDE; dy += SIDE) - for (int dx = x; dx < x + width; ++dx) set_arr_bit(out, dy + dx); - } else { - for (int dy = y * SIDE; dy < (y + height) * SIDE; dy += SIDE) - for (int dx = x; dx < x + width; ++dx) clr_arr_bit(out, dy + dx); - } -} - -template <int V> -template <bool B> -void Qr<V>::draw_bound(int y, int x, int height, int width, uint8_t *out) { - if (B) { - for (int i = y * SIDE + x; i < y * SIDE + x + width; ++i) - set_arr_bit(out, i); - for (int i = (y + height - 1) * SIDE + x; - i < (y + height - 1) * SIDE + x + width; ++i) - set_arr_bit(out, i); - for (int i = (y + 1) * SIDE + x; i < (y + height - 1) * SIDE + x; i += SIDE) - set_arr_bit(out, i); - for (int i = (y + 1) * SIDE + x + width - 1; - i < (y + height - 1) * SIDE + x + width - 1; i += SIDE) - set_arr_bit(out, i); - } else { - for (int i = y * SIDE + x; i < y * SIDE + x + width; ++i) - clr_arr_bit(out, i); - for (int i = (y + height - 1) * SIDE + x; - i < (y + height - 1) * SIDE + x + width; ++i) - clr_arr_bit(out, i); - for (int i = (y + 1) * SIDE + x; i < (y + height - 1) * SIDE + x; i += SIDE) - clr_arr_bit(out, i); - for (int i = (y + 1) * SIDE + x + width - 1; - i < (y + height - 1) * SIDE + x + width - 1; i += SIDE) - clr_arr_bit(out, i); - } -} - -template <int V> -void Qr<V>::reserve_patterns(uint8_t *out) { - draw_rect<true>(0, 6, SIDE, 1, out); - draw_rect<true>(6, 0, 1, SIDE, out); - - draw_rect<true>(0, 0, 9, 9, out); - draw_rect<true>(SIDE - 8, 0, 8, 9, out); - draw_rect<true>(0, SIDE - 8, 9, 8, out); - - for (int i = 0; i < N_ALIGN; ++i) { - for (int j = 0; j < N_ALIGN; ++j) { - if ((!i && !j) || (!i && j == N_ALIGN - 1) || (!j && i == N_ALIGN - 1)) - continue; - draw_rect<true>(ALIGN_POS[V][i] - 2, ALIGN_POS[V][j] - 2, 5, 5, out); - } - } - - if (V >= 7) { - draw_rect<true>(SIDE - 11, 0, 3, 6, out); - draw_rect<true>(0, SIDE - 11, 6, 3, out); - } -} - -template <int V> -template <bool H> -int Qr<V>::rule_1_3_score() { - constexpr int y_max = H ? N_BITS : SIDE; - constexpr int x_max = H ? SIDE : N_BITS; - constexpr int y_step = H ? SIDE : 1; - constexpr int x_step = H ? 1 : SIDE; - - int res = 0; - - for (int y = 0; y < y_max; y += y_step) { - bool color = get_arr_bit(code, y); - int finder = color; - int cnt = 1; - for (int x = 1; x < x_max; x += x_step) { - if (get_arr_bit(code, y + x) == color) { - ++cnt; - if (cnt == 5) res += 3; - if (cnt > 5) ++res; - } else { - color = !color; - cnt = 1; - } - // Finder-like - finder = ((finder << 1) & 0x7ff) | color; - if (x >= x_step * 10) { - if (finder == 0x05d || finder == 0x5d0) res += 40; - } - } - } - return res; -} - -template <int V> -int Qr<V>::penalty_score() { - int res = 0; - - res += rule_1_3_score<true>(); - res += rule_1_3_score<false>(); - - for (int y = 0; y < N_BITS - SIDE; y += SIDE) { - for (int x = 0; x < SIDE - 1; ++x) { - bool c = get_arr_bit(code, y + x); - - if (c == get_arr_bit(code, y + x + 1) && - c == get_arr_bit(code, y + x + SIDE) && - c == get_arr_bit(code, y + x + SIDE + 1)) - res += 3; - } - } - - int black = 0; - for (int y = 0; y < N_BITS; y += SIDE) { - for (int x = 0; x < SIDE; ++x) black += get_arr_bit(code, y + x); - } - res += abs((black * 100) / N_BITS - 50) / 5 * 10; - - return res; -} - -template <int V> -int Qr<V>::select_mask(Ecc ecc, uint8_t *patterns) { - unsigned min_score = -1; - unsigned score = 0; - uint8_t mask = 0; - - for (int i = 0; i < 8; ++i) { - add_format(ecc, i); - apply_mask(i, patterns); - score = penalty_score(); - if (score < min_score) { - mask = i; - min_score = score; - } - apply_mask(i, patterns); - } - return mask; -} - -template <int V> -void Qr<V>::apply_mask(int mask, uint8_t *patterns) { - for (int y = 0, dy = 0; y < SIDE; ++y, dy += SIDE) { - for (int x = 0; x < SIDE; ++x) { - int coord = dy + x; - - if (get_arr_bit(patterns, coord)) continue; - - bool keep = true; - - switch (mask) { - case 0: - keep = (x + y) & 1; - break; - case 1: - keep = y & 1; - break; - case 2: - keep = x % 3; - break; - case 3: - keep = (x + y) % 3; - break; - case 4: - keep = (y / 2 + x / 3) & 1; - break; - case 5: - keep = x * y % 2 + x * y % 3; - break; - case 6: - keep = (x * y % 2 + x * y % 3) & 1; - break; - case 7: - keep = ((x + y) % 2 + x * y % 3) & 1; - break; - } - - if (!keep) { - if (get_arr_bit(code, coord)) - clr_arr_bit(code, coord); - else - set_arr_bit(code, coord); - } - } - } -} - -/// @brief QR code encoder class. -class QrDelegate final { -public: - explicit QrDelegate() = default; - ~QrDelegate() = default; - - NEWOS_COPY_DEFAULT(QrDelegate); - - /// @brief Draw method delegate. - template <int V> - bool draw(Qr<V>& subject, int x, int y) noexcept { - return subject.draw(x, y); - } - -}; -} // namespace qr - -namespace NewOS::Qr { - using namespace qr; -} // namespace NewOS +namespace qr +{ + inline uint8_t min_poly = + 0b11101, /* Minimal polynomial x^8 + x^4 + x^3 + x^2 + 1 */ + generator = 0b10; /* Generator of Galois field */ + + /// @brief galois finite field multiplication. + inline uint8_t gf_mul(uint8_t a, uint8_t b) + { + uint8_t res = 0; + + for (; b; b >>= 1) + { + if (b & 1) + res ^= a; + if (a & 0x80) + a = (a << 1) ^ min_poly; + else + a <<= 1; + } + + return res; + } + + // Size of Ecc block with respect to level and version. 0 version is for + // padding. + constexpr int ECC_CODEWORDS_PER_BLOCK[4][41] = { + {0, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, + 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, + {0, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, + 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, + 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28}, + {0, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, + 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, + {0, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, + 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, + }; + + // Number of Ecc blocks with respect to level and version. 0 version is for + // padding. + constexpr int N_ECC_BLOCKS[4][41] = { + {0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, + 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, + 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25}, + {0, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, + 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, + 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49}, + {0, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, + 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, + 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68}, + {0, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, + 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, + 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81}, + }; + + // Positions of central modules of alignment patterns according to version. 0 + // version is for padding. + constexpr int ALIGN_POS[41][7] = { + {}, + {0}, + {6, 18}, + {6, 22}, + {6, 26}, + {6, 30}, + {6, 34}, + {6, 22, 38}, + {6, 24, 42}, + {6, 26, 46}, + {6, 28, 50}, + {6, 30, 54}, + {6, 32, 58}, + {6, 34, 62}, + {6, 26, 46, 66}, + {6, 26, 48, 70}, + {6, 26, 50, 74}, + {6, 30, 54, 78}, + {6, 30, 56, 82}, + {6, 30, 58, 86}, + {6, 34, 62, 90}, + {6, 28, 50, 72, 94}, + {6, 26, 50, 74, 98}, + {6, 30, 54, 78, 102}, + {6, 28, 54, 80, 106}, + {6, 32, 58, 84, 110}, + {6, 30, 58, 86, 114}, + {6, 34, 62, 90, 118}, + {6, 26, 50, 74, 98, 122}, + {6, 30, 54, 78, 102, 126}, + {6, 26, 52, 78, 104, 130}, + {6, 30, 56, 82, 108, 134}, + {6, 34, 60, 86, 112, 138}, + {6, 30, 58, 86, 114, 142}, + {6, 34, 62, 90, 118, 146}, + {6, 30, 54, 78, 102, 126, 150}, + {6, 24, 50, 76, 102, 128, 154}, + {6, 28, 54, 80, 106, 132, 158}, + {6, 32, 58, 84, 110, 136, 162}, + {6, 26, 54, 82, 110, 138, 166}, + {6, 30, 58, 86, 114, 142, 170}, + }; + + // Return n-th bit of arr starting from MSB. + constexpr uint8_t get_bit_r(uint8_t* arr, int n) + { + return (arr[n >> 3] >> (7 - (n & 7))) & 1; + } + + // Add up to 16 bits to arr. Data starts from MSB as well as each byte of an + // array. + constexpr void add_bits(uint16_t data, int n, uint8_t* arr, size_t& pos) + { + while (n--) + { + arr[pos >> 3] |= ((data >> n) & 1) << (7 - (pos & 7)); + ++pos; + } + } + + // Translate char to alphanumeric encoding value, + constexpr int alphanumeric(char c) + { + if (c >= '0' && c <= '9') + return c - '0'; + + if (c >= 'A' && c <= 'Z') + return c - 'A' + 10; + + switch (c) + { + case ' ': + return 36; + case '$': + return 37; + case '%': + return 38; + case '*': + return 39; + case '+': + return 40; + case '-': + return 41; + case '.': + return 42; + case '/': + return 43; + case ':': + return 44; + } + return -1; + } + + // Check if string can be encoded in alphanumeric mode. + constexpr bool is_alphanumeric(const char* str, size_t len) + { + for (size_t i = 0; i < len; ++i) + if (alphanumeric(str[i]) == -1) + return false; + return true; + } + + // Check if string can be encoded in numeric mode. + constexpr bool is_numeric(const char* str, size_t len) + { + for (size_t i = 0; i < len; ++i) + if (str[i] < '0' || str[i] > '9') + return false; + return true; + } + + // Check if string can be encoded in kanji mode. + constexpr bool is_kanji(const char* str, size_t len) + { + for (size_t i = 0; i < len; i += 2) + { + uint16_t val = uint16_t(str[i]) | (uint16_t(str[i + 1]) << 8); + if (val < 0x8140 || val > 0xebbf || (val > 0x9ffc && val < 0xe040)) + return false; + } + return true; + } + + // Reed-Solomon Ecc generator polynomial for the given degree. + constexpr void gf_gen_poly(int degree, uint8_t* poly) + { + SetMem(poly, 0, degree); + + uint8_t root = poly[degree - 1] = 1; + + for (int i = 0; i < degree; ++i) + { + for (int j = 0; j < degree - 1; ++j) + poly[j] = gf_mul(poly[j], root) ^ poly[j + 1]; + poly[degree - 1] = gf_mul(poly[degree - 1], root); + root = (root << 1) ^ ((root >> 7) * 0x11d); + } + } + + // Polynomial division if Galois Field. + constexpr void gf_poly_div(uint8_t* dividend, size_t len, uint8_t* divisor, int degree, uint8_t* result) + { + SetMem(result, 0, degree); + + for (size_t i = 0; i < len; ++i) + { + uint8_t factor = dividend[i] ^ result[0]; + MoveMem(&result[0], &result[1], degree - 1); + result[degree - 1] = 0; + for (int j = 0; j < degree; ++j) + result[j] ^= gf_mul(divisor[j], factor); + } + } + + enum Ecc + { + L, + M, + Q, + H, + }; + + enum Mode + { + M_NUMERIC, + M_ALPHANUMERIC, + M_BYTE, + M_KANJI, + }; + + // Select appropriate encoding mode for string. + constexpr Mode select_mode(const char* str, size_t len) + { + if (is_numeric(str, len)) + return M_NUMERIC; + if (is_alphanumeric(str, len)) + return M_ALPHANUMERIC; + if (is_kanji(str, len)) + return M_KANJI; + return M_BYTE; + } + + // Return size of Character Control Indicator in bits for given version and + // mode. + constexpr int cci(int ver, Mode mode) + { + constexpr int cnt[4][3] = { + {10, 12, 14}, + {9, 11, 13}, + {8, 16, 16}, + {8, 10, 12}, + }; + if (ver < 10) + return cnt[mode][0]; + if (ver < 27) + return cnt[mode][1]; + return cnt[mode][2]; + } + + template <int V> + struct Qr + { + private: + friend class QrDelegate; + bool draw(int x, int y) noexcept; + + public: + constexpr auto side_size() const + { + return SIDE; + } + constexpr auto data_size() const + { + return N_BITS; + } + constexpr auto data_ptr() const + { + return code; + } + + bool module(int x, int y); + bool encode(const char* str, size_t len, Ecc ecc, int mask = -1); + + private: + bool encode_data(const char* data, size_t len, Ecc ecc, uint8_t* out); + void encode_ecc(uint8_t* data, Ecc ecc, uint8_t* out); + + void add_data(uint8_t* data, uint8_t* patterns); + void add_patterns(); + void add_version(); + void add_format(Ecc ecc, int mask); + void reserve_patterns(uint8_t* out); + + template <bool Black> + void draw_rect(int y, int x, int height, int width, uint8_t* out); + template <bool Black> + void draw_bound(int y, int x, int height, int width, uint8_t* out); + + template <bool Horizontal> + int rule_1_3_score(); + int penalty_score(); + int select_mask(Ecc ecc, uint8_t* patterns); + void apply_mask(int mask, uint8_t* patterns); + + private: + static_assert(V >= 1 && V <= 40, "invalid version"); + static constexpr int SIDE = 17 + V * 4; + static constexpr int N_BITS = SIDE * SIDE; + static constexpr int N_ALIGN = V == 1 ? 0 : V / 7 + 2; + static constexpr int N_ALIGN_BITS = V > 1 ? (N_ALIGN* N_ALIGN - 3) * 25 : 0; + static constexpr int N_TIMING_BITS = + (SIDE - 16) * 2 - (10 * (V > 1 ? N_ALIGN - 2 : 0)); + static constexpr int N_VER_BITS = V > 6 ? 36 : 0; + static constexpr int N_DAT_BITS = + N_BITS - (192 + N_ALIGN_BITS + N_TIMING_BITS + 31 + N_VER_BITS); + static constexpr int N_BYTES = utl::bytes_in_bits(N_BITS); // Actual number of bytes_in_bits + // required to store whole Qr code + static constexpr int N_DAT_BYTES = utl::bytes_in_bits(N_DAT_BITS); // Actual number of bytes_in_bits required to store + // [data + ecc] + static constexpr int N_DAT_CAPACITY = + N_DAT_BITS >> 3; // Capacity of [data + ecc] without remainder bits + private: + /// @brief internal function to retrieve bit from a bitset. + uint8_t get_arr_bit(uint8_t* arr, unsigned bit) const + { + return utl::get_arr_bit(arr, bit); + } + + /// @brief internal function to set bit from a bitset. + void set_arr_bit(uint8_t* arr, unsigned bit) + { + utl::set_arr_bit(arr, bit); + } + + /// @brief internal function to clear bit from a bitset. + void clr_arr_bit(uint8_t* arr, unsigned bit) + { + utl::clr_arr_bit(arr, bit); + } + + uint8_t code[N_BYTES] = {}; + + bool status = false; + }; + + // Get color of a module from left-to-right and top-to-bottom. Black is true. + template <int V> + bool Qr<V>::module(int x, int y) + { + return get_arr_bit(code, y * SIDE + x); + } + + /// @brief draw a new QR code. + template <int V> + bool Qr<V>::draw(int whereX, int whereY) noexcept + { + if (!this->status) + return false; // it may be invalid. + + for (int y = 0; y < (this->side_size()); ++y) + { + for (int x = 0; x < (this->side_size()); ++x) + { + ToolboxDrawZone( + (this->module(x, y) ? RGB(00, 00, 00) : RGB(FF, FF, FF)), + 1, 1, + x + whereX, y + whereY); + } + } + + return false; + } + + // Create Qr code with given error correction level. If mask == -1, + // then best mask selected automatically. NOTE: Automatic mask is the + // most expensive operation. Takes about 95 % of all computation time. + template <int V> + bool Qr<V>::encode(const char* str, size_t len, Ecc ecc, int mask) + { + uint8_t data[N_DAT_BYTES] = {}; + uint8_t data_with_ecc[N_DAT_BYTES] = {}; + uint8_t patterns[N_BYTES] = {}; + + if (!encode_data(str, len, ecc, data)) + { + return status = false; + } + + encode_ecc(data, ecc, data_with_ecc); + + reserve_patterns(patterns); + CopyMem(code, patterns, N_BYTES); + + add_data(data_with_ecc, patterns); + add_patterns(); + add_version(); + + mask = mask != -1 ? mask & 7 : select_mask(ecc, patterns); + + add_format(ecc, mask); + apply_mask(mask, patterns); + + return status = true; + } + + template <int V> + bool Qr<V>::encode_data(const char* data, size_t len, Ecc ecc, uint8_t* out) + { + Mode mode = select_mode(data, len); + + size_t n_bits = + (N_DAT_CAPACITY - ECC_CODEWORDS_PER_BLOCK[ecc][V] * N_ECC_BLOCKS[ecc][V]) + << 3; + size_t pos = 0; + + add_bits(1 << mode, 4, out, pos); + add_bits(len, cci(V, mode), out, pos); + + if (mode == M_NUMERIC) + { + const size_t triplets = len / 3; + const size_t triplets_size = triplets * 3; + const size_t rem = len % 3; + const size_t rem_bits = rem == 2 ? 7 : rem == 1 ? 4 + : 0; + const size_t total_size = 10 * triplets + rem_bits; + + if (pos + total_size > n_bits) + return false; + + char buf[4] = {}; + + for (size_t i = 0; i < triplets_size; i += 3) + { + buf[0] = data[i]; + buf[1] = data[i + 1]; + buf[2] = data[i + 2]; + + uint16_t num = StringToLong(buf, NULL, 10); + add_bits(num, 10, out, pos); + } + + if (rem) + { + buf[0] = data[triplets_size]; + buf[1] = data[triplets_size + 1]; + buf[rem] = 0; + + uint16_t num = StringToLong(buf, NULL, 10); + add_bits(num, rem_bits, out, pos); + } + } + else if (mode == M_ALPHANUMERIC) + { + if (pos + 11 * (int(len & ~1ul) / 2) > n_bits) + return false; + + for (int i = 0; i < int(len & ~1ul); i += 2) + { + uint16_t num = alphanumeric(data[i]) * 45 + alphanumeric(data[i + 1]); + add_bits(num, 11, out, pos); + } + if (len & 1) + { + if (pos + 6 > n_bits) + return false; + + add_bits(alphanumeric(data[len - 1]), 6, out, pos); + } + } + else if (mode == M_BYTE) + { + if (pos + len * 8 > n_bits) + return false; + + for (size_t i = 0; i < len; ++i) + add_bits(data[i], 8, out, pos); + } + else + { + if (pos + 13 * (len / 2) > n_bits) + return false; + + for (size_t i = 0; i < len; i += 2) + { + uint16_t val = ((uint8_t)data[i]) | (((uint8_t)data[i + 1]) << 8); + uint16_t res = 0; + val -= val < 0x9FFC ? 0x8140 : 0xC140; + res += val & 0xff; + res += (val >> 8) * 0xc0; + add_bits(res, 13, out, pos); + } + } + + size_t padding = n_bits - pos; + size_t i = 0; + + add_bits(0, padding > 4 ? 4 : padding, out, pos); + + if (pos & 7) + add_bits(0, (8 - pos) & 7, out, pos); + + while (pos < n_bits) + add_bits(++i & 1 ? 0xec : 0x11, 8, out, pos); + + return true; + } + + template <int V> + void Qr<V>::encode_ecc(uint8_t* data, Ecc ecc, uint8_t* out) + { + int n_blocks = N_ECC_BLOCKS[ecc][V]; + int ecc_len = ECC_CODEWORDS_PER_BLOCK[ecc][V]; + + int n_data_bytes = N_DAT_CAPACITY - ecc_len * n_blocks; + + int n_short_blocks = n_blocks - N_DAT_CAPACITY % n_blocks; + int short_len = N_DAT_CAPACITY / n_blocks - ecc_len; + + uint8_t gen_poly[30]; + uint8_t ecc_buf[30]; + + gf_gen_poly(ecc_len, gen_poly); + + uint8_t* data_ptr = data; + + for (int i = 0; i < n_blocks; ++i) + { + int data_len = short_len; + + if (i >= n_short_blocks) + ++data_len; + + gf_poly_div(data_ptr, data_len, gen_poly, ecc_len, ecc_buf); + + for (int j = 0, k = i; j < data_len; ++j, k += n_blocks) + { + if (j == short_len) + k -= n_short_blocks; + out[k] = data_ptr[j]; + } + for (int j = 0, k = n_data_bytes + i; j < ecc_len; ++j, k += n_blocks) + out[k] = ecc_buf[j]; + + data_ptr += data_len; + } + } + + template <int V> + void Qr<V>::add_data(uint8_t* data, uint8_t* patterns) + { + int data_pos = 0; + + for (int x = SIDE - 1; x >= 1; x -= 2) + { + if (x == 6) + x = 5; + + for (int i = 0; i < SIDE; ++i) + { + int y = !((x + 1) & 2) ? SIDE - 1 - i : i; + int coord = y * SIDE + x; + + if (!get_arr_bit(patterns, coord)) + { + if (get_bit_r(data, data_pos)) + set_arr_bit(code, coord); + + ++data_pos; + } + + if (!get_arr_bit(patterns, coord - 1)) + { + if (get_bit_r(data, data_pos)) + set_arr_bit(code, coord - 1); + + ++data_pos; + } + } + } + } + + template <int V> + void Qr<V>::add_patterns() + { + // White bounds inside finders + draw_bound<false>(1, 1, 5, 5, code); + draw_bound<false>(1, SIDE - 6, 5, 5, code); + draw_bound<false>(SIDE - 6, 1, 5, 5, code); + + // Finish alignment patterns + for (int i = 0; i < N_ALIGN; ++i) + { + for (int j = 0; j < N_ALIGN; ++j) + { + if ((!i && !j) || (!i && j == N_ALIGN - 1) || (!j && i == N_ALIGN - 1)) + continue; + draw_bound<false>(ALIGN_POS[V][i] - 1, ALIGN_POS[V][j] - 1, 3, 3, code); + } + } + + // Draw white separators + draw_rect<false>(7, 0, 1, 8, code); + draw_rect<false>(0, 7, 8, 1, code); + draw_rect<false>(SIDE - 8, 0, 1, 8, code); + draw_rect<false>(SIDE - 8, 7, 8, 1, code); + draw_rect<false>(7, SIDE - 8, 1, 8, code); + draw_rect<false>(0, SIDE - 8, 8, 1, code); + + // Perforate timing patterns + for (int i = 7; i < SIDE - 7; i += 2) + { + clr_arr_bit(code, 6 * SIDE + i); + clr_arr_bit(code, i * SIDE + 6); + } + } + + template <int V> + void Qr<V>::add_version() + { + if (V < 7) + return; + + uint32_t rem = V; + + for (uint8_t i = 0; i < 12; ++i) + rem = (rem << 1) ^ ((rem >> 11) * 0x1F25); + + uint32_t data = V << 12 | rem; + + for (int x = 0; x < 6; ++x) + { + for (int j = 0; j < 3; ++j) + { + int y = SIDE - 11 + j; + + bool black = (data >> (x * 3 + j)) & 1; + + if (!black) + { + clr_arr_bit(code, y * SIDE + x); + clr_arr_bit(code, y + SIDE * x); + } + } + } + } + + template <int V> + void Qr<V>::add_format(Ecc ecc, int mask) + { + int data = (ecc ^ 1) << 3 | mask; + int rem = data; + + for (int i = 0; i < 10; i++) + rem = (rem << 1) ^ ((rem >> 9) * 0b10100110111); + + int res = (data << 10 | rem) ^ 0b101010000010010; + + for (int i = 0; i < 6; ++i) + { + if ((res >> i) & 1) + { + set_arr_bit(code, SIDE * 8 + SIDE - 1 - i); + set_arr_bit(code, SIDE * i + 8); + } + else + { + clr_arr_bit(code, SIDE * 8 + SIDE - 1 - i); + clr_arr_bit(code, SIDE * i + 8); + } + } + + for (int i = 6; i < 8; ++i) + { + if ((res >> i) & 1) + { + set_arr_bit(code, SIDE * 8 + SIDE - 1 - i); + set_arr_bit(code, SIDE * (i + 1) + 8); + } + else + { + clr_arr_bit(code, SIDE * 8 + SIDE - 1 - i); + clr_arr_bit(code, SIDE * (i + 1) + 8); + } + } + + if ((res >> 8) & 1) + { + set_arr_bit(code, SIDE * 8 + 7); + set_arr_bit(code, SIDE * (SIDE - 7) + 8); + } + else + { + clr_arr_bit(code, SIDE * 8 + 7); + clr_arr_bit(code, SIDE * (SIDE - 7) + 8); + } + + for (int i = 9, j = 5; i < 15; ++i, --j) + { + if ((res >> i) & 1) + { + set_arr_bit(code, SIDE * 8 + j); + set_arr_bit(code, SIDE * (SIDE - 1 - j) + 8); + } + else + { + clr_arr_bit(code, SIDE * 8 + j); + clr_arr_bit(code, SIDE * (SIDE - 1 - j) + 8); + } + } + } + + template <int V> + template <bool B> + void Qr<V>::draw_rect(int y, int x, int height, int width, uint8_t* out) + { + if (B) + { + for (int dy = y * SIDE; dy < (y + height) * SIDE; dy += SIDE) + for (int dx = x; dx < x + width; ++dx) + set_arr_bit(out, dy + dx); + } + else + { + for (int dy = y * SIDE; dy < (y + height) * SIDE; dy += SIDE) + for (int dx = x; dx < x + width; ++dx) + clr_arr_bit(out, dy + dx); + } + } + + template <int V> + template <bool B> + void Qr<V>::draw_bound(int y, int x, int height, int width, uint8_t* out) + { + if (B) + { + for (int i = y * SIDE + x; i < y * SIDE + x + width; ++i) + set_arr_bit(out, i); + for (int i = (y + height - 1) * SIDE + x; + i < (y + height - 1) * SIDE + x + width; ++i) + set_arr_bit(out, i); + for (int i = (y + 1) * SIDE + x; i < (y + height - 1) * SIDE + x; i += SIDE) + set_arr_bit(out, i); + for (int i = (y + 1) * SIDE + x + width - 1; + i < (y + height - 1) * SIDE + x + width - 1; i += SIDE) + set_arr_bit(out, i); + } + else + { + for (int i = y * SIDE + x; i < y * SIDE + x + width; ++i) + clr_arr_bit(out, i); + for (int i = (y + height - 1) * SIDE + x; + i < (y + height - 1) * SIDE + x + width; ++i) + clr_arr_bit(out, i); + for (int i = (y + 1) * SIDE + x; i < (y + height - 1) * SIDE + x; i += SIDE) + clr_arr_bit(out, i); + for (int i = (y + 1) * SIDE + x + width - 1; + i < (y + height - 1) * SIDE + x + width - 1; i += SIDE) + clr_arr_bit(out, i); + } + } + + template <int V> + void Qr<V>::reserve_patterns(uint8_t* out) + { + draw_rect<true>(0, 6, SIDE, 1, out); + draw_rect<true>(6, 0, 1, SIDE, out); + + draw_rect<true>(0, 0, 9, 9, out); + draw_rect<true>(SIDE - 8, 0, 8, 9, out); + draw_rect<true>(0, SIDE - 8, 9, 8, out); + + for (int i = 0; i < N_ALIGN; ++i) + { + for (int j = 0; j < N_ALIGN; ++j) + { + if ((!i && !j) || (!i && j == N_ALIGN - 1) || (!j && i == N_ALIGN - 1)) + continue; + draw_rect<true>(ALIGN_POS[V][i] - 2, ALIGN_POS[V][j] - 2, 5, 5, out); + } + } + + if (V >= 7) + { + draw_rect<true>(SIDE - 11, 0, 3, 6, out); + draw_rect<true>(0, SIDE - 11, 6, 3, out); + } + } + + template <int V> + template <bool H> + int Qr<V>::rule_1_3_score() + { + constexpr int y_max = H ? N_BITS : SIDE; + constexpr int x_max = H ? SIDE : N_BITS; + constexpr int y_step = H ? SIDE : 1; + constexpr int x_step = H ? 1 : SIDE; + + int res = 0; + + for (int y = 0; y < y_max; y += y_step) + { + bool color = get_arr_bit(code, y); + int finder = color; + int cnt = 1; + for (int x = 1; x < x_max; x += x_step) + { + if (get_arr_bit(code, y + x) == color) + { + ++cnt; + if (cnt == 5) + res += 3; + if (cnt > 5) + ++res; + } + else + { + color = !color; + cnt = 1; + } + // Finder-like + finder = ((finder << 1) & 0x7ff) | color; + if (x >= x_step * 10) + { + if (finder == 0x05d || finder == 0x5d0) + res += 40; + } + } + } + return res; + } + + template <int V> + int Qr<V>::penalty_score() + { + int res = 0; + + res += rule_1_3_score<true>(); + res += rule_1_3_score<false>(); + + for (int y = 0; y < N_BITS - SIDE; y += SIDE) + { + for (int x = 0; x < SIDE - 1; ++x) + { + bool c = get_arr_bit(code, y + x); + + if (c == get_arr_bit(code, y + x + 1) && + c == get_arr_bit(code, y + x + SIDE) && + c == get_arr_bit(code, y + x + SIDE + 1)) + res += 3; + } + } + + int black = 0; + for (int y = 0; y < N_BITS; y += SIDE) + { + for (int x = 0; x < SIDE; ++x) + black += get_arr_bit(code, y + x); + } + res += abs((black * 100) / N_BITS - 50) / 5 * 10; + + return res; + } + + template <int V> + int Qr<V>::select_mask(Ecc ecc, uint8_t* patterns) + { + unsigned min_score = -1; + unsigned score = 0; + uint8_t mask = 0; + + for (int i = 0; i < 8; ++i) + { + add_format(ecc, i); + apply_mask(i, patterns); + score = penalty_score(); + if (score < min_score) + { + mask = i; + min_score = score; + } + apply_mask(i, patterns); + } + return mask; + } + + template <int V> + void Qr<V>::apply_mask(int mask, uint8_t* patterns) + { + for (int y = 0, dy = 0; y < SIDE; ++y, dy += SIDE) + { + for (int x = 0; x < SIDE; ++x) + { + int coord = dy + x; + + if (get_arr_bit(patterns, coord)) + continue; + + bool keep = true; + + switch (mask) + { + case 0: + keep = (x + y) & 1; + break; + case 1: + keep = y & 1; + break; + case 2: + keep = x % 3; + break; + case 3: + keep = (x + y) % 3; + break; + case 4: + keep = (y / 2 + x / 3) & 1; + break; + case 5: + keep = x * y % 2 + x * y % 3; + break; + case 6: + keep = (x * y % 2 + x * y % 3) & 1; + break; + case 7: + keep = ((x + y) % 2 + x * y % 3) & 1; + break; + } + + if (!keep) + { + if (get_arr_bit(code, coord)) + clr_arr_bit(code, coord); + else + set_arr_bit(code, coord); + } + } + } + } + + /// @brief QR code encoder class. + class QrDelegate final + { + public: + explicit QrDelegate() = default; + ~QrDelegate() = default; + + NEWOS_COPY_DEFAULT(QrDelegate); + + /// @brief Draw method delegate. + template <int V> + bool draw(Qr<V>& subject, int x, int y) noexcept + { + return subject.draw(x, y); + } + }; +} // namespace qr + +namespace NewOS::Qr +{ + using namespace qr; +} // namespace NewOS::Qr #endif diff --git a/Private/NewBoot/BootKit/Vendor/QrVendor/base.h b/Private/NewBoot/BootKit/Vendor/QrVendor/base.h index d8261d1e..a98ae4f0 100644 --- a/Private/NewBoot/BootKit/Vendor/QrVendor/base.h +++ b/Private/NewBoot/BootKit/Vendor/QrVendor/base.h @@ -5,21 +5,22 @@ #include <cstddef> #include <cassert> -namespace utl { +namespace utl +{ -/** + /** * @brief Helper to get number of elements in array. * * @tparam T Auto-deduced element type * @tparam N Auto-deduced number of elements * @return Array size */ -template<class T, size_t N> -constexpr size_t countof(T(&)[N]) -{ - return N; -} + template <class T, size_t N> + constexpr size_t countof(T (&)[N]) + { + return N; + } -} +} // namespace utl #endif
\ No newline at end of file diff --git a/Private/NewBoot/BootKit/Vendor/QrVendor/bit.h b/Private/NewBoot/BootKit/Vendor/QrVendor/bit.h index 646151b6..94ab0bf2 100644 --- a/Private/NewBoot/BootKit/Vendor/QrVendor/bit.h +++ b/Private/NewBoot/BootKit/Vendor/QrVendor/bit.h @@ -3,33 +3,34 @@ #include <bit> -namespace utl { +namespace utl +{ -/** + /** * @brief Size of object in terms of bits. * * @tparam T Object type * @return Number of bits */ -template<class T> -constexpr auto bit_size() -{ - return sizeof(T) * 8; -} + template <class T> + constexpr auto bit_size() + { + return sizeof(T) * 8; + } -/** + /** * @brief Integer with all bits set to 1. * * @tparam T Integer type * @return All set integer */ -template<class T> -constexpr T bit_full() -{ - return T(-1); -} + template <class T> + constexpr T bit_full() + { + return T(-1); + } -/** + /** * @brief Wrap around mask for power of two number of bits * within given integer type. For example: * [ bit_wrap<uint8_t> = 8 - 1 = 0b111 ] @@ -39,13 +40,13 @@ constexpr T bit_full() * @tparam T Integer type * @return Wrap around mask for number of bits */ -template<class T> -constexpr T bit_wrap() -{ - return bit_size<T>() - 1; -} + template <class T> + constexpr T bit_wrap() + { + return bit_size<T>() - 1; + } -/** + /** * @brief Number of bits to fit bit_wrap<T> result, in other words * bit width of (sizeof(T) * 8 - 1). For example: * [ bit_shft<uint8_t> = bit_width(0b111) = 3 ] @@ -55,13 +56,13 @@ constexpr T bit_wrap() * @tparam T Integer type * @return Number of bits to shift to divide by sizeof(T) * 8 */ -template<class T> -constexpr auto bit_shft() -{ - return std::bit_width(bit_wrap<T>()); -} + template <class T> + constexpr auto bit_shft() + { + return std::bit_width(bit_wrap<T>()); + } -/** + /** * @brief Round up division by number of bits within given integer type, * which sizeof(T) * 8 is power of two. * @@ -69,78 +70,78 @@ constexpr auto bit_shft() * @param x Dividend * @return Quotient */ -template<class T> -constexpr auto bit_ceil(auto x) -{ - return (x + bit_wrap<T>()) >> bit_shft<T>(); -} + template <class T> + constexpr auto bit_ceil(auto x) + { + return (x + bit_wrap<T>()) >> bit_shft<T>(); + } -/** + /** * @brief Count leading zeros. * * @param x Unsigned integer argument * @return Number of leading zeros */ -constexpr unsigned cntlz(auto x) -{ - if constexpr (std::is_same_v<decltype(x), int>) - return std::countl_zero(unsigned(x)); - else - return std::countl_zero(x); -} + constexpr unsigned cntlz(auto x) + { + if constexpr (std::is_same_v<decltype(x), int>) + return std::countl_zero(unsigned(x)); + else + return std::countl_zero(x); + } -/** + /** * @brief Count trailing zeros. * * @param x Unsigned integer argument * @return Number of trailing zeros */ -constexpr unsigned cnttz(auto x) -{ - if constexpr (std::is_same_v<decltype(x), int>) - return std::countr_zero(unsigned(x)); - else - return std::countr_zero(x); -} + constexpr unsigned cnttz(auto x) + { + if constexpr (std::is_same_v<decltype(x), int>) + return std::countr_zero(unsigned(x)); + else + return std::countr_zero(x); + } -/** + /** * @brief Get number of words (integers) required to store N bits. * * @tparam T Word integer type * @param n Number of bits to store * @return Number of words */ -template<class T> -constexpr size_t words_in_bits(size_t n) -{ - return (n >> bit_shft<T>()) + !!(n & bit_wrap<T>()); -} + template <class T> + constexpr size_t words_in_bits(size_t n) + { + return (n >> bit_shft<T>()) + !!(n & bit_wrap<T>()); + } -/** + /** * @brief Get number of bytes required to store N bits. * * @param n Number of bits to store * @return Number of bytes */ -constexpr size_t bytes_in_bits(size_t n) -{ - return words_in_bits<uint8_t>(n); -} + constexpr size_t bytes_in_bits(size_t n) + { + return words_in_bits<uint8_t>(n); + } -/** + /** * @brief Make integer with bit at given position. * * @tparam T Inetegr type * @param n Bit position * @return Integer with set bit */ -template<class T = unsigned> -constexpr T bit(int n) -{ - return T(1) << n; -} + template <class T = unsigned> + constexpr T bit(int n) + { + return T(1) << n; + } -/** + /** * @brief Get n-th bit of an integer. * * @tparam T Integer type @@ -148,39 +149,39 @@ constexpr T bit(int n) * @param n Bit position from LSB * @return true if set */ -template<class T> -constexpr bool get_bit(T x, int n) -{ - return (x >> n) & 1; -} + template <class T> + constexpr bool get_bit(T x, int n) + { + return (x >> n) & 1; + } -/** + /** * @brief Set n-th bit of an integer. * * @tparam T Integer type * @param x Integer * @param n Bit position from LSB */ -template<class T> -constexpr void set_bit(T& x, int n) -{ - x |= 1 << n; -} + template <class T> + constexpr void set_bit(T& x, int n) + { + x |= 1 << n; + } -/** + /** * @brief Clear n-th bit of an integer. * * @tparam T Integer type * @param x Integer * @param n Bit position from LSB */ -template<class T> -constexpr void clr_bit(T& x, int n) -{ - x &= ~(1 << n); -} + template <class T> + constexpr void clr_bit(T& x, int n) + { + x &= ~(1 << n); + } -/** + /** * @brief Get n-th bit in array of words (starting from LSB). * * @tparam T Word type @@ -188,39 +189,39 @@ constexpr void clr_bit(T& x, int n) * @param n Index of bit to get * @return true if set */ -template<class T> -constexpr bool get_arr_bit(const T* p, unsigned n) -{ - return get_bit(p[n >> bit_shft<T>()], n & bit_wrap<T>()); -} + template <class T> + constexpr bool get_arr_bit(const T* p, unsigned n) + { + return get_bit(p[n >> bit_shft<T>()], n & bit_wrap<T>()); + } -/** + /** * @brief Set n-th bit in array of words (starting from LSB). * * @tparam T Word type * @param p Array of words * @param n Index of bit to set */ -template<class T> -constexpr void set_arr_bit(T* p, unsigned n) -{ - set_bit(p[n >> bit_shft<T>()], n & bit_wrap<T>()); -} + template <class T> + constexpr void set_arr_bit(T* p, unsigned n) + { + set_bit(p[n >> bit_shft<T>()], n & bit_wrap<T>()); + } -/** + /** * @brief Clear n-th bit in array of words (starting from LSB). * * @tparam T Word type * @param p Array of words * @param n Index of bit to clear */ -template<class T> -constexpr void clr_arr_bit(T* p, unsigned n) -{ - clr_bit(p[n >> bit_shft<T>()], n & bit_wrap<T>()); -} + template <class T> + constexpr void clr_arr_bit(T* p, unsigned n) + { + clr_bit(p[n >> bit_shft<T>()], n & bit_wrap<T>()); + } -/** + /** * @brief Shift bits left in array of integer elements from least significant bit * and considering 0-th byte as the right most. * uint16_t example: 0b10000000'11100001 ==> 0b00000001'11000010. @@ -230,16 +231,17 @@ constexpr void clr_arr_bit(T* p, unsigned n) * @param x Array of integers, nullptr not acceptable! * @param len Number of elements */ -template<class T, size_t L> -constexpr void shift_left(T (&x)[L]) -{ - for (int i = L - 1; i > 0; --i) { - x[i] <<= 1; - x[i] |= x[i - 1] >> bit_wrap<T>(); - } - x[0] <<= 1; -} - -} + template <class T, size_t L> + constexpr void shift_left(T (&x)[L]) + { + for (int i = L - 1; i > 0; --i) + { + x[i] <<= 1; + x[i] |= x[i - 1] >> bit_wrap<T>(); + } + x[0] <<= 1; + } + +} // namespace utl #endif diff --git a/Private/NewBoot/BootKit/Vendor/Support.hxx b/Private/NewBoot/BootKit/Vendor/Support.hxx index 88483984..f9772cbb 100644 --- a/Private/NewBoot/BootKit/Vendor/Support.hxx +++ b/Private/NewBoot/BootKit/Vendor/Support.hxx @@ -10,82 +10,111 @@ #include <BootKit/BootKit.hxx> -#define cLongMax ((long)(~0UL>>1)) +#define cLongMax ((long)(~0UL >> 1)) #define cLongMin (~cLongMax) -#define SetMem(dst, c, sz) memset(dst, c, sz) +#define SetMem(dst, c, sz) memset(dst, c, sz) #define MoveMem(dst, src, sz) memcpy(dst, src, sz) #define CopyMem(dst, src, sz) memcpy(dst, src, sz) -#define StrLen(src) strlen(src) +#define StrLen(src) strlen(src) -inline int isspace(int c) { return c == ' '; } +inline int isspace(int c) +{ + return c == ' '; +} -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'; +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 + 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); + 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/Private/NewBoot/NetBoot/Module.cxx b/Private/NewBoot/NetBoot/Module.cxx index c89d0a5f..ce3c09db 100644 --- a/Private/NewBoot/NetBoot/Module.cxx +++ b/Private/NewBoot/NetBoot/Module.cxx @@ -9,11 +9,11 @@ #include <BootKit/BootKit.hxx> -EXTERN_C Int32 EfiMain(Void) +EXTERN_C Int32 EfiMain(Void) { - /// - Find a network drive called "/OnlineBoot" - /// - Download our image - /// - Boot from it. + /// - Find a network drive called "/OnlineBoot" + /// - Download our image + /// - Boot from it. - return kEfiOk; + return kEfiOk; } diff --git a/Private/NewBoot/Source/HEL/AMD64/BootAHCI.cxx b/Private/NewBoot/Source/HEL/AMD64/BootAHCI.cxx index d04a94d3..a99b8a56 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootAHCI.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootAHCI.cxx @@ -15,7 +15,6 @@ * */ - #include <BootKit/Platform.hxx> #include <BootKit/Protocol.hxx> #include <BootKit/HW/SATA.hxx> diff --git a/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx b/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx index edfbaed8..a5bd9809 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx @@ -23,138 +23,153 @@ #define kATADataLen 256 -static Boolean kATADetected = false; -static Int32 kATADeviceType = kATADeviceCount; -static UInt16 kATAData[kATADataLen] = {0}; +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 < 4; i++) In8(IO + ATA_REG_STATUS); +STATIC Boolean boot_ata_wait_io(UInt16 IO) +{ + for (int i = 0; i < 4; i++) + In8(IO + ATA_REG_STATUS); ATAWaitForIO_Retry: - auto statRdy = In8(IO + ATA_REG_STATUS); + auto statRdy = In8(IO + ATA_REG_STATUS); - if ((statRdy & ATA_SR_BSY)) goto ATAWaitForIO_Retry; + if ((statRdy & ATA_SR_BSY)) + goto ATAWaitForIO_Retry; ATAWaitForIO_Retry2: - statRdy = In8(IO + ATA_REG_STATUS); + statRdy = In8(IO + ATA_REG_STATUS); - if (statRdy & ATA_SR_ERR) return false; + if (statRdy & ATA_SR_ERR) + return false; - if (!(statRdy & ATA_SR_DRDY)) goto ATAWaitForIO_Retry2; + if (!(statRdy & ATA_SR_DRDY)) + goto ATAWaitForIO_Retry2; - return true; + 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); +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; +Boolean boot_ata_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster) +{ + if (boot_ata_detected()) + return true; - BTextWriter writer; + BTextWriter writer; - UInt16 IO = Bus; + UInt16 IO = Bus; - boot_ata_select(IO); + boot_ata_select(IO); - // Bus init, NEIN bit. - Out8(IO + ATA_REG_NEIN, 1); + // Bus init, NEIN bit. + Out8(IO + ATA_REG_NEIN, 1); - // identify until it's good. + // identify until it's good. ATAInit_Retry: - auto statRdy = In8(IO + ATA_REG_STATUS); + auto statRdy = In8(IO + ATA_REG_STATUS); - if (statRdy & ATA_SR_ERR) { - writer.Write( - L"New Boot: ATA: Select error, not an IDE based hard-drive.\r"); + if (statRdy & ATA_SR_ERR) + { + writer.Write( + L"New Boot: ATA: Select error, not an IDE based hard-drive.\r"); - return false; - } + return false; + } - if ((statRdy & ATA_SR_BSY)) goto ATAInit_Retry; + if ((statRdy & ATA_SR_BSY)) + goto ATAInit_Retry; - Out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY); + Out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY); - /// fetch serial info - /// model, speed, number of sectors... + /// fetch serial info + /// model, speed, number of sectors... - boot_ata_wait_io(IO); + boot_ata_wait_io(IO); - for (SizeT indexData = 0ul; indexData < kATADataLen; ++indexData) { - kATAData[indexData] = In16(IO + ATA_REG_DATA); - } + for (SizeT indexData = 0ul; indexData < kATADataLen; ++indexData) + { + kATAData[indexData] = In16(IO + ATA_REG_DATA); + } - OutBus = - (Bus == ATA_PRIMARY_IO) ? BootDeviceATA::kPrimary : BootDeviceATA::kSecondary; + OutBus = + (Bus == ATA_PRIMARY_IO) ? BootDeviceATA::kPrimary : BootDeviceATA::kSecondary; - OutMaster = (Bus == ATA_PRIMARY_IO) ? ATA_MASTER : ATA_SLAVE; + OutMaster = (Bus == ATA_PRIMARY_IO) ? ATA_MASTER : ATA_SLAVE; - return true; + return true; } -Void boot_ata_read(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, - SizeT SectorSz, SizeT Size) { - UInt8 Command = ((!Master) ? 0xE0 : 0xF0); +Void boot_ata_read(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, SizeT SectorSz, SizeT Size) +{ + UInt8 Command = ((!Master) ? 0xE0 : 0xF0); - boot_ata_wait_io(IO); - boot_ata_select(IO); + boot_ata_wait_io(IO); + boot_ata_select(IO); - Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); + Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); - Out8(IO + ATA_REG_SEC_COUNT0, 2); + Out8(IO + ATA_REG_SEC_COUNT0, 2); - Out8(IO + ATA_REG_LBA0, (Lba)); - Out8(IO + ATA_REG_LBA1, (Lba) >> 8); - Out8(IO + ATA_REG_LBA2, (Lba) >> 16); - Out8(IO + ATA_REG_LBA3, (Lba) >> 24); + Out8(IO + ATA_REG_LBA0, (Lba)); + Out8(IO + ATA_REG_LBA1, (Lba) >> 8); + Out8(IO + ATA_REG_LBA2, (Lba) >> 16); + Out8(IO + ATA_REG_LBA3, (Lba) >> 24); - Out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO); + Out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO); - boot_ata_wait_io(IO); + 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); - } + 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) { - UInt8 Command = ((!Master) ? 0xE0 : 0xF0); +Void boot_ata_write(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, SizeT SectorSz, SizeT Size) +{ + UInt8 Command = ((!Master) ? 0xE0 : 0xF0); - boot_ata_wait_io(IO); - boot_ata_select(IO); + boot_ata_wait_io(IO); + boot_ata_select(IO); - Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); + Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); - Out8(IO + ATA_REG_SEC_COUNT0, 2); + Out8(IO + ATA_REG_SEC_COUNT0, 2); - Out8(IO + ATA_REG_LBA0, (Lba)); - Out8(IO + ATA_REG_LBA1, (Lba) >> 8); - Out8(IO + ATA_REG_LBA2, (Lba) >> 16); - Out8(IO + ATA_REG_LBA3, (Lba) >> 24); + Out8(IO + ATA_REG_LBA0, (Lba)); + Out8(IO + ATA_REG_LBA1, (Lba) >> 8); + Out8(IO + ATA_REG_LBA2, (Lba) >> 16); + Out8(IO + ATA_REG_LBA3, (Lba) >> 24); - Out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO); + Out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO); - boot_ata_wait_io(IO); + 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); - } + 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; } +Boolean boot_ata_detected(Void) +{ + return kATADetected; +} /*** * @@ -168,40 +183,48 @@ Boolean boot_ata_detected(Void) { return kATADetected; } * @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; - } +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(); } +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; - } +BootDeviceATA& BootDeviceATA::Read(CharacterTypeUTF8* Buf, const SizeT& SectorSz) +{ + if (!boot_ata_detected()) + { + Leak().mErr = true; + return *this; + } - this->Leak().mErr = false; + this->Leak().mErr = false; - if (!Buf || SectorSz < 1) return *this; + if (!Buf || SectorSz < 1) + return *this; - auto lba = this->Leak().mBase / SectorSz; + auto lba = this->Leak().mBase / SectorSz; - boot_ata_read(lba, this->Leak().mBus, this->Leak().mMaster, - Buf, SectorSz, this->Leak().mSize); + boot_ata_read(lba, this->Leak().mBus, this->Leak().mMaster, + Buf, SectorSz, this->Leak().mSize); - return *this; + return *this; } /** @@ -209,37 +232,45 @@ BootDeviceATA& BootDeviceATA::Read(CharacterTypeUTF8* Buf, const SizeT& SectorSz @param Sz Sector size @param Buf buffer */ -BootDeviceATA& BootDeviceATA::Write(CharacterTypeUTF8* Buf, const SizeT& SectorSz) { - if (!boot_ata_detected()) { - Leak().mErr = true; - return *this; - } +BootDeviceATA& BootDeviceATA::Write(CharacterTypeUTF8* Buf, const SizeT& SectorSz) +{ + if (!boot_ata_detected()) + { + Leak().mErr = true; + return *this; + } - Leak().mErr = false; + Leak().mErr = false; - if (!Buf || SectorSz < 1) return *this; + if (!Buf || SectorSz < 1) + return *this; - auto lba = this->Leak().mBase / SectorSz; + auto lba = this->Leak().mBase / SectorSz; - boot_ata_write(lba, this->Leak().mBus, this->Leak().mMaster, - Buf, SectorSz, this->Leak().mSize); + boot_ata_write(lba, this->Leak().mBus, this->Leak().mMaster, + Buf, SectorSz, this->Leak().mSize); - return *this; + return *this; } /** * @brief ATA trait getter. * @return BootDeviceATA::ATATrait& the drive config. */ -BootDeviceATA::ATATrait& BootDeviceATA::Leak() { return mTrait; } +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::GetSectorsCount() noexcept +{ + return (kATAData[61] << 16) | kATAData[60]; } -SizeT BootDeviceATA::GetDiskSize() noexcept { - return this->GetSectorsCount() * BootDeviceATA::kSectorSize; +SizeT BootDeviceATA::GetDiskSize() noexcept +{ + return this->GetSectorsCount() * BootDeviceATA::kSectorSize; } diff --git a/Private/NewBoot/Source/HEL/AMD64/BootFileReader.cxx b/Private/NewBoot/Source/HEL/AMD64/BootFileReader.cxx index 85b90e57..39c18821 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootFileReader.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootFileReader.cxx @@ -32,117 +32,142 @@ @brief File Reader constructor. */ 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; - EfiFileProtocol* rootFs = nullptr; - - EfiLoadImageProtocol* img = nullptr; - EfiGUID guidImg = EfiGUID(EFI_LOADED_IMAGE_PROTOCOL_GUID); - - if (BS->HandleProtocol(ImageHandle, &guidImg, (void**)&img) != kEfiOk) { - mWriter.Write(L"New Boot: Fetch-Protocol: No-Such-Protocol").Write(L"\r"); - this->mErrorCode = kNotSupported; - } - - if (BS->HandleProtocol(img->DeviceHandle, &guidEfp, (void**)&efp) != kEfiOk) { - mWriter.Write(L"New Boot: Fetch-Protocol: No-Such-Protocol").Write(L"\r"); - this->mErrorCode = kNotSupported; - return; - } - - /// Start doing disk I/O - - if (efp->OpenVolume(efp, &rootFs) != kEfiOk) { - mWriter.Write(L"New Boot: Fetch-Protocol: No-Such-Volume").Write(L"\r"); - this->mErrorCode = kNotSupported; - return; - } - - EfiFileProtocol* kernelFile = nullptr; - - if (rootFs->Open(rootFs, &kernelFile, mPath, kEFIFileRead, kEFIReadOnly) != - kEfiOk) { - mWriter.Write(L"New Boot: Fetch-Protocol: No-Such-Path: ") - .Write(mPath) - .Write(L"\r"); - this->mErrorCode = kNotSupported; - return; - } - - rootFs->Close(rootFs); - - mSizeFile = 0; - mFile = kernelFile; - mErrorCode = kOperationOkay; + 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; + EfiFileProtocol* rootFs = nullptr; + + EfiLoadImageProtocol* img = nullptr; + EfiGUID guidImg = EfiGUID(EFI_LOADED_IMAGE_PROTOCOL_GUID); + + if (BS->HandleProtocol(ImageHandle, &guidImg, (void**)&img) != kEfiOk) + { + mWriter.Write(L"New Boot: Fetch-Protocol: No-Such-Protocol").Write(L"\r"); + this->mErrorCode = kNotSupported; + } + + if (BS->HandleProtocol(img->DeviceHandle, &guidEfp, (void**)&efp) != kEfiOk) + { + mWriter.Write(L"New Boot: Fetch-Protocol: No-Such-Protocol").Write(L"\r"); + this->mErrorCode = kNotSupported; + return; + } + + /// Start doing disk I/O + + if (efp->OpenVolume(efp, &rootFs) != kEfiOk) + { + mWriter.Write(L"New Boot: Fetch-Protocol: No-Such-Volume").Write(L"\r"); + this->mErrorCode = kNotSupported; + return; + } + + EfiFileProtocol* kernelFile = nullptr; + + if (rootFs->Open(rootFs, &kernelFile, mPath, kEFIFileRead, kEFIReadOnly) != + kEfiOk) + { + mWriter.Write(L"New Boot: Fetch-Protocol: No-Such-Path: ") + .Write(mPath) + .Write(L"\r"); + this->mErrorCode = kNotSupported; + return; + } + + rootFs->Close(rootFs); + + mSizeFile = 0; + mFile = kernelFile; + mErrorCode = kOperationOkay; } -BFileReader::~BFileReader() { - if (this->mFile) { - this->mFile->Close(this->mFile); - this->mFile = nullptr; - } +BFileReader::~BFileReader() +{ + if (this->mFile) + { + this->mFile->Close(this->mFile); + this->mFile = nullptr; + } - if (this->mBlob) BS->FreePool(mBlob); + if (this->mBlob) + BS->FreePool(mBlob); - BSetMem(this->mPath, 0, kPathLen); + BSetMem(this->mPath, 0, kPathLen); } /** @brief this reads all of the buffer. @param until read until size is reached. */ -Void BFileReader::ReadAll(SizeT until, SizeT chunk) { - if (mBlob == nullptr) { - if (auto err = BS->AllocatePool(EfiLoaderCode, until, (VoidPtr*)&mBlob) != - kEfiOk) { - mWriter.Write(L"*** EFI-Code: ").Write(err).Write(L" ***\r"); - EFI::ThrowError(L"OutOfMemory", L"Out of memory."); - } - } - - mErrorCode = kNotSupported; - - UInt64 bufSize = chunk; - UInt64 szCnt = 0; - UInt64 curSz = 0; - - while (szCnt < until) { - if (mFile->Read(mFile, &bufSize, (VoidPtr)((UIntPtr)mBlob + curSz)) != - kEfiOk) { - break; - } - - szCnt += bufSize; - curSz += bufSize; - - if (bufSize == 0) break; - } - - mSizeFile = curSz; - mErrorCode = kOperationOkay; +Void BFileReader::ReadAll(SizeT until, SizeT chunk) +{ + if (mBlob == nullptr) + { + if (auto err = BS->AllocatePool(EfiLoaderCode, until, (VoidPtr*)&mBlob) != + kEfiOk) + { + mWriter.Write(L"*** EFI-Code: ").Write(err).Write(L" ***\r"); + EFI::ThrowError(L"OutOfMemory", L"Out of memory."); + } + } + + mErrorCode = kNotSupported; + + UInt64 bufSize = chunk; + UInt64 szCnt = 0; + UInt64 curSz = 0; + + while (szCnt < until) + { + if (mFile->Read(mFile, &bufSize, (VoidPtr)((UIntPtr)mBlob + curSz)) != + kEfiOk) + { + break; + } + + szCnt += bufSize; + curSz += bufSize; + + if (bufSize == 0) + break; + } + + mSizeFile = curSz; + mErrorCode = kOperationOkay; } /// @brief error code getter. /// @return the error code. -Int32& BFileReader::Error() { return mErrorCode; } +Int32& BFileReader::Error() +{ + return mErrorCode; +} /// @brief blob getter. /// @return the blob. -VoidPtr BFileReader::Blob() { return mBlob; } +VoidPtr BFileReader::Blob() +{ + return mBlob; +} /// @breif Size getter. /// @return the size of the file. -UInt64& BFileReader::Size() { return mSizeFile; } +UInt64& BFileReader::Size() +{ + return mSizeFile; +} diff --git a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx index 844179da..ebde06e8 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx @@ -20,13 +20,13 @@ /// make the compiler shut up. #ifndef kMachineModel #define kMachineModel "NeWS HD" -#endif // !kMachineModel +#endif // !kMachineModel /** Graphics related. */ -STATIC EfiGraphicsOutputProtocol* kGop = nullptr; -STATIC UInt16 kStride = 0U; -STATIC EfiGUID kGopGuid; +STATIC EfiGraphicsOutputProtocol* kGop = nullptr; +STATIC UInt16 kStride = 0U; +STATIC EfiGUID kGopGuid; EXTERN_C Void hal_init_platform(HEL::HandoverInformationHeader* HIH); @@ -34,196 +34,211 @@ EXTERN_C Void hal_init_platform(HEL::HandoverInformationHeader* HIH); @brief Finds and stores the GOP. */ -STATIC Void CheckAndFindFramebuffer() noexcept { - kGopGuid = EfiGUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); - kGop = nullptr; +STATIC Void CheckAndFindFramebuffer() noexcept +{ + kGopGuid = EfiGUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); + kGop = nullptr; - extern EfiBootServices* BS; + extern EfiBootServices* BS; - BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*)&kGop); + BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*)&kGop); - kStride = 4; + kStride = 4; } /// @brief check the BootDevice if suitable. -STATIC Bool CheckBootDevice(BootDeviceATA& ataDev) { - if (ataDev.Leak().mErr) return false; - return true; +STATIC Bool CheckBootDevice(BootDeviceATA& ataDev) +{ + if (ataDev.Leak().mErr) + return false; + return true; } /// @brief Main EFI entrypoint. /// @param ImageHandle Handle of this image. /// @param SystemTable The system table of it. /// @return -EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, - EfiSystemTable* SystemTable) { - InitEFI(SystemTable); ///! Init the EFI library. - CheckAndFindFramebuffer(); ///! Init the GOP. +EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, + EfiSystemTable* SystemTable) +{ + InitEFI(SystemTable); ///! Init the EFI library. + CheckAndFindFramebuffer(); ///! Init the GOP. - BTextWriter writer; + BTextWriter writer; - /// Splash screen stuff + /// Splash screen stuff - writer.Write(L"Mahrouss-Logic (R) New Boot: ") - .Write(BVersionString::Shared()); + writer.Write(L"Mahrouss-Logic (R) New Boot: ") + .Write(BVersionString::Shared()); - writer.Write(L"\rNew Boot: Firmware Vendor: ") - .Write(SystemTable->FirmwareVendor) - .Write(L"\r"); + writer.Write(L"\rNew Boot: Firmware Vendor: ") + .Write(SystemTable->FirmwareVendor) + .Write(L"\r"); - UInt32* MapKey = new UInt32(); - UInt32* SizePtr = new UInt32(); - EfiMemoryDescriptor* Descriptor = nullptr; - UInt32* SzDesc = new UInt32(); - UInt32* RevDesc = new UInt32(); + UInt32* MapKey = new UInt32(); + UInt32* SizePtr = new UInt32(); + EfiMemoryDescriptor* Descriptor = nullptr; + UInt32* SzDesc = new UInt32(); + UInt32* RevDesc = new UInt32(); - *MapKey = 0; - *SizePtr = 0; + *MapKey = 0; + *SizePtr = 0; + + HEL::HandoverInformationHeader* handoverHdrPtr = + new HEL::HandoverInformationHeader(); + + for (SizeT indexVT = 0; indexVT < SystemTable->NumberOfTableEntries; + ++indexVT) + { + volatile Char* vendorTable = reinterpret_cast<volatile Char*>( + SystemTable->ConfigurationTable[indexVT].VendorTable); - HEL::HandoverInformationHeader* handoverHdrPtr = - new HEL::HandoverInformationHeader(); - - for (SizeT indexVT = 0; indexVT < SystemTable->NumberOfTableEntries; - ++indexVT) { - volatile Char* vendorTable = reinterpret_cast<volatile Char*>( - SystemTable->ConfigurationTable[indexVT].VendorTable); - - /// ACPI's 'RSD PTR', which contains hardware tables (MADT, FACP...) - if (vendorTable[0] == 'R' && vendorTable[1] == 'S' && - vendorTable[2] == 'D' && vendorTable[3] == ' ' && - vendorTable[4] == 'P' && vendorTable[5] == 'T' && - vendorTable[6] == 'R' && vendorTable[7] == ' ') { - handoverHdrPtr->f_HardwareTables.f_RsdPtr = (VoidPtr)vendorTable; - - break; - } - } - - handoverHdrPtr->f_Magic = kHandoverMagic; - handoverHdrPtr->f_Version = kHandoverVersion; - - BCopyMem(handoverHdrPtr->f_FirmwareVendorName, SystemTable->FirmwareVendor, - handoverHdrPtr->f_FirmwareVendorLen); - - handoverHdrPtr->f_GOP.f_The = kGop->Mode->FrameBufferBase; - handoverHdrPtr->f_GOP.f_Width = kGop->Mode->Info->VerticalResolution; - handoverHdrPtr->f_GOP.f_Height = kGop->Mode->Info->HorizontalResolution; - handoverHdrPtr->f_GOP.f_PixelPerLine = kGop->Mode->Info->PixelsPerScanLine; - handoverHdrPtr->f_GOP.f_PixelFormat = kGop->Mode->Info->PixelFormat; - handoverHdrPtr->f_GOP.f_Size = kGop->Mode->FrameBufferSize; - - ///! Finally draw bootloader screen. - - kHandoverHeader = handoverHdrPtr; - - ToolboxInitRsrc(); - - ToolboxDrawZone(RGB(9d, 9d, 9d), handoverHdrPtr->f_GOP.f_Height, - handoverHdrPtr->f_GOP.f_Width, 0, 0); - - ToolboxClearRsrc(); - - ToolboxDrawRsrc(NewBoot, NEWBOOT_HEIGHT, NEWBOOT_WIDTH, - (handoverHdrPtr->f_GOP.f_Width - NEWBOOT_WIDTH) / 2, - (handoverHdrPtr->f_GOP.f_Height - NEWBOOT_HEIGHT) / 2); - - ToolboxClearRsrc(); - - BS->GetMemoryMap(SizePtr, Descriptor, MapKey, SzDesc, RevDesc); - - Descriptor = new EfiMemoryDescriptor[*SzDesc]; - BS->GetMemoryMap(SizePtr, Descriptor, MapKey, SzDesc, RevDesc); - - writer.Write(L"Kernel-Desc-Count: "); - writer.Write(*SzDesc); - writer.Write(L"\r"); - - auto cDefaultMemoryMap = 0; /// The sixth entry. - - /// A simple loop which finds a usable memory region for us. - SizeT i = 0UL; - for (; Descriptor[i].Kind != EfiMemoryType::EfiConventionalMemory; ++i) { - ; - } - - cDefaultMemoryMap = i; - - writer.Write(L"Number-Of-Pages: ") - .Write(Descriptor[cDefaultMemoryMap].NumberOfPages) - .Write(L"\r"); - writer.Write(L"Virtual-Address: ") - .Write(Descriptor[cDefaultMemoryMap].VirtualStart) - .Write(L"\r"); - writer.Write(L"Phyiscal-Address: ") - .Write(Descriptor[cDefaultMemoryMap].PhysicalStart) - .Write(L"\r"); - writer.Write(L"Page-Kind: ") - .Write(Descriptor[cDefaultMemoryMap].Kind) - .Write(L"\r"); - writer.Write(L"Page-Attribute: ") - .Write(Descriptor[cDefaultMemoryMap].Attribute) - .Write(L"\r"); - - handoverHdrPtr->f_PhysicalStart = - (VoidPtr)Descriptor[cDefaultMemoryMap].PhysicalStart; - - handoverHdrPtr->f_FirmwareSpecific[HEL::kHandoverSpecificAttrib] = - Descriptor[cDefaultMemoryMap].Attribute; - handoverHdrPtr->f_FirmwareSpecific[HEL::kHandoverSpecificKind] = - Descriptor[cDefaultMemoryMap].Kind; - handoverHdrPtr->f_FirmwareSpecific[HEL::kHandoverSpecificMemoryEfi] = - (UIntPtr)Descriptor; - - handoverHdrPtr->f_VirtualStart = - (VoidPtr)Descriptor[cDefaultMemoryMap].VirtualStart; - handoverHdrPtr->f_VirtualSize = - Descriptor[cDefaultMemoryMap].NumberOfPages; /* # of pages */ - - handoverHdrPtr->f_FirmwareVendorLen = BStrLen(SystemTable->FirmwareVendor); - - BFileReader reader(L"SplashScreen.fmt", ImageHandle); - reader.ReadAll(512, 16); - - if (reader.Blob()) { - Char* buf = (Char*)reader.Blob(); - - for (SizeT i = 0; i < reader.Size(); ++i) { - if (buf[i] != '\n' && buf[i] != '\r') { - if (buf[i] == '*') { - writer.WriteCharacter('\t'); - } else { - writer.WriteCharacter(buf[i]); - } - } else - writer.Write(L"\r"); - } - } - - /// - /// The following checks for an exisiting partition - /// inside the disk, if it doesn't have one, - /// format the disk. - // - - BDiskFormatFactory<BootDeviceATA> diskFormatter; - - /// if not formated yet, then format it with the following folders: - /// /, /Boot, /Applications. - if (!diskFormatter.IsPartitionValid()) { - BDiskFormatFactory<BootDeviceATA>::BFileDescriptor rootDesc{0}; - - CopyMem(rootDesc.fFileName, kNewFSRoot, StrLen(kNewFSRoot)); - rootDesc.fKind = kNewFSCatalogKindDir; - - diskFormatter.Format(kMachineModel, &rootDesc, 1); - } - - EFI::ExitBootServices(*MapKey, ImageHandle); - - /// Fallback to builtin kernel. - hal_init_platform(handoverHdrPtr); - - EFI::Stop(); - - CANT_REACH(); + /// ACPI's 'RSD PTR', which contains hardware tables (MADT, FACP...) + if (vendorTable[0] == 'R' && vendorTable[1] == 'S' && + vendorTable[2] == 'D' && vendorTable[3] == ' ' && + vendorTable[4] == 'P' && vendorTable[5] == 'T' && + vendorTable[6] == 'R' && vendorTable[7] == ' ') + { + handoverHdrPtr->f_HardwareTables.f_RsdPtr = (VoidPtr)vendorTable; + + break; + } + } + + handoverHdrPtr->f_Magic = kHandoverMagic; + handoverHdrPtr->f_Version = kHandoverVersion; + + BCopyMem(handoverHdrPtr->f_FirmwareVendorName, SystemTable->FirmwareVendor, + handoverHdrPtr->f_FirmwareVendorLen); + + handoverHdrPtr->f_GOP.f_The = kGop->Mode->FrameBufferBase; + handoverHdrPtr->f_GOP.f_Width = kGop->Mode->Info->VerticalResolution; + handoverHdrPtr->f_GOP.f_Height = kGop->Mode->Info->HorizontalResolution; + handoverHdrPtr->f_GOP.f_PixelPerLine = kGop->Mode->Info->PixelsPerScanLine; + handoverHdrPtr->f_GOP.f_PixelFormat = kGop->Mode->Info->PixelFormat; + handoverHdrPtr->f_GOP.f_Size = kGop->Mode->FrameBufferSize; + + ///! Finally draw bootloader screen. + + kHandoverHeader = handoverHdrPtr; + + ToolboxInitRsrc(); + + ToolboxDrawZone(RGB(9d, 9d, 9d), handoverHdrPtr->f_GOP.f_Height, + handoverHdrPtr->f_GOP.f_Width, 0, 0); + + ToolboxClearRsrc(); + + ToolboxDrawRsrc(NewBoot, NEWBOOT_HEIGHT, NEWBOOT_WIDTH, + (handoverHdrPtr->f_GOP.f_Width - NEWBOOT_WIDTH) / 2, + (handoverHdrPtr->f_GOP.f_Height - NEWBOOT_HEIGHT) / 2); + + ToolboxClearRsrc(); + + BS->GetMemoryMap(SizePtr, Descriptor, MapKey, SzDesc, RevDesc); + + Descriptor = new EfiMemoryDescriptor[*SzDesc]; + BS->GetMemoryMap(SizePtr, Descriptor, MapKey, SzDesc, RevDesc); + + writer.Write(L"Kernel-Desc-Count: "); + writer.Write(*SzDesc); + writer.Write(L"\r"); + + auto cDefaultMemoryMap = 0; /// The sixth entry. + + /// A simple loop which finds a usable memory region for us. + SizeT i = 0UL; + for (; Descriptor[i].Kind != EfiMemoryType::EfiConventionalMemory; ++i) + { + ; + } + + cDefaultMemoryMap = i; + + writer.Write(L"Number-Of-Pages: ") + .Write(Descriptor[cDefaultMemoryMap].NumberOfPages) + .Write(L"\r"); + writer.Write(L"Virtual-Address: ") + .Write(Descriptor[cDefaultMemoryMap].VirtualStart) + .Write(L"\r"); + writer.Write(L"Phyiscal-Address: ") + .Write(Descriptor[cDefaultMemoryMap].PhysicalStart) + .Write(L"\r"); + writer.Write(L"Page-Kind: ") + .Write(Descriptor[cDefaultMemoryMap].Kind) + .Write(L"\r"); + writer.Write(L"Page-Attribute: ") + .Write(Descriptor[cDefaultMemoryMap].Attribute) + .Write(L"\r"); + + handoverHdrPtr->f_PhysicalStart = + (VoidPtr)Descriptor[cDefaultMemoryMap].PhysicalStart; + + handoverHdrPtr->f_FirmwareSpecific[HEL::kHandoverSpecificAttrib] = + Descriptor[cDefaultMemoryMap].Attribute; + handoverHdrPtr->f_FirmwareSpecific[HEL::kHandoverSpecificKind] = + Descriptor[cDefaultMemoryMap].Kind; + handoverHdrPtr->f_FirmwareSpecific[HEL::kHandoverSpecificMemoryEfi] = + (UIntPtr)Descriptor; + + handoverHdrPtr->f_VirtualStart = + (VoidPtr)Descriptor[cDefaultMemoryMap].VirtualStart; + handoverHdrPtr->f_VirtualSize = + Descriptor[cDefaultMemoryMap].NumberOfPages; /* # of pages */ + + handoverHdrPtr->f_FirmwareVendorLen = BStrLen(SystemTable->FirmwareVendor); + + BFileReader reader(L"SplashScreen.fmt", ImageHandle); + reader.ReadAll(512, 16); + + if (reader.Blob()) + { + Char* buf = (Char*)reader.Blob(); + + for (SizeT i = 0; i < reader.Size(); ++i) + { + if (buf[i] != '\n' && buf[i] != '\r') + { + if (buf[i] == '*') + { + writer.WriteCharacter('\t'); + } + else + { + writer.WriteCharacter(buf[i]); + } + } + else + writer.Write(L"\r"); + } + } + + /// + /// The following checks for an exisiting partition + /// inside the disk, if it doesn't have one, + /// format the disk. + // + + BDiskFormatFactory<BootDeviceATA> diskFormatter; + + /// if not formated yet, then format it with the following folders: + /// /, /Boot, /Applications. + if (!diskFormatter.IsPartitionValid()) + { + BDiskFormatFactory<BootDeviceATA>::BFileDescriptor rootDesc{0}; + + CopyMem(rootDesc.fFileName, kNewFSRoot, StrLen(kNewFSRoot)); + rootDesc.fKind = kNewFSCatalogKindDir; + + diskFormatter.Format(kMachineModel, &rootDesc, 1); + } + + EFI::ExitBootServices(*MapKey, ImageHandle); + + /// Fallback to builtin kernel. + hal_init_platform(handoverHdrPtr); + + EFI::Stop(); + + CANT_REACH(); } diff --git a/Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx b/Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx index fa735142..1b8576a9 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx @@ -11,51 +11,92 @@ #ifdef __STANDALONE__ -EXTERN_C void rt_hlt() { asm volatile("hlt"); } +EXTERN_C void rt_hlt() +{ + asm volatile("hlt"); +} -EXTERN_C void rt_cli() { asm volatile("cli"); } +EXTERN_C void rt_cli() +{ + asm volatile("cli"); +} -EXTERN_C void rt_sti() { asm volatile("sti"); } +EXTERN_C void rt_sti() +{ + asm volatile("sti"); +} -EXTERN_C void rt_cld() { asm volatile("cld"); } +EXTERN_C void rt_cld() +{ + asm volatile("cld"); +} -EXTERN_C void rt_std() { asm volatile("std"); } +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 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 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 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"); +EXTERN_C UInt8 In8(UInt16 port) +{ + UInt8 value; + asm volatile("inb %1, %%al" + : "=a"(value) + : "Nd"(port) + : "memory"); - return value; + return value; } -EXTERN_C UInt16 In16(UInt16 port) { - UInt16 value; - asm volatile("inw %%dx, %%ax" : "=a"(value) : "d"(port)); +EXTERN_C UInt16 In16(UInt16 port) +{ + UInt16 value; + asm volatile("inw %%dx, %%ax" + : "=a"(value) + : "d"(port)); - return value; + return value; } -EXTERN_C UInt32 In32(UInt16 port) { - UInt32 value; - asm volatile("inl %1, %%eax" : "=a"(value) : "Nd"(port) : "memory"); +EXTERN_C UInt32 In32(UInt16 port) +{ + UInt32 value; + asm volatile("inl %1, %%eax" + : "=a"(value) + : "Nd"(port) + : "memory"); - return value; + return value; } #else -void rt_hlt() { NewOS::HAL::rt_halt(); } +void rt_hlt() +{ + NewOS::HAL::rt_halt(); +} #endif // 0 diff --git a/Private/NewBoot/Source/HEL/AMD64/BootString.cxx b/Private/NewBoot/Source/HEL/AMD64/BootString.cxx index ef0e4744..34e16737 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootString.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootString.cxx @@ -19,63 +19,74 @@ ///////////////////////////////////////////////////////////////////////////////////////////////////////// -NewOS::SizeT BCopyMem(CharacterTypeUTF16 *dest, CharacterTypeUTF16 *src, - const NewOS::SizeT len) { - if (!dest || !src) return 0; - - SizeT index = 0UL; - for (; index < len; ++index) { - dest[index] = src[index]; - } - - return index; +NewOS::SizeT BCopyMem(CharacterTypeUTF16* dest, CharacterTypeUTF16* src, const NewOS::SizeT len) +{ + if (!dest || !src) + return 0; + + SizeT index = 0UL; + for (; index < len; ++index) + { + dest[index] = src[index]; + } + + return index; } -NewOS::SizeT BStrLen(const CharacterTypeUTF16 *ptr) { - if (!ptr) return 0; +NewOS::SizeT BStrLen(const CharacterTypeUTF16* ptr) +{ + if (!ptr) + return 0; - NewOS::SizeT cnt = 0; + NewOS::SizeT cnt = 0; - while (*ptr != (CharacterTypeUTF16)0) { - ++ptr; - ++cnt; - } + while (*ptr != (CharacterTypeUTF16)0) + { + ++ptr; + ++cnt; + } - return cnt; + return cnt; } -NewOS::SizeT BSetMem(CharacterTypeUTF16 *src, const CharacterTypeUTF16 byte, - const NewOS::SizeT len) { - if (!src) return 0; +NewOS::SizeT BSetMem(CharacterTypeUTF16* src, const CharacterTypeUTF16 byte, const NewOS::SizeT len) +{ + if (!src) + return 0; - NewOS::SizeT cnt = 0UL; + NewOS::SizeT cnt = 0UL; - while (*src != 0) { - if (cnt > len) break; + while (*src != 0) + { + if (cnt > len) + break; - *src = byte; - ++src; + *src = byte; + ++src; - ++cnt; - } + ++cnt; + } - return cnt; + return cnt; } -NewOS::SizeT BSetMem(CharacterTypeUTF8 *src, const CharacterTypeUTF8 byte, - const NewOS::SizeT len) { - if (!src) return 0; +NewOS::SizeT BSetMem(CharacterTypeUTF8* src, const CharacterTypeUTF8 byte, const NewOS::SizeT len) +{ + if (!src) + return 0; - NewOS::SizeT cnt = 0UL; + NewOS::SizeT cnt = 0UL; - while (*src != 0) { - if (cnt > len) break; + while (*src != 0) + { + if (cnt > len) + break; - *src = byte; - ++src; + *src = byte; + ++src; - ++cnt; - } + ++cnt; + } - return cnt; + return cnt; } diff --git a/Private/NewBoot/Source/HEL/AMD64/BootTextWriter.cxx b/Private/NewBoot/Source/HEL/AMD64/BootTextWriter.cxx index 13ef35c8..fcb2154f 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootTextWriter.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootTextWriter.cxx @@ -23,100 +23,118 @@ /** @brief puts wrapper over EFI ConOut. */ -BTextWriter &BTextWriter::Write(const CharacterTypeUTF16 *str) { +BTextWriter& 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; + 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. -BTextWriter &BTextWriter::Write(const Char *str) { +BTextWriter& 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; + 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. */ -BTextWriter &BTextWriter::WriteCharacter(CharacterTypeUTF16 c) { +BTextWriter& BTextWriter::WriteCharacter(CharacterTypeUTF16 c) +{ #ifdef __DEBUG__ - EfiCharType str[2]; + EfiCharType str[2]; - str[0] = c; - str[1] = 0; - ST->ConOut->OutputString(ST->ConOut, str); -#endif // ifdef __DEBUG__ + str[0] = c; + str[1] = 0; + ST->ConOut->OutputString(ST->ConOut, str); +#endif // ifdef __DEBUG__ - return *this; + return *this; } -BTextWriter &BTextWriter::Write(const Long &x) { +BTextWriter& BTextWriter::Write(const Long& x) +{ #ifdef __DEBUG__ - this->Write(L"0x"); - this->_Write(x); + this->Write(L"0x"); + this->_Write(x); -#endif // ifdef __DEBUG__ +#endif // ifdef __DEBUG__ - return *this; + return *this; } -BTextWriter &BTextWriter::_Write(const Long &x) { +BTextWriter& BTextWriter::_Write(const Long& x) +{ #ifdef __DEBUG__ - UInt64 y = (x > 0 ? x : -x) / 16; - UInt64 h = (x > 0 ? x : -x) % 16; + UInt64 y = (x > 0 ? x : -x) / 16; + UInt64 h = (x > 0 ? x : -x) % 16; - if (y) this->_Write(y); + if (y) + this->_Write(y); - /* fail if the hex number is not base-16 */ - if (h > 15) { - this->WriteCharacter('?'); - return *this; - } + /* fail if the hex number is not base-16 */ + if (h > 15) + { + this->WriteCharacter('?'); + return *this; + } - if (y < 0) y = -y; + if (y < 0) + y = -y; - const char NUMBERS[17] = "0123456789ABCDEF"; + const char NUMBERS[17] = "0123456789ABCDEF"; - this->WriteCharacter(NUMBERS[h]); -#endif // ifdef __DEBUG__ + this->WriteCharacter(NUMBERS[h]); +#endif // ifdef __DEBUG__ - return *this; + return *this; } diff --git a/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx b/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx index 8d811bd9..25aaec1c 100644 --- a/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx @@ -16,10 +16,10 @@ /// @return void* operator new(size_t sz) { - void* buf = nullptr; - BS->AllocatePool(EfiMemoryType::EfiLoaderData, sz, &buf); + void* buf = nullptr; + BS->AllocatePool(EfiMemoryType::EfiLoaderData, sz, &buf); - return buf; + return buf; } /// @brief Allocates a new object. @@ -27,17 +27,17 @@ void* operator new(size_t sz) /// @return void* operator new[](size_t sz) { - void* buf = nullptr; - BS->AllocatePool(EfiMemoryType::EfiLoaderData, sz, &buf); + void* buf = nullptr; + BS->AllocatePool(EfiMemoryType::EfiLoaderData, sz, &buf); - return buf; + return buf; } /// @brief Deletes the object. /// @param buf the object. void operator delete(void* buf) { - BS->FreePool(buf); + BS->FreePool(buf); } /// @brief Deletes the object (array specific). @@ -45,7 +45,7 @@ void operator delete(void* buf) /// @param size it's size. void operator delete(void* buf, size_t size) { - BS->FreePool(buf); + BS->FreePool(buf); } #endif // Inactive diff --git a/Private/NewBoot/Source/HEL/AMD64/Support.cxx b/Private/NewBoot/Source/HEL/AMD64/Support.cxx index 3a6974bb..65fb6b48 100644 --- a/Private/NewBoot/Source/HEL/AMD64/Support.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/Support.cxx @@ -14,42 +14,49 @@ /// @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; +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; +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) { - if (!whatToCheck || *whatToCheck == 0) return 0; +EXTERN_C size_t strlen(const char* whatToCheck) +{ + if (!whatToCheck || *whatToCheck == 0) + return 0; - SizeT len = 0; + SizeT len = 0; - while (whatToCheck[len] != 0) { - ++len; - } + while (whatToCheck[len] != 0) + { + ++len; + } - return len; + return len; } /// @brief somthing specific to the microsoft ABI, regarding checking the stack. -EXTERN_C void ___chkstk_ms(void) {} +EXTERN_C void ___chkstk_ms(void) +{ +} #endif diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index 9a03b312..a04531da 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -25,7 +25,7 @@ endif IMG=epm.img IMG_2=epm-slave.img -EMU_FLAGS=-net none -smp 4 -m 4G -M q35 \ +EMU_FLAGS=-net none -smp 4 -m 8G -M q35 \ -bios OVMF.fd -device piix3-ide,id=ide \ -drive id=disk,file=$(IMG),format=raw,if=none \ -device ide-hd,drive=disk,bus=ide.0 -drive \ @@ -49,9 +49,6 @@ FLAG_GNU=-fshort-wchar -D__EFI_x86_64__ -mno-red-zone -D__KERNEL__ -D__NEWBOOT__ -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I./ -c -nostdlib -fno-rtti -fno-exceptions \ -std=c++20 -D__HAVE_MAHROUSS_APIS__ -D__MAHROUSS__ -D__BOOTLOADER__ -I./ -DD=dd -IMG_CREATE=qemu-img -MAX_KERNEL_SIZE=1024K BOOT_LOADER=NewBoot.exe KERNEL=NewKernel.exe |
