xref: /aosp_15_r20/bootable/libbootloader/gbl/efi/BUILD (revision 5225e6b173e52d2efc6bcf950c27374fd72adabc)
1*5225e6b1SAndroid Build Coastguard Worker# Copyright (C) 2023 The Android Open Source Project
2*5225e6b1SAndroid Build Coastguard Worker#
3*5225e6b1SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*5225e6b1SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*5225e6b1SAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*5225e6b1SAndroid Build Coastguard Worker#
7*5225e6b1SAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*5225e6b1SAndroid Build Coastguard Worker#
9*5225e6b1SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*5225e6b1SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*5225e6b1SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*5225e6b1SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*5225e6b1SAndroid Build Coastguard Worker# limitations under the License.
14*5225e6b1SAndroid Build Coastguard Worker
15*5225e6b1SAndroid Build Coastguard Workerload("@gbl//toolchain:gbl_toolchain.bzl", "build_with_platform")
16*5225e6b1SAndroid Build Coastguard Workerload("@gbl//toolchain:gbl_workspace_util.bzl", "ANDROID_RUST_LINTS")
17*5225e6b1SAndroid Build Coastguard Workerload("@gbl_llvm_prebuilts//:info.bzl", "gbl_llvm_tool_path")
18*5225e6b1SAndroid Build Coastguard Workerload("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
19*5225e6b1SAndroid Build Coastguard Worker
20*5225e6b1SAndroid Build Coastguard Workerpackage(
21*5225e6b1SAndroid Build Coastguard Worker    default_visibility = ["//visibility:public"],
22*5225e6b1SAndroid Build Coastguard Worker)
23*5225e6b1SAndroid Build Coastguard Worker
24*5225e6b1SAndroid Build Coastguard Workerrust_library(
25*5225e6b1SAndroid Build Coastguard Worker    name = "libgbl_efi",
26*5225e6b1SAndroid Build Coastguard Worker    srcs = glob(["src/**/*.rs"]),
27*5225e6b1SAndroid Build Coastguard Worker    crate_name = "gbl_efi",
28*5225e6b1SAndroid Build Coastguard Worker    rustc_flags = ANDROID_RUST_LINTS,
29*5225e6b1SAndroid Build Coastguard Worker    deps = [
30*5225e6b1SAndroid Build Coastguard Worker        "@arrayvec",
31*5225e6b1SAndroid Build Coastguard Worker        "@avb",
32*5225e6b1SAndroid Build Coastguard Worker        "@gbl//libasync",
33*5225e6b1SAndroid Build Coastguard Worker        "@gbl//libasync:cyclic_executor",
34*5225e6b1SAndroid Build Coastguard Worker        "@gbl//libboot",
35*5225e6b1SAndroid Build Coastguard Worker        "@gbl//libefi",
36*5225e6b1SAndroid Build Coastguard Worker        "@gbl//libefi_types",
37*5225e6b1SAndroid Build Coastguard Worker        "@gbl//liberror",
38*5225e6b1SAndroid Build Coastguard Worker        "@gbl//libfastboot",
39*5225e6b1SAndroid Build Coastguard Worker        "@gbl//libfdt",
40*5225e6b1SAndroid Build Coastguard Worker        "@gbl//libgbl",
41*5225e6b1SAndroid Build Coastguard Worker        "@gbl//libsafemath",
42*5225e6b1SAndroid Build Coastguard Worker        "@gbl//libstorage",
43*5225e6b1SAndroid Build Coastguard Worker        "@smoltcp",
44*5225e6b1SAndroid Build Coastguard Worker        "@spin",
45*5225e6b1SAndroid Build Coastguard Worker        "@uuid",
46*5225e6b1SAndroid Build Coastguard Worker        "@zbi",
47*5225e6b1SAndroid Build Coastguard Worker        "@zerocopy",
48*5225e6b1SAndroid Build Coastguard Worker    ],
49*5225e6b1SAndroid Build Coastguard Worker)
50*5225e6b1SAndroid Build Coastguard Worker
51*5225e6b1SAndroid Build Coastguard Workerrust_test(
52*5225e6b1SAndroid Build Coastguard Worker    name = "test",
53*5225e6b1SAndroid Build Coastguard Worker    crate = ":libgbl_efi",
54*5225e6b1SAndroid Build Coastguard Worker    # TODO(b/355436086): mock out the rest of the libefi APIs and
55*5225e6b1SAndroid Build Coastguard Worker    # remove dead-code; for now it would require a lot of invasive
56*5225e6b1SAndroid Build Coastguard Worker    # code changes to selectively disable things on tests so this
57*5225e6b1SAndroid Build Coastguard Worker    # is worth it to keep things more readable.
58*5225e6b1SAndroid Build Coastguard Worker    rustc_flags = ANDROID_RUST_LINTS + [
59*5225e6b1SAndroid Build Coastguard Worker        "-A",
60*5225e6b1SAndroid Build Coastguard Worker        "dead-code",
61*5225e6b1SAndroid Build Coastguard Worker    ],
62*5225e6b1SAndroid Build Coastguard Worker    deps = [
63*5225e6b1SAndroid Build Coastguard Worker        "@gbl//libefi:mocks",
64*5225e6b1SAndroid Build Coastguard Worker        "@mockall",
65*5225e6b1SAndroid Build Coastguard Worker    ],
66*5225e6b1SAndroid Build Coastguard Worker)
67*5225e6b1SAndroid Build Coastguard Worker
68*5225e6b1SAndroid Build Coastguard Worker# The UEFI application target.
69*5225e6b1SAndroid Build Coastguard Worker#
70*5225e6b1SAndroid Build Coastguard Worker# Almost all logic should be in the libgbl_efi target; this contains only the
71*5225e6b1SAndroid Build Coastguard Worker# things that would prevent unittesting such as global allocation hooks or
72*5225e6b1SAndroid Build Coastguard Worker# target-specific compiler dependencies.
73*5225e6b1SAndroid Build Coastguard Workerrust_binary(
74*5225e6b1SAndroid Build Coastguard Worker    name = "app",
75*5225e6b1SAndroid Build Coastguard Worker    srcs = glob(["app/**/*.rs"]),
76*5225e6b1SAndroid Build Coastguard Worker    linker_script = select(
77*5225e6b1SAndroid Build Coastguard Worker        {
78*5225e6b1SAndroid Build Coastguard Worker            "@gbl//toolchain:gbl_rust_elf_riscv64": "@gbl//efi/arch/riscv64:riscv64_efi.lds",
79*5225e6b1SAndroid Build Coastguard Worker            "//conditions:default": None,
80*5225e6b1SAndroid Build Coastguard Worker        },
81*5225e6b1SAndroid Build Coastguard Worker    ),
82*5225e6b1SAndroid Build Coastguard Worker    rustc_flags = ANDROID_RUST_LINTS + [
83*5225e6b1SAndroid Build Coastguard Worker        "-C",
84*5225e6b1SAndroid Build Coastguard Worker        "panic=abort",
85*5225e6b1SAndroid Build Coastguard Worker    ],
86*5225e6b1SAndroid Build Coastguard Worker    deps = [
87*5225e6b1SAndroid Build Coastguard Worker        ":libgbl_efi",
88*5225e6b1SAndroid Build Coastguard Worker        "@avb//:avb_crypto_ops_sha_impl_staticlib",
89*5225e6b1SAndroid Build Coastguard Worker        "@gbl//libavb:sysdeps",
90*5225e6b1SAndroid Build Coastguard Worker        "@gbl//libefi",
91*5225e6b1SAndroid Build Coastguard Worker        "@gbl//libefi_types",
92*5225e6b1SAndroid Build Coastguard Worker    ] + select(
93*5225e6b1SAndroid Build Coastguard Worker        {
94*5225e6b1SAndroid Build Coastguard Worker            "@gbl//toolchain:gbl_rust_elf_riscv64": [
95*5225e6b1SAndroid Build Coastguard Worker                "@gbl//efi/arch/riscv64:efi_arch_deps_riscv64",
96*5225e6b1SAndroid Build Coastguard Worker            ],
97*5225e6b1SAndroid Build Coastguard Worker            "//conditions:default": [],
98*5225e6b1SAndroid Build Coastguard Worker        },
99*5225e6b1SAndroid Build Coastguard Worker    ),
100*5225e6b1SAndroid Build Coastguard Worker)
101*5225e6b1SAndroid Build Coastguard Worker
102*5225e6b1SAndroid Build Coastguard Workergenrule(
103*5225e6b1SAndroid Build Coastguard Worker    name = "gbl_efi",
104*5225e6b1SAndroid Build Coastguard Worker    srcs = [":app"],
105*5225e6b1SAndroid Build Coastguard Worker    outs = ["gbl.efi"],
106*5225e6b1SAndroid Build Coastguard Worker    cmd = select(
107*5225e6b1SAndroid Build Coastguard Worker        {
108*5225e6b1SAndroid Build Coastguard Worker            # For RISCV target, existing toolchain can only generate ELF format image.
109*5225e6b1SAndroid Build Coastguard Worker            # The current solution is to manually add a PE/COFF header at image
110*5225e6b1SAndroid Build Coastguard Worker            # start and use objcopy to remove the ELF header to make it a PE/COFF image.
111*5225e6b1SAndroid Build Coastguard Worker            # Also use `elf_static_relocation_checker` to check that our relocation library
112*5225e6b1SAndroid Build Coastguard Worker            # can handle all the generated relocation types. The following expands to two commands:
113*5225e6b1SAndroid Build Coastguard Worker            #
114*5225e6b1SAndroid Build Coastguard Worker            # 1. `llvm-objcopy <elf image> -O binary <efi image>`
115*5225e6b1SAndroid Build Coastguard Worker            # 2. `elf_static_relocation_checker <elf image> <efi image>`
116*5225e6b1SAndroid Build Coastguard Worker            "@gbl//toolchain:gbl_rust_elf_riscv64": """
117*5225e6b1SAndroid Build Coastguard Worker            {} -O binary $(location @gbl//efi:app) $(OUTS) && \\
118*5225e6b1SAndroid Build Coastguard Worker            $(location @gbl//libelf:elf_static_relocation_checker) $(SRCS) $(OUTS)
119*5225e6b1SAndroid Build Coastguard Worker        """.format(
120*5225e6b1SAndroid Build Coastguard Worker                gbl_llvm_tool_path("llvm-objcopy"),
121*5225e6b1SAndroid Build Coastguard Worker            ),
122*5225e6b1SAndroid Build Coastguard Worker            "//conditions:default": "cp $(SRCS) $(OUTS)",
123*5225e6b1SAndroid Build Coastguard Worker        },
124*5225e6b1SAndroid Build Coastguard Worker    ),
125*5225e6b1SAndroid Build Coastguard Worker    tools = select(
126*5225e6b1SAndroid Build Coastguard Worker        {
127*5225e6b1SAndroid Build Coastguard Worker            "@gbl//toolchain:gbl_rust_elf_riscv64": [
128*5225e6b1SAndroid Build Coastguard Worker                "@gbl//libelf:elf_static_relocation_checker",
129*5225e6b1SAndroid Build Coastguard Worker            ],
130*5225e6b1SAndroid Build Coastguard Worker            "//conditions:default": [],
131*5225e6b1SAndroid Build Coastguard Worker        },
132*5225e6b1SAndroid Build Coastguard Worker    ),
133*5225e6b1SAndroid Build Coastguard Worker)
134*5225e6b1SAndroid Build Coastguard Worker
135*5225e6b1SAndroid Build Coastguard Workerbuild_with_platform(
136*5225e6b1SAndroid Build Coastguard Worker    name = "x86_64",
137*5225e6b1SAndroid Build Coastguard Worker    platform = "@gbl//toolchain:gbl_uefi_x86_64",
138*5225e6b1SAndroid Build Coastguard Worker    deps = [":gbl_efi"],
139*5225e6b1SAndroid Build Coastguard Worker)
140*5225e6b1SAndroid Build Coastguard Worker
141*5225e6b1SAndroid Build Coastguard Workerbuild_with_platform(
142*5225e6b1SAndroid Build Coastguard Worker    name = "x86_32",
143*5225e6b1SAndroid Build Coastguard Worker    platform = "@gbl//toolchain:gbl_uefi_x86_32",
144*5225e6b1SAndroid Build Coastguard Worker    deps = [":gbl_efi"],
145*5225e6b1SAndroid Build Coastguard Worker)
146*5225e6b1SAndroid Build Coastguard Worker
147*5225e6b1SAndroid Build Coastguard Workerbuild_with_platform(
148*5225e6b1SAndroid Build Coastguard Worker    name = "aarch64",
149*5225e6b1SAndroid Build Coastguard Worker    platform = "@gbl//toolchain:gbl_uefi_aarch64",
150*5225e6b1SAndroid Build Coastguard Worker    deps = [":gbl_efi"],
151*5225e6b1SAndroid Build Coastguard Worker)
152*5225e6b1SAndroid Build Coastguard Worker
153*5225e6b1SAndroid Build Coastguard Workerbuild_with_platform(
154*5225e6b1SAndroid Build Coastguard Worker    name = "riscv64",
155*5225e6b1SAndroid Build Coastguard Worker    platform = "@gbl//toolchain:gbl_elf_riscv64",
156*5225e6b1SAndroid Build Coastguard Worker    deps = [":gbl_efi"],
157*5225e6b1SAndroid Build Coastguard Worker)
158*5225e6b1SAndroid Build Coastguard Worker
159*5225e6b1SAndroid Build Coastguard Workerfilegroup(
160*5225e6b1SAndroid Build Coastguard Worker    name = "all_platforms",
161*5225e6b1SAndroid Build Coastguard Worker    srcs = [
162*5225e6b1SAndroid Build Coastguard Worker        ":aarch64",
163*5225e6b1SAndroid Build Coastguard Worker        ":riscv64",
164*5225e6b1SAndroid Build Coastguard Worker        ":x86_32",
165*5225e6b1SAndroid Build Coastguard Worker        ":x86_64",
166*5225e6b1SAndroid Build Coastguard Worker    ],
167*5225e6b1SAndroid Build Coastguard Worker)
168