|
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; |
| 12 | +/// A kernel Open Firmware / devicetree match table. |
| 13 | +/// |
| 14 | +/// Can only exist as an `&OfMatchTable` reference (akin to `&str` or |
| 15 | +/// `&Path` in Rust std). |
| 16 | +#[repr(transparent)] |
| 17 | +pub struct OfMatchTable(bindings::of_device_id); |
17 | 18 |
|
18 |
| -type InnerTable = Box<[bindings::of_device_id; 2]>; |
| 19 | +impl<const N: usize> Deref for ConstOfMatchTable<N> { |
| 20 | + type Target = OfMatchTable; |
19 | 21 |
|
20 |
| -/// Wraps a kernel Open Firmware / devicetree match table. |
21 |
| -/// |
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`. |
27 |
| -/// |
28 |
| -/// # Invariants |
29 |
| -/// |
30 |
| -/// The final array element is always filled with zeros (the default). |
31 |
| -pub struct OfMatchTable(InnerTable); |
| 22 | + fn deref(&self) -> &OfMatchTable { |
| 23 | + let head = &self.table[0] as *const bindings::of_device_id as *const OfMatchTable; |
| 24 | + // The returned reference must remain valid for the lifetime of `self`. |
| 25 | + // The raw pointer `head` points to memory inside `self`. So the reference created |
| 26 | + // from this raw pointer has the same lifetime as `self`. |
| 27 | + // Therefore this reference is safe to return. |
| 28 | + unsafe { &*head } |
| 29 | + } |
| 30 | +} |
32 | 31 |
|
33 | 32 | 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)) |
| 33 | + /// Return the table as a static lifetime, sentinel-terminated C array. |
| 34 | + /// |
| 35 | + /// This is suitable to be assigned to the kernel's `of_match_table` field. |
| 36 | + pub fn as_ptr(&'static self) -> *const bindings::of_device_id { |
| 37 | + &self.0 |
44 | 38 | }
|
| 39 | +} |
45 | 40 |
|
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); |
| 41 | +/// An Open Firmware Match Table that can be constructed at build time. |
| 42 | +/// |
| 43 | +/// # Invariants |
| 44 | +/// |
| 45 | +/// `sentinel` always contains zeroes. |
| 46 | +#[repr(C)] |
| 47 | +pub struct ConstOfMatchTable<const N: usize> { |
| 48 | + table: [bindings::of_device_id; N], |
| 49 | + sentinel: bindings::of_device_id, |
| 50 | +} |
| 51 | + |
| 52 | +impl<const N: usize> ConstOfMatchTable<N> { |
| 53 | + /// Create a new Open Firmware Match Table from a list of compatible strings. |
| 54 | + pub const fn new_const(compatibles: [&'static CStr; N]) -> Self { |
| 55 | + let mut table = [Self::zeroed_of_device_id(); N]; |
| 56 | + let mut i = 0; |
| 57 | + loop { |
| 58 | + if i == N { |
| 59 | + break; |
| 60 | + } |
| 61 | + table[i] = Self::new_of_device_id(compatibles[i]); |
| 62 | + i += 1; |
| 63 | + } |
| 64 | + Self { |
| 65 | + table, |
| 66 | + // INVARIANT: we zero the sentinel here, and never change it |
| 67 | + // anwhere. Therefore it always contains zeroes. |
| 68 | + sentinel: Self::zeroed_of_device_id(), |
50 | 69 | }
|
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 | 70 | }
|
60 |
| -} |
61 | 71 |
|
62 |
| -impl PointerWrapper for OfMatchTable { |
63 |
| - fn into_pointer(self) -> *const c_types::c_void { |
64 |
| - // Per the invariant above, the generated pointer points to an |
65 |
| - // array of `bindings::of_device_id`, where the final element is |
66 |
| - // filled with zeros (the sentinel). Therefore, it's suitable to |
67 |
| - // be assigned to `bindings::device_driver::of_match_table`. |
68 |
| - self.0.into_pointer() |
| 72 | + const fn zeroed_of_device_id() -> bindings::of_device_id { |
| 73 | + bindings::of_device_id { |
| 74 | + name: [0; 32usize], |
| 75 | + type_: [0; 32usize], |
| 76 | + compatible: [0; 128usize], |
| 77 | + data: ptr::null(), |
| 78 | + } |
69 | 79 | }
|
70 | 80 |
|
71 |
| - unsafe fn from_pointer(p: *const c_types::c_void) -> Self { |
72 |
| - // SAFETY: The passed pointer comes from a previous call to [`InnerTable::into_pointer()`]. |
73 |
| - Self(unsafe { InnerTable::from_pointer(p) }) |
| 81 | + const fn new_of_device_id(compatible: &'static CStr) -> bindings::of_device_id { |
| 82 | + let mut id = Self::zeroed_of_device_id(); |
| 83 | + let compatible = compatible.as_bytes_with_nul(); |
| 84 | + let mut i = 0; |
| 85 | + loop { |
| 86 | + if i == compatible.len() { |
| 87 | + break; |
| 88 | + } |
| 89 | + // if `compatible` does not fit in `id.compatible`, an |
| 90 | + // "index out of bounds" build time exception will be triggered. |
| 91 | + id.compatible[i] = compatible[i] as c_types::c_char; |
| 92 | + i += 1; |
| 93 | + } |
| 94 | + id |
74 | 95 | }
|
75 | 96 | }
|
0 commit comments