Pierre-Clément Tosi | 20b6096 | 2022-10-17 13:35:27 +0100 | [diff] [blame^] | 1 | // Copyright 2022, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Support for the pvmfw configuration data format. |
| 16 | |
| 17 | use crate::helpers; |
| 18 | use core::fmt; |
| 19 | use core::mem; |
| 20 | use core::num::NonZeroUsize; |
| 21 | use core::ops; |
| 22 | use core::result; |
| 23 | |
| 24 | #[repr(C, packed)] |
| 25 | #[derive(Clone, Copy, Debug)] |
| 26 | struct Header { |
| 27 | magic: u32, |
| 28 | version: u32, |
| 29 | total_size: u32, |
| 30 | flags: u32, |
| 31 | entries: [HeaderEntry; Entry::COUNT], |
| 32 | } |
| 33 | |
| 34 | #[derive(Debug)] |
| 35 | pub enum Error { |
| 36 | /// Reserved region can't fit configuration header. |
| 37 | BufferTooSmall, |
| 38 | /// Header doesn't contain the expect magic value. |
| 39 | InvalidMagic, |
| 40 | /// Version of the header isn't supported. |
| 41 | UnsupportedVersion(u16, u16), |
| 42 | /// Header sets flags incorrectly or uses reserved flags. |
| 43 | InvalidFlags(u32), |
| 44 | /// Header describes configuration data that doesn't fit in the expected buffer. |
| 45 | InvalidSize(usize), |
| 46 | /// Header entry is invalid. |
| 47 | InvalidEntry(Entry), |
| 48 | } |
| 49 | |
| 50 | impl fmt::Display for Error { |
| 51 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 52 | match self { |
| 53 | Self::BufferTooSmall => write!(f, "Reserved region is smaller than config header"), |
| 54 | Self::InvalidMagic => write!(f, "Wrong magic number"), |
| 55 | Self::UnsupportedVersion(x, y) => write!(f, "Version {x}.{y} not supported"), |
| 56 | Self::InvalidFlags(v) => write!(f, "Flags value {v:#x} is incorrect or reserved"), |
| 57 | Self::InvalidSize(sz) => write!(f, "Total size ({sz:#x}) overflows reserved region"), |
| 58 | Self::InvalidEntry(e) => write!(f, "Entry {e:?} is invalid"), |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | pub type Result<T> = result::Result<T, Error>; |
| 64 | |
| 65 | impl Header { |
| 66 | const MAGIC: u32 = u32::from_ne_bytes(*b"pvmf"); |
| 67 | const PADDED_SIZE: usize = |
| 68 | helpers::unchecked_align_up(mem::size_of::<Self>(), mem::size_of::<u64>()); |
| 69 | |
| 70 | pub const fn version(major: u16, minor: u16) -> u32 { |
| 71 | ((major as u32) << 16) | (minor as u32) |
| 72 | } |
| 73 | |
| 74 | pub const fn version_tuple(&self) -> (u16, u16) { |
| 75 | ((self.version >> 16) as u16, self.version as u16) |
| 76 | } |
| 77 | |
| 78 | pub fn total_size(&self) -> usize { |
| 79 | self.total_size as usize |
| 80 | } |
| 81 | |
| 82 | pub fn body_size(&self) -> usize { |
| 83 | self.total_size() - Self::PADDED_SIZE |
| 84 | } |
| 85 | |
| 86 | fn get(&self, entry: Entry) -> HeaderEntry { |
| 87 | self.entries[entry as usize] |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | #[derive(Clone, Copy, Debug)] |
| 92 | pub enum Entry { |
| 93 | Bcc = 0, |
| 94 | DebugPolicy = 1, |
| 95 | } |
| 96 | |
| 97 | impl Entry { |
| 98 | const COUNT: usize = 2; |
| 99 | } |
| 100 | |
| 101 | #[repr(packed)] |
| 102 | #[derive(Clone, Copy, Debug)] |
| 103 | struct HeaderEntry { |
| 104 | offset: u32, |
| 105 | size: u32, |
| 106 | } |
| 107 | |
| 108 | impl HeaderEntry { |
| 109 | pub fn is_empty(&self) -> bool { |
| 110 | self.offset() == 0 && self.size() == 0 |
| 111 | } |
| 112 | |
| 113 | pub fn fits_in(&self, max_size: usize) -> bool { |
| 114 | (Header::PADDED_SIZE..max_size).contains(&self.offset()) |
| 115 | && NonZeroUsize::new(self.size()) |
| 116 | .and_then(|s| s.checked_add(self.offset())) |
| 117 | .filter(|&x| x.get() <= max_size) |
| 118 | .is_some() |
| 119 | } |
| 120 | |
| 121 | pub fn as_body_range(&self) -> ops::Range<usize> { |
| 122 | let start = self.offset() - Header::PADDED_SIZE; |
| 123 | |
| 124 | start..(start + self.size()) |
| 125 | } |
| 126 | |
| 127 | pub fn offset(&self) -> usize { |
| 128 | self.offset as usize |
| 129 | } |
| 130 | |
| 131 | pub fn size(&self) -> usize { |
| 132 | self.size as usize |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | #[derive(Debug)] |
| 137 | pub struct Config<'a> { |
| 138 | header: &'a Header, |
| 139 | body: &'a mut [u8], |
| 140 | } |
| 141 | |
| 142 | impl<'a> Config<'a> { |
| 143 | /// Take ownership of a pvmfw configuration consisting of its header and following entries. |
| 144 | /// |
| 145 | /// SAFETY - 'data' should respect the alignment of Header. |
| 146 | pub unsafe fn new(data: &'a mut [u8]) -> Result<Self> { |
| 147 | let header = data.get(..Header::PADDED_SIZE).ok_or(Error::BufferTooSmall)?; |
| 148 | |
| 149 | let header = &*(header.as_ptr() as *const Header); |
| 150 | |
| 151 | if header.magic != Header::MAGIC { |
| 152 | return Err(Error::InvalidMagic); |
| 153 | } |
| 154 | |
| 155 | if header.version != Header::version(1, 0) { |
| 156 | let (major, minor) = header.version_tuple(); |
| 157 | return Err(Error::UnsupportedVersion(major, minor)); |
| 158 | } |
| 159 | |
| 160 | if header.flags != 0 { |
| 161 | return Err(Error::InvalidFlags(header.flags)); |
| 162 | } |
| 163 | |
| 164 | let total_size = header.total_size(); |
| 165 | |
| 166 | // BCC is a mandatory entry of the configuration data. |
| 167 | if !header.get(Entry::Bcc).fits_in(total_size) { |
| 168 | return Err(Error::InvalidEntry(Entry::Bcc)); |
| 169 | } |
| 170 | |
| 171 | // Debug policy is optional. |
| 172 | let dp = header.get(Entry::DebugPolicy); |
| 173 | if !dp.is_empty() && !dp.fits_in(total_size) { |
| 174 | return Err(Error::InvalidEntry(Entry::DebugPolicy)); |
| 175 | } |
| 176 | |
| 177 | let body = data |
| 178 | .get_mut(Header::PADDED_SIZE..) |
| 179 | .ok_or(Error::BufferTooSmall)? |
| 180 | .get_mut(..header.body_size()) |
| 181 | .ok_or(Error::InvalidSize(total_size))?; |
| 182 | |
| 183 | Ok(Self { header, body }) |
| 184 | } |
| 185 | |
| 186 | /// Get slice containing the platform BCC. |
| 187 | pub fn get_bcc_mut(&mut self) -> &mut [u8] { |
| 188 | &mut self.body[self.header.get(Entry::Bcc).as_body_range()] |
| 189 | } |
| 190 | |
| 191 | /// Get slice containing the platform debug policy. |
| 192 | pub fn get_debug_policy(&mut self) -> Option<&mut [u8]> { |
| 193 | let entry = self.header.get(Entry::DebugPolicy); |
| 194 | if entry.is_empty() { |
| 195 | None |
| 196 | } else { |
| 197 | Some(&mut self.body[entry.as_body_range()]) |
| 198 | } |
| 199 | } |
| 200 | } |