• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    data = [
55        "@gbl//libfdt/test/data:all",
56    ],
57    # TODO(b/355436086): mock out the rest of the libefi APIs and
58    # remove dead-code; for now it would require a lot of invasive
59    # code changes to selectively disable things on tests so this
60    # is worth it to keep things more readable.
61    rustc_flags = ANDROID_RUST_LINTS + [
62        "-A",
63        "dead-code",
64    ],
65    deps = [
66        "@gbl//libefi:mocks",
67        "@mockall",
68    ],
69)
70
71# The UEFI application target.
72#
73# Almost all logic should be in the libgbl_efi target; this contains only the
74# things that would prevent unittesting such as global allocation hooks or
75# target-specific compiler dependencies.
76rust_binary(
77    name = "app",
78    srcs = glob(["app/**/*.rs"]),
79    linker_script = select(
80        {
81            "@gbl//toolchain:gbl_rust_elf_riscv64": "@gbl//efi/arch/riscv64:riscv64_efi.lds",
82            "//conditions:default": None,
83        },
84    ),
85    rustc_flags = ANDROID_RUST_LINTS + [
86        "-C",
87        "panic=abort",
88    ],
89    deps = [
90        ":libgbl_efi",
91        "@avb//:avb_crypto_ops_sha_impl_staticlib",
92        "@gbl//libavb:sysdeps",
93        "@gbl//libefi",
94        "@gbl//libefi_types",
95    ] + select(
96        {
97            "@gbl//toolchain:gbl_rust_elf_riscv64": [
98                "@gbl//efi/arch/riscv64:efi_arch_deps_riscv64",
99            ],
100            "//conditions:default": [],
101        },
102    ),
103)
104
105genrule(
106    name = "gbl_efi",
107    srcs = [":app"],
108    outs = ["gbl.efi"],
109    cmd = select(
110        {
111            # For RISCV target, existing toolchain can only generate ELF format image.
112            # The current solution is to manually add a PE/COFF header at image
113            # start and use objcopy to remove the ELF header to make it a PE/COFF image.
114            # Also use `elf_static_relocation_checker` to check that our relocation library
115            # can handle all the generated relocation types. The following expands to two commands:
116            #
117            # 1. `llvm-objcopy <elf image> -O binary <efi image>`
118            # 2. `elf_static_relocation_checker <elf image> <efi image>`
119            "@gbl//toolchain:gbl_rust_elf_riscv64": """
120            {} -O binary $(location @gbl//efi:app) $(OUTS) && \\
121            $(location @gbl//libelf:elf_static_relocation_checker) $(SRCS) $(OUTS)
122        """.format(
123                gbl_llvm_tool_path("llvm-objcopy"),
124            ),
125            "//conditions:default": "cp $(SRCS) $(OUTS)",
126        },
127    ),
128    tools = select(
129        {
130            "@gbl//toolchain:gbl_rust_elf_riscv64": [
131                "@gbl//libelf:elf_static_relocation_checker",
132            ],
133            "//conditions:default": [],
134        },
135    ),
136)
137
138build_with_platform(
139    name = "x86_64",
140    platform = "@gbl//toolchain:gbl_uefi_x86_64",
141    deps = [":gbl_efi"],
142)
143
144build_with_platform(
145    name = "x86_32",
146    platform = "@gbl//toolchain:gbl_uefi_x86_32",
147    deps = [":gbl_efi"],
148)
149
150build_with_platform(
151    name = "aarch64",
152    platform = "@gbl//toolchain:gbl_uefi_aarch64",
153    deps = [":gbl_efi"],
154)
155
156build_with_platform(
157    name = "riscv64",
158    platform = "@gbl//toolchain:gbl_elf_riscv64",
159    deps = [":gbl_efi"],
160)
161
162filegroup(
163    name = "all_platforms",
164    srcs = [
165        ":aarch64",
166        ":riscv64",
167        ":x86_32",
168        ":x86_64",
169    ],
170)
171