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 for defining Kibibyte-sized constants 24 #[macro_export] 25 macro_rules! KiB ( 26 ($x:expr) => { 27 $x*1024 28 } 29 ); 30 const_assert_eq!(KiB!(1), 1024); 31 const_assert_eq!(KiB!(5), 5 * 1024); 32 33 /// Macro for defining Mebibyte-sized constants 34 #[macro_export] 35 macro_rules! MiB ( 36 ($x:expr) => { 37 $x*KiB!(1024) 38 } 39 ); 40 const_assert_eq!(MiB!(1), 1024 * 1024); 41 const_assert_eq!(MiB!(5), 5 * 1024 * 1024); 42 43 pub use KiB; 44 pub use MiB; 45 46 /// Kernel image alignment requirement. 47 pub const KERNEL_ALIGNMENT: usize = MiB!(2); 48 49 /// Zircon Kernel image alignment requirement. 50 pub const ZIRCON_KERNEL_ALIGNMENT: usize = KiB!(64); 51 52 /// FDT image alignment requirement. 53 pub const FDT_ALIGNMENT: usize = 8; 54 55 /// Expected max size for BootCmd zbi item. 56 pub const BOOTCMD_SIZE: usize = KiB!(16); 57 58 /// Page size 59 pub const PAGE_SIZE: usize = KiB!(4); 60 61 /// Image names list. 62 /// Used for identifying what buffer size/alignment is necessary. 63 #[derive(Debug, PartialEq, Clone)] 64 pub enum ImageName { 65 /// ZBI for Zircon kernel 66 ZbiZircon, 67 /// ZBI items 68 ZbiItems, 69 /// Boot 70 Boot, 71 /// FDT 72 Fdt, 73 } 74 75 impl ImageName { 76 /// Get alignment required for the [ImageName] alignment(&self) -> usize77 pub fn alignment(&self) -> usize { 78 match self { 79 Self::ZbiZircon => ZIRCON_KERNEL_ALIGNMENT, 80 Self::ZbiItems => PAGE_SIZE, 81 Self::Boot => KERNEL_ALIGNMENT, 82 Self::Fdt => FDT_ALIGNMENT, 83 } 84 } 85 } 86 87 impl Display for ImageName { fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result88 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { 89 let str = match self { 90 ImageName::ZbiZircon => "zbi_zircon", 91 ImageName::ZbiItems => "zbi_items", 92 ImageName::Boot => "boot", 93 ImageName::Fdt => "fdt", 94 }; 95 write!(f, "{str}") 96 } 97 } 98 99 impl TryFrom<&str> for ImageName { 100 type Error = Error; 101 try_from(value: &str) -> Result<Self, Self::Error>102 fn try_from(value: &str) -> Result<Self, Self::Error> { 103 Ok(match value { 104 "zbi_zircon" => ImageName::ZbiZircon, 105 "zbi_items" => ImageName::ZbiItems, 106 "boot" => ImageName::Boot, 107 "fdt" => ImageName::Fdt, 108 _ => return Err(Error::InvalidInput), 109 }) 110 } 111 } 112