|
4 | 4 | //!
|
5 | 5 | //! C header: [`include/linux/of_*.h`](../../../../include/linux/of_*.h)
|
6 | 6 |
|
7 |
| -use alloc::boxed::Box; |
| 7 | +use crate::{bindings, c_types, str::CStr}; |
8 | 8 |
|
9 |
| -use crate::{ |
10 |
| - bindings, c_types, |
11 |
| - error::{Error, Result}, |
12 |
| - str::CStr, |
13 |
| - types::PointerWrapper, |
14 |
| -}; |
| 9 | +use core::ops::Deref; |
| 10 | +use core::ptr; |
15 | 11 |
|
16 |
| -use core::mem::transmute; |
17 |
| - |
18 |
| -type InnerTable = Box<[bindings::of_device_id; 2]>; |
19 |
| - |
20 |
| -/// Wraps a kernel Open Firmware / devicetree match table. |
| 12 | +/// A kernel Open Firmware / devicetree match table. |
21 | 13 | ///
|
22 |
| -/// Rust drivers may create this structure to match against devices |
23 |
| -/// described in the devicetree. |
24 |
| -/// |
25 |
| -/// The ['PointerWrapper'] trait provides conversion to/from a raw pointer, |
26 |
| -/// suitable to be assigned to a `bindings::device_driver::of_match_table`. |
| 14 | +/// Can only exist as an `&OfMatchTable` reference (akin to `&str` or |
| 15 | +/// `&Path` in Rust std). |
27 | 16 | ///
|
28 | 17 | /// # Invariants
|
29 | 18 | ///
|
30 |
| -/// The final array element is always filled with zeros (the default). |
31 |
| -pub struct OfMatchTable(InnerTable); |
| 19 | +/// The inner reference points to a sentinel-terminated C array. |
| 20 | +#[repr(transparent)] |
| 21 | +pub struct OfMatchTable(bindings::of_device_id); |
32 | 22 |
|
33 | 23 | impl OfMatchTable {
|
34 |
| - /// Creates a [`OfMatchTable`] from a single `compatible` string. |
35 |
| - pub fn new(compatible: &'static CStr) -> Result<Self> { |
36 |
| - let tbl = Box::try_new([ |
37 |
| - Self::new_of_device_id(compatible)?, |
38 |
| - bindings::of_device_id::default(), |
39 |
| - ])?; |
40 |
| - // INVARIANTS: we allocated an array with `default()` as its final |
41 |
| - // element, therefore that final element will be filled with zeros, |
42 |
| - // and the invariant above will hold. |
43 |
| - Ok(Self(tbl)) |
| 24 | + /// Returns the table as a reference to a static lifetime, sentinel-terminated C array. |
| 25 | + /// |
| 26 | + /// This is suitable to be coerced into the kernel's `of_match_table` field. |
| 27 | + pub fn as_ptr(&'static self) -> &'static bindings::of_device_id { |
| 28 | + // The inner reference points to a sentinel-terminated C array, as per |
| 29 | + // the type invariant. |
| 30 | + &self.0 |
44 | 31 | }
|
| 32 | +} |
45 | 33 |
|
46 |
| - fn new_of_device_id(compatible: &'static CStr) -> Result<bindings::of_device_id> { |
47 |
| - let mut buf = [0_u8; 128]; |
48 |
| - if compatible.len() > buf.len() { |
49 |
| - return Err(Error::EINVAL); |
50 |
| - } |
51 |
| - buf.get_mut(..compatible.len()) |
52 |
| - .ok_or(Error::EINVAL)? |
53 |
| - .copy_from_slice(compatible.as_bytes()); |
54 |
| - Ok(bindings::of_device_id { |
55 |
| - // SAFETY: re-interpretation from [u8] to [c_types::c_char] of same length is always safe. |
56 |
| - compatible: unsafe { transmute::<[u8; 128], [c_types::c_char; 128]>(buf) }, |
57 |
| - ..Default::default() |
58 |
| - }) |
59 |
| - } |
| 34 | +/// An Open Firmware Match Table that can be constructed at build time. |
| 35 | +/// |
| 36 | +/// # Invariants |
| 37 | +/// |
| 38 | +/// `sentinel` always contains zeroes. |
| 39 | +#[repr(C)] |
| 40 | +pub struct ConstOfMatchTable<const N: usize> { |
| 41 | + table: [bindings::of_device_id; N], |
| 42 | + sentinel: bindings::of_device_id, |
60 | 43 | }
|
61 | 44 |
|
62 |
| -impl PointerWrapper for OfMatchTable { |
63 |
| - type Borrowed = <InnerTable as PointerWrapper>::Borrowed; |
| 45 | +impl<const N: usize> ConstOfMatchTable<N> { |
| 46 | + /// Creates a new Open Firmware Match Table from a list of compatible strings. |
| 47 | + pub const fn new_const(compatibles: [&'static CStr; N]) -> Self { |
| 48 | + let mut table = [Self::zeroed_of_device_id(); N]; |
| 49 | + let mut i = 0; |
| 50 | + while i < N { |
| 51 | + table[i] = Self::new_of_device_id(compatibles[i]); |
| 52 | + i += 1; |
| 53 | + } |
| 54 | + Self { |
| 55 | + table, |
| 56 | + // INVARIANTS: we zero the sentinel here, and never change it |
| 57 | + // anywhere. Therefore it always contains zeroes. |
| 58 | + sentinel: Self::zeroed_of_device_id(), |
| 59 | + } |
| 60 | + } |
64 | 61 |
|
65 |
| - fn into_pointer(self) -> *const c_types::c_void { |
66 |
| - // Per the invariant above, the generated pointer points to an |
67 |
| - // array of `bindings::of_device_id`, where the final element is |
68 |
| - // filled with zeros (the sentinel). Therefore, it's suitable to |
69 |
| - // be assigned to `bindings::device_driver::of_match_table`. |
70 |
| - self.0.into_pointer() |
| 62 | + const fn zeroed_of_device_id() -> bindings::of_device_id { |
| 63 | + bindings::of_device_id { |
| 64 | + name: [0; 32], |
| 65 | + type_: [0; 32], |
| 66 | + compatible: [0; 128], |
| 67 | + data: ptr::null(), |
| 68 | + } |
71 | 69 | }
|
72 | 70 |
|
73 |
| - unsafe fn borrow(ptr: *const c_types::c_void) -> Self::Borrowed { |
74 |
| - // SAFETY: The safety requirements for this function are the same as the ones for |
75 |
| - // `InnerTable::borrow`. |
76 |
| - unsafe { InnerTable::borrow(ptr) } |
| 71 | + const fn new_of_device_id(compatible: &'static CStr) -> bindings::of_device_id { |
| 72 | + let mut id = Self::zeroed_of_device_id(); |
| 73 | + let compatible = compatible.as_bytes_with_nul(); |
| 74 | + let mut i = 0; |
| 75 | + while i < compatible.len() { |
| 76 | + // if `compatible` does not fit in `id.compatible`, an |
| 77 | + // "index out of bounds" build time exception will be triggered. |
| 78 | + id.compatible[i] = compatible[i] as c_types::c_char; |
| 79 | + i += 1; |
| 80 | + } |
| 81 | + id |
77 | 82 | }
|
| 83 | +} |
| 84 | + |
| 85 | +impl<const N: usize> Deref for ConstOfMatchTable<N> { |
| 86 | + type Target = OfMatchTable; |
78 | 87 |
|
79 |
| - unsafe fn from_pointer(p: *const c_types::c_void) -> Self { |
80 |
| - // SAFETY: The passed pointer comes from a previous call to [`InnerTable::into_pointer()`]. |
81 |
| - Self(unsafe { InnerTable::from_pointer(p) }) |
| 88 | + fn deref(&self) -> &OfMatchTable { |
| 89 | + // INVARIANTS: `head` points to a sentinel-terminated C array, |
| 90 | + // as per the `ConstOfMatchTable` type invariant, therefore |
| 91 | + // `&OfMatchTable`'s inner reference will point to a sentinel-terminated C array. |
| 92 | + let head = &self.table[0] as *const bindings::of_device_id as *const OfMatchTable; |
| 93 | + // SAFETY: The returned reference must remain valid for the lifetime of `self`. |
| 94 | + // The raw pointer `head` points to memory inside `self`. So the reference created |
| 95 | + // from this raw pointer has the same lifetime as `self`. |
| 96 | + // Therefore this reference remains valid for the lifetime of `self`, and |
| 97 | + // is safe to return. |
| 98 | + unsafe { &*head } |
82 | 99 | }
|
83 | 100 | }
|
0 commit comments