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 | |
| 17 | use crate::error::AvbIOError; |
| 18 | use crate::utils::is_not_null; |
| 19 | use core::ffi::{c_char, CStr}; |
| 20 | |
Alice Wang | 815461f | 2023-01-31 12:59:00 +0000 | [diff] [blame^] | 21 | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
Alice Wang | 8077a86 | 2023-01-18 16:06:37 +0000 | [diff] [blame] | 22 | pub(crate) enum PartitionName { |
Alice Wang | 815461f | 2023-01-31 12:59:00 +0000 | [diff] [blame^] | 23 | /// The default `PartitionName` is needed to build the default `HashDescriptor`. |
| 24 | #[default] |
Alice Wang | 8077a86 | 2023-01-18 16:06:37 +0000 | [diff] [blame] | 25 | Kernel, |
| 26 | InitrdNormal, |
| 27 | InitrdDebug, |
| 28 | } |
| 29 | |
| 30 | impl PartitionName { |
Alice Wang | f275286 | 2023-01-18 11:51:25 +0000 | [diff] [blame] | 31 | pub(crate) const NUM_OF_KNOWN_PARTITIONS: usize = 3; |
| 32 | |
Alice Wang | 8077a86 | 2023-01-18 16:06:37 +0000 | [diff] [blame] | 33 | const KERNEL_PARTITION_NAME: &[u8] = b"boot\0"; |
| 34 | const INITRD_NORMAL_PARTITION_NAME: &[u8] = b"initrd_normal\0"; |
| 35 | const INITRD_DEBUG_PARTITION_NAME: &[u8] = b"initrd_debug\0"; |
| 36 | |
| 37 | pub(crate) fn as_cstr(&self) -> &CStr { |
| 38 | CStr::from_bytes_with_nul(self.as_bytes()).unwrap() |
| 39 | } |
| 40 | |
| 41 | fn as_non_null_terminated_bytes(&self) -> &[u8] { |
| 42 | let partition_name = self.as_bytes(); |
| 43 | &partition_name[..partition_name.len() - 1] |
| 44 | } |
| 45 | |
| 46 | fn as_bytes(&self) -> &[u8] { |
| 47 | match self { |
| 48 | Self::Kernel => Self::KERNEL_PARTITION_NAME, |
| 49 | Self::InitrdNormal => Self::INITRD_NORMAL_PARTITION_NAME, |
| 50 | Self::InitrdDebug => Self::INITRD_DEBUG_PARTITION_NAME, |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | impl TryFrom<*const c_char> for PartitionName { |
| 56 | type Error = AvbIOError; |
| 57 | |
| 58 | fn try_from(partition_name: *const c_char) -> Result<Self, Self::Error> { |
| 59 | is_not_null(partition_name)?; |
| 60 | // SAFETY: It is safe as the raw pointer `partition_name` is a nonnull pointer. |
| 61 | let partition_name = unsafe { CStr::from_ptr(partition_name) }; |
| 62 | partition_name.try_into() |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | impl TryFrom<&CStr> for PartitionName { |
| 67 | type Error = AvbIOError; |
| 68 | |
| 69 | fn try_from(partition_name: &CStr) -> Result<Self, Self::Error> { |
| 70 | match partition_name.to_bytes_with_nul() { |
| 71 | Self::KERNEL_PARTITION_NAME => Ok(Self::Kernel), |
| 72 | Self::INITRD_NORMAL_PARTITION_NAME => Ok(Self::InitrdNormal), |
| 73 | Self::INITRD_DEBUG_PARTITION_NAME => Ok(Self::InitrdDebug), |
| 74 | _ => Err(AvbIOError::NoSuchPartition), |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl TryFrom<&[u8]> for PartitionName { |
| 80 | type Error = AvbIOError; |
| 81 | |
| 82 | fn try_from(non_null_terminated_name: &[u8]) -> Result<Self, Self::Error> { |
| 83 | match non_null_terminated_name { |
| 84 | x if x == Self::Kernel.as_non_null_terminated_bytes() => Ok(Self::Kernel), |
| 85 | x if x == Self::InitrdNormal.as_non_null_terminated_bytes() => Ok(Self::InitrdNormal), |
| 86 | x if x == Self::InitrdDebug.as_non_null_terminated_bytes() => Ok(Self::InitrdDebug), |
| 87 | _ => Err(AvbIOError::NoSuchPartition), |
| 88 | } |
| 89 | } |
| 90 | } |