Skip to content

uefi-raw: Add ServiceBindingProtocol and Dhcp4Protocol #1020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions uefi-raw/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Added
- Added `Ipv4Address`, `Ipv6Address`, and `MacAddress` types.
- Added `ServiceBindingProtocol` and `Dhcp4Protocol`.

# uefi-raw - 0.5.0 (2023-11-12)

Expand Down
8 changes: 8 additions & 0 deletions uefi-raw/src/protocol/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
1 change: 1 addition & 0 deletions uefi-raw/src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
178 changes: 178 additions & 0 deletions uefi-raw/src/protocol/network/dhcp4.rs
Original file line number Diff line number Diff line change
@@ -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");
}
1 change: 1 addition & 0 deletions uefi-raw/src/protocol/network/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod dhcp4;