xref: /aosp_15_r20/external/avb/rust/Android.bp (revision d289c2ba6de359471b23d594623b906876bc48a0)
1// Copyright 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
15// Rust bindgen wrappers to allow calling into libavb from Rust.
16//
17// The auto-generated wrappers are Rust unsafe and somewhat difficult to work
18// with so are not exposed outside of this directory; instead we will provide
19// a safe higher-level Rust API.
20rust_defaults {
21    name: "libavb_bindgen.common.defaults",
22    wrapper_src: "bindgen/avb.h",
23    crate_name: "avb_bindgen",
24    edition: "2021",
25    visibility: [
26        ":__subpackages__",
27        // TODO(b/290110273): add the Rust public API layer here and adjust
28        // Virtualization packages to depend on it instead of the raw bindgen.
29        "//packages/modules/Virtualization:__subpackages__",
30    ],
31    source_stem: "bindings",
32    bindgen_flags: [
33        "--constified-enum-module=AvbDescriptorTag",
34        "--bitfield-enum=Avb.*Flags",
35        "--default-enum-style rust",
36        "--with-derive-default",
37        "--with-derive-custom=Avb.*Descriptor=FromZeroes,FromBytes",
38        "--with-derive-custom=AvbCertPermanentAttributes=FromZeroes,FromBytes,AsBytes",
39        "--with-derive-custom=AvbCertCertificate.*=FromZeroes,FromBytes,AsBytes",
40        "--with-derive-custom=AvbCertUnlock.*=FromZeroes,FromBytes,AsBytes",
41        "--allowlist-type=AvbDescriptorTag",
42        "--allowlist-type=Avb.*Flags",
43        "--allowlist-function=.*",
44        "--allowlist-var=AVB.*",
45        "--use-core",
46        "--raw-line=#![no_std]",
47        "--raw-line=use zerocopy::{AsBytes, FromBytes, FromZeroes};",
48        "--ctypes-prefix=core::ffi",
49    ],
50    cflags: ["-DBORINGSSL_NO_CXX"],
51}
52
53// Full bindgen defaults for std targets.
54rust_defaults {
55    name: "libavb_bindgen.std.defaults",
56    defaults: ["libavb_bindgen.common.defaults"],
57    host_supported: true,
58    static_libs: ["libavb_cert"],
59    shared_libs: ["libcrypto"],
60    rustlibs: ["libzerocopy"],
61    apex_available: ["com.android.virt"],
62}
63
64// Full bindgen default for nostd targets.
65rust_defaults {
66    name: "libavb_bindgen.nostd.defaults",
67    defaults: ["libavb_bindgen.common.defaults"],
68    static_libs: [
69        "libavb_cert_baremetal",
70        "libcrypto_baremetal",
71    ],
72    rustlibs: ["libzerocopy_nostd_noalloc"],
73    dylib: {
74        enabled: false,
75    },
76}
77
78// Internal source-only bindgen with std.
79//
80// This target should only be used as `srcs`, not `rustlibs` or `rlibs`. This
81// is because the `rust_bindgen` rule intentionally only generates rlibs
82// (b/166332519), and also forces its dependencies to use rlibs. However, this
83// can create mismatched library types if the depenency is also used elsewhere
84// in a build rule as a dylib. In particular for us, libzerocopy and its own
85// dependency libbyteorder trigger this problem like so:
86//
87//              build target (prefer dylib)
88//                  /                 \
89//          libavb_rs (dylib)          \
90//               /                      \
91//       libavb_bindgen (rlib)    ... arbitrary dependency chain (dylib) ...
92//            /                             \
93//     libzerocopy (rlib)                    \
94//         /                                  \
95//   libbyteorder (rlib)               libbyteorder (dylib)
96//
97// By using it as a `srcs` instead, we can wrap it in a `rust_library` which
98// allows selecting either library type and fixes the conflict:
99//
100//              build target (prefer dylib)
101//                  /                 \
102//          libavb_rs (dylib)          \
103//               /                      \
104//       libavb_bindgen (dylib)    ... arbitrary dependency chain (dylib) ...
105//            /                         /
106//     libzerocopy (dylib)             /
107//                 \                  /
108//                  libbyteorder (dylib)
109//
110rust_bindgen {
111    name: "libavb_bindgen_for_srcs_only",
112    defaults: ["libavb_bindgen.std.defaults"],
113}
114
115// Bindgen with std.
116//
117// See above for why we need a `rust_library` wrapper here.
118rust_library {
119    name: "libavb_bindgen",
120    defaults: ["libavb_bindgen.std.defaults"],
121    srcs: [":libavb_bindgen_for_srcs_only"],
122}
123
124// Bindgen nostd.
125//
126// Nostd targets always use rlibs, so we don't need a `rust_library` wrapper in
127// this case; the rlib-only bindgen target is sufficient.
128rust_bindgen {
129    name: "libavb_bindgen_nostd",
130    defaults: ["libavb_bindgen.nostd.defaults"],
131}
132
133// Bindgen auto-generated tests.
134rust_test {
135    name: "libavb_bindgen_test",
136    srcs: [":libavb_bindgen_for_srcs_only"],
137    crate_name: "avb_bindgen_test",
138    edition: "2021",
139    test_suites: ["general-tests"],
140    auto_gen_config: true,
141    clippy_lints: "none",
142    lints: "none",
143    rustlibs: ["libzerocopy"],
144}
145
146// Rust library wrapping libavb C implementation.
147
148// Common defaults for all variations.
149rust_defaults {
150    name: "libavb_rs_common.defaults",
151    crate_name: "avb",
152    srcs: ["src/lib.rs"],
153    clippy_lints: "android",
154    lints: "android",
155}
156
157// No std, no features.
158rust_defaults {
159    name: "libavb_rs_nostd.defaults",
160    defaults: ["libavb_rs_common.defaults"],
161    // Only rlib can build without the required nostd hooks (eh_personality,
162    // panic_handler, etc) to defer them for the final binary to implement.
163    prefer_rlib: true,
164    no_stdlibs: true,
165    rustlibs: [
166        "libavb_bindgen_nostd",
167        "libzerocopy_nostd_noalloc",
168    ],
169    whole_static_libs: [
170        "libavb_cert_baremetal",
171    ],
172    stdlibs: [
173        "libcore.rust_sysroot",
174    ],
175}
176
177// Std, no features.
178rust_defaults {
179    name: "libavb_rs.defaults",
180    defaults: ["libavb_rs_common.defaults"],
181    host_supported: true,
182    rustlibs: [
183        "libavb_bindgen",
184        "libzerocopy",
185    ],
186    whole_static_libs: [
187        "libavb_cert",
188    ],
189}
190
191// Adds UUID feature for nostd.
192rust_defaults {
193    name: "libavb_rs_nostd.uuid.defaults",
194    features: [
195        "uuid",
196    ],
197    rustlibs: [
198        "libuuid_nostd",
199    ],
200}
201
202// Adds UUID feature for std.
203rust_defaults {
204    name: "libavb_rs.uuid.defaults",
205    features: [
206        "uuid",
207    ],
208    rustlibs: [
209        "libuuid",
210    ],
211}
212
213// lib: no std, no features.
214rust_library_rlib {
215    name: "libavb_rs_nostd",
216    defaults: ["libavb_rs_nostd.defaults"],
217}
218
219// lib: no std, UUID feature.
220rust_library_rlib {
221    name: "libavb_rs_nostd_uuid",
222    defaults: [
223        "libavb_rs_nostd.defaults",
224        "libavb_rs_nostd.uuid.defaults",
225    ],
226}
227
228// lib: std, no features.
229rust_library {
230    name: "libavb_rs",
231    defaults: ["libavb_rs.defaults"],
232}
233
234// lib: std, UUID feature.
235rust_library {
236    name: "libavb_rs_uuid",
237    defaults: [
238        "libavb_rs.defaults",
239        "libavb_rs.uuid.defaults",
240    ],
241}
242
243// TestOps lib: std
244rust_library {
245    crate_name: "avb_test",
246    name: "libavb_test_rs_testops",
247    srcs: ["tests/test_ops.rs"],
248    clippy_lints: "android",
249    lints: "android",
250    host_supported: true,
251    rustlibs: [
252        "libavb_rs",
253    ],
254    whole_static_libs: [
255        "libavb_cert",
256    ],
257}
258
259// "libavb_rs.defaults" plus additional unit test defaults.
260rust_defaults {
261    name: "libavb_rs_unittest.defaults",
262    defaults: ["libavb_rs.defaults"],
263    data: [":libavb_rs_example_descriptors"],
264    test_suites: ["general-tests"],
265}
266
267// Unit tests: std, no features.
268rust_test {
269    name: "libavb_rs_unittest",
270    defaults: ["libavb_rs_unittest.defaults"],
271}
272
273// Unit tests: std, UUID feature.
274rust_test {
275    name: "libavb_rs_uuid_unittest",
276    defaults: [
277        "libavb_rs_unittest.defaults",
278        "libavb_rs.uuid.defaults",
279    ],
280}
281
282// Example descriptors in binary format.
283filegroup {
284    name: "libavb_rs_example_descriptors",
285    srcs: [
286        "testdata/chain_partition_descriptor.bin",
287        "testdata/hash_descriptor.bin",
288        "testdata/hashtree_descriptor.bin",
289        "testdata/kernel_commandline_descriptor.bin",
290        "testdata/property_descriptor.bin",
291    ],
292}
293
294// Integration test defaults.
295rust_defaults {
296    name: "libavb_rs_test.defaults",
297    srcs: ["tests/tests.rs"],
298    compile_multilib: "first",
299    data: [
300        ":avb_cert_test_permanent_attributes",
301        ":avb_cert_test_unlock_challenge",
302        ":avb_cert_test_unlock_credential",
303        ":avb_testkey_rsa4096_pub_bin",
304        ":avb_testkey_rsa8192_pub_bin",
305        ":avbrs_test_image",
306        ":avbrs_test_image_with_vbmeta_footer",
307        ":avbrs_test_image_with_vbmeta_footer_for_boot",
308        ":avbrs_test_image_with_vbmeta_footer_for_test_part_2",
309        ":avbrs_test_vbmeta",
310        ":avbrs_test_vbmeta_2_parts",
311        ":avbrs_test_vbmeta_cert",
312        ":avbrs_test_vbmeta_persistent_digest",
313        ":avbrs_test_vbmeta_with_chained_partition",
314        ":avbrs_test_vbmeta_with_commandline",
315        ":avbrs_test_vbmeta_with_hashtree",
316        ":avbrs_test_vbmeta_with_property",
317    ],
318    rustlibs: [
319        "libhex",
320        "libzerocopy",
321    ],
322    test_suites: ["general-tests"],
323    clippy_lints: "android",
324    lints: "android",
325}
326
327// Integration test: no features.
328rust_test {
329    name: "libavb_rs_test",
330    defaults: ["libavb_rs_test.defaults"],
331    rustlibs: ["libavb_rs"],
332}
333
334// Integration test: UUID feature.
335rust_test {
336    name: "libavb_rs_uuid_test",
337    defaults: [
338        "libavb_rs.uuid.defaults",
339        "libavb_rs_test.defaults",
340    ],
341    rustlibs: ["libavb_rs_uuid"],
342}
343
344// Test images for verification.
345
346// Unsigned 16KiB test image.
347genrule {
348    name: "avbrs_test_image",
349    tools: ["avbtool"],
350    out: ["test_image.img"],
351    cmd: "$(location avbtool) generate_test_image --image_size 16384 --output $(out)",
352}
353
354// Unsigned vbmeta blob containing the test image descriptor for partition name "test_part".
355avb_gen_vbmeta_image {
356    name: "avbrs_test_image_descriptor",
357    src: ":avbrs_test_image",
358    partition_name: "test_part",
359    salt: "1000",
360}
361
362// Unsigned vbmeta blob containing the test image descriptor for partition name "test_part_2".
363avb_gen_vbmeta_image {
364    name: "avbrs_test_image_descriptor_2",
365    src: ":avbrs_test_image",
366    partition_name: "test_part_2",
367    salt: "1001",
368}
369
370// Unsigned vbmeta blob containing a persistent digest descriptor for partition name
371// "test_part_persistent_digest".
372//
373// Currently this is the only in-tree usage of persistent digests, but if anyone else needs it
374// later on it may be worth folding support for this into the `avb_gen_vbmeta_image` rule.
375genrule {
376    name: "avbrs_test_image_descriptor_persistent_digest",
377    tools: ["avbtool"],
378    srcs: [":avbrs_test_image"],
379    out: ["avbrs_test_image_descriptor_persistent_digest.img"],
380    cmd: "$(location avbtool) add_hash_footer --image $(location :avbrs_test_image) --partition_name test_part_persistent_digest --dynamic_partition_size --do_not_append_vbmeta_image --use_persistent_digest --output_vbmeta_image $(out)",
381}
382
383// Unsigned vbmeta blob containing a hastree descriptor for partition name
384// "test_part_hashtree".
385genrule {
386    name: "avbrs_test_image_descriptor_hashtree",
387    tools: ["avbtool"],
388    srcs: [":avbrs_test_image"],
389    out: ["avbrs_test_image_descriptor_hashtree.img"],
390    // Generating FEC values requires the `fec` tool to be on $PATH, which does
391    // not seems to be possible here. For now pass `--do_not_generate_fec`.
392    cmd: "$(location avbtool) add_hashtree_footer --image $(location :avbrs_test_image) --partition_name test_part_hashtree --partition_size 0 --salt B000 --do_not_append_vbmeta_image --output_vbmeta_image $(out) --do_not_generate_fec",
393}
394
395// Standalone vbmeta image signing the test image descriptor.
396cc_genrule {
397    name: "avbrs_test_vbmeta",
398    compile_multilib: "first",
399    tools: ["avbtool"],
400    srcs: [
401        ":avbrs_test_image_descriptor",
402        ":avb_testkey_rsa4096",
403    ],
404    out: ["test_vbmeta.img"],
405    cmd: "$(location avbtool) make_vbmeta_image --key $(location :avb_testkey_rsa4096) --algorithm SHA512_RSA4096 --include_descriptors_from_image $(location :avbrs_test_image_descriptor) --output $(out)",
406}
407
408// Standalone vbmeta image signing the test image descriptor with
409// `avb_cert_testkey_psk` and `avb_cert_test_metadata`.
410cc_genrule {
411    name: "avbrs_test_vbmeta_cert",
412    compile_multilib: "first",
413    tools: ["avbtool"],
414    srcs: [
415        ":avbrs_test_image_descriptor",
416        ":avb_cert_test_metadata",
417        ":avb_cert_testkey_psk",
418    ],
419    out: ["test_vbmeta_cert.img"],
420    cmd: "$(location avbtool) make_vbmeta_image --key $(location :avb_cert_testkey_psk) --public_key_metadata $(location :avb_cert_test_metadata) --algorithm SHA512_RSA4096 --include_descriptors_from_image $(location :avbrs_test_image_descriptor) --output $(out)",
421}
422
423// Standalone vbmeta image signing the test image descriptors for "test_part" and "test_part_2".
424cc_genrule {
425    name: "avbrs_test_vbmeta_2_parts",
426    compile_multilib: "first",
427    tools: ["avbtool"],
428    srcs: [
429        ":avbrs_test_image_descriptor",
430        ":avbrs_test_image_descriptor_2",
431        ":avb_testkey_rsa4096",
432    ],
433    out: ["test_vbmeta_2_parts.img"],
434    cmd: "$(location avbtool) make_vbmeta_image --key $(location :avb_testkey_rsa4096) --algorithm SHA512_RSA4096 --include_descriptors_from_image $(location :avbrs_test_image_descriptor) --include_descriptors_from_image $(location :avbrs_test_image_descriptor_2) --output $(out)",
435}
436
437// Standalone vbmeta image signing the test image persistent digest descriptor.
438cc_genrule {
439    name: "avbrs_test_vbmeta_persistent_digest",
440    tools: ["avbtool"],
441    srcs: [
442        ":avbrs_test_image_descriptor_persistent_digest",
443        ":avb_testkey_rsa4096",
444    ],
445    out: ["test_vbmeta_persistent_digest.img"],
446    cmd: "$(location avbtool) make_vbmeta_image --key $(location :avb_testkey_rsa4096) --algorithm SHA512_RSA4096 --include_descriptors_from_image $(location :avbrs_test_image_descriptor_persistent_digest) --output $(out)",
447}
448
449// Standalone vbmeta image with property descriptor "test_prop_key" = "test_prop_value".
450cc_genrule {
451    name: "avbrs_test_vbmeta_with_property",
452    compile_multilib: "first",
453    tools: ["avbtool"],
454    srcs: [
455        ":avbrs_test_image_descriptor",
456        ":avb_testkey_rsa4096",
457    ],
458    out: ["test_vbmeta_with_property.img"],
459    cmd: "$(location avbtool) make_vbmeta_image --prop test_prop_key:test_prop_value --key $(location :avb_testkey_rsa4096) --algorithm SHA512_RSA4096 --include_descriptors_from_image $(location :avbrs_test_image_descriptor) --output $(out)",
460}
461
462// Standalone vbmeta image with the test image hashtree descriptor.
463cc_genrule {
464    name: "avbrs_test_vbmeta_with_hashtree",
465    tools: ["avbtool"],
466    srcs: [
467        ":avbrs_test_image_descriptor_hashtree",
468        ":avb_testkey_rsa4096",
469    ],
470    out: ["test_vbmeta_with_hashtree.img"],
471    cmd: "$(location avbtool) make_vbmeta_image --key $(location :avb_testkey_rsa4096) --algorithm SHA512_RSA4096 --include_descriptors_from_image $(location :avbrs_test_image_descriptor_hashtree) --output $(out)",
472}
473
474// Standalone vbmeta image with kernel commandline "test_cmdline_key=test_cmdline_value".
475cc_genrule {
476    name: "avbrs_test_vbmeta_with_commandline",
477    compile_multilib: "first",
478    tools: ["avbtool"],
479    srcs: [
480        ":avbrs_test_image_descriptor",
481        ":avb_testkey_rsa4096",
482    ],
483    out: ["test_vbmeta_with_commandline.img"],
484    cmd: "$(location avbtool) make_vbmeta_image --kernel_cmdline test_cmdline_key=test_cmdline_value --key $(location :avb_testkey_rsa4096) --algorithm SHA512_RSA4096 --include_descriptors_from_image $(location :avbrs_test_image_descriptor) --output $(out)",
485}
486
487// Standalone vbmeta image with chain descriptor to "test_part_2" with rollback
488// index 4, signed by avb_testkey_rsa8192.
489cc_genrule {
490    name: "avbrs_test_vbmeta_with_chained_partition",
491    compile_multilib: "first",
492    tools: ["avbtool"],
493    srcs: [
494        ":avbrs_test_image_descriptor",
495        ":avb_testkey_rsa4096",
496        ":avb_testkey_rsa8192_pub_bin",
497    ],
498    out: ["test_vbmeta_with_chained_partition.img"],
499    cmd: "$(location avbtool) make_vbmeta_image --chain_partition test_part_2:4:$(location :avb_testkey_rsa8192_pub_bin) --key $(location :avb_testkey_rsa4096) --algorithm SHA512_RSA4096 --include_descriptors_from_image $(location :avbrs_test_image_descriptor) --output $(out)",
500}
501
502// Combined test image + signed vbmeta footer for "test_part".
503avb_add_hash_footer {
504    name: "avbrs_test_image_with_vbmeta_footer",
505    src: ":avbrs_test_image",
506    partition_name: "test_part",
507    private_key: ":avb_testkey_rsa4096",
508    salt: "A000",
509}
510
511// Combined test image + signed vbmeta footer for "boot".
512avb_add_hash_footer {
513    name: "avbrs_test_image_with_vbmeta_footer_for_boot",
514    src: ":avbrs_test_image",
515    partition_name: "boot",
516    private_key: ":avb_testkey_rsa4096",
517    salt: "A001",
518}
519
520// Combined test image + signed vbmeta footer for "test_part_2" signed by
521// avb_testkey_rsa8192 with rollback index = 7.
522avb_add_hash_footer {
523    name: "avbrs_test_image_with_vbmeta_footer_for_test_part_2",
524    src: ":avbrs_test_image",
525    partition_name: "test_part_2",
526    private_key: ":avb_testkey_rsa8192",
527    algorithm: "SHA256_RSA8192",
528    salt: "A002",
529    rollback_index: 7,
530}
531