1*d289c2baSAndroid Build Coastguard Worker# Android Verified Boot 2.0 2*d289c2baSAndroid Build Coastguard Worker--- 3*d289c2baSAndroid Build Coastguard Worker 4*d289c2baSAndroid Build Coastguard WorkerThis repository contains tools and libraries for working with Android 5*d289c2baSAndroid Build Coastguard WorkerVerified Boot 2.0. Usually AVB is used to refer to this codebase. 6*d289c2baSAndroid Build Coastguard Worker 7*d289c2baSAndroid Build Coastguard Worker# Table of Contents 8*d289c2baSAndroid Build Coastguard Worker 9*d289c2baSAndroid Build Coastguard Worker* [What is it?](#what-is-it) 10*d289c2baSAndroid Build Coastguard Worker + [The VBMeta struct](#the-vbmeta-struct) 11*d289c2baSAndroid Build Coastguard Worker + [Rollback Protection](#rollback-Protection) 12*d289c2baSAndroid Build Coastguard Worker + [A/B Support](#a_b-Support) 13*d289c2baSAndroid Build Coastguard Worker + [The VBMeta Digest](#the-vbmeta-digest) 14*d289c2baSAndroid Build Coastguard Worker* [Tools and Libraries](#tools-and-libraries) 15*d289c2baSAndroid Build Coastguard Worker + [avbtool and libavb](#avbtool-and-libavb) 16*d289c2baSAndroid Build Coastguard Worker + [Files and Directories](#files-and-directories) 17*d289c2baSAndroid Build Coastguard Worker + [Portability](#portability) 18*d289c2baSAndroid Build Coastguard Worker + [Versioning and Compatibility](#versioning-and-compatibility) 19*d289c2baSAndroid Build Coastguard Worker + [Adding New Features](#adding-new-features) 20*d289c2baSAndroid Build Coastguard Worker + [Using avbtool](#using-avbtool) 21*d289c2baSAndroid Build Coastguard Worker + [Build System Integration](#build-system-integration) 22*d289c2baSAndroid Build Coastguard Worker* [Device Integration](#device-integration) 23*d289c2baSAndroid Build Coastguard Worker + [System Dependencies](#system-dependencies) 24*d289c2baSAndroid Build Coastguard Worker + [Locked and Unlocked mode](#locked-and-unlocked-mode) 25*d289c2baSAndroid Build Coastguard Worker + [Tamper-evident Storage](#tamper_evident-storage) 26*d289c2baSAndroid Build Coastguard Worker + [Named Persistent Values](#named-persistent-values) 27*d289c2baSAndroid Build Coastguard Worker + [Persistent Digests](#persistent-digests) 28*d289c2baSAndroid Build Coastguard Worker + [Updating Stored Rollback Indexes](#updating-stored-rollback-indexes) 29*d289c2baSAndroid Build Coastguard Worker + [Recommended Bootflow](#recommended-bootflow) 30*d289c2baSAndroid Build Coastguard Worker + [Booting Into Recovery](#booting-into-recovery) 31*d289c2baSAndroid Build Coastguard Worker + [Handling dm-verity Errors](#handling-dm_verity-errors) 32*d289c2baSAndroid Build Coastguard Worker + [Android Specific Integration](#android-specific-integration) 33*d289c2baSAndroid Build Coastguard Worker + [Device Specific Notes](#device-specific-notes) 34*d289c2baSAndroid Build Coastguard Worker* [Version History](#version-history) 35*d289c2baSAndroid Build Coastguard Worker 36*d289c2baSAndroid Build Coastguard Worker# What is it? 37*d289c2baSAndroid Build Coastguard Worker 38*d289c2baSAndroid Build Coastguard WorkerVerified boot is the process of assuring the end user of the integrity 39*d289c2baSAndroid Build Coastguard Workerof the software running on a device. It typically starts with a 40*d289c2baSAndroid Build Coastguard Workerread-only portion of the device firmware which loads code and executes 41*d289c2baSAndroid Build Coastguard Workerit only after cryptographically verifying that the code is authentic 42*d289c2baSAndroid Build Coastguard Workerand doesn't have any known security flaws. AVB is one implementation 43*d289c2baSAndroid Build Coastguard Workerof verified boot. 44*d289c2baSAndroid Build Coastguard Worker 45*d289c2baSAndroid Build Coastguard Worker## The VBMeta struct 46*d289c2baSAndroid Build Coastguard Worker 47*d289c2baSAndroid Build Coastguard WorkerThe central data structure used in AVB is the VBMeta struct. This data 48*d289c2baSAndroid Build Coastguard Workerstructure contains a number of descriptors (and other metadata) and 49*d289c2baSAndroid Build Coastguard Workerall of this data is cryptographically signed. Descriptors are used for 50*d289c2baSAndroid Build Coastguard Workerimage hashes, image hashtree metadata, and so-called *chained 51*d289c2baSAndroid Build Coastguard Workerpartitions*. A simple example is the following: 52*d289c2baSAndroid Build Coastguard Worker 53*d289c2baSAndroid Build Coastguard Worker 54*d289c2baSAndroid Build Coastguard Worker 55*d289c2baSAndroid Build Coastguard Workerwhere the `vbmeta` partition holds the hash for the `boot` partition 56*d289c2baSAndroid Build Coastguard Workerin a hash descriptor. For the `system` and `vendor` partitions a 57*d289c2baSAndroid Build Coastguard Workerhashtree follows the filesystem data and the `vbmeta` partition holds 58*d289c2baSAndroid Build Coastguard Workerthe root hash, salt, and offset of the hashtree in hashtree 59*d289c2baSAndroid Build Coastguard Workerdescriptors. Because the VBMeta struct in the `vbmeta` partition is 60*d289c2baSAndroid Build Coastguard Workercryptographically signed, the boot loader can check the signature and 61*d289c2baSAndroid Build Coastguard Workerverify it was made by the owner of `key0` (by e.g. embedding the 62*d289c2baSAndroid Build Coastguard Workerpublic part of `key0`) and thereby trust the hashes used for `boot`, 63*d289c2baSAndroid Build Coastguard Worker`system`, and `vendor`. 64*d289c2baSAndroid Build Coastguard Worker 65*d289c2baSAndroid Build Coastguard WorkerA chained partition descriptor is used to delegate authority - it 66*d289c2baSAndroid Build Coastguard Workercontains the name of the partition where authority is delegated as 67*d289c2baSAndroid Build Coastguard Workerwell as the public key that is trusted for signatures on this 68*d289c2baSAndroid Build Coastguard Workerparticular partition. As an example, consider the following setup: 69*d289c2baSAndroid Build Coastguard Worker 70*d289c2baSAndroid Build Coastguard Worker 71*d289c2baSAndroid Build Coastguard Worker 72*d289c2baSAndroid Build Coastguard WorkerIn this setup the `xyz` partition has a hashtree for 73*d289c2baSAndroid Build Coastguard Workerintegrity-checking. Following the hashtree is a VBMeta struct which 74*d289c2baSAndroid Build Coastguard Workercontains the hashtree descriptor with hashtree metadata (root hash, 75*d289c2baSAndroid Build Coastguard Workersalt, offset, etc.) and this struct is signed with `key1`. Finally, at 76*d289c2baSAndroid Build Coastguard Workerthe end of the partition is a footer which has the offset of the 77*d289c2baSAndroid Build Coastguard WorkerVBMeta struct. 78*d289c2baSAndroid Build Coastguard Worker 79*d289c2baSAndroid Build Coastguard WorkerThis setup allows the bootloader to use the chain partition descriptor 80*d289c2baSAndroid Build Coastguard Workerto find the footer at the end of the partition (using the name in the 81*d289c2baSAndroid Build Coastguard Workerchain partition descriptor) which in turns helps locate the VBMeta 82*d289c2baSAndroid Build Coastguard Workerstruct and verify that it was signed by `key1` (using `key1_pub` stored in the 83*d289c2baSAndroid Build Coastguard Workerchain partition descriptor). Crucially, because there's a footer with 84*d289c2baSAndroid Build Coastguard Workerthe offset, the `xyz` partition can be updated without the `vbmeta` 85*d289c2baSAndroid Build Coastguard Workerpartition needing any changes. 86*d289c2baSAndroid Build Coastguard Worker 87*d289c2baSAndroid Build Coastguard WorkerThe VBMeta struct is flexible enough to allow hash descriptors and hashtree 88*d289c2baSAndroid Build Coastguard Workerdescriptors for any partition to live in the `vbmeta` partition, the partition 89*d289c2baSAndroid Build Coastguard Workerthat they are used to integrity check (via a chain partition descriptor), or any 90*d289c2baSAndroid Build Coastguard Workerother partition (via a chain partition descriptor). This allows for a wide range 91*d289c2baSAndroid Build Coastguard Workerof organizational and trust relationships. 92*d289c2baSAndroid Build Coastguard Worker 93*d289c2baSAndroid Build Coastguard WorkerChained partitions need not use a footer - it is permissible to have a chained 94*d289c2baSAndroid Build Coastguard Workerpartition point to a partition where the VBMeta struct is at the beginning 95*d289c2baSAndroid Build Coastguard Worker(e.g. just like the `vbmeta` partition). This is useful for use-cases where all 96*d289c2baSAndroid Build Coastguard Workerhash- and hashtree-descriptors for the partitions owned by an entire 97*d289c2baSAndroid Build Coastguard Workerorganization are stored in a dedicated partition, for example `vbmeta_google`. 98*d289c2baSAndroid Build Coastguard WorkerIn this example the hashtree descriptor for `system` is in the `vbmeta_google` 99*d289c2baSAndroid Build Coastguard Workerpartition meaning that the bootloader doesn't need to access the `system` 100*d289c2baSAndroid Build Coastguard Workerpartition at all which is helpful if the `system` partition is managed as a 101*d289c2baSAndroid Build Coastguard Workerlogical partition (via e.g. [LVM 102*d289c2baSAndroid Build Coastguard Workertechniques](https://en.wikipedia.org/wiki/Logical_volume_management) or 103*d289c2baSAndroid Build Coastguard Workersimilar). 104*d289c2baSAndroid Build Coastguard Worker 105*d289c2baSAndroid Build Coastguard Worker## Rollback Protection 106*d289c2baSAndroid Build Coastguard Worker 107*d289c2baSAndroid Build Coastguard WorkerAVB includes Rollback Protection which is used to protect against 108*d289c2baSAndroid Build Coastguard Workerknown security flaws. Each VBMeta struct has a *rollback index* baked 109*d289c2baSAndroid Build Coastguard Workerinto it like the following: 110*d289c2baSAndroid Build Coastguard Worker 111*d289c2baSAndroid Build Coastguard Worker 112*d289c2baSAndroid Build Coastguard Worker 113*d289c2baSAndroid Build Coastguard WorkerThese numbers are referred to as `rollback_index[n]` and are increased 114*d289c2baSAndroid Build Coastguard Workerfor each image as security flaws are discovered and 115*d289c2baSAndroid Build Coastguard Workerfixed. Additionally the device stores the last seen rollback index in 116*d289c2baSAndroid Build Coastguard Workertamper-evident storage: 117*d289c2baSAndroid Build Coastguard Worker 118*d289c2baSAndroid Build Coastguard Worker 119*d289c2baSAndroid Build Coastguard Worker 120*d289c2baSAndroid Build Coastguard Workerand these are referred to as `stored_rollback_index[n]`. 121*d289c2baSAndroid Build Coastguard Worker 122*d289c2baSAndroid Build Coastguard WorkerRollback protection is having the device reject an image unless 123*d289c2baSAndroid Build Coastguard Worker`rollback_index[n]` >= `stored_rollback_index[n]` for all `n`, and 124*d289c2baSAndroid Build Coastguard Workerhaving the device increase `stored_rollback_index[n]` over 125*d289c2baSAndroid Build Coastguard Workertime. Exactly how this is done is discussed in 126*d289c2baSAndroid Build Coastguard Workerthe 127*d289c2baSAndroid Build Coastguard Worker[Updating Stored Rollback Indexes](#updating-stored-rollback-indexes) 128*d289c2baSAndroid Build Coastguard Workersection. 129*d289c2baSAndroid Build Coastguard Worker 130*d289c2baSAndroid Build Coastguard Worker## A/B Support 131*d289c2baSAndroid Build Coastguard Worker 132*d289c2baSAndroid Build Coastguard WorkerAVB has been designed to work with A/B by requiring that the A/B 133*d289c2baSAndroid Build Coastguard Workersuffix is never used in any partition names stored in 134*d289c2baSAndroid Build Coastguard Workerdescriptors. Here's an example with two slots: 135*d289c2baSAndroid Build Coastguard Worker 136*d289c2baSAndroid Build Coastguard Worker 137*d289c2baSAndroid Build Coastguard Worker 138*d289c2baSAndroid Build Coastguard WorkerNote how the rollback indexes differ between slots - for slot A the 139*d289c2baSAndroid Build Coastguard Workerrollback indexes are `[42, 101]` and for slot B they are `[43, 103]`. 140*d289c2baSAndroid Build Coastguard Worker 141*d289c2baSAndroid Build Coastguard WorkerIn version 1.1 or later, avbtool supports `--do_not_use_ab` for 142*d289c2baSAndroid Build Coastguard Worker`add_hash_footer` and `add_hashtree_footer` operations. This makes it 143*d289c2baSAndroid Build Coastguard Workerpossible to work with a partition that does not use A/B and should 144*d289c2baSAndroid Build Coastguard Workernever have the prefix. This corresponds to the 145*d289c2baSAndroid Build Coastguard Worker`AVB_HASH[TREE]_DESCRIPTOR_FLAGS_DO_NOT_USE_AB` flags. 146*d289c2baSAndroid Build Coastguard Worker 147*d289c2baSAndroid Build Coastguard WorkerIn version 1.3, avbtool supports `chain_partition_do_not_use_ab` for 148*d289c2baSAndroid Build Coastguard Worker`make_vbmeta_image` operations. This makes it possible to work with 149*d289c2baSAndroid Build Coastguard Workera chain partition that does not use A/B and should not have the suffix. 150*d289c2baSAndroid Build Coastguard WorkerThis corresponds to the `AVB_CHAIN_PARTITION_DESCRIPTOR_FLAGS_DO_NOT_USE_AB` flag. 151*d289c2baSAndroid Build Coastguard Worker 152*d289c2baSAndroid Build Coastguard Worker## The VBMeta Digest 153*d289c2baSAndroid Build Coastguard Worker 154*d289c2baSAndroid Build Coastguard WorkerThe VBMeta digest is a digest over all VBMeta structs including the root struct 155*d289c2baSAndroid Build Coastguard Worker(e.g. in the `vbmeta` partition) and all VBMeta structs in chained 156*d289c2baSAndroid Build Coastguard Workerpartitions. This digest can be calculated at build time using `avbtool 157*d289c2baSAndroid Build Coastguard Workercalculate_vbmeta_digest` and also at runtime using the 158*d289c2baSAndroid Build Coastguard Worker`avb_slot_verify_data_calculate_vbmeta_digest()` function. It is also set on the 159*d289c2baSAndroid Build Coastguard Workerkernel command-line as `androidboot.vbmeta.digest`, see the `avb_slot_verify()` 160*d289c2baSAndroid Build Coastguard Workerdocumentation for exact details. 161*d289c2baSAndroid Build Coastguard Worker 162*d289c2baSAndroid Build Coastguard WorkerThis digest can be used together with `libavb` in userspace inside the loaded 163*d289c2baSAndroid Build Coastguard Workeroperating system to verify authenticity of the loaded vbmeta structs. This is 164*d289c2baSAndroid Build Coastguard Workeruseful if the root-of-trust and/or stored rollback indexes are only available 165*d289c2baSAndroid Build Coastguard Workerwhile running in the boot loader. 166*d289c2baSAndroid Build Coastguard Worker 167*d289c2baSAndroid Build Coastguard WorkerAdditionally, if the VBMeta digest is included in [hardware-backed attestation 168*d289c2baSAndroid Build Coastguard Workerdata](https://developer.android.com/training/articles/security-key-attestation) 169*d289c2baSAndroid Build Coastguard Workera relying party can extract the digest and compare it with list of digests for 170*d289c2baSAndroid Build Coastguard Workerknown good operating systems which, if found, provides additional assurance 171*d289c2baSAndroid Build Coastguard Workerabout the device the application is running on. 172*d289c2baSAndroid Build Coastguard Worker 173*d289c2baSAndroid Build Coastguard WorkerFor [factory images of Pixel 3 and later 174*d289c2baSAndroid Build Coastguard Workerdevices](https://developers.google.com/android/images), the 175*d289c2baSAndroid Build Coastguard Worker`pixel_factory_image_verify.py` located in `tools/transparency` is a convenience 176*d289c2baSAndroid Build Coastguard Workertool for downloading, verifying and calcuating VBMeta Digests. 177*d289c2baSAndroid Build Coastguard Worker 178*d289c2baSAndroid Build Coastguard Worker $ pixel_factory_image_verify.py https://dl.google.com/dl/android/aosp/image.zip 179*d289c2baSAndroid Build Coastguard Worker Fetching file from: https://dl.google.com/dl/android/aosp/image.zip 180*d289c2baSAndroid Build Coastguard Worker Successfully downloaded file. 181*d289c2baSAndroid Build Coastguard Worker Successfully unpacked factory image. 182*d289c2baSAndroid Build Coastguard Worker Successfully unpacked factory image partitions. 183*d289c2baSAndroid Build Coastguard Worker Successfully verified VBmeta. 184*d289c2baSAndroid Build Coastguard Worker Successfully calculated VBMeta Digest. 185*d289c2baSAndroid Build Coastguard Worker The VBMeta Digest for factory image is: 1f329b20a2dd69425e7a29566ca870dad51d2c579311992d41c9ba9ba05e170e 186*d289c2baSAndroid Build Coastguard Worker 187*d289c2baSAndroid Build Coastguard WorkerIf the given argument is not an URL it considered to be a local file: 188*d289c2baSAndroid Build Coastguard Worker 189*d289c2baSAndroid Build Coastguard Worker $ pixel_factory_image_verify.py image.zip 190*d289c2baSAndroid Build Coastguard Worker 191*d289c2baSAndroid Build Coastguard Worker# Tools and Libraries 192*d289c2baSAndroid Build Coastguard Worker 193*d289c2baSAndroid Build Coastguard WorkerThis section contains information about the tools and libraries 194*d289c2baSAndroid Build Coastguard Workerincluded in AVB. 195*d289c2baSAndroid Build Coastguard Worker 196*d289c2baSAndroid Build Coastguard Worker## avbtool and libavb 197*d289c2baSAndroid Build Coastguard Worker 198*d289c2baSAndroid Build Coastguard WorkerThe main job of `avbtool` is to create `vbmeta.img` which is the 199*d289c2baSAndroid Build Coastguard Workertop-level object for verified boot. This image is designed to go into 200*d289c2baSAndroid Build Coastguard Workerthe `vbmeta` partition (or, if using A/B, the slot in question 201*d289c2baSAndroid Build Coastguard Workere.g. `vbmeta_a` or `vbmeta_b`) and be of minimal size (for out-of-band 202*d289c2baSAndroid Build Coastguard Workerupdates). The vbmeta image is cryptographically signed and contains 203*d289c2baSAndroid Build Coastguard Workerverification data (e.g. cryptographic digests) for verifying 204*d289c2baSAndroid Build Coastguard Worker`boot.img`, `system.img`, and other partitions/images. 205*d289c2baSAndroid Build Coastguard Worker 206*d289c2baSAndroid Build Coastguard WorkerThe vbmeta image can also contain references to other partitions where 207*d289c2baSAndroid Build Coastguard Workerverification data is stored as well as a public key indicating who 208*d289c2baSAndroid Build Coastguard Workershould sign the verification data. This indirection provides 209*d289c2baSAndroid Build Coastguard Workerdelegation, that is, it allows a 3rd party to control content on a 210*d289c2baSAndroid Build Coastguard Workergiven partition by including their public key in `vbmeta.img`. By 211*d289c2baSAndroid Build Coastguard Workerdesign, this authority can be easily revoked by simply updating 212*d289c2baSAndroid Build Coastguard Worker`vbmeta.img` with new descriptors for the partition in question. 213*d289c2baSAndroid Build Coastguard Worker 214*d289c2baSAndroid Build Coastguard WorkerStoring signed verification data on other images - for example 215*d289c2baSAndroid Build Coastguard Worker`boot.img` and `system.img` - is also done with `avbtool`. 216*d289c2baSAndroid Build Coastguard Worker 217*d289c2baSAndroid Build Coastguard WorkerThe minimum requirement for running `avbtool` is to either have 218*d289c2baSAndroid Build Coastguard WorkerPython 3.5 installed or build the avbtool with the embedded launcher 219*d289c2baSAndroid Build Coastguard Workerusing `m avbtool` and then run it out of the build artifact directory: 220*d289c2baSAndroid Build Coastguard Worker`out/soong/host/linux-x86/bin/avbtool` 221*d289c2baSAndroid Build Coastguard Worker 222*d289c2baSAndroid Build Coastguard WorkerIn addition to `avbtool`, a library - `libavb` - is provided. This 223*d289c2baSAndroid Build Coastguard Workerlibrary performs all verification on the device side e.g. it starts by 224*d289c2baSAndroid Build Coastguard Workerloading the `vbmeta` partition, checks the signature, and then goes on 225*d289c2baSAndroid Build Coastguard Workerto load the `boot` partition for verification. This library is 226*d289c2baSAndroid Build Coastguard Workerintended to be used in both boot loaders and inside Android. It has a 227*d289c2baSAndroid Build Coastguard Workersimple abstraction for system dependencies (see `avb_sysdeps.h`) as 228*d289c2baSAndroid Build Coastguard Workerwell as operations that the boot loader or OS is expected to implement 229*d289c2baSAndroid Build Coastguard Worker(see `avb_ops.h`). The main entry point for verification is 230*d289c2baSAndroid Build Coastguard Worker`avb_slot_verify()`. 231*d289c2baSAndroid Build Coastguard Worker 232*d289c2baSAndroid Build Coastguard WorkerAn optional extension `libavb_cert` additionally provides a scalable 233*d289c2baSAndroid Build Coastguard Workercertificate-based authorization mechanism. The base `libavb` requires 234*d289c2baSAndroid Build Coastguard Workerthe device to implement public key validation manually (see 235*d289c2baSAndroid Build Coastguard Worker`avb_validate_vbmeta_public_key()` in `avb_ops.h`), which can be 236*d289c2baSAndroid Build Coastguard Workercomplicated when working with anything other than a single hardcoded 237*d289c2baSAndroid Build Coastguard Workerkey. `libavb_cert` provides an implementation of this function which 238*d289c2baSAndroid Build Coastguard Workerprovides built-in support for features such as key rotation. 239*d289c2baSAndroid Build Coastguard Worker 240*d289c2baSAndroid Build Coastguard Worker`libavb_cert` was previously named `libavb_atx` (Android Things eXtension) but 241*d289c2baSAndroid Build Coastguard Workerit has been renamed to better represent its usefulness as a general-purpose 242*d289c2baSAndroid Build Coastguard Workerextension rather than anything specific to the Android Things project. 243*d289c2baSAndroid Build Coastguard Worker 244*d289c2baSAndroid Build Coastguard Worker## Files and Directories 245*d289c2baSAndroid Build Coastguard Worker 246*d289c2baSAndroid Build Coastguard Worker* `libavb/` 247*d289c2baSAndroid Build Coastguard Worker + An implementation of image verification. This code is designed 248*d289c2baSAndroid Build Coastguard Worker to be highly portable so it can be used in as many contexts as 249*d289c2baSAndroid Build Coastguard Worker possible. This code requires a C99-compliant C compiler. Part of 250*d289c2baSAndroid Build Coastguard Worker this code is considered internal to the implementation and 251*d289c2baSAndroid Build Coastguard Worker should not be used outside it. For example, this applies to the 252*d289c2baSAndroid Build Coastguard Worker `avb_rsa.[ch]` and `avb_sha.[ch]` files. System dependencies 253*d289c2baSAndroid Build Coastguard Worker expected to be provided by the platform is defined in 254*d289c2baSAndroid Build Coastguard Worker `avb_sysdeps.h`. If the platform provides the standard C runtime 255*d289c2baSAndroid Build Coastguard Worker `avb_sysdeps_posix.c` can be used. 256*d289c2baSAndroid Build Coastguard Worker* `libavb_cert/` 257*d289c2baSAndroid Build Coastguard Worker + A libavb extension for certificate-based authorization. 258*d289c2baSAndroid Build Coastguard Worker* `libavb_user/` 259*d289c2baSAndroid Build Coastguard Worker + Contains an `AvbOps` implementation suitable for use in Android 260*d289c2baSAndroid Build Coastguard Worker userspace. This is used in `boot_control.avb` and `avbctl`. 261*d289c2baSAndroid Build Coastguard Worker* `libavb_ab/` 262*d289c2baSAndroid Build Coastguard Worker + An experimental A/B implementation for use in boot loaders and 263*d289c2baSAndroid Build Coastguard Worker AVB examples. **NOTE**: This code is *DEPRECATED* and you must 264*d289c2baSAndroid Build Coastguard Worker define `AVB_AB_I_UNDERSTAND_LIBAVB_AB_IS_DEPRECATED` to use 265*d289c2baSAndroid Build Coastguard Worker it. The code will be removed Jun 1 2018. 266*d289c2baSAndroid Build Coastguard Worker* `boot_control/` 267*d289c2baSAndroid Build Coastguard Worker + An implementation of the Android `boot_control` HAL for use with 268*d289c2baSAndroid Build Coastguard Worker boot loaders using the experimental `libavb_ab` A/B stack. 269*d289c2baSAndroid Build Coastguard Worker **NOTE**: This code is *DEPRECATED* and will be removed Jun 1 270*d289c2baSAndroid Build Coastguard Worker 2018. 271*d289c2baSAndroid Build Coastguard Worker* `Android.bp` 272*d289c2baSAndroid Build Coastguard Worker + Build instructions for building `libavb` (a static library for use 273*d289c2baSAndroid Build Coastguard Worker on the device), host-side libraries (for unit tests), and unit 274*d289c2baSAndroid Build Coastguard Worker tests. 275*d289c2baSAndroid Build Coastguard Worker* `avbtool` 276*d289c2baSAndroid Build Coastguard Worker + A tool written in Python for working with images related to 277*d289c2baSAndroid Build Coastguard Worker verified boot. 278*d289c2baSAndroid Build Coastguard Worker* `test/` 279*d289c2baSAndroid Build Coastguard Worker + Unit tests for `abvtool`, `libavb`, `libavb_ab`, and 280*d289c2baSAndroid Build Coastguard Worker `libavb_cert`. 281*d289c2baSAndroid Build Coastguard Worker* `tools/avbctl/` 282*d289c2baSAndroid Build Coastguard Worker + Contains the source-code for a tool that can be used to control 283*d289c2baSAndroid Build Coastguard Worker AVB at runtime in Android. 284*d289c2baSAndroid Build Coastguard Worker* `examples/uefi/` 285*d289c2baSAndroid Build Coastguard Worker + Contains the source-code for a UEFI-based boot-loader utilizing 286*d289c2baSAndroid Build Coastguard Worker `libavb/` and `libavb_ab/`. 287*d289c2baSAndroid Build Coastguard Worker* `examples/cert/` 288*d289c2baSAndroid Build Coastguard Worker + Contains example source-code for using the `avb_cert` extension 289*d289c2baSAndroid Build Coastguard Worker* `README.md` 290*d289c2baSAndroid Build Coastguard Worker + This file. 291*d289c2baSAndroid Build Coastguard Worker* `docs/` 292*d289c2baSAndroid Build Coastguard Worker + Contains documentation files. 293*d289c2baSAndroid Build Coastguard Worker 294*d289c2baSAndroid Build Coastguard Worker## Portability 295*d289c2baSAndroid Build Coastguard Worker 296*d289c2baSAndroid Build Coastguard WorkerThe `libavb` code is intended to be used in bootloaders in devices 297*d289c2baSAndroid Build Coastguard Workerthat will load Android or other operating systems. The suggested 298*d289c2baSAndroid Build Coastguard Workerapproach is to copy the appropriate header and C files mentioned in 299*d289c2baSAndroid Build Coastguard Workerthe previous section into the boot loader and integrate as 300*d289c2baSAndroid Build Coastguard Workerappropriate. 301*d289c2baSAndroid Build Coastguard Worker 302*d289c2baSAndroid Build Coastguard WorkerAs the `libavb/` codebase will evolve over time integration should be 303*d289c2baSAndroid Build Coastguard Workeras non-invasive as possible. The intention is to keep the API of the 304*d289c2baSAndroid Build Coastguard Workerlibrary stable however it will be broken if necessary. As for 305*d289c2baSAndroid Build Coastguard Workerportability, the library is intended to be highly portable, work on 306*d289c2baSAndroid Build Coastguard Workerboth little- and big-endian architectures and 32- and 64-bit. It's 307*d289c2baSAndroid Build Coastguard Workeralso intended to work in non-standard environments without the 308*d289c2baSAndroid Build Coastguard Workerstandard C library and runtime. 309*d289c2baSAndroid Build Coastguard Worker 310*d289c2baSAndroid Build Coastguard WorkerIf the `AVB_ENABLE_DEBUG` preprocessor symbol is set, the code will 311*d289c2baSAndroid Build Coastguard Workerinclude useful debug information and run-time checks. Production 312*d289c2baSAndroid Build Coastguard Workerbuilds should not use this. The preprocessor symbol `AVB_COMPILATION` 313*d289c2baSAndroid Build Coastguard Workershould be set only when compiling the libraries. The code must be 314*d289c2baSAndroid Build Coastguard Workercompiled into a separate library. 315*d289c2baSAndroid Build Coastguard Worker 316*d289c2baSAndroid Build Coastguard WorkerApplications using the compiled `libavb` library must only include the 317*d289c2baSAndroid Build Coastguard Worker`libavb/libavb.h` file (which will include all public interfaces) and 318*d289c2baSAndroid Build Coastguard Workermust not have the `AVB_COMPILATION` preprocessor symbol set. This is 319*d289c2baSAndroid Build Coastguard Workerto ensure that internal code that may be change in the future (for 320*d289c2baSAndroid Build Coastguard Workerexample `avb_sha.[ch]` and `avb_rsa.[ch]`) will not be visible to 321*d289c2baSAndroid Build Coastguard Workerapplication code. 322*d289c2baSAndroid Build Coastguard Worker 323*d289c2baSAndroid Build Coastguard Worker## Versioning and Compatibility 324*d289c2baSAndroid Build Coastguard Worker 325*d289c2baSAndroid Build Coastguard WorkerAVB uses a version number with three fields - the major, minor, and 326*d289c2baSAndroid Build Coastguard Workersub version. Here's an example version number 327*d289c2baSAndroid Build Coastguard Worker 328*d289c2baSAndroid Build Coastguard Worker 1.4.3 329*d289c2baSAndroid Build Coastguard Worker ^ ^ ^ 330*d289c2baSAndroid Build Coastguard Worker | | | 331*d289c2baSAndroid Build Coastguard Worker the major version ---+ | | 332*d289c2baSAndroid Build Coastguard Worker the minor version -----+ | 333*d289c2baSAndroid Build Coastguard Worker the sub version -------+ 334*d289c2baSAndroid Build Coastguard Worker 335*d289c2baSAndroid Build Coastguard WorkerThe major version number is bumped only if compatibility is broken, 336*d289c2baSAndroid Build Coastguard Workere.g. a struct field has been removed or changed. The minor version 337*d289c2baSAndroid Build Coastguard Workernumber is bumped only if a new feature is introduced, for example a 338*d289c2baSAndroid Build Coastguard Workernew algorithm or descriptor has been added. The sub version number is 339*d289c2baSAndroid Build Coastguard Workerbumped when bugs are fixed or other changes not affecting 340*d289c2baSAndroid Build Coastguard Workercompatibility are made. 341*d289c2baSAndroid Build Coastguard Worker 342*d289c2baSAndroid Build Coastguard WorkerThe `AvbVBMetaImageHeader` struct (as defined in the 343*d289c2baSAndroid Build Coastguard Worker`avb_vbmeta_image.h`) carries the major and minor version number of 344*d289c2baSAndroid Build Coastguard Worker`libavb` required to verify the struct in question. This is stored in 345*d289c2baSAndroid Build Coastguard Workerthe `required_libavb_version_major` and 346*d289c2baSAndroid Build Coastguard Worker`required_libavb_version_minor` fields. Additionally this struct 347*d289c2baSAndroid Build Coastguard Workercontains a textual field with the version of `avbtool` used to create 348*d289c2baSAndroid Build Coastguard Workerthe struct, for example "avbtool 1.4.3" or "avbtool 1.4.3 some_board 349*d289c2baSAndroid Build Coastguard WorkerGit-4589fbec". 350*d289c2baSAndroid Build Coastguard Worker 351*d289c2baSAndroid Build Coastguard WorkerNote that it's entirely possible to have a `AvbVBMetaImageHeader` 352*d289c2baSAndroid Build Coastguard Workerstruct with 353*d289c2baSAndroid Build Coastguard Worker 354*d289c2baSAndroid Build Coastguard Worker required_libavb_version_major = 1 355*d289c2baSAndroid Build Coastguard Worker required_libavb_version_minor = 0 356*d289c2baSAndroid Build Coastguard Worker avbtool_release_string = "avbtool 1.4.3" 357*d289c2baSAndroid Build Coastguard Worker 358*d289c2baSAndroid Build Coastguard Workerif, for example, creating an image that does not use any features 359*d289c2baSAndroid Build Coastguard Workeradded after AVB version 1.0. 360*d289c2baSAndroid Build Coastguard Worker 361*d289c2baSAndroid Build Coastguard Worker## Adding New Features 362*d289c2baSAndroid Build Coastguard Worker 363*d289c2baSAndroid Build Coastguard WorkerIf adding a new feature for example a new algorithm or a new 364*d289c2baSAndroid Build Coastguard Workerdescriptor then `AVB_VERSION_MINOR` in `avb_version.h` and `avbtool` 365*d289c2baSAndroid Build Coastguard Workermust be bumped and `AVB_VERSION_SUB` should be set to zero. 366*d289c2baSAndroid Build Coastguard Worker 367*d289c2baSAndroid Build Coastguard WorkerUnit tests **MUST** be added to check that 368*d289c2baSAndroid Build Coastguard Worker 369*d289c2baSAndroid Build Coastguard Worker* The feature is used if - and only if - suitable commands/options are 370*d289c2baSAndroid Build Coastguard Worker passed to `avbtool`. 371*d289c2baSAndroid Build Coastguard Worker* The `required_version_minor` field is set to the bumped value if - 372*d289c2baSAndroid Build Coastguard Worker and only if - the feature is used. Also add tests to check that the 373*d289c2baSAndroid Build Coastguard Worker correct value is output when `--print_required_libavb_version` is 374*d289c2baSAndroid Build Coastguard Worker used. 375*d289c2baSAndroid Build Coastguard Worker 376*d289c2baSAndroid Build Coastguard WorkerIf `AVB_VERSION_MINOR` has already been bumped since the last release 377*d289c2baSAndroid Build Coastguard Workerthere is obviously no need to bump it again. 378*d289c2baSAndroid Build Coastguard Worker 379*d289c2baSAndroid Build Coastguard Worker## Using avbtool 380*d289c2baSAndroid Build Coastguard Worker 381*d289c2baSAndroid Build Coastguard WorkerThe content for the vbmeta partition can be generated as follows: 382*d289c2baSAndroid Build Coastguard Worker 383*d289c2baSAndroid Build Coastguard Worker $ avbtool make_vbmeta_image \ 384*d289c2baSAndroid Build Coastguard Worker [--output OUTPUT] \ 385*d289c2baSAndroid Build Coastguard Worker [--algorithm ALGORITHM] [--key /path/to/key_used_for_signing_or_pub_key] \ 386*d289c2baSAndroid Build Coastguard Worker [--public_key_metadata /path/to/pkmd.bin] \ 387*d289c2baSAndroid Build Coastguard Worker [--rollback_index NUMBER] [--rollback_index_location NUMBER] \ 388*d289c2baSAndroid Build Coastguard Worker [--include_descriptors_from_image /path/to/image.bin] \ 389*d289c2baSAndroid Build Coastguard Worker [--setup_rootfs_from_kernel /path/to/image.bin] \ 390*d289c2baSAndroid Build Coastguard Worker [--chain_partition part_name:rollback_index_location:/path/to/key1.bin] \ 391*d289c2baSAndroid Build Coastguard Worker [--chain_partition_do_not_use_ab part_name:rollback_index_location:/path/to/key.bin] \ 392*d289c2baSAndroid Build Coastguard Worker [--signing_helper /path/to/external/signer] \ 393*d289c2baSAndroid Build Coastguard Worker [--signing_helper_with_files /path/to/external/signer_with_files] \ 394*d289c2baSAndroid Build Coastguard Worker [--print_required_libavb_version] \ 395*d289c2baSAndroid Build Coastguard Worker [--append_to_release_string STR] 396*d289c2baSAndroid Build Coastguard Worker 397*d289c2baSAndroid Build Coastguard WorkerAn integrity footer containing the hash for an entire partition can be 398*d289c2baSAndroid Build Coastguard Workeradded to an existing image as follows: 399*d289c2baSAndroid Build Coastguard Worker 400*d289c2baSAndroid Build Coastguard Worker $ avbtool add_hash_footer \ 401*d289c2baSAndroid Build Coastguard Worker --partition_name PARTNAME --partition_size SIZE \ 402*d289c2baSAndroid Build Coastguard Worker [--image IMAGE] \ 403*d289c2baSAndroid Build Coastguard Worker [--algorithm ALGORITHM] [--key /path/to/key_used_for_signing_or_pub_key] \ 404*d289c2baSAndroid Build Coastguard Worker [--public_key_metadata /path/to/pkmd.bin] \ 405*d289c2baSAndroid Build Coastguard Worker [--rollback_index NUMBER] [--rollback_index_location NUMBER] \ 406*d289c2baSAndroid Build Coastguard Worker [--hash_algorithm HASH_ALG] [--salt HEX] \ 407*d289c2baSAndroid Build Coastguard Worker [--include_descriptors_from_image /path/to/image.bin] \ 408*d289c2baSAndroid Build Coastguard Worker [--setup_rootfs_from_kernel /path/to/image.bin] \ 409*d289c2baSAndroid Build Coastguard Worker [--output_vbmeta_image OUTPUT_IMAGE] [--do_not_append_vbmeta_image] \ 410*d289c2baSAndroid Build Coastguard Worker [--signing_helper /path/to/external/signer] \ 411*d289c2baSAndroid Build Coastguard Worker [--signing_helper_with_files /path/to/external/signer_with_files] \ 412*d289c2baSAndroid Build Coastguard Worker [--print_required_libavb_version] \ 413*d289c2baSAndroid Build Coastguard Worker [--append_to_release_string STR] \ 414*d289c2baSAndroid Build Coastguard Worker [--calc_max_image_size] \ 415*d289c2baSAndroid Build Coastguard Worker [--do_not_use_ab] \ 416*d289c2baSAndroid Build Coastguard Worker [--use_persistent_digest] 417*d289c2baSAndroid Build Coastguard Worker 418*d289c2baSAndroid Build Coastguard WorkerValid values for `HASH_ALG` above include `sha1` and `sha256`. 419*d289c2baSAndroid Build Coastguard Worker 420*d289c2baSAndroid Build Coastguard WorkerAn integrity footer containing the root digest and salt for a hashtree 421*d289c2baSAndroid Build Coastguard Workerfor a partition can be added to an existing image as follows. The 422*d289c2baSAndroid Build Coastguard Workerhashtree is also appended to the image. 423*d289c2baSAndroid Build Coastguard Worker 424*d289c2baSAndroid Build Coastguard Worker $ avbtool add_hashtree_footer \ 425*d289c2baSAndroid Build Coastguard Worker --partition_name PARTNAME --partition_size SIZE \ 426*d289c2baSAndroid Build Coastguard Worker [--image IMAGE] \ 427*d289c2baSAndroid Build Coastguard Worker [--algorithm ALGORITHM] [--key /path/to/key_used_for_signing_or_pub_key] \ 428*d289c2baSAndroid Build Coastguard Worker [--public_key_metadata /path/to/pkmd.bin] \ 429*d289c2baSAndroid Build Coastguard Worker [--rollback_index NUMBER] [--rollback_index_location NUMBER] \ 430*d289c2baSAndroid Build Coastguard Worker [--hash_algorithm HASH_ALG] [--salt HEX] [--block_size SIZE] \ 431*d289c2baSAndroid Build Coastguard Worker [--include_descriptors_from_image /path/to/image.bin] \ 432*d289c2baSAndroid Build Coastguard Worker [--setup_rootfs_from_kernel /path/to/image.bin] \ 433*d289c2baSAndroid Build Coastguard Worker [--setup_as_rootfs_from_kernel] \ 434*d289c2baSAndroid Build Coastguard Worker [--output_vbmeta_image OUTPUT_IMAGE] [--do_not_append_vbmeta_image] \ 435*d289c2baSAndroid Build Coastguard Worker [--do_not_generate_fec] [--fec_num_roots FEC_NUM_ROOTS] \ 436*d289c2baSAndroid Build Coastguard Worker [--signing_helper /path/to/external/signer] \ 437*d289c2baSAndroid Build Coastguard Worker [--signing_helper_with_files /path/to/external/signer_with_files] \ 438*d289c2baSAndroid Build Coastguard Worker [--print_required_libavb_version] \ 439*d289c2baSAndroid Build Coastguard Worker [--append_to_release_string STR] \ 440*d289c2baSAndroid Build Coastguard Worker [--calc_max_image_size] \ 441*d289c2baSAndroid Build Coastguard Worker [--do_not_use_ab] \ 442*d289c2baSAndroid Build Coastguard Worker [--no_hashtree] \ 443*d289c2baSAndroid Build Coastguard Worker [--use_persistent_digest] \ 444*d289c2baSAndroid Build Coastguard Worker [--check_at_most_once] 445*d289c2baSAndroid Build Coastguard Worker 446*d289c2baSAndroid Build Coastguard WorkerValid values for `HASH_ALG` above include `sha1`, `sha256`, and `blake2b-256`. 447*d289c2baSAndroid Build Coastguard Worker 448*d289c2baSAndroid Build Coastguard WorkerThe size of an image with integrity footers can be changed using the 449*d289c2baSAndroid Build Coastguard Worker`resize_image` command: 450*d289c2baSAndroid Build Coastguard Worker 451*d289c2baSAndroid Build Coastguard Worker $ avbtool resize_image \ 452*d289c2baSAndroid Build Coastguard Worker --image IMAGE \ 453*d289c2baSAndroid Build Coastguard Worker --partition_size SIZE 454*d289c2baSAndroid Build Coastguard Worker 455*d289c2baSAndroid Build Coastguard WorkerThe integrity footer on an image can be removed from an image. The 456*d289c2baSAndroid Build Coastguard Workerhashtree can optionally be kept in place. 457*d289c2baSAndroid Build Coastguard Worker 458*d289c2baSAndroid Build Coastguard Worker $ avbtool erase_footer --image IMAGE [--keep_hashtree] 459*d289c2baSAndroid Build Coastguard Worker 460*d289c2baSAndroid Build Coastguard WorkerFor hash- and hashtree-images the vbmeta struct can also be written to 461*d289c2baSAndroid Build Coastguard Workeran external file via the `--output_vbmeta_image` option and one can 462*d289c2baSAndroid Build Coastguard Workeralso specify that the vbmeta struct and footer not be added to the 463*d289c2baSAndroid Build Coastguard Workerimage being operated on. 464*d289c2baSAndroid Build Coastguard Worker 465*d289c2baSAndroid Build Coastguard WorkerThe hashtree and FEC data in an image can be zeroed out with the following 466*d289c2baSAndroid Build Coastguard Workercommand: 467*d289c2baSAndroid Build Coastguard Worker 468*d289c2baSAndroid Build Coastguard Worker $ avbtool zero_hashtree --image IMAGE 469*d289c2baSAndroid Build Coastguard Worker 470*d289c2baSAndroid Build Coastguard WorkerThis is useful for trading compressed image size for having to reculculate the 471*d289c2baSAndroid Build Coastguard Workerhashtree and FEC at runtime. If this is done the hashtree and FEC data is set 472*d289c2baSAndroid Build Coastguard Workerto zero except for the first eight bytes which are set to the magic 473*d289c2baSAndroid Build Coastguard Worker`ZeRoHaSH`. Either the hashtree or FEC data or both may be zeroed this way 474*d289c2baSAndroid Build Coastguard Workerso applications should check for the magic both places. Applications can 475*d289c2baSAndroid Build Coastguard Workeruse the magic to detect if recalculation is needed. 476*d289c2baSAndroid Build Coastguard Worker 477*d289c2baSAndroid Build Coastguard WorkerTo calculate the maximum size of an image that will fit in a partition 478*d289c2baSAndroid Build Coastguard Workerof a given size after having used the `avbtool add_hash_footer` or 479*d289c2baSAndroid Build Coastguard Worker`avbtool add_hashtree_footer` commands on it, use the 480*d289c2baSAndroid Build Coastguard Worker`--calc_max_image_size` option: 481*d289c2baSAndroid Build Coastguard Worker 482*d289c2baSAndroid Build Coastguard Worker $ avbtool add_hash_footer --partition_size $((10*1024*1024)) \ 483*d289c2baSAndroid Build Coastguard Worker --calc_max_image_size 484*d289c2baSAndroid Build Coastguard Worker 10416128 485*d289c2baSAndroid Build Coastguard Worker 486*d289c2baSAndroid Build Coastguard Worker $ avbtool add_hashtree_footer --partition_size $((10*1024*1024)) \ 487*d289c2baSAndroid Build Coastguard Worker --calc_max_image_size 488*d289c2baSAndroid Build Coastguard Worker 10330112 489*d289c2baSAndroid Build Coastguard Worker 490*d289c2baSAndroid Build Coastguard WorkerTo calculate the required libavb version that would be put in the 491*d289c2baSAndroid Build Coastguard Workervbmeta struct when using `make_vbmeta_image`, `add_hash_footer`, and 492*d289c2baSAndroid Build Coastguard Worker`add_hashtree_footer` commands use the 493*d289c2baSAndroid Build Coastguard Worker`--print_required_libavb_version` option: 494*d289c2baSAndroid Build Coastguard Worker 495*d289c2baSAndroid Build Coastguard Worker $ avbtool make_vbmeta_image \ 496*d289c2baSAndroid Build Coastguard Worker --algorithm SHA256_RSA2048 --key /path/to/key.pem \ 497*d289c2baSAndroid Build Coastguard Worker --include_descriptors_from_image /path/to/boot.img \ 498*d289c2baSAndroid Build Coastguard Worker --include_descriptors_from_image /path/to/system.img \ 499*d289c2baSAndroid Build Coastguard Worker --print_required_libavb_version 500*d289c2baSAndroid Build Coastguard Worker 1.0 501*d289c2baSAndroid Build Coastguard Worker 502*d289c2baSAndroid Build Coastguard WorkerAlternatively, `--no_hashtree` can be used with `avbtool add_hashtree_footer` 503*d289c2baSAndroid Build Coastguard Workercommand. If `--no_hashtree` is given, the hashtree blob is omitted and only 504*d289c2baSAndroid Build Coastguard Workerits descriptor is added to the vbmeta struct. The descriptor says the size 505*d289c2baSAndroid Build Coastguard Workerof hashtree is 0, which tells an application the need to recalculate 506*d289c2baSAndroid Build Coastguard Workerhashtree. 507*d289c2baSAndroid Build Coastguard Worker 508*d289c2baSAndroid Build Coastguard WorkerThe `--signing_helper` option can be used in `make_vbmeta_image`, 509*d289c2baSAndroid Build Coastguard Worker`add_hash_footer` and `add_hashtree_footer` commands to specify any 510*d289c2baSAndroid Build Coastguard Workerexternal program for signing hashes. The data to sign (including 511*d289c2baSAndroid Build Coastguard Workerpadding e.g. PKCS1-v1.5) is fed via `STDIN` and the signed data is 512*d289c2baSAndroid Build Coastguard Workerreturned via `STDOUT`. If `--signing_helper` is present in a command 513*d289c2baSAndroid Build Coastguard Workerline, the `--key` option need only contain a public key. Arguments for 514*d289c2baSAndroid Build Coastguard Workera signing helper are `algorithm` and `public key`. If the signing 515*d289c2baSAndroid Build Coastguard Workerhelper exits with a non-zero exit code, it means failure. 516*d289c2baSAndroid Build Coastguard Worker 517*d289c2baSAndroid Build Coastguard WorkerHere's an example invocation: 518*d289c2baSAndroid Build Coastguard Worker 519*d289c2baSAndroid Build Coastguard Worker /path/to/my_signing_program SHA256_RSA2048 /path/to/publickey.pem 520*d289c2baSAndroid Build Coastguard Worker 521*d289c2baSAndroid Build Coastguard WorkerThe `--signing_helper_with_files` is similar to `--signing_helper` 522*d289c2baSAndroid Build Coastguard Workerexcept that a temporary file is used to communicate with the helper 523*d289c2baSAndroid Build Coastguard Workerinstead of `STDIN` and `STDOUT`. This is useful in situations where 524*d289c2baSAndroid Build Coastguard Workerthe signing helper is using code which is outputting diagnostics on 525*d289c2baSAndroid Build Coastguard Worker`STDOUT` instead of `STDERR`. Here's an example invocation 526*d289c2baSAndroid Build Coastguard Worker 527*d289c2baSAndroid Build Coastguard Worker /path/to/my_signing_program_with_files SHA256_RSA2048 \ 528*d289c2baSAndroid Build Coastguard Worker /path/to/publickey.pem /tmp/path/to/communication_file 529*d289c2baSAndroid Build Coastguard Worker 530*d289c2baSAndroid Build Coastguard Workerwhere the last positional argument is a file that contains the data to 531*d289c2baSAndroid Build Coastguard Workersign. The helper should write the signature in this file. 532*d289c2baSAndroid Build Coastguard Worker 533*d289c2baSAndroid Build Coastguard WorkerThe `append_vbmeta_image` command can be used to append an entire 534*d289c2baSAndroid Build Coastguard Workervbmeta blob to the end of another image. This is useful for cases when 535*d289c2baSAndroid Build Coastguard Workernot using any vbmeta partitions, for example: 536*d289c2baSAndroid Build Coastguard Worker 537*d289c2baSAndroid Build Coastguard Worker $ cp boot.img boot-with-vbmeta-appended.img 538*d289c2baSAndroid Build Coastguard Worker $ avbtool append_vbmeta_image \ 539*d289c2baSAndroid Build Coastguard Worker --image boot-with-vbmeta-appended.img \ 540*d289c2baSAndroid Build Coastguard Worker --partition_size SIZE_OF_BOOT_PARTITION \ 541*d289c2baSAndroid Build Coastguard Worker --vbmeta_image vbmeta.img 542*d289c2baSAndroid Build Coastguard Worker $ fastboot flash boot boot-with-vbmeta-appended.img 543*d289c2baSAndroid Build Coastguard Worker 544*d289c2baSAndroid Build Coastguard WorkerInformation about an image can be obtained using the `info_image` command. The 545*d289c2baSAndroid Build Coastguard Workeroutput of this command should not be relied on and the way information is 546*d289c2baSAndroid Build Coastguard Workerstructured may change. 547*d289c2baSAndroid Build Coastguard Worker 548*d289c2baSAndroid Build Coastguard WorkerThe `verify_image` command can be used to verify the contents of 549*d289c2baSAndroid Build Coastguard Workerseveral image files at the same time. When invoked on an image the 550*d289c2baSAndroid Build Coastguard Workerfollowing checks are performed: 551*d289c2baSAndroid Build Coastguard Worker 552*d289c2baSAndroid Build Coastguard Worker* If the image has a VBMeta struct the signature is checked against 553*d289c2baSAndroid Build Coastguard Worker the embedded public key. If the image doesn't look like `vbmeta.img` 554*d289c2baSAndroid Build Coastguard Worker then a footer is looked for and used if present. 555*d289c2baSAndroid Build Coastguard Worker 556*d289c2baSAndroid Build Coastguard Worker* If the option `--key` is passed then a `.pem` file is expected and 557*d289c2baSAndroid Build Coastguard Worker it's checked that the embedded public key in said VBMeta struct 558*d289c2baSAndroid Build Coastguard Worker matches the given key. 559*d289c2baSAndroid Build Coastguard Worker 560*d289c2baSAndroid Build Coastguard Worker* All descriptors in the VBMeta struct are checked in the following 561*d289c2baSAndroid Build Coastguard Worker way: 562*d289c2baSAndroid Build Coastguard Worker + For a hash descriptor the image file corresponding to the 563*d289c2baSAndroid Build Coastguard Worker partition name is loaded and its digest is checked against that 564*d289c2baSAndroid Build Coastguard Worker in the descriptor. 565*d289c2baSAndroid Build Coastguard Worker + For a hashtree descriptor the image file corresponding to the 566*d289c2baSAndroid Build Coastguard Worker partition name is loaded and the hashtree is calculated and its 567*d289c2baSAndroid Build Coastguard Worker root digest compared to that in the descriptor. 568*d289c2baSAndroid Build Coastguard Worker + For a chained partition descriptor its contents is compared 569*d289c2baSAndroid Build Coastguard Worker against content that needs to be passed in via the 570*d289c2baSAndroid Build Coastguard Worker `--expected_chain_partition` options. The format for this option 571*d289c2baSAndroid Build Coastguard Worker is similar to that of the `--chain_partition` option. If there 572*d289c2baSAndroid Build Coastguard Worker is no `--expected_chain_partition` descriptor for the chain 573*d289c2baSAndroid Build Coastguard Worker partition descriptor the check fails. 574*d289c2baSAndroid Build Coastguard Worker 575*d289c2baSAndroid Build Coastguard WorkerHere's an example for a setup where the digests for `boot.img` and 576*d289c2baSAndroid Build Coastguard Worker`system.img` are stored in `vbmeta.img` which is signed with 577*d289c2baSAndroid Build Coastguard Worker`my_key.pem`. It also checks that the chain partition for partition 578*d289c2baSAndroid Build Coastguard Worker`foobar` uses rollback index 8 and that the public key in AVB format 579*d289c2baSAndroid Build Coastguard Workermatches that of the file `foobar_vendor_key.avbpubkey`: 580*d289c2baSAndroid Build Coastguard Worker 581*d289c2baSAndroid Build Coastguard Worker $ avbtool verify_image \ 582*d289c2baSAndroid Build Coastguard Worker --image /path/to/vbmeta.img \ 583*d289c2baSAndroid Build Coastguard Worker --key my_key.pem \ 584*d289c2baSAndroid Build Coastguard Worker --expect_chained_partition foobar:8:foobar_vendor_key.avbpubkey 585*d289c2baSAndroid Build Coastguard Worker 586*d289c2baSAndroid Build Coastguard Worker Verifying image /path/to/vbmeta.img using key at my_key.pem 587*d289c2baSAndroid Build Coastguard Worker vbmeta: Successfully verified SHA256_RSA4096 vbmeta struct in /path_to/vbmeta.img 588*d289c2baSAndroid Build Coastguard Worker boot: Successfully verified sha256 hash of /path/to/boot.img for image of 10543104 bytes 589*d289c2baSAndroid Build Coastguard Worker system: Successfully verified sha1 hashtree of /path/to/system.img for image of 1065213952 bytes 590*d289c2baSAndroid Build Coastguard Worker foobar: Successfully verified chain partition descriptor matches expected data 591*d289c2baSAndroid Build Coastguard Worker 592*d289c2baSAndroid Build Coastguard WorkerIn this example the `verify_image` command verifies the files 593*d289c2baSAndroid Build Coastguard Worker`vbmeta.img`, `boot.img`, and `system.img` in the directory 594*d289c2baSAndroid Build Coastguard Worker`/path/to`. The directory and file extension of the given image 595*d289c2baSAndroid Build Coastguard Worker(e.g. `/path/to/vbmeta.img`) is used together with the partition name 596*d289c2baSAndroid Build Coastguard Workerin the descriptor to calculate the filenames of the images holding 597*d289c2baSAndroid Build Coastguard Workerhash and hashtree images. 598*d289c2baSAndroid Build Coastguard Worker 599*d289c2baSAndroid Build Coastguard WorkerThe `verify_image` command can also be used to check that a custom 600*d289c2baSAndroid Build Coastguard Workersigning helper works as intended. 601*d289c2baSAndroid Build Coastguard Worker 602*d289c2baSAndroid Build Coastguard WorkerThe `calculate_vbmeta_digest` command can be used to calculate the vbmeta digest 603*d289c2baSAndroid Build Coastguard Workerof several image files at the same time. The result is printed as a hexadecimal 604*d289c2baSAndroid Build Coastguard Workerstring either on `STDOUT` or a supplied path (using the `--output` option). 605*d289c2baSAndroid Build Coastguard Worker 606*d289c2baSAndroid Build Coastguard Worker $ avbtool calculate_vbmeta_digest \ 607*d289c2baSAndroid Build Coastguard Worker --hash_algorithm sha256 \ 608*d289c2baSAndroid Build Coastguard Worker --image /path/to/vbmeta.img 609*d289c2baSAndroid Build Coastguard Worker a20fdd01a6638c55065fe08497186acde350d6797d59a55d70ffbcf41e95c2f5 610*d289c2baSAndroid Build Coastguard Worker 611*d289c2baSAndroid Build Coastguard WorkerIn this example the `calculate_vbmeta_digest` command loads the `vbmeta.img` 612*d289c2baSAndroid Build Coastguard Workerfile. If this image has one or more chain partition descriptors, the same logic 613*d289c2baSAndroid Build Coastguard Workeras the `verify_image` command is used to load files for these (e.g. it assumes 614*d289c2baSAndroid Build Coastguard Workerthe same directory and file extension as the given image). Once all vbmeta 615*d289c2baSAndroid Build Coastguard Workerstructs have been loaded, the digest is calculated (using the hash algorithm 616*d289c2baSAndroid Build Coastguard Workergiven by the `--hash_algorithm` option) and printed out. 617*d289c2baSAndroid Build Coastguard Worker 618*d289c2baSAndroid Build Coastguard WorkerTo print hash and hashtree digests embedded in the verified metadata, use the 619*d289c2baSAndroid Build Coastguard Worker`print_partition_digests` command like this: 620*d289c2baSAndroid Build Coastguard Worker 621*d289c2baSAndroid Build Coastguard Worker $ avbtool print_partition_digests --image /path/to/vbmeta.img 622*d289c2baSAndroid Build Coastguard Worker system: ddaa513715fd2e22f3c1cea3c1a1f98ccb515fc6 623*d289c2baSAndroid Build Coastguard Worker boot: 5cba9a418e04b5f9e29ee6a250f6cdbe30c6cec867c59d388f141c3fedcb28c1 624*d289c2baSAndroid Build Coastguard Worker vendor: 06993a9e85e46e53d3892881bb75eff48ecadaa8 625*d289c2baSAndroid Build Coastguard Worker 626*d289c2baSAndroid Build Coastguard WorkerFor partitions with hash descriptors, this prints out the digest and for 627*d289c2baSAndroid Build Coastguard Workerpartitions with hashtree descriptors the root digest is printed out. Like the 628*d289c2baSAndroid Build Coastguard Worker`calculate_vbmeta_digest` and `verify_image` commands, chain partitions are 629*d289c2baSAndroid Build Coastguard Workerfollowed. To use JSON for the output, use the `--json` option. 630*d289c2baSAndroid Build Coastguard Worker 631*d289c2baSAndroid Build Coastguard WorkerIn case you would like to log all command lines for all avbtool invocations for 632*d289c2baSAndroid Build Coastguard Workerdebugging integrations with other tooling, you can configure the envirionment 633*d289c2baSAndroid Build Coastguard Workervariable AVB_INVOCATION_LOGFILE with the name of the log file: 634*d289c2baSAndroid Build Coastguard Worker 635*d289c2baSAndroid Build Coastguard Worker $ export AVB_INVOCATION_LOGFILE='/tmp/avb_invocation.log' 636*d289c2baSAndroid Build Coastguard Worker $ ./avbtool.py version 637*d289c2baSAndroid Build Coastguard Worker $ ./avbtool.py version 638*d289c2baSAndroid Build Coastguard Worker $ cat /tmp/avb_invocation.log 639*d289c2baSAndroid Build Coastguard Worker ./avbtool.py version 640*d289c2baSAndroid Build Coastguard Worker ./avbtool.py version 641*d289c2baSAndroid Build Coastguard Worker 642*d289c2baSAndroid Build Coastguard Worker 643*d289c2baSAndroid Build Coastguard Worker## Build System Integration 644*d289c2baSAndroid Build Coastguard Worker 645*d289c2baSAndroid Build Coastguard WorkerIn Android, AVB is enabled by the `BOARD_AVB_ENABLE` variable 646*d289c2baSAndroid Build Coastguard Worker 647*d289c2baSAndroid Build Coastguard Worker BOARD_AVB_ENABLE := true 648*d289c2baSAndroid Build Coastguard Worker 649*d289c2baSAndroid Build Coastguard WorkerThis will make the build system create `vbmeta.img` which will contain 650*d289c2baSAndroid Build Coastguard Workera hash descriptor for `boot.img`, a hashtree descriptor for 651*d289c2baSAndroid Build Coastguard Worker`system.img`, a kernel-cmdline descriptor for setting up `dm-verity` 652*d289c2baSAndroid Build Coastguard Workerfor `system.img` and append a hash-tree to `system.img`. If the build 653*d289c2baSAndroid Build Coastguard Workersystem is set up such that one or many of `vendor.img` / `product.img` 654*d289c2baSAndroid Build Coastguard Worker/ `system_ext.img` / `odm.img` are being built, the hash-tree for each 655*d289c2baSAndroid Build Coastguard Workerof them will also be appended to the image respectively, and their 656*d289c2baSAndroid Build Coastguard Workerhash-tree descriptors will be included into `vbmeta.img` accordingly. 657*d289c2baSAndroid Build Coastguard Worker 658*d289c2baSAndroid Build Coastguard WorkerBy default, the algorithm `SHA256_RSA4096` is used with a test key 659*d289c2baSAndroid Build Coastguard Workerfrom the `external/avb/test/data` directory. This can be overriden by 660*d289c2baSAndroid Build Coastguard Workerthe `BOARD_AVB_ALGORITHM` and `BOARD_AVB_KEY_PATH` variables to use 661*d289c2baSAndroid Build Coastguard Workere.g. a 4096-bit RSA key and SHA-512: 662*d289c2baSAndroid Build Coastguard Worker 663*d289c2baSAndroid Build Coastguard Worker BOARD_AVB_ALGORITHM := SHA512_RSA4096 664*d289c2baSAndroid Build Coastguard Worker BOARD_AVB_KEY_PATH := /path/to/rsa_key_4096bits.pem 665*d289c2baSAndroid Build Coastguard Worker 666*d289c2baSAndroid Build Coastguard WorkerRemember that the public part of this key needs to be available to the 667*d289c2baSAndroid Build Coastguard Workerbootloader of the device expected to verify resulting images. Use 668*d289c2baSAndroid Build Coastguard Worker`avbtool extract_public_key` to extract the key in the expected format 669*d289c2baSAndroid Build Coastguard Worker(`AVB_pk` in the following). If the device is using a different root 670*d289c2baSAndroid Build Coastguard Workerof trust than `AVB_pk` the `--public_key_metadata` option can be used 671*d289c2baSAndroid Build Coastguard Workerto embed a blob (`AVB_pkmd` in the following) that can be used to 672*d289c2baSAndroid Build Coastguard Workere.g. derive `AVB_pk`. Both `AVB_pk` and `AVB_pkmd` are passed to the 673*d289c2baSAndroid Build Coastguard Worker`validate_vbmeta_public_key()` operation when verifying a slot. 674*d289c2baSAndroid Build Coastguard Worker 675*d289c2baSAndroid Build Coastguard WorkerSome devices may support the end-user configuring the root of trust to use, see 676*d289c2baSAndroid Build Coastguard Workerthe [Device Specific Notes](#device-specific-notes) section for details. 677*d289c2baSAndroid Build Coastguard Worker 678*d289c2baSAndroid Build Coastguard WorkerDevices can be configured to create additional `vbmeta` partitions as 679*d289c2baSAndroid Build Coastguard Worker[chained partitions](#the-vbmeta-struct) in order to update a subset of 680*d289c2baSAndroid Build Coastguard Workerpartitions without changing the top-level `vbmeta` partition. For example, 681*d289c2baSAndroid Build Coastguard Workerthe following variables create `vbmeta_system.img` as a chained `vbmeta` 682*d289c2baSAndroid Build Coastguard Workerimage that contains the hash-tree descriptors for `system.img`, `system_ext.img` 683*d289c2baSAndroid Build Coastguard Workerand `product.img`. `vbmeta_system.img` itself will be signed by the specified 684*d289c2baSAndroid Build Coastguard Workerkey and algorithm. 685*d289c2baSAndroid Build Coastguard Worker 686*d289c2baSAndroid Build Coastguard Worker BOARD_AVB_VBMETA_SYSTEM := system system_ext product 687*d289c2baSAndroid Build Coastguard Worker BOARD_AVB_VBMETA_SYSTEM_KEY_PATH := external/avb/test/data/testkey_rsa2048.pem 688*d289c2baSAndroid Build Coastguard Worker BOARD_AVB_VBMETA_SYSTEM_ALGORITHM := SHA256_RSA2048 689*d289c2baSAndroid Build Coastguard Worker BOARD_AVB_VBMETA_SYSTEM_ROLLBACK_INDEX_LOCATION := 1 690*d289c2baSAndroid Build Coastguard Worker 691*d289c2baSAndroid Build Coastguard WorkerNote that the hash-tree descriptors for `system.img`, `system_ext.img` and 692*d289c2baSAndroid Build Coastguard Worker`product.img` will be included only in `vbmeta_system.img`, but not 693*d289c2baSAndroid Build Coastguard Worker`vbmeta.img`. With the above setup, partitions `system.img`, `system_ext.img`, 694*d289c2baSAndroid Build Coastguard Worker`product.img` and `vbmeta_system.img` can be updated independently - but as a 695*d289c2baSAndroid Build Coastguard Workergroup - of the rest of the partitions, *or* as part of the traditional updates 696*d289c2baSAndroid Build Coastguard Workerthat update all the partitions. 697*d289c2baSAndroid Build Coastguard Worker 698*d289c2baSAndroid Build Coastguard WorkerCurrently build system supports building chained `vbmeta` images of 699*d289c2baSAndroid Build Coastguard Worker`vbmeta_system.img` (`BOARD_AVB_VBMETA_SYSTEM`) and `vbmeta_vendor.img` 700*d289c2baSAndroid Build Coastguard Worker(`BOARD_AVB_VBMETA_VENDOR`). 701*d289c2baSAndroid Build Coastguard Worker 702*d289c2baSAndroid Build Coastguard WorkerTo prevent rollback attacks, the rollback index should be increased on 703*d289c2baSAndroid Build Coastguard Workera regular basis. The rollback index can be set with the 704*d289c2baSAndroid Build Coastguard Worker`BOARD_AVB_ROLLBACK_INDEX` variable: 705*d289c2baSAndroid Build Coastguard Worker 706*d289c2baSAndroid Build Coastguard Worker BOARD_AVB_ROLLBACK_INDEX := 5 707*d289c2baSAndroid Build Coastguard Worker 708*d289c2baSAndroid Build Coastguard WorkerIf this is not set, the rollback index defaults to 0. 709*d289c2baSAndroid Build Coastguard Worker 710*d289c2baSAndroid Build Coastguard WorkerThe variable `BOARD_AVB_MAKE_VBMETA_IMAGE_ARGS` can be used to specify 711*d289c2baSAndroid Build Coastguard Workeradditional options passed to `avbtool make_vbmeta_image`. Typical 712*d289c2baSAndroid Build Coastguard Workeroptions to be used here include `--prop`, `--prop_from_file`, 713*d289c2baSAndroid Build Coastguard Worker`--chain_partition`, `--public_key_metadata`, and `--signing_helper`. 714*d289c2baSAndroid Build Coastguard Worker 715*d289c2baSAndroid Build Coastguard WorkerThe variable `BOARD_AVB_BOOT_ADD_HASH_FOOTER_ARGS` can be used to 716*d289c2baSAndroid Build Coastguard Workerspecify additional options passed to `avbtool add_hash_footer` for 717*d289c2baSAndroid Build Coastguard Worker`boot.img`. Typical options to be used here include `--hash_algorithm` 718*d289c2baSAndroid Build Coastguard Workerand `--salt`. 719*d289c2baSAndroid Build Coastguard Worker 720*d289c2baSAndroid Build Coastguard WorkerThe variable `BOARD_AVB_SYSTEM_ADD_HASHTREE_FOOTER_ARGS` can be used 721*d289c2baSAndroid Build Coastguard Workerto specify additional options passed to `avbtool add_hashtree_footer` 722*d289c2baSAndroid Build Coastguard Workerfor `system.img`. Typical options to be used here include 723*d289c2baSAndroid Build Coastguard Worker`--hash_algorithm`, `--salt`, `--block_size`, and 724*d289c2baSAndroid Build Coastguard Worker`--do_not_generate_fec`. 725*d289c2baSAndroid Build Coastguard Worker 726*d289c2baSAndroid Build Coastguard WorkerThe variable `BOARD_AVB_VENDOR_ADD_HASHTREE_FOOTER_ARGS` can be used 727*d289c2baSAndroid Build Coastguard Workerto specify additional options passed to `avbtool add_hashtree_footer` 728*d289c2baSAndroid Build Coastguard Workerfor `vendor.img`. Typical options to be used here include 729*d289c2baSAndroid Build Coastguard Worker`--hash_algorithm`, `--salt`, `--block_size`, and 730*d289c2baSAndroid Build Coastguard Worker`--do_not_generate_fec`. 731*d289c2baSAndroid Build Coastguard Worker 732*d289c2baSAndroid Build Coastguard WorkerThe variable `BOARD_AVB_DTBO_ADD_HASH_FOOTER_ARGS` can be used to 733*d289c2baSAndroid Build Coastguard Workerspecify additional options passed to `avbtool add_hash_footer` for 734*d289c2baSAndroid Build Coastguard Worker`dtbo.img`. Typical options to be used here include `--hash_algorithm` 735*d289c2baSAndroid Build Coastguard Workerand `--salt`. 736*d289c2baSAndroid Build Coastguard Worker 737*d289c2baSAndroid Build Coastguard WorkerBuild system variables (such as `PRODUCT_SUPPORTS_VERITY_FEC`) used 738*d289c2baSAndroid Build Coastguard Workerfor previous version of Verified Boot in Android are not used in AVB. 739*d289c2baSAndroid Build Coastguard Worker 740*d289c2baSAndroid Build Coastguard WorkerA/B related build system variables can be found [here](https://source.android.com/devices/tech/ota/ab_updates#build-variables). 741*d289c2baSAndroid Build Coastguard Worker 742*d289c2baSAndroid Build Coastguard Worker# Device Integration 743*d289c2baSAndroid Build Coastguard Worker 744*d289c2baSAndroid Build Coastguard WorkerThis section discusses recommendations and best practices for 745*d289c2baSAndroid Build Coastguard Workerintegrating `libavb` with a device boot loader. It's important to 746*d289c2baSAndroid Build Coastguard Workeremphasize that these are just recommendations so the use of the word 747*d289c2baSAndroid Build Coastguard Worker`must` should be taken lightly. 748*d289c2baSAndroid Build Coastguard Worker 749*d289c2baSAndroid Build Coastguard WorkerAdditionally term *HLOS* is used in this chapter to refer to the *High 750*d289c2baSAndroid Build Coastguard WorkerLevel Operating System*. This obviously includes Android (including 751*d289c2baSAndroid Build Coastguard Workerother form-factors than phones) but could also be other operating 752*d289c2baSAndroid Build Coastguard Workersystems. 753*d289c2baSAndroid Build Coastguard Worker 754*d289c2baSAndroid Build Coastguard Worker## System Dependencies 755*d289c2baSAndroid Build Coastguard Worker 756*d289c2baSAndroid Build Coastguard WorkerThe `libavb` library is written in a way so it's portable to any 757*d289c2baSAndroid Build Coastguard Workersystem with a C99 compiler. It does not require the standard C library 758*d289c2baSAndroid Build Coastguard Workerhowever the boot loader must implement a simple set of system 759*d289c2baSAndroid Build Coastguard Workerprimitives required by `libavb` such as `avb_malloc()`, `avb_free()`, 760*d289c2baSAndroid Build Coastguard Workerand `avb_print()`. 761*d289c2baSAndroid Build Coastguard Worker 762*d289c2baSAndroid Build Coastguard WorkerIn addition to the system primitives, `libavb` interfaces with the boot 763*d289c2baSAndroid Build Coastguard Workerloader through the supplied `AvbOps` struct. This includes operations 764*d289c2baSAndroid Build Coastguard Workerto read and write data from partitions, read and write rollback 765*d289c2baSAndroid Build Coastguard Workerindexes, check if the public key used to make a signature should be 766*d289c2baSAndroid Build Coastguard Workeraccepted, and so on. 767*d289c2baSAndroid Build Coastguard Worker 768*d289c2baSAndroid Build Coastguard Worker## Locked and Unlocked mode 769*d289c2baSAndroid Build Coastguard Worker 770*d289c2baSAndroid Build Coastguard WorkerAVB has been designed to support the notion of the device being either 771*d289c2baSAndroid Build Coastguard WorkerLOCKED state or UNLOCKED state as used in Android. 772*d289c2baSAndroid Build Coastguard Worker 773*d289c2baSAndroid Build Coastguard WorkerIn the context of AVB, the LOCKED state means that verification errors 774*d289c2baSAndroid Build Coastguard Workerare fatal whereas in UNLOCKED state they are not. If the device is 775*d289c2baSAndroid Build Coastguard WorkerUNLOCKED pass `AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR` flag in 776*d289c2baSAndroid Build Coastguard Workerthe `flags` parameter of `avb_slot_verify()` and treat verification 777*d289c2baSAndroid Build Coastguard Workererrors including 778*d289c2baSAndroid Build Coastguard Worker 779*d289c2baSAndroid Build Coastguard Worker* `AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED` 780*d289c2baSAndroid Build Coastguard Worker* `AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION` 781*d289c2baSAndroid Build Coastguard Worker* `AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX` 782*d289c2baSAndroid Build Coastguard Worker 783*d289c2baSAndroid Build Coastguard Workeras non-fatal. If the device is in the LOCKED state, don't pass the 784*d289c2baSAndroid Build Coastguard Worker`AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR` flag in the `flags` 785*d289c2baSAndroid Build Coastguard Workerparameter of `avb_slot_verify()` and only treat 786*d289c2baSAndroid Build Coastguard Worker`AVB_SLOT_VERIFY_RESULT_OK` as non-fatal. 787*d289c2baSAndroid Build Coastguard Worker 788*d289c2baSAndroid Build Coastguard WorkerOn Android, device state may be altered through the fastboot interface 789*d289c2baSAndroid Build Coastguard Workerusing, e.g. `fastboot flashing lock` (to transition to the LOCKED 790*d289c2baSAndroid Build Coastguard Workerstate) and `fastboot flashing unlock` (to transition to the UNLOCKED 791*d289c2baSAndroid Build Coastguard Workerstate). 792*d289c2baSAndroid Build Coastguard Worker 793*d289c2baSAndroid Build Coastguard WorkerThe device must only allow state transitions (e.g. from LOCKED to 794*d289c2baSAndroid Build Coastguard WorkerUNLOCKED or UNLOCKED to LOCKED) after asserting physical presence of 795*d289c2baSAndroid Build Coastguard Workerthe user. If the device has a display and buttons this is typically 796*d289c2baSAndroid Build Coastguard Workerdone by showing a dialog and requiring the user to confirm or cancel 797*d289c2baSAndroid Build Coastguard Workerusing physical buttons. 798*d289c2baSAndroid Build Coastguard Worker 799*d289c2baSAndroid Build Coastguard WorkerAll user data must be cleared when transitioning from the LOCKED to 800*d289c2baSAndroid Build Coastguard Workerthe UNLOCKED state (including the `userdata` partition and any NVRAM 801*d289c2baSAndroid Build Coastguard Workerspaces). Additionally all `stored_rollback_index[n]` locations must be 802*d289c2baSAndroid Build Coastguard Workercleared (all elements must be set to zero). Similar action (erasing 803*d289c2baSAndroid Build Coastguard Worker`userdata`, NVRAM spaces, and `stored_rollback_index[n]` locations) 804*d289c2baSAndroid Build Coastguard Workershall also happening when transitioning from UNLOCKED to LOCKED. If 805*d289c2baSAndroid Build Coastguard Workerthe device is required to use full disk encryption, then a less 806*d289c2baSAndroid Build Coastguard Workerintensive wipe is required for UNLOCKED to LOCKED. Depending on the 807*d289c2baSAndroid Build Coastguard Workerdevice form-factor and intended use, the user should be prompted to 808*d289c2baSAndroid Build Coastguard Workerconfirm before any data is erased. 809*d289c2baSAndroid Build Coastguard Worker 810*d289c2baSAndroid Build Coastguard Worker## Tamper-evident Storage 811*d289c2baSAndroid Build Coastguard Worker 812*d289c2baSAndroid Build Coastguard WorkerIn this document, *tamper-evident* means that it's possible to detect 813*d289c2baSAndroid Build Coastguard Workerif the HLOS has tampered with the data, e.g. if it has been 814*d289c2baSAndroid Build Coastguard Workeroverwritten. 815*d289c2baSAndroid Build Coastguard Worker 816*d289c2baSAndroid Build Coastguard WorkerTamper-evident storage must be used for stored rollback indexes, keys 817*d289c2baSAndroid Build Coastguard Workerused for verification, device state (whether the device is LOCKED or 818*d289c2baSAndroid Build Coastguard WorkerUNLOCKED), and named persistent values. If tampering has been detected 819*d289c2baSAndroid Build Coastguard Workerthe corresponding `AvbOps` operation should fail by e.g. returning 820*d289c2baSAndroid Build Coastguard Worker`AVB_IO_RESULT_ERROR_IO`. It is especially important that verification 821*d289c2baSAndroid Build Coastguard Workerkeys cannot be tampered with since they represent the root-of-trust. 822*d289c2baSAndroid Build Coastguard Worker 823*d289c2baSAndroid Build Coastguard WorkerIf verification keys are mutable they must only be set by the end 824*d289c2baSAndroid Build Coastguard Workeruser, e.g. it must never be set at the factory or store or any 825*d289c2baSAndroid Build Coastguard Workerintermediate point before the end user. Additionally, it must only be 826*d289c2baSAndroid Build Coastguard Workerpossible to set or clear a key while the device is in the UNLOCKED 827*d289c2baSAndroid Build Coastguard Workerstate. 828*d289c2baSAndroid Build Coastguard Worker 829*d289c2baSAndroid Build Coastguard Worker## Named Persistent Values 830*d289c2baSAndroid Build Coastguard Worker 831*d289c2baSAndroid Build Coastguard WorkerAVB 1.1 introduces support for named persistent values which must be 832*d289c2baSAndroid Build Coastguard Workertamper evident and allows AVB to store arbitrary key-value pairs. 833*d289c2baSAndroid Build Coastguard WorkerIntegrators may limit support for these values to a set of fixed 834*d289c2baSAndroid Build Coastguard Workerwell-known names, a maximum value size, and / or a maximum number of 835*d289c2baSAndroid Build Coastguard Workervalues. 836*d289c2baSAndroid Build Coastguard Worker 837*d289c2baSAndroid Build Coastguard Worker## Persistent Digests 838*d289c2baSAndroid Build Coastguard Worker 839*d289c2baSAndroid Build Coastguard WorkerUsing a persistent digest for a partition means the digest (or root 840*d289c2baSAndroid Build Coastguard Workerdigest in the case of a hashtree) is not stored in the descriptor but 841*d289c2baSAndroid Build Coastguard Workeris stored in a named persistent value. This allows configuration data 842*d289c2baSAndroid Build Coastguard Workerwhich may differ from device to device to be verified by AVB. It must 843*d289c2baSAndroid Build Coastguard Workernot be possible to modify the persistent digest when the device is in 844*d289c2baSAndroid Build Coastguard Workerthe LOCKED state, except if a digest does not exist it may be initialized. 845*d289c2baSAndroid Build Coastguard Worker 846*d289c2baSAndroid Build Coastguard WorkerTo specify that a descriptor should use a persistent digest, use the 847*d289c2baSAndroid Build Coastguard Worker`--use_persistent_digest` option for the `add_hash_footer` or 848*d289c2baSAndroid Build Coastguard Worker`add_hashtree_footer` avbtool operations. Then, during verification of 849*d289c2baSAndroid Build Coastguard Workerthe descriptor, AVB will look for the digest in the named persistent 850*d289c2baSAndroid Build Coastguard Workervalue `avb.persistent_digest.$(partition_name)` instead of in the 851*d289c2baSAndroid Build Coastguard Workerdescriptor itself. 852*d289c2baSAndroid Build Coastguard Worker 853*d289c2baSAndroid Build Coastguard WorkerFor hashtree descriptors using a persistent digest, the digest value 854*d289c2baSAndroid Build Coastguard Workerwill be available for substitution into kernel command line descriptors 855*d289c2baSAndroid Build Coastguard Workerusing a token of the form `$(AVB_FOO_ROOT_DIGEST)` where 'FOO' is the 856*d289c2baSAndroid Build Coastguard Workeruppercase partition name, in this case for the partition named 'foo'. 857*d289c2baSAndroid Build Coastguard WorkerThe token will be replaced by the digest in hexadecimal form. 858*d289c2baSAndroid Build Coastguard Worker 859*d289c2baSAndroid Build Coastguard WorkerBy default, when the `--use_persistent_digest` option is used with 860*d289c2baSAndroid Build Coastguard Worker`add_hash_footer` or `add_hashtree_footer`, avbtool will generate a 861*d289c2baSAndroid Build Coastguard Workerdescriptor with no salt rather than the typical default of generating a 862*d289c2baSAndroid Build Coastguard Workerrandom salt equal to the digest length. This is because the digest 863*d289c2baSAndroid Build Coastguard Workervalue is stored in persistent storage and thus cannot change over time. 864*d289c2baSAndroid Build Coastguard WorkerAn alternative option would be to manually provide a random salt using 865*d289c2baSAndroid Build Coastguard Worker`--salt`, but this salt would need to remain unchanged for the life 866*d289c2baSAndroid Build Coastguard Workerof the device once the persistent digest value was written. 867*d289c2baSAndroid Build Coastguard Worker 868*d289c2baSAndroid Build Coastguard Worker## Updating Stored Rollback Indexes 869*d289c2baSAndroid Build Coastguard Worker 870*d289c2baSAndroid Build Coastguard WorkerIn order for Rollback Protection to work the bootloader will need to 871*d289c2baSAndroid Build Coastguard Workerupdate the `stored_rollback_indexes[n]` array on the device prior to 872*d289c2baSAndroid Build Coastguard Workertransferring control to the HLOS. If not using A/B this is 873*d289c2baSAndroid Build Coastguard Workerstraightforward - just update it to what's in the AVB metadata for the 874*d289c2baSAndroid Build Coastguard Workerslot before booting. In pseudo-code it would look like this: 875*d289c2baSAndroid Build Coastguard Worker 876*d289c2baSAndroid Build Coastguard Worker```c++ 877*d289c2baSAndroid Build Coastguard Worker// The |slot_data| parameter should be the AvbSlotVerifyData returned 878*d289c2baSAndroid Build Coastguard Worker// by avb_slot_verify() for the slot we're about to boot. 879*d289c2baSAndroid Build Coastguard Worker// 880*d289c2baSAndroid Build Coastguard Workerbool update_stored_rollback_indexes_for_slot(AvbOps* ops, 881*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyData* slot_data) { 882*d289c2baSAndroid Build Coastguard Worker for (int n = 0; n < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; n++) { 883*d289c2baSAndroid Build Coastguard Worker uint64_t rollback_index = slot_data->rollback_indexes[n]; 884*d289c2baSAndroid Build Coastguard Worker if (rollback_index > 0) { 885*d289c2baSAndroid Build Coastguard Worker AvbIOResult io_ret; 886*d289c2baSAndroid Build Coastguard Worker uint64_t current_stored_rollback_index; 887*d289c2baSAndroid Build Coastguard Worker 888*d289c2baSAndroid Build Coastguard Worker io_ret = ops->read_rollback_index(ops, n, ¤t_stored_rollback_index); 889*d289c2baSAndroid Build Coastguard Worker if (io_ret != AVB_IO_RESULT_OK) { 890*d289c2baSAndroid Build Coastguard Worker return false; 891*d289c2baSAndroid Build Coastguard Worker } 892*d289c2baSAndroid Build Coastguard Worker 893*d289c2baSAndroid Build Coastguard Worker if (rollback_index > current_stored_rollback_index) { 894*d289c2baSAndroid Build Coastguard Worker io_ret = ops->write_rollback_index(ops, n, rollback_index); 895*d289c2baSAndroid Build Coastguard Worker if (io_ret != AVB_IO_RESULT_OK) { 896*d289c2baSAndroid Build Coastguard Worker return false; 897*d289c2baSAndroid Build Coastguard Worker } 898*d289c2baSAndroid Build Coastguard Worker } 899*d289c2baSAndroid Build Coastguard Worker } 900*d289c2baSAndroid Build Coastguard Worker } 901*d289c2baSAndroid Build Coastguard Worker return true; 902*d289c2baSAndroid Build Coastguard Worker} 903*d289c2baSAndroid Build Coastguard Worker``` 904*d289c2baSAndroid Build Coastguard Worker 905*d289c2baSAndroid Build Coastguard WorkerHowever if using A/B more care must be taken to still allow the device 906*d289c2baSAndroid Build Coastguard Workerto fall back to the old slot if the update didn't work. 907*d289c2baSAndroid Build Coastguard Worker 908*d289c2baSAndroid Build Coastguard WorkerFor an HLOS like Android where rollback is only supported if the 909*d289c2baSAndroid Build Coastguard Workerupdated OS version is found to not work, `stored_rollback_index[n]` 910*d289c2baSAndroid Build Coastguard Workershould only be updated from slots that are marked as SUCCESSFUL in the 911*d289c2baSAndroid Build Coastguard WorkerA/B metadata. The pseudo-code for that is as follows where 912*d289c2baSAndroid Build Coastguard Worker`is_slot_is_marked_as_successful()` comes from the A/B stack in use: 913*d289c2baSAndroid Build Coastguard Worker 914*d289c2baSAndroid Build Coastguard Worker```c++ 915*d289c2baSAndroid Build Coastguard Workerif (is_slot_is_marked_as_successful(slot->ab_suffix)) { 916*d289c2baSAndroid Build Coastguard Worker if (!update_stored_rollback_indexes_for_slot(ops, slot)) { 917*d289c2baSAndroid Build Coastguard Worker // TODO: handle error. 918*d289c2baSAndroid Build Coastguard Worker } 919*d289c2baSAndroid Build Coastguard Worker} 920*d289c2baSAndroid Build Coastguard Worker``` 921*d289c2baSAndroid Build Coastguard Worker 922*d289c2baSAndroid Build Coastguard WorkerThis logic should ideally be implemented outside of the HLOS. One 923*d289c2baSAndroid Build Coastguard Workerpossible implementation is to update rollback indices in the 924*d289c2baSAndroid Build Coastguard Workerbootloader when booting into a successful slot. This means that 925*d289c2baSAndroid Build Coastguard Workerwhen booting into a new OS not yet marked as successful, the 926*d289c2baSAndroid Build Coastguard Workerrollback indices would not be updated. The first reboot after the 927*d289c2baSAndroid Build Coastguard Workerslot succeeded would trigger an update of the rollback indices. 928*d289c2baSAndroid Build Coastguard Worker 929*d289c2baSAndroid Build Coastguard WorkerFor an HLOS where it's possible to roll back to a previous version, 930*d289c2baSAndroid Build Coastguard Worker`stored_rollback_index[n]` should be set to the largest possible value 931*d289c2baSAndroid Build Coastguard Workerallowing all bootable slots to boot. This approach is implemented in 932*d289c2baSAndroid Build Coastguard WorkerAVB's experimental (and now deprecated) A/B stack `libavb_ab`, see the 933*d289c2baSAndroid Build Coastguard Worker`avb_ab_flow()` implementation. Note that this requires verifying 934*d289c2baSAndroid Build Coastguard Worker*all* bootable slots at every boot and this may impact boot time. 935*d289c2baSAndroid Build Coastguard Worker 936*d289c2baSAndroid Build Coastguard Worker## Recommended Bootflow 937*d289c2baSAndroid Build Coastguard Worker 938*d289c2baSAndroid Build Coastguard WorkerThe recommended boot flow for a device using AVB is as follows: 939*d289c2baSAndroid Build Coastguard Worker 940*d289c2baSAndroid Build Coastguard Worker 941*d289c2baSAndroid Build Coastguard Worker 942*d289c2baSAndroid Build Coastguard WorkerNotes: 943*d289c2baSAndroid Build Coastguard Worker 944*d289c2baSAndroid Build Coastguard Worker* The device is expected to search through all A/B slots until it 945*d289c2baSAndroid Build Coastguard Worker finds a valid OS to boot. Slots that are rejected in the LOCKED 946*d289c2baSAndroid Build Coastguard Worker state might not be rejected in the UNLOCKED state, (e.g. when 947*d289c2baSAndroid Build Coastguard Worker UNLOCKED any key can be used and rollback index failures are 948*d289c2baSAndroid Build Coastguard Worker allowed), so the algorithm used for selecting a slot varies 949*d289c2baSAndroid Build Coastguard Worker depending on what state the device is in. 950*d289c2baSAndroid Build Coastguard Worker 951*d289c2baSAndroid Build Coastguard Worker* If no valid OS (that is, no bootable A/B slot) can be found, the 952*d289c2baSAndroid Build Coastguard Worker device cannot boot and has to enter repair mode. It is 953*d289c2baSAndroid Build Coastguard Worker device-dependent what this looks like. If the device has a screen 954*d289c2baSAndroid Build Coastguard Worker it must convey this state to the user. 955*d289c2baSAndroid Build Coastguard Worker 956*d289c2baSAndroid Build Coastguard Worker* If the device is LOCKED, only an OS signed by an embedded 957*d289c2baSAndroid Build Coastguard Worker verification key (see the previous section) shall be 958*d289c2baSAndroid Build Coastguard Worker accepted. Additionally, `rollback_index[n]` as stored in the 959*d289c2baSAndroid Build Coastguard Worker verified image must be greater or equal than what's in 960*d289c2baSAndroid Build Coastguard Worker `stored_rollback_index[n]` on the device (for all `n`) and the 961*d289c2baSAndroid Build Coastguard Worker `stored_rollback_index[n]` array is expected to be updated as 962*d289c2baSAndroid Build Coastguard Worker specified in the previous section. 963*d289c2baSAndroid Build Coastguard Worker + If the key used for verification was set by the end user, and 964*d289c2baSAndroid Build Coastguard Worker the device has a screen, it must show a warning with the key 965*d289c2baSAndroid Build Coastguard Worker fingerprint to convey that the device is booting a custom 966*d289c2baSAndroid Build Coastguard Worker OS. The warning must be shown for at least 10 seconds before the 967*d289c2baSAndroid Build Coastguard Worker boot process continues. If the device does not have a screen, 968*d289c2baSAndroid Build Coastguard Worker other ways must be used to convey that the device is booting a 969*d289c2baSAndroid Build Coastguard Worker custom OS (lightbars, LEDs, etc.). 970*d289c2baSAndroid Build Coastguard Worker 971*d289c2baSAndroid Build Coastguard Worker* If the device is UNLOCKED, there is no requirement to check the key 972*d289c2baSAndroid Build Coastguard Worker used to sign the OS nor is there any requirement to check or update 973*d289c2baSAndroid Build Coastguard Worker rollback `stored_rollback_index[n]` on the device. Because of this 974*d289c2baSAndroid Build Coastguard Worker the user must always be shown a warning about verification not 975*d289c2baSAndroid Build Coastguard Worker occurring. 976*d289c2baSAndroid Build Coastguard Worker + It is device-dependent how this is implemented since it depends 977*d289c2baSAndroid Build Coastguard Worker on the device form-factor and intended usage. If the device has 978*d289c2baSAndroid Build Coastguard Worker a screen and buttons (for example if it's a phone) the warning 979*d289c2baSAndroid Build Coastguard Worker is to be shown for at least 10 seconds before the boot process 980*d289c2baSAndroid Build Coastguard Worker continues. If the device does not have a screen, other ways must 981*d289c2baSAndroid Build Coastguard Worker be used to convey that the device is UNLOCKED (lightbars, LEDs, 982*d289c2baSAndroid Build Coastguard Worker etc.). 983*d289c2baSAndroid Build Coastguard Worker 984*d289c2baSAndroid Build Coastguard Worker### Booting Into Recovery 985*d289c2baSAndroid Build Coastguard Worker 986*d289c2baSAndroid Build Coastguard WorkerOn Android devices not using A/B, the `recovery` partition usually isn't 987*d289c2baSAndroid Build Coastguard Workerupdated along with other partitions and therefore can't be referenced 988*d289c2baSAndroid Build Coastguard Workerfrom the main `vbmeta` partition. 989*d289c2baSAndroid Build Coastguard Worker 990*d289c2baSAndroid Build Coastguard WorkerIt's still possible to use AVB to protect this partition (and others) 991*d289c2baSAndroid Build Coastguard Workerby signing these partitions and passing the 992*d289c2baSAndroid Build Coastguard Worker`AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION` flag to `avb_slot_verify()`. 993*d289c2baSAndroid Build Coastguard WorkerIn this mode, the key used to sign each requested partition is verified 994*d289c2baSAndroid Build Coastguard Workerby the `validate_public_key_for_partition()` operation which is also 995*d289c2baSAndroid Build Coastguard Workerused to return the rollback index location to be used. 996*d289c2baSAndroid Build Coastguard Worker 997*d289c2baSAndroid Build Coastguard Worker## Handling dm-verity Errors 998*d289c2baSAndroid Build Coastguard Worker 999*d289c2baSAndroid Build Coastguard WorkerBy design, hashtree verification errors are detected by the HLOS and 1000*d289c2baSAndroid Build Coastguard Workernot the bootloader. AVB provides a way to specify how the error should 1001*d289c2baSAndroid Build Coastguard Workerbe handled through the `hashtree_error_mode` parameter in the 1002*d289c2baSAndroid Build Coastguard Worker`avb_slot_verify()` function. Possible values include 1003*d289c2baSAndroid Build Coastguard Worker 1004*d289c2baSAndroid Build Coastguard Worker* `AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE` means that the HLOS 1005*d289c2baSAndroid Build Coastguard Worker will invalidate the current slot and restart. On devices with A/B 1006*d289c2baSAndroid Build Coastguard Worker this would lead to attempting to boot the other slot (if it's marked 1007*d289c2baSAndroid Build Coastguard Worker as bootable) or it could lead to a mode where no OS can be booted 1008*d289c2baSAndroid Build Coastguard Worker (e.g. some form of repair mode). In Linux this requires a kernel 1009*d289c2baSAndroid Build Coastguard Worker built with `CONFIG_DM_VERITY_AVB`. 1010*d289c2baSAndroid Build Coastguard Worker 1011*d289c2baSAndroid Build Coastguard Worker* `AVB_HASHTREE_ERROR_MODE_RESTART` means that the OS will restart 1012*d289c2baSAndroid Build Coastguard Worker without the current slot being invalidated. Be careful using this 1013*d289c2baSAndroid Build Coastguard Worker mode unconditionally as it may introduce boot loops if the same 1014*d289c2baSAndroid Build Coastguard Worker hashtree verification error is hit on every boot. 1015*d289c2baSAndroid Build Coastguard Worker 1016*d289c2baSAndroid Build Coastguard Worker* `AVB_HASHTREE_ERROR_MODE_EIO` means that an `EIO` error will be 1017*d289c2baSAndroid Build Coastguard Worker returned to the application. 1018*d289c2baSAndroid Build Coastguard Worker 1019*d289c2baSAndroid Build Coastguard Worker* `AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO` means that either the **RESTART** 1020*d289c2baSAndroid Build Coastguard Worker or **EIO** mode is used, depending on state. This mode implements a state 1021*d289c2baSAndroid Build Coastguard Worker machine whereby **RESTART** is used by default and when the 1022*d289c2baSAndroid Build Coastguard Worker `AVB_SLOT_VERIFY_FLAGS_RESTART_CAUSED_BY_HASHTREE_CORRUPTION` is passed to 1023*d289c2baSAndroid Build Coastguard Worker `avb_slot_verify()` the mode transitions to **EIO**. When a new OS has been 1024*d289c2baSAndroid Build Coastguard Worker detected the device transitions back to the **RESTART** mode. 1025*d289c2baSAndroid Build Coastguard Worker + To do this persistent storage is needed - specifically this means that the 1026*d289c2baSAndroid Build Coastguard Worker passed in `AvbOps` will need to have the `read_persistent_value()` and 1027*d289c2baSAndroid Build Coastguard Worker `write_persistent_value()` operations implemented. The name of the 1028*d289c2baSAndroid Build Coastguard Worker persistent value used is **avb.managed_verity_mode** and 32 bytes of storage 1029*d289c2baSAndroid Build Coastguard Worker is needed. 1030*d289c2baSAndroid Build Coastguard Worker 1031*d289c2baSAndroid Build Coastguard Worker* `AVB_HASHTREE_ERROR_MODE_LOGGING` means that errors will be logged 1032*d289c2baSAndroid Build Coastguard Worker and corrupt data may be returned to applications. This mode should 1033*d289c2baSAndroid Build Coastguard Worker be used for **ONLY** diagnostics and debugging. It cannot be used 1034*d289c2baSAndroid Build Coastguard Worker unless verification errors are allowed. 1035*d289c2baSAndroid Build Coastguard Worker 1036*d289c2baSAndroid Build Coastguard Worker* `AVB_HASHTREE_ERROR_MODE_PANIC` means that the OS will **panic** without 1037*d289c2baSAndroid Build Coastguard Worker the current slot being invalidated. Be careful using this mode as it may 1038*d289c2baSAndroid Build Coastguard Worker introduce boot panic if the same hashtree verification error is hit on 1039*d289c2baSAndroid Build Coastguard Worker every boot. This mode is available since: 1.7.0 (kernel 5.9) 1040*d289c2baSAndroid Build Coastguard Worker 1041*d289c2baSAndroid Build Coastguard WorkerThe value passed in `hashtree_error_mode` is essentially just passed on through 1042*d289c2baSAndroid Build Coastguard Workerto the HLOS through the the `androidboot.veritymode`, 1043*d289c2baSAndroid Build Coastguard Worker`androidboot.veritymode.managed`, and `androidboot.vbmeta.invalidate_on_error` 1044*d289c2baSAndroid Build Coastguard Workerkernel command-line parameters in the following way: 1045*d289c2baSAndroid Build Coastguard Worker 1046*d289c2baSAndroid Build Coastguard Worker| | `androidboot.veritymode` | `androidboot.veritymode.managed` | `androidboot.vbmeta.invalidate_on_error` | 1047*d289c2baSAndroid Build Coastguard Worker|------|:------------------------:|:--------------------------------:|:----------------------------------------:| 1048*d289c2baSAndroid Build Coastguard Worker| `AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE` | **enforcing** | (unset) | **yes** | 1049*d289c2baSAndroid Build Coastguard Worker| `AVB_HASHTREE_ERROR_MODE_RESTART` | **enforcing** | (unset) | (unset) | 1050*d289c2baSAndroid Build Coastguard Worker| `AVB_HASHTREE_ERROR_MODE_EIO` | **eio** | (unset) | (unset) | 1051*d289c2baSAndroid Build Coastguard Worker| `AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO` | **eio** or **enforcing** | **yes** | (unset) | 1052*d289c2baSAndroid Build Coastguard Worker| `AVB_HASHTREE_ERROR_MODE_LOGGING` | **ignore_corruption** | (unset) | (unset) | 1053*d289c2baSAndroid Build Coastguard Worker| `AVB_HASHTREE_ERROR_MODE_PANIC` | **panicking** | (unset) | (unset) | 1054*d289c2baSAndroid Build Coastguard Worker 1055*d289c2baSAndroid Build Coastguard WorkerThe only exception to this table is that if the 1056*d289c2baSAndroid Build Coastguard Worker`AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED` flag is set in the top-level vbmeta, 1057*d289c2baSAndroid Build Coastguard Workerthen `androidboot.veritymode` is set to **disabled** and 1058*d289c2baSAndroid Build Coastguard Worker`androidboot.veritymode.managed` and `androidboot.vbmeta.invalidate_on_error` 1059*d289c2baSAndroid Build Coastguard Workerare unset. 1060*d289c2baSAndroid Build Coastguard Worker 1061*d289c2baSAndroid Build Coastguard WorkerThe different values of `hashtree_error_mode` parameter in the `avb_slot_verify()` 1062*d289c2baSAndroid Build Coastguard Workerfunction can be categorized into three groups: 1063*d289c2baSAndroid Build Coastguard Worker 1064*d289c2baSAndroid Build Coastguard Worker* `AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE`, which needs `CONFIG_DM_VERITY_AVB` 1065*d289c2baSAndroid Build Coastguard Worker in the kernel config for the kernel to invalidate the current slot and 1066*d289c2baSAndroid Build Coastguard Worker restart. This is kept here for legacy Android Things devices and is not 1067*d289c2baSAndroid Build Coastguard Worker recommended for other device form factors. 1068*d289c2baSAndroid Build Coastguard Worker 1069*d289c2baSAndroid Build Coastguard Worker* The bootloader handles the switch between `AVB_HASHTREE_ERROR_MODE_RESTART` 1070*d289c2baSAndroid Build Coastguard Worker and `AVB_HASHTREE_ERROR_MODE_EIO`. This would need a persistent storage on the 1071*d289c2baSAndroid Build Coastguard Worker device to store the vbmeta digest, so the bootloader can detect if a device 1072*d289c2baSAndroid Build Coastguard Worker ever gets an update or not. Once the new OS is installed and if the device is 1073*d289c2baSAndroid Build Coastguard Worker in **EIO** mode, the bootloader should switch back to **RESTART** mode. 1074*d289c2baSAndroid Build Coastguard Worker 1075*d289c2baSAndroid Build Coastguard Worker* `AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO`: `libavb` helps the 1076*d289c2baSAndroid Build Coastguard Worker bootloader manage **EIO**/**RESTART** state transition. The bootloader needs 1077*d289c2baSAndroid Build Coastguard Worker to implement the callbacks of `AvbOps->read_persistent_value()` and 1078*d289c2baSAndroid Build Coastguard Worker `AvbOps->write_persistent_value()` for `libavb` to store the vbmeta digest to 1079*d289c2baSAndroid Build Coastguard Worker detect whether a new OS is installed. 1080*d289c2baSAndroid Build Coastguard Worker 1081*d289c2baSAndroid Build Coastguard Worker### Which mode should I use for my device? 1082*d289c2baSAndroid Build Coastguard Worker 1083*d289c2baSAndroid Build Coastguard WorkerThis depends entirely on the device, how the device is intended to be 1084*d289c2baSAndroid Build Coastguard Workerused, and the desired user experience. 1085*d289c2baSAndroid Build Coastguard Worker 1086*d289c2baSAndroid Build Coastguard WorkerFor Android devices the `AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO` mode 1087*d289c2baSAndroid Build Coastguard Workershould be used. Also see the [Boot Flow section on source.android.com](https://source.android.com/security/verifiedboot/boot-flow) for the kind of UX and UI the boot loader should implement. 1088*d289c2baSAndroid Build Coastguard Worker 1089*d289c2baSAndroid Build Coastguard WorkerIf the device doesn't have a screen or if the HLOS supports multiple bootable 1090*d289c2baSAndroid Build Coastguard Workerslots simultaneously it may make more sense to just use 1091*d289c2baSAndroid Build Coastguard Worker`AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE`. 1092*d289c2baSAndroid Build Coastguard Worker 1093*d289c2baSAndroid Build Coastguard Worker## Android Specific Integration 1094*d289c2baSAndroid Build Coastguard Worker 1095*d289c2baSAndroid Build Coastguard WorkerOn Android, the boot loader must set the 1096*d289c2baSAndroid Build Coastguard Worker`androidboot.verifiedbootstate` parameter on the kernel command-line 1097*d289c2baSAndroid Build Coastguard Workerto indicate the boot state. It shall use the following values: 1098*d289c2baSAndroid Build Coastguard Worker 1099*d289c2baSAndroid Build Coastguard Worker* **green**: If in LOCKED state and the key used for verification was not set by the end user. 1100*d289c2baSAndroid Build Coastguard Worker* **yellow**: If in LOCKED state and the key used for verification was set by the end user. 1101*d289c2baSAndroid Build Coastguard Worker* **orange**: If in the UNLOCKED state. 1102*d289c2baSAndroid Build Coastguard Worker 1103*d289c2baSAndroid Build Coastguard Worker## GKI 2.0 Integration 1104*d289c2baSAndroid Build Coastguard Worker 1105*d289c2baSAndroid Build Coastguard WorkerStarting from Android 12, devices launching with kernel version 5.10 or higher 1106*d289c2baSAndroid Build Coastguard Workermust ship with the GKI kernel. See [GKI 2.0](https://source.android.com/devices/architecture/kernel/generic-kernel-image#gki2) 1107*d289c2baSAndroid Build Coastguard Workerfor details. 1108*d289c2baSAndroid Build Coastguard Worker 1109*d289c2baSAndroid Build Coastguard WorkerWhile incorporating a certified GKI `boot.img` into a device codebase, the 1110*d289c2baSAndroid Build Coastguard Workerfollowing board variables should be configured. The setting shown below is just 1111*d289c2baSAndroid Build Coastguard Workeran example to be adjusted per device. 1112*d289c2baSAndroid Build Coastguard Worker 1113*d289c2baSAndroid Build Coastguard Worker``` 1114*d289c2baSAndroid Build Coastguard Worker# Uses a prebuilt boot.img 1115*d289c2baSAndroid Build Coastguard WorkerTARGET_NO_KERNEL := true 1116*d289c2baSAndroid Build Coastguard WorkerBOARD_PREBUILT_BOOTIMAGE := device/${company}/${board}/boot.img 1117*d289c2baSAndroid Build Coastguard Worker 1118*d289c2baSAndroid Build Coastguard Worker# Enables chained vbmeta for the boot.img so it can be updated independently, 1119*d289c2baSAndroid Build Coastguard Worker# without updating the vbmeta.img. The following configs are optional. 1120*d289c2baSAndroid Build Coastguard Worker# When they're absent, the hash of the boot.img will be stored then signed in 1121*d289c2baSAndroid Build Coastguard Worker# the vbmeta.img. 1122*d289c2baSAndroid Build Coastguard WorkerBOARD_AVB_BOOT_KEY_PATH := external/avb/test/data/testkey_rsa4096.pem 1123*d289c2baSAndroid Build Coastguard WorkerBOARD_AVB_BOOT_ALGORITHM := SHA256_RSA4096 1124*d289c2baSAndroid Build Coastguard WorkerBOARD_AVB_BOOT_ROLLBACK_INDEX := $(PLATFORM_SECURITY_PATCH_TIMESTAMP) 1125*d289c2baSAndroid Build Coastguard WorkerBOARD_AVB_BOOT_ROLLBACK_INDEX_LOCATION := 2 1126*d289c2baSAndroid Build Coastguard Worker``` 1127*d289c2baSAndroid Build Coastguard Worker 1128*d289c2baSAndroid Build Coastguard Worker**NOTE**: The certified GKI `boot.img` isn't signed for verified boot. 1129*d289c2baSAndroid Build Coastguard WorkerA device-specific verified boot chain should still be configured for a prebuilt 1130*d289c2baSAndroid Build Coastguard WorkerGKI `boot.img`. 1131*d289c2baSAndroid Build Coastguard Worker 1132*d289c2baSAndroid Build Coastguard Worker## Device Specific Notes 1133*d289c2baSAndroid Build Coastguard Worker 1134*d289c2baSAndroid Build Coastguard WorkerThis section contains information about how AVB is integrated into specific 1135*d289c2baSAndroid Build Coastguard Workerdevices. This is not an exhaustive list. 1136*d289c2baSAndroid Build Coastguard Worker 1137*d289c2baSAndroid Build Coastguard Worker### Pixel 2 and later 1138*d289c2baSAndroid Build Coastguard Worker 1139*d289c2baSAndroid Build Coastguard WorkerOn the Pixel 2, Pixel 2 XL and later Pixel models, the boot loader supports a 1140*d289c2baSAndroid Build Coastguard Workervirtual partition with the name `avb_custom_key`. Flashing and erasing this 1141*d289c2baSAndroid Build Coastguard Workerpartition only works in the UNLOCKED state. Setting the custom key is done like 1142*d289c2baSAndroid Build Coastguard Workerthis: 1143*d289c2baSAndroid Build Coastguard Worker 1144*d289c2baSAndroid Build Coastguard Worker avbtool extract_public_key --key key.pem --output pkmd.bin 1145*d289c2baSAndroid Build Coastguard Worker fastboot flash avb_custom_key pkmd.bin 1146*d289c2baSAndroid Build Coastguard Worker 1147*d289c2baSAndroid Build Coastguard WorkerErasing the key is done by erasing the virtual partition: 1148*d289c2baSAndroid Build Coastguard Worker 1149*d289c2baSAndroid Build Coastguard Worker fastboot erase avb_custom_key 1150*d289c2baSAndroid Build Coastguard Worker 1151*d289c2baSAndroid Build Coastguard WorkerWhen the custom key is set and the device is in the LOCKED state it will boot 1152*d289c2baSAndroid Build Coastguard Workerimages signed with both the built-in key as well as the custom key. All other 1153*d289c2baSAndroid Build Coastguard Workersecurity features (including rollback-protection) are in effect, e.g. the 1154*d289c2baSAndroid Build Coastguard Worker**only** difference is the root of trust to use. 1155*d289c2baSAndroid Build Coastguard Worker 1156*d289c2baSAndroid Build Coastguard WorkerWhen booting an image signed with a custom key, a yellow screen will be shown as 1157*d289c2baSAndroid Build Coastguard Workerpart of the boot process to remind the user that the custom key is in use. 1158*d289c2baSAndroid Build Coastguard Worker 1159*d289c2baSAndroid Build Coastguard Worker# Version History 1160*d289c2baSAndroid Build Coastguard Worker 1161*d289c2baSAndroid Build Coastguard Worker### Version 1.3 1162*d289c2baSAndroid Build Coastguard WorkerVersion 1.3 adds support for the following: 1163*d289c2baSAndroid Build Coastguard Worker* A 32-bit `flags` element is added to a chain descriptor. 1164*d289c2baSAndroid Build Coastguard Worker* Support for chain partitions which don't use [A/B](#a_b-support). 1165*d289c2baSAndroid Build Coastguard Worker 1166*d289c2baSAndroid Build Coastguard Worker### Version 1.2 1167*d289c2baSAndroid Build Coastguard Worker 1168*d289c2baSAndroid Build Coastguard WorkerVersion 1.2 adds support for the following: 1169*d289c2baSAndroid Build Coastguard Worker* `rollback_index_location` field of the main vbmeta header. 1170*d289c2baSAndroid Build Coastguard Worker* `check_at_most_once` parameter of dm-verity in a hashtree descriptor. 1171*d289c2baSAndroid Build Coastguard Worker 1172*d289c2baSAndroid Build Coastguard Worker### Version 1.1 1173*d289c2baSAndroid Build Coastguard Worker 1174*d289c2baSAndroid Build Coastguard WorkerVersion 1.1 adds support for the following: 1175*d289c2baSAndroid Build Coastguard Worker 1176*d289c2baSAndroid Build Coastguard Worker* A 32-bit `flags` element is added to hash and hashtree descriptors. 1177*d289c2baSAndroid Build Coastguard Worker* Support for partitions which don't use [A/B](#a_b-support). 1178*d289c2baSAndroid Build Coastguard Worker* Tamper-evident [named persistent values](#named-persistent-values). 1179*d289c2baSAndroid Build Coastguard Worker* [Persistent digests](#persistent-digests) for hash or hashtree descriptors. 1180*d289c2baSAndroid Build Coastguard Worker 1181*d289c2baSAndroid Build Coastguard Worker### Version 1.0 1182*d289c2baSAndroid Build Coastguard Worker 1183*d289c2baSAndroid Build Coastguard WorkerAll features not explicitly listed under a later version are supported by 1.0. 1184