1 // Copyright 2024, 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 //! This file provides common constants that are used in GBL 16 17 // TODO(b/380392958) Cleanup other used of the constants. Move them here as well. 18 19 use core::fmt::{Debug, Display, Formatter}; 20 use liberror::Error; 21 use static_assertions::const_assert_eq; 22 23 macro_rules! KiB ( 24 ($x:expr) => { 25 $x*1024 26 } 27 ); 28 const_assert_eq!(KiB!(1), 1024); 29 const_assert_eq!(KiB!(5), 5 * 1024); 30 31 macro_rules! MiB ( 32 ($x:expr) => { 33 $x*KiB!(1024) 34 } 35 ); 36 const_assert_eq!(MiB!(1), 1024 * 1024); 37 const_assert_eq!(MiB!(5), 5 * 1024 * 1024); 38 39 /// Kernel image alignment requirement. 40 pub const KERNEL_ALIGNMENT: usize = MiB!(2); 41 42 /// Zircon Kernel image alignment requirement. 43 pub const ZIRCON_KERNEL_ALIGNMENT: usize = KiB!(64); 44 45 /// FDT image alignment requirement. 46 pub const FDT_ALIGNMENT: usize = 8; 47 48 /// Expected max size for BootCmd zbi item. 49 pub const BOOTCMD_SIZE: usize = KiB!(16); 50 51 /// Page size 52 pub const PAGE_SIZE: usize = KiB!(4); 53 54 /// Image names list. 55 /// Used for identifying what buffer size/alignment is necessary. 56 #[derive(Debug, PartialEq, Clone)] 57 pub enum ImageName { 58 /// ZBI for Zircon kernel 59 ZbiZircon, 60 /// ZBI items 61 ZbiItems, 62 /// Boot 63 Boot, 64 /// FDT 65 Fdt, 66 } 67 68 impl ImageName { 69 /// Get alignment required for the [ImageName] alignment(&self) -> usize70 pub fn alignment(&self) -> usize { 71 match self { 72 Self::ZbiZircon => ZIRCON_KERNEL_ALIGNMENT, 73 Self::ZbiItems => PAGE_SIZE, 74 Self::Boot => KERNEL_ALIGNMENT, 75 Self::Fdt => FDT_ALIGNMENT, 76 } 77 } 78 } 79 80 impl Display for ImageName { fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result81 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { 82 let str = match self { 83 ImageName::ZbiZircon => "zbi_zircon", 84 ImageName::ZbiItems => "zbi_items", 85 ImageName::Boot => "boot", 86 ImageName::Fdt => "fdt", 87 }; 88 write!(f, "{str}") 89 } 90 } 91 92 impl TryFrom<&str> for ImageName { 93 type Error = Error; 94 try_from(value: &str) -> Result<Self, Self::Error>95 fn try_from(value: &str) -> Result<Self, Self::Error> { 96 Ok(match value { 97 "zbi_zircon" => ImageName::ZbiZircon, 98 "zbi_items" => ImageName::ZbiItems, 99 "boot" => ImageName::Boot, 100 "fdt" => ImageName::Fdt, 101 _ => return Err(Error::InvalidInput), 102 }) 103 } 104 } 105