1*5a923131SAndroid Build Coastguard Worker# Chrome OS Update Process 2*5a923131SAndroid Build Coastguard Worker 3*5a923131SAndroid Build Coastguard Worker[TOC] 4*5a923131SAndroid Build Coastguard Worker 5*5a923131SAndroid Build Coastguard WorkerSystem updates in more modern operating systems like Chrome OS and Android are 6*5a923131SAndroid Build Coastguard Workercalled A/B updates, over-the-air ([OTA]) updates, seamless updates, or simply 7*5a923131SAndroid Build Coastguard Workerauto updates. In contrast to more primitive system updates (like Windows or 8*5a923131SAndroid Build Coastguard WorkermacOS) where the system is booted into a special mode to override the system 9*5a923131SAndroid Build Coastguard Workerpartitions with newer updates and may take several minutes or hours, A/B updates 10*5a923131SAndroid Build Coastguard Workerhave several advantages including but not limited to: 11*5a923131SAndroid Build Coastguard Worker 12*5a923131SAndroid Build Coastguard Worker* Updates maintain a workable system that remains on the disk during and after 13*5a923131SAndroid Build Coastguard Worker an update. Hence, reducing the likelihood of corrupting a device into a 14*5a923131SAndroid Build Coastguard Worker non-usable state. And reducing the need for flashing devices manually or at 15*5a923131SAndroid Build Coastguard Worker repair and warranty centers, etc. 16*5a923131SAndroid Build Coastguard Worker* Updates can happen while the system is running (normally with minimum 17*5a923131SAndroid Build Coastguard Worker overhead) without interrupting the user. The only downside for users is a 18*5a923131SAndroid Build Coastguard Worker required reboot (or, in Chrome OS, a sign out which automatically causes a 19*5a923131SAndroid Build Coastguard Worker reboot if an update was performed where the reboot duration is about 10 20*5a923131SAndroid Build Coastguard Worker seconds and is no different than a normal reboot). 21*5a923131SAndroid Build Coastguard Worker* The user does not need (although they can) to request for an update. The 22*5a923131SAndroid Build Coastguard Worker update checks happen periodically in the background. 23*5a923131SAndroid Build Coastguard Worker* If the update fails to apply, the user is not affected. The user will 24*5a923131SAndroid Build Coastguard Worker continue on the old version of the system and the system will attempt to 25*5a923131SAndroid Build Coastguard Worker apply the update again at a later time. 26*5a923131SAndroid Build Coastguard Worker* If the update applies correctly but fails to boot, the system will rollback 27*5a923131SAndroid Build Coastguard Worker to the old partition and the user can still use the system as usual. 28*5a923131SAndroid Build Coastguard Worker* The user does not need to reserve enough space for the update. The system 29*5a923131SAndroid Build Coastguard Worker has already reserved enough space in terms of two copies (A and B) of a 30*5a923131SAndroid Build Coastguard Worker partition. The system doesn’t even need any cache space on the disk, 31*5a923131SAndroid Build Coastguard Worker everything happens seamlessly from network to memory to the inactive 32*5a923131SAndroid Build Coastguard Worker partitions. 33*5a923131SAndroid Build Coastguard Worker 34*5a923131SAndroid Build Coastguard Worker## Life of an A/B Update 35*5a923131SAndroid Build Coastguard Worker 36*5a923131SAndroid Build Coastguard WorkerIn A/B update capable systems, each partition, such as the kernel or root (or 37*5a923131SAndroid Build Coastguard Workerother artifacts like [DLC]), has two copies. We call these two copies active (A) 38*5a923131SAndroid Build Coastguard Workerand inactive (B). The system is booted into the active partition (depending on 39*5a923131SAndroid Build Coastguard Workerwhich copy has the higher priority at boot time) and when a new update is 40*5a923131SAndroid Build Coastguard Workeravailable, it is written into the inactive partition. After a successful reboot, 41*5a923131SAndroid Build Coastguard Workerthe previously inactive partition becomes active and the old active partition 42*5a923131SAndroid Build Coastguard Workerbecomes inactive. 43*5a923131SAndroid Build Coastguard Worker 44*5a923131SAndroid Build Coastguard Worker### Generation 45*5a923131SAndroid Build Coastguard Worker 46*5a923131SAndroid Build Coastguard WorkerBut everything starts with generating OTA packages on (Google) servers for 47*5a923131SAndroid Build Coastguard Workereach new system image. This is done by calling 48*5a923131SAndroid Build Coastguard Worker[ota_from_target_files](https://cs.android.com/android/platform/superproject/+/master:build/make/tools/releasetools/ota_from_target_files.py) 49*5a923131SAndroid Build Coastguard Workerwith source and destination builds. This script requires target_file.zip to work, 50*5a923131SAndroid Build Coastguard Workerimage files are not sufficient. 51*5a923131SAndroid Build Coastguard Worker 52*5a923131SAndroid Build Coastguard Worker### Distribution/Configuration 53*5a923131SAndroid Build Coastguard WorkerOnce the OTA packages are generated, they are signed with specific keys 54*5a923131SAndroid Build Coastguard Workerand stored in a location known to an update server (GOTA). 55*5a923131SAndroid Build Coastguard WorkerGOTA will then make this OTA package accessible via a public URL. Optionally, 56*5a923131SAndroid Build Coastguard Workeroperators an choose to make this OTA update available only to a specific 57*5a923131SAndroid Build Coastguard Workersubset of devices. 58*5a923131SAndroid Build Coastguard Worker 59*5a923131SAndroid Build Coastguard Worker### Installation 60*5a923131SAndroid Build Coastguard WorkerWhen the device's updater client initiates an update (either periodically or user 61*5a923131SAndroid Build Coastguard Workerinitiated), it first consults different device policies to see if the update 62*5a923131SAndroid Build Coastguard Workercheck is allowed. For example, device policies can prevent an update check 63*5a923131SAndroid Build Coastguard Workerduring certain times of a day or they require the update check time to be 64*5a923131SAndroid Build Coastguard Workerscattered throughout the day randomly, etc. 65*5a923131SAndroid Build Coastguard Worker 66*5a923131SAndroid Build Coastguard WorkerOnce policies allow for the update check, the updater client sends a request to 67*5a923131SAndroid Build Coastguard Workerthe update server (all this communication happens over HTTPS) and identifies its 68*5a923131SAndroid Build Coastguard Workerparameters like its Application ID, hardware ID, version, board, etc. 69*5a923131SAndroid Build Coastguard Worker 70*5a923131SAndroid Build Coastguard WorkerSome policities on the server might prevent the device from getting specific 71*5a923131SAndroid Build Coastguard WorkerOTA updates, these server side policities are often set by operators. For 72*5a923131SAndroid Build Coastguard Workerexample, the operator might want to deliver a beta version of software to only 73*5a923131SAndroid Build Coastguard Workera subset of devices. 74*5a923131SAndroid Build Coastguard Worker 75*5a923131SAndroid Build Coastguard WorkerBut if the update server decides to serve an update payload, it will respond 76*5a923131SAndroid Build Coastguard Workerwith all the parameters needed to perform an update like the URLs to download the 77*5a923131SAndroid Build Coastguard Workerpayloads, the metadata signatures, the payload size and hash, etc. The updater 78*5a923131SAndroid Build Coastguard Workerclient continues communicating with the update server after different state 79*5a923131SAndroid Build Coastguard Workerchanges, like reporting that it started to download the payload or it finished 80*5a923131SAndroid Build Coastguard Workerthe update, or reports that the update failed with specific error codes, etc. 81*5a923131SAndroid Build Coastguard Worker 82*5a923131SAndroid Build Coastguard WorkerThe device will then proceed to actually installing the OTA update. This consists 83*5a923131SAndroid Build Coastguard Workerof roughly 3 steps. 84*5a923131SAndroid Build Coastguard Worker#### Download & Install 85*5a923131SAndroid Build Coastguard WorkerEach payload consists of two main sections: metadata and extra data. The 86*5a923131SAndroid Build Coastguard Workermetadata is basically a list of operations that should be performed for an 87*5a923131SAndroid Build Coastguard Workerupdate. The extra data contains the data blobs needed by some or all of these 88*5a923131SAndroid Build Coastguard Workeroperations. The updater client first downloads the metadata and 89*5a923131SAndroid Build Coastguard Workercryptographically verifies it using the provided signatures from the update 90*5a923131SAndroid Build Coastguard Workerserver’s response. Once the metadata is verified as valid, the rest of the 91*5a923131SAndroid Build Coastguard Workerpayload can easily be verified cryptographically (mostly through SHA256 hashes). 92*5a923131SAndroid Build Coastguard Worker 93*5a923131SAndroid Build Coastguard WorkerNext, the updater client marks the inactive partition as unbootable (because it 94*5a923131SAndroid Build Coastguard Workerneeds to write the new updates into it). At this point the system cannot 95*5a923131SAndroid Build Coastguard Workerrollback to the inactive partition anymore. 96*5a923131SAndroid Build Coastguard Worker 97*5a923131SAndroid Build Coastguard WorkerThen, the updater client performs the operations defined in the metadata (in the 98*5a923131SAndroid Build Coastguard Workerorder they appear in the metadata) and the rest of the payload is gradually 99*5a923131SAndroid Build Coastguard Workerdownloaded when these operations require their data. Once an operation is 100*5a923131SAndroid Build Coastguard Workerfinished its data is discarded. This eliminates the need for caching the entire 101*5a923131SAndroid Build Coastguard Workerpayload before applying it. During this process the updater client periodically 102*5a923131SAndroid Build Coastguard Workercheckpoints the last operation performed so in the event of failure or system 103*5a923131SAndroid Build Coastguard Workershutdown, etc. it can continue from the point it missed without redoing all 104*5a923131SAndroid Build Coastguard Workeroperations from the beginning. 105*5a923131SAndroid Build Coastguard Worker 106*5a923131SAndroid Build Coastguard WorkerDuring the download, the updater client hashes the downloaded bytes and when the 107*5a923131SAndroid Build Coastguard Workerdownload finishes, it checks the payload signature (located at the end of the 108*5a923131SAndroid Build Coastguard Workerpayload). If the signature cannot be verified, the update is rejected. 109*5a923131SAndroid Build Coastguard Worker 110*5a923131SAndroid Build Coastguard Worker#### Hash Verification & Verity Computation 111*5a923131SAndroid Build Coastguard Worker 112*5a923131SAndroid Build Coastguard WorkerAfter the inactive partition is updated, the updater client will compute 113*5a923131SAndroid Build Coastguard WorkerForward-Error-Correction(also known as FEC, Verity) code for each partition, 114*5a923131SAndroid Build Coastguard Workerand wriee the computed verity data to inactive partitions. In some updates, 115*5a923131SAndroid Build Coastguard Workerverity data is included in the extra data, so this step will be skipped. 116*5a923131SAndroid Build Coastguard Worker 117*5a923131SAndroid Build Coastguard WorkerThen, the entire partition is re-read, hashed and compared to a hash value 118*5a923131SAndroid Build Coastguard Workerpassed in the metadata to make sure the update was successfully written into 119*5a923131SAndroid Build Coastguard Workerthe partition. Hash computed in this step includes the verity code written in 120*5a923131SAndroid Build Coastguard Workerlast step. 121*5a923131SAndroid Build Coastguard Worker 122*5a923131SAndroid Build Coastguard Worker#### Postintall 123*5a923131SAndroid Build Coastguard Worker 124*5a923131SAndroid Build Coastguard WorkerIn the next step, the [Postinstall] scripts (if any) is called. From OTA's perspective, 125*5a923131SAndroid Build Coastguard Workerthese postinstall scripts are just blackboxes. Usually postinstall scripts will optimize 126*5a923131SAndroid Build Coastguard Workerexistings apps on the phone and run file system garbage collection, so that device can boot 127*5a923131SAndroid Build Coastguard Workerfast after OTA. But these are managed by other teams. 128*5a923131SAndroid Build Coastguard Worker 129*5a923131SAndroid Build Coastguard Worker#### Finishing Touches 130*5a923131SAndroid Build Coastguard Worker 131*5a923131SAndroid Build Coastguard WorkerThen the updater client goes into a state that identifies the update has 132*5a923131SAndroid Build Coastguard Workercompleted and the user needs to reboot the system. At this point, until the user 133*5a923131SAndroid Build Coastguard Workerreboots (or signs out), the updater client will not do any more system updates 134*5a923131SAndroid Build Coastguard Workereven if newer updates are available. However, it does continue to perform 135*5a923131SAndroid Build Coastguard Workerperiodic update checks so we can have statistics on the number of active devices 136*5a923131SAndroid Build Coastguard Workerin the field. 137*5a923131SAndroid Build Coastguard Worker 138*5a923131SAndroid Build Coastguard WorkerAfter the update proved successful, the inactive partition is marked to have a 139*5a923131SAndroid Build Coastguard Workerhigher priority (on a boot, a partition with higher priority is booted 140*5a923131SAndroid Build Coastguard Workerfirst). Once the user reboots the system, it will boot into the updated 141*5a923131SAndroid Build Coastguard Workerpartition and it is marked as active. At this point, after the reboot, the 142*5a923131SAndroid Build Coastguard Worker[update_verifier](https://cs.android.com/android/platform/superproject/+/master:bootable/recovery/update_verifier/) 143*5a923131SAndroid Build Coastguard Workerprogram runs, read all dm-verity devices to make sure the partitions aren't corrupted, 144*5a923131SAndroid Build Coastguard Workerthen mark the update as successful. 145*5a923131SAndroid Build Coastguard Worker 146*5a923131SAndroid Build Coastguard WorkerA/B updates are considered completed at this point. Virtual A/B updates will have an 147*5a923131SAndroid Build Coastguard Workeradditional step after this, called "merging". Merging usually takes few minutes, after that 148*5a923131SAndroid Build Coastguard WorkerVirtual A/B updates are considered complete. 149*5a923131SAndroid Build Coastguard Worker 150*5a923131SAndroid Build Coastguard Worker## Update Engine Daemon 151*5a923131SAndroid Build Coastguard Worker 152*5a923131SAndroid Build Coastguard WorkerThe `update_engine` is a single-threaded daemon process that runs all the 153*5a923131SAndroid Build Coastguard Workertimes. This process is the heart of the auto updates. It runs with lower 154*5a923131SAndroid Build Coastguard Workerpriorities in the background and is one of the last processes to start after a 155*5a923131SAndroid Build Coastguard Workersystem boot. Different clients (like GMS Core or other services) can send requests 156*5a923131SAndroid Build Coastguard Workerfor update checks to the update engine. The details of how requests are passed 157*5a923131SAndroid Build Coastguard Workerto the update engine is system dependent, but in Chrome OS it is D-Bus. Look at 158*5a923131SAndroid Build Coastguard Workerthe [D-Bus interface] for a list of all available methods. On Android it is binder. 159*5a923131SAndroid Build Coastguard Worker 160*5a923131SAndroid Build Coastguard WorkerThere are many resiliency features embedded in the update engine that makes auto 161*5a923131SAndroid Build Coastguard Workerupdates robust including but not limited to: 162*5a923131SAndroid Build Coastguard Worker 163*5a923131SAndroid Build Coastguard Worker* If the update engine crashes, it will restart automatically. 164*5a923131SAndroid Build Coastguard Worker* During an active update it periodically checkpoints the state of the update 165*5a923131SAndroid Build Coastguard Worker and if it fails to continue the update or crashes in the middle, it will 166*5a923131SAndroid Build Coastguard Worker continue from the last checkpoint. 167*5a923131SAndroid Build Coastguard Worker* It retries failed network communication. 168*5a923131SAndroid Build Coastguard Worker* If it fails to apply a delta payload (due to bit changes on the active 169*5a923131SAndroid Build Coastguard Worker partition) for a few times, it switches to full payload. 170*5a923131SAndroid Build Coastguard Worker 171*5a923131SAndroid Build Coastguard WorkerThe updater clients writes its active preferences in 172*5a923131SAndroid Build Coastguard Worker`/data/misc/update_engine/prefs`. These preferences help with tracking changes 173*5a923131SAndroid Build Coastguard Workerduring the lifetime of the updater client and allows properly continuing the 174*5a923131SAndroid Build Coastguard Workerupdate process after failed attempts or crashes. 175*5a923131SAndroid Build Coastguard Worker 176*5a923131SAndroid Build Coastguard Worker 177*5a923131SAndroid Build Coastguard Worker 178*5a923131SAndroid Build Coastguard Worker### Interactive vs Non-Interactive vs. Forced Updates 179*5a923131SAndroid Build Coastguard Worker 180*5a923131SAndroid Build Coastguard WorkerNon-interactive updates are updates that are scheduled periodically by the 181*5a923131SAndroid Build Coastguard Workerupdate engine and happen in the background. Interactive updates, on the other 182*5a923131SAndroid Build Coastguard Workerhand, happen when a user specifically requests an update check (e.g. by clicking 183*5a923131SAndroid Build Coastguard Workeron “Check For Update” button in Chrome OS’s About page). Depending on the update 184*5a923131SAndroid Build Coastguard Workerserver's policies, interactive updates have higher priority than non-interactive 185*5a923131SAndroid Build Coastguard Workerupdates (by carrying marker hints). They may decide to not provide an update if 186*5a923131SAndroid Build Coastguard Workerthey have busy server load, etc. There are other internal differences between 187*5a923131SAndroid Build Coastguard Workerthese two types of updates too. For example, interactive updates try to install 188*5a923131SAndroid Build Coastguard Workerthe update faster. 189*5a923131SAndroid Build Coastguard Worker 190*5a923131SAndroid Build Coastguard WorkerForced updates are similar to interactive updates (initiated by some kind of 191*5a923131SAndroid Build Coastguard Workeruser action), but they can also be configured to act as non-interactive. Since 192*5a923131SAndroid Build Coastguard Workernon-interactive updates happen periodically, a forced-non-interactive update 193*5a923131SAndroid Build Coastguard Workercauses a non-interactive update at the moment of the request, not at a later 194*5a923131SAndroid Build Coastguard Workertime. We can call a forced non-interactive update with: 195*5a923131SAndroid Build Coastguard Worker 196*5a923131SAndroid Build Coastguard Worker```bash 197*5a923131SAndroid Build Coastguard Workerupdate_engine_client --interactive=false --check_for_update 198*5a923131SAndroid Build Coastguard Worker``` 199*5a923131SAndroid Build Coastguard Worker 200*5a923131SAndroid Build Coastguard Worker### Network 201*5a923131SAndroid Build Coastguard Worker 202*5a923131SAndroid Build Coastguard WorkerThe updater client has the capability to download the payloads using Ethernet, 203*5a923131SAndroid Build Coastguard WorkerWiFi, or Cellular networks depending on which one the device is connected 204*5a923131SAndroid Build Coastguard Workerto. Downloading over Cellular networks will prompt permission from the user as 205*5a923131SAndroid Build Coastguard Workerit can consume a considerable amount of data. 206*5a923131SAndroid Build Coastguard Worker 207*5a923131SAndroid Build Coastguard Worker### Logs 208*5a923131SAndroid Build Coastguard Worker 209*5a923131SAndroid Build Coastguard WorkerIn Chrome OS the `update_engine` logs are located in `/var/log/update_engine` 210*5a923131SAndroid Build Coastguard Workerdirectory. Whenever `update_engine` starts, it starts a new log file with the 211*5a923131SAndroid Build Coastguard Workercurrent data-time format in the log file’s name 212*5a923131SAndroid Build Coastguard Worker(`update_engine.log-DATE-TIME`). Many log files can be seen in 213*5a923131SAndroid Build Coastguard Worker`/var/log/update_engine` after a few restarts of the update engine or after the 214*5a923131SAndroid Build Coastguard Workersystem reboots. The latest active log is symlinked to 215*5a923131SAndroid Build Coastguard Worker`/var/log/update_engine.log`. 216*5a923131SAndroid Build Coastguard Worker 217*5a923131SAndroid Build Coastguard WorkerIn Android the `update_engine` logs are located in `/data/misc/update_engine_log`. 218*5a923131SAndroid Build Coastguard Worker 219*5a923131SAndroid Build Coastguard Worker## Update Payload Generation 220*5a923131SAndroid Build Coastguard Worker 221*5a923131SAndroid Build Coastguard WorkerThe update payload generation is the process of converting a set of 222*5a923131SAndroid Build Coastguard Workerpartitions/files into a format that is both understandable by the updater client 223*5a923131SAndroid Build Coastguard Worker(especially if it's a much older version) and is securely verifiable. This 224*5a923131SAndroid Build Coastguard Workerprocess involves breaking the input partitions into smaller components and 225*5a923131SAndroid Build Coastguard Workercompressing them in order to help with network bandwidth when downloading the 226*5a923131SAndroid Build Coastguard Workerpayloads. 227*5a923131SAndroid Build Coastguard Worker 228*5a923131SAndroid Build Coastguard Worker`delta_generator` is a tool with a wide range of options for generating 229*5a923131SAndroid Build Coastguard Workerdifferent types of update payloads. Its code is located in 230*5a923131SAndroid Build Coastguard Worker`update_engine/payload_generator`. This directory contains all the source code 231*5a923131SAndroid Build Coastguard Workerrelated to mechanics of generating an update payload. None of the files in this 232*5a923131SAndroid Build Coastguard Workerdirectory should be included or used in any other library/executable other than 233*5a923131SAndroid Build Coastguard Workerthe `delta_generator` which means this directory does not get compiled into the 234*5a923131SAndroid Build Coastguard Workerrest of the update engine tools. 235*5a923131SAndroid Build Coastguard Worker 236*5a923131SAndroid Build Coastguard WorkerHowever, it is not recommended to use `delta_generator` directly, as it has way 237*5a923131SAndroid Build Coastguard Workertoo many flags. Wrappers like [ota_from_target_files](https://cs.android.com/android/platform/superproject/+/master:build/make/tools/releasetools/ota_from_target_files.py) 238*5a923131SAndroid Build Coastguard Workeror [OTA Generator](https://github.com/google/ota-generator) should be used. 239*5a923131SAndroid Build Coastguard Worker 240*5a923131SAndroid Build Coastguard Worker### Update Payload File Specification 241*5a923131SAndroid Build Coastguard Worker 242*5a923131SAndroid Build Coastguard WorkerEach update payload file has a specific structure defined in the table below: 243*5a923131SAndroid Build Coastguard Worker 244*5a923131SAndroid Build Coastguard Worker| Field | Size (bytes) | Type | Description | 245*5a923131SAndroid Build Coastguard Worker| ----------------------- | ------------ | ------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------- | 246*5a923131SAndroid Build Coastguard Worker| Magic Number | 4 | char[4] | Magic string "CrAU" identifying this is an update payload. | 247*5a923131SAndroid Build Coastguard Worker| Major Version | 8 | uint64 | Payload major version number. | 248*5a923131SAndroid Build Coastguard Worker| Manifest Size | 8 | uint64 | Manifest size in bytes. | 249*5a923131SAndroid Build Coastguard Worker| Manifest Signature Size | 4 | uint32 | Manifest signature blob size in bytes (only in major version 2). | 250*5a923131SAndroid Build Coastguard Worker| Manifest | Varies | [DeltaArchiveManifest] | The list of operations to be performed. | 251*5a923131SAndroid Build Coastguard Worker| Manifest Signature | Varies | [Signatures] | The signature of the first five fields. There could be multiple signatures if the key has changed. | 252*5a923131SAndroid Build Coastguard Worker| Payload Data | Varies | List of raw or compressed data blobs | The list of binary blobs used by operations in the metadata. | 253*5a923131SAndroid Build Coastguard Worker| Payload Signature Size | Varies | uint64 | The size of the payload signature. | 254*5a923131SAndroid Build Coastguard Worker| Payload Signature | Varies | [Signatures] | The signature of the entire payload except the metadata signature. There could be multiple signatures if the key has changed. | 255*5a923131SAndroid Build Coastguard Worker 256*5a923131SAndroid Build Coastguard Worker### Delta vs. Full Update Payloads 257*5a923131SAndroid Build Coastguard Worker 258*5a923131SAndroid Build Coastguard WorkerThere are two types of payload: Full and Delta. A full payload is generated 259*5a923131SAndroid Build Coastguard Workersolely from the target image (the image we want to update to) and has all the 260*5a923131SAndroid Build Coastguard Workerdata necessary to update the inactive partition. Hence, full payloads can be 261*5a923131SAndroid Build Coastguard Workerquite large in size. A delta payload, on the other hand, is a differential 262*5a923131SAndroid Build Coastguard Workerupdate generated by comparing the source image (the active partitions) and the 263*5a923131SAndroid Build Coastguard Workertarget image and producing the diffs between these two images. It is basically a 264*5a923131SAndroid Build Coastguard Workerdifferential update similar to applications like `diff` or `bsdiff`. Hence, 265*5a923131SAndroid Build Coastguard Workerupdating the system using the delta payloads requires the system to read parts 266*5a923131SAndroid Build Coastguard Workerof the active partition in order to update the inactive partition (or 267*5a923131SAndroid Build Coastguard Workerreconstruct the target partition). The delta payloads are significantly smaller 268*5a923131SAndroid Build Coastguard Workerthan the full payloads. The structure of the payload is equal for both types. 269*5a923131SAndroid Build Coastguard Worker 270*5a923131SAndroid Build Coastguard WorkerPayload generation is quite resource intensive and its tools are implemented 271*5a923131SAndroid Build Coastguard Workerwith high parallelism. 272*5a923131SAndroid Build Coastguard Worker 273*5a923131SAndroid Build Coastguard Worker#### Generating Full Payloads 274*5a923131SAndroid Build Coastguard Worker 275*5a923131SAndroid Build Coastguard WorkerA full payload is generated by breaking the partition into 2MiB (configurable) 276*5a923131SAndroid Build Coastguard Workerchunks and either compressing them using bzip2 or XZ algorithms or keeping it as 277*5a923131SAndroid Build Coastguard Workerraw data depending on which produces smaller data. Full payloads are much larger 278*5a923131SAndroid Build Coastguard Workerin comparison to delta payloads hence require longer download time if the 279*5a923131SAndroid Build Coastguard Workernetwork bandwidth is limited. On the other hand, full payloads are a bit faster 280*5a923131SAndroid Build Coastguard Workerto apply because the system doesn’t need to read data from the source partition. 281*5a923131SAndroid Build Coastguard Worker 282*5a923131SAndroid Build Coastguard Worker#### Generating Delta Payloads 283*5a923131SAndroid Build Coastguard Worker 284*5a923131SAndroid Build Coastguard WorkerDelta payloads are generated by looking at both the source and target images 285*5a923131SAndroid Build Coastguard Workerdata on a file and metadata basis (more precisely, the file system level on each 286*5a923131SAndroid Build Coastguard Workerappropriate partition). The reason we can generate delta payloads is that Chrome 287*5a923131SAndroid Build Coastguard WorkerOS partitions are read only. So with high certainty we can assume the active 288*5a923131SAndroid Build Coastguard Workerpartitions on the client’s device is bit-by-bit equal to the original partitions 289*5a923131SAndroid Build Coastguard Workergenerated in the image generation/signing phase. The process for generating a 290*5a923131SAndroid Build Coastguard Workerdelta payload is roughly as follows: 291*5a923131SAndroid Build Coastguard Worker 292*5a923131SAndroid Build Coastguard Worker1. Find all the zero-filled blocks on the target partition and produce `ZERO` 293*5a923131SAndroid Build Coastguard Worker operation for them. `ZERO` operation basically discards the associated 294*5a923131SAndroid Build Coastguard Worker blocks (depending on the implementation). 295*5a923131SAndroid Build Coastguard Worker2. Find all the blocks that have not changed between the source and target 296*5a923131SAndroid Build Coastguard Worker partitions by directly comparing one-to-one source and target blocks and 297*5a923131SAndroid Build Coastguard Worker produce `SOURCE_COPY` operation. 298*5a923131SAndroid Build Coastguard Worker3. List all the files (and their associated blocks) in the source and target 299*5a923131SAndroid Build Coastguard Worker partitions and remove blocks (and files) which we have already generated 300*5a923131SAndroid Build Coastguard Worker operations for in the last two steps. Assign the remaining metadata (inodes, 301*5a923131SAndroid Build Coastguard Worker etc) of each partition as a file. 302*5a923131SAndroid Build Coastguard Worker4. If a file is new, generate a `REPLACE`, `REPLACE_XZ`, or `REPLACE_BZ` 303*5a923131SAndroid Build Coastguard Worker operation for its data blocks depending on which one generates a smaller 304*5a923131SAndroid Build Coastguard Worker data blob. 305*5a923131SAndroid Build Coastguard Worker5. For each other file, compare the source and target blocks and produce a 306*5a923131SAndroid Build Coastguard Worker `SOURCE_BSDIFF` or `PUFFDIFF` operation depending on which one generates a 307*5a923131SAndroid Build Coastguard Worker smaller data blob. These two operations produce binary diffs between a 308*5a923131SAndroid Build Coastguard Worker source and target data blob. (Look at [bsdiff] and [puffin] for details of 309*5a923131SAndroid Build Coastguard Worker such binary differential programs!) 310*5a923131SAndroid Build Coastguard Worker6. Sort the operations based on their target partitions’ block offset. 311*5a923131SAndroid Build Coastguard Worker7. Optionally merge same or similar operations next to each other into larger 312*5a923131SAndroid Build Coastguard Worker operations for better efficiency and potentially smaller payloads. 313*5a923131SAndroid Build Coastguard Worker 314*5a923131SAndroid Build Coastguard WorkerFull payloads can only contain `REPLACE`, `REPLACE_BZ`, and `REPLACE_XZ` 315*5a923131SAndroid Build Coastguard Workeroperations. Delta payloads can contain any operations. 316*5a923131SAndroid Build Coastguard Worker 317*5a923131SAndroid Build Coastguard Worker### Major and Minor versions 318*5a923131SAndroid Build Coastguard Worker 319*5a923131SAndroid Build Coastguard WorkerThe major and minor versions specify the update payload file format and the 320*5a923131SAndroid Build Coastguard Workercapability of the updater client to accept certain types of update payloads 321*5a923131SAndroid Build Coastguard Workerrespectively. These numbers are [hard coded] in the updater client. 322*5a923131SAndroid Build Coastguard Worker 323*5a923131SAndroid Build Coastguard WorkerMajor version is basically the update payload file version specified in the 324*5a923131SAndroid Build Coastguard Worker[update payload file specification] above (second field). Each updater client 325*5a923131SAndroid Build Coastguard Workersupports a range of major versions. Currently, there are only two major 326*5a923131SAndroid Build Coastguard Workerversions: 1, and 2. And both Chrome OS and Android are on major version 2 (major 327*5a923131SAndroid Build Coastguard Workerversion 1 is being deprecated). Whenever there are new additions that cannot be 328*5a923131SAndroid Build Coastguard Workerfitted in the [Manifest protobuf], we need to uprev the major version. Upreving 329*5a923131SAndroid Build Coastguard Workermajor version should be done with utmost care because older clients do not know 330*5a923131SAndroid Build Coastguard Workerhow to handle the newer versions. Any major version uprev in Chrome OS should be 331*5a923131SAndroid Build Coastguard Workerassociated with a GoldenEye stepping stone. 332*5a923131SAndroid Build Coastguard Worker 333*5a923131SAndroid Build Coastguard WorkerMinor version defines the capability of the updater client to accept certain 334*5a923131SAndroid Build Coastguard Workeroperations or perform certain actions. Each updater client supports a range of 335*5a923131SAndroid Build Coastguard Workerminor versions. For example, the updater client with minor version 4 (or less) 336*5a923131SAndroid Build Coastguard Workerdoes not know how to handle a `PUFFDIFF` operation. So when generating a delta 337*5a923131SAndroid Build Coastguard Workerpayload for an image which has an updater client with minor version 4 (or less) 338*5a923131SAndroid Build Coastguard Workerwe cannot produce PUFFDIFF operation for it. The payload generation process 339*5a923131SAndroid Build Coastguard Workerlooks at the source image’s minor version to decide the type of operations it 340*5a923131SAndroid Build Coastguard Workersupports and only a payload that confirms to those restrictions. Similarly, if 341*5a923131SAndroid Build Coastguard Workerthere is a bug in a client with a specific minor version, an uprev in the minor 342*5a923131SAndroid Build Coastguard Workerversion helps with avoiding to generate payloads that cause that bug to 343*5a923131SAndroid Build Coastguard Workermanifest. However, upreving minor versions is quite expensive too in terms of 344*5a923131SAndroid Build Coastguard Workermaintainability and it can be error prone. So one should practice caution when 345*5a923131SAndroid Build Coastguard Workermaking such a change. 346*5a923131SAndroid Build Coastguard Worker 347*5a923131SAndroid Build Coastguard WorkerMinor versions are irrelevant in full payloads. Full payloads should always be 348*5a923131SAndroid Build Coastguard Workerable to be applied for very old clients. The reason is that the updater clients 349*5a923131SAndroid Build Coastguard Workermay not send their current version, so if we had different types of full 350*5a923131SAndroid Build Coastguard Workerpayloads, we would not have known which version to serve to the client. 351*5a923131SAndroid Build Coastguard Worker 352*5a923131SAndroid Build Coastguard Worker### Signed vs Unsigned Payloads 353*5a923131SAndroid Build Coastguard Worker 354*5a923131SAndroid Build Coastguard WorkerUpdate payloads can be signed (with private/public key pairs) for use in 355*5a923131SAndroid Build Coastguard Workerproduction or be kept unsigned for use in testing. Tools like `delta_generator` 356*5a923131SAndroid Build Coastguard Workerhelp with generating metadata and payload hashes or signing the payloads given 357*5a923131SAndroid Build Coastguard Workerprivate keys. 358*5a923131SAndroid Build Coastguard Worker 359*5a923131SAndroid Build Coastguard Worker## update_payload Scripts 360*5a923131SAndroid Build Coastguard Worker 361*5a923131SAndroid Build Coastguard Worker[update_payload] contains a set of python scripts used mostly to validate 362*5a923131SAndroid Build Coastguard Workerpayload generation and application. We normally test the update payloads using 363*5a923131SAndroid Build Coastguard Workeran actual device (live tests). [`brillo_update_payload`] script can be used to 364*5a923131SAndroid Build Coastguard Workergenerate and test applying of a payload on a host device machine. These tests 365*5a923131SAndroid Build Coastguard Workercan be viewed as dynamic tests without the need for an actual device. Other 366*5a923131SAndroid Build Coastguard Worker`update_payload` scripts (like [`check_update_payload`]) can be used to 367*5a923131SAndroid Build Coastguard Workerstatically check that a payload is in the correct state and its application 368*5a923131SAndroid Build Coastguard Workerworks correctly. These scripts actually apply the payload statically without 369*5a923131SAndroid Build Coastguard Workerrunning the code in payload_consumer. 370*5a923131SAndroid Build Coastguard Worker 371*5a923131SAndroid Build Coastguard Worker## Postinstall 372*5a923131SAndroid Build Coastguard Worker 373*5a923131SAndroid Build Coastguard Worker[Postinstall] is a process called after the updater client writes the new image 374*5a923131SAndroid Build Coastguard Workerartifacts to the inactive partitions. One of postinstall's main responsibilities 375*5a923131SAndroid Build Coastguard Workeris to recreate the dm-verity tree hash at the end of the root partition. Among 376*5a923131SAndroid Build Coastguard Workerother things, it installs new firmware updates or any board specific 377*5a923131SAndroid Build Coastguard Workerprocesses. Postinstall runs in separate chroot inside the newly installed 378*5a923131SAndroid Build Coastguard Workerpartition. So it is quite separated from the rest of the active running 379*5a923131SAndroid Build Coastguard Workersystem. Anything that needs to be done after an update and before the device is 380*5a923131SAndroid Build Coastguard Workerrebooted, should be implemented inside the postinstall. 381*5a923131SAndroid Build Coastguard Worker 382*5a923131SAndroid Build Coastguard Worker## Building Update Engine 383*5a923131SAndroid Build Coastguard Worker 384*5a923131SAndroid Build Coastguard WorkerYou can build `update_engine` the same as other platform applications: 385*5a923131SAndroid Build Coastguard Worker 386*5a923131SAndroid Build Coastguard Worker### Setup 387*5a923131SAndroid Build Coastguard Worker 388*5a923131SAndroid Build Coastguard WorkerRun these commands at top of Android repository before building anything. 389*5a923131SAndroid Build Coastguard WorkerYou only need to do this once per shell. 390*5a923131SAndroid Build Coastguard Worker 391*5a923131SAndroid Build Coastguard Worker* `source build/envsetup.sh` 392*5a923131SAndroid Build Coastguard Worker* `lunch aosp_cf_x86_64_only_phone-userdebug` (Or replace aosp_cf_x86_64_only_phone-userdebug with your own target) 393*5a923131SAndroid Build Coastguard Worker 394*5a923131SAndroid Build Coastguard Worker 395*5a923131SAndroid Build Coastguard Worker### Building 396*5a923131SAndroid Build Coastguard Worker 397*5a923131SAndroid Build Coastguard Worker`m update_engine update_engine_client delta_generator` 398*5a923131SAndroid Build Coastguard Worker 399*5a923131SAndroid Build Coastguard Worker## Running Unit Tests 400*5a923131SAndroid Build Coastguard Worker 401*5a923131SAndroid Build Coastguard Worker[Running unit tests similar to other platforms]: 402*5a923131SAndroid Build Coastguard Worker 403*5a923131SAndroid Build Coastguard Worker* `atest update_engine_unittests` You will need a device connected to 404*5a923131SAndroid Build Coastguard Worker your laptop and accessible via ADB to do this. Cuttlefish works as well. 405*5a923131SAndroid Build Coastguard Worker* `atest update_engine_host_unittests` Run a subset of tests on host, no device 406*5a923131SAndroid Build Coastguard Workerrequired. 407*5a923131SAndroid Build Coastguard Worker 408*5a923131SAndroid Build Coastguard Worker## Initiating a Configured Update 409*5a923131SAndroid Build Coastguard Worker 410*5a923131SAndroid Build Coastguard WorkerThere are different methods to initiate an update: 411*5a923131SAndroid Build Coastguard Worker 412*5a923131SAndroid Build Coastguard Worker* Click on the “Check For Update” button in setting’s About page. There is no 413*5a923131SAndroid Build Coastguard Worker way to configure this way of update check. 414*5a923131SAndroid Build Coastguard Worker* Use the [`scripts/update_device.py`] program and pass a path to your OTA zip file. 415*5a923131SAndroid Build Coastguard Worker 416*5a923131SAndroid Build Coastguard Worker 417*5a923131SAndroid Build Coastguard Worker 418*5a923131SAndroid Build Coastguard Worker## Note to Developers and Maintainers 419*5a923131SAndroid Build Coastguard Worker 420*5a923131SAndroid Build Coastguard WorkerWhen changing the update engine source code be extra careful about these things: 421*5a923131SAndroid Build Coastguard Worker 422*5a923131SAndroid Build Coastguard Worker### Do NOT Break Backward Compatibility 423*5a923131SAndroid Build Coastguard Worker 424*5a923131SAndroid Build Coastguard WorkerAt each release cycle we should be able to generate full and delta payloads that 425*5a923131SAndroid Build Coastguard Workercan correctly be applied to older devices that run older versions of the update 426*5a923131SAndroid Build Coastguard Workerengine client. So for example, removing or not passing arguments in the metadata 427*5a923131SAndroid Build Coastguard Workerproto file might break older clients. Or passing operations that are not 428*5a923131SAndroid Build Coastguard Workerunderstood in older clients will break them. Whenever changing anything in the 429*5a923131SAndroid Build Coastguard Workerpayload generation process, ask yourself this question: Would it work on older 430*5a923131SAndroid Build Coastguard Workerclients? If not, do I need to control it with minor versions or any other means. 431*5a923131SAndroid Build Coastguard Worker 432*5a923131SAndroid Build Coastguard WorkerEspecially regarding enterprise rollback, a newer updater client should be able 433*5a923131SAndroid Build Coastguard Workerto accept an older update payload. Normally this happens using a full payload, 434*5a923131SAndroid Build Coastguard Workerbut care should be taken in order to not break this compatibility. 435*5a923131SAndroid Build Coastguard Worker 436*5a923131SAndroid Build Coastguard Worker### Think About The Future 437*5a923131SAndroid Build Coastguard Worker 438*5a923131SAndroid Build Coastguard WorkerWhen creating a change in the update engine, think about 5 years from now: 439*5a923131SAndroid Build Coastguard Worker 440*5a923131SAndroid Build Coastguard Worker* How can the change be implemented that five years from now older clients 441*5a923131SAndroid Build Coastguard Worker don’t break? 442*5a923131SAndroid Build Coastguard Worker* How is it going to be maintained five years from now? 443*5a923131SAndroid Build Coastguard Worker* How can it make it easier for future changes without breaking older clients 444*5a923131SAndroid Build Coastguard Worker or incurring heavy maintenance costs? 445*5a923131SAndroid Build Coastguard Worker 446*5a923131SAndroid Build Coastguard Worker### Prefer Not To Implement Your Feature In The Updater Client 447*5a923131SAndroid Build Coastguard WorkerIf a feature can be implemented from server side, Do NOT implement it in the 448*5a923131SAndroid Build Coastguard Workerclient updater. Because the client updater can be fragile at points and small 449*5a923131SAndroid Build Coastguard Workermistakes can have catastrophic consequences. For example, if a bug is introduced 450*5a923131SAndroid Build Coastguard Workerin the updater client that causes it to crash right before checking for update 451*5a923131SAndroid Build Coastguard Workerand we can't quite catch this bug early in the release process, then the 452*5a923131SAndroid Build Coastguard Workerproduction devices which have already moved to the new buggy system, may no 453*5a923131SAndroid Build Coastguard Workerlonger receive automatic updates anymore. So, always think if the feature is 454*5a923131SAndroid Build Coastguard Workerbeing implemented can be done form the server side (with potentially minimal 455*5a923131SAndroid Build Coastguard Workerchanges to the client updater)? Or can the feature be moved to another service 456*5a923131SAndroid Build Coastguard Workerwith minimal interface to the updater client. Answering these questions will pay 457*5a923131SAndroid Build Coastguard Workeroff greatly in the future. 458*5a923131SAndroid Build Coastguard Worker 459*5a923131SAndroid Build Coastguard Worker### Be Respectful Of Other Code Bases 460*5a923131SAndroid Build Coastguard Worker 461*5a923131SAndroid Build Coastguard Worker~~The current update engine code base is used in many projects like Android.~~~ 462*5a923131SAndroid Build Coastguard Worker 463*5a923131SAndroid Build Coastguard WorkerThe Android and ChromeOS codebase have officially diverged. 464*5a923131SAndroid Build Coastguard Worker 465*5a923131SAndroid Build Coastguard WorkerWe sync the code base among these two projects frequently. Try to not break Android 466*5a923131SAndroid Build Coastguard Workeror other systems that share the update engine code. Whenever landing a change, 467*5a923131SAndroid Build Coastguard Workeralways think about whether Android needs that change: 468*5a923131SAndroid Build Coastguard Worker 469*5a923131SAndroid Build Coastguard Worker* How will it affect Android? 470*5a923131SAndroid Build Coastguard Worker* Can the change be moved to an interface and stubs implementations be 471*5a923131SAndroid Build Coastguard Worker implemented so as not to affect Android? 472*5a923131SAndroid Build Coastguard Worker* Can Chrome OS or Android specific code be guarded by macros? 473*5a923131SAndroid Build Coastguard Worker 474*5a923131SAndroid Build Coastguard WorkerAs a basic measure, if adding/removing/renaming code, make sure to change both 475*5a923131SAndroid Build Coastguard Worker`build.gn` and `Android.bp`. Do not bring Chrome OS specific code (for example 476*5a923131SAndroid Build Coastguard Workerother libraries that live in `system_api` or `dlcservice`) into the common code 477*5a923131SAndroid Build Coastguard Workerof update_engine. Try to separate these concerns using best software engineering 478*5a923131SAndroid Build Coastguard Workerpractices. 479*5a923131SAndroid Build Coastguard Worker 480*5a923131SAndroid Build Coastguard Worker### Merging from Android (or other code bases) 481*5a923131SAndroid Build Coastguard Worker 482*5a923131SAndroid Build Coastguard WorkerChrome OS tracks the Android code as an [upstream branch]. To merge the Android 483*5a923131SAndroid Build Coastguard Workercode to Chrome OS (or vice versa) just do a `git merge` of that branch into 484*5a923131SAndroid Build Coastguard WorkerChrome OS, test it using whatever means and upload a merge commit. 485*5a923131SAndroid Build Coastguard Worker 486*5a923131SAndroid Build Coastguard Worker```bash 487*5a923131SAndroid Build Coastguard Workerrepo start merge-aosp 488*5a923131SAndroid Build Coastguard Workergit merge --no-ff --strategy=recursive -X patience cros/upstream 489*5a923131SAndroid Build Coastguard Workerrepo upload --cbr --no-verify . 490*5a923131SAndroid Build Coastguard Worker``` 491*5a923131SAndroid Build Coastguard Worker 492*5a923131SAndroid Build Coastguard Worker[Postinstall]: #postinstall 493*5a923131SAndroid Build Coastguard Worker[update payload file specification]: #update-payload-file-specification 494*5a923131SAndroid Build Coastguard Worker[OTA]: https://source.android.com/devices/tech/ota 495*5a923131SAndroid Build Coastguard Worker[DLC]: https://chromium.googlesource.com/chromiumos/platform2/+/master/dlcservice 496*5a923131SAndroid Build Coastguard Worker[`chromeos-setgoodkernel`]: https://chromium.googlesource.com/chromiumos/platform2/+/master/installer/chromeos-setgoodkernel 497*5a923131SAndroid Build Coastguard Worker[D-Bus interface]: /dbus_bindings/org.chromium.UpdateEngineInterface.dbus-xml 498*5a923131SAndroid Build Coastguard Worker[this repository]: / 499*5a923131SAndroid Build Coastguard Worker[UpdateManager]: /update_manager/update_manager.cc 500*5a923131SAndroid Build Coastguard Worker[update_manager]: /update_manager/ 501*5a923131SAndroid Build Coastguard Worker[P2P update related code]: https://chromium.googlesource.com/chromiumos/platform2/+/master/p2p/ 502*5a923131SAndroid Build Coastguard Worker[`cros_generate_update_payloads`]: https://chromium.googlesource.com/chromiumos/chromite/+/master/scripts/cros_generate_update_payload.py 503*5a923131SAndroid Build Coastguard Worker[`chromite/lib/paygen`]: https://chromium.googlesource.com/chromiumos/chromite/+/master/lib/paygen/ 504*5a923131SAndroid Build Coastguard Worker[DeltaArchiveManifest]: /update_metadata.proto#302 505*5a923131SAndroid Build Coastguard Worker[Signatures]: /update_metadata.proto#122 506*5a923131SAndroid Build Coastguard Worker[hard coded]: /update_engine.conf 507*5a923131SAndroid Build Coastguard Worker[Manifest protobuf]: /update_metadata.proto 508*5a923131SAndroid Build Coastguard Worker[update_payload]: /scripts/ 509*5a923131SAndroid Build Coastguard Worker[Postinstall]: https://chromium.googlesource.com/chromiumos/platform2/+/master/installer/chromeos-postinst 510*5a923131SAndroid Build Coastguard Worker[`update_engine` protobufs]: https://chromium.googlesource.com/chromiumos/platform2/+/master/system_api/dbus/update_engine/ 511*5a923131SAndroid Build Coastguard Worker[Running unit tests similar to other platforms]: https://chromium.googlesource.com/chromiumos/docs/+/master/testing/running_unit_tests.md 512*5a923131SAndroid Build Coastguard Worker[Nebraska]: https://chromium.googlesource.com/chromiumos/platform/dev-util/+/master/nebraska/ 513*5a923131SAndroid Build Coastguard Worker[upstream branch]: https://chromium.googlesource.com/aosp/platform/system/update_engine/+/upstream 514*5a923131SAndroid Build Coastguard Worker[`cros flash`]: https://chromium.googlesource.com/chromiumos/docs/+/master/cros_flash.md 515*5a923131SAndroid Build Coastguard Worker[bsdiff]: https://android.googlesource.com/platform/external/bsdiff/+/master 516*5a923131SAndroid Build Coastguard Worker[puffin]: https://android.googlesource.com/platform/external/puffin/+/master 517*5a923131SAndroid Build Coastguard Worker[`update_engine_client`]: /update_engine_client.cc 518*5a923131SAndroid Build Coastguard Worker[`brillo_update_payload`]: /scripts/brillo_update_payload 519*5a923131SAndroid Build Coastguard Worker[`check_update_payload`]: /scripts/paycheck.py 520*5a923131SAndroid Build Coastguard Worker[Dev Server]: https://chromium.googlesource.com/chromiumos/chromite/+/master/docs/devserver.md 521