From 83d870e58457a1d335a1d9b9966a6a1887cc297b Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 24 Nov 2025 03:02:43 +0100 Subject: feat! breaking changes on kernel sources. Signed-off-by: Amlal El Mahrouss --- src/boot/modules/BootNet/.hgkeep | 0 src/boot/modules/BootNet/BootNet.cc | 121 ++++++++++++++++++++++++++++++ src/boot/modules/BootNet/BootNet.h | 12 +++ src/boot/modules/BootNet/BootNetStartup.S | 24 ++++++ src/boot/modules/BootNet/amd64.json | 23 ++++++ 5 files changed, 180 insertions(+) create mode 100644 src/boot/modules/BootNet/.hgkeep create mode 100644 src/boot/modules/BootNet/BootNet.cc create mode 100644 src/boot/modules/BootNet/BootNet.h create mode 100644 src/boot/modules/BootNet/BootNetStartup.S create mode 100644 src/boot/modules/BootNet/amd64.json (limited to 'src/boot/modules/BootNet') diff --git a/src/boot/modules/BootNet/.hgkeep b/src/boot/modules/BootNet/.hgkeep new file mode 100644 index 00000000..e69de29b diff --git a/src/boot/modules/BootNet/BootNet.cc b/src/boot/modules/BootNet/BootNet.cc new file mode 100644 index 00000000..20d1a6c9 --- /dev/null +++ b/src/boot/modules/BootNet/BootNet.cc @@ -0,0 +1,121 @@ +/* + * ======================================================== + * + * BootNet + * Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. + * + * ======================================================== + */ + +#include +#include +#include +#include + +STATIC EFI_GUID kEfiSimpleProtoGUID = EFI_SIMPLE_NETWORK_PROTOCOL_GUID; +STATIC EFI_SIMPLE_NETWORK_PROTOCOL* kEfiProtocol = nullptr; + +STATIC Void bootnet_read_ip_packet(BOOTNET_INTERNET_HEADER inet, + BOOTNET_INTERNET_HEADER** inet_out); + +EXTERN_C Int32 BootNetModuleMain(Kernel::HEL::BootInfoHeader* handover) { + fw_init_efi((EfiSystemTable*) handover->f_FirmwareCustomTables[Kernel::HEL::kHandoverTableST]); + + Boot::BootTextWriter writer; + + writer.Write("BootNet: Init BootNet...\r"); + + if (BS->LocateProtocol(&kEfiSimpleProtoGUID, nullptr, (VoidPtr*) &kEfiProtocol) != kEfiOk) { + writer.Write("BootNet: Not supported by firmware.\r"); + return kEfiFail; + } + + BOOTNET_INTERNET_HEADER inet{}; + BOOTNET_INTERNET_HEADER* inet_out = nullptr; + + SetMem(&inet, 0, sizeof(BOOTNET_INTERNET_HEADER)); + + writer.Write("BootNet: Downloading kernel...\r"); + + bootnet_read_ip_packet(inet, &inet_out); + + if (inet_out->Length < 1) { + writer.Write("BootNet: No executable attached to the packet, aborting.\r"); + return kEfiFail; + } + + if (inet_out->Version != kBootNetVersion) { + writer.Write("BootNet: The version clashes, not good.\r"); + return kEfiFail; + } + + if (!inet_out->ImpliesProgram) { + Boot::BootThread thread(inet_out->Data); + + if (thread.IsValid()) { + writer.Write("BootNet: Running NeKernel...\r"); + return thread.Start(handover, YES); + } + + return kEfiFail; + } else { + constexpr auto kROMSize = 0x200; + + if (inet_out->Length > kROMSize) { + writer.Write("BootNet: Not within 512K, won't flash EEPROM.\r"); + return kEfiFail; + } + + writer.Write("BootNet: Programming the flash...\r"); + + /// TODO: Program new firmware to EEPROM (if crc and size matches) + + const ATTRIBUTE(unused) UIntPtr kEEPROMStartAddress = 0; + const ATTRIBUTE(unused) UInt16 kEEPROMSize = inet_out->Length; + + return kEfiFail; + } + + return kEfiFail; +} + +STATIC Void bootnet_read_ip_packet(BOOTNET_INTERNET_HEADER inet, + BOOTNET_INTERNET_HEADER** inet_out) { + NE_UNUSED(inet); + + kEfiProtocol->Start(kEfiProtocol); + + UInt32 size_inet = sizeof(inet); + + /// Connect to the local BootNet server. + + /// And receive the handshake packet. + if (kEfiProtocol->Receive(kEfiProtocol, &size_inet, (UInt32*) &inet.Length, (VoidPtr) &inet, + nullptr, nullptr, nullptr) == kEfiOk) { + BOOTNET_INTERNET_HEADER* out = nullptr; + + BS->AllocatePool(EfiLoaderData, sizeof(BOOTNET_INTERNET_HEADER) + inet.Length, (VoidPtr*) &out); + + if (out == nullptr) { + kEfiProtocol->Stop(kEfiProtocol); + kEfiProtocol->Shutdown(kEfiProtocol); + return; + } + + SetMem(out, 0, sizeof(BOOTNET_INTERNET_HEADER)); + SetMem(out->Data, 0, inet.Length); + + size_inet = sizeof(BOOTNET_INTERNET_HEADER); + + auto full_size = size_inet + inet.Length; + + /// Ask for it again since we know the full_size variable now. + kEfiProtocol->Receive(kEfiProtocol, &size_inet, (UInt32*) &full_size, (VoidPtr) out, nullptr, + nullptr, nullptr); + + *inet_out = out; + } + + kEfiProtocol->Stop(kEfiProtocol); + kEfiProtocol->Shutdown(kEfiProtocol); +} \ No newline at end of file diff --git a/src/boot/modules/BootNet/BootNet.h b/src/boot/modules/BootNet/BootNet.h new file mode 100644 index 00000000..9fe1a186 --- /dev/null +++ b/src/boot/modules/BootNet/BootNet.h @@ -0,0 +1,12 @@ +/* + * ======================================================== + * + * BootNet + * Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. + * + * ======================================================== + */ + +#pragma once + +#include diff --git a/src/boot/modules/BootNet/BootNetStartup.S b/src/boot/modules/BootNet/BootNetStartup.S new file mode 100644 index 00000000..a5832ee6 --- /dev/null +++ b/src/boot/modules/BootNet/BootNetStartup.S @@ -0,0 +1,24 @@ +;; /* +;; * ======================================================== +;; * +;; * BootZ +;; * Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. +;; * +;; * ======================================================== +;; */ + +#ifdef __NE_AMD64__ +.code64 +.intel_syntax noprefix +#endif + +#define kTypeDriver 101 +#define kArchAmd64 122 +#define kHandoverMagic 0xBADCC + +.section .ldr + +.quad kHandoverMagic +.word kTypeDriver +.word 0 +.word kArchAmd64 \ No newline at end of file diff --git a/src/boot/modules/BootNet/amd64.json b/src/boot/modules/BootNet/amd64.json new file mode 100644 index 00000000..3d58cbc1 --- /dev/null +++ b/src/boot/modules/BootNet/amd64.json @@ -0,0 +1,23 @@ +{ + "compiler_path": "x86_64-w64-mingw32-g++", + "compiler_std": "c++20", + "headers_path": ["../", "../../", "../../../kernel", "../../../", "./"], + "sources_path": ["*.cc", "*.S", "../../src/HEL/AMD64/*.cc", "../../src/HEL/AMD64/*.S", "../../src/*.cc"], + "output_name": "net.efi", + "compiler_flags": [ + "-nostdlib", + "-std=c++20", + "-fno-rtti", + "-fno-exceptions", + "-Wl,--subsystem=17,--image-base,0x10000000,-e,BootNetModuleMain" + ], + "cpp_macros": [ + "__NEOSKRNL__", + "__BOOTZ__", + "__BOOTZ_STANDALONE__", + "__NE_AMD64__", + "kBootNetVersionHighest=0x0100", + "kBootNetVersionLowest=0x0100", + "kBootNetEFIVersion=0x0100" + ] +} -- cgit v1.2.3