blob: 82d89f8b1cb41862c3fe676e83e8387c3d9d2d8c [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
28impl PartitionName {
29 const KERNEL_PARTITION_NAME: &[u8] = b"boot\0";
30 const INITRD_NORMAL_PARTITION_NAME: &[u8] = b"initrd_normal\0";
31 const INITRD_DEBUG_PARTITION_NAME: &[u8] = b"initrd_debug\0";
32
33 pub(crate) fn as_cstr(&self) -> &CStr {
34 CStr::from_bytes_with_nul(self.as_bytes()).unwrap()
35 }
36
37 fn as_non_null_terminated_bytes(&self) -> &[u8] {
38 let partition_name = self.as_bytes();
39 &partition_name[..partition_name.len() - 1]
40 }
41
42 fn as_bytes(&self) -> &[u8] {
43 match self {
44 Self::Kernel => Self::KERNEL_PARTITION_NAME,
45 Self::InitrdNormal => Self::INITRD_NORMAL_PARTITION_NAME,
46 Self::InitrdDebug => Self::INITRD_DEBUG_PARTITION_NAME,
47 }
48 }
49}
50
51impl TryFrom<*const c_char> for PartitionName {
52 type Error = AvbIOError;
53
54 fn try_from(partition_name: *const c_char) -> Result<Self, Self::Error> {
55 is_not_null(partition_name)?;
56 // SAFETY: It is safe as the raw pointer `partition_name` is a nonnull pointer.
57 let partition_name = unsafe { CStr::from_ptr(partition_name) };
58 partition_name.try_into()
59 }
60}
61
62impl TryFrom<&CStr> for PartitionName {
63 type Error = AvbIOError;
64
65 fn try_from(partition_name: &CStr) -> Result<Self, Self::Error> {
66 match partition_name.to_bytes_with_nul() {
67 Self::KERNEL_PARTITION_NAME => Ok(Self::Kernel),
68 Self::INITRD_NORMAL_PARTITION_NAME => Ok(Self::InitrdNormal),
69 Self::INITRD_DEBUG_PARTITION_NAME => Ok(Self::InitrdDebug),
70 _ => Err(AvbIOError::NoSuchPartition),
71 }
72 }
73}
74
75impl TryFrom<&[u8]> for PartitionName {
76 type Error = AvbIOError;
77
78 fn try_from(non_null_terminated_name: &[u8]) -> Result<Self, Self::Error> {
79 match non_null_terminated_name {
80 x if x == Self::Kernel.as_non_null_terminated_bytes() => Ok(Self::Kernel),
81 x if x == Self::InitrdNormal.as_non_null_terminated_bytes() => Ok(Self::InitrdNormal),
82 x if x == Self::InitrdDebug.as_non_null_terminated_bytes() => Ok(Self::InitrdDebug),
83 _ => Err(AvbIOError::NoSuchPartition),
84 }
85 }
86}