xref: /aosp_15_r20/bootable/libbootloader/gbl/libavb/BUILD.avb.bazel (revision 5225e6b173e52d2efc6bcf950c27374fd72adabc)
1# Copyright (C) 2024 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", "link_static_cc_library")
16load("@gbl_llvm_prebuilts//:info.bzl", "LLVM_PREBUILTS_C_INCLUDE")
17load("@rules_rust//bindgen:defs.bzl", "rust_bindgen")
18load("@rules_rust//rust:defs.bzl", "rust_library")
19
20package(
21    default_visibility = ["//visibility:public"],
22)
23
24exports_files(glob(["**/*"]))
25
26# Newer version of `rust_bindgen` requires a `cc_library` target that actually produces a static
27# library and instead of only headers. Thus we generate a placeholder source file to meet the
28# requirement.
29genrule(
30    name = "bindgen_noop_cc",
31    outs = ["bindgen_noop_cc.cc"],
32    cmd = "touch $(OUTS)",
33)
34
35cc_library(
36    name = "headers",
37    srcs = [":bindgen_noop_cc"],
38    hdrs = [
39        "libavb/avb_chain_partition_descriptor.h",
40        "libavb/avb_cmdline.h",
41        "libavb/avb_crypto.h",
42        "libavb/avb_descriptor.h",
43        "libavb/avb_footer.h",
44        "libavb/avb_hash_descriptor.h",
45        "libavb/avb_hashtree_descriptor.h",
46        "libavb/avb_kernel_cmdline_descriptor.h",
47        "libavb/avb_ops.h",
48        "libavb/avb_property_descriptor.h",
49        "libavb/avb_rsa.h",
50        "libavb/avb_sha.h",
51        "libavb/avb_slot_verify.h",
52        "libavb/avb_sysdeps.h",
53        "libavb/avb_util.h",
54        "libavb/avb_vbmeta_image.h",
55        "libavb/avb_version.h",
56        "libavb/libavb.h",
57        "libavb/sha/avb_crypto_ops_impl.h",
58        "libavb_cert/avb_cert_ops.h",
59        "libavb_cert/avb_cert_types.h",
60        "libavb_cert/avb_cert_validate.h",
61        "libavb_cert/libavb_cert.h",
62    ],
63    includes = [
64        ".",
65        "libavb/sha/",
66    ],
67    deps = ["@gbl//libc:headers"],
68)
69
70cc_library(
71    name = "libavb_c",
72    srcs = [
73        "libavb/avb_chain_partition_descriptor.c",
74        "libavb/avb_cmdline.c",
75        "libavb/avb_crc32.c",
76        "libavb/avb_crypto.c",
77        "libavb/avb_descriptor.c",
78        "libavb/avb_footer.c",
79        "libavb/avb_hash_descriptor.c",
80        "libavb/avb_hashtree_descriptor.c",
81        "libavb/avb_kernel_cmdline_descriptor.c",
82        "libavb/avb_property_descriptor.c",
83        "libavb/avb_rsa.c",
84        "libavb/avb_slot_verify.c",
85        "libavb/avb_util.c",
86        "libavb/avb_vbmeta_image.c",
87        "libavb/avb_version.c",
88        "libavb_cert/avb_cert_validate.c",
89
90        # Contains noop placeholder for avb_printv/avb_printf
91        "@gbl//libavb:print.c",
92    ],
93    copts = [
94        "-D_FILE_OFFSET_BITS=64",
95        "-D_POSIX_C_SOURCE=199309L",
96        "-Wa,--noexecstack",
97        "-Werror",
98        "-Wall",
99        "-Wextra",
100        "-Wformat=2",
101        "-Wmissing-prototypes",
102        "-Wno-unused-parameter",
103        "-ffunction-sections",
104        "-g",
105        "-DAVB_ENABLE_DEBUG",
106        "-DAVB_COMPILATION",
107        # libavb uses more than 4K of stack space. This prevents the compiler from inserting
108        # _chkstk().
109        "-mstack-probe-size=8192",
110    ],
111    deps = [
112        ":headers",
113    ],
114)
115
116link_static_cc_library(
117    name = "libavb_c_staticlib",
118    cc_library = ":libavb_c",
119)
120
121cc_library(
122    name = "avb_crypto_ops_sha_impl",
123    srcs = [
124        "libavb/sha/sha256_impl.c",
125        "libavb/sha/sha512_impl.c",
126    ],
127    copts = ["-DAVB_COMPILATION"],
128    deps = [
129        ":headers",
130    ],
131)
132
133link_static_cc_library(
134    name = "avb_crypto_ops_sha_impl_staticlib",
135    cc_library = ":avb_crypto_ops_sha_impl",
136)
137
138rust_bindgen(
139    name = "libavb_c_bindgen",
140    # Flags should match Soong definition in external/avb/rust/Android.bp.
141    bindgen_flags = [
142        "--constified-enum-module=AvbDescriptorTag",
143        "--bitfield-enum=Avb.*Flags",
144        "--default-enum-style=rust",
145        "--with-derive-default",
146        "--with-derive-custom=Avb.*Descriptor=FromZeroes,FromBytes",
147        "--with-derive-custom=AvbCertPermanentAttributes=FromZeroes,FromBytes,AsBytes",
148        "--with-derive-custom=AvbCertCertificate.*=FromZeroes,FromBytes,AsBytes",
149        "--with-derive-custom=AvbCertUnlock.*=FromZeroes,FromBytes,AsBytes",
150        "--allowlist-type=AvbDescriptorTag",
151        "--allowlist-type=Avb.*Flags",
152        "--allowlist-function=.*",
153        "--allowlist-var=AVB.*",
154        "--use-core",
155        "--raw-line=#![no_std]",
156        "--raw-line=use zerocopy::{AsBytes, FromBytes, FromZeroes};",
157        "--ctypes-prefix=core::ffi",
158    ],
159    cc_lib = "headers",
160    # For x86_32, we need to explicitly specify 32bit architecture.
161    clang_flags = select({
162        "@gbl//toolchain:gbl_rust_uefi_x86_32": ["-m32"],
163        "//conditions:default": ["-m64"],
164    }) + [
165        "-I{}".format(LLVM_PREBUILTS_C_INCLUDE),
166        "-nostdinc",
167    ],
168    header = "libavb_cert/libavb_cert.h",
169)
170
171rust_library(
172    name = "avb_bindgen",
173    srcs = [":libavb_c_bindgen"],
174    rustc_flags = [
175        "--allow=non_snake_case",
176        "--allow=non_camel_case_types",
177    ],
178    deps = ["@zerocopy"],
179)
180
181rust_library(
182    name = "avb",
183    srcs = glob(["rust/src/**/*.rs"]),
184    crate_features = ["uuid"],
185    edition = "2021",
186    deps = [
187        ":avb_bindgen",
188        ":libavb_c_staticlib",
189        "@uuid",
190        "@zerocopy",
191    ],
192)
193
194rust_library(
195    name = "avb_test",
196    srcs = ["rust/tests/test_ops.rs"],
197    crate_features = ["uuid"],
198    edition = "2021",
199    deps = [
200        ":avb",
201        "@uuid",
202    ],
203)
204