kernel/
firmware.rs

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
7use crate::{bindings, device::Device, error::Error, error::Result, str::CStr};
8use 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`.
14struct FwFunc(
15    unsafe extern "C" fn(*mut *const bindings::firmware, *const u8, *mut bindings::device) -> i32,
16);
17
18impl 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::get_device(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/// ```
55pub struct Firmware(NonNull<bindings::firmware>);
56
57impl 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
104impl 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.
113unsafe 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.
117unsafe impl Sync for Firmware {}
118
119/// Create firmware .modinfo entries.
120///
121/// This macro is the counterpart of the C macro `MODULE_FIRMWARE()`, but instead of taking a
122/// simple string literals, which is already covered by the `firmware` field of
123/// [`crate::prelude::module!`], it allows the caller to pass a builder type, based on the
124/// [`ModInfoBuilder`], which can create the firmware modinfo strings in a more flexible way.
125///
126/// Drivers should extend the [`ModInfoBuilder`] with their own driver specific builder type.
127///
128/// The `builder` argument must be a type which implements the following function.
129///
130/// `const fn create(module_name: &'static CStr) -> ModInfoBuilder`
131///
132/// `create` should pass the `module_name` to the [`ModInfoBuilder`] and, with the help of
133/// it construct the corresponding firmware modinfo.
134///
135/// Typically, such contracts would be enforced by a trait, however traits do not (yet) support
136/// const functions.
137///
138/// # Example
139///
140/// ```
141/// # mod module_firmware_test {
142/// # use kernel::firmware;
143/// # use kernel::prelude::*;
144/// #
145/// # struct MyModule;
146/// #
147/// # impl kernel::Module for MyModule {
148/// #     fn init(_module: &'static ThisModule) -> Result<Self> {
149/// #         Ok(Self)
150/// #     }
151/// # }
152/// #
153/// #
154/// struct Builder<const N: usize>;
155///
156/// impl<const N: usize> Builder<N> {
157///     const DIR: &'static str = "vendor/chip/";
158///     const FILES: [&'static str; 3] = [ "foo", "bar", "baz" ];
159///
160///     const fn create(module_name: &'static kernel::str::CStr) -> firmware::ModInfoBuilder<N> {
161///         let mut builder = firmware::ModInfoBuilder::new(module_name);
162///
163///         let mut i = 0;
164///         while i < Self::FILES.len() {
165///             builder = builder.new_entry()
166///                 .push(Self::DIR)
167///                 .push(Self::FILES[i])
168///                 .push(".bin");
169///
170///                 i += 1;
171///         }
172///
173///         builder
174///      }
175/// }
176///
177/// module! {
178///    type: MyModule,
179///    name: "module_firmware_test",
180///    author: "Rust for Linux",
181///    description: "module_firmware! test module",
182///    license: "GPL",
183/// }
184///
185/// kernel::module_firmware!(Builder);
186/// # }
187/// ```
188#[macro_export]
189macro_rules! module_firmware {
190    // The argument is the builder type without the const generic, since it's deferred from within
191    // this macro. Hence, we can neither use `expr` nor `ty`.
192    ($($builder:tt)*) => {
193        const _: () = {
194            const __MODULE_FIRMWARE_PREFIX: &'static $crate::str::CStr = if cfg!(MODULE) {
195                $crate::c_str!("")
196            } else {
197                <LocalModule as $crate::ModuleMetadata>::NAME
198            };
199
200            #[link_section = ".modinfo"]
201            #[used]
202            static __MODULE_FIRMWARE: [u8; $($builder)*::create(__MODULE_FIRMWARE_PREFIX)
203                .build_length()] = $($builder)*::create(__MODULE_FIRMWARE_PREFIX).build();
204        };
205    };
206}
207
208/// Builder for firmware module info.
209///
210/// [`ModInfoBuilder`] is a helper component to flexibly compose firmware paths strings for the
211/// .modinfo section in const context.
212///
213/// Therefore the [`ModInfoBuilder`] provides the methods [`ModInfoBuilder::new_entry`] and
214/// [`ModInfoBuilder::push`], where the latter is used to push path components and the former to
215/// mark the beginning of a new path string.
216///
217/// [`ModInfoBuilder`] is meant to be used in combination with [`kernel::module_firmware!`].
218///
219/// The const generic `N` as well as the `module_name` parameter of [`ModInfoBuilder::new`] is an
220/// internal implementation detail and supplied through the above macro.
221pub struct ModInfoBuilder<const N: usize> {
222    buf: [u8; N],
223    n: usize,
224    module_name: &'static CStr,
225}
226
227impl<const N: usize> ModInfoBuilder<N> {
228    /// Create an empty builder instance.
229    pub const fn new(module_name: &'static CStr) -> Self {
230        Self {
231            buf: [0; N],
232            n: 0,
233            module_name,
234        }
235    }
236
237    const fn push_internal(mut self, bytes: &[u8]) -> Self {
238        let mut j = 0;
239
240        if N == 0 {
241            self.n += bytes.len();
242            return self;
243        }
244
245        while j < bytes.len() {
246            if self.n < N {
247                self.buf[self.n] = bytes[j];
248            }
249            self.n += 1;
250            j += 1;
251        }
252        self
253    }
254
255    /// Push an additional path component.
256    ///
257    /// Append path components to the [`ModInfoBuilder`] instance. Paths need to be separated
258    /// with [`ModInfoBuilder::new_entry`].
259    ///
260    /// # Example
261    ///
262    /// ```
263    /// use kernel::firmware::ModInfoBuilder;
264    ///
265    /// # const DIR: &str = "vendor/chip/";
266    /// # const fn no_run<const N: usize>(builder: ModInfoBuilder<N>) {
267    /// let builder = builder.new_entry()
268    ///     .push(DIR)
269    ///     .push("foo.bin")
270    ///     .new_entry()
271    ///     .push(DIR)
272    ///     .push("bar.bin");
273    /// # }
274    /// ```
275    pub const fn push(self, s: &str) -> Self {
276        // Check whether there has been an initial call to `next_entry()`.
277        if N != 0 && self.n == 0 {
278            crate::build_error!("Must call next_entry() before push().");
279        }
280
281        self.push_internal(s.as_bytes())
282    }
283
284    const fn push_module_name(self) -> Self {
285        let mut this = self;
286        let module_name = this.module_name;
287
288        if !this.module_name.is_empty() {
289            this = this.push_internal(module_name.as_bytes_with_nul());
290
291            if N != 0 {
292                // Re-use the space taken by the NULL terminator and swap it with the '.' separator.
293                this.buf[this.n - 1] = b'.';
294            }
295        }
296
297        this
298    }
299
300    /// Prepare the [`ModInfoBuilder`] for the next entry.
301    ///
302    /// This method acts as a separator between module firmware path entries.
303    ///
304    /// Must be called before constructing a new entry with subsequent calls to
305    /// [`ModInfoBuilder::push`].
306    ///
307    /// See [`ModInfoBuilder::push`] for an example.
308    pub const fn new_entry(self) -> Self {
309        self.push_internal(b"\0")
310            .push_module_name()
311            .push_internal(b"firmware=")
312    }
313
314    /// Build the byte array.
315    pub const fn build(self) -> [u8; N] {
316        // Add the final NULL terminator.
317        let this = self.push_internal(b"\0");
318
319        if this.n == N {
320            this.buf
321        } else {
322            crate::build_error!("Length mismatch.");
323        }
324    }
325}
326
327impl ModInfoBuilder<0> {
328    /// Return the length of the byte array to build.
329    pub const fn build_length(self) -> usize {
330        // Compensate for the NULL terminator added by `build`.
331        self.n + 1
332    }
333}