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 / media / file / info.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | use super::FileAttribute; |
| 4 | use crate::data_types::Align; |
| 5 | use crate::runtime::Time; |
| 6 | use crate::{CStr16, Char16, Error, Guid, Identify, Status}; |
| 7 | use core::fmt::{self, Display, Formatter}; |
| 8 | use core::{error, ptr, result}; |
| 9 | use ptr_meta::Pointee; |
| 10 | use uefi_raw::Boolean; |
| 11 | use uefi_raw::protocol::file_system::{ |
| 12 | FileInfo as RawFileInfo, FileSystemInfo as RawFileSystemInfo, |
| 13 | FileSystemVolumeLabel as RawFileSystemVolumeLabel, |
| 14 | }; |
| 15 | |
| 16 | /// Common trait for data structures that can be used with |
| 17 | /// `File::set_info()` or `File::get_info()`. |
| 18 | /// |
| 19 | /// The long-winded name is needed because "FileInfo" is already taken by UEFI. |
| 20 | pub trait FileProtocolInfo: Align + Identify + FromUefi + Pointee {} |
| 21 | |
| 22 | /// Trait for going from a buffer filled by UEFI to a Rust reference |
| 23 | /// |
| 24 | /// This is trivial for `Sized` types, but requires some work when operating on |
| 25 | /// dynamic-sized types like `NamedFileProtocolInfo`, as the second member of |
| 26 | /// the fat pointer must be reconstructed using hidden UEFI-provided metadata. |
| 27 | pub trait FromUefi { |
| 28 | /// Turn a buffer filled by UEFI into a (possibly fat) Rust reference |
| 29 | /// |
| 30 | /// `buffer` is the whole buffer that was passed to the firmware and |
| 31 | /// `written` is the number of bytes the firmware reported to have written |
| 32 | /// into it. Only those bytes are trusted, so the name's NUL terminator |
| 33 | /// must be among them. |
| 34 | /// |
| 35 | /// # Errors |
| 36 | /// |
| 37 | /// * [`Status::BAD_BUFFER_SIZE`]: the written bytes do not contain a |
| 38 | /// complete, NUL-terminated structure. |
| 39 | /// * [`Status::BUFFER_TOO_SMALL`]: `buffer` cannot hold the structure |
| 40 | /// including the trailing padding of the Rust type. The required size |
| 41 | /// is returned as the error payload. |
| 42 | /// |
| 43 | /// # Panics |
| 44 | /// |
| 45 | /// Panics if `buffer` is not aligned for `Self`. |
| 46 | fn from_uefi(buffer: &mut [u8], written: usize) -> crate::Result<&mut Self, Option<usize>>; |
| 47 | } |
| 48 | |
| 49 | /// Internal trait for initializing one of the info types. |
| 50 | /// |
| 51 | /// This is used with `FileInfo`, `FileSystemInfo`, and |
| 52 | /// `FileSystemVolumeLabel`, all of which are dynamically-sized structs |
| 53 | /// that have zero or more header fields followed by a variable-length |
| 54 | /// [Char16] name. |
| 55 | trait InfoInternal: Align + ptr_meta::Pointee<Metadata = usize> { |
| 56 | /// Offset in bytes of the start of the name slice at the end of |
| 57 | /// the struct. |
| 58 | fn name_offset() -> usize; |
| 59 | |
| 60 | /// Get a mutable pointer to the name slice at the end of the |
| 61 | /// struct. |
| 62 | unsafe fn name_ptr(ptr: *mut u8) -> *mut Char16 { |
| 63 | let offset_of_str = Self::name_offset(); |
| 64 | // SAFETY: The memory is valid. |
| 65 | unsafe { ptr.add(offset_of_str).cast::<Char16>() } |
| 66 | } |
| 67 | |
| 68 | /// Create a new info type in user-provided storage. |
| 69 | /// |
| 70 | /// The structure will be created in-place within the provided |
| 71 | /// `storage` buffer. The alignment and size of the buffer is |
| 72 | /// checked, then the `init` function is called to fill in the |
| 73 | /// struct header (everything except for the name slice). The name |
| 74 | /// slice is then initialized, and at that point the struct is fully |
| 75 | /// initialized so it's safe to create a reference. |
| 76 | /// |
| 77 | /// # Safety |
| 78 | /// |
| 79 | /// The `init` function must initialize the entire struct except for |
| 80 | /// the name slice. |
| 81 | unsafe fn new_impl<'buf, F>( |
| 82 | storage: &'buf mut [u8], |
| 83 | name: &CStr16, |
| 84 | init: F, |
| 85 | ) -> Result<&'buf mut Self, FileInfoCreationError> |
| 86 | where |
| 87 | F: FnOnce(*mut Self, u64), |
| 88 | { |
| 89 | // Calculate the final size of the struct. |
| 90 | let name_length_ucs2 = name.as_slice_with_nul().len(); |
| 91 | let name_size = size_of_val(name.as_slice_with_nul()); |
| 92 | let info_size = Self::name_offset() + name_size; |
| 93 | let info_size = Self::round_up_to_alignment(info_size); |
| 94 | |
| 95 | // Make sure that the storage is properly aligned |
| 96 | let storage = Self::align_buf(storage) |
| 97 | .ok_or(FileInfoCreationError::InsufficientStorage(info_size))?; |
| 98 | Self::assert_aligned(storage); |
| 99 | |
| 100 | // Make sure that the storage is large enough for our needs |
| 101 | if storage.len() < info_size { |
| 102 | return Err(FileInfoCreationError::InsufficientStorage(info_size)); |
| 103 | } |
| 104 | |
| 105 | // Create a raw fat pointer using the `storage` as a base. |
| 106 | let info_ptr: *mut Self = |
| 107 | ptr_meta::from_raw_parts_mut(storage.as_mut_ptr().cast::<()>(), name_length_ucs2); |
| 108 | |
| 109 | // Initialize the struct header. |
| 110 | init(info_ptr, info_size as u64); |
| 111 | |
| 112 | // Create a pointer to the part of info where the name is |
| 113 | // stored. Note that `info_ptr` is used rather than `storage` to |
| 114 | // comply with Stacked Borrows. |
| 115 | // SAFETY: The memory is valid. |
| 116 | let info_name_ptr = unsafe { Self::name_ptr(info_ptr.cast::<u8>()) }; |
| 117 | |
| 118 | // Initialize the name slice. |
| 119 | // SAFETY: The memory is valid. |
| 120 | unsafe { ptr::copy(name.as_ptr(), info_name_ptr, name_length_ucs2) }; |
| 121 | |
| 122 | // SAFETY: The pointer is not null, aligned, and initialized. |
| 123 | let info = unsafe { &mut *info_ptr }; |
| 124 | Ok(info) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | impl<T> FromUefi for T |
| 129 | where |
| 130 | T: InfoInternal + ?Sized, |
| 131 | { |
| 132 | fn from_uefi(buffer: &mut [u8], written: usize) -> crate::Result<&mut Self, Option<usize>> { |
| 133 | Self::assert_aligned(buffer); |
| 134 | |
| 135 | // Only the bytes the firmware wrote are meaningful. Anything beyond |
| 136 | // may be stale data from the caller. |
| 137 | let written = &buffer[..written.min(buffer.len())]; |
| 138 | let name = written |
| 139 | .get(Self::name_offset()..) |
| 140 | .ok_or(Error::new(Status::BAD_BUFFER_SIZE, None))?; |
| 141 | // Length of the UCS-2 name including its NUL terminator. |
| 142 | let (name, _) = name.as_chunks::<{ size_of::<Char16>() }>(); |
| 143 | let name_len_ucs2 = name |
| 144 | .iter() |
| 145 | .position(|c| *c == [0, 0]) |
| 146 | .ok_or(Error::new(Status::BAD_BUFFER_SIZE, None))? |
| 147 | + 1; |
| 148 | |
| 149 | // The Rust type has trailing padding, so the reference may span more |
| 150 | // bytes than the firmware wrote. All of them must be in the buffer. |
| 151 | let name_size = name_len_ucs2 * size_of::<Char16>(); |
| 152 | let info_size = Self::round_up_to_alignment(Self::name_offset() + name_size); |
| 153 | if buffer.len() < info_size { |
| 154 | return Err(Error::new(Status::BUFFER_TOO_SMALL, Some(info_size))); |
| 155 | } |
| 156 | |
| 157 | // SAFETY: The pointer is aligned and the first `info_size` bytes are |
| 158 | // initialized and in bounds. Every bit pattern is a valid header. |
| 159 | Ok(unsafe { |
| 160 | &mut *ptr_meta::from_raw_parts_mut(buffer.as_mut_ptr().cast::<()>(), name_len_ucs2) |
| 161 | }) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /// Errors that can occur when creating a `FileProtocolInfo` |
| 166 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 167 | pub enum FileInfoCreationError { |
| 168 | /// The provided buffer was too small to hold the `FileInfo`. You need at |
| 169 | /// least the indicated buffer size (in bytes). Please remember that using |
| 170 | /// a misaligned buffer will cause a decrease of usable storage capacity. |
| 171 | InsufficientStorage(usize), |
| 172 | } |
| 173 | |
| 174 | impl Display for FileInfoCreationError { |
| 175 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 176 | match self { |
| 177 | Self::InsufficientStorage(bytes) => write!( |
| 178 | f, |
| 179 | "provided buffer was too small. need at least {bytes} bytes" |
| 180 | ), |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | impl error::Error for FileInfoCreationError {} |
| 186 | |
| 187 | /// Generic file information |
| 188 | /// |
| 189 | /// The following rules apply when using this struct with `set_info()`: |
| 190 | /// |
| 191 | /// - On directories, the file size is determined by the contents of the |
| 192 | /// directory and cannot be changed by setting `file_size`. This member is |
| 193 | /// ignored by `set_info()`. |
| 194 | /// - The `physical_size` is determined by the `file_size` and cannot be |
| 195 | /// changed. This member is ignored by `set_info()`. |
| 196 | /// - The `FileAttribute::DIRECTORY` bit cannot be changed. It must match the |
| 197 | /// file’s actual type. |
| 198 | /// - A value of zero in create_time, last_access, or modification_time causes |
| 199 | /// the fields to be ignored (and not updated). |
| 200 | /// - It is forbidden to change the name of a file to the name of another |
| 201 | /// existing file in the same directory. |
| 202 | /// - If a file is read-only, the only allowed change is to remove the read-only |
| 203 | /// attribute. Other changes must be carried out in a separate transaction. |
| 204 | #[derive(Debug, Eq, PartialEq, Pointee)] |
| 205 | #[repr(C)] |
| 206 | pub struct FileInfo { |
| 207 | size: u64, |
| 208 | file_size: u64, |
| 209 | physical_size: u64, |
| 210 | create_time: Time, |
| 211 | last_access_time: Time, |
| 212 | modification_time: Time, |
| 213 | attribute: FileAttribute, |
| 214 | file_name: [Char16], |
| 215 | } |
| 216 | |
| 217 | impl FileInfo { |
| 218 | /// Create a `FileInfo` structure |
| 219 | /// |
| 220 | /// The structure will be created in-place within the provided storage |
| 221 | /// buffer. The buffer must be large enough to hold the data structure, |
| 222 | /// including a null-terminated UCS-2 `name` string. |
| 223 | /// |
| 224 | /// The buffer must be correctly aligned. You can query the required |
| 225 | /// alignment using the `alignment()` method of the `Align` trait that this |
| 226 | /// struct implements. |
| 227 | #[expect(clippy::too_many_arguments)] |
| 228 | pub fn new<'buf>( |
| 229 | storage: &'buf mut [u8], |
| 230 | file_size: u64, |
| 231 | physical_size: u64, |
| 232 | create_time: Time, |
| 233 | last_access_time: Time, |
| 234 | modification_time: Time, |
| 235 | attribute: FileAttribute, |
| 236 | file_name: &CStr16, |
| 237 | ) -> result::Result<&'buf mut Self, FileInfoCreationError> { |
| 238 | // SAFETY: The memory is valid. |
| 239 | unsafe { |
| 240 | Self::new_impl(storage, file_name, |ptr, size| { |
| 241 | (&raw mut (*ptr).size).write(size); |
| 242 | (&raw mut (*ptr).file_size).write(file_size); |
| 243 | (&raw mut (*ptr).physical_size).write(physical_size); |
| 244 | (&raw mut (*ptr).create_time).write(create_time); |
| 245 | (&raw mut (*ptr).last_access_time).write(last_access_time); |
| 246 | (&raw mut (*ptr).modification_time).write(modification_time); |
| 247 | (&raw mut (*ptr).attribute).write(attribute); |
| 248 | }) |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /// File size (number of bytes stored in the file) |
| 253 | #[must_use] |
| 254 | pub const fn file_size(&self) -> u64 { |
| 255 | self.file_size |
| 256 | } |
| 257 | |
| 258 | /// Physical space consumed by the file on the file system volume |
| 259 | #[must_use] |
| 260 | pub const fn physical_size(&self) -> u64 { |
| 261 | self.physical_size |
| 262 | } |
| 263 | |
| 264 | /// Time when the file was created |
| 265 | #[must_use] |
| 266 | pub const fn create_time(&self) -> &Time { |
| 267 | &self.create_time |
| 268 | } |
| 269 | |
| 270 | /// Time when the file was last accessed |
| 271 | #[must_use] |
| 272 | pub const fn last_access_time(&self) -> &Time { |
| 273 | &self.last_access_time |
| 274 | } |
| 275 | |
| 276 | /// Time when the file's contents were last modified |
| 277 | #[must_use] |
| 278 | pub const fn modification_time(&self) -> &Time { |
| 279 | &self.modification_time |
| 280 | } |
| 281 | |
| 282 | /// Attribute bits for the file |
| 283 | #[must_use] |
| 284 | pub const fn attribute(&self) -> FileAttribute { |
| 285 | self.attribute |
| 286 | } |
| 287 | |
| 288 | /// Name of the file |
| 289 | #[must_use] |
| 290 | pub fn file_name(&self) -> &CStr16 { |
| 291 | // SAFETY: The memory is valid. |
| 292 | unsafe { CStr16::from_ptr(self.file_name.as_ptr()) } |
| 293 | } |
| 294 | |
| 295 | /// Returns if the file is a directory. |
| 296 | #[must_use] |
| 297 | pub const fn is_directory(&self) -> bool { |
| 298 | self.attribute.contains(FileAttribute::DIRECTORY) |
| 299 | } |
| 300 | |
| 301 | /// Returns if the file is a regular file. |
| 302 | #[must_use] |
| 303 | pub const fn is_regular_file(&self) -> bool { |
| 304 | !self.is_directory() |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | impl Align for FileInfo { |
| 309 | fn alignment() -> usize { |
| 310 | 8 |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // SAFETY: The type satisfies the contract required by this unsafe impl. |
| 315 | unsafe impl Identify for FileInfo { |
| 316 | const GUID: Guid = RawFileInfo::ID; |
| 317 | } |
| 318 | |
| 319 | impl InfoInternal for FileInfo { |
| 320 | fn name_offset() -> usize { |
| 321 | 80 |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | impl FileProtocolInfo for FileInfo {} |
| 326 | |
| 327 | /// System volume information |
| 328 | /// |
| 329 | /// May only be obtained on the root directory's file handle. |
| 330 | /// |
| 331 | /// Please note that only the system volume's volume label may be set using |
| 332 | /// this information structure. Consider using `FileSystemVolumeLabel` instead. |
| 333 | #[derive(Debug, Eq, PartialEq, Pointee)] |
| 334 | #[repr(C)] |
| 335 | pub struct FileSystemInfo { |
| 336 | size: u64, |
| 337 | read_only: Boolean, |
| 338 | volume_size: u64, |
| 339 | free_space: u64, |
| 340 | block_size: u32, |
| 341 | volume_label: [Char16], |
| 342 | } |
| 343 | |
| 344 | impl FileSystemInfo { |
| 345 | /// Create a `FileSystemInfo` structure |
| 346 | /// |
| 347 | /// The structure will be created in-place within the provided storage |
| 348 | /// buffer. The buffer must be large enough to hold the data structure, |
| 349 | /// including a null-terminated UCS-2 `name` string. |
| 350 | /// |
| 351 | /// The buffer must be correctly aligned. You can query the required |
| 352 | /// alignment using the `alignment()` method of the `Align` trait that this |
| 353 | /// struct implements. |
| 354 | pub fn new<'buf>( |
| 355 | storage: &'buf mut [u8], |
| 356 | read_only: bool, |
| 357 | volume_size: u64, |
| 358 | free_space: u64, |
| 359 | block_size: u32, |
| 360 | volume_label: &CStr16, |
| 361 | ) -> result::Result<&'buf mut Self, FileInfoCreationError> { |
| 362 | // SAFETY: The memory is valid. |
| 363 | unsafe { |
| 364 | Self::new_impl(storage, volume_label, |ptr, size| { |
| 365 | (&raw mut (*ptr).size).write(size); |
| 366 | (&raw mut (*ptr).read_only).write(Boolean::from(read_only)); |
| 367 | (&raw mut (*ptr).volume_size).write(volume_size); |
| 368 | (&raw mut (*ptr).free_space).write(free_space); |
| 369 | (&raw mut (*ptr).block_size).write(block_size); |
| 370 | }) |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /// Truth that the volume only supports read access |
| 375 | #[must_use] |
| 376 | pub const fn read_only(&self) -> bool { |
| 377 | self.read_only.is_true() |
| 378 | } |
| 379 | |
| 380 | /// Number of bytes managed by the file system |
| 381 | #[must_use] |
| 382 | pub const fn volume_size(&self) -> u64 { |
| 383 | self.volume_size |
| 384 | } |
| 385 | |
| 386 | /// Number of available bytes for use by the file system |
| 387 | #[must_use] |
| 388 | pub const fn free_space(&self) -> u64 { |
| 389 | self.free_space |
| 390 | } |
| 391 | |
| 392 | /// Nominal block size by which files are typically grown |
| 393 | #[must_use] |
| 394 | pub const fn block_size(&self) -> u32 { |
| 395 | self.block_size |
| 396 | } |
| 397 | |
| 398 | /// Volume label |
| 399 | #[must_use] |
| 400 | pub fn volume_label(&self) -> &CStr16 { |
| 401 | // SAFETY: The memory is valid. |
| 402 | unsafe { CStr16::from_ptr(self.volume_label.as_ptr()) } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | impl Align for FileSystemInfo { |
| 407 | fn alignment() -> usize { |
| 408 | 8 |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | // SAFETY: The type satisfies the contract required by this unsafe impl. |
| 413 | unsafe impl Identify for FileSystemInfo { |
| 414 | const GUID: Guid = RawFileSystemInfo::ID; |
| 415 | } |
| 416 | |
| 417 | impl InfoInternal for FileSystemInfo { |
| 418 | fn name_offset() -> usize { |
| 419 | 36 |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | impl FileProtocolInfo for FileSystemInfo {} |
| 424 | |
| 425 | /// System volume label |
| 426 | /// |
| 427 | /// May only be obtained on the root directory's file handle. |
| 428 | #[derive(Debug, Eq, PartialEq, Pointee)] |
| 429 | #[repr(C)] |
| 430 | pub struct FileSystemVolumeLabel { |
| 431 | volume_label: [Char16], |
| 432 | } |
| 433 | |
| 434 | impl FileSystemVolumeLabel { |
| 435 | /// Create a `FileSystemVolumeLabel` structure |
| 436 | /// |
| 437 | /// The structure will be created in-place within the provided storage |
| 438 | /// buffer. The buffer must be large enough to hold the data structure, |
| 439 | /// including a null-terminated UCS-2 `name` string. |
| 440 | /// |
| 441 | /// The buffer must be correctly aligned. You can query the required |
| 442 | /// alignment using the `alignment()` method of the `Align` trait that this |
| 443 | /// struct implements. |
| 444 | pub fn new<'buf>( |
| 445 | storage: &'buf mut [u8], |
| 446 | volume_label: &CStr16, |
| 447 | ) -> result::Result<&'buf mut Self, FileInfoCreationError> { |
| 448 | // SAFETY: The memory is valid. |
| 449 | unsafe { Self::new_impl(storage, volume_label, |_ptr, _size| {}) } |
| 450 | } |
| 451 | |
| 452 | /// Volume label |
| 453 | #[must_use] |
| 454 | pub fn volume_label(&self) -> &CStr16 { |
| 455 | // SAFETY: The memory is valid. |
| 456 | unsafe { CStr16::from_ptr(self.volume_label.as_ptr()) } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | impl Align for FileSystemVolumeLabel { |
| 461 | fn alignment() -> usize { |
| 462 | 2 |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | // SAFETY: The type satisfies the contract required by this unsafe impl. |
| 467 | unsafe impl Identify for FileSystemVolumeLabel { |
| 468 | const GUID: Guid = RawFileSystemVolumeLabel::ID; |
| 469 | } |
| 470 | |
| 471 | impl InfoInternal for FileSystemVolumeLabel { |
| 472 | fn name_offset() -> usize { |
| 473 | 0 |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | impl FileProtocolInfo for FileSystemVolumeLabel {} |
| 478 | |
| 479 | #[cfg(test)] |
| 480 | mod tests { |
| 481 | use super::*; |
| 482 | use crate::CString16; |
| 483 | use crate::runtime::{Daylight, Time, TimeParams}; |
| 484 | use alloc::vec; |
| 485 | |
| 486 | fn validate_layout<T: InfoInternal + ?Sized>(info: &T, name: &[Char16]) { |
| 487 | // Check the hardcoded struct alignment. |
| 488 | assert_eq!(align_of_val(info), T::alignment()); |
| 489 | // Check the hardcoded name slice offset. |
| 490 | assert_eq!( |
| 491 | // SAFETY: The memory is valid. |
| 492 | unsafe { |
| 493 | name.as_ptr() |
| 494 | .cast::<u8>() |
| 495 | .offset_from(ptr::from_ref(info).cast::<u8>()) |
| 496 | }, |
| 497 | T::name_offset() as isize |
| 498 | ); |
| 499 | } |
| 500 | |
| 501 | #[test] |
| 502 | fn test_file_info() { |
| 503 | let mut storage = vec![0; 128]; |
| 504 | |
| 505 | let file_size = 123; |
| 506 | let physical_size = 456; |
| 507 | let tp = TimeParams { |
| 508 | year: 1970, |
| 509 | month: 1, |
| 510 | day: 1, |
| 511 | hour: 0, |
| 512 | minute: 0, |
| 513 | second: 0, |
| 514 | nanosecond: 0, |
| 515 | time_zone: None, |
| 516 | daylight: Daylight::IN_DAYLIGHT, |
| 517 | }; |
| 518 | let create_time = Time::new(tp).unwrap(); |
| 519 | let last_access_time = Time::new(TimeParams { year: 1971, ..tp }).unwrap(); |
| 520 | let modification_time = Time::new(TimeParams { year: 1972, ..tp }).unwrap(); |
| 521 | let attribute = FileAttribute::READ_ONLY; |
| 522 | let name = CString16::try_from("test_name").unwrap(); |
| 523 | let info = FileInfo::new( |
| 524 | &mut storage, |
| 525 | file_size, |
| 526 | physical_size, |
| 527 | create_time, |
| 528 | last_access_time, |
| 529 | modification_time, |
| 530 | attribute, |
| 531 | &name, |
| 532 | ) |
| 533 | .unwrap(); |
| 534 | |
| 535 | validate_layout(info, &info.file_name); |
| 536 | |
| 537 | // Header size: 80 bytes |
| 538 | // + Name size (including trailing null): 20 bytes |
| 539 | // = 100 |
| 540 | // Round size up to match FileInfo alignment of 8: 104 |
| 541 | assert_eq!(info.size, 104); |
| 542 | assert_eq!(info.size, size_of_val(info) as u64); |
| 543 | |
| 544 | assert_eq!(info.file_size(), file_size); |
| 545 | assert_eq!(info.physical_size(), physical_size); |
| 546 | assert_eq!(info.create_time(), &create_time); |
| 547 | assert_eq!(info.last_access_time(), &last_access_time); |
| 548 | assert_eq!(info.modification_time(), &modification_time); |
| 549 | assert_eq!(info.attribute(), attribute); |
| 550 | assert_eq!(info.file_name(), name); |
| 551 | } |
| 552 | |
| 553 | #[test] |
| 554 | fn test_file_system_info() { |
| 555 | let mut storage = vec![0; 128]; |
| 556 | |
| 557 | let read_only = true; |
| 558 | let volume_size = 123; |
| 559 | let free_space = 456; |
| 560 | let block_size = 789; |
| 561 | let name = CString16::try_from("test_name2").unwrap(); |
| 562 | let info = FileSystemInfo::new( |
| 563 | &mut storage, |
| 564 | read_only, |
| 565 | volume_size, |
| 566 | free_space, |
| 567 | block_size, |
| 568 | &name, |
| 569 | ) |
| 570 | .unwrap(); |
| 571 | |
| 572 | validate_layout(info, &info.volume_label); |
| 573 | |
| 574 | // Header size: 36 bytes |
| 575 | // + Name size (including trailing null): 22 bytes |
| 576 | // = 58 |
| 577 | // Round size up to match FileSystemInfo alignment of 8: 64 |
| 578 | assert_eq!(info.size, 64); |
| 579 | assert_eq!(info.size, size_of_val(info) as u64); |
| 580 | |
| 581 | assert_eq!(info.read_only(), read_only); |
| 582 | assert_eq!(info.volume_size, volume_size); |
| 583 | assert_eq!(info.free_space, free_space); |
| 584 | assert_eq!(info.block_size, block_size); |
| 585 | assert_eq!(info.volume_label(), name); |
| 586 | } |
| 587 | |
| 588 | #[test] |
| 589 | fn test_file_system_volume_label() { |
| 590 | let mut storage = vec![0; 128]; |
| 591 | |
| 592 | let name = CString16::try_from("test_name").unwrap(); |
| 593 | let info = FileSystemVolumeLabel::new(&mut storage, &name).unwrap(); |
| 594 | |
| 595 | validate_layout(info, &info.volume_label); |
| 596 | |
| 597 | assert_eq!(info.volume_label(), name); |
| 598 | } |
| 599 | |
| 600 | #[test] |
| 601 | fn test_file_info_from_uefi() { |
| 602 | #[repr(align(8))] |
| 603 | struct Storage([u8; 88]); |
| 604 | |
| 605 | // Header size 80 + name "ab" with NUL 6 = 86, padded to 88. |
| 606 | let mut storage = Storage([0; 88]); |
| 607 | let name = CString16::try_from("ab").unwrap(); |
| 608 | let time = Time::invalid(); |
| 609 | FileInfo::new( |
| 610 | &mut storage.0, |
| 611 | 0, |
| 612 | 0, |
| 613 | time, |
| 614 | time, |
| 615 | time, |
| 616 | FileAttribute::empty(), |
| 617 | &name, |
| 618 | ) |
| 619 | .unwrap(); |
| 620 | |
| 621 | // Firmware reports the unpadded size; the padding must be in the |
| 622 | // buffer nevertheless. |
| 623 | let info = FileInfo::from_uefi(&mut storage.0, 86).unwrap(); |
| 624 | assert_eq!(size_of_val(info), 88); |
| 625 | assert_eq!(info.file_name(), name); |
| 626 | assert_eq!( |
| 627 | FileInfo::from_uefi(&mut storage.0[..86], 86).unwrap_err(), |
| 628 | Error::new(Status::BUFFER_TOO_SMALL, Some(88)) |
| 629 | ); |
| 630 | |
| 631 | // NUL terminator not within the written bytes. |
| 632 | assert_eq!( |
| 633 | FileInfo::from_uefi(&mut storage.0, 85).unwrap_err(), |
| 634 | Error::new(Status::BAD_BUFFER_SIZE, None) |
| 635 | ); |
| 636 | assert_eq!( |
| 637 | FileInfo::from_uefi(&mut storage.0, 0).unwrap_err(), |
| 638 | Error::new(Status::BAD_BUFFER_SIZE, None) |
| 639 | ); |
| 640 | } |
| 641 | } |