interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / uefi-raw / src / protocol / network / pxe.rs

578 lines · 18400 bytes Raw Blame History
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3use crate::{Boolean, Char8, Guid, IpAddress, MacAddress, Status, guid, newtype_enum};
4use bitflags::bitflags;
5use core::ffi::c_void;
6use core::fmt::{self, Debug, Display, Formatter};
7use core::{error, net};
8
9#[derive(Debug)]
10#[repr(C)]
11pub struct PxeBaseCodeProtocol {
12 pub revision: u64,
13 pub start: unsafe extern "efiapi" fn(this: *mut Self, use_ipv6: Boolean) -> Status,
14 pub stop: unsafe extern "efiapi" fn(this: *mut Self) -> Status,
15 pub dhcp: unsafe extern "efiapi" fn(this: *mut Self, sort_offers: Boolean) -> Status,
16 pub discover: unsafe extern "efiapi" fn(
17 this: *mut Self,
18 ty: PxeBaseCodeBootType,
19 layer: *mut u16,
20 use_bis: Boolean,
21 info: *const PxeBaseCodeDiscoverInfo,
22 ) -> Status,
23 pub mtftp: unsafe extern "efiapi" fn(
24 this: *mut Self,
25 operation: PxeBaseCodeTftpOpcode,
26 buffer: *mut c_void,
27 overwrite: Boolean,
28 buffer_size: *mut u64,
29 block_size: *const usize,
30 server_ip: *const IpAddress,
31 filename: *const Char8,
32 info: *const PxeBaseCodeMtftpInfo,
33 dont_use_buffer: Boolean,
34 ) -> Status,
35 pub udp_write: unsafe extern "efiapi" fn(
36 this: *mut Self,
37 op_flags: PxeBaseCodeUdpOpFlags,
38 dest_ip: *const IpAddress,
39 dest_port: *const PxeBaseCodeUdpPort,
40 gateway_ip: *const IpAddress,
41 src_ip: *const IpAddress,
42 src_port: *mut PxeBaseCodeUdpPort,
43 header_size: *const usize,
44 header_ptr: *const c_void,
45 buffer_size: *const usize,
46 buffer_ptr: *const c_void,
47 ) -> Status,
48 pub udp_read: unsafe extern "efiapi" fn(
49 this: *mut Self,
50 op_flags: PxeBaseCodeUdpOpFlags,
51 dest_ip: *mut IpAddress,
52 dest_port: *mut PxeBaseCodeUdpPort,
53 src_ip: *mut IpAddress,
54 src_port: *mut PxeBaseCodeUdpPort,
55 header_size: *const usize,
56 header_ptr: *mut c_void,
57 buffer_size: *mut usize,
58 buffer_ptr: *mut c_void,
59 ) -> Status,
60 pub set_ip_filter: unsafe extern "efiapi" fn(
61 this: *mut Self,
62 new_filter: *const PxeBaseCodeIpFilter,
63 ) -> Status,
64 pub arp: unsafe extern "efiapi" fn(
65 this: *mut Self,
66 ip_addr: *const IpAddress,
67 mac_addr: *mut MacAddress,
68 ) -> Status,
69 pub set_parameters: unsafe extern "efiapi" fn(
70 this: *mut Self,
71 new_auto_arp: *const Boolean,
72 new_send_guid: *const Boolean,
73 new_ttl: *const u8,
74 new_tos: *const u8,
75 new_make_callback: *const Boolean,
76 ) -> Status,
77 pub set_station_ip: unsafe extern "efiapi" fn(
78 this: *mut Self,
79 new_station_ip: *const IpAddress,
80 new_subnet_mask: *const IpAddress,
81 ) -> Status,
82 pub set_packets: unsafe extern "efiapi" fn(
83 this: *mut Self,
84 new_dhcp_discover_valid: *const Boolean,
85 new_dhcp_ack_received: *const Boolean,
86 new_proxy_offer_received: *const Boolean,
87 new_pxe_discover_valid: *const Boolean,
88 new_pxe_reply_received: *const Boolean,
89 new_pxe_bis_reply_received: *const Boolean,
90 new_dhcp_discover: *const PxeBaseCodePacket,
91 new_dhcp_ack: *const PxeBaseCodePacket,
92 new_proxy_offer: *const PxeBaseCodePacket,
93 new_pxe_discover: *const PxeBaseCodePacket,
94 new_pxe_reply: *const PxeBaseCodePacket,
95 new_pxe_bis_reply: *const PxeBaseCodePacket,
96 ) -> Status,
97 pub mode: *const PxeBaseCodeMode,
98}
99
100impl PxeBaseCodeProtocol {
101 pub const GUID: Guid = guid!("03c4e603-ac28-11d3-9a2d-0090273fc14d");
102}
103
104newtype_enum! {
105 pub enum PxeBaseCodeBootType: u16 => {
106 BOOTSTRAP = 0,
107 MS_WINNT_RIS = 1,
108 INTEL_LCM = 2,
109 DOS_UNDI = 3,
110 NEC_ESMPRO = 4,
111 IBM_WSOD = 5,
112 IBM_LCCM = 6,
113 CA_UNICENTER_TNG = 7,
114 HP_OPENVIEW = 8,
115 ALTIRIS_9 = 9,
116 ALTIRIS_10 = 10,
117 ALTIRIS_11 = 11,
118 NOT_USED_12 = 12,
119 REDHAT_INSTALL = 13,
120 REDHAT_BOOT = 14,
121 REMBO = 15,
122 BEOBOOT = 16,
123 // 17..=32767: reserved.
124 // 32768..=65279: reserved for vendor use.
125 // 65280..=65534: reserved.
126 PXETEST = 65535,
127 }
128}
129
130newtype_enum! {
131 pub enum PxeBaseCodeTftpOpcode: i32 => {
132 TFTP_FIRST = 0,
133 TFTP_GET_FILE_SIZE = 1,
134 TFTP_READ_FILE = 2,
135 TFTP_WRITE_FILE = 3,
136 TFTP_READ_DIRECTORY = 4,
137 MTFTP_GET_FILE_SIZE = 5,
138 MTFTP_READ_FILE = 6,
139 MTFTP_READ_DIRECTORY = 7,
140 MTFTP_LAST = 8,
141 }
142}
143
144#[derive(Debug)]
145#[repr(C)]
146pub struct PxeBaseCodeDiscoverInfo {
147 pub use_m_cast: Boolean,
148 pub use_b_cast: Boolean,
149 pub use_u_cast: Boolean,
150 pub must_use_list: Boolean,
151 pub server_m_cast_ip: IpAddress,
152 pub ip_cnt: u16,
153
154 /// Note that this field is actually a variable-length array.
155 pub srv_list: [PxeBaseCodeSrvlist; 0],
156}
157
158/// An entry in the boot server list.
159///
160/// In the C API, this corresponds to the `EFI_PXE_BASE_CODE_SRVLIST` type.
161#[derive(Clone, Copy, Debug)]
162#[repr(C)]
163pub struct PxeBaseCodeSrvlist {
164 pub server_type: PxeBaseCodeBootType,
165 pub accept_any_response: Boolean,
166 pub reserved: u8,
167 pub ip_addr: IpAddress,
168}
169
170impl PxeBaseCodeSrvlist {
171 /// Construct a [`PxeBaseCodeSrvlist`] for a boot server reply type. If `ip_addr` is not `None`,
172 /// only boot server replies matching the provided IP address will be accepted.
173 #[must_use]
174 pub fn new(server_type: PxeBaseCodeBootType, ip_addr: Option<IpAddress>) -> Self {
175 Self {
176 server_type,
177 accept_any_response: Boolean::from(ip_addr.is_none()),
178 reserved: 0,
179 ip_addr: ip_addr.unwrap_or_default(),
180 }
181 }
182
183 /// Returns `None` if any response should be accepted, or otherwise the IP
184 /// address of a boot server whose responses should be accepted.
185 #[must_use]
186 pub fn ip_addr(&self) -> Option<&IpAddress> {
187 if self.accept_any_response.into() {
188 None
189 } else {
190 Some(&self.ip_addr)
191 }
192 }
193}
194
195pub type PxeBaseCodeUdpPort = u16;
196
197/// MTFTP connection parameters.
198///
199/// In the C API, this corresponds to the `EFI_PXE_BASE_CODE_MTFTP_INFO` type.
200#[derive(Clone, Copy, Debug)]
201#[repr(C)]
202pub struct PxeBaseCodeMtftpInfo {
203 /// We need a low level type and a high-level type with `IpAddr`
204 /// File multicast IP address. This is the IP address to which the server
205 /// will send the requested file.
206 pub m_cast_ip: IpAddress,
207 /// Client multicast listening port. This is the UDP port to which the
208 /// server will send the requested file.
209 pub c_port: PxeBaseCodeUdpPort,
210 /// Server multicast listening port. This is the UDP port on which the
211 /// server listens for multicast open requests and data acks.
212 pub s_port: PxeBaseCodeUdpPort,
213 /// The number of seconds a client should listen for an active multicast
214 /// session before requesting a new multicast session.
215 pub listen_timeout: u16,
216 /// The number of seconds a client should wait for a packet from the server
217 /// before retransmitting the previous open request or data ack packet.
218 pub transmit_timeout: u16,
219}
220
221bitflags! {
222 /// Flags for UDP read and write operations.
223 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
224 #[repr(transparent)]
225 pub struct PxeBaseCodeUdpOpFlags: u16 {
226 /// Receive a packet sent from any IP address in UDP read operations.
227 const ANY_SRC_IP = 0x0001;
228
229 /// Receive a packet sent from any UDP port in UDP read operations. If
230 /// the source port is not specified in UDP write operations, the
231 /// source port will be automatically selected.
232 const ANY_SRC_PORT = 0x0002;
233
234 /// Receive a packet sent to any IP address in UDP read operations.
235 const ANY_DEST_IP = 0x0004;
236
237 /// Receive a packet sent to any UDP port in UDP read operations.
238 const ANY_DEST_PORT = 0x0008;
239
240 /// The software filter is used in UDP read operations.
241 const USE_FILTER = 0x0010;
242
243 /// If required, a UDP write operation may be broken up across multiple packets.
244 const MAY_FRAGMENT = 0x0020;
245 }
246}
247
248#[derive(Clone, Debug)]
249#[repr(C)]
250pub struct PxeBaseCodeMode {
251 pub started: Boolean,
252 pub ipv6_available: Boolean,
253 pub ipv6_supported: Boolean,
254 pub using_ipv6: Boolean,
255 pub bis_supported: Boolean,
256 pub bis_detected: Boolean,
257 pub auto_arp: Boolean,
258 pub send_guid: Boolean,
259 pub dhcp_discover_valid: Boolean,
260 pub dhcp_ack_received: Boolean,
261 pub proxy_offer_received: Boolean,
262 pub pxe_discover_valid: Boolean,
263 pub pxe_reply_received: Boolean,
264 pub pxe_bis_reply_received: Boolean,
265 pub icmp_error_received: Boolean,
266 pub tftp_error_received: Boolean,
267 pub make_callbacks: Boolean,
268 pub ttl: u8,
269 pub tos: u8,
270 pub station_ip: IpAddress,
271 pub subnet_mask: IpAddress,
272 pub dhcp_discover: PxeBaseCodePacket,
273 pub dhcp_ack: PxeBaseCodePacket,
274 pub proxy_offer: PxeBaseCodePacket,
275 pub pxe_discover: PxeBaseCodePacket,
276 pub pxe_reply: PxeBaseCodePacket,
277 pub pxe_bis_reply: PxeBaseCodePacket,
278 pub ip_filter: PxeBaseCodeIpFilter,
279 pub arp_cache_entries: u32,
280 pub arp_cache: [PxeBaseCodeArpEntry; 8],
281 pub route_table_entries: u32,
282 pub route_table: [PxeBaseCodeRouteEntry; 8],
283 pub icmp_error: PxeBaseCodeIcmpError,
284 pub tftp_error: PxeBaseCodeTftpError,
285}
286
287/// A network packet.
288///
289/// In the C API, this corresponds to the `EFI_PXE_BASE_CODE_PACKET` type.
290#[derive(Clone, Copy)]
291#[repr(C)]
292pub union PxeBaseCodePacket {
293 pub raw: [u8; 1472],
294 pub dhcpv4: PxeBaseCodeDhcpV4Packet,
295 pub dhcpv6: PxeBaseCodeDhcpV6Packet,
296}
297
298impl Debug for PxeBaseCodePacket {
299 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
300 f.debug_struct("PxeBaseCodePacket").finish()
301 }
302}
303
304impl AsRef<[u8; 1472]> for PxeBaseCodePacket {
305 fn as_ref(&self) -> &[u8; 1472] {
306 // SAFETY: The packet union is defined with `raw` as the byte view for
307 // the full storage, and any byte pattern is valid here.
308 unsafe { &self.raw }
309 }
310}
311
312impl AsRef<PxeBaseCodeDhcpV4Packet> for PxeBaseCodePacket {
313 fn as_ref(&self) -> &PxeBaseCodeDhcpV4Packet {
314 // SAFETY: The caller chooses this view; the union stores the packet
315 // bytes in a layout compatible with the DHCPv4 packet type.
316 unsafe { &self.dhcpv4 }
317 }
318}
319
320impl AsRef<PxeBaseCodeDhcpV6Packet> for PxeBaseCodePacket {
321 fn as_ref(&self) -> &PxeBaseCodeDhcpV6Packet {
322 // SAFETY: The caller chooses this view; the union stores the packet
323 // bytes in a layout compatible with the DHCPv6 packet type.
324 unsafe { &self.dhcpv6 }
325 }
326}
327
328/// A DHCPv4 packet.
329///
330/// In the C API, this corresponds to the `EFI_PXE_BASE_CODE_DHCPV4_PACKET` type.
331#[derive(Clone, Copy, Debug)]
332#[repr(C)]
333pub struct PxeBaseCodeDhcpV4Packet {
334 /// Packet op code / message type.
335 pub bootp_opcode: u8,
336 /// Hardware address type.
337 pub bootp_hw_type: u8,
338 /// Hardware address length.
339 pub bootp_hw_addr_len: u8,
340 /// Client sets to zero, optionally used by gateways in cross-gateway booting.
341 pub bootp_gate_hops: u8,
342 pub bootp_ident: u32,
343 pub bootp_seconds: u16,
344 pub bootp_flags: u16,
345 /// Client IP address, filled in by client in bootrequest if known.
346 pub bootp_ci_addr: [u8; 4],
347 /// 'your' (client) IP address; filled by server if client doesn't know its own address (`bootp_ci_addr` was 0).
348 pub bootp_yi_addr: [u8; 4],
349 /// Server IP address, returned in bootreply by server.
350 pub bootp_si_addr: [u8; 4],
351 /// Gateway IP address, used in optional cross-gateway booting.
352 pub bootp_gi_addr: [u8; 4],
353 /// Client hardware address, filled in by client.
354 pub bootp_hw_addr: [u8; 16],
355 /// Optional server host name, null terminated string.
356 pub bootp_srv_name: [u8; 64],
357 /// Boot file name, null terminated string, 'generic' name or null in
358 /// bootrequest, fully qualified directory-path name in bootreply.
359 pub bootp_boot_file: [u8; 128],
360 /// Validation magic number.
361 pub dhcp_magik: u32,
362 /// Optional vendor-specific area, e.g. could be hardware type/serial on request, or 'capability' / remote file system handle on reply. This info may be set aside for use by a third phase bootstrap or kernel.
363 pub dhcp_options: [u8; 56],
364}
365
366impl PxeBaseCodeDhcpV4Packet {
367 /// The expected value for [`Self::dhcp_magik`].
368 pub const DHCP_MAGIK: u32 = 0x63825363;
369
370 /// Transaction ID, a random number, used to match this boot request with the responses it generates.
371 #[must_use]
372 pub const fn bootp_ident(&self) -> u32 {
373 u32::from_be(self.bootp_ident)
374 }
375
376 /// Filled in by client, seconds elapsed since client started trying to boot.
377 #[must_use]
378 pub const fn bootp_seconds(&self) -> u16 {
379 u16::from_be(self.bootp_seconds)
380 }
381
382 /// The flags.
383 #[must_use]
384 pub const fn bootp_flags(&self) -> PxeBaseCodeDhcpV4Flags {
385 PxeBaseCodeDhcpV4Flags::from_bits_truncate(u16::from_be(self.bootp_flags))
386 }
387
388 /// A magic cookie, should be [`Self::DHCP_MAGIK`].
389 #[must_use]
390 pub const fn dhcp_magik(&self) -> u32 {
391 u32::from_be(self.dhcp_magik)
392 }
393}
394
395bitflags! {
396 /// Represents the 'flags' field for a [`PxeBaseCodeDhcpV4Packet`].
397 #[repr(transparent)]
398 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
399 pub struct PxeBaseCodeDhcpV4Flags: u16 {
400 /// Should be set when the client cannot receive unicast IP datagrams
401 /// until its protocol software has been configured with an IP address.
402 const BROADCAST = 1;
403 }
404}
405
406/// A DHCPv6 packet.
407///
408/// In the C API, this corresponds to the `EFI_PXE_BASE_CODE_DHCPV6_PACKET` type.
409#[derive(Clone, Copy, Debug)]
410#[repr(C)]
411pub struct PxeBaseCodeDhcpV6Packet {
412 /// The message type.
413 pub message_type: u8,
414 /// The transaction id.
415 pub transaction_id: [u8; 3],
416 /// A byte array containing DHCP options.
417 pub dhcp_options: [u8; 1024],
418}
419
420impl PxeBaseCodeDhcpV6Packet {
421 /// The transaction id.
422 #[must_use]
423 pub fn transaction_id(&self) -> u32 {
424 (u32::from(self.transaction_id[0]) << 16)
425 | (u32::from(self.transaction_id[1]) << 8)
426 | u32::from(self.transaction_id[2])
427 }
428}
429
430/// IP receive filter settings.
431///
432/// In the C API, this corresponds to the `EFI_PXE_BASE_CODE_IP_FILTER` type.
433#[derive(Clone, Copy, Debug)]
434#[repr(C)]
435pub struct PxeBaseCodeIpFilter {
436 pub filters: PxeBaseCodeIpFilterFlags,
437 pub ip_cnt: u8,
438 pub reserved: u16,
439 pub ip_list: [IpAddress; 8],
440}
441
442impl PxeBaseCodeIpFilter {
443 #[must_use]
444 pub fn new(filters: PxeBaseCodeIpFilterFlags, ip_list: &[net::IpAddr]) -> Self {
445 assert!(ip_list.len() <= 8);
446
447 let ip_cnt = ip_list.len() as u8;
448 let mut buffer = [IpAddress::default(); 8];
449 for (index, ip_address) in ip_list
450 .iter()
451 .cloned()
452 .map(|value| value.into())
453 .enumerate()
454 {
455 buffer[index] = ip_address;
456 }
457
458 Self {
459 filters,
460 ip_cnt,
461 reserved: 0,
462 ip_list: buffer,
463 }
464 }
465
466 /// A list of IP addresses other than the station IP that should be enabled.
467 ///
468 /// May be multicast or unicast.
469 #[must_use]
470 pub fn ip_list(&self) -> &[IpAddress] {
471 &self.ip_list[..usize::from(self.ip_cnt)]
472 }
473}
474
475bitflags! {
476 /// IP receive filters.
477 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
478 #[repr(transparent)]
479 pub struct PxeBaseCodeIpFilterFlags: u8 {
480 /// Enable the Station IP address.
481 const STATION_IP = 0x01;
482
483 /// Enable IPv4 broadcast addresses.
484 const BROADCAST = 0x02;
485
486 /// Enable all addresses.
487 const PROMISCUOUS = 0x04;
488
489 /// Enable all multicast addresses.
490 const PROMISCUOUS_MULTICAST = 0x08;
491 }
492}
493
494/// An entry for the ARP cache found in [`PxeBaseCodeMode::arp_cache`].
495///
496/// In the C API, this corresponds to the `EFI_PXE_BASE_CODE_ARP_ENTRY` type.
497#[derive(Clone, Copy, Debug)]
498#[repr(C)]
499pub struct PxeBaseCodeArpEntry {
500 pub ip_addr: IpAddress,
501 pub mac_addr: MacAddress,
502}
503
504/// An entry for the route table found in [`PxeBaseCodeMode::route_table`].
505///
506/// In the C API, this corresponds to the `EFI_PXE_BASE_CODE_ROUTE_ENTRY` type.
507#[derive(Clone, Copy, Debug)]
508#[repr(C)]
509pub struct PxeBaseCodeRouteEntry {
510 pub ip_addr: IpAddress,
511 pub subnet_mask: IpAddress,
512 pub gw_addr: IpAddress,
513}
514
515/// An ICMP error packet.
516///
517/// In the C API, this corresponds to the `EFI_PXE_BASE_CODE_ICMP_ERROR` type.
518#[derive(Clone, Debug)]
519#[repr(C)]
520pub struct PxeBaseCodeIcmpError {
521 pub ty: u8,
522 pub code: u8,
523 pub checksum: u16,
524 pub u: PxeBaseCodeIcmpErrorUnion,
525 pub data: [u8; 494],
526}
527
528impl Display for PxeBaseCodeIcmpError {
529 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
530 write!(f, "{self:?}")
531 }
532}
533
534impl error::Error for PxeBaseCodeIcmpError {}
535
536/// In the C API, this is an anonymous union inside the definition of
537/// `EFI_PXE_BASE_CODE_ICMP_ERROR`.
538#[derive(Clone, Copy)]
539#[repr(C)]
540pub union PxeBaseCodeIcmpErrorUnion {
541 pub reserved: u32,
542 pub mtu: u32,
543 pub pointer: u32,
544 pub echo: PxeBaseCodeIcmpErrorEcho,
545}
546
547impl Debug for PxeBaseCodeIcmpErrorUnion {
548 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
549 f.debug_struct("PxeBaseCodeIcmpErrorUnion").finish()
550 }
551}
552
553/// In the C API, this is an anonymous struct inside the definition of
554/// `EFI_PXE_BASE_CODE_ICMP_ERROR`.
555#[derive(Clone, Copy, Debug)]
556#[repr(C)]
557pub struct PxeBaseCodeIcmpErrorEcho {
558 pub identifier: u16,
559 pub sequence: u16,
560}
561
562/// A TFTP error packet.
563///
564/// In the C API, this corresponds to the `EFI_PXE_BASE_CODE_TFTP_ERROR` type.
565#[derive(Clone, Debug)]
566#[repr(C)]
567pub struct PxeBaseCodeTftpError {
568 pub error_code: u8,
569 pub error_string: [Char8; 127],
570}
571
572impl Display for PxeBaseCodeTftpError {
573 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
574 write!(f, "{self:?}")
575 }
576}
577
578impl error::Error for PxeBaseCodeTftpError {}