aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / proto / network / ip4config2.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | #![cfg(feature = "alloc")] |
| 4 | |
| 5 | //! IP4 Config2 Protocol. |
| 6 | |
| 7 | use alloc::vec; |
| 8 | use alloc::vec::Vec; |
| 9 | use core::ffi::c_void; |
| 10 | use core::net::Ipv4Addr; |
| 11 | use core::ptr; |
| 12 | use core::time::Duration; |
| 13 | use log::{debug, trace}; |
| 14 | use uefi::boot::ScopedProtocol; |
| 15 | use uefi::prelude::*; |
| 16 | use uefi::proto::unsafe_protocol; |
| 17 | use uefi_raw::protocol::network::ip4_config2::{ |
| 18 | Ip4Config2DataType, Ip4Config2InterfaceInfo, Ip4Config2Policy, Ip4Config2Protocol, |
| 19 | }; |
| 20 | |
| 21 | /// IP4 Config2 [`Protocol`]. Configure IPv4 networking. |
| 22 | /// |
| 23 | /// [`Protocol`]: uefi::proto::Protocol |
| 24 | #[derive(Debug)] |
| 25 | #[unsafe_protocol(Ip4Config2Protocol::GUID)] |
| 26 | #[repr(transparent)] |
| 27 | pub struct Ip4Config2(pub Ip4Config2Protocol); |
| 28 | |
| 29 | impl Ip4Config2 { |
| 30 | /// Open IP4 Config2 protocol for the given NIC handle. |
| 31 | pub fn new(nic_handle: Handle) -> uefi::Result<ScopedProtocol<Self>> { |
| 32 | let protocol; |
| 33 | // SAFETY: The memory is valid. |
| 34 | unsafe { |
| 35 | protocol = boot::open_protocol::<Self>( |
| 36 | boot::OpenProtocolParams { |
| 37 | handle: nic_handle, |
| 38 | agent: boot::image_handle(), |
| 39 | controller: None, |
| 40 | }, |
| 41 | boot::OpenProtocolAttributes::GetProtocol, |
| 42 | )?; |
| 43 | } |
| 44 | Ok(protocol) |
| 45 | } |
| 46 | |
| 47 | /// Set configuration data. It is recommended to type-specific set_* helpers instead of calling this directly. |
| 48 | pub fn set_data(&mut self, data_type: Ip4Config2DataType, data: &mut [u8]) -> uefi::Result<()> { |
| 49 | // SAFETY: The memory is valid. |
| 50 | let status = unsafe { |
| 51 | let data_ptr = data.as_mut_ptr().cast::<c_void>(); |
| 52 | (self.0.set_data)(&mut self.0, data_type, data.len(), data_ptr) |
| 53 | }; |
| 54 | match status { |
| 55 | Status::SUCCESS => Ok(()), |
| 56 | _ => Err(status.into()), |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// Get configuration data. It is recommended to type-specific get_* helpers instead of calling this directly. |
| 61 | pub fn get_data(&mut self, data_type: Ip4Config2DataType) -> uefi::Result<Vec<u8>> { |
| 62 | let mut data_size = 0; |
| 63 | |
| 64 | // call #1: figure return buffer size |
| 65 | // SAFETY: The memory is valid. |
| 66 | let status = unsafe { |
| 67 | let null = ptr::null_mut(); |
| 68 | (self.0.get_data)(&mut self.0, data_type, &mut data_size, null) |
| 69 | }; |
| 70 | if status != Status::BUFFER_TOO_SMALL { |
| 71 | return Err(status.into()); |
| 72 | } |
| 73 | |
| 74 | // call #2: get data |
| 75 | let mut data = vec![0; data_size]; |
| 76 | // SAFETY: The memory is valid. |
| 77 | let status = unsafe { |
| 78 | let data_ptr = data.as_mut_ptr().cast::<c_void>(); |
| 79 | (self.0.get_data)(&mut self.0, data_type, &mut data_size, data_ptr) |
| 80 | }; |
| 81 | match status { |
| 82 | Status::SUCCESS => Ok(data), |
| 83 | _ => Err(status.into()), |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /// Set config policy (static vs. dhcp). |
| 88 | pub fn set_policy(&mut self, policy: Ip4Config2Policy) -> uefi::Result<()> { |
| 89 | let mut data: [u8; 4] = policy.0.to_ne_bytes(); |
| 90 | self.set_data(Ip4Config2DataType::POLICY, &mut data) |
| 91 | } |
| 92 | |
| 93 | /// Get current interface configuration. |
| 94 | pub fn get_interface_info(&mut self) -> uefi::Result<Ip4Config2InterfaceInfo> { |
| 95 | let data = self.get_data(Ip4Config2DataType::INTERFACE_INFO)?; |
| 96 | if data.len() < size_of::<Ip4Config2InterfaceInfo>() { |
| 97 | return Err(Status::BAD_BUFFER_SIZE.into()); |
| 98 | } |
| 99 | // SAFETY: `data` holds at least a full `Ip4Config2InterfaceInfo`. |
| 100 | let info = unsafe { |
| 101 | data.as_ptr() |
| 102 | .cast::<Ip4Config2InterfaceInfo>() |
| 103 | // The buffer is a `Vec<u8>` and thus only byte-aligned, while |
| 104 | // `Ip4Config2InterfaceInfo` has a larger alignment. |
| 105 | .read_unaligned() |
| 106 | }; |
| 107 | Ok(Ip4Config2InterfaceInfo { |
| 108 | route_table_size: 0, |
| 109 | route_table: ptr::null_mut(), |
| 110 | ..info |
| 111 | }) |
| 112 | } |
| 113 | |
| 114 | /// Bring up network interface. |
| 115 | /// |
| 116 | /// Does nothing in case the network is already set up. Otherwise turns on |
| 117 | /// DHCP and waits until an IPv4 address has been assigned. |
| 118 | /// |
| 119 | /// Returns TIMEOUT error in case DHCP configuration does not finish within |
| 120 | /// 30 seconds. |
| 121 | pub fn ifup(&mut self) -> uefi::Result<()> { |
| 122 | const TIMEOUT_SECS: u64 = 30; |
| 123 | |
| 124 | let no_address = Ipv4Addr::from_bits(0); |
| 125 | |
| 126 | let info = self.get_interface_info()?; |
| 127 | if info.station_addr != no_address.into() { |
| 128 | debug!("Network is already up: addr v4: {}", info.station_addr); |
| 129 | return Ok(()); |
| 130 | } |
| 131 | |
| 132 | debug!("DHCP "); |
| 133 | self.set_policy(Ip4Config2Policy::DHCP)?; |
| 134 | |
| 135 | for _ in 0..TIMEOUT_SECS { |
| 136 | boot::stall(Duration::from_secs(1)); |
| 137 | trace!(".\r"); |
| 138 | let info = self.get_interface_info()?; |
| 139 | if info.station_addr != no_address.into() { |
| 140 | debug!("OK: addr v4: {}", info.station_addr); |
| 141 | return Ok(()); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | Err(Status::TIMEOUT.into()) |
| 146 | } |
| 147 | } |