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