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