blob: ca450c9f5f4e8ffb8d528b3c6e8a9d2f3aad5673 [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
Alice Wang8077a862023-01-18 16:06:37 +000017use crate::utils::is_not_null;
18use core::ffi::{c_char, CStr};
19
Alice Wang1f0add02023-01-23 16:22:53 +000020#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
Alice Wang8077a862023-01-18 16:06:37 +000021pub(crate) enum PartitionName {
Alice Wang815461f2023-01-31 12:59:00 +000022 /// The default `PartitionName` is needed to build the default `HashDescriptor`.
23 #[default]
Alice Wang8077a862023-01-18 16:06:37 +000024 Kernel,
25 InitrdNormal,
26 InitrdDebug,
27}
28
29impl PartitionName {
Alice Wangf2752862023-01-18 11:51:25 +000030 pub(crate) const NUM_OF_KNOWN_PARTITIONS: usize = 3;
31
Alice Wang8077a862023-01-18 16:06:37 +000032 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
54impl TryFrom<*const c_char> for PartitionName {
David Pursella7c727b2023-08-14 16:24:40 -070055 type Error = avb::IoError;
Alice Wang8077a862023-01-18 16:06:37 +000056
57 fn try_from(partition_name: *const c_char) -> Result<Self, Self::Error> {
58 is_not_null(partition_name)?;
59 // SAFETY: It is safe as the raw pointer `partition_name` is a nonnull pointer.
60 let partition_name = unsafe { CStr::from_ptr(partition_name) };
61 partition_name.try_into()
62 }
63}
64
65impl TryFrom<&CStr> for PartitionName {
David Pursella7c727b2023-08-14 16:24:40 -070066 type Error = avb::IoError;
Alice Wang8077a862023-01-18 16:06:37 +000067
68 fn try_from(partition_name: &CStr) -> Result<Self, Self::Error> {
69 match partition_name.to_bytes_with_nul() {
70 Self::KERNEL_PARTITION_NAME => Ok(Self::Kernel),
71 Self::INITRD_NORMAL_PARTITION_NAME => Ok(Self::InitrdNormal),
72 Self::INITRD_DEBUG_PARTITION_NAME => Ok(Self::InitrdDebug),
David Pursella7c727b2023-08-14 16:24:40 -070073 _ => Err(avb::IoError::NoSuchPartition),
Alice Wang8077a862023-01-18 16:06:37 +000074 }
75 }
76}
77
78impl TryFrom<&[u8]> for PartitionName {
David Pursella7c727b2023-08-14 16:24:40 -070079 type Error = avb::IoError;
Alice Wang8077a862023-01-18 16:06:37 +000080
81 fn try_from(non_null_terminated_name: &[u8]) -> Result<Self, Self::Error> {
82 match non_null_terminated_name {
83 x if x == Self::Kernel.as_non_null_terminated_bytes() => Ok(Self::Kernel),
84 x if x == Self::InitrdNormal.as_non_null_terminated_bytes() => Ok(Self::InitrdNormal),
85 x if x == Self::InitrdDebug.as_non_null_terminated_bytes() => Ok(Self::InitrdDebug),
David Pursella7c727b2023-08-14 16:24:40 -070086 _ => Err(avb::IoError::NoSuchPartition),
Alice Wang8077a862023-01-18 16:06:37 +000087 }
88 }
89}