firmware.rs 4.1 KB

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