diff options
| author | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-04-19 08:56:33 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-04-19 09:18:29 +0200 |
| commit | 7b6e3242a0e136c9a525c25152034f449dbf319c (patch) | |
| tree | da935f0b77530523b8074c64f229012982908265 | |
| parent | 0ae4062bfe9936cc9fd2c7bb924442480b067d93 (diff) | |
MHR-5: Made BootKit more portable.
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
| -rw-r--r-- | Private/CFKit/Property.hpp | 1 | ||||
| -rw-r--r-- | Private/NewBoot/BootKit/BootKit.hxx | 141 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/BootFileReader.cxx | 30 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/BootMain.cxx | 33 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx | 33 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/BootTextWriter.cxx | 19 | ||||
| -rw-r--r-- | Private/NewBoot/Source/makefile | 2 |
7 files changed, 141 insertions, 118 deletions
diff --git a/Private/CFKit/Property.hpp b/Private/CFKit/Property.hpp index ea427481..10c8ebf0 100644 --- a/Private/CFKit/Property.hpp +++ b/Private/CFKit/Property.hpp @@ -15,6 +15,7 @@ namespace NewOS { using PropertyId = Int; +/// @brief Kernel property class. class Property { public: explicit Property(const StringView &sw) : fName(sw) {} diff --git a/Private/NewBoot/BootKit/BootKit.hxx b/Private/NewBoot/BootKit/BootKit.hxx index be16c2c5..da178624 100644 --- a/Private/NewBoot/BootKit/BootKit.hxx +++ b/Private/NewBoot/BootKit/BootKit.hxx @@ -11,6 +11,12 @@ #pragma once +/***********************************************************************************/ +/// Framebuffer helpers. +/***********************************************************************************/ + +#define RGB(R, G, B) (UInt32)(0x##R##G##B) + class BTextWriter; class BFileReader; class BFileRunner; @@ -19,6 +25,13 @@ class BVersionString; #include <BootKit/HW/ATA.hxx> #include <CompilerKit/Version.hxx> +/***********************************************************************************/ +/// Include other APIs. +/***********************************************************************************/ + +#include <BootKit/Platform.hxx> +#include <BootKit/Protocol.hxx> + #ifdef __EFI_x86_64__ #define kBootVirtualAddress (0xffffff80000000) #include <FirmwareKit/EFI.hxx> @@ -27,6 +40,8 @@ class BVersionString; #include <FirmwareKit/EPM.hxx> #include <NewKit/Defines.hpp> +#define kMaxReadSize (320) + using namespace NewOS; typedef Char *PEFImagePtr; @@ -88,6 +103,7 @@ class BFileReader final { EfiHandlePtr ImageHandle); ~BFileReader(); + public: Void ReadAll(); enum { @@ -99,15 +115,17 @@ class BFileReader final { kCount, }; - Int32 &Error() { return mErrorCode; } - VoidPtr Blob() { return mBlob; } - EfiFileProtocolPtr File() { return mFile; } - UInt64 &Size() { return mSizeFile; } + /// @brief error code getter. + /// @return the error code. + Int32 &Error(); - UInt64 &Size(const UInt64 &Sz) { - mSizeFile = Sz; - return mSizeFile; - } + /// @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; @@ -124,14 +142,13 @@ class BFileReader final { typedef UInt8 *BlobType; -#define kMaxReadSize (320) - -/***********************************************************************************/ -/// Include other APIs. -/***********************************************************************************/ +class BVersionString final { + public: + static const CharacterTypeUTF16 *Shared() { return BOOTLOADER_VERSION; } +}; -#include <BootKit/Platform.hxx> -#include <BootKit/Protocol.hxx> +/// @brief Bootloader main type. +typedef void (*BootMainKind)(HEL::HandoverInformationHeader *handoverInfo); /***********************************************************************************/ /// Provide some useful processor features. @@ -139,43 +156,17 @@ typedef UInt8 *BlobType; #ifdef __EFI_x86_64__ -inline void Out8(UInt16 port, UInt8 value) { - asm volatile("outb %%al, %1" : : "a"(value), "Nd"(port) : "memory"); -} - -inline void Out16(UInt16 port, UInt16 value) { - asm volatile("outw %%ax, %1" : : "a"(value), "Nd"(port) : "memory"); -} - -inline void Out32(UInt16 port, UInt32 value) { - asm volatile("outl %%eax, %1" : : "a"(value), "Nd"(port) : "memory"); -} - -inline UInt8 In8(UInt16 port) { - UInt8 value; - asm volatile("inb %1, %%al" : "=a"(value) : "Nd"(port) : "memory"); - - return value; -} - -inline UInt16 In16(UInt16 port) { - UInt16 value; - asm volatile("inw %%dx, %%ax" : "=a"(value) : "d"(port)); - - return value; -} - -inline UInt32 In32(UInt16 port) { - UInt32 value; - asm volatile("inl %1, %%eax" : "=a"(value) : "Nd"(port) : "memory"); - - return value; -} - /*** * Common processor instructions. */ +EXTERN_C void Out8(UInt16 port, UInt8 value); +EXTERN_C void Out16(UInt16 port, UInt16 value); +EXTERN_C void Out32(UInt16 port, UInt32 value); +EXTERN_C UInt8 In8(UInt16 port); +EXTERN_C UInt16 In16(UInt16 port); +EXTERN_C UInt32 In32(UInt16 port); + EXTERN_C void rt_hlt(); EXTERN_C void rt_cli(); EXTERN_C void rt_sti(); @@ -184,52 +175,8 @@ EXTERN_C void rt_std(); #endif // __EFI_x86_64__ -/***********************************************************************************/ -/// Framebuffer. -/***********************************************************************************/ - -#define RGB(R, G, B) (UInt32)(0x##R##G##B) - -const UInt32 kRgbRed = 0x000000FF; -const UInt32 kRgbGreen = 0x0000FF00; -const UInt32 kRgbBlue = 0x00FF0000; -const UInt32 kRgbBlack = 0x00000000; -const UInt32 kRgbWhite = 0x00FFFFFF; - -#ifdef __EFI_x86_64__ -/** GOP and related. */ -inline EfiGraphicsOutputProtocol *kGop; -inline UInt16 kStride; -inline EfiGUID kGopGuid; - -/** -@brief Inits the QuickTemplate GUI framework. -*/ -inline Void InitGOP() noexcept { - kGopGuid = EfiGUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); - kGop = nullptr; - - extern EfiBootServices *BS; - - BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr *)&kGop); - - kStride = 4; -} -#endif // __EFI_x86_64__ - -class BVersionString final { - public: - static const CharacterTypeUTF16 *Shared() { return BOOTLOADER_VERSION; } -}; - -/// @brief Writes an EPM partition on the main disk. -/// @param namePart the partition's name -/// @param namePartLength the partition name's length -/// @param bootDev the disk interface. -/// @return -EXTERN_C Boolean boot_write_epm_partition(const Char *namePart, - SizeT namePartLength, - BootDevice *bootDev); - -/// @brief Bootloader main type. -typedef void (*BootMainKind)(HEL::HandoverInformationHeader* handoverInfo); +static inline const UInt32 kRgbRed = 0x000000FF; +static inline const UInt32 kRgbGreen = 0x0000FF00; +static inline const UInt32 kRgbBlue = 0x00FF0000; +static inline const UInt32 kRgbBlack = 0x00000000; +static inline const UInt32 kRgbWhite = 0x00FFFFFF; diff --git a/Private/NewBoot/Source/HEL/AMD64/BootFileReader.cxx b/Private/NewBoot/Source/HEL/AMD64/BootFileReader.cxx index b31f37de..6e279498 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootFileReader.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootFileReader.cxx @@ -25,7 +25,8 @@ /*** @brief File Reader constructor. */ -BFileReader::BFileReader(const CharacterTypeUTF16* path, EfiHandlePtr ImageHandle) { +BFileReader::BFileReader(const CharacterTypeUTF16* path, + EfiHandlePtr ImageHandle) { if (path != nullptr) { SizeT index = 0UL; for (; path[index] != L'\0'; ++index) { @@ -46,12 +47,12 @@ BFileReader::BFileReader(const CharacterTypeUTF16* path, EfiHandlePtr ImageHandl EfiGUID guidImg = EfiGUID(EFI_LOADED_IMAGE_PROTOCOL_GUID); if (BS->HandleProtocol(ImageHandle, &guidImg, (void**)&img) != kEfiOk) { - mWriter.Write(L"NewOS: Fetch-Protocol: No-Such-Protocol").Write(L"\r\n"); + mWriter.Write(L"NewBoot: Fetch-Protocol: No-Such-Protocol").Write(L"\r\n"); this->mErrorCode = kNotSupported; } if (BS->HandleProtocol(img->DeviceHandle, &guidEfp, (void**)&efp) != kEfiOk) { - mWriter.Write(L"NewOS: Fetch-Protocol: No-Such-Protocol").Write(L"\r\n"); + mWriter.Write(L"NewBoot: Fetch-Protocol: No-Such-Protocol").Write(L"\r\n"); this->mErrorCode = kNotSupported; return; } @@ -59,7 +60,7 @@ BFileReader::BFileReader(const CharacterTypeUTF16* path, EfiHandlePtr ImageHandl /// Start doing disk I/O if (efp->OpenVolume(efp, &rootFs) != kEfiOk) { - mWriter.Write(L"NewOS: Fetch-Protocol: No-Such-Volume").Write(L"\r\n"); + mWriter.Write(L"NewBoot: Fetch-Protocol: No-Such-Volume").Write(L"\r\n"); this->mErrorCode = kNotSupported; return; } @@ -68,7 +69,7 @@ BFileReader::BFileReader(const CharacterTypeUTF16* path, EfiHandlePtr ImageHandl if (rootFs->Open(rootFs, &kernelFile, mPath, kEFIFileRead, kEFIReadOnly) != kEfiOk) { - mWriter.Write(L"NewOS: Fetch-Protocol: No-Such-Path: ") + mWriter.Write(L"NewBoot: Fetch-Protocol: No-Such-Path: ") .Write(mPath) .Write(L"\r\n"); this->mErrorCode = kNotSupported; @@ -88,8 +89,7 @@ BFileReader::~BFileReader() { this->mFile = nullptr; } - if (this->mBlob) - BS->FreePool(mBlob); + if (this->mBlob) BS->FreePool(mBlob); BSetMem(this->mPath, 0, kPathLen); } @@ -99,13 +99,11 @@ BFileReader::~BFileReader() { @param ImageHandle used internally. */ Void BFileReader::ReadAll() { - /// Allocate Handover page. - if (this->mErrorCode != kOperationOkay) return; if (mBlob == nullptr) { if (auto err = BS->AllocatePool(EfiLoaderCode, mSizeFile, - (VoidPtr*)&mBlob) != kEfiOk) { + (VoidPtr*)&mBlob) != kEfiOk) { mWriter.Write(L"*** EFI-Code: ").Write(err).Write(L" ***\r\n"); EFI::RaiseHardError(L"NewBoot_PageError", L"Allocation error."); } @@ -118,3 +116,15 @@ Void BFileReader::ReadAll() { mErrorCode = kOperationOkay; } + +/// @brief error code getter. +/// @return the error code. +Int32& BFileReader::Error() { return mErrorCode; } + +/// @brief blob getter. +/// @return the blob. +VoidPtr BFileReader::Blob(){ return mBlob; } + +/// @breif Size getter. +/// @return the size of the file. +UInt64& BFileReader::Size() { return mSizeFile; } diff --git a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx index a5014f92..88d01dfc 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx @@ -13,6 +13,27 @@ #define kMaxBufSize 256 +/** Graphics related. */ + +STATIC EfiGraphicsOutputProtocol *kGop = nullptr; +STATIC UInt16 kStride = 0U; +STATIC EfiGUID kGopGuid; + +/** + @brief Finds and stores the GOP. +*/ + +STATIC Void InitGOP() noexcept { + kGopGuid = EfiGUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); + kGop = nullptr; + + extern EfiBootServices *BS; + + BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr *)&kGop); + + kStride = 4; +} + /// @brief check the BootDevice if suitable. STATIC Bool CheckBootDevice(BootDeviceATA& ataDev) { if (ataDev.Leak().mErr) return false; @@ -23,7 +44,7 @@ STATIC Bool CheckBootDevice(BootDeviceATA& ataDev) { /// @param ImageHandle Handle of this image. /// @param SystemTable The system table of it. /// @return -EFI_EXTERN_C EFI_API Int efi_main(EfiHandlePtr ImageHandle, +EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, EfiSystemTable* SystemTable) { InitEFI(SystemTable); ///! Init the EFI library. InitGOP(); ///! Init the GOP. @@ -48,7 +69,7 @@ EFI_EXTERN_C EFI_API Int efi_main(EfiHandlePtr ImageHandle, if (BS->AllocatePool(EfiLoaderData, sizeof(UInt32), (VoidPtr*)&SizePtr) != kEfiOk) { - EFI::RaiseHardError(L"__bad_alloc", L"NewBoot ran out of memory!"); + EFI::RaiseHardError(L"Bad-Alloc", L"NewBoot ran out of memory!"); } /**** @@ -61,7 +82,7 @@ EFI_EXTERN_C EFI_API Int efi_main(EfiHandlePtr ImageHandle, if (BS->AllocatePool(EfiLoaderData, sizeof(EfiMemoryDescriptor), (VoidPtr*)&Descriptor) != kEfiOk) { - EFI::RaiseHardError(L"__bad_alloc", L"NewBoot ran out of memory!"); + EFI::RaiseHardError(L"Bad-Alloc", L"NewBoot ran out of memory!"); } HEL::HandoverInformationHeader* handoverHdrPtr = nullptr; @@ -77,8 +98,6 @@ EFI_EXTERN_C EFI_API Int efi_main(EfiHandlePtr ImageHandle, vendorTable[6] == 'R' && vendorTable[7] == ' ') { handoverHdrPtr->f_HardwareTables.f_RsdPtr = (VoidPtr)vendorTable; - writer.Write(L"NewBoot: Found RSDP for kernel.\r\n"); - break; } } @@ -114,11 +133,11 @@ EFI_EXTERN_C EFI_API Int efi_main(EfiHandlePtr ImageHandle, handoverHdrPtr->f_FirmwareVendorLen = BStrLen(SystemTable->FirmwareVendor); - // EFI::ExitBootServices(MapKey, ImageHandle); + EFI::ExitBootServices(MapKey, ImageHandle); /// TODO: Read catalog and read NewKernel.exe - // EFI::Stop(); + EFI::Stop(); CANT_REACH(); } diff --git a/Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx b/Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx index 00f7bdd8..d0da09b7 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootPlatform.cxx @@ -15,3 +15,36 @@ EXTERN_C void rt_sti() { asm volatile("sti"); } EXTERN_C void rt_cld() { asm volatile("cld"); } EXTERN_C void rt_std() { asm volatile("std"); } + +EXTERN_C void Out8(UInt16 port, UInt8 value) { + asm volatile("outb %%al, %1" : : "a"(value), "Nd"(port) : "memory"); +} + +EXTERN_C void Out16(UInt16 port, UInt16 value) { + asm volatile("outw %%ax, %1" : : "a"(value), "Nd"(port) : "memory"); +} + +EXTERN_C void Out32(UInt16 port, UInt32 value) { + asm volatile("outl %%eax, %1" : : "a"(value), "Nd"(port) : "memory"); +} + +EXTERN_C UInt8 In8(UInt16 port) { + UInt8 value; + asm volatile("inb %1, %%al" : "=a"(value) : "Nd"(port) : "memory"); + + return value; +} + +EXTERN_C UInt16 In16(UInt16 port) { + UInt16 value; + asm volatile("inw %%dx, %%ax" : "=a"(value) : "d"(port)); + + return value; +} + +EXTERN_C UInt32 In32(UInt16 port) { + UInt32 value; + asm volatile("inl %1, %%eax" : "=a"(value) : "Nd"(port) : "memory"); + + return value; +} diff --git a/Private/NewBoot/Source/HEL/AMD64/BootTextWriter.cxx b/Private/NewBoot/Source/HEL/AMD64/BootTextWriter.cxx index 7ccc37aa..2ebde023 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootTextWriter.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootTextWriter.cxx @@ -21,23 +21,27 @@ @brief puts wrapper over EFI ConOut. */ BTextWriter &BTextWriter::Write(const CharacterTypeUTF16 *str) { - if (*str == 0 || !str) return *this; +#ifdef __DEBUG__ + if (!str || *str == 0) return *this; ST->ConOut->OutputString(ST->ConOut, str); +#endif // ifdef __DEBUG__ return *this; } BTextWriter &BTextWriter::Write(const UChar *str) { - if (*str == 0 || !str) return *this; +#ifdef __DEBUG__ + if (!str || *str == 0) return *this; CharacterTypeUTF16 strTmp[2]; strTmp[1] = 0; - + for (size_t i = 0; str[i] != 0; i++) { strTmp[0] = str[i]; ST->ConOut->OutputString(ST->ConOut, strTmp); } +#endif // ifdef __DEBUG__ return *this; } @@ -46,22 +50,29 @@ BTextWriter &BTextWriter::Write(const UChar *str) { @brief putc wrapper over EFI ConOut. */ BTextWriter &BTextWriter::WriteCharacter(CharacterTypeUTF16 c) { +#ifdef __DEBUG__ EfiCharType str[2]; + str[0] = c; str[1] = 0; ST->ConOut->OutputString(ST->ConOut, str); +#endif // ifdef __DEBUG__ return *this; } BTextWriter &BTextWriter::Write(const Long &x) { +#ifdef __DEBUG__ this->Write(L"0x"); this->_Write(x); +#endif // ifdef __DEBUG__ + return *this; } BTextWriter &BTextWriter::_Write(const Long &x) { +#ifdef __DEBUG__ int y = x / 16; int h = x % 16; @@ -78,5 +89,7 @@ BTextWriter &BTextWriter::_Write(const Long &x) { const char NUMBERS[17] = "0123456789ABCDEF"; this->WriteCharacter(NUMBERS[h]); +#endif // ifdef __DEBUG__ + return *this; } diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index a73b3adb..24f46fd6 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -22,7 +22,7 @@ IMG=epm.img IMG_2=epm-slave.img EMU_FLAGS=-net none -smp 4 -m 4G -M q35 -bios OVMF.fd -device piix3-ide,id=ide -drive id=disk,file=$(IMG),format=raw,if=none -device ide-hd,drive=disk,bus=ide.0 -drive file=fat:rw:CDROM,index=2,format=raw -d int -hdd epm-slave.img -LD_FLAGS=-e efi_main --subsystem=10 +LD_FLAGS=-e Main --subsystem=10 OBJ=$(wildcard *.o) $(wildcard HEL/AMD64/*.obj) |
