blob: 02a78c6b51df853e9e614b08a15db81d0c817eb3 [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 {
Chris Wailes98f18232023-12-07 12:04:21 -080030 const KERNEL_PARTITION_NAME: &'static [u8] = b"boot\0";
31 const INITRD_NORMAL_PARTITION_NAME: &'static [u8] = b"initrd_normal\0";
32 const INITRD_DEBUG_PARTITION_NAME: &'static [u8] = b"initrd_debug\0";
Alice Wang8077a862023-01-18 16:06:37 +000033
34 pub(crate) fn as_cstr(&self) -> &CStr {
35 CStr::from_bytes_with_nul(self.as_bytes()).unwrap()
36 }
37
38 fn as_non_null_terminated_bytes(&self) -> &[u8] {
39 let partition_name = self.as_bytes();
40 &partition_name[..partition_name.len() - 1]
41 }
42
43 fn as_bytes(&self) -> &[u8] {
44 match self {
45 Self::Kernel => Self::KERNEL_PARTITION_NAME,
46 Self::InitrdNormal => Self::INITRD_NORMAL_PARTITION_NAME,
47 Self::InitrdDebug => Self::INITRD_DEBUG_PARTITION_NAME,
48 }
49 }
50}
51
Alice Wang8077a862023-01-18 16:06:37 +000052impl TryFrom<&CStr> for PartitionName {
David Pursellb59bcc42023-11-10 16:59:19 -080053 type Error = IoError;
Alice Wang8077a862023-01-18 16:06:37 +000054
55 fn try_from(partition_name: &CStr) -> Result<Self, Self::Error> {
56 match partition_name.to_bytes_with_nul() {
57 Self::KERNEL_PARTITION_NAME => Ok(Self::Kernel),
58 Self::INITRD_NORMAL_PARTITION_NAME => Ok(Self::InitrdNormal),
59 Self::INITRD_DEBUG_PARTITION_NAME => Ok(Self::InitrdDebug),
David Pursellb59bcc42023-11-10 16:59:19 -080060 _ => Err(IoError::NoSuchPartition),
Alice Wang8077a862023-01-18 16:06:37 +000061 }
62 }
63}
64
65impl TryFrom<&[u8]> for PartitionName {
David Pursellb59bcc42023-11-10 16:59:19 -080066 type Error = IoError;
Alice Wang8077a862023-01-18 16:06:37 +000067
68 fn try_from(non_null_terminated_name: &[u8]) -> Result<Self, Self::Error> {
69 match non_null_terminated_name {
70 x if x == Self::Kernel.as_non_null_terminated_bytes() => Ok(Self::Kernel),
71 x if x == Self::InitrdNormal.as_non_null_terminated_bytes() => Ok(Self::InitrdNormal),
72 x if x == Self::InitrdDebug.as_non_null_terminated_bytes() => Ok(Self::InitrdDebug),
David Pursellb59bcc42023-11-10 16:59:19 -080073 _ => Err(IoError::NoSuchPartition),
Alice Wang8077a862023-01-18 16:06:37 +000074 }
75 }
76}