~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/rust/kernel/firmware.rs

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 
  3 //! Firmware abstraction
  4 //!
  5 //! C header: [`include/linux/firmware.h`](srctree/include/linux/firmware.h)
  6 
  7 use crate::{bindings, device::Device, error::Error, error::Result, str::CStr};
  8 use core::ptr::NonNull;
  9 
 10 /// # Invariants
 11 ///
 12 /// One of the following: `bindings::request_firmware`, `bindings::firmware_request_nowarn`,
 13 /// `bindings::firmware_request_platform`, `bindings::request_firmware_direct`.
 14 struct FwFunc(
 15     unsafe extern "C" fn(*mut *const bindings::firmware, *const i8, *mut bindings::device) -> i32,
 16 );
 17 
 18 impl FwFunc {
 19     fn request() -> Self {
 20         Self(bindings::request_firmware)
 21     }
 22 
 23     fn request_nowarn() -> Self {
 24         Self(bindings::firmware_request_nowarn)
 25     }
 26 }
 27 
 28 /// Abstraction around a C `struct firmware`.
 29 ///
 30 /// This is a simple abstraction around the C firmware API. Just like with the C API, firmware can
 31 /// be requested. Once requested the abstraction provides direct access to the firmware buffer as
 32 /// `&[u8]`. The firmware is released once [`Firmware`] is dropped.
 33 ///
 34 /// # Invariants
 35 ///
 36 /// The pointer is valid, and has ownership over the instance of `struct firmware`.
 37 ///
 38 /// The `Firmware`'s backing buffer is not modified.
 39 ///
 40 /// # Examples
 41 ///
 42 /// ```no_run
 43 /// # use kernel::{c_str, device::Device, firmware::Firmware};
 44 ///
 45 /// # fn no_run() -> Result<(), Error> {
 46 /// # // SAFETY: *NOT* safe, just for the example to get an `ARef<Device>` instance
 47 /// # let dev = unsafe { Device::from_raw(core::ptr::null_mut()) };
 48 ///
 49 /// let fw = Firmware::request(c_str!("path/to/firmware.bin"), &dev)?;
 50 /// let blob = fw.data();
 51 ///
 52 /// # Ok(())
 53 /// # }
 54 /// ```
 55 pub struct Firmware(NonNull<bindings::firmware>);
 56 
 57 impl Firmware {
 58     fn request_internal(name: &CStr, dev: &Device, func: FwFunc) -> Result<Self> {
 59         let mut fw: *mut bindings::firmware = core::ptr::null_mut();
 60         let pfw: *mut *mut bindings::firmware = &mut fw;
 61 
 62         // SAFETY: `pfw` is a valid pointer to a NULL initialized `bindings::firmware` pointer.
 63         // `name` and `dev` are valid as by their type invariants.
 64         let ret = unsafe { func.0(pfw as _, name.as_char_ptr(), dev.as_raw()) };
 65         if ret != 0 {
 66             return Err(Error::from_errno(ret));
 67         }
 68 
 69         // SAFETY: `func` not bailing out with a non-zero error code, guarantees that `fw` is a
 70         // valid pointer to `bindings::firmware`.
 71         Ok(Firmware(unsafe { NonNull::new_unchecked(fw) }))
 72     }
 73 
 74     /// Send a firmware request and wait for it. See also `bindings::request_firmware`.
 75     pub fn request(name: &CStr, dev: &Device) -> Result<Self> {
 76         Self::request_internal(name, dev, FwFunc::request())
 77     }
 78 
 79     /// Send a request for an optional firmware module. See also
 80     /// `bindings::firmware_request_nowarn`.
 81     pub fn request_nowarn(name: &CStr, dev: &Device) -> Result<Self> {
 82         Self::request_internal(name, dev, FwFunc::request_nowarn())
 83     }
 84 
 85     fn as_raw(&self) -> *mut bindings::firmware {
 86         self.0.as_ptr()
 87     }
 88 
 89     /// Returns the size of the requested firmware in bytes.
 90     pub fn size(&self) -> usize {
 91         // SAFETY: `self.as_raw()` is valid by the type invariant.
 92         unsafe { (*self.as_raw()).size }
 93     }
 94 
 95     /// Returns the requested firmware as `&[u8]`.
 96     pub fn data(&self) -> &[u8] {
 97         // SAFETY: `self.as_raw()` is valid by the type invariant. Additionally,
 98         // `bindings::firmware` guarantees, if successfully requested, that
 99         // `bindings::firmware::data` has a size of `bindings::firmware::size` bytes.
100         unsafe { core::slice::from_raw_parts((*self.as_raw()).data, self.size()) }
101     }
102 }
103 
104 impl Drop for Firmware {
105     fn drop(&mut self) {
106         // SAFETY: `self.as_raw()` is valid by the type invariant.
107         unsafe { bindings::release_firmware(self.as_raw()) };
108     }
109 }
110 
111 // SAFETY: `Firmware` only holds a pointer to a C `struct firmware`, which is safe to be used from
112 // any thread.
113 unsafe impl Send for Firmware {}
114 
115 // SAFETY: `Firmware` only holds a pointer to a C `struct firmware`, references to which are safe to
116 // be used from any thread.
117 unsafe impl Sync for Firmware {}

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php