xref: /aosp_15_r20/bootable/libbootloader/gbl/README.md (revision 5225e6b173e52d2efc6bcf950c27374fd72adabc)
1*5225e6b1SAndroid Build Coastguard Worker# Generic Bootloader Library
2*5225e6b1SAndroid Build Coastguard Worker
3*5225e6b1SAndroid Build Coastguard WorkerThis directory hosts the Generic Bootloader Library project. A Bazel
4*5225e6b1SAndroid Build Coastguard Workerworkspace is setup for building the library as well as an EFI executable that
5*5225e6b1SAndroid Build Coastguard Workercan be loaded directly from the firmware.
6*5225e6b1SAndroid Build Coastguard Worker
7*5225e6b1SAndroid Build Coastguard Worker## Get source tree and build
8*5225e6b1SAndroid Build Coastguard Worker
9*5225e6b1SAndroid Build Coastguard WorkerTo successfully get and build the tree your machine must have the following dependencies installed:
10*5225e6b1SAndroid Build Coastguard Worker
11*5225e6b1SAndroid Build Coastguard Worker```
12*5225e6b1SAndroid Build Coastguard Worker# repo to work with android repositories (https://source.android.com/docs/setup/reference/repo)
13*5225e6b1SAndroid Build Coastguard Worker# bazel-bootstrap to build (https://bazel.build/)
14*5225e6b1SAndroid Build Coastguard Workersudo apt install repo bazel-bootstrap
15*5225e6b1SAndroid Build Coastguard Worker```
16*5225e6b1SAndroid Build Coastguard Worker
17*5225e6b1SAndroid Build Coastguard WorkerThe GBL project are intended to be built from the
18*5225e6b1SAndroid Build Coastguard Worker[Android UEFI Manifest](https://android.googlesource.com/kernel/manifest/+/refs/heads/uefi-gbl-mainline/default.xml)
19*5225e6b1SAndroid Build Coastguard Workercheckout:
20*5225e6b1SAndroid Build Coastguard Worker
21*5225e6b1SAndroid Build Coastguard Worker```
22*5225e6b1SAndroid Build Coastguard Workerrepo init -u https://android.googlesource.com/kernel/manifest -b uefi-gbl-mainline
23*5225e6b1SAndroid Build Coastguard Workerrepo sync -j16
24*5225e6b1SAndroid Build Coastguard Worker```
25*5225e6b1SAndroid Build Coastguard Worker
26*5225e6b1SAndroid Build Coastguard WorkerTo build the EFI application:
27*5225e6b1SAndroid Build Coastguard Worker
28*5225e6b1SAndroid Build Coastguard Worker```
29*5225e6b1SAndroid Build Coastguard Worker./tools/bazel run //bootable/libbootloader:gbl_efi_dist --extra_toolchains=@gbl//toolchain:all
30*5225e6b1SAndroid Build Coastguard Worker```
31*5225e6b1SAndroid Build Coastguard Worker
32*5225e6b1SAndroid Build Coastguard WorkerThe above builds the EFI application for all of `x86_64`, `x86_32`, `aarch64`
33*5225e6b1SAndroid Build Coastguard Workerand `riscv64` platforms.
34*5225e6b1SAndroid Build Coastguard Worker
35*5225e6b1SAndroid Build Coastguard WorkerTo run the set of unit tests:
36*5225e6b1SAndroid Build Coastguard Worker
37*5225e6b1SAndroid Build Coastguard Worker```
38*5225e6b1SAndroid Build Coastguard Worker./tools/bazel test @gbl//tests --extra_toolchains=@gbl//toolchain:all
39*5225e6b1SAndroid Build Coastguard Worker```
40*5225e6b1SAndroid Build Coastguard Worker
41*5225e6b1SAndroid Build Coastguard Worker## IDE Setup
42*5225e6b1SAndroid Build Coastguard Worker
43*5225e6b1SAndroid Build Coastguard WorkerFor rust development, we recommend use VSCode + rust-analyzer plugin.
44*5225e6b1SAndroid Build Coastguard Worker
45*5225e6b1SAndroid Build Coastguard Workerrust-analyzer requires `rust-project.json` to work properly. Luckily, bazel has
46*5225e6b1SAndroid Build Coastguard Workersupport for generating `rust-project.json`:
47*5225e6b1SAndroid Build Coastguard Worker
48*5225e6b1SAndroid Build Coastguard Worker```
49*5225e6b1SAndroid Build Coastguard Worker./tools/bazel run @rules_rust//tools/rust_analyzer:gen_rust_project --norepository_disable_download -- --bazel ./tools/bazel @gbl//efi/...
50*5225e6b1SAndroid Build Coastguard Worker```
51*5225e6b1SAndroid Build Coastguard Worker
52*5225e6b1SAndroid Build Coastguard Worker`@gbl//efi/...` is the target to generate rust project for, here it means
53*5225e6b1SAndroid Build Coastguard Worker"everything under @gbl//efi/ directory" . Omitting the target specifier would
54*5225e6b1SAndroid Build Coastguard Workerresult in analyzing "@/..." , which would most likely fail due to some obscure
55*5225e6b1SAndroid Build Coastguard Workerreason. Should targets get moved around in the future, this path spec also need
56*5225e6b1SAndroid Build Coastguard Workerto be updated.
57*5225e6b1SAndroid Build Coastguard Worker
58*5225e6b1SAndroid Build Coastguard WorkerAfter generating `rust-project.json`, you would notice that your IDE still
59*5225e6b1SAndroid Build Coastguard Workerdoesn't offer auto completion. This is because some source file paths pointing
60*5225e6b1SAndroid Build Coastguard Workerto bazel-output dir, and you are most likely editing source files in
61*5225e6b1SAndroid Build Coastguard Worker`bootable/libbootloader/gbl`. In addition, the generated rust-project.json sets
62*5225e6b1SAndroid Build Coastguard Worker"cfg=test" for all targets, which causes certain dependency graph to resolve
63*5225e6b1SAndroid Build Coastguard Workerincorrectly. To fix this, run
64*5225e6b1SAndroid Build Coastguard Worker
65*5225e6b1SAndroid Build Coastguard Worker```
66*5225e6b1SAndroid Build Coastguard Workerpython3 bootable/libbootloader/gbl/rewrite_rust_project_path.py rust-project.json
67*5225e6b1SAndroid Build Coastguard Worker```
68*5225e6b1SAndroid Build Coastguard Worker
69*5225e6b1SAndroid Build Coastguard WorkerAnd reload your IDE.
70*5225e6b1SAndroid Build Coastguard Worker
71*5225e6b1SAndroid Build Coastguard Worker## Run the EFI application
72*5225e6b1SAndroid Build Coastguard Worker
73*5225e6b1SAndroid Build Coastguard Worker### Boot Android on Cuttlefish
74*5225e6b1SAndroid Build Coastguard Worker
75*5225e6b1SAndroid Build Coastguard WorkerIf you have a main AOSP checkout and is setup to run
76*5225e6b1SAndroid Build Coastguard Worker[Cuttlefish](https://source.android.com/docs/setup/create/cuttlefish), you can
77*5225e6b1SAndroid Build Coastguard Workerrun the EFI image directly with:
78*5225e6b1SAndroid Build Coastguard Worker
79*5225e6b1SAndroid Build Coastguard Worker```
80*5225e6b1SAndroid Build Coastguard Workercvd start --android_efi_loader=<path to the EFI image> ...
81*5225e6b1SAndroid Build Coastguard Worker```
82*5225e6b1SAndroid Build Coastguard Worker
83*5225e6b1SAndroid Build Coastguard WorkerThe above uses the same setting as a normal `cvd start` run, except that
84*5225e6b1SAndroid Build Coastguard Workerinstead of booting Android directly, the emulator first hands off to the EFI
85*5225e6b1SAndroid Build Coastguard Workerapplication, which will take over booting android.
86*5225e6b1SAndroid Build Coastguard Worker
87*5225e6b1SAndroid Build Coastguard WorkerNote: For x86 platform, use the EFI image built for `x86_32`.
88*5225e6b1SAndroid Build Coastguard Worker
89*5225e6b1SAndroid Build Coastguard Worker### Boot Fuchsia on Vim3
90*5225e6b1SAndroid Build Coastguard Worker
91*5225e6b1SAndroid Build Coastguard WorkerBooting Fuchsia on a Vim3 development board is supported. To run the
92*5225e6b1SAndroid Build Coastguard Workerapplication:
93*5225e6b1SAndroid Build Coastguard Worker
94*5225e6b1SAndroid Build Coastguard Worker1. Complete all
95*5225e6b1SAndroid Build Coastguard Worker[bootstrap steps](https://fuchsia.dev/fuchsia-src/development/hardware/khadas-vim3?hl=en)
96*5225e6b1SAndroid Build Coastguard Workerto setup Vim3 as a Fuchsia device.
97*5225e6b1SAndroid Build Coastguard Worker2. Reboot the device into fastboot mode.
98*5225e6b1SAndroid Build Coastguard Worker3. Run fastboot command:
99*5225e6b1SAndroid Build Coastguard Worker```
100*5225e6b1SAndroid Build Coastguard Workerfastboot stage <path to the EFI binary> && fastboot oem run-staged-efi
101*5225e6b1SAndroid Build Coastguard Worker```
102*5225e6b1SAndroid Build Coastguard Worker
103*5225e6b1SAndroid Build Coastguard Worker### Run on standalone QEMU
104*5225e6b1SAndroid Build Coastguard Worker
105*5225e6b1SAndroid Build Coastguard WorkerIf you want to test the EFI image directly on QEMU with your custom
106*5225e6b1SAndroid Build Coastguard Workerconfigurations:
107*5225e6b1SAndroid Build Coastguard Worker
108*5225e6b1SAndroid Build Coastguard Worker1. Install EDK, QEMU and u-boot prebuilts
109*5225e6b1SAndroid Build Coastguard Worker
110*5225e6b1SAndroid Build Coastguard Worker   ```
111*5225e6b1SAndroid Build Coastguard Worker   sudo apt-get install qemu-system ovmf u-boot-qemu
112*5225e6b1SAndroid Build Coastguard Worker   ```
113*5225e6b1SAndroid Build Coastguard Worker
114*5225e6b1SAndroid Build Coastguard Worker1. Depending on the target architecture you want to run:
115*5225e6b1SAndroid Build Coastguard Worker
116*5225e6b1SAndroid Build Coastguard Worker   For `x86_64`:
117*5225e6b1SAndroid Build Coastguard Worker   ```
118*5225e6b1SAndroid Build Coastguard Worker   mkdir -p /tmp/esp/EFI/BOOT && \
119*5225e6b1SAndroid Build Coastguard Worker   cp <path to EFI image> /tmp/esp/EFI/BOOT/bootx64.efi && \
120*5225e6b1SAndroid Build Coastguard Worker   qemu-system-x86_64 -nographic \
121*5225e6b1SAndroid Build Coastguard Worker       -drive if=pflash,format=raw,readonly=on,file=/usr/share/OVMF/OVMF_CODE.fd \
122*5225e6b1SAndroid Build Coastguard Worker       -drive format=raw,file=fat:rw:/tmp/esp
123*5225e6b1SAndroid Build Coastguard Worker   ```
124*5225e6b1SAndroid Build Coastguard Worker
125*5225e6b1SAndroid Build Coastguard Worker   For `aarch64`:
126*5225e6b1SAndroid Build Coastguard Worker   ```
127*5225e6b1SAndroid Build Coastguard Worker   mkdir -p /tmp/esp/EFI/BOOT && \
128*5225e6b1SAndroid Build Coastguard Worker   cp <path to EFI image> /tmp/esp/EFI/BOOT/bootaa64.efi && \
129*5225e6b1SAndroid Build Coastguard Worker   qemu-system-aarch64 -nographic -machine virt -m 1G -cpu cortex-a57 \
130*5225e6b1SAndroid Build Coastguard Worker       -drive if=pflash,format=raw,readonly=on,file=/usr/share/AAVMF/AAVMF_CODE.fd \
131*5225e6b1SAndroid Build Coastguard Worker       -drive format=raw,file=fat:rw:/tmp/esp
132*5225e6b1SAndroid Build Coastguard Worker   ```
133*5225e6b1SAndroid Build Coastguard Worker
134*5225e6b1SAndroid Build Coastguard Worker   For `riscv64`:
135*5225e6b1SAndroid Build Coastguard Worker   ```
136*5225e6b1SAndroid Build Coastguard Worker   mkdir -p /tmp/esp/EFI/BOOT && \
137*5225e6b1SAndroid Build Coastguard Worker   cp <path to EFI image> /tmp/esp/EFI/BOOT/bootriscv64.efi && \
138*5225e6b1SAndroid Build Coastguard Worker   qemu-system-riscv64 -nographic -machine virt -m 256M \
139*5225e6b1SAndroid Build Coastguard Worker       -bios /usr/lib/u-boot/qemu-riscv64/u-boot.bin \
140*5225e6b1SAndroid Build Coastguard Worker       -drive format=raw,file=fat:rw:/tmp/esp,id=blk0 \
141*5225e6b1SAndroid Build Coastguard Worker       -device virtio-blk-device,drive=blk0
142*5225e6b1SAndroid Build Coastguard Worker   ```
143*5225e6b1SAndroid Build Coastguard Worker
144*5225e6b1SAndroid Build Coastguard Worker### Boot Fuchsia on emulator
145*5225e6b1SAndroid Build Coastguard Worker
146*5225e6b1SAndroid Build Coastguard Worker1. Make sure Fuchsia target pass control to GBL.
147*5225e6b1SAndroid Build Coastguard Worker
148*5225e6b1SAndroid Build Coastguard Worker   Set path to GBL binary here: [fuchsia/src/firmware/gigaboot/cpp/backends.gni : gigaboot_gbl_efi_app](https://cs.opensource.google/fuchsia/fuchsia/+/main:src/firmware/gigaboot/cpp/backends.gni;l=25?q=gigaboot_gbl_efi_app)
149*5225e6b1SAndroid Build Coastguard Worker
150*5225e6b1SAndroid Build Coastguard Worker   E.g. in `fuchsia/src/firmware/gigaboot/cpp/backends.gni`:
151*5225e6b1SAndroid Build Coastguard Worker   ```
152*5225e6b1SAndroid Build Coastguard Worker   $ cat ./fuchsia/src/firmware/gigaboot/cpp/backends.gni
153*5225e6b1SAndroid Build Coastguard Worker   ...
154*5225e6b1SAndroid Build Coastguard Worker   declare_args() {
155*5225e6b1SAndroid Build Coastguard Worker      ...
156*5225e6b1SAndroid Build Coastguard Worker      gigaboot_gbl_efi_app = "<path to EFI image>/gbl_x86_64.efi"
157*5225e6b1SAndroid Build Coastguard Worker   }
158*5225e6b1SAndroid Build Coastguard Worker   ```
159*5225e6b1SAndroid Build Coastguard Worker
160*5225e6b1SAndroid Build Coastguard Worker   Or in `fx set`:
161*5225e6b1SAndroid Build Coastguard Worker   ```
162*5225e6b1SAndroid Build Coastguard Worker   fx set core.x64 --args=gigaboot_gbl_efi_app='"<path to EFI image>/gbl_x86_64.efi"'
163*5225e6b1SAndroid Build Coastguard Worker   ```
164*5225e6b1SAndroid Build Coastguard Worker
165*5225e6b1SAndroid Build Coastguard Worker2. Build: (this has to be done every time if EFI app changes)
166*5225e6b1SAndroid Build Coastguard Worker
167*5225e6b1SAndroid Build Coastguard Worker   `fx build`
168*5225e6b1SAndroid Build Coastguard Worker
169*5225e6b1SAndroid Build Coastguard Worker3. Run emulator in UEFI mode with raw disk
170*5225e6b1SAndroid Build Coastguard Worker
171*5225e6b1SAndroid Build Coastguard Worker   ```
172*5225e6b1SAndroid Build Coastguard Worker   fx qemu -a x64 --uefi --disktype=nvme -D ./out/default/obj/build/images/disk.raw
173*5225e6b1SAndroid Build Coastguard Worker   ```
174*5225e6b1SAndroid Build Coastguard Worker
175*5225e6b1SAndroid Build Coastguard Worker## EFI Protocols
176*5225e6b1SAndroid Build Coastguard Worker
177*5225e6b1SAndroid Build Coastguard WorkerList of EFI protocols used by GBL and a brief description of each [here](./docs/efi_protocols.md).
178