Alice Wang | 8077a86 | 2023-01-18 16:06:37 +0000 | [diff] [blame] | 1 | // Copyright 2023, 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 | //! Struct and functions relating to well-known partition names. |
| 16 | |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame^] | 17 | use avb::IoError; |
| 18 | use core::ffi::CStr; |
Alice Wang | 8077a86 | 2023-01-18 16:06:37 +0000 | [diff] [blame] | 19 | |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 20 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
Alice Wang | 8077a86 | 2023-01-18 16:06:37 +0000 | [diff] [blame] | 21 | pub(crate) enum PartitionName { |
Alice Wang | 815461f | 2023-01-31 12:59:00 +0000 | [diff] [blame] | 22 | /// The default `PartitionName` is needed to build the default `HashDescriptor`. |
| 23 | #[default] |
Alice Wang | 8077a86 | 2023-01-18 16:06:37 +0000 | [diff] [blame] | 24 | Kernel, |
| 25 | InitrdNormal, |
| 26 | InitrdDebug, |
| 27 | } |
| 28 | |
| 29 | impl PartitionName { |
Alice Wang | f275286 | 2023-01-18 11:51:25 +0000 | [diff] [blame] | 30 | pub(crate) const NUM_OF_KNOWN_PARTITIONS: usize = 3; |
| 31 | |
Alice Wang | 8077a86 | 2023-01-18 16:06:37 +0000 | [diff] [blame] | 32 | const KERNEL_PARTITION_NAME: &[u8] = b"boot\0"; |
| 33 | const INITRD_NORMAL_PARTITION_NAME: &[u8] = b"initrd_normal\0"; |
| 34 | const INITRD_DEBUG_PARTITION_NAME: &[u8] = b"initrd_debug\0"; |
| 35 | |
| 36 | pub(crate) fn as_cstr(&self) -> &CStr { |
| 37 | CStr::from_bytes_with_nul(self.as_bytes()).unwrap() |
| 38 | } |
| 39 | |
| 40 | fn as_non_null_terminated_bytes(&self) -> &[u8] { |
| 41 | let partition_name = self.as_bytes(); |
| 42 | &partition_name[..partition_name.len() - 1] |
| 43 | } |
| 44 | |
| 45 | fn as_bytes(&self) -> &[u8] { |
| 46 | match self { |
| 47 | Self::Kernel => Self::KERNEL_PARTITION_NAME, |
| 48 | Self::InitrdNormal => Self::INITRD_NORMAL_PARTITION_NAME, |
| 49 | Self::InitrdDebug => Self::INITRD_DEBUG_PARTITION_NAME, |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
Alice Wang | 8077a86 | 2023-01-18 16:06:37 +0000 | [diff] [blame] | 54 | impl TryFrom<&CStr> for PartitionName { |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame^] | 55 | type Error = IoError; |
Alice Wang | 8077a86 | 2023-01-18 16:06:37 +0000 | [diff] [blame] | 56 | |
| 57 | fn try_from(partition_name: &CStr) -> Result<Self, Self::Error> { |
| 58 | match partition_name.to_bytes_with_nul() { |
| 59 | Self::KERNEL_PARTITION_NAME => Ok(Self::Kernel), |
| 60 | Self::INITRD_NORMAL_PARTITION_NAME => Ok(Self::InitrdNormal), |
| 61 | Self::INITRD_DEBUG_PARTITION_NAME => Ok(Self::InitrdDebug), |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame^] | 62 | _ => Err(IoError::NoSuchPartition), |
Alice Wang | 8077a86 | 2023-01-18 16:06:37 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | impl TryFrom<&[u8]> for PartitionName { |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame^] | 68 | type Error = IoError; |
Alice Wang | 8077a86 | 2023-01-18 16:06:37 +0000 | [diff] [blame] | 69 | |
| 70 | fn try_from(non_null_terminated_name: &[u8]) -> Result<Self, Self::Error> { |
| 71 | match non_null_terminated_name { |
| 72 | x if x == Self::Kernel.as_non_null_terminated_bytes() => Ok(Self::Kernel), |
| 73 | x if x == Self::InitrdNormal.as_non_null_terminated_bytes() => Ok(Self::InitrdNormal), |
| 74 | x if x == Self::InitrdDebug.as_non_null_terminated_bytes() => Ok(Self::InitrdDebug), |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame^] | 75 | _ => Err(IoError::NoSuchPartition), |
Alice Wang | 8077a86 | 2023-01-18 16:06:37 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | } |