blob: 636bfd327d6e50b87fcc0e423ceb0c7d7d23d6d1 [file] [log] [blame]
Alice Wang8077a862023-01-18 16:06:37 +00001// 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
17use crate::error::AvbIOError;
18use crate::utils::is_not_null;
19use core::ffi::{c_char, CStr};
20
Alice Wang815461f2023-01-31 12:59:00 +000021#[derive(Clone, Debug, Default, PartialEq, Eq)]
Alice Wang8077a862023-01-18 16:06:37 +000022pub(crate) enum PartitionName {
Alice Wang815461f2023-01-31 12:59:00 +000023 /// The default `PartitionName` is needed to build the default `HashDescriptor`.
24 #[default]
Alice Wang8077a862023-01-18 16:06:37 +000025 Kernel,
26 InitrdNormal,
27 InitrdDebug,
28}
29
30impl PartitionName {
Alice Wangf2752862023-01-18 11:51:25 +000031 pub(crate) const NUM_OF_KNOWN_PARTITIONS: usize = 3;
32
Alice Wang8077a862023-01-18 16:06:37 +000033 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
55impl 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
66impl 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
79impl 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}