blob: 3fe94798f0d8a9d9031bfd3b0c51f1f2d0834672 [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
David Pursellb59bcc42023-11-10 16:59:19 -080017use avb::IoError;
18use core::ffi::CStr;
Alice Wang8077a862023-01-18 16:06:37 +000019
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
Alice Wang8077a862023-01-18 16:06:37 +000054impl TryFrom<&CStr> for PartitionName {
David Pursellb59bcc42023-11-10 16:59:19 -080055 type Error = IoError;
Alice Wang8077a862023-01-18 16:06:37 +000056
57 fn try_from(partition_name: &CStr) -> Result<Self, Self::Error> {
58 match partition_name.to_bytes_with_nul() {
59 Self::KERNEL_PARTITION_NAME => Ok(Self::Kernel),
60 Self::INITRD_NORMAL_PARTITION_NAME => Ok(Self::InitrdNormal),
61 Self::INITRD_DEBUG_PARTITION_NAME => Ok(Self::InitrdDebug),
David Pursellb59bcc42023-11-10 16:59:19 -080062 _ => Err(IoError::NoSuchPartition),
Alice Wang8077a862023-01-18 16:06:37 +000063 }
64 }
65}
66
67impl TryFrom<&[u8]> for PartitionName {
David Pursellb59bcc42023-11-10 16:59:19 -080068 type Error = IoError;
Alice Wang8077a862023-01-18 16:06:37 +000069
70 fn try_from(non_null_terminated_name: &[u8]) -> Result<Self, Self::Error> {
71 match non_null_terminated_name {
72 x if x == Self::Kernel.as_non_null_terminated_bytes() => Ok(Self::Kernel),
73 x if x == Self::InitrdNormal.as_non_null_terminated_bytes() => Ok(Self::InitrdNormal),
74 x if x == Self::InitrdDebug.as_non_null_terminated_bytes() => Ok(Self::InitrdDebug),
David Pursellb59bcc42023-11-10 16:59:19 -080075 _ => Err(IoError::NoSuchPartition),
Alice Wang8077a862023-01-18 16:06:37 +000076 }
77 }
78}