• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_cc//cc:defs.bzl", "cc_library")
18load("@rules_rust//bindgen:defs.bzl", "rust_bindgen")
19load("@rules_rust//rust:defs.bzl", "rust_library")
20
21package(
22    default_visibility = ["//visibility:public"],
23)
24
25exports_files(glob(["**/*"]))
26
27# Newer version of `rust_bindgen` requires a `cc_library` target that actually produces a static
28# library and instead of only headers. Thus we generate a placeholder source file to meet the
29# requirement.
30genrule(
31    name = "bindgen_noop_cc",
32    outs = ["bindgen_noop_cc.cc"],
33    cmd = "touch $(OUTS)",
34)
35
36cc_library(
37    name = "headers",
38    srcs = [":bindgen_noop_cc"],
39    hdrs = [
40        "libavb/avb_chain_partition_descriptor.h",
41        "libavb/avb_cmdline.h",
42        "libavb/avb_crypto.h",
43        "libavb/avb_descriptor.h",
44        "libavb/avb_footer.h",
45        "libavb/avb_hash_descriptor.h",
46        "libavb/avb_hashtree_descriptor.h",
47        "libavb/avb_kernel_cmdline_descriptor.h",
48        "libavb/avb_ops.h",
49        "libavb/avb_property_descriptor.h",
50        "libavb/avb_rsa.h",
51        "libavb/avb_sha.h",
52        "libavb/avb_slot_verify.h",
53        "libavb/avb_sysdeps.h",
54        "libavb/avb_util.h",
55        "libavb/avb_vbmeta_image.h",
56        "libavb/avb_version.h",
57        "libavb/libavb.h",
58        "libavb/sha/avb_crypto_ops_impl.h",
59        "libavb_cert/avb_cert_ops.h",
60        "libavb_cert/avb_cert_types.h",
61        "libavb_cert/avb_cert_validate.h",
62        "libavb_cert/libavb_cert.h",
63    ],
64    includes = [
65        ".",
66        "libavb/sha/",
67    ],
68    deps = ["@gbl//libc:headers"],
69)
70
71cc_library(
72    name = "libavb_c",
73    srcs = [
74        "libavb/avb_chain_partition_descriptor.c",
75        "libavb/avb_cmdline.c",
76        "libavb/avb_crc32.c",
77        "libavb/avb_crypto.c",
78        "libavb/avb_descriptor.c",
79        "libavb/avb_footer.c",
80        "libavb/avb_hash_descriptor.c",
81        "libavb/avb_hashtree_descriptor.c",
82        "libavb/avb_kernel_cmdline_descriptor.c",
83        "libavb/avb_property_descriptor.c",
84        "libavb/avb_rsa.c",
85        "libavb/avb_slot_verify.c",
86        "libavb/avb_util.c",
87        "libavb/avb_vbmeta_image.c",
88        "libavb/avb_version.c",
89        "libavb_cert/avb_cert_validate.c",
90        # C implementations for sysdeps which cannot be implemented using Rust
91        "@gbl//libavb:deps.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_USE_PRINTF_LOGS",
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=FromBytes,Immutable,KnownLayout",
147        "--with-derive-custom=AvbCertPermanentAttributes=FromBytes,IntoBytes",
148        "--with-derive-custom=AvbCertCertificate.*=FromBytes,IntoBytes",
149        "--with-derive-custom=AvbCertUnlock.*=FromBytes,IntoBytes",
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::{Immutable, IntoBytes, FromBytes, KnownLayout};",
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