• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
22 pub(crate) enum PartitionName {
23     /// The default `PartitionName` is needed to build the default `HashDescriptor`.
24     #[default]
25     Kernel,
26     InitrdNormal,
27     InitrdDebug,
28 }
29 
30 impl PartitionName {
31     pub(crate) const NUM_OF_KNOWN_PARTITIONS: usize = 3;
32 
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 
as_cstr(&self) -> &CStr37     pub(crate) fn as_cstr(&self) -> &CStr {
38         CStr::from_bytes_with_nul(self.as_bytes()).unwrap()
39     }
40 
as_non_null_terminated_bytes(&self) -> &[u8]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 
as_bytes(&self) -> &[u8]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 
try_from(partition_name: *const c_char) -> Result<Self, Self::Error>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 
try_from(partition_name: &CStr) -> Result<Self, Self::Error>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 
try_from(non_null_terminated_name: &[u8]) -> Result<Self, Self::Error>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 }
91