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.rs

206 lines · 7914 bytes Raw Blame History
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Disk I/O protocols [`DiskIo`] and [`DiskIo2`].
4
5use crate::proto::unsafe_protocol;
6use crate::util::opt_nonnull_to_ptr;
7use crate::{Event, Result, Status, StatusExt};
8use core::ptr::NonNull;
9use uefi_raw::protocol::disk::{DiskIo2Protocol, DiskIoProtocol};
10
11/// Disk I/O [`Protocol`].
12///
13/// This protocol is used to abstract the block accesses of the block I/O
14/// protocol to a more general offset-length protocol. Firmware is
15/// responsible for adding this protocol to any block I/O interface that
16/// appears in the system that does not already have a disk I/O protocol.
17///
18/// [`Protocol`]: uefi::proto::Protocol
19#[derive(Debug)]
20#[repr(transparent)]
21#[unsafe_protocol(DiskIoProtocol::GUID)]
22pub struct DiskIo(DiskIoProtocol);
23
24impl DiskIo {
25 /// Reads bytes from the disk device.
26 ///
27 /// # Arguments
28 /// * `media_id` - ID of the medium to be read.
29 /// * `offset` - Starting byte offset on the logical block I/O device to read from.
30 /// * `buffer` - Pointer to a buffer to read into.
31 ///
32 /// # Errors
33 /// * [`Status::INVALID_PARAMETER`] The read request contains device addresses that are not valid for the device.
34 /// * [`Status::DEVICE_ERROR`] The device reported an error while performing the read operation.
35 /// * [`Status::NO_MEDIA`] There is no medium in the device.
36 /// * [`Status::MEDIA_CHANGED`] `media_id` is not for the current medium.
37 pub fn read_disk(&self, media_id: u32, offset: u64, buffer: &mut [u8]) -> Result {
38 // SAFETY: The memory is valid.
39 unsafe {
40 (self.0.read_disk)(
41 &self.0,
42 media_id,
43 offset,
44 buffer.len(),
45 buffer.as_mut_ptr().cast(),
46 )
47 }
48 .to_result()
49 }
50
51 /// Writes bytes to the disk device.
52 ///
53 /// # Arguments
54 /// * `media_id` - ID of the medium to be written.
55 /// * `offset` - Starting byte offset on the logical block I/O device to write to.
56 /// * `buffer` - Pointer to a buffer to write from.
57 ///
58 /// # Errors
59 /// * [`Status::INVALID_PARAMETER`] The write request contains device addresses that are not valid for the device.
60 /// * [`Status::DEVICE_ERROR`] The device reported an error while performing the write operation.
61 /// * [`Status::NO_MEDIA`] There is no medium in the device.
62 /// * [`Status::MEDIA_CHANGED`] `media_id` is not for the current medium.
63 /// * [`Status::WRITE_PROTECTED`] The device cannot be written to.
64 pub fn write_disk(&mut self, media_id: u32, offset: u64, buffer: &[u8]) -> Result {
65 // SAFETY: The memory is valid.
66 unsafe {
67 (self.0.write_disk)(
68 &mut self.0,
69 media_id,
70 offset,
71 buffer.len(),
72 buffer.as_ptr().cast(),
73 )
74 }
75 .to_result()
76 }
77}
78
79/// Asynchronous transaction token for disk I/O 2 operations.
80#[repr(C)]
81#[derive(Debug)]
82pub struct DiskIo2Token {
83 /// Event to be signalled when an asynchronous disk I/O operation completes.
84 pub event: Option<Event>,
85 /// Transaction status code.
86 pub transaction_status: Status,
87}
88
89/// Disk I/O 2 [`Protocol`].
90///
91/// This protocol provides an extension to the disk I/O protocol to enable
92/// non-blocking / asynchronous byte-oriented disk operation.
93///
94/// [`Protocol`]: uefi::proto::Protocol
95#[derive(Debug)]
96#[repr(transparent)]
97#[unsafe_protocol(DiskIo2Protocol::GUID)]
98pub struct DiskIo2(DiskIo2Protocol);
99
100impl DiskIo2 {
101 /// Terminates outstanding asynchronous requests to the device.
102 ///
103 /// # Errors
104 /// * [`Status::DEVICE_ERROR`] The device reported an error while performing
105 pub fn cancel(&mut self) -> Result {
106 // SAFETY: The memory is valid.
107 unsafe { (self.0.cancel)(&mut self.0) }.to_result()
108 }
109
110 /// Reads bytes from the disk device.
111 ///
112 /// # Arguments
113 /// * `media_id` - ID of the medium to be read from.
114 /// * `offset` - Starting byte offset on the logical block I/O device to read from.
115 /// * `token` - Transaction token for asynchronous read.
116 /// * `len` - Buffer size.
117 /// * `buffer` - Buffer to read into.
118 ///
119 /// # Safety
120 ///
121 /// Because of the asynchronous nature of the disk transaction, manual lifetime
122 /// tracking is required.
123 ///
124 /// # Errors
125 /// * [`Status::INVALID_PARAMETER`] The read request contains device addresses that are not valid for the device.
126 /// * [`Status::OUT_OF_RESOURCES`] The request could not be completed due to a lack of resources.
127 /// * [`Status::MEDIA_CHANGED`] `media_id` is not for the current medium.
128 /// * [`Status::NO_MEDIA`] There is no medium in the device.
129 /// * [`Status::DEVICE_ERROR`] The device reported an error while performing the read operation.
130 pub unsafe fn read_disk_raw(
131 &self,
132 media_id: u32,
133 offset: u64,
134 token: Option<NonNull<DiskIo2Token>>,
135 len: usize,
136 buffer: *mut u8,
137 ) -> Result {
138 let token = opt_nonnull_to_ptr(token);
139 // SAFETY: The memory is valid.
140 unsafe {
141 (self.0.read_disk_ex)(&self.0, media_id, offset, token.cast(), len, buffer.cast())
142 }
143 .to_result()
144 }
145
146 /// Writes bytes to the disk device.
147 ///
148 /// # Arguments
149 /// * `media_id` - ID of the medium to write to.
150 /// * `offset` - Starting byte offset on the logical block I/O device to write to.
151 /// * `token` - Transaction token for asynchronous write.
152 /// * `len` - Buffer size.
153 /// * `buffer` - Buffer to write from.
154 ///
155 /// # Safety
156 ///
157 /// Because of the asynchronous nature of the disk transaction, manual lifetime
158 /// tracking is required.
159 ///
160 /// # Errors
161 /// * [`Status::INVALID_PARAMETER`] The write request contains device addresses that are not valid for the device.
162 /// * [`Status::OUT_OF_RESOURCES`] The request could not be completed due to a lack of resources.
163 /// * [`Status::MEDIA_CHANGED` `media_id` is not for the current medium.
164 /// * [`Status::NO_MEDIA`] There is no medium in the device.
165 /// * [`Status::DEVICE_ERROR`] The device reported an error while performing the write operation.
166 /// * [`Status::WRITE_PROTECTED`] The device cannot be written to.
167 pub unsafe fn write_disk_raw(
168 &mut self,
169 media_id: u32,
170 offset: u64,
171 token: Option<NonNull<DiskIo2Token>>,
172 len: usize,
173 buffer: *const u8,
174 ) -> Result {
175 let token = opt_nonnull_to_ptr(token);
176 // SAFETY: The memory is valid.
177 unsafe {
178 (self.0.write_disk_ex)(
179 &mut self.0,
180 media_id,
181 offset,
182 token.cast(),
183 len,
184 buffer.cast(),
185 )
186 }
187 .to_result()
188 }
189
190 /// Flushes all modified data to the physical device.
191 ///
192 /// # Arguments
193 /// * `token` - Transaction token for the asynchronous flush.
194 ///
195 /// # Errors
196 /// * [`Status::OUT_OF_RESOURCES`] The request could not be completed due to a lack of resources.
197 /// * [`Status::MEDIA_CHANGED`] The medium in the device has changed since the last access.
198 /// * [`Status::NO_MEDIA`] There is no medium in the device.
199 /// * [`Status::DEVICE_ERROR`] The device reported an error while performing the flush operation.
200 /// * [`Status::WRITE_PROTECTED`] The device cannot be written to.
201 pub fn flush_disk(&mut self, token: Option<NonNull<DiskIo2Token>>) -> Result {
202 let token = opt_nonnull_to_ptr(token);
203 // SAFETY: The memory is valid.
204 unsafe { (self.0.flush_disk_ex)(&mut self.0, token.cast()) }.to_result()
205 }
206}