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, ffi, 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(
16        *mut *const bindings::firmware,
17        *const ffi::c_char,
18        *mut bindings::device,
19    ) -> i32,
20);
21
22impl FwFunc {
23    fn request() -> Self {
24        Self(bindings::request_firmware)
25    }
26
27    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/// ```
59pub struct Firmware(NonNull<bindings::firmware>);
60
61impl Firmware {
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        let pfw: *mut *const bindings::firmware = pfw.cast();
66
67        // SAFETY: `pfw` is a valid pointer to a NULL initialized `bindings::firmware` pointer.
68        // `name` and `dev` are valid as by their type invariants.
69        let ret = unsafe { func.0(pfw, name.as_char_ptr(), dev.as_raw()) };
70        if ret != 0 {
71            return Err(Error::from_errno(ret));
72        }
73
74        // SAFETY: `func` not bailing out with a non-zero error code, guarantees that `fw` is a
75        // valid pointer to `bindings::firmware`.
76        Ok(Firmware(unsafe { NonNull::new_unchecked(fw) }))
77    }
78
79    /// Send a firmware request and wait for it. See also `bindings::request_firmware`.
80    pub fn request(name: &CStr, dev: &Device) -> Result<Self> {
81        Self::request_internal(name, dev, FwFunc::request())
82    }
83
84    /// Send a request for an optional firmware module. See also
85    /// `bindings::firmware_request_nowarn`.
86    pub fn request_nowarn(name: &CStr, dev: &Device) -> Result<Self> {
87        Self::request_internal(name, dev, FwFunc::request_nowarn())
88    }
89
90    fn as_raw(&self) -> *mut bindings::firmware {
91        self.0.as_ptr()
92    }
93
94    /// Returns the size of the requested firmware in bytes.
95    pub fn size(&self) -> usize {
96        // SAFETY: `self.as_raw()` is valid by the type invariant.
97        unsafe { (*self.as_raw()).size }
98    }
99
100    /// Returns the requested firmware as `&[u8]`.
101    pub fn data(&self) -> &[u8] {
102        // SAFETY: `self.as_raw()` is valid by the type invariant. Additionally,
103        // `bindings::firmware` guarantees, if successfully requested, that
104        // `bindings::firmware::data` has a size of `bindings::firmware::size` bytes.
105        unsafe { core::slice::from_raw_parts((*self.as_raw()).data, self.size()) }
106    }
107}
108
109impl Drop for Firmware {
110    fn drop(&mut self) {
111        // SAFETY: `self.as_raw()` is valid by the type invariant.
112        unsafe { bindings::release_firmware(self.as_raw()) };
113    }
114}
115
116// SAFETY: `Firmware` only holds a pointer to a C `struct firmware`, which is safe to be used from
117// any thread.
118unsafe impl Send for Firmware {}
119
120// SAFETY: `Firmware` only holds a pointer to a C `struct firmware`, references to which are safe to
121// be used from any thread.
122unsafe impl Sync for Firmware {}
123
124/// Create firmware .modinfo entries.
125///
126/// This macro is the counterpart of the C macro `MODULE_FIRMWARE()`, but instead of taking a
127/// simple string literals, which is already covered by the `firmware` field of
128/// [`crate::prelude::module!`], it allows the caller to pass a builder type, based on the
129/// [`ModInfoBuilder`], which can create the firmware modinfo strings in a more flexible way.
130///
131/// Drivers should extend the [`ModInfoBuilder`] with their own driver specific builder type.
132///
133/// The `builder` argument must be a type which implements the following function.
134///
135/// `const fn create(module_name: &'static CStr) -> ModInfoBuilder`
136///
137/// `create` should pass the `module_name` to the [`ModInfoBuilder`] and, with the help of
138/// it construct the corresponding firmware modinfo.
139///
140/// Typically, such contracts would be enforced by a trait, however traits do not (yet) support
141/// const functions.
142///
143/// # Examples
144///
145/// ```
146/// # mod module_firmware_test {
147/// # use kernel::firmware;
148/// # use kernel::prelude::*;
149/// #
150/// # struct MyModule;
151/// #
152/// # impl kernel::Module for MyModule {
153/// #     fn init(_module: &'static ThisModule) -> Result<Self> {
154/// #         Ok(Self)
155/// #     }
156/// # }
157/// #
158/// #
159/// struct Builder<const N: usize>;
160///
161/// impl<const N: usize> Builder<N> {
162///     const DIR: &'static str = "vendor/chip/";
163///     const FILES: [&'static str; 3] = [ "foo", "bar", "baz" ];
164///
165///     const fn create(module_name: &'static kernel::str::CStr) -> firmware::ModInfoBuilder<N> {
166///         let mut builder = firmware::ModInfoBuilder::new(module_name);
167///
168///         let mut i = 0;
169///         while i < Self::FILES.len() {
170///             builder = builder.new_entry()
171///                 .push(Self::DIR)
172///                 .push(Self::FILES[i])
173///                 .push(".bin");
174///
175///                 i += 1;
176///         }
177///
178///         builder
179///      }
180/// }
181///
182/// module! {
183///    type: MyModule,
184///    name: "module_firmware_test",
185///    authors: ["Rust for Linux"],
186///    description: "module_firmware! test module",
187///    license: "GPL",
188/// }
189///
190/// kernel::module_firmware!(Builder);
191/// # }
192/// ```
193#[macro_export]
194macro_rules! module_firmware {
195    // The argument is the builder type without the const generic, since it's deferred from within
196    // this macro. Hence, we can neither use `expr` nor `ty`.
197    ($($builder:tt)*) => {
198        const _: () = {
199            const __MODULE_FIRMWARE_PREFIX: &'static $crate::str::CStr = if cfg!(MODULE) {
200                $crate::c_str!("")
201            } else {
202                <LocalModule as $crate::ModuleMetadata>::NAME
203            };
204
205            #[link_section = ".modinfo"]
206            #[used]
207            static __MODULE_FIRMWARE: [u8; $($builder)*::create(__MODULE_FIRMWARE_PREFIX)
208                .build_length()] = $($builder)*::create(__MODULE_FIRMWARE_PREFIX).build();
209        };
210    };
211}
212
213/// Builder for firmware module info.
214///
215/// [`ModInfoBuilder`] is a helper component to flexibly compose firmware paths strings for the
216/// .modinfo section in const context.
217///
218/// Therefore the [`ModInfoBuilder`] provides the methods [`ModInfoBuilder::new_entry`] and
219/// [`ModInfoBuilder::push`], where the latter is used to push path components and the former to
220/// mark the beginning of a new path string.
221///
222/// [`ModInfoBuilder`] is meant to be used in combination with [`kernel::module_firmware!`].
223///
224/// The const generic `N` as well as the `module_name` parameter of [`ModInfoBuilder::new`] is an
225/// internal implementation detail and supplied through the above macro.
226pub struct ModInfoBuilder<const N: usize> {
227    buf: [u8; N],
228    n: usize,
229    module_name: &'static CStr,
230}
231
232impl<const N: usize> ModInfoBuilder<N> {
233    /// Create an empty builder instance.
234    pub const fn new(module_name: &'static CStr) -> Self {
235        Self {
236            buf: [0; N],
237            n: 0,
238            module_name,
239        }
240    }
241
242    const fn push_internal(mut self, bytes: &[u8]) -> Self {
243        let mut j = 0;
244
245        if N == 0 {
246            self.n += bytes.len();
247            return self;
248        }
249
250        while j < bytes.len() {
251            if self.n < N {
252                self.buf[self.n] = bytes[j];
253            }
254            self.n += 1;
255            j += 1;
256        }
257        self
258    }
259
260    /// Push an additional path component.
261    ///
262    /// Append path components to the [`ModInfoBuilder`] instance. Paths need to be separated
263    /// with [`ModInfoBuilder::new_entry`].
264    ///
265    /// # Examples
266    ///
267    /// ```
268    /// use kernel::firmware::ModInfoBuilder;
269    ///
270    /// # const DIR: &str = "vendor/chip/";
271    /// # const fn no_run<const N: usize>(builder: ModInfoBuilder<N>) {
272    /// let builder = builder.new_entry()
273    ///     .push(DIR)
274    ///     .push("foo.bin")
275    ///     .new_entry()
276    ///     .push(DIR)
277    ///     .push("bar.bin");
278    /// # }
279    /// ```
280    pub const fn push(self, s: &str) -> Self {
281        // Check whether there has been an initial call to `next_entry()`.
282        if N != 0 && self.n == 0 {
283            crate::build_error!("Must call next_entry() before push().");
284        }
285
286        self.push_internal(s.as_bytes())
287    }
288
289    const fn push_module_name(self) -> Self {
290        let mut this = self;
291        let module_name = this.module_name;
292
293        if !this.module_name.is_empty() {
294            this = this.push_internal(module_name.as_bytes_with_nul());
295
296            if N != 0 {
297                // Re-use the space taken by the NULL terminator and swap it with the '.' separator.
298                this.buf[this.n - 1] = b'.';
299            }
300        }
301
302        this
303    }
304
305    /// Prepare the [`ModInfoBuilder`] for the next entry.
306    ///
307    /// This method acts as a separator between module firmware path entries.
308    ///
309    /// Must be called before constructing a new entry with subsequent calls to
310    /// [`ModInfoBuilder::push`].
311    ///
312    /// See [`ModInfoBuilder::push`] for an example.
313    pub const fn new_entry(self) -> Self {
314        self.push_internal(b"\0")
315            .push_module_name()
316            .push_internal(b"firmware=")
317    }
318
319    /// Build the byte array.
320    pub const fn build(self) -> [u8; N] {
321        // Add the final NULL terminator.
322        let this = self.push_internal(b"\0");
323
324        if this.n == N {
325            this.buf
326        } else {
327            crate::build_error!("Length mismatch.");
328        }
329    }
330}
331
332impl ModInfoBuilder<0> {
333    /// Return the length of the byte array to build.
334    pub const fn build_length(self) -> usize {
335        // Compensate for the NULL terminator added by `build`.
336        self.n + 1
337    }
338}