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