From a9164997dc9db5ae0e5f2632be6379bd96c11d5c Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Sun, 30 Jul 2023 00:47:08 -0400 Subject: [PATCH 1/2] uefi-raw: Add ServiceBindingProtocol Co-authored-by: Ben Krieger --- uefi-raw/CHANGELOG.md | 1 + uefi-raw/src/protocol/driver.rs | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/uefi-raw/CHANGELOG.md b/uefi-raw/CHANGELOG.md index b8ac8be73..aaea25daa 100644 --- a/uefi-raw/CHANGELOG.md +++ b/uefi-raw/CHANGELOG.md @@ -2,6 +2,7 @@ ## Added - Added `Ipv4Address`, `Ipv6Address`, and `MacAddress` types. +- Added `ServiceBindingProtocol`. # uefi-raw - 0.5.0 (2023-11-12) diff --git a/uefi-raw/src/protocol/driver.rs b/uefi-raw/src/protocol/driver.rs index 97617936a..52584d101 100644 --- a/uefi-raw/src/protocol/driver.rs +++ b/uefi-raw/src/protocol/driver.rs @@ -28,3 +28,11 @@ impl ComponentName2Protocol { /// 4646. pub const DEPRECATED_COMPONENT_NAME_GUID: Guid = guid!("107a772c-d5e1-11d4-9a46-0090273fc14d"); } + +#[derive(Debug)] +#[repr(C)] +pub struct ServiceBindingProtocol { + pub create_child: + unsafe extern "efiapi" fn(this: *mut Self, child_handle: *mut Handle) -> Status, + pub destroy_child: unsafe extern "efiapi" fn(this: *mut Self, child_handle: Handle) -> Status, +} From 474c98ca1a194cce1311cd1c4da1c42501bb3cde Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Mon, 10 Jul 2023 23:39:54 -0400 Subject: [PATCH 2/2] uefi-raw: Add Dhcp4Protocol Co-authored-by: Ben Krieger --- uefi-raw/CHANGELOG.md | 2 +- uefi-raw/src/protocol/mod.rs | 1 + uefi-raw/src/protocol/network/dhcp4.rs | 178 +++++++++++++++++++++++++ uefi-raw/src/protocol/network/mod.rs | 1 + 4 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 uefi-raw/src/protocol/network/dhcp4.rs create mode 100644 uefi-raw/src/protocol/network/mod.rs diff --git a/uefi-raw/CHANGELOG.md b/uefi-raw/CHANGELOG.md index aaea25daa..2b15a2ecf 100644 --- a/uefi-raw/CHANGELOG.md +++ b/uefi-raw/CHANGELOG.md @@ -2,7 +2,7 @@ ## Added - Added `Ipv4Address`, `Ipv6Address`, and `MacAddress` types. -- Added `ServiceBindingProtocol`. +- Added `ServiceBindingProtocol` and `Dhcp4Protocol`. # uefi-raw - 0.5.0 (2023-11-12) diff --git a/uefi-raw/src/protocol/mod.rs b/uefi-raw/src/protocol/mod.rs index 03459bd18..31b684535 100644 --- a/uefi-raw/src/protocol/mod.rs +++ b/uefi-raw/src/protocol/mod.rs @@ -12,5 +12,6 @@ pub mod driver; pub mod file_system; pub mod loaded_image; pub mod memory_protection; +pub mod network; pub mod rng; pub mod shell_params; diff --git a/uefi-raw/src/protocol/network/dhcp4.rs b/uefi-raw/src/protocol/network/dhcp4.rs new file mode 100644 index 000000000..7ff96833c --- /dev/null +++ b/uefi-raw/src/protocol/network/dhcp4.rs @@ -0,0 +1,178 @@ +use crate::{guid, Char8, Event, Guid, Ipv4Address, MacAddress, Status}; +use core::ffi::c_void; + +newtype_enum! { + pub enum Dhcp4Event: i32 => { + SEND_DISCOVER = 0x01, + RCVD_OFFER = 0x02, + SELECT_OFFER = 0x03, + SEND_REQUEST = 0x04, + RCVD_ACK = 0x05, + RCVD_NAK = 0x06, + SEND_DECLINE = 0x07, + BOUND_COMPLETED = 0x08, + ENTER_RENEWING = 0x09, + ENTER_REBINDING = 0x0a, + ADDRESS_LOST = 0x0b, + FAIL = 0x0c, + } +} + +newtype_enum! { + pub enum Dhcp4State: i32 => { + STOPPED = 0x0, + INIT = 0x1, + SELECTING = 0x2, + REQUESTING = 0x3, + BOUND = 0x4, + RENEWING = 0x5, + REBINDING = 0x6, + INIT_REBOOT = 0x7, + REBOOTING = 0x8, + } +} + +#[derive(Debug)] +#[repr(C, packed)] +pub struct Dhcp4Packet { + pub size: u32, + pub length: u32, + pub header: Dhcp4Header, + pub magik: u32, + + /// Start of the DHCP packed option data. + /// + /// Note that this field is actually a variable-length array. + pub option: [u8; 0], +} + +#[derive(Clone, Copy, Debug)] +#[repr(C, packed)] +pub struct Dhcp4Header { + pub op_code: u8, + pub hw_type: u8, + pub hw_addr_len: u8, + pub hops: u8, + pub xid: u32, + pub seconds: u16, + pub reserved: u16, + pub client_addr: Ipv4Address, + pub your_addr: Ipv4Address, + pub server_addr: Ipv4Address, + pub gateway_addr: Ipv4Address, + pub client_hw_addr: [u8; 16], + pub server_name: [Char8; 64], + pub boot_file_name: [Char8; 128], +} + +#[derive(Debug)] +#[repr(C, packed)] +pub struct Dhcp4PacketOption { + pub op_code: u8, + pub length: u8, + + /// Start of the DHCP option data. + /// + /// Note that this field is actually a variable-length array. + pub data: [u8; 0], +} + +#[derive(Debug)] +#[repr(C)] +pub struct Dhcp4ConfigData { + pub discover_try_count: u32, + pub discover_timeout: *mut u32, + pub request_try_count: u32, + pub request_timeout: *mut u32, + pub client_address: Ipv4Address, + pub callback: Option< + unsafe extern "efiapi" fn( + this: *mut Dhcp4Protocol, + context: *const c_void, + current_state: Dhcp4State, + dhcp4_event: Dhcp4Event, + packet: *const Dhcp4Packet, + new_packet: *mut *const Dhcp4Packet, + ) -> Status, + >, + pub callback_context: *mut c_void, + pub option_count: u32, + pub option_list: *mut *const Dhcp4PacketOption, +} + +#[derive(Debug)] +#[repr(C)] +pub struct Dhcp4ModeData { + pub state: Dhcp4State, + pub config_data: Dhcp4ConfigData, + pub client_address: Ipv4Address, + pub client_mac_address: MacAddress, + pub server_address: Ipv4Address, + pub router_address: Ipv4Address, + pub subnet_mask: Ipv4Address, + pub lease_time: u32, + pub reply_packet: *const Dhcp4Packet, +} + +#[derive(Debug)] +#[repr(C)] +pub struct Dhcp4ListenPoint { + pub listen_address: Ipv4Address, + pub subnet_mask: Ipv4Address, + pub listen_port: u16, +} + +#[derive(Debug)] +#[repr(C)] +pub struct Dhcp4TransmitReceiveToken { + pub status: Status, + pub completion_event: Event, + pub remote_address: Ipv4Address, + pub remote_port: u16, + pub gateway_address: Ipv4Address, + pub listen_point_count: u32, + pub listen_points: *mut Dhcp4ListenPoint, + pub timeout_value: u32, + pub packet: *mut Dhcp4Packet, + pub response_count: u32, + pub response_list: *mut Dhcp4Packet, +} + +#[derive(Debug)] +#[repr(C)] +pub struct Dhcp4Protocol { + pub get_mode_data: + unsafe extern "efiapi" fn(this: *const Self, mode_data: *mut Dhcp4ModeData) -> Status, + pub configure: + unsafe extern "efiapi" fn(this: *mut Self, cfg_data: *const Dhcp4ConfigData) -> Status, + pub start: unsafe extern "efiapi" fn(this: *mut Self, completion_event: Event) -> Status, + pub renew_rebind: unsafe extern "efiapi" fn( + this: *mut Self, + rebind_request: bool, + completion_event: Event, + ) -> Status, + pub release: unsafe extern "efiapi" fn(this: *mut Self) -> Status, + pub stop: unsafe extern "efiapi" fn(this: *mut Self) -> Status, + pub build: unsafe extern "efiapi" fn( + this: *mut Self, + seed_packet: *mut Dhcp4Packet, + delete_count: u32, + delete_list: *mut u8, + append_count: u32, + append_list: *const *const Dhcp4PacketOption, + new_packet: *mut *mut Dhcp4Packet, + ) -> Status, + pub transmit_receive: + unsafe extern "efiapi" fn(this: *mut Self, token: *mut Dhcp4TransmitReceiveToken) -> Status, + pub parse: unsafe extern "efiapi" fn( + this: *mut Self, + packet: *mut Dhcp4Packet, + option_count: *mut u32, + packet_option_list: *mut *mut Dhcp4PacketOption, + ) -> Status, +} + +impl Dhcp4Protocol { + pub const GUID: Guid = guid!("8a219718-4ef5-4761-91c8-c0f04bda9e56"); + pub const SERVICE_BINDING_GUID: Guid = guid!("9d9a39d8-bd42-4a73-a4d5-8ee94be11380"); +} diff --git a/uefi-raw/src/protocol/network/mod.rs b/uefi-raw/src/protocol/network/mod.rs new file mode 100644 index 000000000..f8f24d706 --- /dev/null +++ b/uefi-raw/src/protocol/network/mod.rs @@ -0,0 +1 @@ +pub mod dhcp4;