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