diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-05-02 19:38:46 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-02 19:38:46 +0200 |
| commit | 997be16e5ac9a68d54882ab69529815860d62955 (patch) | |
| tree | 19d6129c2d776bb1edc5d4a7325e39ca176c3403 /dev/boot/modules/BootNet | |
| parent | 618104e74c195d7508a18450524f8ed7f9af8cc6 (diff) | |
| parent | b3b4b1ebdcd6adeac914869017c86d892b7a8ced (diff) | |
Merge pull request #28 from nekernel-org/dev
0.0.2
Diffstat (limited to 'dev/boot/modules/BootNet')
| -rw-r--r-- | dev/boot/modules/BootNet/BootNet.cc | 134 | ||||
| -rw-r--r-- | dev/boot/modules/BootNet/BootNet.h | 23 |
2 files changed, 84 insertions, 73 deletions
diff --git a/dev/boot/modules/BootNet/BootNet.cc b/dev/boot/modules/BootNet/BootNet.cc index 92f7aa7a..840aabd5 100644 --- a/dev/boot/modules/BootNet/BootNet.cc +++ b/dev/boot/modules/BootNet/BootNet.cc @@ -7,78 +7,110 @@ * ======================================================== */ -#include <FirmwareKit/EFI/API.h> -#include <modules/BootNet/BootNet.h> #include <BootKit/BootKit.h> #include <BootKit/BootThread.h> +#include <FirmwareKit/EFI/API.h> +#include <modules/BootNet/BootNet.h> + +STATIC EfiGUID kEfiSimpleProtoGUID = EFI_SIMPLE_NETWORK_PROTOCOL_GUID; +STATIC EFI_SIMPLE_NETWORK_PROTOCOL* kEfiProtocol = nullptr; -STATIC EfiGUID 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); -STATIC Void bootnet_read_ip_packet(BOOTNET_INTERNET_HEADER); +EXTERN_C Int32 BootNetModuleMain(Kernel::HEL::BootInfoHeader* handover) { + fw_init_efi((EfiSystemTable*) handover->f_FirmwareCustomTables[1]); -EXTERN_C Int32 BootNetModuleMain(Kernel::HEL::BootInfoHeader* handover) -{ - Boot::BootTextWriter writer; + Boot::BootTextWriter writer; - writer.Write("BootNet: Init EFI...\r"); + writer.Write("BootNet: Init BootNet...\r"); - fw_init_efi((EfiSystemTable*)handover->f_FirmwareCustomTables[1]); + if (BS->LocateProtocol(&kEfiSimpleProtoGUID, nullptr, (VoidPtr*) &kEfiProtocol) != kEfiOk) { + writer.Write("BootNet: Not supported by firmware.\r"); + return kEfiFail; + } - 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; - BOOTNET_INTERNET_HEADER inet{}; + SetMem(&inet, 0, sizeof(BOOTNET_INTERNET_HEADER)); - SetMem(&inet, 0, sizeof(BOOTNET_INTERNET_HEADER)); + writer.Write("BootNet: Downloading kernel...\r"); - writer.Write("BootNet: Downloading kernel...\r"); + bootnet_read_ip_packet(inet, &inet_out); - bootnet_read_ip_packet(inet); + if (inet_out->Length < 1) { + writer.Write("BootNet: No executable attached to the packet, aborting.\r"); + return kEfiFail; + } - if (inet.Length < 1) - { - writer.Write("BootNet: No executable attached to the packet, aborting.\r"); - return kEfiFail; - } + if (!inet_out->ImpliesProgram) { + Boot::BootThread thread(inet_out->Data); - if (!inet.ImpliesProgram) - { - Boot::BootThread thread(inet.Data); + if (thread.IsValid()) { + writer.Write("BootNet: Running kernel...\r"); + return thread.Start(handover, YES); + } - if (thread.IsValid()) - { - writer.Write("BootNet: Running kernel...\r"); - return thread.Start(handover, YES); - } + return kEfiFail; + } else { + constexpr auto kROMSize = 0x200; - return kEfiFail; - } - else - { - constexpr auto kROMSize = 0x200; + if (inet_out->Length > kROMSize) { + writer.Write("BootNet: Not within 512K.\r"); + return kEfiFail; + } - if (inet.Length != kROMSize) - { - writer.Write("BootNet: Not within 512K.\r"); - return kEfiFail; - } + writer.Write("BootNet: Programming the flash...\r"); - writer.Write("BootNet: Programming the flash is not available as of right now.\r"); + /// TODO: Program new firmware to EEPROM (if crc and size matches) - /// 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; - const UIntPtr kEEPROMAddress = 0; - const UInt16 kEEPROMSize = inet.Length; - } + return kEfiFail; + } - return kEfiFail; + return kEfiFail; } -STATIC Void bootnet_read_ip_packet(BOOTNET_INTERNET_HEADER inet) -{ - NE_UNUSED(inet); +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/dev/boot/modules/BootNet/BootNet.h b/dev/boot/modules/BootNet/BootNet.h index b38ed0e9..0b319387 100644 --- a/dev/boot/modules/BootNet/BootNet.h +++ b/dev/boot/modules/BootNet/BootNet.h @@ -9,25 +9,4 @@ #pragma once -#include <NewKit/Defines.h> - -#define kBootNetINetMagic "NETB" -#define kBootNetINetMagicLength (4) - -#define kBootNetNameLen (256U) - -/// @brief the internet header is used to download updates OTA. -typedef struct BOOTNET_INTERNET_HEADER -{ - Kernel::Char NB1; /// magic char 1 'N' - Kernel::Char NB2; /// magic char 2 'E' - Kernel::Char NB3; /// magic char 3 'T' - Kernel::Char NB4; /// magic char 4 'B' - - Kernel::Char Name[kBootNetNameLen]; /// example: Modjo - Kernel::Int32 Length; /// the patch length. - Kernel::Char Target[kBootNetNameLen]; /// the target file. - Kernel::Boolean ImpliesProgram : 1; /// does it imply an EEPROM reprogram? - Kernel::Boolean Preflight : 1; /// is it a preflight packet. - Kernel::Char Data[1]; /// non preflight packet has a patch blob for a **PatchTarget** -} ATTRIBUTE(packed) BOOTNET_INTERNET_HEADER; +#include <FirmwareKit/CoreBoot/BootNet.h> |
