1 // 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 //! This EFI application implements a demo for booting Android/Fuchsia from disk. See
16 //! bootable/libbootloader/gbl/README.md for how to run the demo. See comments of
17 //! `android_boot:android_boot_demo()` and `fuchsia_boot:fuchsia_boot_demo()` for
18 //! supported/unsupported features at the moment.
19
20 #![cfg_attr(not(test), no_std)]
21
22 // For the `vec!` macro
23 #[macro_use]
24 extern crate alloc;
25
26 mod efi_blocks;
27 mod error;
28 mod ops;
29 #[macro_use]
30 mod utils;
31
32 // Currently un-testable modules.
33 //
34 // The libefi API surface is large and complex; rather than trying to mock it all out at once, we
35 // will selectively enable modules for test as they become mockable.
36 #[cfg(not(test))]
37 mod android_boot;
38 #[cfg(not(test))]
39 mod fastboot;
40 #[cfg(not(test))]
41 mod fuchsia_boot;
42 #[cfg(not(test))]
43 mod net;
44
45 // In tests, map the `efi_mocks` module as `efi`. This allows other modules to `use crate::efi`
46 // and automatically pick up the correct one.
47 #[cfg(not(test))]
48 pub(crate) use efi;
49 #[cfg(test)]
50 pub(crate) use efi_mocks as efi;
51
52 #[cfg(not(test))]
53 use {
54 core::fmt::Write,
55 efi::{efi_print, efi_println, EfiEntry},
56 libgbl::Result,
57 utils::loaded_image_path,
58 };
59
60 #[cfg(not(test))]
61 enum TargetOs {
62 Android,
63 Fuchsia,
64 }
65
66 #[cfg(not(test))]
get_target_os(entry: &EfiEntry) -> TargetOs67 fn get_target_os(entry: &EfiEntry) -> TargetOs {
68 let mut buf = [0u8; 1];
69 if entry
70 .system_table()
71 .runtime_services()
72 .get_variable(&efi::GBL_EFI_VENDOR_GUID, efi::GBL_EFI_OS_BOOT_TARGET_VARNAME, &mut buf)
73 .is_ok()
74 {
75 efi_println!(
76 entry,
77 "`{}` is set. Proceeding as Fuchsia.",
78 efi::GBL_EFI_OS_BOOT_TARGET_VARNAME
79 );
80 TargetOs::Fuchsia
81 } else if fuchsia_boot::is_fuchsia_gpt(&entry).is_ok() {
82 efi_println!(entry, "Partition layout looks like Fuchsia. Proceeding as Fuchsia");
83 TargetOs::Fuchsia
84 } else {
85 efi_println!(entry, "Proceeding as Android");
86 TargetOs::Android
87 }
88 }
89
90 /// GBL EFI application logic entry point.
91 #[cfg(not(test))]
app_main(entry: EfiEntry) -> Result<()>92 pub fn app_main(entry: EfiEntry) -> Result<()> {
93 efi_println!(entry, "****Generic Bootloader Application****");
94 if let Ok(v) = loaded_image_path(&entry) {
95 efi_println!(entry, "Image path: {}", v);
96 }
97
98 match get_target_os(&entry) {
99 TargetOs::Fuchsia => fuchsia_boot::fuchsia_boot_demo(entry)?,
100 TargetOs::Android => android_boot::android_boot_demo(entry)?,
101 }
102
103 Ok(())
104 }
105