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 / src / proto / media / disk_info.rs

190 lines · 7632 bytes Raw Blame History
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! DiskInfo protocol.
4
5use crate::StatusExt;
6use uefi_macros::unsafe_protocol;
7use uefi_raw::protocol::disk::DiskInfoProtocol;
8
9#[cfg(doc)]
10use crate::Status;
11
12/// Enum representing the interface type of the disk.
13///
14/// This protocol abstracts various disk interfaces, including IDE, USB, AHCI, NVME, and more.
15/// Unknown indicates an unrecognized or not yet implemented interface type.
16#[derive(Debug, Eq, PartialEq)]
17pub enum DiskInfoInterface {
18 /// Unrecognized or unsupported interface.
19 Unknown,
20 /// Integrated Drive Electronics (IDE) interface.
21 IDE,
22 /// Universal Flash Storage (UFS) interface.
23 UFS,
24 /// Universal Serial Bus (USB) interface.
25 USB,
26 /// Advanced Host Controller Interface (AHCI) interface.
27 AHCI,
28 /// Non-Volatile Memory Express (NVME) interface.
29 NVME,
30 /// Small Computer System Interface (SCSI).
31 SCSI,
32 /// Secure Digital Memory Card (SDMMC) interface.
33 SDMMC,
34}
35
36/// Structure containing metadata about the result for a call to [`DiskInfo::sense_data`].
37#[derive(Debug)]
38pub struct SenseDataInfo {
39 /// Amount of bytes returned by the [`DiskInfo::sense_data`].
40 pub bytes: usize,
41 /// Number of sense data messages contained in the resulting buffer from calling [`DiskInfo::sense_data`].
42 pub number: u8,
43}
44
45/// Structure containing information about the physical device location on the bus.
46///
47/// This is not supported by all interface types.
48#[derive(Debug)]
49pub struct DeviceLocationInfo {
50 /// For IDE, this addresses the channel (primary or secondary).
51 /// For AHCI, this returns the port.
52 pub channel: u32,
53 /// For IDE, this contains whether the device is master or slave.
54 /// For AHCI, this returns the port multiplier port.
55 pub device: u32,
56}
57
58/// Disk Info [`Protocol`].
59///
60/// This allows querying hardware information for detected disks in a simple way.
61/// Originally, this was designed for IDE and it shows.
62/// But support for a wide range of interfaces was retrofitted.
63///
64/// Not all operations are supported by all interface types!
65/// Either use [`DiskInfo::interface`] to determine what should be possible, or simply
66/// try and handle the [`Status::UNSUPPORTED`] error return value.
67///
68/// # UEFI Specification
69/// Provides the basic interfaces to abstract platform information regarding an IDE controller.
70///
71/// [`Protocol`]: uefi::proto::Protocol
72#[derive(Debug)]
73#[repr(transparent)]
74#[unsafe_protocol(DiskInfoProtocol::GUID)]
75pub struct DiskInfo(DiskInfoProtocol);
76
77impl DiskInfo {
78 /// Retrieves the interface type of the disk device.
79 ///
80 /// # Returns
81 /// [`DiskInfoInterface`] value representing the disk interface (e.g., IDE, USB, NVME, etc.).
82 #[must_use]
83 pub const fn interface(&self) -> DiskInfoInterface {
84 match self.0.interface {
85 DiskInfoProtocol::IDE_INTERFACE_GUID => DiskInfoInterface::IDE,
86 DiskInfoProtocol::UFS_INTERFACE_GUID => DiskInfoInterface::UFS,
87 DiskInfoProtocol::USB_INTERFACE_GUID => DiskInfoInterface::USB,
88 DiskInfoProtocol::AHCI_INTERFACE_GUID => DiskInfoInterface::AHCI,
89 DiskInfoProtocol::NVME_INTERFACE_GUID => DiskInfoInterface::NVME,
90 DiskInfoProtocol::SCSI_INTERFACE_GUID => DiskInfoInterface::SCSI,
91 DiskInfoProtocol::SD_MMC_INTERFACE_GUID => DiskInfoInterface::SDMMC,
92 _ => DiskInfoInterface::Unknown,
93 }
94 }
95
96 /// Performs an inquiry command on the disk device.
97 ///
98 /// # Arguments
99 /// - `bfr`: A mutable byte buffer to store the inquiry data.
100 ///
101 /// # Returns
102 /// Length of the response (amount of bytes that were written to the given buffer).
103 ///
104 /// # Errors
105 /// - [`Status::SUCCESS`] The command was accepted without any errors.
106 /// - [`Status::NOT_FOUND`] The device does not support this data class.
107 /// - [`Status::DEVICE_ERROR`] An error occurred while reading the InquiryData from the device.
108 /// - [`Status::BUFFER_TOO_SMALL`] The provided InquiryDataSize buffer is not large enough to store the required data.
109 pub fn inquiry(&self, bfr: &mut [u8]) -> crate::Result<usize> {
110 let mut len: u32 = bfr.len() as u32;
111 // SAFETY: The memory is valid.
112 unsafe {
113 (self.0.inquiry)(&self.0, bfr.as_mut_ptr().cast(), &mut len)
114 .to_result_with_val(|| len as usize)
115 }
116 }
117
118 /// Performs an identify command on the disk device.
119 ///
120 /// # Arguments
121 /// - `bfr`: A mutable byte buffer to store the identification data.
122 ///
123 /// # Returns
124 /// Length of the response (amount of bytes that were written to the given buffer).
125 ///
126 /// # Errors
127 /// - [`Status::SUCCESS`] The command was accepted without any errors.
128 /// - [`Status::NOT_FOUND`] The device does not support this data class.
129 /// - [`Status::DEVICE_ERROR`] An error occurred while reading the IdentifyData from the device.
130 /// - [`Status::BUFFER_TOO_SMALL`] The provided IdentifyDataSize buffer is not large enough to store the required data.
131 pub fn identify(&self, bfr: &mut [u8]) -> crate::Result<usize> {
132 let mut len: u32 = bfr.len() as u32;
133 // SAFETY: The memory is valid.
134 unsafe {
135 (self.0.identify)(&self.0, bfr.as_mut_ptr().cast(), &mut len)
136 .to_result_with_val(|| len as usize)
137 }
138 }
139
140 /// Retrieves sense data from the disk device.
141 ///
142 /// # Arguments
143 /// - `bfr`: A mutable byte buffer to store the sense data.
144 ///
145 /// # Returns
146 /// [`SenseDataInfo`] struct containing the number of bytes of sense data and the number of sense data structures.
147 ///
148 /// # Errors
149 /// - [`Status::SUCCESS`] The command was accepted without any errors.
150 /// - [`Status::NOT_FOUND`] The device does not support this data class.
151 /// - [`Status::DEVICE_ERROR`] An error occurred while reading the SenseData from the device.
152 /// - [`Status::BUFFER_TOO_SMALL`] The provided SenseDataSize buffer is not large enough to store the required data.
153 pub fn sense_data(&self, bfr: &mut [u8]) -> crate::Result<SenseDataInfo> {
154 let mut len: u32 = bfr.len() as u32;
155 let mut number: u8 = 0;
156 // SAFETY: The memory is valid.
157 unsafe {
158 (self.0.sense_data)(&self.0, bfr.as_mut_ptr().cast(), &mut len, &mut number)
159 .to_result_with_val(|| SenseDataInfo {
160 bytes: len as usize,
161 number,
162 })
163 }
164 }
165
166 /// Retrieves the physical location of the device on the bus.
167 ///
168 /// This operation provides information about the channel and device identifiers, which can
169 /// help determine the device's physical connection point.
170 ///
171 /// # Returns
172 /// [`DeviceLocationInfo`] struct containing the channel and device numbers.
173 ///
174 /// # Errors
175 /// - [`Status::SUCCESS`] The `IdeChannel` and `IdeDevice` values are valid.
176 /// - [`Status::UNSUPPORTED`] Not supported by this disk's interface type.
177 pub fn bus_location(&self) -> crate::Result<DeviceLocationInfo> {
178 let mut ide_channel: u32 = 0; // called ide, but also useful for other interfaces
179 let mut ide_device: u32 = 0;
180 // SAFETY: The memory is valid.
181 unsafe {
182 (self.0.which_ide)(&self.0, &mut ide_channel, &mut ide_device).to_result_with_val(
183 || DeviceLocationInfo {
184 channel: ide_channel,
185 device: ide_device,
186 },
187 )
188 }
189 }
190}