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