From af8a516fc22865abd80d6e26f1541fa3d6bebfdc Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 9 May 2024 00:42:44 +0200 Subject: MHR-23: :boom:, refactors. - Move NewBoot to /Boot, thus making Kernel directory only containing the kernel. Signed-off-by: Amlal El Mahrouss --- Boot/Source/.gitkeep | 0 Boot/Source/BootloaderRsrc.rsrc | 27 +++ Boot/Source/HEL/AMD64/.gitkeep | 0 Boot/Source/HEL/AMD64/BootAHCI.cxx | 20 +++ Boot/Source/HEL/AMD64/BootATA.cxx | 276 +++++++++++++++++++++++++++++++ Boot/Source/HEL/AMD64/BootFileReader.cxx | 173 +++++++++++++++++++ Boot/Source/HEL/AMD64/BootJump.S | 9 + Boot/Source/HEL/AMD64/BootMain.cxx | 244 +++++++++++++++++++++++++++ Boot/Source/HEL/AMD64/BootPlatform.cxx | 102 ++++++++++++ Boot/Source/HEL/AMD64/BootString.cxx | 92 +++++++++++ Boot/Source/HEL/AMD64/BootTextWriter.cxx | 140 ++++++++++++++++ Boot/Source/HEL/AMD64/New+Delete.cxx | 51 ++++++ Boot/Source/HEL/AMD64/Support.cxx | 62 +++++++ Boot/Source/HEL/AMD64/compile_flags.txt | 3 + Boot/Source/HEL/ARM64/.gitkeep | 0 Boot/Source/HEL/POWER/.gitkeep | 0 Boot/Source/HEL/POWER/CoreBootStartup.S | 34 ++++ Boot/Source/HEL/RISCV/.gitkeep | 0 Boot/Source/HEL/RISCV/BootRISCV.S | 22 +++ Boot/Source/Root/EFI/STARTUP.NSH | 2 + Boot/Source/Root/SplashScreen.fmt | 7 + Boot/Source/compile_flags.txt | 4 + Boot/Source/makefile | 106 ++++++++++++ Boot/Source/ovmf.ps1 | 4 + 24 files changed, 1378 insertions(+) create mode 100644 Boot/Source/.gitkeep create mode 100644 Boot/Source/BootloaderRsrc.rsrc create mode 100644 Boot/Source/HEL/AMD64/.gitkeep create mode 100644 Boot/Source/HEL/AMD64/BootAHCI.cxx create mode 100644 Boot/Source/HEL/AMD64/BootATA.cxx create mode 100644 Boot/Source/HEL/AMD64/BootFileReader.cxx create mode 100644 Boot/Source/HEL/AMD64/BootJump.S create mode 100644 Boot/Source/HEL/AMD64/BootMain.cxx create mode 100644 Boot/Source/HEL/AMD64/BootPlatform.cxx create mode 100644 Boot/Source/HEL/AMD64/BootString.cxx create mode 100644 Boot/Source/HEL/AMD64/BootTextWriter.cxx create mode 100644 Boot/Source/HEL/AMD64/New+Delete.cxx create mode 100644 Boot/Source/HEL/AMD64/Support.cxx create mode 100644 Boot/Source/HEL/AMD64/compile_flags.txt create mode 100644 Boot/Source/HEL/ARM64/.gitkeep create mode 100644 Boot/Source/HEL/POWER/.gitkeep create mode 100644 Boot/Source/HEL/POWER/CoreBootStartup.S create mode 100644 Boot/Source/HEL/RISCV/.gitkeep create mode 100644 Boot/Source/HEL/RISCV/BootRISCV.S create mode 100644 Boot/Source/Root/EFI/STARTUP.NSH create mode 100644 Boot/Source/Root/SplashScreen.fmt create mode 100644 Boot/Source/compile_flags.txt create mode 100644 Boot/Source/makefile create mode 100644 Boot/Source/ovmf.ps1 (limited to 'Boot/Source') diff --git a/Boot/Source/.gitkeep b/Boot/Source/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Boot/Source/BootloaderRsrc.rsrc b/Boot/Source/BootloaderRsrc.rsrc new file mode 100644 index 00000000..d79211e7 --- /dev/null +++ b/Boot/Source/BootloaderRsrc.rsrc @@ -0,0 +1,27 @@ +#include "../../Kernel/CompilerKit/Version.hxx" + +1 ICON "../../Kernel/Root/Boot/Icons/boot-logo.ico" + +1 VERSIONINFO +FILEVERSION 1,0,0,0 +PRODUCTVERSION 1,0,0,0 +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "080904E4" + BEGIN + VALUE "CompanyName", "SoftwareLabs" + VALUE "FileDescription", "New OS multi-platform bootloader." + VALUE "FileVersion", BOOTLOADER_VERSION + VALUE "InternalName", "NewBoot" + VALUE "LegalCopyright", "Copyright SoftwareLabs, all rights reserved." + VALUE "OriginalFilename", "NewBoot.exe" + VALUE "ProductName", "NewBoot" + VALUE "ProductVersion", BOOTLOADER_VERSION + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x809, 1252 + END +END diff --git a/Boot/Source/HEL/AMD64/.gitkeep b/Boot/Source/HEL/AMD64/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Boot/Source/HEL/AMD64/BootAHCI.cxx b/Boot/Source/HEL/AMD64/BootAHCI.cxx new file mode 100644 index 00000000..6676f660 --- /dev/null +++ b/Boot/Source/HEL/AMD64/BootAHCI.cxx @@ -0,0 +1,20 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + +------------------------------------------- */ + +/** + * @file AHCI.cxx + * @author Amlal El Mahrouss (amlalelmahrouss@icloud.com) + * @brief AHCI driver. + * @version 0.1 + * @date 2024-02-02 + * + * @copyright Copyright (c) SoftwareLabs + * + */ + +#include +#include +#include diff --git a/Boot/Source/HEL/AMD64/BootATA.cxx b/Boot/Source/HEL/AMD64/BootATA.cxx new file mode 100644 index 00000000..499596ca --- /dev/null +++ b/Boot/Source/HEL/AMD64/BootATA.cxx @@ -0,0 +1,276 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + +------------------------------------------- */ + +/** + * @file ATA.cxx + * @author Amlal El Mahrouss (amlalelmahrouss@icloud.com) + * @brief ATA driver. + * @version 0.1 + * @date 2024-02-02 + * + * @copyright Copyright (c) SoftwareLabs + * + */ + +#include +#include +#include + +/// bugs: 0 + +#define kATADataLen 256 + +static Boolean kATADetected = false; +static Int32 kATADeviceType = kATADeviceCount; +static UInt16 kATAData[kATADataLen] = {0}; + +Boolean boot_ata_detected(Void); + +STATIC Boolean boot_ata_wait_io(UInt16 IO) +{ + for (int i = 0; i < 4; i++) + In8(IO + ATA_REG_STATUS); + +ATAWaitForIO_Retry: + auto statRdy = In8(IO + ATA_REG_STATUS); + + if ((statRdy & ATA_SR_BSY)) + goto ATAWaitForIO_Retry; + +ATAWaitForIO_Retry2: + statRdy = In8(IO + ATA_REG_STATUS); + + if (statRdy & ATA_SR_ERR) + return false; + + if (!(statRdy & ATA_SR_DRDY)) + goto ATAWaitForIO_Retry2; + + return true; +} + +Void boot_ata_select(UInt16 Bus) +{ + if (Bus == ATA_PRIMARY_IO) + Out8(Bus + ATA_REG_HDDEVSEL, ATA_PRIMARY_SEL); + else + Out8(Bus + ATA_REG_HDDEVSEL, ATA_SECONDARY_SEL); +} + +Boolean boot_ata_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster) +{ + if (boot_ata_detected()) + return true; + + BTextWriter writer; + + UInt16 IO = Bus; + + boot_ata_select(IO); + + // Bus init, NEIN bit. + Out8(IO + ATA_REG_NEIN, 1); + + // identify until it's good. +ATAInit_Retry: + auto statRdy = In8(IO + ATA_REG_STATUS); + + if (statRdy & ATA_SR_ERR) + { + writer.Write( + L"New Boot: ATA: Select error, not an IDE based hard-drive.\r"); + + return false; + } + + if ((statRdy & ATA_SR_BSY)) + goto ATAInit_Retry; + + Out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY); + + /// fetch serial info + /// model, speed, number of sectors... + + boot_ata_wait_io(IO); + + for (SizeT indexData = 0ul; indexData < kATADataLen; ++indexData) + { + kATAData[indexData] = In16(IO + ATA_REG_DATA); + } + + OutBus = + (Bus == ATA_PRIMARY_IO) ? BootDeviceATA::kPrimary : BootDeviceATA::kSecondary; + + OutMaster = (Bus == ATA_PRIMARY_IO) ? ATA_MASTER : ATA_SLAVE; + + return true; +} + +Void boot_ata_read(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, SizeT SectorSz, SizeT Size) +{ + UInt8 Command = ((!Master) ? 0xE0 : 0xF0); + + boot_ata_wait_io(IO); + boot_ata_select(IO); + + Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); + + Out8(IO + ATA_REG_SEC_COUNT0, 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_COMMAND, ATA_CMD_READ_PIO); + + boot_ata_wait_io(IO); + + for (SizeT IndexOff = 0; IndexOff < Size; ++IndexOff) + { + boot_ata_wait_io(IO); + Buf[IndexOff] = In16(IO + ATA_REG_DATA); + boot_ata_wait_io(IO); + } +} + +Void boot_ata_write(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, SizeT SectorSz, SizeT Size) +{ + UInt8 Command = ((!Master) ? 0xE0 : 0xF0); + + boot_ata_wait_io(IO); + boot_ata_select(IO); + + Out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); + + Out8(IO + ATA_REG_SEC_COUNT0, 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_COMMAND, ATA_CMD_WRITE_PIO); + + boot_ata_wait_io(IO); + + for (SizeT IndexOff = 0; IndexOff < Size; ++IndexOff) + { + boot_ata_wait_io(IO); + Out16(IO + ATA_REG_DATA, Buf[IndexOff]); + boot_ata_wait_io(IO); + } +} + +/// @check is ATA detected? +Boolean boot_ata_detected(Void) +{ + return kATADetected; +} + +/*** + * + * + * @brief ATA Device class. + * + * + */ + +/** + * @brief ATA Device constructor. + * @param void none. + */ +BootDeviceATA::BootDeviceATA() noexcept +{ + if (boot_ata_init(ATA_PRIMARY_IO, true, this->Leak().mBus, + this->Leak().mMaster) || + boot_ata_init(ATA_SECONDARY_IO, true, this->Leak().mBus, + this->Leak().mMaster)) + { + kATADetected = true; + } +} +/** + * @brief Is ATA detected? + */ +BootDeviceATA::operator bool() +{ + return boot_ata_detected(); +} + +/** + @brief Read Buf from disk + @param Sz Sector size + @param Buf buffer +*/ +BootDeviceATA& BootDeviceATA::Read(CharacterTypeUTF8* Buf, const SizeT& SectorSz) +{ + if (!boot_ata_detected()) + { + Leak().mErr = true; + return *this; + } + + this->Leak().mErr = false; + + if (!Buf || SectorSz < 1) + return *this; + + auto lba = this->Leak().mBase / SectorSz; + + boot_ata_read(lba, this->Leak().mBus, this->Leak().mMaster, + Buf, SectorSz, this->Leak().mSize); + + return *this; +} + +/** + @brief Write Buf into disk + @param Sz Sector size + @param Buf buffer +*/ +BootDeviceATA& BootDeviceATA::Write(CharacterTypeUTF8* Buf, const SizeT& SectorSz) +{ + if (!boot_ata_detected()) + { + Leak().mErr = true; + return *this; + } + + Leak().mErr = false; + + if (!Buf || SectorSz < 1) + return *this; + + auto lba = this->Leak().mBase / SectorSz; + + boot_ata_write(lba, this->Leak().mBus, this->Leak().mMaster, + Buf, SectorSz, this->Leak().mSize); + + return *this; +} + +/** + * @brief ATA trait getter. + * @return BootDeviceATA::ATATrait& the drive config. + */ +BootDeviceATA::ATATrait& BootDeviceATA::Leak() +{ + return mTrait; +} + +/*** + @brief Getter, gets the number of sectors inside the drive. +*/ +SizeT BootDeviceATA::GetSectorsCount() noexcept +{ + return (kATAData[61] << 16) | kATAData[60]; +} + +SizeT BootDeviceATA::GetDiskSize() noexcept +{ + return this->GetSectorsCount() * BootDeviceATA::kSectorSize; +} diff --git a/Boot/Source/HEL/AMD64/BootFileReader.cxx b/Boot/Source/HEL/AMD64/BootFileReader.cxx new file mode 100644 index 00000000..61adc4f3 --- /dev/null +++ b/Boot/Source/HEL/AMD64/BootFileReader.cxx @@ -0,0 +1,173 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + + File: FileReader.cxx + Purpose: New Boot FileReader, + Read complete file and store it in a buffer. + +------------------------------------------- */ + +#include +#include +#include +#include +#include +#include + +/// @file BootFileReader +/// @brief Bootloader File reader. +/// BUGS: 0 + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// +/// +/// @name BFileReader class +/// @brief Reads the file as a blob. +/// +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/*** + @brief File Reader constructor. +*/ +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; +} + +BFileReader::~BFileReader() +{ + if (this->mFile) + { + this->mFile->Close(this->mFile); + this->mFile = nullptr; + } + + if (this->mBlob) + BS->FreePool(mBlob); + + 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; +} + +/// @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/Boot/Source/HEL/AMD64/BootJump.S b/Boot/Source/HEL/AMD64/BootJump.S new file mode 100644 index 00000000..af278cc7 --- /dev/null +++ b/Boot/Source/HEL/AMD64/BootJump.S @@ -0,0 +1,9 @@ +.global rt_jump_to_address +.text + +.code64 +.intel_syntax noprefix + +rt_jump_to_address: + jmp rcx + ret diff --git a/Boot/Source/HEL/AMD64/BootMain.cxx b/Boot/Source/HEL/AMD64/BootMain.cxx new file mode 100644 index 00000000..f422b661 --- /dev/null +++ b/Boot/Source/HEL/AMD64/BootMain.cxx @@ -0,0 +1,244 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + +------------------------------------------- */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/// make the compiler shut up. +#ifndef kMachineModel +#define kMachineModel "NeWS HD" +#endif // !kMachineModel + +/** Graphics related. */ + +STATIC EfiGraphicsOutputProtocol* kGop = nullptr; +STATIC UInt16 kStride = 0U; +STATIC EfiGUID kGopGuid; + +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; + + 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; + 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. + + BTextWriter writer; + + /// Splash screen stuff + + writer.Write(L"SoftwareLabs (R) New Boot: ") + .Write(BVersionString::Shared()); + + 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(); + + *MapKey = 0; + *SizePtr = 0; + + HEL::HandoverInformationHeader* handoverHdrPtr = + new HEL::HandoverInformationHeader(); + + for (SizeT indexVT = 0; indexVT < SystemTable->NumberOfTableEntries; + ++indexVT) + { + volatile Char* vendorTable = reinterpret_cast( + 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 diskFormatter; + + /// if not formated yet, then format it with the following folders: + /// /, /Boot, /Applications. + if (!diskFormatter.IsPartitionValid()) + { + BDiskFormatFactory::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/Boot/Source/HEL/AMD64/BootPlatform.cxx b/Boot/Source/HEL/AMD64/BootPlatform.cxx new file mode 100644 index 00000000..9d125ec6 --- /dev/null +++ b/Boot/Source/HEL/AMD64/BootPlatform.cxx @@ -0,0 +1,102 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + +------------------------------------------- */ + +#include +#include +#include +#include "HALKit/AMD64/Processor.hpp" + +#ifdef __STANDALONE__ + +EXTERN_C void rt_hlt() +{ + asm volatile("hlt"); +} + +EXTERN_C void rt_cli() +{ + asm volatile("cli"); +} + +EXTERN_C void rt_sti() +{ + asm volatile("sti"); +} + +EXTERN_C void rt_cld() +{ + asm volatile("cld"); +} + +EXTERN_C void rt_std() +{ + asm volatile("std"); +} + +EXTERN_C void Out8(UInt16 port, UInt8 value) +{ + asm volatile("outb %%al, %1" + : + : "a"(value), "Nd"(port) + : "memory"); +} + +EXTERN_C void Out16(UInt16 port, UInt16 value) +{ + asm volatile("outw %%ax, %1" + : + : "a"(value), "Nd"(port) + : "memory"); +} + +EXTERN_C void Out32(UInt16 port, UInt32 value) +{ + asm volatile("outl %%eax, %1" + : + : "a"(value), "Nd"(port) + : "memory"); +} + +EXTERN_C UInt8 In8(UInt16 port) +{ + UInt8 value; + asm volatile("inb %1, %%al" + : "=a"(value) + : "Nd"(port) + : "memory"); + + return value; +} + +EXTERN_C UInt16 In16(UInt16 port) +{ + UInt16 value; + asm volatile("inw %%dx, %%ax" + : "=a"(value) + : "d"(port)); + + return value; +} + +EXTERN_C UInt32 In32(UInt16 port) +{ + UInt32 value; + asm volatile("inl %1, %%eax" + : "=a"(value) + : "Nd"(port) + : "memory"); + + return value; +} + +#else + +void rt_hlt() +{ + NewOS::HAL::rt_halt(); +} + +#endif // 0 diff --git a/Boot/Source/HEL/AMD64/BootString.cxx b/Boot/Source/HEL/AMD64/BootString.cxx new file mode 100644 index 00000000..a93943cf --- /dev/null +++ b/Boot/Source/HEL/AMD64/BootString.cxx @@ -0,0 +1,92 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + + File: String.cxx + Purpose: NewBoot string library + + Revision History: + + + +------------------------------------------- */ + +#include +#include +#include + +/// bugs 0 + +///////////////////////////////////////////////////////////////////////////////////////////////////////// + +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 cnt = 0; + + while (*ptr != (CharacterTypeUTF16)0) + { + ++ptr; + ++cnt; + } + + return cnt; +} + +NewOS::SizeT BSetMem(CharacterTypeUTF16* src, const CharacterTypeUTF16 byte, const NewOS::SizeT len) +{ + if (!src) + return 0; + + NewOS::SizeT cnt = 0UL; + + while (*src != 0) + { + if (cnt > len) + break; + + *src = byte; + ++src; + + ++cnt; + } + + return cnt; +} + +NewOS::SizeT BSetMem(CharacterTypeUTF8* src, const CharacterTypeUTF8 byte, const NewOS::SizeT len) +{ + if (!src) + return 0; + + NewOS::SizeT cnt = 0UL; + + while (*src != 0) + { + if (cnt > len) + break; + + *src = byte; + ++src; + + ++cnt; + } + + return cnt; +} diff --git a/Boot/Source/HEL/AMD64/BootTextWriter.cxx b/Boot/Source/HEL/AMD64/BootTextWriter.cxx new file mode 100644 index 00000000..30c328ad --- /dev/null +++ b/Boot/Source/HEL/AMD64/BootTextWriter.cxx @@ -0,0 +1,140 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + + File: String.cxx + Purpose: NewBoot string library + + Revision History: + + + +------------------------------------------- */ + +#include +#include +#include +#include + +/// BUGS: 0 + +///////////////////////////////////////////////////////////////////////////////////////////////////////// + +/** +@brief puts wrapper over EFI ConOut. +*/ +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; +} + +/// @brief UTF-8 equivalent of Write (UTF-16). +/// @param str the input string. +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; +} + +/** +@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__ + UInt64 y = (x > 0 ? x : -x) / 16; + UInt64 h = (x > 0 ? x : -x) % 16; + + if (y) + this->_Write(y); + + /* fail if the hex number is not base-16 */ + if (h > 15) + { + this->WriteCharacter('?'); + return *this; + } + + if (y < 0) + y = -y; + + const char NUMBERS[17] = "0123456789ABCDEF"; + + this->WriteCharacter(NUMBERS[h]); +#endif // ifdef __DEBUG__ + + return *this; +} diff --git a/Boot/Source/HEL/AMD64/New+Delete.cxx b/Boot/Source/HEL/AMD64/New+Delete.cxx new file mode 100644 index 00000000..3316ac1d --- /dev/null +++ b/Boot/Source/HEL/AMD64/New+Delete.cxx @@ -0,0 +1,51 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + +------------------------------------------- */ + +#include +#include +#include +#include /* Since we're using GCC for this EFI program. */ + +#ifdef __STANDALONE__ + +/// @brief Allocates a new object. +/// @param sz the size. +/// @return +void* operator new(size_t sz) +{ + void* buf = nullptr; + BS->AllocatePool(EfiMemoryType::EfiLoaderData, sz, &buf); + + return buf; +} + +/// @brief Allocates a new object. +/// @param sz the size. +/// @return +void* operator new[](size_t sz) +{ + void* buf = nullptr; + BS->AllocatePool(EfiMemoryType::EfiLoaderData, sz, &buf); + + return buf; +} + +/// @brief Deletes the object. +/// @param buf the object. +void operator delete(void* buf) +{ + BS->FreePool(buf); +} + +/// @brief Deletes the object (array specific). +/// @param buf the object. +/// @param size it's size. +void operator delete(void* buf, size_t size) +{ + BS->FreePool(buf); +} + +#endif // Inactive diff --git a/Boot/Source/HEL/AMD64/Support.cxx b/Boot/Source/HEL/AMD64/Support.cxx new file mode 100644 index 00000000..12c136f8 --- /dev/null +++ b/Boot/Source/HEL/AMD64/Support.cxx @@ -0,0 +1,62 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + +------------------------------------------- */ + +#include +#include +#include + +#ifdef __STANDALONE__ + +/// @brief memset definition in C++. +/// @param dst destination pointer. +/// @param byte value to fill in. +/// @param len length of of src. +EXTERN_C VoidPtr memset(void* dst, int byte, long long unsigned int len) +{ + for (size_t i = 0UL; i < len; ++i) + { + ((int*)dst)[i] = byte; + } + + return dst; +} + +/// @brief memcpy definition in C++. +/// @param dst destination pointer. +/// @param src source pointer. +/// @param len length of of src. +EXTERN_C VoidPtr memcpy(void* dst, const void* src, long long unsigned int len) +{ + for (size_t i = 0UL; i < len; ++i) + { + ((int*)dst)[i] = ((int*)src)[i]; + } + + return dst; +} + +/// @brief strlen definition in C++. +EXTERN_C size_t strlen(const char* whatToCheck) +{ + if (!whatToCheck || *whatToCheck == 0) + return 0; + + SizeT len = 0; + + while (whatToCheck[len] != 0) + { + ++len; + } + + return len; +} + +/// @brief somthing specific to the microsoft ABI, regarding checking the stack. +EXTERN_C void ___chkstk_ms(void) +{ +} + +#endif diff --git a/Boot/Source/HEL/AMD64/compile_flags.txt b/Boot/Source/HEL/AMD64/compile_flags.txt new file mode 100644 index 00000000..e4515efe --- /dev/null +++ b/Boot/Source/HEL/AMD64/compile_flags.txt @@ -0,0 +1,3 @@ +-std=c++20 +-I../../../ +-I../../../../ diff --git a/Boot/Source/HEL/ARM64/.gitkeep b/Boot/Source/HEL/ARM64/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Boot/Source/HEL/POWER/.gitkeep b/Boot/Source/HEL/POWER/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Boot/Source/HEL/POWER/CoreBootStartup.S b/Boot/Source/HEL/POWER/CoreBootStartup.S new file mode 100644 index 00000000..f8ff99d7 --- /dev/null +++ b/Boot/Source/HEL/POWER/CoreBootStartup.S @@ -0,0 +1,34 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + +------------------------------------------- */ + +.section .boot_hdr +.align 4 + +/* NewBoot boot header begin */ + +boot_hdr_mag: + .ascii "CB" +boot_hdr_name: + // it has to match ten bytes. + .asciz "NewBoot\0\0\0" +boot_hdr_ver: + .word 0x104 +boot_hdr_proc: + .long bootloader_start + +/* NewOS boot header end */ + +.extern bootloader_main +.extern bootloader_stack + +.globl bootloader_start +bootloader_start: + mflr 4 /* real address of .Laddr */ + lwz 0,(bootloader_stack-bootloader_start)(4) /* stack address location */ + mr 1,0 /* use user defined stack */ + + bl bootloader_main + blr diff --git a/Boot/Source/HEL/RISCV/.gitkeep b/Boot/Source/HEL/RISCV/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Boot/Source/HEL/RISCV/BootRISCV.S b/Boot/Source/HEL/RISCV/BootRISCV.S new file mode 100644 index 00000000..7a7e7db0 --- /dev/null +++ b/Boot/Source/HEL/RISCV/BootRISCV.S @@ -0,0 +1,22 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + +------------------------------------------- */ + +.section .init +.align 4 + +/* NewOS kernel header begin */ + +k_hdr_mag: + .ascii "LX" +k_hdr_name: + // it has to match ten bytes. + .asciz "New OS\0\0\0\0" +k_hdr_ver: + .word 0x104 +k_hdr_proc: + .long __bootloader_start + +/* end */ \ No newline at end of file diff --git a/Boot/Source/Root/EFI/STARTUP.NSH b/Boot/Source/Root/EFI/STARTUP.NSH new file mode 100644 index 00000000..d29ba8fd --- /dev/null +++ b/Boot/Source/Root/EFI/STARTUP.NSH @@ -0,0 +1,2 @@ +fs0: +BOOT\BOOTX64.EFI diff --git a/Boot/Source/Root/SplashScreen.fmt b/Boot/Source/Root/SplashScreen.fmt new file mode 100644 index 00000000..863d7b62 --- /dev/null +++ b/Boot/Source/Root/SplashScreen.fmt @@ -0,0 +1,7 @@ +================================================================== +Welcome to NeWS. +Brought to you by: Amlal EL Mahrouss. +* NewBoot, NewKernel: Amlal EL Mahrouss. +This copy can boot directly to NewKernel (Unified System). +Copyright SoftwareLabs, all rights reserved. +================================================================== diff --git a/Boot/Source/compile_flags.txt b/Boot/Source/compile_flags.txt new file mode 100644 index 00000000..c74d22b2 --- /dev/null +++ b/Boot/Source/compile_flags.txt @@ -0,0 +1,4 @@ +-std=c++20 +-I../ +-I../../ +-D__NEWOS_AMD64__ diff --git a/Boot/Source/makefile b/Boot/Source/makefile new file mode 100644 index 00000000..70fa7232 --- /dev/null +++ b/Boot/Source/makefile @@ -0,0 +1,106 @@ +################################################## +# (C) SoftwareLabs, all rights reserved. +# This is the bootloader makefile. +################################################## + +CC_GNU=x86_64-w64-mingw32-g++ +LD_GNU=x86_64-w64-mingw32-ld + +WINDRES=x86_64-w64-mingw32-windres + +ADD_FILE=touch +COPY=cp +HTTP_GET=wget + +ifeq ($(shell uname), Windows_NT) +EMU=qemu-system-x86_64w +else +EMU=qemu-system-x86_64 +endif + +ifeq ($(NEWS_MODEL), ) +NEWOS_MODEL=-DkMachineModel="\"Generic NeWS HD\"" +endif + +IMG=epm.img +IMG_2=epm-slave.img + +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 \ + file=fat:rw:Root,index=2,format=raw -d int -hdd epm-slave.img + +LD_FLAGS=-e Main --subsystem=10 + +ifeq ($(NEWS_STANDLONE), ) +OBJ=*.o ../../Kernel/Objects/*.obj +else +RESCMD=$(WINDRES) BootloaderRsrc.rsrc -O coff -o BootloaderRsrc.o +STANDALONE_MACRO=-D__STANDALONE__ +OBJ=*.o +endif + +REM=rm +REM_FLAG=-f + +FLAG_ASM=-f win64 +FLAG_GNU=-fshort-wchar -D__EFI_x86_64__ -mno-red-zone -D__KERNEL__ -D__NEWBOOT__ \ + -DEFI_FUNCTION_WRAPPER -I../ -I../../Kernel -I./ -c -nostdlib -fno-rtti -fno-exceptions \ + -std=c++20 -D__HAVE_MAHROUSS_APIS__ -D__MAHROUSS__ -D__BOOTLOADER__ -I./ + +BOOT_LOADER=NewBoot.exe +KERNEL=NewKernel.exe + +.PHONY: invalid-recipe +invalid-recipe: + @echo "invalid-recipe: Use make compile- instead." + +.PHONY: all +all: compile-amd64 + mkdir -p Root/EFI/BOOT + $(LD_GNU) $(OBJ) $(LD_FLAGS) -o $(BOOT_LOADER) + $(COPY) $(BOOT_LOADER) Root/EFI/BOOT/BOOTX64.EFI + $(COPY) $(BOOT_LOADER) Root/EFI/BOOT/NEWBOOT.EFI + $(COPY) ../../Kernel/$(KERNEL) Root/$(KERNEL) + +ifneq ($(DEBUG_SUPPORT), ) +DEBUG = -D__DEBUG__ +endif + +.PHONY: compile-amd64 +compile-amd64: + $(RESCMD) + $(CC_GNU) $(NEWOS_MODEL) $(STANDALONE_MACRO) $(FLAG_GNU) $(DEBUG) $(wildcard HEL/AMD64/*.cxx) $(wildcard HEL/AMD64/*.S) $(wildcard *.cxx) + +.PHONY: run-efi-amd64 +run-efi-amd64: + $(EMU) $(EMU_FLAGS) + +# img_2 is the rescue disk. img is the bootable disk, as provided by the NeWS. +.PHONY: epm-img +epm-img: + qemu-img create -f raw $(IMG) 10G + qemu-img create -f raw $(IMG_2) 512M + +.PHONY: download-edk +download-edk: + $(HTTP_GET) https://retrage.github.io/edk2-nightly/bin/DEBUGX64_OVMF.fd -O OVMF.fd + +BINS=*.bin +EXECUTABLES=NewBoot.exe NewKernel.exe OVMF.fd + +TARGETS=$(REM_FLAG) $(OBJ) $(BIN) $(IMG) $(IMG_2) $(EXECUTABLES) + +.PHONY: clean +clean: + $(REM) $(TARGETS) + +.PHONY: help +help: + @echo "=== HELP ===" + @echo "epm-img: Format a disk using the Explicit Partition Map." + @echo "gpt-img: Format a disk using the Explicit Partition Map." + @echo "clean: clean bootloader." + @echo "bootloader-amd64: Build bootloader. (PC AMD64)" + @echo "run-efi-amd64: Run bootloader. (PC AMD64)" diff --git a/Boot/Source/ovmf.ps1 b/Boot/Source/ovmf.ps1 new file mode 100644 index 00000000..5a2c5f0e --- /dev/null +++ b/Boot/Source/ovmf.ps1 @@ -0,0 +1,4 @@ +$client = new-object System.Net.WebClient +$output = "$PSScriptRoot\OVMF.fd" + +$client.DownloadFile("https://retrage.github.io/edk2-nightly/bin/DEBUGX64_OVMF.fd", $output) -- cgit v1.2.3 From 915c14eb3b717bbd168d069e296a4246c6aef117 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 9 May 2024 19:23:04 +0200 Subject: MHR-23: Starting to implement SMP for AMD64. Signed-off-by: Amlal El Mahrouss --- Boot/Source/HEL/AMD64/BootMain.cxx | 3 +- Kernel/HALKit/64x0/ReadMe.md | 4 +++ Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx | 11 ++++--- .../HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp | 33 ++++++++++--------- Kernel/HALKit/AMD64/HalHardwareMP.cpp | 4 +-- Kernel/HALKit/AMD64/HalInterruptAPI.asm | 20 ++++++------ Kernel/HALKit/AMD64/ReadMe.md | 4 +++ Kernel/HALKit/ARM64/ReadMe.md | 4 +++ Kernel/HALKit/POWER/ReadMe.md | 4 +++ Kernel/HALKit/RISCV/ReadMe.md | 4 +++ Kernel/KernelKit/DebugOutput.hpp | 28 +++++++++++++++- Kernel/Source/AppMain.cxx | 10 ++++++ Kernel/Source/CodeManager.cxx | 37 +++++++++++----------- 13 files changed, 115 insertions(+), 51 deletions(-) create mode 100644 Kernel/HALKit/64x0/ReadMe.md create mode 100644 Kernel/HALKit/AMD64/ReadMe.md create mode 100644 Kernel/HALKit/ARM64/ReadMe.md create mode 100644 Kernel/HALKit/POWER/ReadMe.md create mode 100644 Kernel/HALKit/RISCV/ReadMe.md (limited to 'Boot/Source') diff --git a/Boot/Source/HEL/AMD64/BootMain.cxx b/Boot/Source/HEL/AMD64/BootMain.cxx index f422b661..649596b4 100644 --- a/Boot/Source/HEL/AMD64/BootMain.cxx +++ b/Boot/Source/HEL/AMD64/BootMain.cxx @@ -90,7 +90,7 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, for (SizeT indexVT = 0; indexVT < SystemTable->NumberOfTableEntries; ++indexVT) { - volatile Char* vendorTable = reinterpret_cast( + Char* vendorTable = reinterpret_cast( SystemTable->ConfigurationTable[indexVT].VendorTable); /// ACPI's 'RSD PTR', which contains hardware tables (MADT, FACP...) @@ -99,6 +99,7 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, vendorTable[4] == 'P' && vendorTable[5] == 'T' && vendorTable[6] == 'R' && vendorTable[7] == ' ') { + writer.Write(L"New Boot: Found ACPI RSD PTR!\r"); handoverHdrPtr->f_HardwareTables.f_RsdPtr = (VoidPtr)vendorTable; break; diff --git a/Kernel/HALKit/64x0/ReadMe.md b/Kernel/HALKit/64x0/ReadMe.md new file mode 100644 index 00000000..6744f602 --- /dev/null +++ b/Kernel/HALKit/64x0/ReadMe.md @@ -0,0 +1,4 @@ +64x0 Hardware Abstraction Layer + +- Supported CPU: SoftwareLabs 64x0 +- Supported Firmware: CoreBoot \ No newline at end of file diff --git a/Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx b/Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx index 3609165f..f4c9226e 100644 --- a/Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx +++ b/Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx @@ -75,7 +75,7 @@ namespace NewOS SDT* xsdt = (SDT*)(rsdPtr->XsdtAddress >> (rsdPtr->XsdtAddress & 0xFFF)); - SizeT num = xsdt->Length + sizeof(SDT) / 8; + SizeT num = (xsdt->Length + sizeof(SDT)) / 8; this->fEntries = num; @@ -83,10 +83,11 @@ namespace NewOS kcout << "ACPI: Address of XSDT: " << hex_number((UIntPtr)xsdt) << endl; constexpr short ACPI_SIGNATURE_LENGTH = 4; + SizeT offsetToAdd = 0UL; for (Size index = 0; index < num; ++index) { - SDT* sdt = (SDT*)*((UInt64*)(UInt64)xsdt + sizeof(SDT) + (index * 8)); + SDT* sdt = &(xsdt[index]) + offsetToAdd; for (int signature_index = 0; signature_index < 4; signature_index++) { @@ -94,11 +95,13 @@ namespace NewOS break; if (signature_index == 3) - return ErrorOr(reinterpret_cast((SDT*)sdt)); + return ErrorOr(reinterpret_cast(sdt)); } + + offsetToAdd = sdt->Length; } - return ErrorOr{-1}; + return ErrorOr{nullptr}; } /*** diff --git a/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp b/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp index 2129d790..ec6d47da 100644 --- a/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp +++ b/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp @@ -23,12 +23,12 @@ namespace NewOS::HAL constexpr Int32 kThreadBoot = 4; /* - * - * this is used to store info about the current running thread - * we use this struct to determine if we can use it, or mark it as used or on - * sleep. - * - */ + * + * this is used to store info about the current running thread + * we use this struct to determine if we can use it, or mark it as used or on + * sleep. + * + */ struct ProcessorInfoAMD64 final { @@ -51,12 +51,12 @@ namespace NewOS::HAL { struct MadtAddress final { + UInt32 fAddress; UInt32 fFlags; // 1 = Dual Legacy PICs installed - UInt32 fPhysicalAddress; Char fType; Char fRecLen; // record length - } Madt[]; + } fMadt[]; }; struct MadtProcessorLocalApic final @@ -104,20 +104,23 @@ namespace NewOS::HAL void hal_system_get_cores(voidPtr rsdPtr) { - kcout << "New OS: Constructing ACPIFactoryInterface...\r"; - auto acpi = ACPIFactoryInterface(rsdPtr); kApicMadt = acpi.Find(kApicSignature).Leak().Leak(); if (kApicMadt) { - kcout << "New OS: Successfuly fetched the MADT!\r"; + kcout << "New OS: Successfuly fetched the cores!\r"; kApicInfoBlock = (MadtType*)kApicMadt; + + kcout << "New OS: Revision: "; + kcout.HexNumber(kApicInfoBlock->Revision).EndLine(); + + ke_stop(RUNTIME_CHECK_BOOTSTRAP); } - else - { - MUST_PASS(false); - } + else + { + ke_stop(RUNTIME_CHECK_BOOTSTRAP); + } } } // namespace NewOS::HAL diff --git a/Kernel/HALKit/AMD64/HalHardwareMP.cpp b/Kernel/HALKit/AMD64/HalHardwareMP.cpp index 2ebf45fd..83e99ef8 100644 --- a/Kernel/HALKit/AMD64/HalHardwareMP.cpp +++ b/Kernel/HALKit/AMD64/HalHardwareMP.cpp @@ -16,7 +16,7 @@ namespace NewOS { HAL::rt_cli(); - stack->Rcx = 0; + HAL::rt_sti(); } @@ -27,7 +27,7 @@ namespace NewOS { HAL::rt_cli(); - stack->Rcx = 1; + HAL::rt_sti(); } diff --git a/Kernel/HALKit/AMD64/HalInterruptAPI.asm b/Kernel/HALKit/AMD64/HalInterruptAPI.asm index e4063388..875a1abc 100644 --- a/Kernel/HALKit/AMD64/HalInterruptAPI.asm +++ b/Kernel/HALKit/AMD64/HalInterruptAPI.asm @@ -14,16 +14,16 @@ %define kInterruptId 0x21 %macro IntExp 1 -global __HCR_INT_%1 -__HCR_INT_%1: +global __NEW_INT_%1 +__NEW_INT_%1: cld iretq %endmacro %macro IntNormal 1 -global __HCR_INT_%1 -__HCR_INT_%1: +global __NEW_INT_%1 +__NEW_INT_%1: cld iretq @@ -54,7 +54,7 @@ IntNormal 4 IntNormal 5 ;; Invalid opcode interrupt -__HCR_INT_6: +__NEW_INT_6: cli push rax @@ -75,7 +75,7 @@ IntExp 11 IntExp 12 -__HCR_INT_13: +__NEW_INT_13: cli push rax @@ -88,7 +88,7 @@ __HCR_INT_13: sti iretq -__HCR_INT_14: +__NEW_INT_14: cli push rax @@ -135,7 +135,7 @@ IntNormal 41 IntNormal 42 IntNormal 43 -__HCR_INT_44: +__NEW_INT_44: cli ;; TODO: CoreEvents dispatch routine. @@ -153,7 +153,7 @@ IntNormal 47 IntNormal 48 IntNormal 49 -__HCR_INT_50: +__NEW_INT_50: cli ;; todo handle system calls. @@ -217,6 +217,6 @@ section .data kInterruptVectorTable: %assign i 0 %rep 256 - dq __HCR_INT_%+i + dq __NEW_INT_%+i %assign i i+1 %endrep diff --git a/Kernel/HALKit/AMD64/ReadMe.md b/Kernel/HALKit/AMD64/ReadMe.md new file mode 100644 index 00000000..0be48c77 --- /dev/null +++ b/Kernel/HALKit/AMD64/ReadMe.md @@ -0,0 +1,4 @@ +AMD64 Hardware Abstraction Layer + +- Supported CPU: AMD64 CPU +- Supported Firmware: EDK 2 \ No newline at end of file diff --git a/Kernel/HALKit/ARM64/ReadMe.md b/Kernel/HALKit/ARM64/ReadMe.md new file mode 100644 index 00000000..89679e18 --- /dev/null +++ b/Kernel/HALKit/ARM64/ReadMe.md @@ -0,0 +1,4 @@ +ARM64 Hardware Abstraction Layer + +- Supported CPU: Qualcomm CPU +- Supported Firmware: EDK 2 \ No newline at end of file diff --git a/Kernel/HALKit/POWER/ReadMe.md b/Kernel/HALKit/POWER/ReadMe.md new file mode 100644 index 00000000..a9751581 --- /dev/null +++ b/Kernel/HALKit/POWER/ReadMe.md @@ -0,0 +1,4 @@ +POWER Hardware Abstraction Layer + +- Supported CPU: POWER +- Supported Firmware: CoreBoot \ No newline at end of file diff --git a/Kernel/HALKit/RISCV/ReadMe.md b/Kernel/HALKit/RISCV/ReadMe.md new file mode 100644 index 00000000..b099aa31 --- /dev/null +++ b/Kernel/HALKit/RISCV/ReadMe.md @@ -0,0 +1,4 @@ +RISCV64 Hardware Abstraction Layer + +- Supported CPU: RISCV64 +- Supported Firmware: CoreBoot \ No newline at end of file diff --git a/Kernel/KernelKit/DebugOutput.hpp b/Kernel/KernelKit/DebugOutput.hpp index 7fde69aa..656fe7a9 100644 --- a/Kernel/KernelKit/DebugOutput.hpp +++ b/Kernel/KernelKit/DebugOutput.hpp @@ -15,7 +15,7 @@ #define kDebugUnboundPort 0x0FEED -#define kDebugMag0 'H' +#define kDebugMag0 'N' #define kDebugMag1 'D' #define kDebugMag2 'B' #define kDebugMag3 'G' @@ -27,6 +27,12 @@ namespace NewOS { + class TerminalDevice; + + inline TerminalDevice& end_line(); + inline TerminalDevice& number(const Long& x); + inline TerminalDevice& hex_number(const Long& x); + // @brief Emulates a VT100 terminal. class TerminalDevice final : public DeviceInterface { @@ -34,10 +40,30 @@ namespace NewOS TerminalDevice(void (*print)(const Char*), void (*get)(const Char*)) : DeviceInterface(print, get) { + } virtual ~TerminalDevice() { + + } + + TerminalDevice& Number(const Long Data) noexcept + { + number(Data); + return *this; + } + + TerminalDevice& HexNumber(const Long Data) noexcept + { + number(Data); + return *this; + } + + TerminalDevice& EndLine() noexcept + { + end_line(); + return *this; } /// @brief returns device name (terminal name) diff --git a/Kernel/Source/AppMain.cxx b/Kernel/Source/AppMain.cxx index b91d6082..4f5de11f 100644 --- a/Kernel/Source/AppMain.cxx +++ b/Kernel/Source/AppMain.cxx @@ -22,6 +22,7 @@ #include #include #include +#include namespace Detail { @@ -177,6 +178,12 @@ namespace Detail return fNewFS; } }; + + STATIC NewOS::Void AppWatchdogThread(NewOS::Void) + { + NewOS::kcout << "SystemSanityThread: Exiting process..."; + NewOS::ProcessScheduler::Shared().Leak().GetCurrent().Leak().Exit(0); + } } // namespace Detail /// @file Main microkernel entrypoint. @@ -186,6 +193,9 @@ EXTERN_C NewOS::Void AppMain(NewOS::Void) /// Now run kernel loop, until no process are running. Detail::FilesystemWizard wizard; // automatic. + auto cWatchdogThreadName = "SystemSanityThread"; + NewOS::execute_from_image((NewOS::MainKind)Detail::AppWatchdogThread, cWatchdogThreadName); + while (NewOS::ProcessScheduler::Shared().Leak().Run() > 0) { ; diff --git a/Kernel/Source/CodeManager.cxx b/Kernel/Source/CodeManager.cxx index 39917163..001795ce 100644 --- a/Kernel/Source/CodeManager.cxx +++ b/Kernel/Source/CodeManager.cxx @@ -8,22 +8,23 @@ #include #include -using namespace NewOS; - -/// @brief Executes a new process from a function. kernel code only. -/// @note This sets up a new stack, anything on the main function that calls the kernel will not be accessible. -/// @param main the start of the process. -/// @return if the process was started or not. -bool execute_from_image(MainKind main, const char* processName) noexcept +namespace NewOS { - if (!main) - return false; - - ProcessHeader proc((VoidPtr)main); - proc.Kind = ProcessHeader::kDriverKind; - rt_copy_memory((VoidPtr)processName, proc.Name, rt_string_len(proc.Name)); - - Ref refProc = proc; - - return ProcessScheduler::Shared().Leak().Add(refProc); -} \ No newline at end of file + /// @brief Executes a new process from a function. kernel code only. + /// @note This sets up a new stack, anything on the main function that calls the kernel will not be accessible. + /// @param main the start of the process. + /// @return if the process was started or not. + bool execute_from_image(MainKind main, const char* processName) noexcept + { + if (!main) + return false; + + ProcessHeader proc((VoidPtr)main); + proc.Kind = ProcessHeader::kDriverKind; + rt_copy_memory((VoidPtr)processName, proc.Name, rt_string_len(proc.Name)); + + Ref refProc = proc; + + return ProcessScheduler::Shared().Leak().Add(refProc); + } +} // namespace NewOS \ No newline at end of file -- cgit v1.2.3 From 84b0e780dfd9272b177c32cc3bb99f37bb88304d Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 10 May 2024 07:06:43 +0200 Subject: MHR-23: Getting SMP to work... Signed-off-by: Amlal El Mahrouss --- Boot/NetBoot/Module.cxx | 5 +- Boot/NetBoot/NetBoot.hxx | 12 +++ Boot/Source/BootloaderRsrc.rsrc | 2 +- Boot/Source/makefile | 106 -------------------- Boot/makefile | 110 +++++++++++++++++++++ Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx | 19 ++-- .../HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp | 58 +++++------ Kernel/HALKit/AXP/README.TXT | 2 +- Kernel/KernelKit/DebugOutput.hpp | 2 +- Kernel/KernelKit/FileManager.hpp | 2 - Kernel/KernelKit/ProcessScheduler.hpp | 4 +- Kernel/KernelRsrc.rsrc | 2 +- Kernel/Linker/16x0.json | 2 +- Kernel/Linker/32x0.json | 2 +- Kernel/Linker/64x0.json | 2 +- Kernel/NewKit/Application.hxx | 31 ------ Kernel/NewKit/ApplicationInterface.hxx | 31 ++++++ Kernel/Source/AppMain.cxx | 39 ++++++-- Kernel/Source/FileManager.cxx | 22 +---- Kernel/Source/KernelCheck.cxx | 2 +- Kernel/Source/ProcessScheduler.cxx | 2 +- Kernel/Source/Variant.cxx | 5 - Kernel/makefile | 5 +- 23 files changed, 240 insertions(+), 227 deletions(-) create mode 100644 Boot/NetBoot/NetBoot.hxx delete mode 100644 Boot/Source/makefile create mode 100644 Boot/makefile delete mode 100644 Kernel/NewKit/Application.hxx create mode 100644 Kernel/NewKit/ApplicationInterface.hxx (limited to 'Boot/Source') diff --git a/Boot/NetBoot/Module.cxx b/Boot/NetBoot/Module.cxx index c296903b..57841904 100644 --- a/Boot/NetBoot/Module.cxx +++ b/Boot/NetBoot/Module.cxx @@ -8,10 +8,11 @@ */ #include +#include -EXTERN_C Int32 EfiMain(Void) +EXTERN_C Int32 ModuleMain(Void) { - /// - Find a network drive called "/OnlineBoot" + /// - Find a network drive called "/Remote/NewOSKrnl" /// - Download our image /// - Boot from it. diff --git a/Boot/NetBoot/NetBoot.hxx b/Boot/NetBoot/NetBoot.hxx new file mode 100644 index 00000000..d45f1de1 --- /dev/null +++ b/Boot/NetBoot/NetBoot.hxx @@ -0,0 +1,12 @@ +/* + * ======================================================== + * + * NetBoot + * Copyright SoftwareLabs, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include diff --git a/Boot/Source/BootloaderRsrc.rsrc b/Boot/Source/BootloaderRsrc.rsrc index d79211e7..0282192b 100644 --- a/Boot/Source/BootloaderRsrc.rsrc +++ b/Boot/Source/BootloaderRsrc.rsrc @@ -15,7 +15,7 @@ BEGIN VALUE "FileVersion", BOOTLOADER_VERSION VALUE "InternalName", "NewBoot" VALUE "LegalCopyright", "Copyright SoftwareLabs, all rights reserved." - VALUE "OriginalFilename", "NewBoot.exe" + VALUE "OriginalFilename", "NewOSLdr.exe" VALUE "ProductName", "NewBoot" VALUE "ProductVersion", BOOTLOADER_VERSION END diff --git a/Boot/Source/makefile b/Boot/Source/makefile deleted file mode 100644 index 70fa7232..00000000 --- a/Boot/Source/makefile +++ /dev/null @@ -1,106 +0,0 @@ -################################################## -# (C) SoftwareLabs, all rights reserved. -# This is the bootloader makefile. -################################################## - -CC_GNU=x86_64-w64-mingw32-g++ -LD_GNU=x86_64-w64-mingw32-ld - -WINDRES=x86_64-w64-mingw32-windres - -ADD_FILE=touch -COPY=cp -HTTP_GET=wget - -ifeq ($(shell uname), Windows_NT) -EMU=qemu-system-x86_64w -else -EMU=qemu-system-x86_64 -endif - -ifeq ($(NEWS_MODEL), ) -NEWOS_MODEL=-DkMachineModel="\"Generic NeWS HD\"" -endif - -IMG=epm.img -IMG_2=epm-slave.img - -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 \ - file=fat:rw:Root,index=2,format=raw -d int -hdd epm-slave.img - -LD_FLAGS=-e Main --subsystem=10 - -ifeq ($(NEWS_STANDLONE), ) -OBJ=*.o ../../Kernel/Objects/*.obj -else -RESCMD=$(WINDRES) BootloaderRsrc.rsrc -O coff -o BootloaderRsrc.o -STANDALONE_MACRO=-D__STANDALONE__ -OBJ=*.o -endif - -REM=rm -REM_FLAG=-f - -FLAG_ASM=-f win64 -FLAG_GNU=-fshort-wchar -D__EFI_x86_64__ -mno-red-zone -D__KERNEL__ -D__NEWBOOT__ \ - -DEFI_FUNCTION_WRAPPER -I../ -I../../Kernel -I./ -c -nostdlib -fno-rtti -fno-exceptions \ - -std=c++20 -D__HAVE_MAHROUSS_APIS__ -D__MAHROUSS__ -D__BOOTLOADER__ -I./ - -BOOT_LOADER=NewBoot.exe -KERNEL=NewKernel.exe - -.PHONY: invalid-recipe -invalid-recipe: - @echo "invalid-recipe: Use make compile- instead." - -.PHONY: all -all: compile-amd64 - mkdir -p Root/EFI/BOOT - $(LD_GNU) $(OBJ) $(LD_FLAGS) -o $(BOOT_LOADER) - $(COPY) $(BOOT_LOADER) Root/EFI/BOOT/BOOTX64.EFI - $(COPY) $(BOOT_LOADER) Root/EFI/BOOT/NEWBOOT.EFI - $(COPY) ../../Kernel/$(KERNEL) Root/$(KERNEL) - -ifneq ($(DEBUG_SUPPORT), ) -DEBUG = -D__DEBUG__ -endif - -.PHONY: compile-amd64 -compile-amd64: - $(RESCMD) - $(CC_GNU) $(NEWOS_MODEL) $(STANDALONE_MACRO) $(FLAG_GNU) $(DEBUG) $(wildcard HEL/AMD64/*.cxx) $(wildcard HEL/AMD64/*.S) $(wildcard *.cxx) - -.PHONY: run-efi-amd64 -run-efi-amd64: - $(EMU) $(EMU_FLAGS) - -# img_2 is the rescue disk. img is the bootable disk, as provided by the NeWS. -.PHONY: epm-img -epm-img: - qemu-img create -f raw $(IMG) 10G - qemu-img create -f raw $(IMG_2) 512M - -.PHONY: download-edk -download-edk: - $(HTTP_GET) https://retrage.github.io/edk2-nightly/bin/DEBUGX64_OVMF.fd -O OVMF.fd - -BINS=*.bin -EXECUTABLES=NewBoot.exe NewKernel.exe OVMF.fd - -TARGETS=$(REM_FLAG) $(OBJ) $(BIN) $(IMG) $(IMG_2) $(EXECUTABLES) - -.PHONY: clean -clean: - $(REM) $(TARGETS) - -.PHONY: help -help: - @echo "=== HELP ===" - @echo "epm-img: Format a disk using the Explicit Partition Map." - @echo "gpt-img: Format a disk using the Explicit Partition Map." - @echo "clean: clean bootloader." - @echo "bootloader-amd64: Build bootloader. (PC AMD64)" - @echo "run-efi-amd64: Run bootloader. (PC AMD64)" diff --git a/Boot/makefile b/Boot/makefile new file mode 100644 index 00000000..878cee2a --- /dev/null +++ b/Boot/makefile @@ -0,0 +1,110 @@ +################################################## +# (C) SoftwareLabs, all rights reserved. +# This is the bootloader makefile. +################################################## + +CC_GNU=x86_64-w64-mingw32-g++ +LD_GNU=x86_64-w64-mingw32-ld + +WINDRES=x86_64-w64-mingw32-windres + +ADD_FILE=touch +COPY=cp +HTTP_GET=wget + +ifeq ($(shell uname), Windows_NT) +EMU=qemu-system-x86_64w +else +EMU=qemu-system-x86_64 +endif + +ifeq ($(NEWS_MODEL), ) +NEWOS_MODEL=-DkMachineModel="\"Generic NeWS HD\"" +endif + +BIOS=OVMF.fd +IMG=epm.img +IMG_2=epm-slave.img + +EMU_FLAGS=-net none -smp 4,sockets=1,cores=4,threads=1 -m 8G -M q35 \ + -bios Source/$(BIOS) -device piix3-ide,id=ide \ + -drive id=disk,file=Source/$(IMG),format=raw,if=none \ + -device ide-hd,drive=disk,bus=ide.0 -drive \ + file=fat:rw:Source/Root,index=2,format=raw -d int -hdd Source/$(IMG_2) + +LD_FLAGS=-e Main --subsystem=10 + +ifeq ($(NEWS_STANDLONE), ) +OBJ=*.o ../Kernel/Objects/*.obj +else +RESCMD=$(WINDRES) BootloaderRsrc.rsrc -O coff -o BootloaderRsrc.o +STANDALONE_MACRO=-D__STANDALONE__ +OBJ=*.o +endif + +REM=rm +REM_FLAG=-f + +FLAG_ASM=-f win64 +FLAG_GNU=-fshort-wchar -D__EFI_x86_64__ -mno-red-zone -D__KERNEL__ -D__NEWBOOT__ \ + -DEFI_FUNCTION_WRAPPER -I./ -I../Kernel -I./ -c -nostdlib -fno-rtti -fno-exceptions \ + -std=c++20 -D__HAVE_MAHROUSS_APIS__ -D__MAHROUSS__ -D__BOOTLOADER__ -I./ + +BOOT_LOADER=NewOSLdr.exe +KERNEL=NewOSKrnl.exe + +.PHONY: invalid-recipe +invalid-recipe: + @echo "invalid-recipe: Use make compile- instead." + +.PHONY: all +all: compile-amd64 + mkdir -p Root/EFI/BOOT + $(LD_GNU) $(OBJ) $(LD_FLAGS) -o Source/$(BOOT_LOADER) + $(COPY) Source/$(BOOT_LOADER) Source/Root/EFI/BOOT/BOOTX64.EFI + $(COPY) Source/$(BOOT_LOADER) Source/Root/EFI/BOOT/NEWBOOT.EFI + $(COPY) ../Kernel/$(KERNEL) Source/Root/$(KERNEL) + +ifneq ($(DEBUG_SUPPORT), ) +DEBUG = -D__DEBUG__ +endif + +.PHONY: compile-amd64 +compile-amd64: + $(RESCMD) + $(CC_GNU) $(NEWOS_MODEL) $(STANDALONE_MACRO) $(FLAG_GNU) $(DEBUG) \ + $(wildcard Source/HEL/AMD64/*.cxx) \ + $(wildcard Source/HEL/AMD64/*.S) + $(wildcard Source/*.cxx) + +.PHONY: run-efi-amd64 +run-efi-amd64: + $(EMU) $(EMU_FLAGS) + +# img_2 is the rescue disk. img is the bootable disk, as provided by the NeWS. +.PHONY: epm-img +epm-img: + qemu-img create -f raw $(IMG) 10G + qemu-img create -f raw $(IMG_2) 512M + +.PHONY: download-edk +download-edk: + $(HTTP_GET) https://retrage.github.io/edk2-nightly/bin/DEBUGX64_OVMF.fd -O OVMF.fd + +BINS=*.bin +EXECUTABLES=NewOSLdr.exe NewOSKrnl.exe OVMF.fd + +TARGETS=$(REM_FLAG) $(OBJ) $(BIN) $(IMG) $(IMG_2) $(EXECUTABLES) + +.PHONY: clean +clean: + $(REM) $(TARGETS) + +.PHONY: help +help: + @echo "=== HELP ===" + @echo "epm-img: Format a disk using the Explicit Partition Map." + @echo "gpt-img: Format a disk using the Explicit Partition Map." + @echo "clean: clean bootloader." + @echo "bootloader-amd64: Build bootloader. (PC AMD64)" + @echo "run-efi-amd64: Run bootloader. (PC AMD64)" diff --git a/Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx b/Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx index f4c9226e..db82616e 100644 --- a/Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx +++ b/Kernel/HALKit/AMD64/HalACPIFactoryInterface.cxx @@ -75,7 +75,7 @@ namespace NewOS SDT* xsdt = (SDT*)(rsdPtr->XsdtAddress >> (rsdPtr->XsdtAddress & 0xFFF)); - SizeT num = (xsdt->Length + sizeof(SDT)) / 8; + SizeT num = -(xsdt->Length - sizeof(SDT)) / 8; this->fEntries = num; @@ -83,22 +83,19 @@ namespace NewOS kcout << "ACPI: Address of XSDT: " << hex_number((UIntPtr)xsdt) << endl; constexpr short ACPI_SIGNATURE_LENGTH = 4; - SizeT offsetToAdd = 0UL; - - for (Size index = 0; index < num; ++index) + + for (Size index = 0; index < this->fEntries; ++index) { - SDT* sdt = &(xsdt[index]) + offsetToAdd; + SDT &sdt = xsdt[index]; - for (int signature_index = 0; signature_index < 4; signature_index++) + for (short signature_index = 0; signature_index < ACPI_SIGNATURE_LENGTH; ++signature_index) { - if (sdt->Signature[signature_index] != signature[signature_index]) + if (sdt.Signature[signature_index] != signature[signature_index]) break; - if (signature_index == 3) - return ErrorOr(reinterpret_cast(sdt)); + if (signature_index == 4) + return ErrorOr(reinterpret_cast(&sdt)); } - - offsetToAdd = sdt->Length; } return ErrorOr{nullptr}; diff --git a/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp b/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp index ec6d47da..470a3286 100644 --- a/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp +++ b/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cpp @@ -46,54 +46,54 @@ namespace NewOS::HAL STATIC voidPtr kApicMadt = nullptr; STATIC const char* kApicSignature = "APIC"; - /// @brief Multiple APIC descriptor table. + /// @brief Multiple APIC Descriptor Table. struct MadtType final : public SDT { struct MadtAddress final { - UInt32 fAddress; - UInt32 fFlags; // 1 = Dual Legacy PICs installed - - Char fType; - Char fRecLen; // record length - } fMadt[]; + Char RecordType; + Char RecordLen; // record length + + UInt32 Address; + UInt32 Flags; // 1 = Dual Legacy PICs installed + } MadtRecords[]; }; struct MadtProcessorLocalApic final { - Char fProcessorId; - Char fApicId; - UInt32 fFlags; + Char AcpiProcessorId; + Char Reserved; + UInt32 Flags; }; struct MadtIOApic final { - Char fApicId; - Char fReserved; - UInt32 fAddress; - UInt32 fSystemInterruptBase; + Char ApicId; + Char Reserved; + UInt32 Address; + UInt32 SystemInterruptBase; }; struct MadtInterruptSource final { - Char fBusSource; - Char fIrqSource; - UInt32 fGSI; - UInt16 fFlags; + Char BusSource; + Char IrqSource; + UInt32 GSI; + UInt16 Flags; }; struct MadtInterruptNmi final { - Char fNmiSource; - Char fReserved; - UInt16 fFlags; - UInt32 fGSI; + Char NmiSource; + Char Reserved; + UInt16 Flags; + UInt32 GSI; }; struct MadtLocalApicAddressOverride final { - UInt16 fResvered; - UIntPtr fAddress; + UInt16 Resvered; + UIntPtr Address; }; /////////////////////////////////////////////////////////////////////////////////////// @@ -109,17 +109,13 @@ namespace NewOS::HAL if (kApicMadt) { - kcout << "New OS: Successfuly fetched the cores!\r"; + kcout << "New OS: APIC is present...\r"; kApicInfoBlock = (MadtType*)kApicMadt; - - kcout << "New OS: Revision: "; - kcout.HexNumber(kApicInfoBlock->Revision).EndLine(); - - ke_stop(RUNTIME_CHECK_BOOTSTRAP); } else { - ke_stop(RUNTIME_CHECK_BOOTSTRAP); + kcout << "New OS: APIC is not present! it is a vital component.\r"; + ke_stop(RUNTIME_CHECK_FAILED); } } } // namespace NewOS::HAL diff --git a/Kernel/HALKit/AXP/README.TXT b/Kernel/HALKit/AXP/README.TXT index 03c2b816..d4ef257d 100644 --- a/Kernel/HALKit/AXP/README.TXT +++ b/Kernel/HALKit/AXP/README.TXT @@ -1 +1 @@ -A Toy HAL to test the Kernel portability. +An toy HAL to test the kernel portability. diff --git a/Kernel/KernelKit/DebugOutput.hpp b/Kernel/KernelKit/DebugOutput.hpp index 656fe7a9..c6eb4485 100644 --- a/Kernel/KernelKit/DebugOutput.hpp +++ b/Kernel/KernelKit/DebugOutput.hpp @@ -56,7 +56,7 @@ namespace NewOS TerminalDevice& HexNumber(const Long Data) noexcept { - number(Data); + hex_number(Data); return *this; } diff --git a/Kernel/KernelKit/FileManager.hpp b/Kernel/KernelKit/FileManager.hpp index d0843a5e..a5ac6a93 100644 --- a/Kernel/KernelKit/FileManager.hpp +++ b/Kernel/KernelKit/FileManager.hpp @@ -136,8 +136,6 @@ namespace NewOS NewFSParser* GetImpl() noexcept; private: - Char fDataFork[kNewFSForkNameLen] = {0}; - Char fRsrcFork[kNewFSForkNameLen] = {0}; NewFSParser* fImpl{nullptr}; }; diff --git a/Kernel/KernelKit/ProcessScheduler.hpp b/Kernel/KernelKit/ProcessScheduler.hpp index ba34f9cb..243857ae 100644 --- a/Kernel/KernelKit/ProcessScheduler.hpp +++ b/Kernel/KernelKit/ProcessScheduler.hpp @@ -163,7 +163,7 @@ namespace NewOS enum { - kUserKind = 3, + kAppKind = 3, kLibKind = 3, kDriverKind = 0, kKindCount, @@ -178,7 +178,7 @@ namespace NewOS ProcessTime PTime; PID ProcessId{kSchedInvalidPID}; Int32 Ring{kRingDriverKind}; - Int32 Kind{kUserKind}; + Int32 Kind{kAppKind}; public: //! @brief boolean operator, check status. diff --git a/Kernel/KernelRsrc.rsrc b/Kernel/KernelRsrc.rsrc index d8fc6473..6689d10c 100644 --- a/Kernel/KernelRsrc.rsrc +++ b/Kernel/KernelRsrc.rsrc @@ -15,7 +15,7 @@ BEGIN VALUE "FileVersion", KERNEL_VERSION VALUE "InternalName", "NewKernel" VALUE "LegalCopyright", "SoftwareLabs" - VALUE "OriginalFilename", "NewKernel.exe" + VALUE "OriginalFilename", "NewOSKrnl.exe" VALUE "ProductName", "NewKernel" VALUE "ProductVersion", KERNEL_VERSION END diff --git a/Kernel/Linker/16x0.json b/Kernel/Linker/16x0.json index 5851a2d3..40cee7c9 100644 --- a/Kernel/Linker/16x0.json +++ b/Kernel/Linker/16x0.json @@ -1,6 +1,6 @@ { "executable_type": "kernel", - "output_name": "NewKernel.exe", + "output_name": "NewOSKrnl.exe", "start_proc": "__ImageStart", "format": "PEF" } diff --git a/Kernel/Linker/32x0.json b/Kernel/Linker/32x0.json index 5851a2d3..40cee7c9 100644 --- a/Kernel/Linker/32x0.json +++ b/Kernel/Linker/32x0.json @@ -1,6 +1,6 @@ { "executable_type": "kernel", - "output_name": "NewKernel.exe", + "output_name": "NewOSKrnl.exe", "start_proc": "__ImageStart", "format": "PEF" } diff --git a/Kernel/Linker/64x0.json b/Kernel/Linker/64x0.json index 5851a2d3..40cee7c9 100644 --- a/Kernel/Linker/64x0.json +++ b/Kernel/Linker/64x0.json @@ -1,6 +1,6 @@ { "executable_type": "kernel", - "output_name": "NewKernel.exe", + "output_name": "NewOSKrnl.exe", "start_proc": "__ImageStart", "format": "PEF" } diff --git a/Kernel/NewKit/Application.hxx b/Kernel/NewKit/Application.hxx deleted file mode 100644 index 19a892d6..00000000 --- a/Kernel/NewKit/Application.hxx +++ /dev/null @@ -1,31 +0,0 @@ -/* ------------------------------------------- - - Copyright SoftwareLabs - -------------------------------------------- */ - -#pragma once - -/// -/// @brief Application object, given by the OS to the process. interact with the OS. -/// @file Application.hxx -/// @author Amlal EL Mahrouss -/// - -#include -#include - -/// \brief Application Interface. -/// \author Amlal El Mahrouss -typedef struct _ApplicationInterface final -{ - /// @brief Releases the object exit the process on main object. - NewOS::Void (*Release)(struct _Application* Self, NewOS::Int32 ExitCode); - /// @brief Invoke a function from the application object. - NewOS::IntPtr (*Invoke)(struct _Application* Self, NewOS::Int32 Sel, ...); - /// @brief Query a new application object from a GUID. - /// @note this doesn't query a process, it query a registered object withtin that app. - NewOS::Void (*Query)(struct _Application* Self, NewOS::VoidPtr* Dst, NewOS::SizeT SzDst, NewOS::XRN::GUIDSequence GuidOf); -} ApplicationInterface, *ApplicationInterfaceRef; - -#define app_cast reinterpret_cast diff --git a/Kernel/NewKit/ApplicationInterface.hxx b/Kernel/NewKit/ApplicationInterface.hxx new file mode 100644 index 00000000..09d2c901 --- /dev/null +++ b/Kernel/NewKit/ApplicationInterface.hxx @@ -0,0 +1,31 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + +------------------------------------------- */ + +#pragma once + +/// +/// @brief Application object, given by the OS to the process. interact with the OS. +/// @file ApplicationInterface.hxx +/// @author Amlal EL Mahrouss +/// + +#include +#include + +/// \brief Application Interface. +/// \author Amlal El Mahrouss +typedef struct _ApplicationInterface final +{ + /// @brief Releases the object exit the process on main object. + NewOS::Void (*Release)(struct _Application* Self, NewOS::Int32 ExitCode); + /// @brief Invoke a function from the application object. + NewOS::IntPtr (*Invoke)(struct _Application* Self, NewOS::Int32 Sel, ...); + /// @brief Query a new application object from a GUID. + /// @note this doesn't query a process, it query a registered object withtin that app. + NewOS::Void (*Query)(struct _Application* Self, NewOS::VoidPtr* Dst, NewOS::SizeT SzDst, NewOS::XRN::GUIDSequence GuidOf); +} ApplicationInterface, *ApplicationInterfaceRef; + +#define app_cast reinterpret_cast diff --git a/Kernel/Source/AppMain.cxx b/Kernel/Source/AppMain.cxx index 4f5de11f..028e70e5 100644 --- a/Kernel/Source/AppMain.cxx +++ b/Kernel/Source/AppMain.cxx @@ -179,25 +179,50 @@ namespace Detail } }; - STATIC NewOS::Void AppWatchdogThread(NewOS::Void) + /// @brief System loader entrypoint. + /// @param void no parameters. + /// @return void no return value. + STATIC NewOS::Void AppSystemLoader(NewOS::Void) { - NewOS::kcout << "SystemSanityThread: Exiting process..."; + NewOS::PEFLoader coreGraphicsShLib("/System/CoreGraphics"); + + if (!coreGraphicsShLib.IsLoaded()) + { + NewOS::ke_stop(RUNTIME_CHECK_FAILED); + } + + NewOS::Utils::execute_from_image(coreGraphicsShLib, + NewOS::ProcessHeader::kLibKind); + + NewOS::PEFLoader logonService("/System/Login"); + + if (!logonService.IsLoaded()) + { + NewOS::ke_stop(RUNTIME_CHECK_FAILED); + } + + NewOS::Utils::execute_from_image(logonService, + NewOS::ProcessHeader::kAppKind); + + NewOS::kcout << "SystemLoader: Exiting process, we're done initializing stuff..."; + NewOS::ProcessScheduler::Shared().Leak().GetCurrent().Leak().Exit(0); } } // namespace Detail -/// @file Main microkernel entrypoint. - +/// @brief Application entrypoint. +/// @param Void +/// @return Void EXTERN_C NewOS::Void AppMain(NewOS::Void) { /// Now run kernel loop, until no process are running. Detail::FilesystemWizard wizard; // automatic. - auto cWatchdogThreadName = "SystemSanityThread"; - NewOS::execute_from_image((NewOS::MainKind)Detail::AppWatchdogThread, cWatchdogThreadName); + auto cLoaderName = "SystemLoader"; + NewOS::execute_from_image(Detail::AppSystemLoader, cLoaderName); while (NewOS::ProcessScheduler::Shared().Leak().Run() > 0) { - ; + NewOS::kcout << "New OS: sleeping...\r"; } } diff --git a/Kernel/Source/FileManager.cxx b/Kernel/Source/FileManager.cxx index 72ea15a1..ed90dab8 100644 --- a/Kernel/Source/FileManager.cxx +++ b/Kernel/Source/FileManager.cxx @@ -90,11 +90,11 @@ namespace NewOS NEWOS_UNUSED(flags); - const char* cReadAllFork = fDataFork; - + auto dataForkName = "FileData"; + if ((reinterpret_cast(node))->Kind == kNewFSCatalogKindFile) fImpl->WriteCatalog(reinterpret_cast(node), data, size, - cReadAllFork); + dataForkName); } /// @brief Read from filesystem fork. @@ -112,11 +112,11 @@ namespace NewOS NEWOS_UNUSED(flags); - const char* cReadAllFork = fDataFork; + auto dataForkName = "FileData"; if ((reinterpret_cast(node))->Kind == kNewFSCatalogKindFile) return fImpl->ReadCatalog(reinterpret_cast(node), sz, - cReadAllFork); + dataForkName); return nullptr; } @@ -167,17 +167,5 @@ namespace NewOS { return fImpl; } - - void NewFilesystemManager::SetResourceFork(const char* forkName) - { - if (!forkName) return; - rt_copy_memory((VoidPtr)forkName, (VoidPtr)fRsrcFork, rt_string_len(forkName)); - } - - void NewFilesystemManager::SetDataFork(const char* forkName) - { - if (!forkName) return; - rt_copy_memory((VoidPtr)forkName, (VoidPtr)fDataFork, rt_string_len(forkName)); - } #endif // __FSKIT_NEWFS__ } // namespace NewOS diff --git a/Kernel/Source/KernelCheck.cxx b/Kernel/Source/KernelCheck.cxx index e7c43650..5df52248 100644 --- a/Kernel/Source/KernelCheck.cxx +++ b/Kernel/Source/KernelCheck.cxx @@ -26,7 +26,7 @@ namespace NewOS void ke_stop(const NewOS::Int& id) { kcout << "*** STOP *** \r"; - kcout << "*** NewKernel.exe has trigerred a runtime stop. *** \r"; + kcout << "*** Kernel has trigerred a runtime stop. *** \r"; switch (id) { diff --git a/Kernel/Source/ProcessScheduler.cxx b/Kernel/Source/ProcessScheduler.cxx index 859ce657..555dfe07 100644 --- a/Kernel/Source/ProcessScheduler.cxx +++ b/Kernel/Source/ProcessScheduler.cxx @@ -218,7 +218,7 @@ namespace NewOS kcout << "ProcessScheduler::Add(Ref& process)\r"; /// Create heap according to type of process. - if (process.Leak().Kind == ProcessHeader::kUserKind) + if (process.Leak().Kind == ProcessHeader::kAppKind) process.Leak().HeapPtr = rt_new_heap(kUserHeapUser | kUserHeapRw); else if (process.Leak().Kind == ProcessHeader::kLibKind) process.Leak().HeapPtr = rt_new_heap(kUserHeapUser | kUserHeapRw | kUserHeapShared); diff --git a/Kernel/Source/Variant.cxx b/Kernel/Source/Variant.cxx index afe66624..1a09718b 100644 --- a/Kernel/Source/Variant.cxx +++ b/Kernel/Source/Variant.cxx @@ -10,11 +10,6 @@ namespace NewOS { const Char* Variant::ToString() { - if (fPtr == nullptr) - { - return ("Memory:{Nullptr}"); - } - switch (fKind) { case VariantKind::kString: diff --git a/Kernel/makefile b/Kernel/makefile index 61eb9dfe..74365a60 100644 --- a/Kernel/makefile +++ b/Kernel/makefile @@ -37,10 +37,7 @@ LDFLAGS = -e __ImageStart --subsystem=17 LDOBJ = Objects/*.obj # This file is the kernel, responsible of task management and memory. -KERNEL = NewKernel.exe - -# The kernel entrypoint -SCRIPT = --script=Linker/Platforms/PC.lds +KERNEL = NewOSKrnl.exe .PHONY: error error: -- cgit v1.2.3