mirror of
https://github.com/chenxiaolong/avbroot.git
synced 2026-07-03 14:05:11 +02:00
Compare commits
58 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 02ae9cd0e3 | |||
| 9c92d32a80 | |||
| 296df25fb9 | |||
| 352352b4d4 | |||
| e28ef42c0e | |||
| 5775c86e5b | |||
| 3dc8e07c0f | |||
| 040dcd1a5c | |||
| 41a578975f | |||
| e9638d25b0 | |||
| e25080ddef | |||
| 0e7d778cf5 | |||
| 8ca1a289a8 | |||
| dfbc2f807f | |||
| e55cf3d679 | |||
| 7c38c5609b | |||
| 13c910274e | |||
| dd40f64918 | |||
| 7113fb32ef | |||
| 9c9d656ea2 | |||
| e06674a7f0 | |||
| cf5ef13e47 | |||
| 5fada419cb | |||
| eeea9f41b4 | |||
| 0bebf120c6 | |||
| 264c602fdb | |||
| 343e2e279c | |||
| 59ca759262 | |||
| 83ab475c11 | |||
| fe54640029 | |||
| fec1840a5f | |||
| 395f6934ff | |||
| a83b2fbfa9 | |||
| e18ef20e4d | |||
| b140620ed3 | |||
| dd9d8959fd | |||
| 9a7cece973 | |||
| aac3aded78 | |||
| 24320d4fae | |||
| 6800ae073e | |||
| 4d90ee2ac6 | |||
| 6b087c0844 | |||
| d384a8a99b | |||
| bc358c62af | |||
| fc05cb901a | |||
| 031ac8aa31 | |||
| 4a1dab4069 | |||
| bf885e40cf | |||
| c2a441cf78 | |||
| f6c6a9509a | |||
| 5a00995366 | |||
| edf537aad6 | |||
| f006f20209 | |||
| f36c1ca451 | |||
| 29b72961a3 | |||
| e105efb6d3 | |||
| 2a7df104ed | |||
| 8e52a9cf8c |
@@ -19,6 +19,7 @@ jobs:
|
||||
# https://github.com/rust-lang/rust/issues/78210
|
||||
RUSTFLAGS: -C strip=symbols -C target-feature=+crt-static
|
||||
TARGETS: ${{ join(matrix.artifact.targets, ' ') || matrix.artifact.name }}
|
||||
ANDROID_API: ${{ matrix.artifact.android_api }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
@@ -33,6 +34,12 @@ jobs:
|
||||
- aarch64-apple-darwin
|
||||
- x86_64-apple-darwin
|
||||
combine: lipo
|
||||
# ubuntu-latest is not 24.04 yet and 22.04's qemu-user-static segfaults.
|
||||
- os: ubuntu-24.04
|
||||
name: aarch64-linux-android31
|
||||
targets:
|
||||
- aarch64-linux-android
|
||||
android_api: '31'
|
||||
steps:
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@v4
|
||||
@@ -40,6 +47,26 @@ jobs:
|
||||
# For git describe
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Install qemu-user-static
|
||||
if: ${{ contains(matrix.artifact.name, 'android') }}
|
||||
shell: bash
|
||||
run: |
|
||||
sudo apt-get -y update
|
||||
sudo apt-get -y install qemu-user-static
|
||||
|
||||
- name: Set Android temporary directory
|
||||
if: ${{ contains(matrix.artifact.name, 'android') }}
|
||||
shell: bash
|
||||
run: |
|
||||
echo "TMPDIR=/tmp" >> "${GITHUB_ENV}"
|
||||
|
||||
- name: Install cargo-android
|
||||
shell: bash
|
||||
run: |
|
||||
cargo install \
|
||||
--git https://github.com/chenxiaolong/cargo-android \
|
||||
--tag v0.1.1
|
||||
|
||||
- name: Get version
|
||||
id: get_version
|
||||
shell: bash
|
||||
@@ -63,7 +90,8 @@ jobs:
|
||||
shell: bash
|
||||
run: |
|
||||
for target in ${TARGETS}; do
|
||||
cargo clippy --release --workspace --features static \
|
||||
cargo android \
|
||||
clippy --release --workspace --features static \
|
||||
--target "${target}"
|
||||
done
|
||||
|
||||
@@ -71,7 +99,8 @@ jobs:
|
||||
shell: bash
|
||||
run: |
|
||||
for target in ${TARGETS}; do
|
||||
cargo build --release --workspace --features static \
|
||||
cargo android \
|
||||
build --release --workspace --features static \
|
||||
--target "${target}"
|
||||
done
|
||||
|
||||
@@ -79,7 +108,8 @@ jobs:
|
||||
shell: bash
|
||||
run: |
|
||||
for target in ${TARGETS}; do
|
||||
cargo test --release --workspace --features static \
|
||||
cargo android \
|
||||
test --release --workspace --features static \
|
||||
--target "${target}"
|
||||
done
|
||||
|
||||
@@ -87,7 +117,8 @@ jobs:
|
||||
shell: bash
|
||||
run: |
|
||||
for target in ${TARGETS}; do
|
||||
cargo run --release -p e2e --features static \
|
||||
cargo android \
|
||||
run --release -p e2e --features static \
|
||||
--target "${target}" \
|
||||
-- test -a -c e2e/e2e.toml
|
||||
done
|
||||
|
||||
@@ -7,6 +7,50 @@
|
||||
to update the actual links at the bottom of the file.
|
||||
-->
|
||||
|
||||
### Version 3.7.0
|
||||
|
||||
* Fix a nasty regression since version 2.0.0 where recovery mode's `otacerts.zip` modifications were lost when using `--prepatched` with Magisk on some older devices, like the Pixel 4a ([Issue #356], [PR #357])
|
||||
* This affected older devices without `vendor_boot` or `recovery` partitions.
|
||||
* **This caused sideloading patched OTA updates from recovery mode to break on the affected devices.** To fix the problem without wiping the device and starting fresh, please follow the [steps in the PR](https://github.com/chenxiaolong/avbroot/pull/357#issuecomment-2365343050).
|
||||
* Print a useful error message when trying to prompt for a passphrase without an interactive terminal ([PR #336])
|
||||
* Add a new `--zip-mode seekable` option to allow writing OTA zip files without data descriptors ([Issue #328], [PR #337])
|
||||
* Add new commands for packing and unpacking logical partition images (`super.img`) ([PR #342], [PR #343])
|
||||
* Add new commands for packing and unpacking Android sparse images ([PR #347])
|
||||
* Allow `avbroot payload repack` and `avbroot payload info` commands to read delta payloads ([PR #354])
|
||||
* Switch to passterm library for password prompts ([PR #355])
|
||||
|
||||
### Version 3.6.0
|
||||
|
||||
* Add support for gzip compression when computing CoW size estimates ([Issue #332], [PR #333])
|
||||
* This allows `--replace` to successfully replace dynamic partitions on legacy devices, like the Pixel 4a 5G
|
||||
* Minor code cleanup ([PR #334], [PR #335])
|
||||
|
||||
### Version 3.5.0
|
||||
|
||||
* Update all dependencies ([PR #329])
|
||||
* Add new unpack and pack commands for `payload.bin` files ([Issue #328], [PR #331])
|
||||
|
||||
### Version 3.4.1
|
||||
|
||||
* Update all dependencies ([PR #321])
|
||||
* Add support for Magisk 27006 ([PR #323])
|
||||
|
||||
### Version 3.4.0
|
||||
|
||||
* Fix (unreachable) minor error handling logic when attempting to use unsupported AVB signing algorithms ([PR #311])
|
||||
* Add support for performing signing operations with external programs ([Issue #310], [PR #312])
|
||||
* See the linked issue for an example of how to sign with a Yubikey.
|
||||
|
||||
### Version 3.3.0
|
||||
|
||||
* Recompute CoW size estimate when replacing dynamic partitions ([Issue #306], [PR #307])
|
||||
* Fixes out of space error when flashing a patched OTA that uses `--replace` to replace a dynamic partition (eg. `system`) with a larger or more incompressible image
|
||||
* Add `avbroot payload info` subcommand for inspecting `payload.bin` headers ([PR #309])
|
||||
|
||||
### Version 3.2.3
|
||||
|
||||
* Add prebuilt binary for Android (aarch64) ([PR #304])
|
||||
|
||||
### Version 3.2.2
|
||||
|
||||
* Add new `--recompute-size` option to `avbroot avb pack` to automatically recompute the image size for resizable images ([Discussion #294], [PR #296])
|
||||
@@ -207,6 +251,11 @@ Behind-the-scenes changes:
|
||||
[Issue #285]: https://github.com/chenxiaolong/avbroot/issues/285
|
||||
[Issue #291]: https://github.com/chenxiaolong/avbroot/issues/291
|
||||
[Issue #301]: https://github.com/chenxiaolong/avbroot/issues/301
|
||||
[Issue #306]: https://github.com/chenxiaolong/avbroot/issues/306
|
||||
[Issue #310]: https://github.com/chenxiaolong/avbroot/issues/310
|
||||
[Issue #328]: https://github.com/chenxiaolong/avbroot/issues/328
|
||||
[Issue #332]: https://github.com/chenxiaolong/avbroot/issues/332
|
||||
[Issue #356]: https://github.com/chenxiaolong/avbroot/issues/356
|
||||
[PR #130]: https://github.com/chenxiaolong/avbroot/pull/130
|
||||
[PR #132]: https://github.com/chenxiaolong/avbroot/pull/132
|
||||
[PR #133]: https://github.com/chenxiaolong/avbroot/pull/133
|
||||
@@ -295,3 +344,23 @@ Behind-the-scenes changes:
|
||||
[PR #293]: https://github.com/chenxiaolong/avbroot/pull/293
|
||||
[PR #296]: https://github.com/chenxiaolong/avbroot/pull/296
|
||||
[PR #297]: https://github.com/chenxiaolong/avbroot/pull/297
|
||||
[PR #304]: https://github.com/chenxiaolong/avbroot/pull/304
|
||||
[PR #307]: https://github.com/chenxiaolong/avbroot/pull/307
|
||||
[PR #309]: https://github.com/chenxiaolong/avbroot/pull/309
|
||||
[PR #311]: https://github.com/chenxiaolong/avbroot/pull/311
|
||||
[PR #312]: https://github.com/chenxiaolong/avbroot/pull/312
|
||||
[PR #321]: https://github.com/chenxiaolong/avbroot/pull/321
|
||||
[PR #323]: https://github.com/chenxiaolong/avbroot/pull/323
|
||||
[PR #329]: https://github.com/chenxiaolong/avbroot/pull/329
|
||||
[PR #331]: https://github.com/chenxiaolong/avbroot/pull/331
|
||||
[PR #333]: https://github.com/chenxiaolong/avbroot/pull/333
|
||||
[PR #334]: https://github.com/chenxiaolong/avbroot/pull/334
|
||||
[PR #335]: https://github.com/chenxiaolong/avbroot/pull/335
|
||||
[PR #336]: https://github.com/chenxiaolong/avbroot/pull/336
|
||||
[PR #337]: https://github.com/chenxiaolong/avbroot/pull/337
|
||||
[PR #342]: https://github.com/chenxiaolong/avbroot/pull/342
|
||||
[PR #343]: https://github.com/chenxiaolong/avbroot/pull/343
|
||||
[PR #347]: https://github.com/chenxiaolong/avbroot/pull/347
|
||||
[PR #354]: https://github.com/chenxiaolong/avbroot/pull/354
|
||||
[PR #355]: https://github.com/chenxiaolong/avbroot/pull/355
|
||||
[PR #357]: https://github.com/chenxiaolong/avbroot/pull/357
|
||||
|
||||
Generated
+327
-334
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -4,7 +4,7 @@ members = ["avbroot", "e2e", "fuzz", "xtask"]
|
||||
resolver = "2"
|
||||
|
||||
[workspace.package]
|
||||
version = "3.2.2"
|
||||
version = "3.7.0"
|
||||
license = "GPL-3.0-only"
|
||||
edition = "2021"
|
||||
repository = "https://github.com/chenxiaolong/avbroot"
|
||||
|
||||
+134
@@ -252,3 +252,137 @@ avbroot hash-tree verify -i <input data file> -H <input hash tree file>
|
||||
```
|
||||
|
||||
This will check if the input file has any corrupted blocks. Currently, the command cannot report which specific blocks are corrupted, only whether the file is valid.
|
||||
|
||||
## `avbroot lp`
|
||||
|
||||
This set of commands is for working with LP (logical partition) images. These are the containers for dynamically-allocated partitions, like `system`. All LP images are supported:
|
||||
|
||||
* Empty images: These are the `super_empty.img` images in the factory images for newer Google Pixel devices. They define the layout of the `super` partition, but don't contain any actual data. They also do not contain a backup copy of the metadata. As an optimization, `fastboot` can fill in the actual data during flashing to avoid needing to reboot to fastbootd mode.
|
||||
* Normal images backed by a single device: These are standalone `super.img` images and are how logical partitions are physically stored on disk in most newer devices. They contain a backup copy of all metadata as well as actual partition data.
|
||||
* Normal images backed by multiple devices: These are images split across multiple files/partitions and are used on devices where support for LP was retrofitted. For example, the LP setup on newer Android builds for the Google Pixel 3a XL reuse the legacy `system` and `vendor` partitions because there is no `super` partition. These are similar to the single-file LP setups, except that data can be stored across all of the LP images. However, the metadata is only stored on the first LP image.
|
||||
|
||||
### Unpacking an LP image
|
||||
|
||||
```bash
|
||||
avbroot lp unpack -i <input LP image> [-i <input LP image>]...
|
||||
```
|
||||
|
||||
This subcommand unpacks the LP metadata to `lp.toml` and the partition images to the `lp_images` directory (for normal images).
|
||||
|
||||
If there are multiple images, they must be specified in order. If the order is not known, run `avbroot lp info` on each of the images. The one that successfully parses is the first image and the `block_devices` field in the output specifies the full ordering.
|
||||
|
||||
An LP image can have multiple slots. If the LP image originated from a factory image or OTA, all slots are likely identical. If the LP image was dumped from a real device that installed OTA updates in the past, the slots may differ. If the slots are not identical, then the `--slot` option is required to specify which slot to unpack.
|
||||
|
||||
### Packing an LP image
|
||||
|
||||
```bash
|
||||
avbroot lp pack -o <output LP image> [-o <output LP image>]...
|
||||
```
|
||||
|
||||
This subcommand packs a new LP image from the `lp.toml` file and `lp_images` directory (for normal images). Any `.img` files in the `lp_images` directory that don't have a corresponding entry in `lp.toml` are silently ignored.
|
||||
|
||||
All metadata slots in the newly packed LP image will be identical.
|
||||
|
||||
### Repacking an LP image
|
||||
|
||||
```bash
|
||||
avbroot lp repack -i <input LP image> [-i <input LP image>]... -o <output LP image> [-o <output LP image>]...
|
||||
```
|
||||
|
||||
This subcommand is logically equivalent to `avbroot lp unpack` followed by `avbroot lp pack`, except more efficient. Instead of unpacking and packing all partition images, the raw data is directly copied from the old LP image to the new LP image.
|
||||
|
||||
When `--slot` is specified, this is useful for discarding unwanted metadata slots and the partition data exclusive to them.
|
||||
|
||||
### Showing LP image metadata
|
||||
|
||||
```bash
|
||||
avbroot lp info -i <first LP image>
|
||||
```
|
||||
|
||||
This subcommand shows the LP image metadata, including all metadata slots. If there are multiple images, only the first one is needed because it is the only one that stores the metadata.
|
||||
|
||||
## `avbroot payload`
|
||||
|
||||
This set of commands is for working with payload binary files (`payload.bin`). The `unpack` and `pack` commands can only work with full payloads because they require the complete data to be available, but the `repack` and `info` commands also work with delta payloads.
|
||||
|
||||
### Unpacking a payload binary
|
||||
|
||||
```bash
|
||||
avbroot payload unpack -i <input payload>
|
||||
```
|
||||
|
||||
This subcommand unpacks the payload header information to `payload.toml` and the partition images to the `payload_images` directory.
|
||||
|
||||
Only full payload binaries can be unpacked. Delta payload binaries from incremental OTAs are not supported.
|
||||
|
||||
### Packing a payload binary
|
||||
|
||||
```bash
|
||||
avbroot payload pack -o <output payload> --key <OTA private key>
|
||||
```
|
||||
|
||||
This subcommand packs a new payload binary from the `payload.toml` file and `payload_images` directory. Any `.img` files in the `payload_images` directory that don't have a corresponding entry in `payload.toml` are silently ignored.
|
||||
|
||||
Packing a payload binary requires compressing all of the partition images, which is very CPU intensive. If re-signing an existing payload binary without making any other modifications is all that's needed, use the `repack` subcommand instead.
|
||||
|
||||
### Repacking a payload binary
|
||||
|
||||
```bash
|
||||
avbroot payload repack -i <input payload> -o <output payload> --key <OTA private key>
|
||||
```
|
||||
|
||||
This subcommand is logically equivalent to `avbroot payload unpack` followed by `avbroot payload pack`, except significantly more efficient. Instead of decompressing and recompressing all partition images, the raw data is directly copied from the input payload binary.
|
||||
|
||||
This is useful for re-signing a payload binary without making any other changes.
|
||||
|
||||
### Showing payload header information
|
||||
|
||||
```bash
|
||||
avbroot payload info -i <payload>
|
||||
```
|
||||
|
||||
This subcommand shows all of the payload header fields (which will likely be extremely long).
|
||||
|
||||
## `avbroot sparse`
|
||||
|
||||
This set of commands is for working with Android sparse images. All features of the file format are supported, including hole chunks and CRC32 checksums.
|
||||
|
||||
### Unpacking a sparse image
|
||||
|
||||
```bash
|
||||
avbroot sparse unpack -o <input sparse image> -o <output raw image>
|
||||
```
|
||||
|
||||
This subcommand unpacks a sparse image to a raw image. If the sparse image contains CRC32 checksums, they will be validated during unpacking. If the sparse image contains holes, the output image will be created as a native sparse file.
|
||||
|
||||
Certain fastboot factory images may have multiple sparse images, like `super_1.img`, `super_2.img`, etc., where they all touch a disjoint set of regions on the same partition. These can be unpacked by running this subcommand for each sparse image and specifying the `--preserve` option along with using the same output file. This preserves the existing data in the output file when unpacking each sparse image.
|
||||
|
||||
### Packing a sparse image
|
||||
|
||||
```bash
|
||||
avbroot sparse pack -i <input raw image> -o <output sparse image>
|
||||
```
|
||||
|
||||
This subcommand packs a new sparse image from a raw image. The default block size is 4096 bytes, which can be changed with the `--block-size` option.
|
||||
|
||||
By default, this will pack the entire input file. However, on Linux, there is an optimization where all holes in the input file, if it is a native sparse file, will be stored as hole chunks instead of `0`-filled chunks in the output sparse image.
|
||||
|
||||
To pack a partial sparse image, such as those used in the special fastboot factory images mentioned above, pass in `--region <start> <end>`. This option can be specified multiple times to pack multiple regions.
|
||||
|
||||
Unlike AOSP's `img2simg` tool, which never writes CRC32 checksums, this subcommand will write checksums if the input file has no holes and the entire file is being packed.
|
||||
|
||||
### Repacking a sparse image
|
||||
|
||||
```bash
|
||||
avbroot sparse repack -i <input sparse image> -o <output sparse image>
|
||||
```
|
||||
|
||||
This subcommand is logically equivalent to `avbroot sparse unpack` followed by `avbroot sparse pack`, except more efficient. This is useful for roundtrip testing of avbroot's sparse file parser.
|
||||
|
||||
### Showing sparse image metadata
|
||||
|
||||
```bash
|
||||
avbroot sparse info -i <input sparse image>
|
||||
```
|
||||
|
||||
This subcommand shows the sparse image metadata, including the header and all chunks.
|
||||
|
||||
@@ -35,7 +35,11 @@ avbroot applies the following patches to the partition images:
|
||||
|
||||
Repeat: **_ALWAYS leave `OEM unlocking` enabled if rooted._**
|
||||
|
||||
* Any operation that causes an improperly-signed boot image to be flashed will result in the device being unbootable and unrecoverable without unlocking the bootloader again (and thus, triggering a data wipe). This includes the `Direct install` method for updating Magisk. Magisk updates **must** be done by repatching the OTA, not via the app.
|
||||
* Any operation that causes an improperly-signed boot image to be flashed will result in the device being unbootable and unrecoverable without unlocking the bootloader again (and thus, triggering a data wipe). A couple ways an improperly-signed boot image could be flashed include:
|
||||
|
||||
* The `Direct install` method for updating Magisk. Magisk updates **must** be done by repatching the OTA, not via the app.
|
||||
|
||||
* The `Uninstall Magisk` feature in Magisk. If root access is no longer needed, Magisk **must** be removed by repatching the OTA with the `--rootless` option, not via the app.
|
||||
|
||||
If the boot image is ever modified, **do not reboot**. [Open an issue](https://github.com/chenxiaolong/avbroot/issues/new) for support and be very clear about what steps were done that lead to the situation. If Android is still running and root access works, it might be possible to recover without wiping and starting over.
|
||||
|
||||
@@ -126,21 +130,27 @@ When patching OTAs for multiple devices, generating unique keys for each device
|
||||
avbroot key generate-cert -k ota.key -o ota.crt
|
||||
```
|
||||
|
||||
The commands above are provided for convenience. avbroot is compatible with any standard PKCS8-encoded 4096-bit RSA private key and PEM-encoded X509 certificate, like those generated by openssl.
|
||||
The commands above are provided for convenience. avbroot is compatible with any standard PKCS#8-encoded 4096-bit RSA private key and PEM-encoded X509 certificate, like those generated by openssl.
|
||||
|
||||
If you lose your AVB or OTA signing key, you will no longer be able to sign new OTA zips. You will have to generate new signing keys and unlock your bootloader again (triggering a data wipe). Follow the [Usage section](#usage) as if doing an initial setup.
|
||||
|
||||
## Initial setup
|
||||
|
||||
1. Reboot into fastboot mode and unlock the bootloader if it isn't already unlocked. This will trigger a data wipe.
|
||||
1. Make sure that the version of fastboot is 34 or newer. Older versions have bugs that prevent the `fastboot flashall` command (required later) from working properly.
|
||||
|
||||
```bash
|
||||
fastboot --version
|
||||
```
|
||||
|
||||
2. Reboot into fastboot mode and unlock the bootloader if it isn't already unlocked. This will trigger a data wipe.
|
||||
|
||||
```bash
|
||||
fastboot flashing unlock
|
||||
```
|
||||
|
||||
2. When setting things up for the first time, the device must already be running the correct OS. Flash the original unpatched OTA if needed.
|
||||
3. When setting things up for the first time, the device must already be running the correct OS. Flash the original unpatched OTA if needed.
|
||||
|
||||
3. Extract the partition images from the patched OTA that are different from the original.
|
||||
4. Extract the partition images from the patched OTA that are different from the original.
|
||||
|
||||
```bash
|
||||
avbroot ota extract \
|
||||
@@ -151,17 +161,37 @@ If you lose your AVB or OTA signing key, you will no longer be able to sign new
|
||||
|
||||
If you prefer to extract and flash all OS partitions just to be safe, pass in `--all`.
|
||||
|
||||
4. Flash the partition images that were extracted.
|
||||
5. Set the `ANDROID_PRODUCT_OUT` environment variable to the directory containing the extracted files.
|
||||
|
||||
For sh/bash/zsh (Linux, macOS, WSL):
|
||||
|
||||
```bash
|
||||
ANDROID_PRODUCT_OUT=extracted fastboot flashall --skip-reboot
|
||||
export ANDROID_PRODUCT_OUT=extracted
|
||||
```
|
||||
|
||||
For PowerShell (Windows):
|
||||
|
||||
```powershell
|
||||
$env:ANDROID_PRODUCT_OUT = "extracted"
|
||||
```
|
||||
|
||||
For cmd (Windows):
|
||||
|
||||
```bat
|
||||
set ANDROID_PRODUCT_OUT=extracted
|
||||
```
|
||||
|
||||
6. Flash the partition images that were extracted.
|
||||
|
||||
```bash
|
||||
fastboot flashall --skip-reboot
|
||||
```
|
||||
|
||||
Note that this only flashes the OS partitions. The bootloader and modem/radio partitions are left untouched due to fastboot limitations. If they are not already up to date or if unsure, after fastboot completes, follow the steps in the [updates section](#updates) to sideload the patched OTA once. Sideloading OTAs always ensures that all partitions are up to date.
|
||||
|
||||
Alternatively, for Pixel devices, running `flash-base.sh` from the factory image will also update the bootloader and modem.
|
||||
|
||||
5. Set up the custom AVB public key in the bootloader after rebooting from fastbootd to bootloader.
|
||||
7. Set up the custom AVB public key in the bootloader after rebooting from fastbootd to bootloader.
|
||||
|
||||
```bash
|
||||
fastboot reboot-bootloader
|
||||
@@ -169,7 +199,7 @@ If you lose your AVB or OTA signing key, you will no longer be able to sign new
|
||||
fastboot flash avb_custom_key /path/to/avb_pkmd.bin
|
||||
```
|
||||
|
||||
6. **[Optional]** Before locking the bootloader, reboot into Android once to confirm that everything is properly signed.
|
||||
8. **[Optional]** Before locking the bootloader, reboot into Android once to confirm that everything is properly signed.
|
||||
|
||||
Install the Magisk or KernelSU app and run the following command:
|
||||
|
||||
@@ -183,7 +213,7 @@ If you lose your AVB or OTA signing key, you will no longer be able to sign new
|
||||
init: [libfs_avb]Returning avb_handle with status: Success
|
||||
```
|
||||
|
||||
7. Reboot back into fastboot and lock the bootloader. This will trigger a data wipe again.
|
||||
9. Reboot back into fastboot and lock the bootloader. This will trigger a data wipe again.
|
||||
|
||||
```bash
|
||||
fastboot flashing lock
|
||||
@@ -195,7 +225,7 @@ If you lose your AVB or OTA signing key, you will no longer be able to sign new
|
||||
|
||||
**WARNING**: If you are flashing CalyxOS, the setup wizard will [automatically turn off the `OEM unlocking` switch](https://github.com/CalyxOS/platform_packages_apps_SetupWizard/blob/7d2df25cedcbff83ddb608e628f9d97b38259c26/src/org/lineageos/setupwizard/SetupWizardApp.java#L135-L140). Make sure to manually reenable it again from Android's developer settings. Consider using the [`OEMUnlockOnBoot` module](https://github.com/chenxiaolong/OEMUnlockOnBoot) to automatically ensure OEM unlocking is enabled on every boot.
|
||||
|
||||
8. That's it! To install future OS, Magisk, or KernelSU updates, see the [next section](#updates).
|
||||
10. That's it! To install future OS, Magisk, or KernelSU updates, see the [next section](#updates).
|
||||
|
||||
## Updates
|
||||
|
||||
@@ -426,6 +456,34 @@ avbroot ota extract \
|
||||
--all
|
||||
```
|
||||
|
||||
### Zip write mode
|
||||
|
||||
By default, avbroot uses streaming writes for the output OTA during patching. This means it computes the sha256 digest for the digital signature as the file is being written. This mode causes the zip file to contain data descriptors, which is part of the zip standard and works on the vast majority of devices. However, some devices may have broken zip file parsers and fail to properly read OTA zip files containing data descriptors. If this is the case, pass in `--zip-mode seekable` when patching.
|
||||
|
||||
The seekable mode writes zip files without data descriptors, but as the name implies, requires seeking around the file instead of writing it sequentially. The sha256 digest for the digital signature is computed after the zip file has been fully written.
|
||||
|
||||
### Signing with an external program
|
||||
|
||||
avbroot supports delegating all RSA signing operations to an external program with the `--signing-helper` option. When using this option, the `--key-avb` and `--key-ota` options must be given a public key instead of a private key.
|
||||
|
||||
For each signing operation, avbroot will invoke the program with:
|
||||
|
||||
```bash
|
||||
<helper> <algorithm> <public key>
|
||||
```
|
||||
|
||||
The algorithm is one of `SHA{256,512}_RSA{2048,4096}` and the public key is what was passed to avbroot. The program can use the public key to find the corresponding private key (eg. on a hardware security module). avbroot will write a PKCS#1 v1.5 padded digest to `stdin` and the helper program is expected to perform a raw RSA signing operation and write the raw signature (octet string matching key size) to `stdout`.
|
||||
|
||||
By default, this behavior is compatible with the `--signing_helper` option in AOSP's avbtool. However, avbroot additionally extends the arguments to support non-interactive use. If `--pass-{avb,ota}-file` or `--pass-{avb,ota}-env-var` are used, then the helper program will be invoked with two additional arguments that point to the password file or environment variable.
|
||||
|
||||
```bash
|
||||
<helper> <algorithm> <public key> file <pass file>
|
||||
# or
|
||||
<helper> <algorithm> <public key> env <env file>
|
||||
```
|
||||
|
||||
Note that avbroot will verify the signature returned by helper program against the public key. This ensures that the patching process will fail appropriately if the wrong private key was used.
|
||||
|
||||
## Building from source
|
||||
|
||||
Make sure the [Rust toolchain](https://www.rust-lang.org/) is installed. Then run:
|
||||
@@ -440,25 +498,19 @@ Debug builds work too, but they will run significantly slower (in the sha256 com
|
||||
|
||||
By default, the executable links to the system's bzip2 and liblzma libraries, which are the only external libraries avbroot depends on. To compile and statically link these two libraries, pass in `--features static`.
|
||||
|
||||
### Android cross-compilation
|
||||
|
||||
To cross-compile for Android, install [cargo-android](https://github.com/chenxiaolong/cargo-android) and use the `cargo android` wrapper. To make a release build for aarch64, run:
|
||||
|
||||
```bash
|
||||
cargo android build --release --target aarch64-linux-android
|
||||
```
|
||||
|
||||
It is possible to run the tests if the host is running Linux, qemu-user-static is installed, and the executable is built with `RUSTFLAGS=-C target-feature=+crt-static` and `--features static`.
|
||||
|
||||
## Verifying digital signatures
|
||||
|
||||
First, save the public key to a file listing the keys to be trusted. This is the same key listed in [the author's profile](https://github.com/chenxiaolong/).
|
||||
|
||||
```bash
|
||||
echo 'avbroot ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDOe6/tBnO7xZhAWXRj3ApUYgn+XZ0wnQiXM8B7tPgv4' > avbroot_trusted_keys
|
||||
```
|
||||
|
||||
Then, verify the signature of the zip file using the list of trusted keys.
|
||||
|
||||
```bash
|
||||
ssh-keygen -Y verify -f avbroot_trusted_keys -I avbroot -n file -s <file>.zip.sig < <file>.zip
|
||||
```
|
||||
|
||||
If the file is successfully verified, the output will be:
|
||||
|
||||
```
|
||||
Good "file" signature for avbroot with ED25519 key SHA256:Ct0HoRyrFLrnF9W+A/BKEiJmwx7yWkgaW/JvghKrboA
|
||||
```
|
||||
To verify the digital signatures of the downloads, follow [the steps here](https://github.com/chenxiaolong/chenxiaolong/blob/master/VERIFY_SSH_SIGNATURES.md).
|
||||
|
||||
## Contributing
|
||||
|
||||
|
||||
+80
-28
@@ -33,7 +33,11 @@ avbroot модифицирует следующие образы:
|
||||
|
||||
Повторюсь: **_ВСЕГДА оставляйте `Заводскую разблокировку` включенной при наличии root-прав._**
|
||||
|
||||
* Любая операция, приводящая к прошивке некорректно подписанного загрузочного образа, приведет к тому, что устройство больше не сможет загрузиться в систему/режим Recovery, а для его восстановления потребуется повторная разблокировка загрузчика (и, следовательно, стирание всех пользовательских данных). Это же относится и к методу `Прямой установки` для обновления Magisk. Обновление Magisk **должно выполняться только путем обновления OTA,** а не через Magisk Manager.
|
||||
* Любая операция, приводящая к прошивке некорректно подписанного загрузочного образа, приведет к тому, что устройство больше не сможет загрузиться в систему/режим Recovery, а для его восстановления потребуется повторная разблокировка загрузчика (и, следовательно, стирание всех пользовательских данных). К подобным операциям в том числе относятся:
|
||||
|
||||
* Метод `Прямой установки` для обновления Magisk. Magisk можно обновлять **только путем репатчинга OTA,** но не через его приложение.
|
||||
|
||||
* Функция `Удаление Magisk` в приложении Magisk. Если вам больше не нужен root-доступ, Magisk **должен быть удален путем репатчинга OTA** с использованием параметра `--rootless`, но не через его приложение.
|
||||
|
||||
Если в загрузочный раздел были внесены какие-либо изменения, **не перезагружайтесь**. Обратитесь за помощью, [открыв Issue,](https://github.com/chenxiaolong/avbroot/issues/new) и четко разъясните, какие конкретные действия привели к возникновению такой ситуации. Если Android всё еще работает и доступ к root-правам сохранился – вероятно, получится откатить изменения до исходного состояния, не стирая ваши данные.
|
||||
|
||||
@@ -124,21 +128,27 @@ avbroot модифицирует следующие образы:
|
||||
avbroot key generate-cert -k ota.key -o ota.crt
|
||||
```
|
||||
|
||||
avbroot совместим с любым стандартным 4096-битным приватным ключом RSA в кодировке PKCS8 и сертификатом X509 в кодировке PEM, например с теми, которые генерируются openssl.
|
||||
avbroot совместим с любым стандартным 4096-битным приватным ключом RSA в кодировке PKCS#8 и сертификатом X509 в кодировке PEM, например с теми, которые генерируются openssl.
|
||||
|
||||
Если вы потеряете ключ(-и) подписи AVB или OTA, вы больше не сможете подписывать новые OTA-архивы. Придется генерировать новые ключи подписи и разблокировать загрузчик (что приведет к стиранию всех данных). В таком случае возвращайтесь к инструкции в разделе [использования.](#использование)
|
||||
|
||||
## Первоначальная настройка
|
||||
|
||||
1. Перезагрузитесь в режим fastboot и разблокируйте загрузчик, если не сделали этого ранее. Это приведет к стиранию всех пользовательских данных.
|
||||
1. Убедитесь, что вы используете утилиту fastboot версии 34 или новее. Предыдущие версии содержат баги, что не позволяют команде `fastboot flashall` (которая понадобится по ходу инструкции) работать правильно.
|
||||
|
||||
```bash
|
||||
fastboot --version
|
||||
```
|
||||
|
||||
2. Перезагрузитесь в режим fastboot и разблокируйте загрузчик, если не сделали этого ранее. Это приведет к стиранию всех пользовательских данных.
|
||||
|
||||
```bash
|
||||
fastboot flashing unlock
|
||||
```
|
||||
|
||||
2. Перед первой установкой, на устройстве уже должна быть установлена в оригинальном виде та прошивка, пропатченную версию которой вы собираетесь ставить. Если это не так, сначала установите оригинальную непропатченную OTA.
|
||||
3. Перед первой установкой, на устройстве уже должна быть установлена в оригинальном виде та прошивка, пропатченную версию которой вы собираетесь ставить. Если это не так, сначала установите оригинальную непропатченную OTA.
|
||||
|
||||
3. Извлекаем из пропатченного OTA модифицированные образы:
|
||||
4. Извлекаем из пропатченного OTA модифицированные образы:
|
||||
|
||||
```bash
|
||||
avbroot ota extract \
|
||||
@@ -149,17 +159,37 @@ avbroot совместим с любым стандартным 4096-битны
|
||||
|
||||
Если вы на всякий случай хотите прошить вообще все разделы из ОТА, извлечь их можно, указав аргумент `--all`.
|
||||
|
||||
4. Прошейте извлеченные образы разделов.
|
||||
5. Установите переменную окружения `ANDROID_PRODUCT_OUT`, указав директорию с извлеченными файлами.
|
||||
|
||||
Для sh/bash/zsh (Linux, macOS, WSL):
|
||||
|
||||
```bash
|
||||
ANDROID_PRODUCT_OUT=extracted fastboot flashall --skip-reboot
|
||||
export ANDROID_PRODUCT_OUT=extracted
|
||||
```
|
||||
|
||||
Для PowerShell (Windows):
|
||||
|
||||
```powershell
|
||||
$env:ANDROID_PRODUCT_OUT = "extracted"
|
||||
```
|
||||
|
||||
Для cmd (Командная строка или Терминал) (Windows):
|
||||
|
||||
```bat
|
||||
set ANDROID_PRODUCT_OUT=extracted
|
||||
```
|
||||
|
||||
6. Прошейте извлеченные образы разделов.
|
||||
|
||||
```bash
|
||||
fastboot flashall --skip-reboot
|
||||
```
|
||||
|
||||
Обратите внимание, что так прошиваются лишь те образы, что относятся к системе. Разделы загрузчика и модема же остаются нетронутыми из-за ограничений fastboot. Если они не обновлены до необходимой версии, или вы не уверены в этом, после прошивки перейдите к пункту [обновлений](#обновления) и установите пропатченный OTA-архив сайдлоадом в режиме Recovery. Прошивка полного OTA гарантирует, что абсолютно все разделы будут обновлены.
|
||||
|
||||
Для устройств Pixel есть ещё один вариант: запуск скрипта `flash-base.sh` из папки заводских образов (factory images) обновит загрузчик и модем.
|
||||
|
||||
5. После перезагрузки из fastbootd в загрузчик (bootloader), установите пользовательский публичный ключ AVB в загрузчик:
|
||||
7. После перезагрузки из fastbootd в загрузчик (bootloader), установите пользовательский публичный ключ AVB в загрузчик:
|
||||
|
||||
```bash
|
||||
fastboot reboot-bootloader
|
||||
@@ -167,7 +197,7 @@ avbroot совместим с любым стандартным 4096-битны
|
||||
fastboot flash avb_custom_key /путь/к/avb_pkmd.bin
|
||||
```
|
||||
|
||||
6. **[Опционально]** Перед блокировкой загрузчика загрузитесь в систему, дабы убедиться, что все подписано правильно.
|
||||
8. **[Опционально]** Перед блокировкой загрузчика загрузитесь в систему, дабы убедиться, что все подписано правильно.
|
||||
|
||||
Установите приложение Magisk или KernelSU и выполните следующую команду:
|
||||
|
||||
@@ -181,7 +211,7 @@ avbroot совместим с любым стандартным 4096-битны
|
||||
init: [libfs_avb]Returning avb_handle with status: Success
|
||||
```
|
||||
|
||||
7. Перезагрузитесь в fastboot и заблокируйте загрузчик. Это снова приведет к стиранию данных.
|
||||
9. Перезагрузитесь в fastboot и заблокируйте загрузчик. Это снова приведет к стиранию данных.
|
||||
|
||||
```bash
|
||||
fastboot flashing lock
|
||||
@@ -193,7 +223,7 @@ avbroot совместим с любым стандартным 4096-битны
|
||||
|
||||
**ПРЕДУПРЕЖДЕНИЕ**: Если вы прошили CalyxOS, мастер настройки [автоматически отключит опцию `Заводской разблокировки`.](https://github.com/CalyxOS/platform_packages_apps_SetupWizard/blob/7d2df25cedcbff83ddb608e628f9d97b38259c26/src/org/lineageos/setupwizard/SetupWizardApp.java#L135-L140) Не забудьте снова включить её вручную в настройках для разработчиков. Для перестраховки можете использовать [модуль `OEMUnlockOnBoot`,](https://github.com/chenxiaolong/OEMUnlockOnBoot) который автоматически включает пункт Заводской разблокировки при каждом запуске системы.
|
||||
|
||||
8. Готово. Установка последующих обновлений системы, Magisk или KernelSU, описывается в [следующем разделе.](#обновления)
|
||||
10. Готово! Установка последующих обновлений системы, Magisk или KernelSU, описывается в [следующем разделе.](#обновления)
|
||||
|
||||
## Обновления
|
||||
|
||||
@@ -416,6 +446,34 @@ avbroot ota extract \
|
||||
--all
|
||||
```
|
||||
|
||||
### Режим записи ZIP
|
||||
|
||||
По умолчанию, avbroot использует потоковую запись для вывода OTA во время патчинга. Это означает, что он вычисляет дайджест sha256 для цифровой подписи одновременно с записью файла. Такой режим приводит к тому, что в ZIP-файле появляются описатели данных, что является частью стандарта ZIP и работает на подавляющем большинстве устройств. Однако некоторые устройства могут иметь некорректно работающие парсеры ZIP-файлов и не смогут правильно прочитать ZIP-файлы OTA, содержащие описатели данных. Если это так, используйте опцию `--zip-mode seekable` при патчинге.
|
||||
|
||||
Режим seekable записывает ZIP-файлы без описателей данных, но, как следует из названия, требует перемещения по файлу, вместо последовательной записи. Дайджест sha256 для цифровой подписи вычисляется после того, как ZIP-файл был полностью записан.
|
||||
|
||||
### Подписание с использованием внешней программы
|
||||
|
||||
avbroot поддерживает делегирование всех операций подписания RSA внешней программе с помощью опции `--signing-helper`. При использовании этой опции, для `--key-avb` и `--key-ota` должен быть указан публичный ключ вместо приватного.
|
||||
|
||||
Для каждой операции подписания, avbroot будет вызывать программу с параметрами:
|
||||
|
||||
```bash
|
||||
<helper> <algorithm> <public key>
|
||||
```
|
||||
|
||||
Алгоритм (`<algorithm>`) — это один из `SHA{256,512}_RSA{2048,4096}`, а публичный ключ (`<public key>`) — это тот, что был передан в avbroot. Внешняя программа может использовать публичный ключ для поиска соответствующего приватного ключа (например, на аппаратном модуле безопасности). avbroot запишет дайджест, отформатированный по PKCS#1 v1.5, в `stdin`, а внешняя программа должна выполнить операцию сырого подписания RSA и записать сырую подпись (октетная строка, соответствующая размеру ключа) в `stdout`.
|
||||
|
||||
По умолчанию, это поведение совместимо с опцией `--signing_helper` в avbtool от AOSP. Однако avbroot дополнительно расширяет аргументы для поддержки неинтерактивного использования. Если используются опции `--pass-{avb,ota}-file` или `--pass-{avb,ota}-env-var`, то внешняя программа будет вызвана с двумя дополнительными аргументами, указывающими на файл пароля или переменную окружения.
|
||||
|
||||
```bash
|
||||
<helper> <algorithm> <public key> file <pass file>
|
||||
# или
|
||||
<helper> <algorithm> <public key> env <env file>
|
||||
```
|
||||
|
||||
Обратите внимание, что avbroot проверит подпись, возвращенную внешней программой, на соответствие с публичным ключом. Это гарантирует, что процесс патчинга завершится ошибкой, если был использован неправильный приватный ключ.
|
||||
|
||||
## Сборка из исходного кода
|
||||
|
||||
Убедитесь, что у вас установлен [набор инструментов Rust.](https://www.rust-lang.org/ru/) Затем выполните:
|
||||
@@ -430,25 +488,19 @@ cargo build --release
|
||||
|
||||
По умолчанию исполняемый файл ссылается на системные библиотеки bzip2 и liblzma, от которых зависит avbroot. Чтобы скомпилировать и статически связать эти две библиотеки, укажите аргумент `--features static`.
|
||||
|
||||
### Кросс-компиляция на Android
|
||||
|
||||
Чтобы использовать кросс-компиляцию на Android, установите [cargo-android](https://github.com/chenxiaolong/cargo-android) и воспользуйтесь оболочкой `cargo android`. Чтобы создать релизную сборку для aarch64, выполните:
|
||||
|
||||
```bash
|
||||
cargo android build --release --target aarch64-linux-android
|
||||
```
|
||||
|
||||
Возможно выполнение тестов, если хост работает под управлением Linux, установлен qemu-user-static, а исполняемый файл собран с `RUSTFLAGS=-C target-feature=+crt-static` и `--features static`.
|
||||
|
||||
## Проверка цифровых подписей
|
||||
|
||||
Сначала сохраните открытый ключ в файл, перечисляющий ключи, которым можно доверять. Это тот же ключ, который указан [в профиле автора.](https://github.com/chenxiaolong/)
|
||||
|
||||
```bash
|
||||
echo 'avbroot ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDOe6/tBnO7xZhAWXRj3ApUYgn+XZ0wnQiXM8B7tPgv4' > avbroot_trusted_keys
|
||||
```
|
||||
|
||||
Затем проверьте подпись zip-файла, используя список доверенных ключей.
|
||||
|
||||
```bash
|
||||
ssh-keygen -Y verify -f avbroot_trusted_keys -I avbroot -n file -s <file>.zip.sig < <file>.zip
|
||||
```
|
||||
|
||||
Если файл успешно проверен, вывод будет следующим:
|
||||
|
||||
```
|
||||
Good "file" signature for avbroot with ED25519 key SHA256:Ct0HoRyrFLrnF9W+A/BKEiJmwx7yWkgaW/JvghKrboA
|
||||
```
|
||||
Чтобы проверить цифровые подписи, [следуйте этой инструкции.](https://github.com/chenxiaolong/chenxiaolong/blob/master/VERIFY_SSH_SIGNATURES.md)
|
||||
|
||||
## Вклад
|
||||
|
||||
|
||||
+15
-5
@@ -11,7 +11,7 @@ publish = false
|
||||
[dependencies]
|
||||
anyhow = "1.0.75"
|
||||
base64 = "0.22.1"
|
||||
bitflags = "2.4.1"
|
||||
bitflags = { version = "2.4.1", features = ["serde"] }
|
||||
bstr = "1.6.2"
|
||||
byteorder = "1.4.3"
|
||||
cap-std = "3.0.0"
|
||||
@@ -20,18 +20,22 @@ clap = { version = "4.4.1", features = ["derive"] }
|
||||
clap_complete = "4.4.0"
|
||||
cms = { version = "0.2.2", features = ["std"] }
|
||||
const-oid = "0.9.5"
|
||||
crc32fast = "1.4.2"
|
||||
ctrlc = "3.4.0"
|
||||
dlv-list = "0.5.2"
|
||||
flate2 = "1.0.27"
|
||||
gf256 = { version = "0.3.0", features = ["rs"] }
|
||||
hex = { version = "0.4.3", features = ["serde"] }
|
||||
liblzma = "0.3.0"
|
||||
lz4_flex = "0.11.1"
|
||||
memchr = "2.6.0"
|
||||
miniz_oxide = "0.8.0"
|
||||
num-bigint-dig = "0.8.4"
|
||||
num-traits = "0.2.16"
|
||||
passterm = "2.0.3"
|
||||
phf = { version = "0.11.2", features = ["macros"] }
|
||||
pkcs8 = { version = "0.10.2", features = ["encryption", "pem"] }
|
||||
prost = "0.12.1"
|
||||
prost = "0.13.1"
|
||||
rand = "0.8.5"
|
||||
rayon = "1.7.0"
|
||||
regex = { version = "1.9.4", default-features = false, features = ["perf", "std"] }
|
||||
@@ -40,7 +44,6 @@ regex = { version = "1.9.4", default-features = false, features = ["perf", "std"
|
||||
# instructions. sha2 is still used for signing purposes.
|
||||
# https://github.com/RustCrypto/hashes/issues/327
|
||||
ring = "0.17.0"
|
||||
rpassword = "7.2.0"
|
||||
rsa = { version = "0.9.2", features = ["sha1", "sha2"] }
|
||||
serde = { version = "1.0.188", features = ["derive"] }
|
||||
sha1 = "0.10.5"
|
||||
@@ -52,6 +55,8 @@ topological-sort = "0.2.2"
|
||||
tracing = "0.1.40"
|
||||
tracing-subscriber = "0.3.18"
|
||||
x509-cert = { version = "0.2.4", features = ["builder"] }
|
||||
zerocopy = "0.7.35"
|
||||
zerocopy-derive = "0.7.35"
|
||||
|
||||
# There are multiple upstream bugs that cause infinite loops in the Drop
|
||||
# implementation of write::BzDecoder. Unfortunately, the project is no longer
|
||||
@@ -68,14 +73,19 @@ default-features = false
|
||||
features = ["deflate"]
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
libc = "0.2.158"
|
||||
rustix = { version = "0.38.9", default-features = false, features = ["process"] }
|
||||
|
||||
[build-dependencies]
|
||||
prost-build = "0.12.1"
|
||||
protox = "0.6.0"
|
||||
constcat = "0.5.0"
|
||||
prost-build = "0.13.1"
|
||||
protox = "0.7.0"
|
||||
|
||||
[dev-dependencies]
|
||||
assert_matches = "1.5.0"
|
||||
|
||||
[features]
|
||||
static = ["bzip2/static", "liblzma/static"]
|
||||
|
||||
[lints.rust]
|
||||
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(fuzzing)'] }
|
||||
|
||||
+55
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Andrew Gunnerson
|
||||
* SPDX-FileCopyrightText: 2023-2024 Andrew Gunnerson
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
@@ -30,8 +30,62 @@ fn main() {
|
||||
|
||||
let file_descriptors = protox::compile(&protos, [&in_dir]).unwrap();
|
||||
|
||||
const CUE_AI: &str = ".chromeos_update_engine.ApexInfo";
|
||||
const CUE_DAM: &str = ".chromeos_update_engine.DeltaArchiveManifest";
|
||||
const CUE_DPG: &str = ".chromeos_update_engine.DynamicPartitionGroup";
|
||||
const CUE_DPM: &str = ".chromeos_update_engine.DynamicPartitionMetadata";
|
||||
const CUE_PU: &str = ".chromeos_update_engine.PartitionUpdate";
|
||||
const CUE_VABCFS: &str = ".chromeos_update_engine.VABCFeatureSet";
|
||||
|
||||
const DERIVE_SERDE: &str = "#[derive(serde::Deserialize, serde::Serialize)]";
|
||||
const SERDE_DEFAULT: &str = "#[serde(default)]";
|
||||
const SERDE_SKIP: &str = "#[serde(skip)]";
|
||||
const SERDE_SKIP_IF_VEC_EMPTY: &str = "#[serde(skip_serializing_if = \"Vec::is_empty\")]";
|
||||
|
||||
use constcat::concat as c;
|
||||
|
||||
prost_build::Config::new()
|
||||
.btree_map(["."])
|
||||
// Allow deserializing and serializing the types we care about.
|
||||
.type_attribute(CUE_AI, DERIVE_SERDE)
|
||||
.type_attribute(CUE_DAM, DERIVE_SERDE)
|
||||
.type_attribute(CUE_DPG, DERIVE_SERDE)
|
||||
.type_attribute(CUE_DPM, DERIVE_SERDE)
|
||||
.type_attribute(CUE_PU, DERIVE_SERDE)
|
||||
.type_attribute(CUE_VABCFS, DERIVE_SERDE)
|
||||
// Allow default-initializing all fields.
|
||||
.type_attribute(CUE_AI, SERDE_DEFAULT)
|
||||
.type_attribute(CUE_DAM, SERDE_DEFAULT)
|
||||
.type_attribute(CUE_DPG, SERDE_DEFAULT)
|
||||
.type_attribute(CUE_DPM, SERDE_DEFAULT)
|
||||
.type_attribute(CUE_PU, SERDE_DEFAULT)
|
||||
.type_attribute(CUE_VABCFS, SERDE_DEFAULT)
|
||||
// Don't serialize fields that define the structure of the payload
|
||||
// binary and that we recompute during packing.
|
||||
.field_attribute(c!(CUE_DAM, ".signatures_offset"), SERDE_SKIP)
|
||||
.field_attribute(c!(CUE_DAM, ".signatures_size"), SERDE_SKIP)
|
||||
.field_attribute(c!(CUE_PU, ".operations"), SERDE_SKIP)
|
||||
.field_attribute(c!(CUE_PU, ".estimate_cow_size"), SERDE_SKIP)
|
||||
.field_attribute(c!(CUE_PU, ".old_partition_info"), SERDE_SKIP)
|
||||
.field_attribute(c!(CUE_PU, ".new_partition_info"), SERDE_SKIP)
|
||||
// Don't serialize AVB 1.0 fields.
|
||||
.field_attribute(c!(CUE_PU, ".hash_tree_data_extent"), SERDE_SKIP)
|
||||
.field_attribute(c!(CUE_PU, ".hash_tree_extent"), SERDE_SKIP)
|
||||
.field_attribute(c!(CUE_PU, ".hash_tree_algorithm"), SERDE_SKIP)
|
||||
.field_attribute(c!(CUE_PU, ".hash_tree_salt"), SERDE_SKIP)
|
||||
.field_attribute(c!(CUE_PU, ".fec_data_extent"), SERDE_SKIP)
|
||||
.field_attribute(c!(CUE_PU, ".fec_extent"), SERDE_SKIP)
|
||||
.field_attribute(c!(CUE_PU, ".fec_roots"), SERDE_SKIP)
|
||||
// Don't serialize fields for incremental OTAs.
|
||||
.field_attribute(c!(CUE_PU, ".merge_operations"), SERDE_SKIP)
|
||||
// Don't serialize fields for vendor-signed images, which update_engine
|
||||
// doesn't support anyway.
|
||||
.field_attribute(c!(CUE_PU, ".new_partition_signature"), SERDE_SKIP)
|
||||
// Don't serialize empty lists.
|
||||
.field_attribute(c!(CUE_DAM, ".apex_info"), SERDE_SKIP_IF_VEC_EMPTY)
|
||||
.field_attribute(c!(CUE_DAM, ".partitions"), SERDE_SKIP_IF_VEC_EMPTY)
|
||||
.field_attribute(c!(CUE_DPG, ".partition_names"), SERDE_SKIP_IF_VEC_EMPTY)
|
||||
.field_attribute(c!(CUE_DPM, ".groups"), SERDE_SKIP_IF_VEC_EMPTY)
|
||||
.compile_fds(file_descriptors)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
+12
-39
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Andrew Gunnerson
|
||||
* SPDX-FileCopyrightText: 2023-2024 Andrew Gunnerson
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
@@ -15,7 +15,7 @@ use clap::{Parser, Subcommand, ValueEnum};
|
||||
use tracing::{debug, Level};
|
||||
use tracing_subscriber::fmt::{format::Writer, time::FormatTime};
|
||||
|
||||
use crate::cli::{avb, boot, completion, cpio, fec, hashtree, key, ota};
|
||||
use crate::cli::{avb, boot, completion, cpio, fec, hashtree, key, lp, ota, payload, sparse};
|
||||
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
#[derive(Debug, Subcommand)]
|
||||
@@ -27,7 +27,10 @@ pub enum Command {
|
||||
Fec(fec::FecCli),
|
||||
HashTree(hashtree::HashTreeCli),
|
||||
Key(key::KeyCli),
|
||||
Lp(lp::LpCli),
|
||||
Ota(ota::OtaCli),
|
||||
Payload(payload::PayloadCli),
|
||||
Sparse(sparse::SparseCli),
|
||||
/// (Deprecated: Use `avbroot ota patch` instead.)
|
||||
Patch(ota::PatchCli),
|
||||
/// (Deprecated: Use `avbroot ota extract` instead.)
|
||||
@@ -36,39 +39,6 @@ pub enum Command {
|
||||
MagiskInfo(boot::MagiskInfoCli),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, ValueEnum)]
|
||||
pub enum LogLevel {
|
||||
Trace,
|
||||
Debug,
|
||||
Info,
|
||||
Warn,
|
||||
Error,
|
||||
}
|
||||
|
||||
impl LogLevel {
|
||||
fn as_level(self) -> Level {
|
||||
match self {
|
||||
Self::Trace => Level::TRACE,
|
||||
Self::Debug => Level::DEBUG,
|
||||
Self::Info => Level::INFO,
|
||||
Self::Warn => Level::WARN,
|
||||
Self::Error => Level::ERROR,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for LogLevel {
|
||||
fn default() -> Self {
|
||||
Self::Info
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for LogLevel {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(self.to_possible_value().ok_or(fmt::Error)?.get_name())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, ValueEnum)]
|
||||
pub enum LogFormat {
|
||||
Short,
|
||||
@@ -95,8 +65,8 @@ pub struct Cli {
|
||||
pub command: Command,
|
||||
|
||||
/// Lowest log message severity to output.
|
||||
#[arg(long, global = true, value_name = "LEVEL", default_value_t)]
|
||||
pub log_level: LogLevel,
|
||||
#[arg(long, global = true, value_name = "LEVEL", default_value_t = Level::INFO)]
|
||||
pub log_level: Level,
|
||||
|
||||
/// Output format for log messages.
|
||||
#[arg(long, global = true, value_name = "FORMAT", default_value_t)]
|
||||
@@ -123,11 +93,11 @@ impl FormatTime for ShortUptime {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init_logging(log_level: LogLevel, log_format: LogFormat) {
|
||||
pub fn init_logging(log_level: Level, log_format: LogFormat) {
|
||||
let builder = tracing_subscriber::fmt()
|
||||
.with_writer(io::stderr)
|
||||
.with_ansi(io::stderr().is_terminal())
|
||||
.with_max_level(log_level.as_level());
|
||||
.with_max_level(log_level);
|
||||
|
||||
match log_format {
|
||||
LogFormat::Short => {
|
||||
@@ -162,7 +132,10 @@ pub fn main(logging_initialized: &AtomicBool, cancel_signal: &AtomicBool) -> Res
|
||||
Command::Fec(c) => fec::fec_main(&c, cancel_signal),
|
||||
Command::HashTree(c) => hashtree::hash_tree_main(&c, cancel_signal),
|
||||
Command::Key(c) => key::key_main(&c),
|
||||
Command::Lp(c) => lp::lp_main(&c, cancel_signal),
|
||||
Command::Ota(c) => ota::ota_main(&c, cancel_signal),
|
||||
Command::Payload(c) => payload::payload_main(&c, cancel_signal),
|
||||
Command::Sparse(c) => sparse::sparse_main(&c, cancel_signal),
|
||||
// Deprecated aliases.
|
||||
Command::Patch(c) => ota::patch_subcommand(&c, cancel_signal),
|
||||
Command::Extract(c) => ota::extract_subcommand(&c, cancel_signal),
|
||||
|
||||
+34
-8
@@ -25,7 +25,7 @@ use serde::{Deserialize, Serialize};
|
||||
use tracing::{debug_span, info, warn, Span};
|
||||
|
||||
use crate::{
|
||||
crypto::{self, PassphraseSource},
|
||||
crypto::{self, PassphraseSource, RsaSigningKey},
|
||||
format::avb::{
|
||||
self, AlgorithmType, AppendedDescriptorMut, AppendedDescriptorRef, Descriptor, Footer,
|
||||
HashTreeDescriptor, Header, KernelCmdlineDescriptor,
|
||||
@@ -383,12 +383,26 @@ fn sign_or_clear(info: &mut AvbInfo, orig_header: &Header, key_group: &KeyGroup)
|
||||
key_group.pass_file.as_deref(),
|
||||
key_group.pass_env_var.as_deref(),
|
||||
);
|
||||
let private_key = crypto::read_pem_key_file(key_path, &source)
|
||||
.with_context(|| format!("Failed to load key: {key_path:?}"))?;
|
||||
let signing_key = if let Some(helper) = &key_group.signing_helper {
|
||||
let public_key = crypto::read_pem_public_key_file(key_path)
|
||||
.with_context(|| format!("Failed to load key: {key_path:?}"))?;
|
||||
|
||||
info.header.set_algo_for_key(&private_key)?;
|
||||
RsaSigningKey::External {
|
||||
program: helper.clone(),
|
||||
public_key_file: key_path.clone(),
|
||||
public_key,
|
||||
passphrase_source: source,
|
||||
}
|
||||
} else {
|
||||
let private_key = crypto::read_pem_key_file(key_path, &source)
|
||||
.with_context(|| format!("Failed to load key: {key_path:?}"))?;
|
||||
|
||||
RsaSigningKey::Internal(private_key)
|
||||
};
|
||||
|
||||
info.header.set_algo_for_key(&signing_key)?;
|
||||
info.header
|
||||
.sign(&private_key)
|
||||
.sign(&signing_key)
|
||||
.context("Failed to sign new AVB header")?;
|
||||
}
|
||||
SignAction::Clear => {
|
||||
@@ -743,12 +757,15 @@ struct DisplayGroup {
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
struct KeyGroup {
|
||||
/// Path to private key for signing.
|
||||
/// Path to signing key.
|
||||
///
|
||||
/// A private key is needed if packing an image where the original header
|
||||
/// A signing key is needed if packing an image where the original header
|
||||
/// was signed and the header needs to be modified (eg. for a new checksum).
|
||||
/// If the header was originally not signed, then the private key is not
|
||||
/// If the header was originally not signed, then the signing key is not
|
||||
/// used, unless --force is specified.
|
||||
///
|
||||
/// This should normally be a private key. However, if --signing-helper is
|
||||
/// used, then it should be a public key instead.
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
key: Option<PathBuf>,
|
||||
|
||||
@@ -768,6 +785,15 @@ struct KeyGroup {
|
||||
/// File containing private key passphrase.
|
||||
#[arg(long, value_name = "FILE", value_parser, group = "pass")]
|
||||
pass_file: Option<PathBuf>,
|
||||
|
||||
/// External program for signing.
|
||||
///
|
||||
/// If this option is specified, then --key must refer to a public key. The
|
||||
/// program will be invoked as:
|
||||
///
|
||||
/// <program> <algo> <public key> [file <pass file>|env <pass env>]
|
||||
#[arg(long, value_name = "PROGRAM", value_parser)]
|
||||
signing_helper: Option<PathBuf>,
|
||||
}
|
||||
|
||||
/// Unpack an AVB image.
|
||||
|
||||
@@ -303,7 +303,7 @@ struct UnpackCli {
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
input: PathBuf,
|
||||
|
||||
/// Path to output cpio info TOML.
|
||||
/// Path to output info TOML.
|
||||
#[arg(long, value_name = "FILE", value_parser, default_value = "cpio.toml")]
|
||||
output_info: PathBuf,
|
||||
|
||||
|
||||
@@ -55,6 +55,9 @@ pub fn key_main(cli: &KeyCli) -> Result<()> {
|
||||
.with_context(|| format!("Failed to load key: {p:?}"))?;
|
||||
|
||||
private_key.to_public_key()
|
||||
} else if let Some(p) = &c.input.public_key {
|
||||
crypto::read_pem_public_key_file(p)
|
||||
.with_context(|| format!("Failed to load public key: {p:?}"))?
|
||||
} else if let Some(p) = &c.input.cert {
|
||||
let certificate = crypto::read_pem_cert_file(p)
|
||||
.with_context(|| format!("Failed to load certificate: {p:?}"))?;
|
||||
@@ -93,6 +96,10 @@ struct PublicKeyInputGroup {
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
key: Option<PathBuf>,
|
||||
|
||||
/// Path to public key.
|
||||
#[arg(short, long, value_name = "FILE", value_parser, conflicts_with_all = ["pass_env_var", "pass_file"])]
|
||||
public_key: Option<PathBuf>,
|
||||
|
||||
/// Path to certificate.
|
||||
#[arg(short, long, value_name = "FILE", value_parser, conflicts_with_all = ["pass_env_var", "pass_file"])]
|
||||
cert: Option<PathBuf>,
|
||||
|
||||
@@ -0,0 +1,681 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024 Andrew Gunnerson
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
use std::{
|
||||
ffi::OsStr,
|
||||
fs::{self, File},
|
||||
io::{Seek, SeekFrom},
|
||||
path::{Path, PathBuf},
|
||||
sync::atomic::AtomicBool,
|
||||
};
|
||||
|
||||
use anyhow::{bail, Context, Result};
|
||||
use cap_std::{ambient_authority, fs::Dir};
|
||||
use clap::{CommandFactory, Parser, Subcommand};
|
||||
use rayon::iter::{
|
||||
IndexedParallelIterator, IntoParallelIterator, IntoParallelRefIterator, ParallelIterator,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
format::lp::{Extent, ExtentType, ImageType, Metadata, SECTOR_SIZE},
|
||||
stream::{self, FromReader, PSeekFile, Reopen, ToWriter},
|
||||
};
|
||||
|
||||
fn open_lp_inputs(paths: &[impl AsRef<Path>]) -> Result<(Vec<PSeekFile>, Metadata)> {
|
||||
let mut inputs = paths
|
||||
.iter()
|
||||
.map(|p| {
|
||||
let p = p.as_ref();
|
||||
|
||||
File::open(p)
|
||||
.map(PSeekFile::new)
|
||||
.with_context(|| format!("Failed to open LP image for reading: {p:?}"))
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
|
||||
let metadata = Metadata::from_reader(&mut inputs[0])
|
||||
.with_context(|| format!("Failed to parse LP image metadata: {:?}", paths[0].as_ref()))?;
|
||||
|
||||
Ok((inputs, metadata))
|
||||
}
|
||||
|
||||
fn open_lp_outputs(paths: &[impl AsRef<Path>]) -> Result<Vec<PSeekFile>> {
|
||||
paths
|
||||
.iter()
|
||||
.map(|p| {
|
||||
let p = p.as_ref();
|
||||
|
||||
File::create(p)
|
||||
.map(PSeekFile::new)
|
||||
.with_context(|| format!("Failed to open LP image for writing: {p:?}"))
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()
|
||||
}
|
||||
|
||||
fn read_info(path: &Path) -> Result<Metadata> {
|
||||
let data = fs::read_to_string(path)
|
||||
.with_context(|| format!("Failed to read metadata info TOML: {path:?}"))?;
|
||||
let info = toml_edit::de::from_str(&data)
|
||||
.with_context(|| format!("Failed to parse metadata info TOML: {path:?}"))?;
|
||||
|
||||
Ok(info)
|
||||
}
|
||||
|
||||
fn write_info(path: &Path, metadata: &Metadata) -> Result<()> {
|
||||
let data = toml_edit::ser::to_string_pretty(metadata)
|
||||
.with_context(|| format!("Failed to serialize metadata info TOML: {path:?}"))?;
|
||||
fs::write(path, data)
|
||||
.with_context(|| format!("Failed to write metadata info TOML: {path:?}"))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn display_metadata(cli: &LpCli, metadata: &Metadata) {
|
||||
if !cli.quiet {
|
||||
println!("{metadata:#?}");
|
||||
}
|
||||
}
|
||||
|
||||
struct CopyExtent {
|
||||
device_index: usize,
|
||||
lp_offset: u64,
|
||||
out_offset: u64,
|
||||
size: u64,
|
||||
}
|
||||
|
||||
/// Split extents into smaller ones for parallelization.
|
||||
fn split_extents(extents: &[Extent]) -> Vec<CopyExtent> {
|
||||
// 64 MiB is the smallest size we'll parallelize by.
|
||||
const CHUNK_SIZE: u64 = 64 * 1024 * 1024;
|
||||
|
||||
let mut result = vec![];
|
||||
let mut out_offset = 0;
|
||||
|
||||
for extent in extents {
|
||||
let mut remain = extent.num_sectors * u64::from(SECTOR_SIZE);
|
||||
|
||||
match extent.extent_type {
|
||||
ExtentType::Linear {
|
||||
start_sector,
|
||||
block_device_index,
|
||||
} => {
|
||||
let mut lp_offset = start_sector * u64::from(SECTOR_SIZE);
|
||||
|
||||
// 64 MiB is the smallest size we'll parallelize by.
|
||||
let num_chunks = remain.div_ceil(64 * 1024 * 1024);
|
||||
|
||||
for _ in 0..num_chunks {
|
||||
let chunk_size = CHUNK_SIZE.min(remain);
|
||||
|
||||
result.push(CopyExtent {
|
||||
device_index: block_device_index,
|
||||
out_offset,
|
||||
lp_offset,
|
||||
size: chunk_size,
|
||||
});
|
||||
|
||||
out_offset += chunk_size;
|
||||
lp_offset += chunk_size;
|
||||
remain -= chunk_size;
|
||||
}
|
||||
}
|
||||
ExtentType::Zero => out_offset += remain,
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// Use the CLI-specified slot or automatically select one if all slots are
|
||||
/// identical.
|
||||
fn get_slot_number(metadata: &Metadata, cli_slot: Option<u32>) -> Result<usize> {
|
||||
match cli_slot {
|
||||
Some(n) => {
|
||||
let n = n as usize;
|
||||
if n >= metadata.slots.len() {
|
||||
bail!("Slot out of range: {n}");
|
||||
}
|
||||
|
||||
Ok(n)
|
||||
}
|
||||
None => {
|
||||
if metadata.slots.windows(2).any(|w| w[0] != w[1]) {
|
||||
bail!("A slot must be specified because they are not all identical");
|
||||
}
|
||||
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove all slots aside from the specified one and return the old slot count.
|
||||
fn retain_slot(metadata: &mut Metadata, slot: usize) -> usize {
|
||||
let slot_count = metadata.slots.len();
|
||||
metadata.slots.swap(0, slot);
|
||||
metadata.slots.truncate(1);
|
||||
slot_count
|
||||
}
|
||||
|
||||
/// Duplicate the first slot until the required number of slots is reached.
|
||||
fn fill_slots(metadata: &mut Metadata) {
|
||||
let required = match metadata.image_type {
|
||||
ImageType::Normal => metadata.metadata_slot_count as usize,
|
||||
ImageType::Empty => 1,
|
||||
};
|
||||
|
||||
for _ in metadata.slots.len()..required {
|
||||
metadata.slots.extend_from_within(0..=0);
|
||||
}
|
||||
}
|
||||
|
||||
fn unpack_subcommand(lp_cli: &LpCli, cli: &UnpackCli, cancel_signal: &AtomicBool) -> Result<()> {
|
||||
let mut inputs = cli
|
||||
.input
|
||||
.iter()
|
||||
.map(|p| {
|
||||
File::open(p)
|
||||
.map(PSeekFile::new)
|
||||
.with_context(|| format!("Failed to open LP image for reading: {p:?}"))
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
|
||||
let mut metadata = Metadata::from_reader(&mut inputs[0])
|
||||
.with_context(|| format!("Failed to read LP image metadata: {:?}", cli.input[0]))?;
|
||||
|
||||
// Display and write only the selected slot.
|
||||
let slot_number = get_slot_number(&metadata, cli.slot)?;
|
||||
retain_slot(&mut metadata, slot_number);
|
||||
display_metadata(lp_cli, &metadata);
|
||||
write_info(&cli.output_info, &metadata)?;
|
||||
|
||||
// For empty images, there's no data to unpack.
|
||||
if metadata.image_type == ImageType::Empty {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let authority = ambient_authority();
|
||||
Dir::create_ambient_dir_all(&cli.output_images, authority)
|
||||
.with_context(|| format!("Failed to create directory: {:?}", cli.output_images))?;
|
||||
let directory = Dir::open_ambient_dir(&cli.output_images, authority)
|
||||
.with_context(|| format!("Failed to open directory: {:?}", cli.output_images))?;
|
||||
|
||||
let slot = &metadata.slots[0];
|
||||
|
||||
if slot.block_devices.len() != inputs.len() {
|
||||
bail!(
|
||||
"Need {} input images, but have {}",
|
||||
slot.block_devices.len(),
|
||||
inputs.len(),
|
||||
);
|
||||
}
|
||||
|
||||
// Preopen all image output files.
|
||||
let mut paths = vec![];
|
||||
let mut files = vec![];
|
||||
|
||||
for group in &slot.groups {
|
||||
let mut group_paths = vec![];
|
||||
let mut group_files = vec![];
|
||||
|
||||
for partition in &group.partitions {
|
||||
// A partition name with unsafe characters fails during parsing.
|
||||
let path = format!("{}.img", partition.name);
|
||||
|
||||
let file = directory
|
||||
.create(&path)
|
||||
.map(|f| PSeekFile::new(f.into_std()))
|
||||
.with_context(|| format!("Failed to open for writing: {path:?}"))?;
|
||||
|
||||
file.set_len(partition.size()?)
|
||||
.with_context(|| format!("Failed to truncate file: {path:?}"))?;
|
||||
|
||||
group_paths.push(path);
|
||||
group_files.push(file);
|
||||
}
|
||||
|
||||
paths.push(group_paths);
|
||||
files.push(group_files);
|
||||
}
|
||||
|
||||
slot.groups
|
||||
.par_iter()
|
||||
.enumerate()
|
||||
// Flatten grouped partitions.
|
||||
.flat_map(|(g_index, g)| {
|
||||
g.partitions
|
||||
.par_iter()
|
||||
.enumerate()
|
||||
.map(move |(p_index, p)| (g_index, p_index, p))
|
||||
})
|
||||
// Flatten extents in all partitions and split them to smaller chunks
|
||||
// for better parallelism.
|
||||
.flat_map(|(g_index, p_index, p)| {
|
||||
split_extents(&p.extents)
|
||||
.into_par_iter()
|
||||
.map(move |e| (g_index, p_index, e))
|
||||
})
|
||||
.map(|(g_index, p_index, extent)| {
|
||||
// Never fails for PSeekFiles.
|
||||
let mut reader = inputs[extent.device_index].reopen()?;
|
||||
let mut writer = files[g_index][p_index].reopen()?;
|
||||
|
||||
let r_path = &cli.input[extent.device_index];
|
||||
let w_path = &paths[g_index][p_index];
|
||||
|
||||
reader
|
||||
.seek(SeekFrom::Start(extent.lp_offset))
|
||||
.with_context(|| format!("Failed to seek file: {r_path:?}"))?;
|
||||
writer
|
||||
.seek(SeekFrom::Start(extent.out_offset))
|
||||
.with_context(|| format!("Failed to seek file: {w_path:?}"))?;
|
||||
|
||||
stream::copy_n(&mut reader, &mut writer, extent.size, cancel_signal)
|
||||
.with_context(|| format!("Failed to copy extent: {r_path:?} -> {w_path:?}"))?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.collect::<Result<()>>()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pack_subcommand(lp_cli: &LpCli, cli: &PackCli, cancel_signal: &AtomicBool) -> Result<()> {
|
||||
let mut metadata = read_info(&cli.input_info)?;
|
||||
|
||||
if metadata.slots.len() != 1 {
|
||||
bail!("There must be exactly one metadata slot");
|
||||
}
|
||||
|
||||
let slot = &mut metadata.slots[0];
|
||||
|
||||
let mut outputs = open_lp_outputs(&cli.output)?;
|
||||
|
||||
if slot.block_devices.len() != outputs.len() {
|
||||
bail!(
|
||||
"Need {} output images, but have {}",
|
||||
slot.block_devices.len(),
|
||||
outputs.len(),
|
||||
);
|
||||
}
|
||||
|
||||
if metadata.image_type == ImageType::Normal {
|
||||
for (i, (block_device, output)) in slot.block_devices.iter().zip(&outputs).enumerate() {
|
||||
output
|
||||
.set_len(block_device.size)
|
||||
.with_context(|| format!("Failed to truncate file: {:?}", cli.output[i]))?;
|
||||
}
|
||||
}
|
||||
|
||||
for group in &slot.groups {
|
||||
for partition in &group.partitions {
|
||||
let name = &partition.name;
|
||||
|
||||
if Path::new(name).file_name() != Some(OsStr::new(name)) {
|
||||
bail!("Unsafe partition name: {name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Preopen all image input files.
|
||||
let mut paths = vec![];
|
||||
let mut files = vec![];
|
||||
|
||||
if metadata.image_type == ImageType::Normal {
|
||||
let authority = ambient_authority();
|
||||
let directory = Dir::open_ambient_dir(&cli.input_images, authority)
|
||||
.with_context(|| format!("Failed to open directory: {:?}", cli.input_images))?;
|
||||
|
||||
for group in &mut slot.groups {
|
||||
let mut group_paths = vec![];
|
||||
let mut group_files = vec![];
|
||||
|
||||
for partition in &mut group.partitions {
|
||||
let path = format!("{}.img", partition.name);
|
||||
|
||||
let mut file = directory
|
||||
.open(&path)
|
||||
.map(|f| PSeekFile::new(f.into_std()))
|
||||
.with_context(|| format!("Failed to open for reading: {path:?}"))?;
|
||||
|
||||
let size = file
|
||||
.seek(SeekFrom::End(0))
|
||||
.with_context(|| format!("Failed to seek file: {path:?}"))?;
|
||||
|
||||
if size % u64::from(SECTOR_SIZE) != 0 {
|
||||
bail!("File size is not {SECTOR_SIZE}B aligned: {size}: {path:?}");
|
||||
}
|
||||
|
||||
// This will be filled out properly later during reallocation.
|
||||
partition.extents.push(Extent {
|
||||
num_sectors: size / u64::from(SECTOR_SIZE),
|
||||
extent_type: ExtentType::Linear {
|
||||
start_sector: 0,
|
||||
block_device_index: 0,
|
||||
},
|
||||
});
|
||||
|
||||
group_paths.push(path);
|
||||
group_files.push(file);
|
||||
}
|
||||
|
||||
paths.push(group_paths);
|
||||
files.push(group_files);
|
||||
}
|
||||
|
||||
// Now that we have all the partition sizes, actually allocate extents
|
||||
// for them on the block devices.
|
||||
slot.reallocate_extents()
|
||||
.context("Failed to allocate extents")?;
|
||||
}
|
||||
|
||||
// Display only the selected slot and make the rest identical.
|
||||
let _ = slot;
|
||||
display_metadata(lp_cli, &metadata);
|
||||
fill_slots(&mut metadata);
|
||||
let slot = &metadata.slots[0];
|
||||
|
||||
// Write the new metadata.
|
||||
metadata
|
||||
.to_writer(&mut outputs[0])
|
||||
.with_context(|| format!("Failed to write LP image metadata: {:?}", cli.output[0]))?;
|
||||
|
||||
// For empty images, there's no data to pack.
|
||||
if metadata.image_type == ImageType::Empty {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
slot.groups
|
||||
.par_iter()
|
||||
.enumerate()
|
||||
// Flatten grouped partitions.
|
||||
.flat_map(|(g_index, g)| {
|
||||
g.partitions
|
||||
.par_iter()
|
||||
.enumerate()
|
||||
.map(move |(p_index, p)| (g_index, p_index, p))
|
||||
})
|
||||
// Flatten extents in all partitions and split them to smaller chunks
|
||||
// for better parallelism.
|
||||
.flat_map(|(g_index, p_index, p)| {
|
||||
split_extents(&p.extents)
|
||||
.into_par_iter()
|
||||
.map(move |e| (g_index, p_index, e))
|
||||
})
|
||||
.map(|(g_index, p_index, extent)| {
|
||||
// Never fails for PSeekFiles.
|
||||
let mut reader = files[g_index][p_index].reopen()?;
|
||||
let mut writer = outputs[extent.device_index].reopen()?;
|
||||
|
||||
let r_path = &paths[g_index][p_index];
|
||||
let w_path = &cli.output[extent.device_index];
|
||||
|
||||
reader
|
||||
.seek(SeekFrom::Start(extent.out_offset))
|
||||
.with_context(|| format!("Failed to seek file: {r_path:?}"))?;
|
||||
writer
|
||||
.seek(SeekFrom::Start(extent.lp_offset))
|
||||
.with_context(|| format!("Failed to seek file: {w_path:?}"))?;
|
||||
|
||||
stream::copy_n(&mut reader, &mut writer, extent.size, cancel_signal)
|
||||
.with_context(|| format!("Failed to copy extent: {r_path:?} -> {w_path:?}"))?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.collect::<Result<()>>()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn repack_subcommand(lp_cli: &LpCli, cli: &RepackCli, cancel_signal: &AtomicBool) -> Result<()> {
|
||||
// Show a clap-style error if the number of inputs and outputs aren't equal.
|
||||
if cli.input.len() != cli.output.len() {
|
||||
let (arg_id, actual_len, expected_len) = if cli.input.len() < cli.output.len() {
|
||||
("input", cli.input.len(), cli.output.len())
|
||||
} else {
|
||||
("output", cli.output.len(), cli.input.len())
|
||||
};
|
||||
|
||||
let mut command = RepackCli::command();
|
||||
command.build();
|
||||
|
||||
let arg = command
|
||||
.get_arguments()
|
||||
.find(|a| a.get_id() == arg_id)
|
||||
.expect("argument not found");
|
||||
|
||||
let mut error =
|
||||
clap::Error::new(clap::error::ErrorKind::WrongNumberOfValues).with_cmd(&command);
|
||||
error.insert(
|
||||
clap::error::ContextKind::InvalidArg,
|
||||
clap::error::ContextValue::String(arg.to_string()),
|
||||
);
|
||||
error.insert(
|
||||
clap::error::ContextKind::ActualNumValues,
|
||||
clap::error::ContextValue::Number(actual_len as isize),
|
||||
);
|
||||
error.insert(
|
||||
clap::error::ContextKind::ExpectedNumValues,
|
||||
clap::error::ContextValue::Number(expected_len as isize),
|
||||
);
|
||||
|
||||
// We don't show the usage because only Command::_build_subcommand() can
|
||||
// create an appropriate Command instance for showing the subcommand
|
||||
// usage and there's no way to call that, directly or indirectly.
|
||||
|
||||
error.exit();
|
||||
}
|
||||
|
||||
let (inputs, mut metadata) = open_lp_inputs(&cli.input)?;
|
||||
let mut outputs = open_lp_outputs(&cli.output)?;
|
||||
|
||||
// Display only the selected slot and make the rest identical.
|
||||
let slot_number = get_slot_number(&metadata, cli.slot)?;
|
||||
retain_slot(&mut metadata, slot_number);
|
||||
display_metadata(lp_cli, &metadata);
|
||||
fill_slots(&mut metadata);
|
||||
|
||||
let slot = &metadata.slots[0];
|
||||
|
||||
if slot.block_devices.len() != inputs.len() {
|
||||
bail!(
|
||||
"Need {} images, but have {}",
|
||||
slot.block_devices.len(),
|
||||
inputs.len(),
|
||||
);
|
||||
}
|
||||
|
||||
// Write the new metadata.
|
||||
metadata
|
||||
.to_writer(&mut outputs[0])
|
||||
.with_context(|| format!("Failed to write LP image metadata: {:?}", cli.output[0]))?;
|
||||
|
||||
// Explicitly set the file size in case there are dm-zero extents, which are
|
||||
// ignored below.
|
||||
if metadata.image_type == ImageType::Normal {
|
||||
for (i, (block_device, output)) in slot.block_devices.iter().zip(&outputs).enumerate() {
|
||||
output
|
||||
.set_len(block_device.size)
|
||||
.with_context(|| format!("Failed to truncate file: {:?}", cli.output[i]))?;
|
||||
}
|
||||
}
|
||||
|
||||
slot.groups
|
||||
.par_iter()
|
||||
// Flatten grouped partitions.
|
||||
.flat_map(|group| &group.partitions)
|
||||
// Flatten extents in all partitions and split them to smaller chunks
|
||||
// for better parallelism.
|
||||
.flat_map(|partition| split_extents(&partition.extents))
|
||||
.map(|extent| {
|
||||
// Never fails for PSeekFiles.
|
||||
let mut reader = inputs[extent.device_index].reopen()?;
|
||||
let mut writer = outputs[extent.device_index].reopen()?;
|
||||
|
||||
let r_path = &cli.input[extent.device_index];
|
||||
let w_path = &cli.output[extent.device_index];
|
||||
|
||||
reader
|
||||
.seek(SeekFrom::Start(extent.lp_offset))
|
||||
.with_context(|| format!("Failed to seek file: {r_path:?}"))?;
|
||||
writer
|
||||
.seek(SeekFrom::Start(extent.lp_offset))
|
||||
.with_context(|| format!("Failed to seek file: {w_path:?}"))?;
|
||||
|
||||
stream::copy_n(&mut reader, &mut writer, extent.size, cancel_signal)
|
||||
.with_context(|| format!("Failed to copy extent: {r_path:?} -> {w_path:?}"))?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.collect::<Result<()>>()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn info_subcommand(lp_cli: &LpCli, cli: &InfoCli) -> Result<()> {
|
||||
let (_, metadata) = open_lp_inputs(&[&cli.input])?;
|
||||
|
||||
// Unlike the other subcommands, we show all metadata slots here.
|
||||
display_metadata(lp_cli, &metadata);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn lp_main(cli: &LpCli, cancel_signal: &AtomicBool) -> Result<()> {
|
||||
match &cli.command {
|
||||
LpCommand::Unpack(c) => unpack_subcommand(cli, c, cancel_signal),
|
||||
LpCommand::Pack(c) => pack_subcommand(cli, c, cancel_signal),
|
||||
LpCommand::Repack(c) => repack_subcommand(cli, c, cancel_signal),
|
||||
LpCommand::Info(c) => info_subcommand(cli, c),
|
||||
}
|
||||
}
|
||||
|
||||
/// Unpack an LP image.
|
||||
///
|
||||
/// The LP image metadata is written to the info TOML file. For normal images,
|
||||
/// each partition is extracted to `<partition name>.img` in the output images
|
||||
/// directory. For empty images, the output images directory is unused.
|
||||
///
|
||||
/// If any partition names are unsafe to use in a path, the extraction process
|
||||
/// will fail and exit. Extracted files are never written outside of the tree
|
||||
/// directory, even if an external process tries to interfere.
|
||||
#[derive(Debug, Parser)]
|
||||
struct UnpackCli {
|
||||
/// Path to input LP images.
|
||||
///
|
||||
/// If there are multiple images, they must be specified in order. If the
|
||||
/// order is unknown, run `avbroot lp info` against the `super` image and
|
||||
/// look at the `block_devices` field.
|
||||
#[arg(short, long, value_name = "FILE", value_parser, required = true)]
|
||||
input: Vec<PathBuf>,
|
||||
|
||||
/// Path to output info TOML.
|
||||
#[arg(long, value_name = "FILE", value_parser, default_value = "lp.toml")]
|
||||
output_info: PathBuf,
|
||||
|
||||
/// Path to output images directory.
|
||||
#[arg(long, value_name = "DIR", value_parser, default_value = "lp_images")]
|
||||
output_images: PathBuf,
|
||||
|
||||
/// The LP metadata slot to use.
|
||||
///
|
||||
/// This slot is the only slot where data extents are copied from. Any data
|
||||
/// referenced exclusively by other slots (if any) will be ignored.
|
||||
///
|
||||
/// This option is required if not all slots are identical.
|
||||
#[arg(short, long)]
|
||||
slot: Option<u32>,
|
||||
}
|
||||
|
||||
/// Pack an LP image.
|
||||
///
|
||||
/// For normal images, the number of metadata slots written is equal to the
|
||||
/// `metadata_slot_count` value in the info TOML. Each slot has identical
|
||||
/// metadata. It is not possible to write multiple slots with different metadata
|
||||
/// using this tool. For empty images, only a single slot is written, regardless
|
||||
/// of the value of `metadata_slot_count`, as required by the file format.
|
||||
///
|
||||
/// The new LP image will *only* contain images listed in the info TOML file and
|
||||
/// they are added in the order listed. The input images directory is not used
|
||||
/// when packing an empty image.
|
||||
#[derive(Debug, Parser)]
|
||||
struct PackCli {
|
||||
/// Path to output LP images.
|
||||
///
|
||||
/// If there are multiple images, they must be specified in the same order
|
||||
/// as the block device entries are listed in the info TOML.
|
||||
#[arg(short, long, value_name = "FILE", value_parser, required = true)]
|
||||
output: Vec<PathBuf>,
|
||||
|
||||
/// Path to input info TOML.
|
||||
#[arg(long, value_name = "FILE", value_parser, default_value = "lp.toml")]
|
||||
input_info: PathBuf,
|
||||
|
||||
/// Path to input images directory.
|
||||
#[arg(long, value_name = "DIR", value_parser, default_value = "lp_images")]
|
||||
input_images: PathBuf,
|
||||
}
|
||||
|
||||
/// Repack an LP image.
|
||||
///
|
||||
/// This command is equivalent to running `unpack` and `pack`, except without
|
||||
/// storing the unpacked data to disk.
|
||||
#[derive(Debug, Parser)]
|
||||
struct RepackCli {
|
||||
/// Path to input LP images.
|
||||
///
|
||||
/// If there are multiple images, they must be specified in order. If the
|
||||
/// order is unknown, run `avbroot lp info` against the `super` image and
|
||||
/// look at the `block_devices` field.
|
||||
#[arg(short, long, value_name = "FILE", value_parser, required = true)]
|
||||
input: Vec<PathBuf>,
|
||||
|
||||
/// Path to output LP images.
|
||||
///
|
||||
/// The number of output images must equal the number of input images.
|
||||
#[arg(short, long, value_name = "FILE", value_parser, required = true)]
|
||||
output: Vec<PathBuf>,
|
||||
|
||||
/// The LP metadata slot to use.
|
||||
///
|
||||
/// This slot is the only slot where data extents are copied to the output
|
||||
/// images. Any data referenced exclusively by other slots (if any) will be
|
||||
/// ignored.
|
||||
///
|
||||
/// This option is required if not all slots are identical.
|
||||
#[arg(short, long)]
|
||||
slot: Option<u32>,
|
||||
}
|
||||
|
||||
/// Display LP image metadata.
|
||||
#[derive(Debug, Parser)]
|
||||
struct InfoCli {
|
||||
/// Path to input LP image.
|
||||
///
|
||||
/// If there are multiple images, this should refer to the first one, which
|
||||
/// is usually the `super` image. The other images are not needed when
|
||||
/// inspecting the metadata because the metadata is only stored in the first
|
||||
/// image.
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
input: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
enum LpCommand {
|
||||
Unpack(UnpackCli),
|
||||
Pack(PackCli),
|
||||
Repack(RepackCli),
|
||||
Info(InfoCli),
|
||||
}
|
||||
|
||||
/// Pack, unpack, and inspect LP images.
|
||||
#[derive(Debug, Parser)]
|
||||
pub struct LpCli {
|
||||
#[command(subcommand)]
|
||||
command: LpCommand,
|
||||
|
||||
/// Don't print LP metadata information.
|
||||
#[arg(short, long, global = true)]
|
||||
quiet: bool,
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Andrew Gunnerson
|
||||
* SPDX-FileCopyrightText: 2023-2024 Andrew Gunnerson
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
@@ -11,4 +11,7 @@ pub mod cpio;
|
||||
pub mod fec;
|
||||
pub mod hashtree;
|
||||
pub mod key;
|
||||
pub mod lp;
|
||||
pub mod ota;
|
||||
pub mod payload;
|
||||
pub mod sparse;
|
||||
|
||||
+157
-51
@@ -21,7 +21,6 @@ use cap_std::{ambient_authority, fs::Dir};
|
||||
use cap_tempfile::TempDir;
|
||||
use clap::{value_parser, ArgAction, Args, Parser, Subcommand};
|
||||
use rayon::{iter::IntoParallelRefIterator, prelude::ParallelIterator};
|
||||
use rsa::RsaPrivateKey;
|
||||
use tempfile::NamedTempFile;
|
||||
use topological_sort::TopologicalSort;
|
||||
use tracing::{debug_span, info, warn};
|
||||
@@ -30,12 +29,12 @@ use zip::{write::FileOptions, CompressionMethod, ZipArchive, ZipWriter};
|
||||
|
||||
use crate::{
|
||||
cli,
|
||||
crypto::{self, PassphraseSource},
|
||||
crypto::{self, PassphraseSource, RsaSigningKey},
|
||||
format::{
|
||||
avb::{self, Descriptor, Header},
|
||||
ota::{self, SigningWriter, ZipEntry},
|
||||
ota::{self, SigningWriter, ZipEntry, ZipMode},
|
||||
padding,
|
||||
payload::{self, PayloadHeader, PayloadWriter},
|
||||
payload::{self, PayloadHeader, PayloadWriter, VabcAlgo},
|
||||
},
|
||||
patch::{
|
||||
boot::{
|
||||
@@ -48,8 +47,8 @@ use crate::{
|
||||
build::tools::releasetools::OtaMetadata, chromeos_update_engine::DeltaArchiveManifest,
|
||||
},
|
||||
stream::{
|
||||
self, CountingWriter, FromReader, HashingWriter, HolePunchingWriter, PSeekFile,
|
||||
ReadSeekReopen, Reopen, SectionReader, ToWriter, WriteSeekReopen,
|
||||
self, CountingWriter, FromReader, HashingWriter, PSeekFile, ReadSeekReopen, Reopen,
|
||||
SectionReader, ToWriter, WriteSeekReopen,
|
||||
},
|
||||
util,
|
||||
};
|
||||
@@ -197,7 +196,7 @@ fn patch_boot_images<'a, 'b: 'a>(
|
||||
required_images: &'b RequiredImages,
|
||||
input_files: &mut HashMap<String, InputFile>,
|
||||
boot_patchers: Vec<Box<dyn BootImagePatch + Sync>>,
|
||||
key_avb: &RsaPrivateKey,
|
||||
key_avb: &RsaSigningKey,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<()> {
|
||||
let input_files = Mutex::new(input_files);
|
||||
@@ -241,7 +240,7 @@ fn patch_system_image<'a, 'b: 'a>(
|
||||
required_images: &'b RequiredImages,
|
||||
input_files: &mut HashMap<String, InputFile>,
|
||||
cert_ota: &Certificate,
|
||||
key_avb: &RsaPrivateKey,
|
||||
key_avb: &RsaSigningKey,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<(&'b str, Vec<Range<u64>>)> {
|
||||
let Some(target) = required_images.iter_system().next() else {
|
||||
@@ -565,7 +564,7 @@ fn update_vbmeta_headers(
|
||||
headers: &mut HashMap<String, Header>,
|
||||
order: &mut [(String, HashSet<String>)],
|
||||
clear_vbmeta_flags: bool,
|
||||
key: &RsaPrivateKey,
|
||||
key: &RsaSigningKey,
|
||||
block_size: u64,
|
||||
) -> Result<()> {
|
||||
for (name, deps) in order {
|
||||
@@ -624,7 +623,7 @@ fn update_vbmeta_headers(
|
||||
/// If `ranges` is [`None`], then the entire file is compressed. Otherwise, only
|
||||
/// the chunks containing the specified ranges are compressed. In the latter
|
||||
/// scenario, unmodified chunks must be copied from the original payload.
|
||||
fn compress_image(
|
||||
pub fn compress_image(
|
||||
name: &str,
|
||||
file: &mut PSeekFile,
|
||||
header: &mut PayloadHeader,
|
||||
@@ -674,12 +673,43 @@ fn compress_image(
|
||||
|
||||
info!("Compressing full image: {name}");
|
||||
|
||||
// Otherwise, compress the entire image.
|
||||
let (partition_info, operations) =
|
||||
payload::compress_image(&*file, &writer, name, block_size, cancel_signal)?;
|
||||
// Otherwise, compress the entire image. If VABC is enabled, we need to
|
||||
// update the CoW size estimate or else the CoW block device may run out of
|
||||
// space during flashing.
|
||||
let vabc_algo = if partition.estimate_cow_size.is_some() {
|
||||
info!("Needs updated CoW size estimate: {name}");
|
||||
|
||||
// Only CoW v2 seems to exist in the wild currently, so that is all we
|
||||
// support.
|
||||
let Some(dpm) = &header.manifest.dynamic_partition_metadata else {
|
||||
bail!("Dynamic partition metadata is missing");
|
||||
};
|
||||
|
||||
if !dpm.vabc_enabled() {
|
||||
bail!("Partition has CoW estimate, but VABC is disabled: {name}");
|
||||
}
|
||||
|
||||
let cow_version = dpm.cow_version();
|
||||
if dpm.cow_version() != 2 {
|
||||
bail!("Unsupported CoW version: {cow_version}");
|
||||
}
|
||||
|
||||
let compression = dpm.vabc_compression_param();
|
||||
let Some(vabc_algo) = VabcAlgo::new(compression) else {
|
||||
bail!("Unsupported VABC compression: {compression}");
|
||||
};
|
||||
|
||||
Some(vabc_algo)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let (partition_info, operations, cow_estimate) =
|
||||
payload::compress_image(&*file, &writer, name, block_size, vabc_algo, cancel_signal)?;
|
||||
|
||||
partition.new_partition_info = Some(partition_info);
|
||||
partition.operations = operations;
|
||||
partition.estimate_cow_size = cow_estimate;
|
||||
|
||||
*file = writer;
|
||||
|
||||
@@ -694,20 +724,18 @@ fn patch_ota_payload(
|
||||
external_images: &HashMap<String, PathBuf>,
|
||||
boot_patchers: Vec<Box<dyn BootImagePatch + Sync>>,
|
||||
clear_vbmeta_flags: bool,
|
||||
key_avb: &RsaPrivateKey,
|
||||
key_ota: &RsaPrivateKey,
|
||||
key_avb: &RsaSigningKey,
|
||||
key_ota: &RsaSigningKey,
|
||||
cert_ota: &Certificate,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<(String, u64)> {
|
||||
let header = PayloadHeader::from_reader(payload.reopen_boxed()?)
|
||||
let mut header = PayloadHeader::from_reader(payload.reopen_boxed()?)
|
||||
.context("Failed to load OTA payload header")?;
|
||||
if !header.is_full_ota() {
|
||||
bail!("Payload is a delta OTA, not a full OTA");
|
||||
}
|
||||
|
||||
let header = Mutex::new(header);
|
||||
let mut header_locked = header.lock().unwrap();
|
||||
let all_partitions = header_locked
|
||||
let all_partitions = header
|
||||
.manifest
|
||||
.partitions
|
||||
.iter()
|
||||
@@ -725,7 +753,7 @@ fn patch_ota_payload(
|
||||
// Determine what images need to be patched. For simplicity, we pre-read all
|
||||
// vbmeta images since they're tiny. They're discarded later if the they
|
||||
// don't need to be modified.
|
||||
let required_images = RequiredImages::new(&header_locked.manifest);
|
||||
let required_images = RequiredImages::new(&header.manifest);
|
||||
let vbmeta_images = required_images.iter_vbmeta().collect::<HashSet<_>>();
|
||||
|
||||
// The set of source images to be inserted into the new payload, replacing
|
||||
@@ -737,7 +765,7 @@ fn patch_ota_payload(
|
||||
payload,
|
||||
&required_images,
|
||||
external_images,
|
||||
&header_locked,
|
||||
&header,
|
||||
cancel_signal,
|
||||
)?;
|
||||
|
||||
@@ -779,7 +807,7 @@ fn patch_ota_payload(
|
||||
&mut vbmeta_order,
|
||||
clear_vbmeta_flags,
|
||||
key_avb,
|
||||
header_locked.manifest.block_size().into(),
|
||||
header.manifest.block_size().into(),
|
||||
)?;
|
||||
|
||||
// Unmodified vbmeta images no longer need to be kept around either.
|
||||
@@ -791,7 +819,7 @@ fn patch_ota_payload(
|
||||
let modified_operations = compress_image(
|
||||
&name,
|
||||
&mut input_file.file,
|
||||
&mut header_locked,
|
||||
&mut header,
|
||||
// We can only perform the optimization of avoiding
|
||||
// recompression if the image came from the original payload.
|
||||
if name == system_target && !external_images.contains_key(&name) {
|
||||
@@ -809,7 +837,7 @@ fn patch_ota_payload(
|
||||
|
||||
info!("Generating new OTA payload");
|
||||
|
||||
let mut payload_writer = PayloadWriter::new(writer, header_locked.clone(), key_ota.clone())
|
||||
let mut payload_writer = PayloadWriter::new(writer, header.clone(), key_ota.clone())
|
||||
.context("Failed to write payload header")?;
|
||||
let mut orig_payload_reader = payload.reopen_boxed().context("Failed to open payload")?;
|
||||
|
||||
@@ -827,7 +855,7 @@ fn patch_ota_payload(
|
||||
|
||||
let pi = payload_writer.partition_index().unwrap();
|
||||
let oi = payload_writer.operation_index().unwrap();
|
||||
let orig_partition = &header_locked.manifest.partitions[pi];
|
||||
let orig_partition = &header.manifest.partitions[pi];
|
||||
let orig_operation = &orig_partition.operations[oi];
|
||||
let data_offset = orig_operation
|
||||
.data_offset
|
||||
@@ -857,7 +885,7 @@ fn patch_ota_payload(
|
||||
|
||||
// Otherwise, copy from the original payload.
|
||||
let data_offset = data_offset
|
||||
.checked_add(header_locked.blob_offset)
|
||||
.checked_add(header.blob_offset)
|
||||
.ok_or_else(|| anyhow!("data_offset overflow in partition #{pi} operation #{oi}"))?;
|
||||
|
||||
orig_payload_reader
|
||||
@@ -873,7 +901,7 @@ fn patch_ota_payload(
|
||||
.with_context(|| format!("Failed to copy from original payload: {name}"))?;
|
||||
}
|
||||
|
||||
let (_, properties, metadata_size) = payload_writer
|
||||
let (_, _, properties, metadata_size) = payload_writer
|
||||
.finish()
|
||||
.context("Failed to finalize payload")?;
|
||||
|
||||
@@ -888,8 +916,9 @@ fn patch_ota_zip(
|
||||
external_images: &HashMap<String, PathBuf>,
|
||||
mut boot_patchers: Vec<Box<dyn BootImagePatch + Sync>>,
|
||||
clear_vbmeta_flags: bool,
|
||||
key_avb: &RsaPrivateKey,
|
||||
key_ota: &RsaPrivateKey,
|
||||
zip_mode: ZipMode,
|
||||
key_avb: &RsaSigningKey,
|
||||
key_ota: &RsaSigningKey,
|
||||
cert_ota: &Certificate,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<(OtaMetadata, u64)> {
|
||||
@@ -1050,7 +1079,16 @@ fn patch_ota_zip(
|
||||
|
||||
info!("Generating new OTA metadata");
|
||||
|
||||
let data_descriptor_size = if last_entry_used_zip64 { 24 } else { 16 };
|
||||
let data_descriptor_size = match zip_mode {
|
||||
ZipMode::Streaming => {
|
||||
if last_entry_used_zip64 {
|
||||
24
|
||||
} else {
|
||||
16
|
||||
}
|
||||
}
|
||||
ZipMode::Seekable => 0,
|
||||
};
|
||||
let metadata = ota::add_metadata(
|
||||
&entries,
|
||||
zip_writer,
|
||||
@@ -1058,13 +1096,14 @@ fn patch_ota_zip(
|
||||
entries.last().map(|e| e.offset + e.size).unwrap() + data_descriptor_size,
|
||||
&metadata.unwrap(),
|
||||
payload_metadata_size.unwrap(),
|
||||
zip_mode,
|
||||
)
|
||||
.context("Failed to write new OTA metadata")?;
|
||||
|
||||
Ok((metadata, payload_metadata_size.unwrap()))
|
||||
}
|
||||
|
||||
fn extract_ota_zip(
|
||||
pub fn extract_payload(
|
||||
raw_reader: &PSeekFile,
|
||||
directory: &Dir,
|
||||
payload_offset: u64,
|
||||
@@ -1190,10 +1229,38 @@ pub fn patch_subcommand(cli: &PatchCli, cancel_signal: &AtomicBool) -> Result<()
|
||||
cli.pass_ota_env_var.as_deref(),
|
||||
);
|
||||
|
||||
let key_avb = crypto::read_pem_key_file(&cli.key_avb, &source_avb)
|
||||
.with_context(|| format!("Failed to load key: {:?}", cli.key_avb))?;
|
||||
let key_ota = crypto::read_pem_key_file(&cli.key_ota, &source_ota)
|
||||
.with_context(|| format!("Failed to load key: {:?}", cli.key_ota))?;
|
||||
let (key_avb, key_ota) = if let Some(helper) = &cli.signing_helper {
|
||||
let public_key_avb = crypto::read_pem_public_key_file(&cli.key_avb)
|
||||
.with_context(|| format!("Failed to load key: {:?}", cli.key_avb))?;
|
||||
let public_key_ota = crypto::read_pem_public_key_file(&cli.key_ota)
|
||||
.with_context(|| format!("Failed to load key: {:?}", cli.key_ota))?;
|
||||
|
||||
let key_avb = RsaSigningKey::External {
|
||||
program: helper.clone(),
|
||||
public_key_file: cli.key_avb.clone(),
|
||||
public_key: public_key_avb,
|
||||
passphrase_source: source_avb,
|
||||
};
|
||||
let key_ota = RsaSigningKey::External {
|
||||
program: helper.clone(),
|
||||
public_key_file: cli.key_ota.clone(),
|
||||
public_key: public_key_ota,
|
||||
passphrase_source: source_ota,
|
||||
};
|
||||
|
||||
(key_avb, key_ota)
|
||||
} else {
|
||||
let private_key_avb = crypto::read_pem_key_file(&cli.key_avb, &source_avb)
|
||||
.with_context(|| format!("Failed to load key: {:?}", cli.key_avb))?;
|
||||
let private_key_ota = crypto::read_pem_key_file(&cli.key_ota, &source_ota)
|
||||
.with_context(|| format!("Failed to load key: {:?}", cli.key_ota))?;
|
||||
|
||||
let key_avb = RsaSigningKey::Internal(private_key_avb);
|
||||
let key_ota = RsaSigningKey::Internal(private_key_ota);
|
||||
|
||||
(key_avb, key_ota)
|
||||
};
|
||||
|
||||
let cert_ota = crypto::read_pem_cert_file(&cli.cert_ota)
|
||||
.with_context(|| format!("Failed to load certificate: {:?}", cli.cert_ota))?;
|
||||
|
||||
@@ -1217,7 +1284,6 @@ pub fn patch_subcommand(cli: &PatchCli, cancel_signal: &AtomicBool) -> Result<()
|
||||
}
|
||||
|
||||
let mut boot_patchers = Vec::<Box<dyn BootImagePatch + Sync>>::new();
|
||||
boot_patchers.push(Box::new(OtaCertPatcher::new(cert_ota.clone())));
|
||||
|
||||
if let Some(magisk) = &cli.root.magisk {
|
||||
boot_patchers.push(Box::new(
|
||||
@@ -1230,6 +1296,8 @@ pub fn patch_subcommand(cli: &PatchCli, cancel_signal: &AtomicBool) -> Result<()
|
||||
.context("Failed to create Magisk boot image patcher")?,
|
||||
));
|
||||
} else if let Some(prepatched) = &cli.root.prepatched {
|
||||
// NOTE: This patcher must run first! Otherwise, it'll wipe out any
|
||||
// ramdisk changes made by other patchers.
|
||||
boot_patchers.push(Box::new(PrepatchedImagePatcher::new(
|
||||
prepatched,
|
||||
cli.ignore_prepatched_compat + 1,
|
||||
@@ -1238,6 +1306,8 @@ pub fn patch_subcommand(cli: &PatchCli, cancel_signal: &AtomicBool) -> Result<()
|
||||
assert!(cli.root.rootless);
|
||||
};
|
||||
|
||||
boot_patchers.push(Box::new(OtaCertPatcher::new(cert_ota.clone())));
|
||||
|
||||
if cli.dsu {
|
||||
boot_patchers.push(Box::new(DsuPubKeyPatcher::new(key_avb.to_public_key())));
|
||||
}
|
||||
@@ -1257,10 +1327,16 @@ pub fn patch_subcommand(cli: &PatchCli, cancel_signal: &AtomicBool) -> Result<()
|
||||
)
|
||||
.context("Failed to open temporary output file")?;
|
||||
let temp_path = temp_writer.path().to_owned();
|
||||
let hole_punching_writer = HolePunchingWriter::new(temp_writer);
|
||||
let buffered_writer = BufWriter::new(hole_punching_writer);
|
||||
let signing_writer = SigningWriter::new(buffered_writer);
|
||||
let mut zip_writer = ZipWriter::new_streaming(signing_writer);
|
||||
let mut zip_writer = match cli.zip_mode {
|
||||
ZipMode::Streaming => {
|
||||
let signing_writer = SigningWriter::new_streaming(temp_writer);
|
||||
ZipWriter::new_streaming(signing_writer)
|
||||
}
|
||||
ZipMode::Seekable => {
|
||||
let signing_writer = SigningWriter::new_seekable(temp_writer);
|
||||
ZipWriter::new(signing_writer)
|
||||
}
|
||||
};
|
||||
|
||||
let (metadata, payload_metadata_size) = patch_ota_zip(
|
||||
&raw_reader,
|
||||
@@ -1269,6 +1345,7 @@ pub fn patch_subcommand(cli: &PatchCli, cancel_signal: &AtomicBool) -> Result<()
|
||||
&external_images,
|
||||
boot_patchers,
|
||||
cli.clear_vbmeta_flags,
|
||||
cli.zip_mode,
|
||||
&key_avb,
|
||||
&key_ota,
|
||||
&cert_ota,
|
||||
@@ -1279,13 +1356,9 @@ pub fn patch_subcommand(cli: &PatchCli, cancel_signal: &AtomicBool) -> Result<()
|
||||
let signing_writer = zip_writer
|
||||
.finish()
|
||||
.context("Failed to finalize output zip")?;
|
||||
let buffered_writer = signing_writer
|
||||
.finish(&key_ota, &cert_ota)
|
||||
let mut temp_writer = signing_writer
|
||||
.finish(&key_ota, &cert_ota, cancel_signal)
|
||||
.context("Failed to sign output zip")?;
|
||||
let hole_punching_writer = buffered_writer
|
||||
.into_inner()
|
||||
.context("Failed to flush output zip")?;
|
||||
let mut temp_writer = hole_punching_writer.into_inner();
|
||||
temp_writer.flush().context("Failed to flush output zip")?;
|
||||
|
||||
// We do a lot of low-level hackery. Reopen and verify offsets.
|
||||
@@ -1387,7 +1460,7 @@ pub fn extract_subcommand(cli: &ExtractCli, cancel_signal: &AtomicBool) -> Resul
|
||||
let directory = Dir::open_ambient_dir(&cli.directory, authority)
|
||||
.with_context(|| format!("Failed to open directory: {:?}", cli.directory))?;
|
||||
|
||||
extract_ota_zip(
|
||||
extract_payload(
|
||||
&raw_reader,
|
||||
&directory,
|
||||
payload_offset,
|
||||
@@ -1599,7 +1672,7 @@ pub fn verify_subcommand(cli: &VerifyCli, cancel_signal: &AtomicBool) -> Result<
|
||||
.cloned()
|
||||
.collect::<BTreeSet<_>>();
|
||||
|
||||
extract_ota_zip(
|
||||
extract_payload(
|
||||
&raw_reader,
|
||||
&temp_dir,
|
||||
pf_payload.offset,
|
||||
@@ -1720,7 +1793,10 @@ pub struct PatchCli {
|
||||
#[arg(short, long, value_name = "FILE", value_parser, help_heading = HEADING_PATH)]
|
||||
pub output: Option<PathBuf>,
|
||||
|
||||
/// Private key for signing vbmeta images.
|
||||
/// Signing key for vbmeta headers.
|
||||
///
|
||||
/// This should normally be a private key. However, if --signing-helper is
|
||||
/// used, then it should be a public key instead.
|
||||
#[arg(
|
||||
long,
|
||||
alias = "privkey-avb",
|
||||
@@ -1730,7 +1806,10 @@ pub struct PatchCli {
|
||||
)]
|
||||
pub key_avb: PathBuf,
|
||||
|
||||
/// Private key for signing the OTA.
|
||||
/// Signing key for the OTA.
|
||||
///
|
||||
/// This should normally be a private key. However, if --signing-helper is
|
||||
/// used, then it should be a public key instead.
|
||||
#[arg(
|
||||
long,
|
||||
alias = "privkey-ota",
|
||||
@@ -1788,13 +1867,22 @@ pub struct PatchCli {
|
||||
)]
|
||||
pub pass_ota_file: Option<PathBuf>,
|
||||
|
||||
/// External program for signing.
|
||||
///
|
||||
/// If this option is specified, then --key-avb and --key-ota must refer to
|
||||
/// public keys. The program will be invoked as:
|
||||
///
|
||||
/// <program> <algo> <public key> [file <pass file>|env <pass env>]
|
||||
#[arg(long, value_name = "PROGRAM", value_parser, help_heading = HEADING_KEY)]
|
||||
pub signing_helper: Option<PathBuf>,
|
||||
|
||||
/// Use partition image from a file instead of the original payload.
|
||||
#[arg(
|
||||
long,
|
||||
value_names = ["PARTITION", "FILE"],
|
||||
value_parser = value_parser!(OsString),
|
||||
num_args = 2,
|
||||
help_heading = HEADING_PATH,
|
||||
help_heading = HEADING_PATH
|
||||
)]
|
||||
pub replace: Vec<OsString>,
|
||||
|
||||
@@ -1844,6 +1932,24 @@ pub struct PatchCli {
|
||||
#[arg(long, help_heading = HEADING_OTHER)]
|
||||
pub clear_vbmeta_flags: bool,
|
||||
|
||||
/// Zip creation mode for the output OTA zip.
|
||||
///
|
||||
/// The streaming mode produces zip files that contain data descriptors.
|
||||
/// The zip file is hashed as it is being written. This mode is the default
|
||||
/// and works with the vast majority of devices.
|
||||
///
|
||||
/// The seekable mode produces zip files that do not use data descriptors.
|
||||
/// The zip file is reread and hashed after it has been fully written. The
|
||||
/// output file is more likely to be compatible with devices that have
|
||||
/// broken zip file parsers.
|
||||
#[arg(
|
||||
long,
|
||||
value_name = "MODE",
|
||||
default_value_t = ZipMode::Streaming,
|
||||
help_heading = HEADING_OTHER
|
||||
)]
|
||||
pub zip_mode: ZipMode,
|
||||
|
||||
/// (Deprecated: no longer needed)
|
||||
#[arg(
|
||||
long,
|
||||
|
||||
@@ -0,0 +1,473 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024 Andrew Gunnerson
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
ffi::{OsStr, OsString},
|
||||
fs::{self, File},
|
||||
io::{BufReader, BufWriter, Seek, SeekFrom},
|
||||
path::{Path, PathBuf},
|
||||
sync::atomic::AtomicBool,
|
||||
};
|
||||
|
||||
use anyhow::{anyhow, bail, Context, Result};
|
||||
use cap_std::{ambient_authority, fs::Dir};
|
||||
use clap::{Args, Parser, Subcommand};
|
||||
use tracing::info;
|
||||
|
||||
use crate::{
|
||||
cli::ota,
|
||||
crypto::{self, PassphraseSource, RsaSigningKey},
|
||||
format::payload::{PayloadHeader, PayloadWriter},
|
||||
stream::{self, FromReader, PSeekFile},
|
||||
};
|
||||
|
||||
fn open_reader(path: &Path, allow_delta: bool) -> Result<(BufReader<File>, PayloadHeader)> {
|
||||
let mut reader = File::open(path)
|
||||
.map(BufReader::new)
|
||||
.with_context(|| format!("Failed to open payload for reading: {path:?}"))?;
|
||||
let header = PayloadHeader::from_reader(&mut reader)
|
||||
.with_context(|| format!("Failed to read payload header: {path:?}"))?;
|
||||
if !allow_delta && !header.is_full_ota() {
|
||||
bail!("Payload is a delta OTA, not a full OTA");
|
||||
}
|
||||
|
||||
Ok((reader, header))
|
||||
}
|
||||
|
||||
fn open_writer(
|
||||
path: &Path,
|
||||
header: PayloadHeader,
|
||||
key: RsaSigningKey,
|
||||
) -> Result<PayloadWriter<BufWriter<File>>> {
|
||||
let writer = File::create(path)
|
||||
.map(BufWriter::new)
|
||||
.with_context(|| format!("Failed to open payload for writing: {path:?}"))?;
|
||||
let payload_writer = PayloadWriter::new(writer, header, key)
|
||||
.with_context(|| format!("Failed to write payload header: {path:?}"))?;
|
||||
|
||||
Ok(payload_writer)
|
||||
}
|
||||
|
||||
fn read_info(path: &Path) -> Result<PayloadHeader> {
|
||||
let data = fs::read_to_string(path)
|
||||
.with_context(|| format!("Failed to read payload info TOML: {path:?}"))?;
|
||||
let info = toml_edit::de::from_str(&data)
|
||||
.with_context(|| format!("Failed to parse payload info TOML: {path:?}"))?;
|
||||
|
||||
Ok(info)
|
||||
}
|
||||
|
||||
fn write_info(path: &Path, manifest: &PayloadHeader) -> Result<()> {
|
||||
let data = toml_edit::ser::to_string_pretty(manifest)
|
||||
.with_context(|| format!("Failed to serialize payload info TOML: {path:?}"))?;
|
||||
fs::write(path, data)
|
||||
.with_context(|| format!("Failed to write payload info TOML: {path:?}"))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn display_header(cli: &PayloadCli, header: &PayloadHeader) {
|
||||
if !cli.quiet {
|
||||
println!("{header:#?}");
|
||||
}
|
||||
}
|
||||
|
||||
fn load_key(group: &KeyGroup) -> Result<RsaSigningKey> {
|
||||
let source = PassphraseSource::new(
|
||||
&group.key,
|
||||
group.pass_file.as_deref(),
|
||||
group.pass_env_var.as_deref(),
|
||||
);
|
||||
let signing_key = if let Some(helper) = &group.signing_helper {
|
||||
let public_key = crypto::read_pem_public_key_file(&group.key)
|
||||
.with_context(|| format!("Failed to load key: {:?}", group.key))?;
|
||||
|
||||
RsaSigningKey::External {
|
||||
program: helper.clone(),
|
||||
public_key_file: group.key.clone(),
|
||||
public_key,
|
||||
passphrase_source: source,
|
||||
}
|
||||
} else {
|
||||
let private_key = crypto::read_pem_key_file(&group.key, &source)
|
||||
.with_context(|| format!("Failed to load key: {:?}", group.key))?;
|
||||
|
||||
RsaSigningKey::Internal(private_key)
|
||||
};
|
||||
|
||||
Ok(signing_key)
|
||||
}
|
||||
|
||||
fn unpack_subcommand(
|
||||
payload_cli: &PayloadCli,
|
||||
cli: &UnpackCli,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<()> {
|
||||
let (mut reader, header) = open_reader(&cli.input, false)?;
|
||||
let payload_size = reader
|
||||
.seek(SeekFrom::End(0))
|
||||
.with_context(|| format!("Failed to get file size: {:?}", cli.input))?;
|
||||
|
||||
display_header(payload_cli, &header);
|
||||
|
||||
write_info(&cli.output_info, &header)?;
|
||||
|
||||
let authority = ambient_authority();
|
||||
Dir::create_ambient_dir_all(&cli.output_images, authority)
|
||||
.with_context(|| format!("Failed to create directory: {:?}", cli.output_images))?;
|
||||
let directory = Dir::open_ambient_dir(&cli.output_images, authority)
|
||||
.with_context(|| format!("Failed to open directory: {:?}", cli.output_images))?;
|
||||
|
||||
ota::extract_payload(
|
||||
&PSeekFile::new(reader.into_inner()),
|
||||
&directory,
|
||||
0,
|
||||
payload_size,
|
||||
&header,
|
||||
&header
|
||||
.manifest
|
||||
.partitions
|
||||
.iter()
|
||||
.map(|p| &p.partition_name)
|
||||
.cloned()
|
||||
.collect(),
|
||||
cancel_signal,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pack_subcommand(
|
||||
payload_cli: &PayloadCli,
|
||||
cli: &PackCli,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<()> {
|
||||
let signing_key = load_key(&cli.key)?;
|
||||
|
||||
let mut header = read_info(&cli.input_info)?;
|
||||
|
||||
let authority = ambient_authority();
|
||||
let directory = Dir::open_ambient_dir(&cli.input_images, authority)
|
||||
.with_context(|| format!("Failed to open directory: {:?}", cli.input_images))?;
|
||||
|
||||
for p in &header.manifest.partitions {
|
||||
let name = &p.partition_name;
|
||||
|
||||
if Path::new(name).file_name() != Some(OsStr::new(name)) {
|
||||
bail!("Unsafe partition name: {name}");
|
||||
}
|
||||
}
|
||||
|
||||
// Pre-open all of the image files.
|
||||
let input_files = header
|
||||
.manifest
|
||||
.partitions
|
||||
.iter()
|
||||
.map(|p| {
|
||||
let path = format!("{}.img", p.partition_name);
|
||||
let file = directory
|
||||
.open(&path)
|
||||
.map(|f| PSeekFile::new(f.into_std()))
|
||||
.with_context(|| format!("Failed to open file: {path:?}"))?;
|
||||
|
||||
Ok((p.partition_name.clone(), file))
|
||||
})
|
||||
.collect::<Result<HashMap<_, _>>>()?;
|
||||
|
||||
// Compress the images and compute the list of install operations for
|
||||
// insertion into the payload header. The compressed data is stored in new
|
||||
// temp files and the original input files are dropped.
|
||||
let mut compressed_files = input_files
|
||||
.into_iter()
|
||||
.map(|(name, mut input_file)| {
|
||||
ota::compress_image(&name, &mut input_file, &mut header, None, cancel_signal)
|
||||
.with_context(|| format!("Failed to compress image: {name}"))?;
|
||||
|
||||
Ok((name, input_file))
|
||||
})
|
||||
.collect::<Result<HashMap<_, _>>>()?;
|
||||
|
||||
info!("Generating new OTA payload");
|
||||
|
||||
// Now we can write the actual payload. With everything precomputed, this is
|
||||
// mostly just a simple copy.
|
||||
let mut payload_writer = open_writer(&cli.output, header.clone(), signing_key)?;
|
||||
|
||||
while payload_writer
|
||||
.begin_next_operation()
|
||||
.context("Failed to begin next payload blob entry")?
|
||||
{
|
||||
let name = payload_writer.partition().unwrap().partition_name.clone();
|
||||
let operation = payload_writer.operation().unwrap();
|
||||
|
||||
let Some(data_length) = operation.data_length else {
|
||||
// Otherwise, this is a ZERO/DISCARD operation.
|
||||
continue;
|
||||
};
|
||||
|
||||
let pi = payload_writer.partition_index().unwrap();
|
||||
let oi = payload_writer.operation_index().unwrap();
|
||||
let orig_partition = &header.manifest.partitions[pi];
|
||||
let orig_operation = &orig_partition.operations[oi];
|
||||
let data_offset = orig_operation
|
||||
.data_offset
|
||||
.ok_or_else(|| anyhow!("Missing data_offset in partition #{pi} operation #{oi}"))?;
|
||||
|
||||
// The compressed chunks are laid out sequentially and data_offset is
|
||||
// set to the offset within that file.
|
||||
let Some(input_file) = compressed_files.get_mut(&name) else {
|
||||
unreachable!("Compressed data not found for image: {name}");
|
||||
};
|
||||
|
||||
input_file
|
||||
.seek(SeekFrom::Start(data_offset))
|
||||
.with_context(|| format!("Failed to seek image: {name}"))?;
|
||||
|
||||
stream::copy_n(input_file, &mut payload_writer, data_length, cancel_signal)
|
||||
.with_context(|| format!("Failed to copy from replacement image: {name}"))?;
|
||||
}
|
||||
|
||||
let (_, header, properties, _) = payload_writer
|
||||
.finish()
|
||||
.context("Failed to finalize payload")?;
|
||||
|
||||
// Display the header information now that it has been finalized.
|
||||
display_header(payload_cli, &header);
|
||||
|
||||
// Optionally, write payload_properties.txt.
|
||||
if let Some(path) = &cli.output_properties {
|
||||
fs::write(path, properties)
|
||||
.with_context(|| format!("Failed to write payload properties: {path:?}"))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn repack_subcommand(
|
||||
payload_cli: &PayloadCli,
|
||||
cli: &RepackCli,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<()> {
|
||||
let signing_key = load_key(&cli.key)?;
|
||||
|
||||
let (mut reader, header) = open_reader(&cli.input, true)?;
|
||||
|
||||
info!("Generating new OTA payload");
|
||||
|
||||
let mut payload_writer = open_writer(&cli.output, header.clone(), signing_key)?;
|
||||
|
||||
while payload_writer
|
||||
.begin_next_operation()
|
||||
.context("Failed to begin next payload blob entry")?
|
||||
{
|
||||
let name = payload_writer.partition().unwrap().partition_name.clone();
|
||||
let operation = payload_writer.operation().unwrap();
|
||||
|
||||
let Some(data_length) = operation.data_length else {
|
||||
// Otherwise, this is a ZERO/DISCARD operation.
|
||||
continue;
|
||||
};
|
||||
|
||||
let pi = payload_writer.partition_index().unwrap();
|
||||
let oi = payload_writer.operation_index().unwrap();
|
||||
let orig_partition = &header.manifest.partitions[pi];
|
||||
let orig_operation = &orig_partition.operations[oi];
|
||||
let data_offset = orig_operation
|
||||
.data_offset
|
||||
.ok_or_else(|| anyhow!("Missing data_offset in partition #{pi} operation #{oi}"))?;
|
||||
|
||||
// Directly copy blobs from the original payload.
|
||||
let data_offset = data_offset
|
||||
.checked_add(header.blob_offset)
|
||||
.ok_or_else(|| anyhow!("data_offset overflow in partition #{pi} operation #{oi}"))?;
|
||||
|
||||
reader
|
||||
.seek(SeekFrom::Start(data_offset))
|
||||
.with_context(|| format!("Failed to seek original payload to {data_offset}"))?;
|
||||
|
||||
stream::copy_n(&mut reader, &mut payload_writer, data_length, cancel_signal)
|
||||
.with_context(|| format!("Failed to copy from original payload: {name}"))?;
|
||||
}
|
||||
|
||||
let (_, header, properties, _) = payload_writer
|
||||
.finish()
|
||||
.context("Failed to finalize payload")?;
|
||||
|
||||
// Display the header information now that it has been finalized.
|
||||
display_header(payload_cli, &header);
|
||||
|
||||
// Optionally, write payload_properties.txt.
|
||||
if let Some(path) = &cli.output_properties {
|
||||
fs::write(path, properties)
|
||||
.with_context(|| format!("Failed to write payload properties: {path:?}"))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn info_subcommand(payload_cli: &PayloadCli, cli: &InfoCli) -> Result<()> {
|
||||
let (_, header) = open_reader(&cli.input, true)?;
|
||||
|
||||
display_header(payload_cli, &header);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn payload_main(cli: &PayloadCli, cancel_signal: &AtomicBool) -> Result<()> {
|
||||
match &cli.command {
|
||||
PayloadCommand::Unpack(c) => unpack_subcommand(cli, c, cancel_signal),
|
||||
PayloadCommand::Pack(c) => pack_subcommand(cli, c, cancel_signal),
|
||||
PayloadCommand::Repack(c) => repack_subcommand(cli, c, cancel_signal),
|
||||
PayloadCommand::Info(c) => info_subcommand(cli, c),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
struct KeyGroup {
|
||||
/// Path to signing key.
|
||||
///
|
||||
/// This should normally be a private key. However, if --signing-helper is
|
||||
/// used, then it should be a public key instead.
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
key: PathBuf,
|
||||
|
||||
/// Environment variable containing private key passphrase.
|
||||
#[arg(long, value_name = "ENV_VAR", value_parser, group = "pass")]
|
||||
pass_env_var: Option<OsString>,
|
||||
|
||||
/// File containing private key passphrase.
|
||||
#[arg(long, value_name = "FILE", value_parser, group = "pass")]
|
||||
pass_file: Option<PathBuf>,
|
||||
|
||||
/// External program for signing.
|
||||
///
|
||||
/// If this option is specified, then --key must refer to a public key. The
|
||||
/// program will be invoked as:
|
||||
///
|
||||
/// <program> <algo> <public key> [file <pass file>|env <pass env>]
|
||||
#[arg(long, value_name = "PROGRAM", value_parser)]
|
||||
signing_helper: Option<PathBuf>,
|
||||
}
|
||||
|
||||
/// Unpack a payload binary.
|
||||
///
|
||||
/// Each partition is extracted to `<partition name>.img` in the output images
|
||||
/// directory. The payload header metadata is written to the info TOML file.
|
||||
///
|
||||
/// If any partition names are unsafe to use in a path, the extraction process
|
||||
/// will fail and exit. Extracted files are never written outside of the tree
|
||||
/// directory, even if an external process tries to interfere.
|
||||
#[derive(Debug, Parser)]
|
||||
struct UnpackCli {
|
||||
/// Path to input payload binary.
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
input: PathBuf,
|
||||
|
||||
/// Path to output info TOML.
|
||||
#[arg(
|
||||
long,
|
||||
value_name = "FILE",
|
||||
value_parser,
|
||||
default_value = "payload.toml"
|
||||
)]
|
||||
output_info: PathBuf,
|
||||
|
||||
/// Path to output images directory.
|
||||
#[arg(
|
||||
long,
|
||||
value_name = "DIR",
|
||||
value_parser,
|
||||
default_value = "payload_images"
|
||||
)]
|
||||
output_images: PathBuf,
|
||||
}
|
||||
|
||||
/// Pack a payload binary.
|
||||
///
|
||||
/// The new payload binary will *only* contain images listed in the info TOML
|
||||
/// file. Extra images in the input images directory that aren't listed will be
|
||||
/// silently ignored. Images are added to the payload in the order that they are
|
||||
/// listed in the info TOML file.
|
||||
#[derive(Debug, Parser)]
|
||||
struct PackCli {
|
||||
/// Path to output payload binary.
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
output: PathBuf,
|
||||
|
||||
/// Path to output payload properties file.
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
output_properties: Option<PathBuf>,
|
||||
|
||||
/// Path to input info TOML.
|
||||
#[arg(
|
||||
long,
|
||||
value_name = "FILE",
|
||||
value_parser,
|
||||
default_value = "payload.toml"
|
||||
)]
|
||||
input_info: PathBuf,
|
||||
|
||||
/// Path to input images directory.
|
||||
#[arg(
|
||||
long,
|
||||
value_name = "DIR",
|
||||
value_parser,
|
||||
default_value = "payload_images"
|
||||
)]
|
||||
input_images: PathBuf,
|
||||
|
||||
#[command(flatten)]
|
||||
key: KeyGroup,
|
||||
}
|
||||
|
||||
/// Repack a payload binary.
|
||||
///
|
||||
/// This command is equivalent to running `unpack` and `pack`, except without
|
||||
/// storing the unpacked data to disk nor recompressing the partition images.
|
||||
#[derive(Debug, Parser)]
|
||||
struct RepackCli {
|
||||
/// Path to input payload binary.
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
input: PathBuf,
|
||||
|
||||
/// Path to output payload binary.
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
output: PathBuf,
|
||||
|
||||
/// Path to output payload properties file.
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
output_properties: Option<PathBuf>,
|
||||
|
||||
#[command(flatten)]
|
||||
key: KeyGroup,
|
||||
}
|
||||
|
||||
/// Display payload information.
|
||||
#[derive(Debug, Parser)]
|
||||
struct InfoCli {
|
||||
/// Path to input payload file.
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
input: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
enum PayloadCommand {
|
||||
Unpack(UnpackCli),
|
||||
Pack(PackCli),
|
||||
Repack(RepackCli),
|
||||
Info(InfoCli),
|
||||
}
|
||||
|
||||
/// Pack, unpack, and inspect OTA payloads.
|
||||
#[derive(Debug, Parser)]
|
||||
pub struct PayloadCli {
|
||||
#[command(subcommand)]
|
||||
command: PayloadCommand,
|
||||
|
||||
/// Don't print payload header information.
|
||||
#[arg(short, long, global = true)]
|
||||
quiet: bool,
|
||||
}
|
||||
@@ -0,0 +1,632 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024 Andrew Gunnerson
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
use std::{
|
||||
fmt,
|
||||
fs::{File, OpenOptions},
|
||||
io::{Read, Seek, SeekFrom, Write},
|
||||
ops::Range,
|
||||
path::{Path, PathBuf},
|
||||
sync::atomic::AtomicBool,
|
||||
};
|
||||
|
||||
use anyhow::{anyhow, bail, Context, Result};
|
||||
use clap::{Parser, Subcommand};
|
||||
use crc32fast::Hasher;
|
||||
use zerocopy::{little_endian, AsBytes};
|
||||
|
||||
use crate::{
|
||||
format::{
|
||||
padding,
|
||||
sparse::{
|
||||
self, Chunk, ChunkBounds, ChunkData, ChunkList, CrcMode, Header, SparseReader,
|
||||
SparseWriter,
|
||||
},
|
||||
},
|
||||
stream,
|
||||
};
|
||||
|
||||
struct CompactView<'a, T>(&'a [T]);
|
||||
|
||||
impl<'a, T: fmt::Debug> fmt::Debug for CompactView<'a, T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let mut list = f.debug_list();
|
||||
|
||||
for item in self.0 {
|
||||
// No alternate mode for no inner newlines.
|
||||
list.entry(&format_args!("{item:?}"));
|
||||
}
|
||||
|
||||
list.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Metadata {
|
||||
header: Header,
|
||||
chunks: Vec<Chunk>,
|
||||
}
|
||||
|
||||
impl fmt::Debug for Metadata {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("Metadata")
|
||||
.field("header", &self.header)
|
||||
.field("chunks", &CompactView(&self.chunks))
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
fn open_reader(path: &Path) -> Result<File> {
|
||||
File::open(path).with_context(|| format!("Failed to open for reading: {path:?}"))
|
||||
}
|
||||
|
||||
fn open_writer(path: &Path, truncate: bool) -> Result<File> {
|
||||
OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(truncate)
|
||||
.open(path)
|
||||
.with_context(|| format!("Failed to open for writing: {path:?}"))
|
||||
}
|
||||
|
||||
fn display_metadata(cli: &SparseCli, metadata: &Metadata) {
|
||||
if !cli.quiet {
|
||||
println!("{metadata:#?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Splits large data chunks to ensure that none exceed 64 MiB. This is not
|
||||
/// necessary in most cases, but is kept to match the behavior of AOSP's
|
||||
/// libsparse.
|
||||
fn split_chunks(chunks: &[Chunk], block_size: u32) -> Vec<Chunk> {
|
||||
const MAX_BYTES: u32 = 64 * 1024 * 1024;
|
||||
|
||||
let max_blocks_per_chunk = MAX_BYTES / block_size;
|
||||
let mut result = vec![];
|
||||
|
||||
for mut chunk in chunks.iter().copied() {
|
||||
if chunk.data == ChunkData::Data {
|
||||
while chunk.bounds.len() > max_blocks_per_chunk {
|
||||
result.push(Chunk {
|
||||
bounds: ChunkBounds {
|
||||
start: chunk.bounds.start,
|
||||
end: chunk.bounds.start + max_blocks_per_chunk,
|
||||
},
|
||||
data: chunk.data,
|
||||
});
|
||||
|
||||
chunk.bounds.start += max_blocks_per_chunk;
|
||||
}
|
||||
}
|
||||
|
||||
result.push(chunk);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// [Linux only] Find allocated regions of the file. This avoids needing to read
|
||||
/// unused portions of the file if it is a native sparse file.
|
||||
#[cfg(any(target_os = "linux", target_os = "android"))]
|
||||
fn find_allocated_regions(
|
||||
path: &Path,
|
||||
reader: &mut File,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<Vec<Range<u64>>> {
|
||||
use rustix::{fs::SeekFrom, io::Errno};
|
||||
|
||||
let mut result = vec![];
|
||||
let mut start;
|
||||
let mut end = 0;
|
||||
|
||||
loop {
|
||||
stream::check_cancel(cancel_signal)?;
|
||||
|
||||
start = match rustix::fs::seek(&*reader, SeekFrom::Data(end as i64)) {
|
||||
Ok(offset) => offset,
|
||||
Err(e) if e == Errno::NXIO => break,
|
||||
Err(e) => return Err(e).with_context(|| format!("Failed to seek to data: {path:?}")),
|
||||
};
|
||||
|
||||
end = rustix::fs::seek(&*reader, SeekFrom::Hole(start as i64))
|
||||
.with_context(|| format!("Failed to seek to hole: {path:?}"))?;
|
||||
|
||||
result.push(start..end);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Compute chunk boundaries for the list of potentially overlapping file byte
|
||||
/// regions. If `exact_bounds` is true, then the regions must be block-aligned.
|
||||
/// Otherwise, the lower boundaries are aligned down and the upper boundaries
|
||||
/// are aligned up.
|
||||
fn get_chunks_for_regions(
|
||||
block_size: u32,
|
||||
file_size: u64,
|
||||
file_regions: &[Range<u64>],
|
||||
exact_bounds: bool,
|
||||
) -> Result<(u32, Vec<ChunkBounds>)> {
|
||||
let block_size_64 = u64::from(block_size);
|
||||
|
||||
let file_blocks: u32 = (file_size / u64::from(block_size))
|
||||
.try_into()
|
||||
.map_err(|_| anyhow!("File size {file_size} too large for block size {block_size}"))?;
|
||||
|
||||
let mut chunk_list = ChunkList::new();
|
||||
chunk_list.set_len(file_blocks);
|
||||
|
||||
for region in file_regions {
|
||||
let mut start_byte = region.start;
|
||||
let mut end_byte = region.end;
|
||||
|
||||
if exact_bounds {
|
||||
if start_byte % block_size_64 != 0 || end_byte % block_size_64 != 0 {
|
||||
bail!("File region bounds are not block-aligned: {region:?}");
|
||||
}
|
||||
} else {
|
||||
start_byte = start_byte / block_size_64 * block_size_64;
|
||||
end_byte = padding::round(end_byte, block_size_64).unwrap();
|
||||
}
|
||||
|
||||
let start_block: u32 = (start_byte / block_size_64).try_into().map_err(|_| {
|
||||
anyhow!("Region start offset {start_byte} too large for block size {block_size}")
|
||||
})?;
|
||||
let end_block: u32 = (end_byte / block_size_64).try_into().map_err(|_| {
|
||||
anyhow!("Region end offset {end_byte} too large for block size {block_size}")
|
||||
})?;
|
||||
|
||||
chunk_list.insert_data(ChunkBounds {
|
||||
start: start_block,
|
||||
end: end_block,
|
||||
});
|
||||
}
|
||||
|
||||
let chunks = chunk_list.iter_allocated().map(|c| c.bounds).collect();
|
||||
|
||||
Ok((file_blocks, chunks))
|
||||
}
|
||||
|
||||
/// Compute the sparse [`Chunk`]s needed to cover the specified regions.
|
||||
fn compute_chunks(
|
||||
path: &Path,
|
||||
reader: &mut File,
|
||||
block_size: u32,
|
||||
file_blocks: u32,
|
||||
block_regions: &[ChunkBounds],
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<(ChunkList, u32)> {
|
||||
let mut chunk_list = ChunkList::new();
|
||||
let mut hasher = Some(Hasher::new());
|
||||
let mut buf = vec![0u8; block_size as usize];
|
||||
let mut block = 0;
|
||||
|
||||
chunk_list.set_len(file_blocks);
|
||||
|
||||
for bounds in block_regions {
|
||||
if bounds.start != block {
|
||||
// Not contiguous so we cannot compute the checksum.
|
||||
hasher = None;
|
||||
}
|
||||
|
||||
let offset = u64::from(bounds.start) * u64::from(block_size);
|
||||
|
||||
reader
|
||||
.seek(SeekFrom::Start(offset))
|
||||
.with_context(|| format!("Failed to seek file: {path:?}"))?;
|
||||
|
||||
for block in *bounds {
|
||||
stream::check_cancel(cancel_signal)?;
|
||||
|
||||
reader
|
||||
.read_exact(&mut buf)
|
||||
.with_context(|| format!("Failed to read full block: {path:?}"))?;
|
||||
|
||||
if let Some(h) = &mut hasher {
|
||||
h.update(&buf);
|
||||
}
|
||||
|
||||
let new_bounds = ChunkBounds {
|
||||
start: block,
|
||||
end: block + 1,
|
||||
};
|
||||
|
||||
if buf.chunks_exact(4).all(|c| c == &buf[..4]) {
|
||||
let fill_value = u32::from_le_bytes(buf[..4].try_into().unwrap());
|
||||
chunk_list.insert_fill(new_bounds, fill_value);
|
||||
} else {
|
||||
chunk_list.insert_data(new_bounds);
|
||||
}
|
||||
}
|
||||
|
||||
block = bounds.end;
|
||||
}
|
||||
|
||||
if block != file_blocks {
|
||||
hasher = None;
|
||||
}
|
||||
|
||||
let crc32 = hasher.map(|h| h.finalize()).unwrap_or_default();
|
||||
|
||||
Ok((chunk_list, crc32))
|
||||
}
|
||||
|
||||
fn unpack_subcommand(
|
||||
sparse_cli: &SparseCli,
|
||||
cli: &UnpackCli,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<()> {
|
||||
let reader = open_reader(&cli.input)?;
|
||||
let mut sparse_reader = SparseReader::new(reader, CrcMode::Validate)
|
||||
.with_context(|| format!("Failed to read sparse file: {:?}", cli.input))?;
|
||||
|
||||
let mut metadata = Metadata {
|
||||
header: sparse_reader.header(),
|
||||
chunks: vec![],
|
||||
};
|
||||
|
||||
let mut writer = open_writer(&cli.output, !cli.preserve)?;
|
||||
|
||||
if cli.preserve {
|
||||
let expected_size =
|
||||
u64::from(metadata.header.num_blocks) * u64::from(metadata.header.block_size);
|
||||
let file_size = writer
|
||||
.seek(SeekFrom::End(0))
|
||||
.with_context(|| format!("Failed to get file size: {:?}", cli.output))?;
|
||||
|
||||
if file_size < expected_size {
|
||||
writer
|
||||
.set_len(expected_size)
|
||||
.with_context(|| format!("Failed to set file size: {:?}", cli.output))?;
|
||||
}
|
||||
|
||||
writer
|
||||
.seek(SeekFrom::Start(0))
|
||||
.with_context(|| format!("Failed to seek file: {:?}", cli.output))?;
|
||||
}
|
||||
|
||||
while let Some(chunk) = sparse_reader
|
||||
.next_chunk()
|
||||
.with_context(|| format!("Failed to read chunk: {:?}", cli.input))?
|
||||
{
|
||||
match chunk.data {
|
||||
ChunkData::Fill(value) => {
|
||||
let fill_value = little_endian::U32::from(value);
|
||||
let buf = vec![fill_value; metadata.header.block_size as usize / 4];
|
||||
|
||||
for _ in chunk.bounds {
|
||||
stream::check_cancel(cancel_signal)?;
|
||||
|
||||
writer
|
||||
.write_all(buf.as_bytes())
|
||||
.with_context(|| format!("Failed to write data: {:?}", cli.output))?;
|
||||
}
|
||||
}
|
||||
ChunkData::Data => {
|
||||
// This cannot overflow.
|
||||
let to_copy = chunk.bounds.len() * metadata.header.block_size;
|
||||
|
||||
stream::copy_n(
|
||||
&mut sparse_reader,
|
||||
&mut writer,
|
||||
to_copy.into(),
|
||||
cancel_signal,
|
||||
)
|
||||
.with_context(|| {
|
||||
format!("Failed to copy data: {:?} -> {:?}", cli.input, cli.output)
|
||||
})?;
|
||||
}
|
||||
ChunkData::Hole => {
|
||||
// This cannot overflow.
|
||||
let to_skip = chunk.bounds.len() * metadata.header.block_size;
|
||||
|
||||
writer
|
||||
.seek(SeekFrom::Current(to_skip.into()))
|
||||
.with_context(|| format!("Failed to seek file: {:?}", cli.output))?;
|
||||
}
|
||||
ChunkData::Crc32(_) => {}
|
||||
}
|
||||
|
||||
metadata.chunks.push(chunk);
|
||||
}
|
||||
|
||||
display_metadata(sparse_cli, &metadata);
|
||||
|
||||
sparse_reader
|
||||
.finish()
|
||||
.with_context(|| format!("Failed to finalize reader: {:?}", cli.input))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pack_subcommand(
|
||||
sparse_cli: &SparseCli,
|
||||
cli: &PackCli,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<()> {
|
||||
if cli.block_size == 0 || cli.block_size % 4 != 0 {
|
||||
bail!(
|
||||
"Block size must be a non-zero multiple of 4: {}",
|
||||
cli.block_size,
|
||||
);
|
||||
}
|
||||
|
||||
let mut reader = open_reader(&cli.input)?;
|
||||
|
||||
let file_size = reader
|
||||
.seek(SeekFrom::End(0))
|
||||
.with_context(|| format!("Failed to get file size: {:?}", cli.input))?;
|
||||
if file_size % u64::from(cli.block_size) != 0 {
|
||||
bail!(
|
||||
"File size {file_size} is not a multiple of block size {}",
|
||||
cli.block_size,
|
||||
);
|
||||
}
|
||||
|
||||
// Compute the byte regions to pack into the sparse file.
|
||||
let (file_regions, exact_bounds) = if !cli.region.is_empty() {
|
||||
let regions = cli
|
||||
.region
|
||||
.chunks_exact(2)
|
||||
.map(|c| c[0]..c[1])
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
(regions, false)
|
||||
} else {
|
||||
#[cfg(any(target_os = "linux", target_os = "android"))]
|
||||
{
|
||||
let regions = find_allocated_regions(&cli.input, &mut reader, cancel_signal)?;
|
||||
|
||||
(regions, false)
|
||||
}
|
||||
#[cfg(not(any(target_os = "linux", target_os = "android")))]
|
||||
{
|
||||
(vec![0..file_size], true)
|
||||
}
|
||||
};
|
||||
|
||||
// Get the file regions as non-overlapping and sorted block regions.
|
||||
let (file_blocks, block_regions) =
|
||||
get_chunks_for_regions(cli.block_size, file_size, &file_regions, exact_bounds)?;
|
||||
|
||||
// Compute the checksum (if possible) and the list of actual chunks.
|
||||
let (chunk_list, crc32) = compute_chunks(
|
||||
&cli.input,
|
||||
&mut reader,
|
||||
cli.block_size,
|
||||
file_blocks,
|
||||
&block_regions,
|
||||
cancel_signal,
|
||||
)?;
|
||||
|
||||
let chunks = split_chunks(&chunk_list.to_chunks(), cli.block_size);
|
||||
let metadata = Metadata {
|
||||
header: Header {
|
||||
major_version: sparse::MAJOR_VERSION,
|
||||
minor_version: sparse::MINOR_VERSION,
|
||||
block_size: cli.block_size,
|
||||
num_blocks: chunk_list.len(),
|
||||
// This can't overflow because the number of chunks is always
|
||||
// smaller than the number of blocks (because we don't add CRC32
|
||||
// chunks).
|
||||
num_chunks: chunks.len() as u32,
|
||||
// This will be zero if the regions don't span the entire file.
|
||||
crc32,
|
||||
},
|
||||
chunks,
|
||||
};
|
||||
|
||||
display_metadata(sparse_cli, &metadata);
|
||||
|
||||
let writer = open_writer(&cli.output, true)?;
|
||||
let mut sparse_writer = SparseWriter::new(writer, metadata.header)
|
||||
.with_context(|| format!("Failed to initialize sparse file: {:?}", cli.output))?;
|
||||
|
||||
for chunk in metadata.chunks {
|
||||
sparse_writer
|
||||
.start_chunk(chunk)
|
||||
.with_context(|| format!("Failed to start chunk: {:?}", cli.output))?;
|
||||
|
||||
if chunk.data == ChunkData::Data {
|
||||
let offset = u64::from(chunk.bounds.start) * u64::from(cli.block_size);
|
||||
|
||||
reader
|
||||
.seek(SeekFrom::Start(offset))
|
||||
.with_context(|| format!("Failed to seek file: {:?}", cli.input))?;
|
||||
|
||||
let to_copy = u64::from(chunk.bounds.len()) * u64::from(cli.block_size);
|
||||
|
||||
stream::copy_n(&mut reader, &mut sparse_writer, to_copy, cancel_signal).with_context(
|
||||
|| format!("Failed to copy data: {:?} -> {:?}", cli.input, cli.output),
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
sparse_writer
|
||||
.finish()
|
||||
.with_context(|| format!("Failed to finalize writer: {:?}", cli.output))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn repack_subcommand(
|
||||
sparse_cli: &SparseCli,
|
||||
cli: &RepackCli,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<()> {
|
||||
let reader = open_reader(&cli.input)?;
|
||||
let mut sparse_reader = SparseReader::new_seekable(reader, CrcMode::Validate)
|
||||
.with_context(|| format!("Failed to read sparse file: {:?}", cli.input))?;
|
||||
|
||||
let mut metadata = Metadata {
|
||||
header: sparse_reader.header(),
|
||||
chunks: vec![],
|
||||
};
|
||||
|
||||
let writer = open_writer(&cli.output, true)?;
|
||||
let mut sparse_writer = SparseWriter::new(writer, metadata.header)
|
||||
.with_context(|| format!("Failed to initialize sparse file: {:?}", cli.output))?;
|
||||
|
||||
while let Some(chunk) = sparse_reader
|
||||
.next_chunk()
|
||||
.with_context(|| format!("Failed to read chunk: {:?}", cli.input))?
|
||||
{
|
||||
sparse_writer
|
||||
.start_chunk(chunk)
|
||||
.with_context(|| format!("Failed to start chunk: {:?}", cli.output))?;
|
||||
|
||||
if chunk.data == ChunkData::Data {
|
||||
// This cannot overflow.
|
||||
let to_copy = chunk.bounds.len() * metadata.header.block_size;
|
||||
|
||||
stream::copy_n(
|
||||
&mut sparse_reader,
|
||||
&mut sparse_writer,
|
||||
to_copy.into(),
|
||||
cancel_signal,
|
||||
)
|
||||
.with_context(|| format!("Failed to copy data: {:?} -> {:?}", cli.input, cli.output))?;
|
||||
}
|
||||
|
||||
metadata.chunks.push(chunk);
|
||||
}
|
||||
|
||||
display_metadata(sparse_cli, &metadata);
|
||||
|
||||
sparse_reader
|
||||
.finish()
|
||||
.with_context(|| format!("Failed to finalize reader: {:?}", cli.input))?;
|
||||
sparse_writer
|
||||
.finish()
|
||||
.with_context(|| format!("Failed to finalize writer: {:?}", cli.output))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn info_subcommand(sparse_cli: &SparseCli, cli: &InfoCli) -> Result<()> {
|
||||
let reader = open_reader(&cli.input)?;
|
||||
let mut sparse_reader = SparseReader::new_seekable(reader, CrcMode::Ignore)
|
||||
.with_context(|| format!("Failed to read sparse file: {:?}", cli.input))?;
|
||||
|
||||
let mut metadata = Metadata {
|
||||
header: sparse_reader.header(),
|
||||
chunks: vec![],
|
||||
};
|
||||
|
||||
while let Some(chunk) = sparse_reader
|
||||
.next_chunk()
|
||||
.with_context(|| format!("Failed to read chunk: {:?}", cli.input))?
|
||||
{
|
||||
metadata.chunks.push(chunk);
|
||||
}
|
||||
|
||||
display_metadata(sparse_cli, &metadata);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn sparse_main(cli: &SparseCli, cancel_signal: &AtomicBool) -> Result<()> {
|
||||
match &cli.command {
|
||||
SparseCommand::Unpack(c) => unpack_subcommand(cli, c, cancel_signal),
|
||||
SparseCommand::Pack(c) => pack_subcommand(cli, c, cancel_signal),
|
||||
SparseCommand::Repack(c) => repack_subcommand(cli, c, cancel_signal),
|
||||
SparseCommand::Info(c) => info_subcommand(cli, c),
|
||||
}
|
||||
}
|
||||
|
||||
/// Unpack a sparse image.
|
||||
#[derive(Debug, Parser)]
|
||||
struct UnpackCli {
|
||||
/// Path to input sparse image.
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
input: PathBuf,
|
||||
|
||||
/// Path to output raw image.
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
output: PathBuf,
|
||||
|
||||
/// Preserve existing data in the output file.
|
||||
///
|
||||
/// This is useful when unpacking multiple sparse files into a single output
|
||||
/// file because they contain disjoint blocks of data.
|
||||
#[arg(long)]
|
||||
preserve: bool,
|
||||
}
|
||||
|
||||
/// Pack a sparse image.
|
||||
#[derive(Debug, Parser)]
|
||||
struct PackCli {
|
||||
/// Path to output sparse image.
|
||||
///
|
||||
/// If `--region` is not used and the input file is not a (native) sparse
|
||||
/// file on Linux, then the output sparse image is written with a CRC32
|
||||
/// checksum in the header.
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
output: PathBuf,
|
||||
|
||||
/// Path to input raw image.
|
||||
///
|
||||
/// On Linux, if this is a (native) sparse file, then the unallocated
|
||||
/// sections of the file will be skipped and will be stored in the output
|
||||
/// file as hole chunks.
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
input: PathBuf,
|
||||
|
||||
/// Block size.
|
||||
#[arg(short, long, value_name = "BYTES", default_value_t = 4096)]
|
||||
block_size: u32,
|
||||
|
||||
/// Pack certain byte regions from the file.
|
||||
///
|
||||
/// The start offset will be aligned down to the block size and the end
|
||||
/// offset will be aligned up. This option can be specified any number of
|
||||
/// times and in any order. Overlapping regions are allowed.
|
||||
///
|
||||
/// Unused regions will be stored in the sparse file as hole chunks.
|
||||
#[arg(short, long, value_names = ["START", "END"], num_args = 2)]
|
||||
region: Vec<u64>,
|
||||
}
|
||||
|
||||
/// Repack a sparse image.
|
||||
///
|
||||
/// This command is equivalent to running `unpack` and `pack`, except without
|
||||
/// storing the unpacked data to disk.
|
||||
#[derive(Debug, Parser)]
|
||||
struct RepackCli {
|
||||
/// Path to input sparse image.
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
input: PathBuf,
|
||||
|
||||
/// Path to output sparse image.
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
output: PathBuf,
|
||||
}
|
||||
|
||||
/// Display sparse image metadata.
|
||||
#[derive(Debug, Parser)]
|
||||
struct InfoCli {
|
||||
/// Path to input sparse image.
|
||||
#[arg(short, long, value_name = "FILE", value_parser)]
|
||||
input: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
enum SparseCommand {
|
||||
Unpack(UnpackCli),
|
||||
Pack(PackCli),
|
||||
Repack(RepackCli),
|
||||
Info(InfoCli),
|
||||
}
|
||||
|
||||
/// Pack, unpack, and inspect sparse images.
|
||||
#[derive(Debug, Parser)]
|
||||
pub struct SparseCli {
|
||||
#[command(subcommand)]
|
||||
command: SparseCommand,
|
||||
|
||||
/// Don't print sparse image metadata.
|
||||
#[arg(short, long, global = true)]
|
||||
quiet: bool,
|
||||
}
|
||||
+279
-11
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Andrew Gunnerson
|
||||
* SPDX-FileCopyrightText: 2023-2024 Andrew Gunnerson
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
@@ -9,6 +9,7 @@ use std::{
|
||||
fs::{self, File, OpenOptions},
|
||||
io::{self, BufReader, BufWriter, Read, Write},
|
||||
path::{Path, PathBuf},
|
||||
process::{Command, ExitStatus, Stdio},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
@@ -20,14 +21,19 @@ use cms::{
|
||||
SignedData, SignerIdentifier, SignerInfo, SignerInfos,
|
||||
},
|
||||
};
|
||||
use passterm::PromptError;
|
||||
use pkcs8::{
|
||||
pkcs5::{pbes2, scrypt},
|
||||
DecodePrivateKey, EncodePrivateKey, EncodePublicKey, EncryptedPrivateKeyInfo, LineEnding,
|
||||
PrivateKeyInfo,
|
||||
DecodePrivateKey, DecodePublicKey, EncodePrivateKey, EncodePublicKey, EncryptedPrivateKeyInfo,
|
||||
LineEnding, PrivateKeyInfo,
|
||||
};
|
||||
use rand::RngCore;
|
||||
use rsa::{pkcs1v15::SigningKey, Pkcs1v15Sign, RsaPrivateKey, RsaPublicKey};
|
||||
use sha2::Sha256;
|
||||
use rsa::{
|
||||
pkcs1v15::SigningKey, traits::PublicKeyParts, Pkcs1v15Sign, RsaPrivateKey, RsaPublicKey,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha1::Sha1;
|
||||
use sha2::{Digest, Sha256, Sha512};
|
||||
use thiserror::Error;
|
||||
use x509_cert::{
|
||||
builder::{Builder, CertificateBuilder, Profile},
|
||||
@@ -40,6 +46,24 @@ use x509_cert::{
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Error {
|
||||
#[error("Signature algorithm not supported: {0:?}")]
|
||||
UnsupportedAlgorithm(SignatureAlgorithm),
|
||||
#[error("RSA key size ({}) not supported", .0 * 8)]
|
||||
UnsupportedKey(usize),
|
||||
#[error("Invalid digest length ({0} bytes) for {1:?}")]
|
||||
InvalidDigestLength(usize, SignatureAlgorithm),
|
||||
#[error("Invalid signature length ({0} bytes) for {1:?}")]
|
||||
InvalidSignatureLength(usize, SignatureAlgorithm),
|
||||
#[error("Failed to run command: {0}")]
|
||||
CommandSpawnFailed(String, #[source] io::Error),
|
||||
#[error("Command failed with status: {1}: {0}")]
|
||||
CommandExecutionFailed(String, ExitStatus),
|
||||
#[error("Signature from signing helper does not match public key: {0:?}")]
|
||||
SigningHelperBadSignature(PathBuf),
|
||||
#[error("Passphrase prompt requires an interactive terminal")]
|
||||
NotInteractive(#[source] io::Error),
|
||||
#[error("Failed to prompt for passphrase")]
|
||||
PassphrasePrompt(#[source] PromptError),
|
||||
#[error("Passphrases do not match")]
|
||||
ConfirmPassphrase,
|
||||
#[error("Failed to read environment variable: {0:?}")]
|
||||
@@ -54,6 +78,10 @@ pub enum Error {
|
||||
SaveKeyEncrypted(#[source] pkcs8::Error),
|
||||
#[error("Failed to save unencrypted private key")]
|
||||
SaveKeyUnencrypted(#[source] pkcs8::Error),
|
||||
#[error("Failed to RSA sign digest")]
|
||||
RsaSign(#[source] rsa::Error),
|
||||
#[error("Failed to RSA verify signature")]
|
||||
RsaVerify(#[source] rsa::Error),
|
||||
#[error("X509 error")]
|
||||
X509(#[from] x509_cert::builder::Error),
|
||||
#[error("SPKI error")]
|
||||
@@ -68,6 +96,34 @@ pub enum Error {
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
|
||||
pub enum SignatureAlgorithm {
|
||||
Sha1WithRsa,
|
||||
Sha256WithRsa,
|
||||
Sha512WithRsa,
|
||||
}
|
||||
|
||||
impl SignatureAlgorithm {
|
||||
/// Length of digest required by the signing algorithm.
|
||||
pub fn digest_len(self) -> usize {
|
||||
match self {
|
||||
Self::Sha1WithRsa => Sha1::output_size(),
|
||||
Self::Sha256WithRsa => Sha256::output_size(),
|
||||
Self::Sha512WithRsa => Sha512::output_size(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Compute the digest of the specified data.
|
||||
pub fn hash(self, data: &[u8]) -> Vec<u8> {
|
||||
match self {
|
||||
Self::Sha1WithRsa => Sha1::digest(data).to_vec(),
|
||||
Self::Sha256WithRsa => Sha256::digest(data).to_vec(),
|
||||
Self::Sha512WithRsa => Sha512::digest(data).to_vec(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum PassphraseSource {
|
||||
Prompt(String),
|
||||
EnvVar(OsString),
|
||||
@@ -85,13 +141,33 @@ impl PassphraseSource {
|
||||
}
|
||||
}
|
||||
|
||||
fn prompt(prompt: &str) -> Result<String> {
|
||||
match passterm::prompt_password_tty(Some(prompt)) {
|
||||
Ok(p) => Ok(p),
|
||||
Err(e) => {
|
||||
#[cfg(unix)]
|
||||
if let PromptError::IOError(io_e) = e {
|
||||
if let Some(errno) = io_e.raw_os_error() {
|
||||
if errno == libc::ENXIO || errno == libc::ENOTTY {
|
||||
return Err(Error::NotInteractive(io_e));
|
||||
}
|
||||
}
|
||||
|
||||
return Err(Error::PassphrasePrompt(PromptError::IOError(io_e)));
|
||||
}
|
||||
|
||||
Err(Error::PassphrasePrompt(e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn acquire(&self, confirm: bool) -> Result<String> {
|
||||
let passphrase = match self {
|
||||
Self::Prompt(p) => {
|
||||
let first = rpassword::prompt_password(p)?;
|
||||
let first = Self::prompt(p)?;
|
||||
|
||||
if confirm {
|
||||
let second = rpassword::prompt_password("Confirm: ")?;
|
||||
let second = Self::prompt("Confirm: ")?;
|
||||
|
||||
if first != second {
|
||||
return Err(Error::ConfirmPassphrase);
|
||||
@@ -110,6 +186,181 @@ impl PassphraseSource {
|
||||
}
|
||||
}
|
||||
|
||||
fn check_key_size(size: usize) -> Result<()> {
|
||||
// RustCrypto does not support 8192-bit keys.
|
||||
if size > 4096 / 8 {
|
||||
return Err(Error::UnsupportedKey(size));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Copied from rsa-0.9.6 since the function is not exported.
|
||||
fn pkcs1v15_sign_pad(prefix: &[u8], hashed: &[u8], k: usize) -> rsa::Result<Vec<u8>> {
|
||||
let hash_len = hashed.len();
|
||||
let t_len = prefix.len() + hashed.len();
|
||||
if k < t_len + 11 {
|
||||
return Err(rsa::Error::MessageTooLong);
|
||||
}
|
||||
|
||||
// EM = 0x00 || 0x01 || PS || 0x00 || T
|
||||
let mut em = vec![0xff; k];
|
||||
em[0] = 0;
|
||||
em[1] = 1;
|
||||
em[k - t_len - 1] = 0;
|
||||
em[k - t_len..k - hash_len].copy_from_slice(prefix);
|
||||
em[k - hash_len..k].copy_from_slice(hashed);
|
||||
|
||||
Ok(em)
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum RsaSigningKey {
|
||||
Internal(RsaPrivateKey),
|
||||
External {
|
||||
program: PathBuf,
|
||||
public_key_file: PathBuf,
|
||||
public_key: RsaPublicKey,
|
||||
passphrase_source: PassphraseSource,
|
||||
},
|
||||
}
|
||||
|
||||
impl RsaSigningKey {
|
||||
/// Size of key in bytes.
|
||||
pub fn size(&self) -> usize {
|
||||
match self {
|
||||
Self::Internal(key) => key.size(),
|
||||
Self::External { public_key, .. } => public_key.size(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the public key portion of the signing key.
|
||||
pub fn to_public_key(&self) -> RsaPublicKey {
|
||||
match self {
|
||||
RsaSigningKey::Internal(key) => key.to_public_key(),
|
||||
RsaSigningKey::External { public_key, .. } => public_key.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Sign the digest with the specified signature algorithm.
|
||||
pub fn sign(&self, algo: SignatureAlgorithm, digest: &[u8]) -> Result<Vec<u8>> {
|
||||
if digest.len() != algo.digest_len() {
|
||||
return Err(Error::InvalidDigestLength(digest.len(), algo));
|
||||
}
|
||||
|
||||
check_key_size(self.size())?;
|
||||
|
||||
let scheme = match algo {
|
||||
// We don't support signing with insecure algorithms.
|
||||
SignatureAlgorithm::Sha1WithRsa => return Err(Error::UnsupportedAlgorithm(algo)),
|
||||
SignatureAlgorithm::Sha256WithRsa => Pkcs1v15Sign::new::<Sha256>(),
|
||||
SignatureAlgorithm::Sha512WithRsa => Pkcs1v15Sign::new::<Sha512>(),
|
||||
};
|
||||
|
||||
match self {
|
||||
Self::Internal(key) => key.sign(scheme, digest).map_err(Error::RsaSign),
|
||||
Self::External {
|
||||
program,
|
||||
public_key,
|
||||
public_key_file,
|
||||
passphrase_source,
|
||||
} => {
|
||||
let key_bits = public_key.size() * 8;
|
||||
let algo_str = match algo {
|
||||
SignatureAlgorithm::Sha1WithRsa => unreachable!(),
|
||||
SignatureAlgorithm::Sha256WithRsa => format!("SHA256_RSA{key_bits}"),
|
||||
SignatureAlgorithm::Sha512WithRsa => format!("SHA512_RSA{key_bits}"),
|
||||
};
|
||||
|
||||
let mut command = Command::new(program);
|
||||
command.arg(algo_str);
|
||||
command.arg(public_key_file);
|
||||
|
||||
match passphrase_source {
|
||||
PassphraseSource::Prompt(_) => {}
|
||||
PassphraseSource::EnvVar(v) => {
|
||||
command.arg("env");
|
||||
command.arg(v);
|
||||
}
|
||||
PassphraseSource::File(p) => {
|
||||
command.arg("file");
|
||||
command.arg(p);
|
||||
}
|
||||
}
|
||||
|
||||
command.stdin(Stdio::piped());
|
||||
command.stdout(Stdio::piped());
|
||||
command.stderr(Stdio::inherit());
|
||||
|
||||
let mut child = command
|
||||
.spawn()
|
||||
.map_err(|e| Error::CommandSpawnFailed(format!("{command:?}"), e))?;
|
||||
|
||||
// We don't bother with spawning a thread. The pipe capacity on
|
||||
// all major OSs is significantly larger than the digest, so we
|
||||
// don't risk deadlocking even if the process doesn't read from
|
||||
// stdin.
|
||||
//
|
||||
// Pipe capacities:
|
||||
// * Linux: 64 KiB
|
||||
// * macOS: 4 KiB, 16 KiB (usually), or 64 KiB
|
||||
// * Windows: 4 KiB
|
||||
|
||||
let padded_digest = pkcs1v15_sign_pad(&scheme.prefix, digest, public_key.size())?;
|
||||
child.stdin.as_mut().unwrap().write_all(&padded_digest)?;
|
||||
|
||||
let child = child.wait_with_output()?;
|
||||
|
||||
if !child.status.success() {
|
||||
return Err(Error::CommandExecutionFailed(
|
||||
format!("{command:?}"),
|
||||
child.status,
|
||||
));
|
||||
} else if child.stdout.len() != self.size() {
|
||||
return Err(Error::InvalidSignatureLength(child.stdout.len(), algo));
|
||||
}
|
||||
|
||||
// Check that the helper signed with the proper key.
|
||||
if let Err(e) = self.to_public_key().verify_sig(algo, digest, &child.stdout) {
|
||||
return match e {
|
||||
Error::RsaVerify(_) => {
|
||||
Err(Error::SigningHelperBadSignature(public_key_file.clone()))
|
||||
}
|
||||
e => Err(e),
|
||||
};
|
||||
}
|
||||
|
||||
Ok(child.stdout)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait RsaPublicKeyExt {
|
||||
fn verify_sig(&self, algo: SignatureAlgorithm, digest: &[u8], signature: &[u8]) -> Result<()>;
|
||||
}
|
||||
|
||||
impl RsaPublicKeyExt for RsaPublicKey {
|
||||
/// Verify the signature against the specified key.
|
||||
fn verify_sig(&self, algo: SignatureAlgorithm, digest: &[u8], signature: &[u8]) -> Result<()> {
|
||||
// Check this explicitly so we can provide a better error message.
|
||||
if digest.len() != algo.digest_len() {
|
||||
return Err(Error::InvalidDigestLength(digest.len(), algo));
|
||||
}
|
||||
|
||||
check_key_size(self.size())?;
|
||||
|
||||
let scheme = match algo {
|
||||
SignatureAlgorithm::Sha1WithRsa => Pkcs1v15Sign::new::<Sha1>(),
|
||||
SignatureAlgorithm::Sha256WithRsa => Pkcs1v15Sign::new::<Sha256>(),
|
||||
SignatureAlgorithm::Sha512WithRsa => Pkcs1v15Sign::new::<Sha512>(),
|
||||
};
|
||||
|
||||
self.verify(scheme, digest, signature)
|
||||
.map_err(Error::RsaVerify)
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate an 4096-bit RSA key pair.
|
||||
pub fn generate_rsa_key_pair() -> Result<RsaPrivateKey> {
|
||||
let mut rng = rand::thread_rng();
|
||||
@@ -228,6 +479,16 @@ pub fn write_pem_cert_file(path: &Path, cert: &Certificate) -> Result<()> {
|
||||
write_pem_cert(writer, cert)
|
||||
}
|
||||
|
||||
/// Read PEM-encoded PKCS8 public key from a reader.
|
||||
pub fn read_pem_public_key(mut reader: impl Read) -> Result<RsaPublicKey> {
|
||||
let mut data = String::new();
|
||||
reader.read_to_string(&mut data)?;
|
||||
|
||||
let key = RsaPublicKey::from_public_key_pem(&data)?;
|
||||
|
||||
Ok(key)
|
||||
}
|
||||
|
||||
/// Write PEM-encoded PKCS8 public key to a writer.
|
||||
pub fn write_pem_public_key(mut writer: impl Write, key: &RsaPublicKey) -> Result<()> {
|
||||
let data = key.to_public_key_pem(LineEnding::LF)?;
|
||||
@@ -237,6 +498,14 @@ pub fn write_pem_public_key(mut writer: impl Write, key: &RsaPublicKey) -> Resul
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Read PEM-encoded PKCS8 public key from a file.
|
||||
pub fn read_pem_public_key_file(path: &Path) -> Result<RsaPublicKey> {
|
||||
let file = File::open(path)?;
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
read_pem_public_key(reader)
|
||||
}
|
||||
|
||||
/// Write PEM-encoded PKCS8 public key to a file.
|
||||
pub fn write_pem_public_key_file(path: &Path, key: &RsaPublicKey) -> Result<()> {
|
||||
let file = File::create(path)?;
|
||||
@@ -351,7 +620,7 @@ pub fn get_public_key(cert: &Certificate) -> Result<RsaPublicKey> {
|
||||
}
|
||||
|
||||
/// Check if a certificate matches a private key.
|
||||
pub fn cert_matches_key(cert: &Certificate, key: &RsaPrivateKey) -> Result<bool> {
|
||||
pub fn cert_matches_key(cert: &Certificate, key: &RsaSigningKey) -> Result<bool> {
|
||||
let public_key = get_public_key(cert)?;
|
||||
|
||||
Ok(key.to_public_key() == public_key)
|
||||
@@ -389,12 +658,11 @@ pub fn get_cms_certs(sd: &SignedData) -> Vec<Certificate> {
|
||||
/// a transport mechanism for a raw signature. Thus, we need to ensure that the
|
||||
/// signature covers nothing but the raw data.
|
||||
pub fn cms_sign_external(
|
||||
key: &RsaPrivateKey,
|
||||
key: &RsaSigningKey,
|
||||
cert: &Certificate,
|
||||
digest: &[u8],
|
||||
) -> Result<ContentInfo> {
|
||||
let scheme = Pkcs1v15Sign::new::<Sha256>();
|
||||
let signature = key.sign(scheme, digest)?;
|
||||
let signature = key.sign(SignatureAlgorithm::Sha256WithRsa, digest)?;
|
||||
|
||||
let digest_algorithm = AlgorithmIdentifierOwned {
|
||||
oid: const_oid::db::rfc5912::ID_SHA_256,
|
||||
|
||||
+56
-81
@@ -16,12 +16,12 @@ use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
||||
use num_bigint_dig::{ModInverse, ToBigInt};
|
||||
use num_traits::{Pow, ToPrimitive};
|
||||
use ring::digest::{Algorithm, Context};
|
||||
use rsa::{traits::PublicKeyParts, BigUint, Pkcs1v15Sign, RsaPrivateKey, RsaPublicKey};
|
||||
use rsa::{traits::PublicKeyParts, BigUint, RsaPublicKey};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256, Sha512};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{
|
||||
crypto::{self, RsaPublicKeyExt, RsaSigningKey, SignatureAlgorithm},
|
||||
escape,
|
||||
format::{
|
||||
fec::{self, Fec},
|
||||
@@ -103,12 +103,9 @@ pub enum Error {
|
||||
UnsupportedAlgorithm(AlgorithmType),
|
||||
#[error("Hashing algorithm not supported: {0:?}")]
|
||||
UnsupportedHashAlgorithm(String),
|
||||
#[error("Incorrect key size ({key_size} bytes) for algorithm {algo:?} ({} bytes)", algo.public_key_len())]
|
||||
IncorrectKeySize {
|
||||
key_size: usize,
|
||||
algo: AlgorithmType,
|
||||
},
|
||||
#[error("RSA key size (0) is not compatible with any AVB signing algorithm")]
|
||||
#[error("Incorrect key size ({}) for algorithm {1:?}", .0 * 8)]
|
||||
IncorrectKeySize(usize, AlgorithmType),
|
||||
#[error("RSA key size ({}) is not compatible with any AVB signing algorithm", .0 * 8)]
|
||||
UnsupportedKey(usize),
|
||||
#[error("Hash tree does not immediately follow image data")]
|
||||
HashTreeGap,
|
||||
@@ -120,18 +117,18 @@ pub enum Error {
|
||||
MismatchedFecBlockSizes { data: u32, hash: u32 },
|
||||
#[error("Must have exactly one hash or hash tree descriptor")]
|
||||
NoAppendedDescriptor,
|
||||
#[error("Failed to RSA sign digest")]
|
||||
RsaSign(#[source] rsa::Error),
|
||||
#[error("Failed to RSA verify signature")]
|
||||
RsaVerify(#[source] rsa::Error),
|
||||
#[error("{0} byte image size is too small to fit header")]
|
||||
TooSmallForHeader(u64),
|
||||
#[error("{0} byte image size is too small to fit footer")]
|
||||
TooSmallForFooter(u64),
|
||||
#[error("Crypto error")]
|
||||
Crypto(#[from] crypto::Error),
|
||||
#[error("Hash tree error")]
|
||||
HashTree(#[from] hashtree::Error),
|
||||
#[error("FEC error")]
|
||||
Fec(#[from] fec::Error),
|
||||
#[error("RSA error")]
|
||||
Rsa(#[from] rsa::Error),
|
||||
#[error("I/O error")]
|
||||
Io(#[from] io::Error),
|
||||
}
|
||||
@@ -156,6 +153,7 @@ pub enum AlgorithmType {
|
||||
Sha512Rsa2048,
|
||||
Sha512Rsa4096,
|
||||
Sha512Rsa8192,
|
||||
#[serde(untagged)]
|
||||
Unknown(u32),
|
||||
}
|
||||
|
||||
@@ -186,18 +184,24 @@ impl AlgorithmType {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hash_len(self) -> usize {
|
||||
pub fn to_digest_algorithm(self) -> Option<SignatureAlgorithm> {
|
||||
match self {
|
||||
Self::None | Self::Unknown(_) => 0,
|
||||
Self::Sha256Rsa2048 | Self::Sha256Rsa4096 | Self::Sha256Rsa8192 => {
|
||||
Sha256::output_size()
|
||||
Some(SignatureAlgorithm::Sha256WithRsa)
|
||||
}
|
||||
Self::Sha512Rsa2048 | Self::Sha512Rsa4096 | Self::Sha512Rsa8192 => {
|
||||
Sha512::output_size()
|
||||
Some(SignatureAlgorithm::Sha512WithRsa)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn digest_len(self) -> usize {
|
||||
self.to_digest_algorithm()
|
||||
.map(|a| a.digest_len())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn signature_len(self) -> usize {
|
||||
match self {
|
||||
Self::None | Self::Unknown(_) => 0,
|
||||
@@ -217,49 +221,36 @@ impl AlgorithmType {
|
||||
}
|
||||
|
||||
pub fn hash(self, data: &[u8]) -> Vec<u8> {
|
||||
match self {
|
||||
Self::None | Self::Unknown(_) => vec![],
|
||||
Self::Sha256Rsa2048 | Self::Sha256Rsa4096 | Self::Sha256Rsa8192 => {
|
||||
Sha256::digest(data).to_vec()
|
||||
}
|
||||
Self::Sha512Rsa2048 | Self::Sha512Rsa4096 | Self::Sha512Rsa8192 => {
|
||||
Sha512::digest(data).to_vec()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sign(self, key: &RsaPrivateKey, digest: &[u8]) -> Result<Vec<u8>> {
|
||||
let signature = match self {
|
||||
Self::None | Self::Unknown(_) => vec![],
|
||||
Self::Sha256Rsa2048 | Self::Sha256Rsa4096 | Self::Sha256Rsa8192 => {
|
||||
let scheme = Pkcs1v15Sign::new::<Sha256>();
|
||||
key.sign(scheme, digest).map_err(Error::RsaSign)?
|
||||
}
|
||||
Self::Sha512Rsa2048 | Self::Sha512Rsa4096 | Self::Sha512Rsa8192 => {
|
||||
let scheme = Pkcs1v15Sign::new::<Sha512>();
|
||||
key.sign(scheme, digest).map_err(Error::RsaSign)?
|
||||
}
|
||||
let Some(algo) = self.to_digest_algorithm() else {
|
||||
return vec![];
|
||||
};
|
||||
|
||||
Ok(signature)
|
||||
algo.hash(data)
|
||||
}
|
||||
|
||||
pub fn sign(self, key: &RsaSigningKey, digest: &[u8]) -> Result<Vec<u8>> {
|
||||
let Some(algo) = self.to_digest_algorithm() else {
|
||||
return if self == Self::None {
|
||||
Ok(vec![])
|
||||
} else {
|
||||
Err(Error::UnsupportedAlgorithm(self))
|
||||
};
|
||||
};
|
||||
|
||||
key.sign(algo, digest).map_err(|e| e.into())
|
||||
}
|
||||
|
||||
pub fn verify(self, key: &RsaPublicKey, digest: &[u8], signature: &[u8]) -> Result<()> {
|
||||
match self {
|
||||
Self::None | Self::Unknown(_) => {}
|
||||
Self::Sha256Rsa2048 | Self::Sha256Rsa4096 | Self::Sha256Rsa8192 => {
|
||||
let scheme = Pkcs1v15Sign::new::<Sha256>();
|
||||
key.verify(scheme, digest, signature)
|
||||
.map_err(Error::RsaVerify)?;
|
||||
}
|
||||
Self::Sha512Rsa2048 | Self::Sha512Rsa4096 | Self::Sha512Rsa8192 => {
|
||||
let scheme = Pkcs1v15Sign::new::<Sha512>();
|
||||
key.verify(scheme, digest, signature)
|
||||
.map_err(Error::RsaVerify)?;
|
||||
}
|
||||
}
|
||||
let Some(algo) = self.to_digest_algorithm() else {
|
||||
return if self == Self::None {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::UnsupportedAlgorithm(self))
|
||||
};
|
||||
};
|
||||
|
||||
Ok(())
|
||||
key.verify_sig(algo, digest, signature)
|
||||
.map_err(|e| e.into())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1461,7 +1452,7 @@ impl Header {
|
||||
result.ok_or(Error::NoAppendedDescriptor)
|
||||
}
|
||||
|
||||
pub fn set_algo_for_key(&mut self, key: &RsaPrivateKey) -> Result<()> {
|
||||
pub fn set_algo_for_key(&mut self, key: &RsaSigningKey) -> Result<()> {
|
||||
let key_raw = encode_public_key(&key.to_public_key())?;
|
||||
|
||||
for algo in [AlgorithmType::Sha256Rsa2048, AlgorithmType::Sha256Rsa4096] {
|
||||
@@ -1481,30 +1472,17 @@ impl Header {
|
||||
self.public_key_metadata.clear();
|
||||
}
|
||||
|
||||
pub fn sign(&mut self, key: &RsaPrivateKey) -> Result<()> {
|
||||
pub fn sign(&mut self, key: &RsaSigningKey) -> Result<()> {
|
||||
let key_raw = encode_public_key(&key.to_public_key())?;
|
||||
|
||||
// RustCrypto does not support 8192-bit keys.
|
||||
match self.algorithm_type {
|
||||
AlgorithmType::Sha256Rsa8192
|
||||
| AlgorithmType::Sha512Rsa8192
|
||||
| AlgorithmType::Unknown(_) => {
|
||||
return Err(Error::UnsupportedAlgorithm(self.algorithm_type));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
if key_raw.len() != self.algorithm_type.public_key_len() {
|
||||
return Err(Error::IncorrectKeySize {
|
||||
key_size: key_raw.len(),
|
||||
algo: self.algorithm_type,
|
||||
});
|
||||
return Err(Error::IncorrectKeySize(key.size(), self.algorithm_type));
|
||||
}
|
||||
|
||||
// The public key and the sizes of the hash and signature are included
|
||||
// in the data that's about to be signed.
|
||||
self.public_key = key_raw;
|
||||
self.hash.resize(self.algorithm_type.hash_len(), 0);
|
||||
self.hash.resize(self.algorithm_type.digest_len(), 0);
|
||||
self.signature
|
||||
.resize(self.algorithm_type.signature_len(), 0);
|
||||
|
||||
@@ -1525,18 +1503,16 @@ impl Header {
|
||||
/// and return the public key. If the header is not signed, then `None` is
|
||||
/// returned.
|
||||
pub fn verify(&self) -> Result<Option<RsaPublicKey>> {
|
||||
// RustCrypto does not support 8192-bit keys.
|
||||
match self.algorithm_type {
|
||||
AlgorithmType::None => return Ok(None),
|
||||
a @ AlgorithmType::Sha256Rsa8192
|
||||
| a @ AlgorithmType::Sha512Rsa8192
|
||||
| a @ AlgorithmType::Unknown(_) => return Err(Error::UnsupportedAlgorithm(a)),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// Reconstruct the public key.
|
||||
let public_key = decode_public_key(&self.public_key)?;
|
||||
|
||||
if self.public_key.len() != self.algorithm_type.public_key_len() {
|
||||
return Err(Error::IncorrectKeySize(
|
||||
public_key.size(),
|
||||
self.algorithm_type,
|
||||
));
|
||||
}
|
||||
|
||||
let mut without_auth_writer = Cursor::new(Vec::new());
|
||||
self.to_writer_internal(&mut without_auth_writer, true)?;
|
||||
let without_auth = without_auth_writer.into_inner();
|
||||
@@ -1825,8 +1801,7 @@ pub fn decode_public_key(data: &[u8]) -> Result<RsaPublicKey> {
|
||||
reader.read_exact(&mut modulus_raw)?;
|
||||
|
||||
let modulus = BigUint::from_bytes_be(&modulus_raw);
|
||||
let public_key =
|
||||
RsaPublicKey::new(modulus, BigUint::from(65537u32)).map_err(Error::RsaVerify)?;
|
||||
let public_key = RsaPublicKey::new(modulus, BigUint::from(65537u32))?;
|
||||
|
||||
Ok(public_key)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Andrew Gunnerson
|
||||
* SPDX-FileCopyrightText: 2023-2024 Andrew Gunnerson
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
@@ -12,11 +12,11 @@ use std::{
|
||||
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
||||
use num_traits::ToPrimitive;
|
||||
use ring::digest::Context;
|
||||
use rsa::RsaPrivateKey;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{
|
||||
crypto::RsaSigningKey,
|
||||
format::{
|
||||
avb::{self, Descriptor, Header},
|
||||
padding,
|
||||
@@ -765,7 +765,7 @@ impl BootImageV3Through4 {
|
||||
/// Sign the boot image with a legacy VTS signature. Returns true if the
|
||||
/// image was successfully signed. Returns false if there's no vbmeta
|
||||
/// structure to sign in [`V4Extra::signature`].
|
||||
pub fn sign(&mut self, key: &RsaPrivateKey) -> Result<bool> {
|
||||
pub fn sign(&mut self, key: &RsaSigningKey) -> Result<bool> {
|
||||
let mut context = Context::new(&ring::digest::SHA256);
|
||||
let image_size;
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -9,7 +9,9 @@ pub mod compression;
|
||||
pub mod cpio;
|
||||
pub mod fec;
|
||||
pub mod hashtree;
|
||||
pub mod lp;
|
||||
pub mod ota;
|
||||
pub mod padding;
|
||||
pub mod payload;
|
||||
pub mod sparse;
|
||||
pub mod verityrs;
|
||||
|
||||
+267
-54
@@ -5,25 +5,24 @@
|
||||
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
fmt,
|
||||
io::{self, Cursor, Read, Seek, SeekFrom, Write},
|
||||
iter,
|
||||
sync::atomic::AtomicBool,
|
||||
};
|
||||
|
||||
use clap::ValueEnum;
|
||||
use cms::signed_data::SignedData;
|
||||
use const_oid::{db::rfc5912, ObjectIdentifier};
|
||||
use memchr::memmem;
|
||||
use prost::Message;
|
||||
use ring::digest::Context;
|
||||
use rsa::{Pkcs1v15Sign, RsaPrivateKey};
|
||||
use sha1::Sha1;
|
||||
use sha2::Sha256;
|
||||
use thiserror::Error;
|
||||
use x509_cert::{der::Encode, Certificate};
|
||||
use zip::{result::ZipError, write::FileOptions, CompressionMethod, ZipArchive, ZipWriter};
|
||||
|
||||
use crate::{
|
||||
crypto,
|
||||
crypto::{self, RsaPublicKeyExt, RsaSigningKey, SignatureAlgorithm},
|
||||
format::payload::{self, PayloadHeader},
|
||||
protobuf::build::tools::releasetools::{ota_metadata::OtaType, OtaMetadata},
|
||||
stream::{self, FromReader, HashingReader, HashingWriter},
|
||||
@@ -88,8 +87,6 @@ pub enum Error {
|
||||
Spki(#[from] pkcs8::spki::Error),
|
||||
#[error("x509 DER error")]
|
||||
Der(#[from] x509_cert::der::Error),
|
||||
#[error("RSA error")]
|
||||
Rsa(#[from] rsa::Error),
|
||||
#[error("Zip error")]
|
||||
Zip(#[from] ZipError),
|
||||
#[error("I/O error")]
|
||||
@@ -379,6 +376,18 @@ fn add_payload_metadata_entry(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
|
||||
pub enum ZipMode {
|
||||
Streaming,
|
||||
Seekable,
|
||||
}
|
||||
|
||||
impl fmt::Display for ZipMode {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(self.to_possible_value().ok_or(fmt::Error)?.get_name())
|
||||
}
|
||||
}
|
||||
|
||||
/// Add metadata files to the output OTA zip. `zip_entries` is the list of
|
||||
/// [`ZipEntry`] already written to `zip_writer`. `next_offset` is the current
|
||||
/// file offset (where the next zip entry's local header begins).
|
||||
@@ -394,6 +403,7 @@ pub fn add_metadata(
|
||||
next_offset: u64,
|
||||
metadata: &OtaMetadata,
|
||||
payload_metadata_size: u64,
|
||||
zip_mode: ZipMode,
|
||||
) -> Result<OtaMetadata> {
|
||||
let mut metadata = metadata.clone();
|
||||
let options = FileOptions::default().compression_method(CompressionMethod::Stored);
|
||||
@@ -414,7 +424,11 @@ pub fn add_metadata(
|
||||
// Add the placeholders to a temporary zip to compute final property files.
|
||||
let (temp_legacy_offset, temp_modern_offset) = {
|
||||
let (legacy_raw, modern_raw) = serialize_metadata(&metadata)?;
|
||||
let mut writer = ZipWriter::new_streaming(Cursor::new(Vec::new()));
|
||||
let raw_writer = Cursor::new(Vec::new());
|
||||
let mut writer = match zip_mode {
|
||||
ZipMode::Streaming => ZipWriter::new_streaming(raw_writer),
|
||||
ZipMode::Seekable => ZipWriter::new(raw_writer),
|
||||
};
|
||||
|
||||
writer.start_file_with_extra_data(PATH_METADATA, options)?;
|
||||
let legacy_offset = writer.end_extra_data()?;
|
||||
@@ -586,12 +600,12 @@ pub fn verify_ota(mut reader: impl Read + Seek, cancel_signal: &AtomicBool) -> R
|
||||
reader.seek(SeekFrom::Start(0))?;
|
||||
|
||||
// We support SHA1 for verification only.
|
||||
let (algorithm, scheme) = if signer.digest_alg.oid == rfc5912::ID_SHA_256 {
|
||||
(&ring::digest::SHA256, Pkcs1v15Sign::new::<Sha256>())
|
||||
let (algorithm, algo) = if signer.digest_alg.oid == rfc5912::ID_SHA_256 {
|
||||
(&ring::digest::SHA256, SignatureAlgorithm::Sha256WithRsa)
|
||||
} else {
|
||||
(
|
||||
&ring::digest::SHA1_FOR_LEGACY_USE_ONLY,
|
||||
Pkcs1v15Sign::new::<Sha1>(),
|
||||
SignatureAlgorithm::Sha1WithRsa,
|
||||
)
|
||||
};
|
||||
|
||||
@@ -603,7 +617,7 @@ pub fn verify_ota(mut reader: impl Read + Seek, cancel_signal: &AtomicBool) -> R
|
||||
let digest = context.finish();
|
||||
|
||||
// Verify the signature against the public key.
|
||||
public_key.verify(scheme, digest.as_ref(), signer.signature.as_bytes())?;
|
||||
public_key.verify_sig(algo, digest.as_ref(), signer.signature.as_bytes())?;
|
||||
|
||||
Ok(cert.clone())
|
||||
}
|
||||
@@ -642,17 +656,83 @@ pub fn parse_zip_ota_info(
|
||||
Ok((metadata, certificate, header, properties))
|
||||
}
|
||||
|
||||
/// Ensure that we're using a non-zip64 EOCD and there's no archive comment.
|
||||
fn validate_eocd(eocd: &[u8]) -> io::Result<()> {
|
||||
if &eocd[..4] != b"PK\x05\x06" {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"EOCD magic not found",
|
||||
));
|
||||
} else if &eocd[20..22] != b"\0\0" {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"Archive comment is not 0 bytes",
|
||||
));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Compute the digital signature for the specified digest, formatted as a zip
|
||||
/// file archive comment. The returned buffer includes both the 2-byte comment
|
||||
/// size field and the comment itself. It should be written to the end of the
|
||||
/// zip file after truncating the original 2-byte comment size field.
|
||||
fn compute_signature_comment(
|
||||
key: &RsaSigningKey,
|
||||
cert: &Certificate,
|
||||
digest: ring::digest::Digest,
|
||||
) -> Result<Vec<u8>> {
|
||||
let cms_signature = crypto::cms_sign_external(key, cert, digest.as_ref())?;
|
||||
let cms_signature_der = cms_signature.to_der()?;
|
||||
|
||||
// Includes placeholder for the EOCD comment size field.
|
||||
let mut buf = vec![0; 2];
|
||||
|
||||
// NULL-terminated readable message and actual signature.
|
||||
buf.extend(COMMENT_MESSAGE);
|
||||
buf.extend(&cms_signature_der);
|
||||
|
||||
// 6-byte OTA footer.
|
||||
let comment_size = buf.len() - 2 + 6;
|
||||
|
||||
// Absolute value of the offset of the signature from the end of the archive
|
||||
// comment.
|
||||
buf.extend((cms_signature_der.len() as u16 + 6).to_le_bytes());
|
||||
|
||||
// Magic value.
|
||||
buf.extend(b"\xff\xff");
|
||||
|
||||
// Archive comment size (for use by the OTA signature verifier).
|
||||
buf.extend(((comment_size) as u16).to_le_bytes());
|
||||
|
||||
if let Some(o) = memmem::find(&buf[2..], ZIP_EOCD_MAGIC) {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
format!("Archive comment contains EOCD magic at offset {o}"),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
|
||||
// Archive comment size (for the EOCD comment size field).
|
||||
buf[..2].copy_from_slice(&((comment_size) as u16).to_le_bytes());
|
||||
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
/// A writer that produces a signapk-style signed zip file with a whole-file
|
||||
/// signature stored in the zip archive comment. The data will be left in an
|
||||
/// unusable state if [`Self::finish()`] is not called.
|
||||
pub struct SigningWriter<W: Write> {
|
||||
///
|
||||
/// This writer works with streaming zip files created with data descriptors.
|
||||
/// The data is hashed as it is being written.
|
||||
pub struct StreamingSigningWriter<W> {
|
||||
inner: HashingWriter<W>,
|
||||
// Android only supports non-zip64 EOCD.
|
||||
queue: [u8; 22],
|
||||
used: usize,
|
||||
}
|
||||
|
||||
impl<W: Write> SigningWriter<W> {
|
||||
impl<W: Write> StreamingSigningWriter<W> {
|
||||
pub fn new(inner: W) -> Self {
|
||||
Self {
|
||||
inner: HashingWriter::new(inner, Context::new(&ring::digest::SHA256)),
|
||||
@@ -661,64 +741,29 @@ impl<W: Write> SigningWriter<W> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn finish(mut self, key: &RsaPrivateKey, cert: &Certificate) -> Result<W> {
|
||||
pub fn finish(mut self, key: &RsaSigningKey, cert: &Certificate) -> Result<W> {
|
||||
if self.used < self.queue.len() {
|
||||
return Err(
|
||||
io::Error::new(io::ErrorKind::InvalidData, "Too small to contain EOCD").into(),
|
||||
);
|
||||
} else if &self.queue[..4] != b"PK\x05\x06" {
|
||||
return Err(io::Error::new(io::ErrorKind::InvalidData, "EOCD magic not found").into());
|
||||
} else if &self.queue[20..22] != b"\0\0" {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"Archive comment is not 0 bytes",
|
||||
)
|
||||
.into());
|
||||
}
|
||||
|
||||
validate_eocd(&self.queue)?;
|
||||
|
||||
// Chop off the archive comment size field and write the remaining data.
|
||||
self.inner.write_all(&self.queue[..20])?;
|
||||
|
||||
let (mut raw_writer, context) = self.inner.finish();
|
||||
let digest = context.finish();
|
||||
|
||||
let cms_signature = crypto::cms_sign_external(key, cert, digest.as_ref())?;
|
||||
let cms_signature_der = cms_signature.to_der()?;
|
||||
|
||||
let mut comment = COMMENT_MESSAGE.to_vec();
|
||||
comment.extend(&cms_signature_der);
|
||||
|
||||
let comment_size = comment.len() + 6;
|
||||
|
||||
// Absolute value of the offset of the signature from the end of the
|
||||
// archive comment.
|
||||
comment.extend((cms_signature_der.len() as u16 + 6).to_le_bytes());
|
||||
|
||||
// Magic value.
|
||||
comment.extend(b"\xff\xff");
|
||||
|
||||
// EOCD archive comment size.
|
||||
comment.extend(((comment_size) as u16).to_le_bytes());
|
||||
|
||||
if let Some(o) = memmem::find(&comment, ZIP_EOCD_MAGIC) {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
format!("Archive comment contains EOCD magic at offset {o}"),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
|
||||
// Write EOCD comment size field, which was removed before.
|
||||
raw_writer.write_all(&((comment_size) as u16).to_le_bytes())?;
|
||||
|
||||
// Finally, write the comment.
|
||||
raw_writer.write_all(&comment)?;
|
||||
let size_and_comment = compute_signature_comment(key, cert, digest)?;
|
||||
raw_writer.write_all(&size_and_comment)?;
|
||||
|
||||
Ok(raw_writer)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Write> Write for SigningWriter<W> {
|
||||
impl<W: Write> Write for StreamingSigningWriter<W> {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
let (front, back) = buf.split_at(buf.len().saturating_sub(self.queue.len()));
|
||||
if front.is_empty() {
|
||||
@@ -750,3 +795,171 @@ impl<W: Write> Write for SigningWriter<W> {
|
||||
self.inner.flush()
|
||||
}
|
||||
}
|
||||
|
||||
/// A writer that produces a signapk-style signed zip file with a whole-file
|
||||
/// signature stored in the zip archive comment. The data will be left in an
|
||||
/// unusable state if [`Self::finish()`] is not called.
|
||||
///
|
||||
/// This writer works with zip files written without data descriptors. The data
|
||||
/// is hashed during [`Self::finish()`].
|
||||
pub struct SeekableSigningWriter<W> {
|
||||
inner: W,
|
||||
}
|
||||
|
||||
impl<W: Read + Write + Seek> SeekableSigningWriter<W> {
|
||||
pub fn new(inner: W) -> Self {
|
||||
Self { inner }
|
||||
}
|
||||
|
||||
pub fn finish(
|
||||
mut self,
|
||||
key: &RsaSigningKey,
|
||||
cert: &Certificate,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<W> {
|
||||
let file_size = self.seek(SeekFrom::End(0))?;
|
||||
|
||||
// Android only supports non-zip64 EOCD.
|
||||
if file_size < 22 {
|
||||
return Err(
|
||||
io::Error::new(io::ErrorKind::InvalidData, "Too small to contain EOCD").into(),
|
||||
);
|
||||
}
|
||||
|
||||
self.seek_relative(-22)?;
|
||||
let mut eocd = [0u8; 22];
|
||||
self.read_exact(&mut eocd)?;
|
||||
|
||||
validate_eocd(&eocd)?;
|
||||
|
||||
// Compute the digest of everything up until the comment size field.
|
||||
let mut hashing_writer = HashingWriter::new(
|
||||
io::sink(),
|
||||
ring::digest::Context::new(&ring::digest::SHA256),
|
||||
);
|
||||
|
||||
self.rewind()?;
|
||||
stream::copy_n(&mut self, &mut hashing_writer, file_size - 2, cancel_signal)?;
|
||||
|
||||
let digest = hashing_writer.finish().1.finish();
|
||||
|
||||
let size_and_comment = compute_signature_comment(key, cert, digest)?;
|
||||
self.inner.write_all(&size_and_comment)?;
|
||||
|
||||
Ok(self.inner)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Read> Read for SeekableSigningWriter<W> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
self.inner.read(buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Write> Write for SeekableSigningWriter<W> {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
self.inner.write(buf)
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
self.inner.flush()
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Seek> Seek for SeekableSigningWriter<W> {
|
||||
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
|
||||
self.inner.seek(pos)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
enum SigningWriterInner<W> {
|
||||
Streaming {
|
||||
inner: StreamingSigningWriter<W>,
|
||||
},
|
||||
Seekable {
|
||||
inner: SeekableSigningWriter<W>,
|
||||
/// We need to store this because [`SigningWriter::finish`] needs to be
|
||||
/// in `impl<W: Write>`, where [`SeekableSigningWriter::finish`] is not
|
||||
/// available.
|
||||
finish: fn(
|
||||
SeekableSigningWriter<W>,
|
||||
key: &RsaSigningKey,
|
||||
cert: &Certificate,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<W>,
|
||||
},
|
||||
}
|
||||
|
||||
/// A writer that produces a signapk-style signed zip file with a whole-file
|
||||
/// signature stored in the zip archive comment. The data will be left in an
|
||||
/// unusable state if [`Self::finish()`] is not called.
|
||||
///
|
||||
/// This is a partially type-erased wrapper around [`StreamingSigningWriter`]
|
||||
/// and [`SeekableSigningWriter`].
|
||||
pub struct SigningWriter<W>(SigningWriterInner<W>);
|
||||
|
||||
impl<W: Write> SigningWriter<W> {
|
||||
pub fn new_streaming(inner: W) -> Self {
|
||||
Self(SigningWriterInner::Streaming {
|
||||
inner: StreamingSigningWriter::new(inner),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn finish(
|
||||
self,
|
||||
key: &RsaSigningKey,
|
||||
cert: &Certificate,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<W> {
|
||||
match self.0 {
|
||||
SigningWriterInner::Streaming { inner } => inner.finish(key, cert),
|
||||
SigningWriterInner::Seekable { inner, finish } => {
|
||||
finish(inner, key, cert, cancel_signal)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Read + Write + Seek> SigningWriter<W> {
|
||||
pub fn new_seekable(inner: W) -> Self {
|
||||
Self(SigningWriterInner::Seekable {
|
||||
inner: SeekableSigningWriter::new(inner),
|
||||
finish: SeekableSigningWriter::finish,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Read> Read for SigningWriter<W> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
match &mut self.0 {
|
||||
SigningWriterInner::Streaming { .. } => panic!("Called with streaming writer"),
|
||||
SigningWriterInner::Seekable { inner, .. } => inner.read(buf),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Write> Write for SigningWriter<W> {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
match &mut self.0 {
|
||||
SigningWriterInner::Streaming { inner } => inner.write(buf),
|
||||
SigningWriterInner::Seekable { inner, .. } => inner.write(buf),
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
match &mut self.0 {
|
||||
SigningWriterInner::Streaming { inner } => inner.flush(),
|
||||
SigningWriterInner::Seekable { inner, .. } => inner.flush(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Seek> Seek for SigningWriter<W> {
|
||||
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
|
||||
match &mut self.0 {
|
||||
SigningWriterInner::Streaming { .. } => panic!("Called with streaming writer"),
|
||||
SigningWriterInner::Seekable { inner, .. } => inner.seek(pos),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+108
-26
@@ -1,10 +1,11 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022-2023 Andrew Gunnerson
|
||||
* SPDX-FileCopyrightText: 2022-2024 Andrew Gunnerson
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
fmt,
|
||||
io::{self, Cursor, Read, Seek, SeekFrom, Write},
|
||||
ops::Range,
|
||||
sync::atomic::AtomicBool,
|
||||
@@ -26,13 +27,12 @@ use rayon::{
|
||||
prelude::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator},
|
||||
};
|
||||
use ring::digest::{Context, Digest};
|
||||
use rsa::{traits::PublicKeyParts, Pkcs1v15Sign, RsaPrivateKey};
|
||||
use sha2::Sha256;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
use x509_cert::Certificate;
|
||||
|
||||
use crate::{
|
||||
crypto,
|
||||
crypto::{self, RsaPublicKeyExt, RsaSigningKey, SignatureAlgorithm},
|
||||
protobuf::chromeos_update_engine::{
|
||||
install_operation::Type, signatures::Signature, DeltaArchiveManifest, Extent,
|
||||
InstallOperation, PartitionInfo, PartitionUpdate, Signatures,
|
||||
@@ -100,19 +100,19 @@ pub enum Error {
|
||||
ProtobufDecode(#[from] prost::DecodeError),
|
||||
#[error("XZ stream error")]
|
||||
XzStream(#[from] liblzma::stream::Error),
|
||||
#[error("RSA error")]
|
||||
Rsa(#[from] rsa::Error),
|
||||
#[error("I/O error")]
|
||||
Io(#[from] io::Error),
|
||||
}
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct PayloadHeader {
|
||||
pub version: u64,
|
||||
pub manifest: DeltaArchiveManifest,
|
||||
#[serde(skip)]
|
||||
pub metadata_signature_size: u32,
|
||||
#[serde(skip)]
|
||||
pub blob_offset: u64,
|
||||
}
|
||||
|
||||
@@ -175,9 +175,8 @@ impl<R: Read> FromReader<R> for PayloadHeader {
|
||||
|
||||
/// Sign `digest` with `key` and return a [`Signatures`] protobuf struct with
|
||||
/// the signature padded to the maximum size.
|
||||
fn sign_digest(digest: &[u8], key: &RsaPrivateKey) -> Result<Signatures> {
|
||||
let scheme = Pkcs1v15Sign::new::<Sha256>();
|
||||
let mut digest_signed = key.sign(scheme, digest)?;
|
||||
fn sign_digest(digest: &[u8], key: &RsaSigningKey) -> Result<Signatures> {
|
||||
let mut digest_signed = key.sign(SignatureAlgorithm::Sha256WithRsa, digest)?;
|
||||
assert!(
|
||||
digest_signed.len() <= key.size(),
|
||||
"Signature exceeds maximum size",
|
||||
@@ -214,8 +213,7 @@ fn verify_digest(digest: &[u8], signatures: &Signatures, cert: &Certificate) ->
|
||||
};
|
||||
let without_padding = &data[..size as usize];
|
||||
|
||||
let scheme = Pkcs1v15Sign::new::<Sha256>();
|
||||
match public_key.verify(scheme, digest, without_padding) {
|
||||
match public_key.verify_sig(SignatureAlgorithm::Sha256WithRsa, digest, without_padding) {
|
||||
Ok(_) => return Ok(()),
|
||||
Err(e) => last_error = Some(e),
|
||||
}
|
||||
@@ -292,7 +290,7 @@ pub struct PayloadWriter<W: Write> {
|
||||
h_partial: Context,
|
||||
/// Includes signatures (hashes are for properties file).
|
||||
h_full: Context,
|
||||
key: RsaPrivateKey,
|
||||
key: RsaSigningKey,
|
||||
}
|
||||
|
||||
/// Write data to a writer and one or more hashers.
|
||||
@@ -315,7 +313,7 @@ impl<W: Write> PayloadWriter<W> {
|
||||
/// fields are ignored and internally recomputed to guarantee that there are
|
||||
/// no gaps. All partitions' install operation data is written to the blob
|
||||
/// section in order.
|
||||
pub fn new(mut inner: W, mut header: PayloadHeader, key: RsaPrivateKey) -> Result<Self> {
|
||||
pub fn new(mut inner: W, mut header: PayloadHeader, key: RsaSigningKey) -> Result<Self> {
|
||||
let mut blob_size = 0;
|
||||
|
||||
// The blob must contain all data in sequential order with no gaps.
|
||||
@@ -390,12 +388,12 @@ impl<W: Write> PayloadWriter<W> {
|
||||
}
|
||||
|
||||
/// Finalize the payload. If this function is not called, the payload will
|
||||
/// be left in an incomplete state. Returns the original writer, the
|
||||
/// contents that should be written for `payload_properties.txt` and the
|
||||
/// length of the header + manifest + manifest signature sections (for
|
||||
/// constructing the `payload_metadata.bin` OTA metadata property files
|
||||
/// be left in an incomplete state. Returns the original writer, the final
|
||||
/// header, the contents that should be written for `payload_properties.txt`
|
||||
/// and the length of the header + manifest + manifest signature sections
|
||||
/// (for constructing the `payload_metadata.bin` OTA metadata property files
|
||||
/// entry).
|
||||
pub fn finish(mut self) -> Result<(W, String, u64)> {
|
||||
pub fn finish(mut self) -> Result<(W, PayloadHeader, String, u64)> {
|
||||
// Append payload signature.
|
||||
let payload_partial_hash = self.h_partial.clone().finish();
|
||||
let payload_sig = sign_digest(payload_partial_hash.as_ref(), &self.key)?;
|
||||
@@ -419,7 +417,7 @@ impl<W: Write> PayloadWriter<W> {
|
||||
self.metadata_size as u64,
|
||||
);
|
||||
|
||||
Ok((self.inner, properties, metadata_with_sig_size))
|
||||
Ok((self.inner, self.header, properties, metadata_with_sig_size))
|
||||
}
|
||||
|
||||
/// Prepare for writing the next source data blob corresponding to an
|
||||
@@ -890,6 +888,52 @@ fn compress_chunk(raw_data: &[u8], cancel_signal: &AtomicBool) -> Result<(Vec<u8
|
||||
Ok((data, digest_compressed))
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
pub enum VabcAlgo {
|
||||
Lz4,
|
||||
Gzip,
|
||||
}
|
||||
|
||||
impl VabcAlgo {
|
||||
pub fn new(name: &str) -> Option<Self> {
|
||||
match name {
|
||||
"lz4" => Some(Self::Lz4),
|
||||
"gz" => Some(Self::Gzip),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn compressed_size(&self, mut raw_data: &[u8], block_size: u32) -> u64 {
|
||||
let mut total = 0;
|
||||
|
||||
while !raw_data.is_empty() {
|
||||
let n = raw_data.len().min(block_size as usize);
|
||||
let compressed = match self {
|
||||
Self::Lz4 => lz4_flex::block::compress(&raw_data[..n]),
|
||||
// We use the miniz_oxide backend for flate2, but flate2 doesn't
|
||||
// expose a nice function for compressing to a vec, so just use
|
||||
// miniz_oxide directly.
|
||||
Self::Gzip => miniz_oxide::deflate::compress_to_vec_zlib(&raw_data[..n], 9),
|
||||
};
|
||||
|
||||
total += compressed.len().min(n) as u64;
|
||||
|
||||
raw_data = &raw_data[n..];
|
||||
}
|
||||
|
||||
total
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for VabcAlgo {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Lz4 => f.write_str("lz4"),
|
||||
Self::Gzip => f.write_str("gz"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Compress the image and return the corresponding information to insert into
|
||||
/// the payload manifest's [`PartitionUpdate`] instance. The uncompressed data
|
||||
/// is split into 2 MiB chunks, which are read and compressed in parallel, and
|
||||
@@ -897,13 +941,21 @@ fn compress_chunk(raw_data: &[u8], cancel_signal: &AtomicBool) -> Result<(Vec<u8
|
||||
/// a corresponding [`InstallOperation`] in the return value. The caller must
|
||||
/// update [`InstallOperation::data_offset`] in each operation manually because
|
||||
/// the initial values are relative to 0.
|
||||
///
|
||||
/// If `vabc_algo` is set, the VABC CoW v2 size estimate will be computed. The
|
||||
/// caller must update [`PartitionUpdate::estimate_cow_size`] with this value or
|
||||
/// else update_engine may fail to flash the partition due to running out of
|
||||
/// space on the CoW block device. CoW v2 + other algorithms and also CoW v3 are
|
||||
/// currently unsupported because there currently are no known OTAs that use
|
||||
/// those configurations.
|
||||
pub fn compress_image(
|
||||
input: &(dyn ReadSeekReopen + Sync),
|
||||
output: &(dyn WriteSeekReopen + Sync),
|
||||
partition_name: &str,
|
||||
block_size: u32,
|
||||
vabc_algo: Option<VabcAlgo>,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<(PartitionInfo, Vec<InstallOperation>)> {
|
||||
) -> Result<(PartitionInfo, Vec<InstallOperation>, Option<u64>)> {
|
||||
const CHUNK_SIZE: u64 = 2 * 1024 * 1024;
|
||||
const CHUNK_GROUP: u64 = 32;
|
||||
|
||||
@@ -921,6 +973,7 @@ pub fn compress_image(
|
||||
let chunks_total = file_size.div_ceil(CHUNK_SIZE);
|
||||
let mut bytes_compressed = 0;
|
||||
let mut context_uncompressed = Context::new(&ring::digest::SHA256);
|
||||
let mut cow_estimate = 0;
|
||||
let mut operations = vec![];
|
||||
|
||||
// Read the file one group at a time. This allows for some parallelization
|
||||
@@ -957,8 +1010,13 @@ pub fn compress_image(
|
||||
let mut compressed_data_group = uncompressed_data_group
|
||||
.into_par_iter()
|
||||
.map(
|
||||
|(raw_offset, raw_data)| -> Result<(Vec<u8>, InstallOperation)> {
|
||||
|(raw_offset, raw_data)| -> Result<(Vec<u8>, InstallOperation, u64)> {
|
||||
let (data, digest_compressed) = compress_chunk(&raw_data, cancel_signal)?;
|
||||
let cow_size = if let Some(algo) = vabc_algo {
|
||||
algo.compressed_size(&raw_data, block_size)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
let extent = Extent {
|
||||
start_block: Some(raw_offset / u64::from(block_size)),
|
||||
@@ -971,19 +1029,20 @@ pub fn compress_image(
|
||||
operation.dst_extents.push(extent);
|
||||
operation.data_sha256_hash = Some(digest_compressed.as_ref().to_vec());
|
||||
|
||||
Ok((data, operation))
|
||||
Ok((data, operation, cow_size))
|
||||
},
|
||||
)
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
|
||||
for (data, operation) in &mut compressed_data_group {
|
||||
for (data, operation, cow_size) in &mut compressed_data_group {
|
||||
operation.data_offset = Some(bytes_compressed);
|
||||
bytes_compressed += data.len() as u64;
|
||||
cow_estimate += *cow_size;
|
||||
}
|
||||
|
||||
let group_operations = compressed_data_group
|
||||
.into_par_iter()
|
||||
.map(|(data, operation)| -> Result<InstallOperation> {
|
||||
.map(|(data, operation, _)| -> Result<InstallOperation> {
|
||||
let mut writer = output.reopen_boxed()?;
|
||||
writer.seek(SeekFrom::Start(operation.data_offset.unwrap()))?;
|
||||
writer.write_all(&data)?;
|
||||
@@ -1001,7 +1060,30 @@ pub fn compress_image(
|
||||
hash: Some(digest_uncompressed.as_ref().to_vec()),
|
||||
};
|
||||
|
||||
Ok((partition_info, operations))
|
||||
let cow_estimate = if vabc_algo.is_some() {
|
||||
// lz4_flex and miniz_oxide usually compress better than the lz4 and
|
||||
// zlib implementations used by libsnapshot_cow. Make up for this by
|
||||
// adding percentage-based overhead.
|
||||
cow_estimate += cow_estimate / 100;
|
||||
|
||||
// We also need to account for constant overhead, especially with
|
||||
// smaller partitions. We can match what delta_generator normally adds
|
||||
// in CowWriterV2::InitPos() exactly. Since we only ever create full
|
||||
// OTAs, we can assume that all CoW operations are kCowReplaceOp.
|
||||
|
||||
// sizeof(CowHeader).
|
||||
cow_estimate += 38;
|
||||
// header_.buffer_size (equal to BUFFER_REGION_DEFAULT_SIZE).
|
||||
cow_estimate += 2 * 1024 * 1024;
|
||||
// CowOptions::cluster_ops * sizeof(CowOperationV2).
|
||||
cow_estimate += 200 * 20;
|
||||
|
||||
Some(cow_estimate)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Ok((partition_info, operations, cow_estimate))
|
||||
}
|
||||
|
||||
fn extents_sorted(operations: &[InstallOperation]) -> bool {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+9
-6
@@ -3,16 +3,19 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
use std::sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
use std::{
|
||||
process::ExitCode,
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
},
|
||||
};
|
||||
|
||||
use tracing::error;
|
||||
|
||||
static LOGGING_INITIALIZED: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
fn main() {
|
||||
fn main() -> ExitCode {
|
||||
// Set up a cancel signal so we can properly clean up any temporary files.
|
||||
let cancel_signal = Arc::new(AtomicBool::new(false));
|
||||
{
|
||||
@@ -25,14 +28,14 @@ fn main() {
|
||||
}
|
||||
|
||||
match avbroot::cli::args::main(&LOGGING_INITIALIZED, &cancel_signal) {
|
||||
Ok(_) => {}
|
||||
Ok(_) => ExitCode::SUCCESS,
|
||||
Err(e) => {
|
||||
if LOGGING_INITIALIZED.load(Ordering::SeqCst) {
|
||||
error!("{e:?}");
|
||||
} else {
|
||||
eprintln!("{e:?}");
|
||||
}
|
||||
std::process::exit(1);
|
||||
ExitCode::FAILURE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,14 +23,14 @@ use liblzma::{
|
||||
use rayon::iter::{IntoParallelRefIterator, IntoParallelRefMutIterator, ParallelIterator};
|
||||
use regex::bytes::Regex;
|
||||
use ring::digest::Context;
|
||||
use rsa::{RsaPrivateKey, RsaPublicKey};
|
||||
use rsa::RsaPublicKey;
|
||||
use thiserror::Error;
|
||||
use tracing::{debug, debug_span, trace, warn, Span};
|
||||
use x509_cert::Certificate;
|
||||
use zip::{result::ZipError, ZipArchive};
|
||||
|
||||
use crate::{
|
||||
crypto,
|
||||
crypto::{self, RsaSigningKey},
|
||||
format::{
|
||||
avb::{self, AppendedDescriptorMut, Footer, Header},
|
||||
bootimage::{self, BootImage, BootImageExt, RamdiskMeta},
|
||||
@@ -161,6 +161,7 @@ impl MagiskRootPatcher {
|
||||
const VER_XZ_BACKUP: Range<u32> =
|
||||
26403..Self::VERS_SUPPORTED[Self::VERS_SUPPORTED.len() - 1].end;
|
||||
|
||||
const ZIP_INIT_LD: &'static str = "lib/arm64-v8a/libinit-ld.so";
|
||||
const ZIP_LIBMAGISK: &'static str = "lib/arm64-v8a/libmagisk.so";
|
||||
const ZIP_LIBMAGISK32: &'static str = "lib/armeabi-v7a/libmagisk32.so";
|
||||
const ZIP_LIBMAGISK64: &'static str = "lib/arm64-v8a/libmagisk64.so";
|
||||
@@ -462,6 +463,13 @@ impl BootImagePatch for MagiskRootPatcher {
|
||||
xz_files.insert(Self::ZIP_STUB, b"overlay.d/sbin/stub.xz");
|
||||
}
|
||||
|
||||
// Add init-ld, which only exists after Magisk commit
|
||||
// 33aebb59763b6ec27209563035303700e998633d
|
||||
if zip.file_names().any(|n| n == Self::ZIP_INIT_LD) {
|
||||
debug!("Magisk init-ld found");
|
||||
xz_files.insert(Self::ZIP_INIT_LD, b"overlay.d/sbin/init-ld.xz");
|
||||
}
|
||||
|
||||
for (source, target) in xz_files {
|
||||
let reader = zip
|
||||
.by_name(source)
|
||||
@@ -1122,7 +1130,7 @@ pub fn patch_boot_images<'a>(
|
||||
names: &[&'a str],
|
||||
open_input: impl Fn(&str) -> io::Result<Box<dyn ReadSeek>> + Sync,
|
||||
open_output: impl Fn(&str) -> io::Result<Box<dyn WriteSeek>> + Sync,
|
||||
key: &RsaPrivateKey,
|
||||
key: &RsaSigningKey,
|
||||
patchers: &[Box<dyn BootImagePatch + Sync>],
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<HashSet<&'a str>> {
|
||||
|
||||
@@ -11,13 +11,13 @@ use std::{
|
||||
|
||||
use memchr::memmem;
|
||||
use rayon::iter::{IntoParallelIterator, ParallelIterator};
|
||||
use rsa::RsaPrivateKey;
|
||||
use thiserror::Error;
|
||||
use tracing::{debug, debug_span, trace, Span};
|
||||
use x509_cert::Certificate;
|
||||
use zip::ZipArchive;
|
||||
|
||||
use crate::{
|
||||
crypto::RsaSigningKey,
|
||||
format::{
|
||||
avb::{self, AppendedDescriptorMut, Footer},
|
||||
ota,
|
||||
@@ -111,7 +111,7 @@ pub fn patch_system_image(
|
||||
input: &(dyn ReadSeekReopen + Sync),
|
||||
output: &(dyn WriteSeekReopen + Sync),
|
||||
certificate: &Certificate,
|
||||
key: &RsaPrivateKey,
|
||||
key: &RsaSigningKey,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<(Vec<Range<u64>>, Vec<Range<u64>>)> {
|
||||
// This must be a multiple of normal filesystem block sizes (eg. 4 KiB).
|
||||
|
||||
+7
-55
@@ -211,7 +211,7 @@ impl<W: Write + Reopen> Reopen for BufWriter<W> {
|
||||
|
||||
/// A reader wrapper that implements [`Seek`], but only for reporting the
|
||||
/// current file position.
|
||||
pub struct CountingReader<R: Read> {
|
||||
pub struct CountingReader<R> {
|
||||
inner: R,
|
||||
offset: u64,
|
||||
}
|
||||
@@ -249,7 +249,7 @@ impl<R: Read> Seek for CountingReader<R> {
|
||||
|
||||
/// A writer wrapper that implements [`Seek`], but only for reporting the
|
||||
/// current file position.
|
||||
pub struct CountingWriter<W: Write> {
|
||||
pub struct CountingWriter<W> {
|
||||
inner: W,
|
||||
offset: u64,
|
||||
}
|
||||
@@ -290,7 +290,7 @@ impl<W: Write> Seek for CountingWriter<W> {
|
||||
}
|
||||
|
||||
/// A reader wrapper that hashes data as it's being read.
|
||||
pub struct HashingReader<R: Read> {
|
||||
pub struct HashingReader<R> {
|
||||
inner: R,
|
||||
context: Context,
|
||||
}
|
||||
@@ -314,7 +314,7 @@ impl<R: Read> Read for HashingReader<R> {
|
||||
}
|
||||
|
||||
/// A writer wrapper that hashes data as it's being written.
|
||||
pub struct HashingWriter<W: Write> {
|
||||
pub struct HashingWriter<W> {
|
||||
inner: W,
|
||||
context: Context,
|
||||
}
|
||||
@@ -342,7 +342,7 @@ impl<W: Write> Write for HashingWriter<W> {
|
||||
}
|
||||
|
||||
/// A reader wrapper that only allows reading a specific section of a file.
|
||||
pub struct SectionReader<R: Read + Seek> {
|
||||
pub struct SectionReader<R> {
|
||||
inner: R,
|
||||
start: u64,
|
||||
size: u64,
|
||||
@@ -416,38 +416,6 @@ impl<R: Read + Seek> Seek for SectionReader<R> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A writer wrapper that seeks instead of writing when a write buffer consists
|
||||
/// solely of zeros.
|
||||
#[derive(Debug)]
|
||||
pub struct HolePunchingWriter<W: Write + Seek> {
|
||||
inner: W,
|
||||
}
|
||||
|
||||
impl<W: Write + Seek> HolePunchingWriter<W> {
|
||||
pub fn new(inner: W) -> Self {
|
||||
Self { inner }
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> W {
|
||||
self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Write + Seek> Write for HolePunchingWriter<W> {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
if util::is_zero(buf) {
|
||||
self.inner.seek(SeekFrom::Current(buf.len() as i64))?;
|
||||
Ok(buf.len())
|
||||
} else {
|
||||
self.inner.write(buf)
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
self.inner.flush()
|
||||
}
|
||||
}
|
||||
|
||||
/// A file wrapper that uses a userspace file offset. A reopened instance uses
|
||||
/// the same underlying kernel file descriptor, but a new userspace file offset,
|
||||
/// initially set to 0.
|
||||
@@ -720,9 +688,8 @@ mod tests {
|
||||
use ring::digest::Context;
|
||||
|
||||
use super::{
|
||||
CountingReader, CountingWriter, HashingReader, HashingWriter, HolePunchingWriter,
|
||||
PSeekFile, ReadDiscardExt, ReadStringExt, Reopen, SectionReader, SharedCursor,
|
||||
WriteStringExt, WriteZerosExt,
|
||||
CountingReader, CountingWriter, HashingReader, HashingWriter, PSeekFile, ReadDiscardExt,
|
||||
ReadStringExt, Reopen, SectionReader, SharedCursor, WriteStringExt, WriteZerosExt,
|
||||
};
|
||||
|
||||
const FOOBAR_SHA256: [u8; 32] = [
|
||||
@@ -882,21 +849,6 @@ mod tests {
|
||||
assert_eq!(raw_reader.stream_position().unwrap(), 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hole_punching_writer() {
|
||||
let raw_writer = Cursor::new(b"foobar foobar".to_owned());
|
||||
let mut writer = HolePunchingWriter::new(raw_writer);
|
||||
|
||||
writer.write_all(b"hello").unwrap();
|
||||
writer.write_all(b"").unwrap();
|
||||
writer.write_all(b"\0").unwrap();
|
||||
writer.write_all(b"\0\0").unwrap();
|
||||
writer.write_all(b"world").unwrap();
|
||||
|
||||
let raw_writer = writer.into_inner();
|
||||
assert_eq!(&raw_writer.into_inner(), b"hellor fworld");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pseek_file() {
|
||||
let raw_file = tempfile::tempfile().unwrap();
|
||||
|
||||
@@ -14,6 +14,7 @@ use rsa::RsaPrivateKey;
|
||||
|
||||
use avbroot::{
|
||||
self,
|
||||
crypto::RsaSigningKey,
|
||||
format::avb::{
|
||||
self, AlgorithmType, AppendedDescriptorMut, AppendedDescriptorRef,
|
||||
ChainPartitionDescriptor, Descriptor, Footer, HashDescriptor, HashTreeDescriptor, Header,
|
||||
@@ -22,7 +23,7 @@ use avbroot::{
|
||||
stream::SharedCursor,
|
||||
};
|
||||
|
||||
fn get_test_key() -> RsaPrivateKey {
|
||||
fn get_test_key() -> RsaSigningKey {
|
||||
let data = include_str!(concat!(
|
||||
env!("CARGO_WORKSPACE_DIR"),
|
||||
"/e2e/keys/TEST_KEY_DO_NOT_USE_avb.key",
|
||||
@@ -32,7 +33,8 @@ fn get_test_key() -> RsaPrivateKey {
|
||||
"/e2e/keys/TEST_KEY_DO_NOT_USE_avb.passphrase",
|
||||
));
|
||||
|
||||
RsaPrivateKey::from_pkcs8_encrypted_pem(data, passphrase.trim_end()).unwrap()
|
||||
let key = RsaPrivateKey::from_pkcs8_encrypted_pem(data, passphrase.trim_end()).unwrap();
|
||||
RsaSigningKey::Internal(key)
|
||||
}
|
||||
|
||||
fn repeat_str(s: &str, max_len: usize) -> String {
|
||||
|
||||
@@ -7,6 +7,7 @@ use std::io::Cursor;
|
||||
|
||||
use avbroot::{
|
||||
self,
|
||||
crypto::RsaSigningKey,
|
||||
format::{
|
||||
avb::{AlgorithmType, Descriptor, HashDescriptor, Header},
|
||||
bootimage::{
|
||||
@@ -19,7 +20,7 @@ use avbroot::{
|
||||
use pkcs8::DecodePrivateKey;
|
||||
use rsa::RsaPrivateKey;
|
||||
|
||||
fn get_test_key() -> RsaPrivateKey {
|
||||
fn get_test_key() -> RsaSigningKey {
|
||||
let data = include_str!(concat!(
|
||||
env!("CARGO_WORKSPACE_DIR"),
|
||||
"/e2e/keys/TEST_KEY_DO_NOT_USE_avb.key",
|
||||
@@ -29,7 +30,8 @@ fn get_test_key() -> RsaPrivateKey {
|
||||
"/e2e/keys/TEST_KEY_DO_NOT_USE_avb.passphrase",
|
||||
));
|
||||
|
||||
RsaPrivateKey::from_pkcs8_encrypted_pem(data, passphrase.trim_end()).unwrap()
|
||||
let key = RsaPrivateKey::from_pkcs8_encrypted_pem(data, passphrase.trim_end()).unwrap();
|
||||
RsaSigningKey::Internal(key)
|
||||
}
|
||||
|
||||
fn repeat(s: &str, max_len: usize) -> String {
|
||||
|
||||
@@ -0,0 +1,428 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024 Andrew Gunnerson
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
use std::{io::Cursor, num::NonZeroU64};
|
||||
|
||||
use avbroot::{
|
||||
format::lp::{
|
||||
BlockDevice, BlockDeviceFlags, Extent, ExtentType, HeaderFlags, ImageType, Metadata,
|
||||
MetadataSlot, Partition, PartitionAttributes, PartitionGroup, PartitionGroupFlags,
|
||||
},
|
||||
stream::{FromReader, ToWriter},
|
||||
};
|
||||
|
||||
fn round_trip(metadata: &Metadata, sha512: &[u8; 64]) {
|
||||
let mut writer = Cursor::new(Vec::new());
|
||||
metadata.to_writer(&mut writer).unwrap();
|
||||
let data = writer.into_inner();
|
||||
|
||||
assert_eq!(
|
||||
ring::digest::digest(&ring::digest::SHA512, &data).as_ref(),
|
||||
sha512,
|
||||
);
|
||||
|
||||
let mut reader = Cursor::new(&data);
|
||||
let new_metadata = Metadata::from_reader(&mut reader).unwrap();
|
||||
|
||||
assert_eq!(&new_metadata, metadata);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trip_empty_image() {
|
||||
// Layout from Google Pixel 9 Pro XL stock factory image:
|
||||
// komodo-ad1a.240530.047-factory-bb04e484.zip -> super_empty.img
|
||||
let metadata = Metadata {
|
||||
image_type: ImageType::Empty,
|
||||
metadata_max_size: 65536,
|
||||
metadata_slot_count: 3,
|
||||
logical_block_size: 4096,
|
||||
slots: vec![MetadataSlot {
|
||||
major_version: 10,
|
||||
minor_version: 2,
|
||||
groups: vec![
|
||||
PartitionGroup {
|
||||
name: "default".into(),
|
||||
flags: PartitionGroupFlags::empty(),
|
||||
maximum_size: None,
|
||||
partitions: vec![],
|
||||
},
|
||||
PartitionGroup {
|
||||
name: "google_dynamic_partitions_a".into(),
|
||||
flags: PartitionGroupFlags::empty(),
|
||||
maximum_size: NonZeroU64::new(8527020032),
|
||||
partitions: vec![
|
||||
Partition {
|
||||
name: "system_a".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![],
|
||||
},
|
||||
Partition {
|
||||
name: "system_dlkm_a".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![],
|
||||
},
|
||||
Partition {
|
||||
name: "system_ext_a".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![],
|
||||
},
|
||||
Partition {
|
||||
name: "product_a".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![],
|
||||
},
|
||||
Partition {
|
||||
name: "vendor_a".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![],
|
||||
},
|
||||
Partition {
|
||||
name: "vendor_dlkm_a".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![],
|
||||
},
|
||||
],
|
||||
},
|
||||
PartitionGroup {
|
||||
name: "google_dynamic_partitions_b".into(),
|
||||
flags: PartitionGroupFlags::empty(),
|
||||
maximum_size: NonZeroU64::new(8527020032),
|
||||
partitions: vec![
|
||||
Partition {
|
||||
name: "system_b".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![],
|
||||
},
|
||||
Partition {
|
||||
name: "system_dlkm_b".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![],
|
||||
},
|
||||
Partition {
|
||||
name: "system_ext_b".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![],
|
||||
},
|
||||
Partition {
|
||||
name: "product_b".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![],
|
||||
},
|
||||
Partition {
|
||||
name: "vendor_b".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![],
|
||||
},
|
||||
Partition {
|
||||
name: "vendor_dlkm_b".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
block_devices: vec![BlockDevice {
|
||||
first_logical_sector: 2048,
|
||||
alignment: 1048576,
|
||||
alignment_offset: 0,
|
||||
size: 8531214336,
|
||||
partition_name: "super".into(),
|
||||
flags: BlockDeviceFlags::empty(),
|
||||
}],
|
||||
flags: HeaderFlags::VIRTUAL_AB_DEVICE,
|
||||
}],
|
||||
};
|
||||
// This is semantically equivalent, but not identical. The Metadata data
|
||||
// structure only retains the order of partitions within a group, but not
|
||||
// globally. This checksum is meant to protect against unintended future
|
||||
// changes.
|
||||
let sha512 = [
|
||||
0xfa, 0xdf, 0xf2, 0xb6, 0x74, 0xec, 0x78, 0x7d, 0x0f, 0x7d, 0x17, 0x54, 0xcf, 0x1b, 0x53,
|
||||
0x13, 0x66, 0x13, 0x5e, 0x8e, 0xcc, 0x84, 0xa2, 0x63, 0xaf, 0x0d, 0x68, 0x96, 0xc6, 0x40,
|
||||
0x4e, 0x83, 0xe4, 0xe9, 0xef, 0x61, 0xdc, 0x2a, 0x25, 0x5f, 0xa2, 0x7d, 0x29, 0x0b, 0xb6,
|
||||
0x26, 0x93, 0x59, 0xc9, 0xa8, 0x56, 0x3b, 0x3d, 0x3d, 0x15, 0x6b, 0xee, 0x78, 0x56, 0x78,
|
||||
0xa1, 0x83, 0x6d, 0x70,
|
||||
];
|
||||
|
||||
round_trip(&metadata, &sha512);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trip_normal_image() {
|
||||
// Layout from Google Pixel 9 Pro XL GrapheneOS factory image:
|
||||
// komodo-install-2024082500.zip -> super_1.img
|
||||
let slot = MetadataSlot {
|
||||
major_version: 10,
|
||||
minor_version: 2,
|
||||
groups: vec![
|
||||
PartitionGroup {
|
||||
name: "default".into(),
|
||||
flags: PartitionGroupFlags::empty(),
|
||||
maximum_size: None,
|
||||
partitions: vec![],
|
||||
},
|
||||
PartitionGroup {
|
||||
name: "google_dynamic_partitions_a".into(),
|
||||
flags: PartitionGroupFlags::empty(),
|
||||
maximum_size: NonZeroU64::new(8527020032),
|
||||
partitions: vec![
|
||||
Partition {
|
||||
name: "system_a".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![Extent {
|
||||
num_sectors: 2465952,
|
||||
extent_type: ExtentType::Linear {
|
||||
start_sector: 2048,
|
||||
block_device_index: 0,
|
||||
},
|
||||
}],
|
||||
},
|
||||
Partition {
|
||||
name: "system_dlkm_a".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![Extent {
|
||||
num_sectors: 23720,
|
||||
extent_type: ExtentType::Linear {
|
||||
start_sector: 2469888,
|
||||
block_device_index: 0,
|
||||
},
|
||||
}],
|
||||
},
|
||||
Partition {
|
||||
name: "system_ext_a".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![Extent {
|
||||
num_sectors: 786144,
|
||||
extent_type: ExtentType::Linear {
|
||||
start_sector: 2494464,
|
||||
block_device_index: 0,
|
||||
},
|
||||
}],
|
||||
},
|
||||
Partition {
|
||||
name: "product_a".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![Extent {
|
||||
num_sectors: 1396432,
|
||||
extent_type: ExtentType::Linear {
|
||||
start_sector: 3280896,
|
||||
block_device_index: 0,
|
||||
},
|
||||
}],
|
||||
},
|
||||
Partition {
|
||||
name: "vendor_a".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![Extent {
|
||||
num_sectors: 1959024,
|
||||
extent_type: ExtentType::Linear {
|
||||
start_sector: 4677632,
|
||||
block_device_index: 0,
|
||||
},
|
||||
}],
|
||||
},
|
||||
Partition {
|
||||
name: "vendor_dlkm_a".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![Extent {
|
||||
num_sectors: 55008,
|
||||
extent_type: ExtentType::Linear {
|
||||
start_sector: 6637568,
|
||||
block_device_index: 0,
|
||||
},
|
||||
}],
|
||||
},
|
||||
],
|
||||
},
|
||||
PartitionGroup {
|
||||
name: "google_dynamic_partitions_b".into(),
|
||||
flags: PartitionGroupFlags::empty(),
|
||||
maximum_size: NonZeroU64::new(8527020032),
|
||||
partitions: vec![
|
||||
Partition {
|
||||
name: "system_b".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![],
|
||||
},
|
||||
Partition {
|
||||
name: "system_dlkm_b".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![],
|
||||
},
|
||||
Partition {
|
||||
name: "system_ext_b".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![],
|
||||
},
|
||||
Partition {
|
||||
name: "product_b".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![],
|
||||
},
|
||||
Partition {
|
||||
name: "vendor_b".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![],
|
||||
},
|
||||
Partition {
|
||||
name: "vendor_dlkm_b".into(),
|
||||
attributes: PartitionAttributes::READONLY,
|
||||
extents: vec![],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
block_devices: vec![BlockDevice {
|
||||
first_logical_sector: 2048,
|
||||
alignment: 1048576,
|
||||
alignment_offset: 0,
|
||||
size: 8531214336,
|
||||
partition_name: "super".into(),
|
||||
flags: BlockDeviceFlags::empty(),
|
||||
}],
|
||||
flags: HeaderFlags::VIRTUAL_AB_DEVICE,
|
||||
};
|
||||
let metadata = Metadata {
|
||||
image_type: ImageType::Normal,
|
||||
metadata_max_size: 65536,
|
||||
metadata_slot_count: 3,
|
||||
logical_block_size: 4096,
|
||||
slots: vec![slot; 3],
|
||||
};
|
||||
// This is semantically equivalent, but not identical. The Metadata data
|
||||
// structure only retains the order of partitions within a group, but not
|
||||
// globally. This checksum is meant to protect against unintended future
|
||||
// changes.
|
||||
let sha512 = [
|
||||
0x3b, 0xad, 0xd4, 0x22, 0xa1, 0x5a, 0xc5, 0xdf, 0x72, 0x7d, 0x92, 0x35, 0x04, 0x8a, 0x75,
|
||||
0xd9, 0x33, 0x0d, 0xaa, 0x9e, 0x97, 0xd4, 0x13, 0x28, 0x5e, 0x0f, 0x12, 0x0c, 0xf2, 0xb3,
|
||||
0xdc, 0x35, 0x89, 0x65, 0x40, 0xb0, 0x67, 0xb1, 0x54, 0x09, 0x52, 0x3e, 0x78, 0x3d, 0x3f,
|
||||
0xa7, 0xf7, 0xa0, 0x77, 0xa8, 0xfc, 0xb7, 0x93, 0x19, 0xcd, 0x43, 0xea, 0x9a, 0x74, 0x65,
|
||||
0x54, 0x3c, 0xaa, 0x12,
|
||||
];
|
||||
|
||||
round_trip(&metadata, &sha512);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trip_retrofit_image() {
|
||||
// Layout from Google Pixel 3a XL stock factory image:
|
||||
// bonito-ota-sp2a.220505.008-37a410d5.zip -> system.img
|
||||
let slot = MetadataSlot {
|
||||
major_version: 10,
|
||||
minor_version: 0,
|
||||
groups: vec![
|
||||
PartitionGroup {
|
||||
name: "default".into(),
|
||||
flags: PartitionGroupFlags::empty(),
|
||||
maximum_size: None,
|
||||
partitions: vec![],
|
||||
},
|
||||
PartitionGroup {
|
||||
name: "google_dynamic_partitions".into(),
|
||||
flags: PartitionGroupFlags::SLOT_SUFFIXED,
|
||||
maximum_size: NonZeroU64::new(4068474880),
|
||||
partitions: vec![
|
||||
Partition {
|
||||
name: "system".into(),
|
||||
attributes: PartitionAttributes::READONLY
|
||||
| PartitionAttributes::SLOT_SUFFIXED,
|
||||
extents: vec![Extent {
|
||||
num_sectors: 1757416,
|
||||
extent_type: ExtentType::Linear {
|
||||
start_sector: 2048,
|
||||
block_device_index: 0,
|
||||
},
|
||||
}],
|
||||
},
|
||||
Partition {
|
||||
name: "vendor".into(),
|
||||
attributes: PartitionAttributes::READONLY
|
||||
| PartitionAttributes::SLOT_SUFFIXED,
|
||||
extents: vec![Extent {
|
||||
num_sectors: 991848,
|
||||
extent_type: ExtentType::Linear {
|
||||
start_sector: 1761280,
|
||||
block_device_index: 0,
|
||||
},
|
||||
}],
|
||||
},
|
||||
Partition {
|
||||
name: "product".into(),
|
||||
attributes: PartitionAttributes::READONLY
|
||||
| PartitionAttributes::SLOT_SUFFIXED,
|
||||
extents: vec![
|
||||
Extent {
|
||||
num_sectors: 3627008,
|
||||
extent_type: ExtentType::Linear {
|
||||
start_sector: 2754560,
|
||||
block_device_index: 0,
|
||||
},
|
||||
},
|
||||
Extent {
|
||||
num_sectors: 538240,
|
||||
extent_type: ExtentType::Linear {
|
||||
start_sector: 2048,
|
||||
block_device_index: 1,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
Partition {
|
||||
name: "system_ext".into(),
|
||||
attributes: PartitionAttributes::READONLY
|
||||
| PartitionAttributes::SLOT_SUFFIXED,
|
||||
extents: vec![Extent {
|
||||
num_sectors: 490744,
|
||||
extent_type: ExtentType::Linear {
|
||||
start_sector: 540672,
|
||||
block_device_index: 1,
|
||||
},
|
||||
}],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
block_devices: vec![
|
||||
BlockDevice {
|
||||
first_logical_sector: 2048,
|
||||
alignment: 1048576,
|
||||
alignment_offset: 0,
|
||||
size: 3267362816,
|
||||
partition_name: "system".into(),
|
||||
flags: BlockDeviceFlags::SLOT_SUFFIXED,
|
||||
},
|
||||
BlockDevice {
|
||||
first_logical_sector: 2048,
|
||||
alignment: 1048576,
|
||||
alignment_offset: 0,
|
||||
size: 805306368,
|
||||
partition_name: "vendor".into(),
|
||||
flags: BlockDeviceFlags::SLOT_SUFFIXED,
|
||||
},
|
||||
],
|
||||
flags: HeaderFlags::empty(),
|
||||
};
|
||||
let metadata = Metadata {
|
||||
image_type: ImageType::Normal,
|
||||
metadata_max_size: 65536,
|
||||
metadata_slot_count: 2,
|
||||
logical_block_size: 4096,
|
||||
slots: vec![slot; 2],
|
||||
};
|
||||
// First 274432 bytes of system.img. Unlike the other test cases, this is
|
||||
// identical to the original image because there is only one partition group
|
||||
// with partitions, so the group-level ordering is the same as the global
|
||||
// ordering.
|
||||
let sha512 = [
|
||||
0xb9, 0x97, 0xf5, 0x83, 0x39, 0x37, 0x90, 0x0a, 0xb6, 0x46, 0xdd, 0x27, 0x57, 0xf1, 0xf3,
|
||||
0xbd, 0x8f, 0xc4, 0x63, 0x07, 0x6f, 0xf4, 0x19, 0xc0, 0x02, 0x28, 0x48, 0x99, 0x54, 0xbb,
|
||||
0xb3, 0xbf, 0x67, 0x95, 0xc4, 0xa7, 0x99, 0xf4, 0xa9, 0xc4, 0xf4, 0x1d, 0xf7, 0x59, 0x28,
|
||||
0xeb, 0xbc, 0x85, 0x46, 0xd1, 0x7d, 0x65, 0x0f, 0xbe, 0x21, 0xf6, 0xf2, 0xa2, 0x20, 0x5b,
|
||||
0xda, 0xde, 0xfa, 0x50,
|
||||
];
|
||||
|
||||
round_trip(&metadata, &sha512);
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024 Andrew Gunnerson
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
use std::io::{Cursor, Read, Write};
|
||||
|
||||
use avbroot::format::sparse::{
|
||||
self, Chunk, ChunkBounds, ChunkData, CrcMode, Header, SparseReader, SparseWriter,
|
||||
};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct TestChunk {
|
||||
chunk: Chunk,
|
||||
data: &'static [u8],
|
||||
}
|
||||
|
||||
fn round_trip(block_size: u32, crc32: u32, test_chunks: &[TestChunk], sha512: &[u8; 64]) {
|
||||
let num_blocks = test_chunks.iter().map(|d| d.chunk.bounds.len()).sum();
|
||||
let header = Header {
|
||||
major_version: sparse::MAJOR_VERSION,
|
||||
minor_version: sparse::MINOR_VERSION,
|
||||
block_size,
|
||||
num_blocks,
|
||||
num_chunks: test_chunks.len() as u32,
|
||||
crc32,
|
||||
};
|
||||
|
||||
let writer = Cursor::new(Vec::new());
|
||||
let mut sparse_writer = SparseWriter::new(writer, header).unwrap();
|
||||
|
||||
for test_chunk in test_chunks {
|
||||
sparse_writer.start_chunk(test_chunk.chunk).unwrap();
|
||||
|
||||
if !test_chunk.data.is_empty() {
|
||||
sparse_writer.write_all(test_chunk.data).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
let writer = sparse_writer.finish().unwrap();
|
||||
let data = writer.into_inner();
|
||||
|
||||
assert_eq!(
|
||||
ring::digest::digest(&ring::digest::SHA512, &data).as_ref(),
|
||||
sha512,
|
||||
);
|
||||
|
||||
let reader = Cursor::new(&data);
|
||||
let mut sparse_reader = SparseReader::new(reader, CrcMode::Validate).unwrap();
|
||||
|
||||
assert_eq!(sparse_reader.header(), header);
|
||||
|
||||
let mut test_chunks_iter = test_chunks.iter();
|
||||
|
||||
while let Some(chunk) = sparse_reader.next_chunk().unwrap() {
|
||||
let test_chunk = test_chunks_iter.next().unwrap();
|
||||
|
||||
assert_eq!(chunk, test_chunk.chunk);
|
||||
|
||||
if !test_chunk.data.is_empty() {
|
||||
let mut buf = vec![];
|
||||
sparse_reader.read_to_end(&mut buf).unwrap();
|
||||
|
||||
assert_eq!(buf, test_chunk.data);
|
||||
}
|
||||
}
|
||||
|
||||
assert!(test_chunks_iter.next().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trip_full_image() {
|
||||
let block_size = 8;
|
||||
let file_crc32 = 0xf6e23567;
|
||||
let test_chunks = [
|
||||
TestChunk {
|
||||
chunk: Chunk {
|
||||
bounds: ChunkBounds { start: 0, end: 1 },
|
||||
data: ChunkData::Data,
|
||||
},
|
||||
data: b"\x00\x01\x02\x03\x04\x05\x06\x07",
|
||||
},
|
||||
TestChunk {
|
||||
chunk: Chunk {
|
||||
bounds: ChunkBounds { start: 1, end: 1 },
|
||||
data: ChunkData::Crc32(0x88aa689f),
|
||||
},
|
||||
data: b"",
|
||||
},
|
||||
TestChunk {
|
||||
chunk: Chunk {
|
||||
bounds: ChunkBounds { start: 1, end: 2 },
|
||||
data: ChunkData::Fill(0x01234567),
|
||||
},
|
||||
data: b"",
|
||||
},
|
||||
TestChunk {
|
||||
chunk: Chunk {
|
||||
bounds: ChunkBounds { start: 2, end: 3 },
|
||||
data: ChunkData::Data,
|
||||
},
|
||||
data: b"\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
|
||||
},
|
||||
TestChunk {
|
||||
chunk: Chunk {
|
||||
bounds: ChunkBounds { start: 3, end: 3 },
|
||||
data: ChunkData::Crc32(0xf6e23567),
|
||||
},
|
||||
data: b"",
|
||||
},
|
||||
];
|
||||
let sha512 = [
|
||||
0x19, 0x5f, 0xa7, 0xdb, 0x18, 0xc6, 0xb9, 0x0e, 0xce, 0x4b, 0x4f, 0x35, 0x36, 0x79, 0x46,
|
||||
0x02, 0x7a, 0x45, 0x66, 0x63, 0x0e, 0xd9, 0x76, 0x93, 0x2b, 0x88, 0xe2, 0xbc, 0x0b, 0xd9,
|
||||
0x1f, 0x21, 0x51, 0x92, 0x00, 0x2e, 0xe3, 0xa2, 0xff, 0x24, 0xea, 0xef, 0x24, 0xd5, 0x24,
|
||||
0xf0, 0x46, 0xf3, 0x10, 0x32, 0xf4, 0xa6, 0x3b, 0x9d, 0xcd, 0xc5, 0x57, 0xf4, 0xc0, 0xe8,
|
||||
0x01, 0xe8, 0x1d, 0xb3,
|
||||
];
|
||||
|
||||
round_trip(block_size, file_crc32, &test_chunks, &sha512);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trip_partial_image() {
|
||||
let block_size = 8;
|
||||
let file_crc32 = 0;
|
||||
let test_chunks = [
|
||||
TestChunk {
|
||||
chunk: Chunk {
|
||||
bounds: ChunkBounds { start: 0, end: 1 },
|
||||
data: ChunkData::Hole,
|
||||
},
|
||||
data: b"",
|
||||
},
|
||||
TestChunk {
|
||||
chunk: Chunk {
|
||||
bounds: ChunkBounds { start: 1, end: 2 },
|
||||
data: ChunkData::Data,
|
||||
},
|
||||
data: b"\x00\x01\x02\x03\x04\x05\x06\x07",
|
||||
},
|
||||
TestChunk {
|
||||
chunk: Chunk {
|
||||
bounds: ChunkBounds { start: 2, end: 3 },
|
||||
data: ChunkData::Hole,
|
||||
},
|
||||
data: b"",
|
||||
},
|
||||
TestChunk {
|
||||
chunk: Chunk {
|
||||
bounds: ChunkBounds { start: 3, end: 4 },
|
||||
data: ChunkData::Data,
|
||||
},
|
||||
data: b"\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
|
||||
},
|
||||
TestChunk {
|
||||
chunk: Chunk {
|
||||
bounds: ChunkBounds { start: 4, end: 5 },
|
||||
data: ChunkData::Hole,
|
||||
},
|
||||
data: b"",
|
||||
},
|
||||
];
|
||||
let sha512 = [
|
||||
0xee, 0x07, 0xc5, 0x4d, 0x85, 0xee, 0x69, 0x91, 0x61, 0x07, 0x10, 0xed, 0xec, 0x13, 0x5e,
|
||||
0xfb, 0xc3, 0x7d, 0xcf, 0x1f, 0x2a, 0x13, 0xf0, 0xb6, 0x85, 0xb4, 0xee, 0xe9, 0xd7, 0xa1,
|
||||
0x12, 0x79, 0x14, 0x16, 0x30, 0x7a, 0x81, 0xf9, 0x4f, 0x72, 0xb2, 0xdd, 0x33, 0xbe, 0x5d,
|
||||
0x55, 0x70, 0xa9, 0xe3, 0x94, 0x29, 0x40, 0x29, 0x8f, 0x35, 0x23, 0xf8, 0x78, 0x7f, 0xfe,
|
||||
0xd6, 0x4b, 0x60, 0x16,
|
||||
];
|
||||
|
||||
round_trip(block_size, file_crc32, &test_chunks, &sha512);
|
||||
}
|
||||
@@ -33,6 +33,7 @@ allow = [
|
||||
"Apache-2.0",
|
||||
"Apache-2.0 WITH LLVM-exception",
|
||||
"BSD-3-Clause",
|
||||
"CC0-1.0",
|
||||
"GPL-3.0",
|
||||
"ISC",
|
||||
"MIT",
|
||||
@@ -62,8 +63,6 @@ include-workspace = true
|
||||
bypass = [
|
||||
# Copies of unmodified crashwrangler objects for old macOS versions.
|
||||
{ name = "honggfuzz", allow-globs = ["honggfuzz/third_party/mac/CrashReport_*.o"] },
|
||||
# Test files for liblzma's test suite
|
||||
{ name = "liblzma-sys", allow-globs = ["xz/tests/compress_prepared_bcj_*"] },
|
||||
]
|
||||
|
||||
[sources]
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@ clap = { version = "4.4.1", features = ["derive"] }
|
||||
ctrlc = "3.4.0"
|
||||
hex = { version = "0.4.3", features = ["serde"] }
|
||||
ring = "0.17.0"
|
||||
rsa = "0.9.6"
|
||||
rsa = { version = "0.9.6", features = ["hazmat"] }
|
||||
serde = { version = "1.0.188", features = ["derive"] }
|
||||
tempfile = "3.8.0"
|
||||
toml_edit = { version = "0.22.9", features = ["serde"] }
|
||||
|
||||
+40
-12
@@ -12,6 +12,9 @@ security_patch_level = "2024-01-01"
|
||||
# Google Pixel 7 Pro
|
||||
# What's unique: init_boot (boot v4) + vendor_boot (vendor v4)
|
||||
|
||||
[profile.pixel_v4_gki]
|
||||
vabc_algo = "Lz4"
|
||||
|
||||
[profile.pixel_v4_gki.partitions.boot]
|
||||
avb.signed = true
|
||||
data.type = "boot"
|
||||
@@ -45,13 +48,20 @@ data.type = "boot"
|
||||
data.version = "vendor_v4"
|
||||
data.ramdisks = [["otacerts", "first_stage", "dsu_key_dir"]]
|
||||
|
||||
[profile.pixel_v4_gki.hashes]
|
||||
original = "24a0a62cc08b96563f4872aee2fdd4a84d1a977b55c326dd9d4ddd92a1d326ea"
|
||||
patched = "29889670efea78bace221742b19e8ace88b67137fc2f46dcd9dbdf67e4e42267"
|
||||
[profile.pixel_v4_gki.hashes_streaming]
|
||||
original = "c00f891f941f3dddb28966f7b07f3acea773bee104dace82b37c2d1341f09422"
|
||||
patched = "ce9d8ee97828d233809742a5d3f23aa27b042675b1935ca9e3df0592c55788fd"
|
||||
|
||||
[profile.pixel_v4_gki.hashes_seekable]
|
||||
original = "96a6c366b5de1c3b10d4d6cb4ca503c83ac4cd9ca952a965cceb041990ba7022"
|
||||
patched = "e7b4609ba7a23609211dcae143bc43f091f286fbbb3a9301c02ee25614d35deb"
|
||||
|
||||
# Google Pixel 6a
|
||||
# What's unique: boot (boot v4, no ramdisk) + vendor_boot (vendor v4, 2 ramdisks)
|
||||
|
||||
[profile.pixel_v4_non_gki]
|
||||
vabc_algo = "Lz4"
|
||||
|
||||
[profile.pixel_v4_non_gki.partitions.boot]
|
||||
avb.signed = true
|
||||
data.type = "boot"
|
||||
@@ -79,13 +89,20 @@ data.type = "boot"
|
||||
data.version = "vendor_v4"
|
||||
data.ramdisks = [["init", "otacerts", "first_stage", "dsu_key_dir"], ["dlkm"]]
|
||||
|
||||
[profile.pixel_v4_non_gki.hashes]
|
||||
original = "0e7d0924a68d46e00abe96abfa0e5f3a98d5d15a32bef7401b91fca9a19748e8"
|
||||
patched = "1a2f53d9ac3a1da75e5e7502110bda437c5a725764f16656c564d42460136279"
|
||||
[profile.pixel_v4_non_gki.hashes_streaming]
|
||||
original = "4d692bc777b568b0626d3c08d2e6f83f1b472db5ad903486daaec6a78d0cc26e"
|
||||
patched = "e27673e4f30933710c11d51f0e73849068cbe9bc9f54e6076bdd93f9a5c8ea0a"
|
||||
|
||||
[profile.pixel_v4_non_gki.hashes_seekable]
|
||||
original = "ea27ecd9718c17b63400b2548680bb3cee93ce63b4fc44ff9654ca0d9c5372a8"
|
||||
patched = "3456b14e014cf565a808a9e834d9105a23539f07b2c460db19c9384aadbc3b93"
|
||||
|
||||
# Google Pixel 4a 5G
|
||||
# What's unique: boot (boot v3) + vendor_boot (vendor v3)
|
||||
|
||||
[profile.pixel_v3]
|
||||
vabc_algo = "Lz4"
|
||||
|
||||
[profile.pixel_v3.partitions.boot]
|
||||
avb.signed = true
|
||||
data.type = "boot"
|
||||
@@ -114,13 +131,20 @@ data.type = "boot"
|
||||
data.version = "vendor_v3"
|
||||
data.ramdisks = [["otacerts", "first_stage", "dsu_key_dir"]]
|
||||
|
||||
[profile.pixel_v3.hashes]
|
||||
original = "533e6f233cb98c98c945044c2ee81a6069e66baee6f7dbcfbf7523795a11215e"
|
||||
patched = "1eeae9dba0302c2d469bd03c8bccdc0469c171204dbd676a20cb6620d2d11c5c"
|
||||
[profile.pixel_v3.hashes_streaming]
|
||||
original = "f432dc7931520feb238474aa707dd5299747562ffe6129f3f763b5f11ac473ab"
|
||||
patched = "3850a2e73bd783a1ec4a70c59f37d2374e017c20df7ab4b591182b14d187c18e"
|
||||
|
||||
[profile.pixel_v3.hashes_seekable]
|
||||
original = "7d29ecc6780953c22052a576b8dc85066c8667a875e918a786a08ff4545b47d1"
|
||||
patched = "9f6342940b7cfbeb27b0567f006bb35cbee910ef038ec535403c662d5252ca71"
|
||||
|
||||
# Google Pixel 4a
|
||||
# What's unique: boot (boot v2)
|
||||
|
||||
[profile.pixel_v2]
|
||||
vabc_algo = "Gzip"
|
||||
|
||||
[profile.pixel_v2.partitions.boot]
|
||||
avb.signed = false
|
||||
data.type = "boot"
|
||||
@@ -143,6 +167,10 @@ avb.signed = true
|
||||
data.type = "vbmeta"
|
||||
data.deps = ["system"]
|
||||
|
||||
[profile.pixel_v2.hashes]
|
||||
original = "958dfa428abd2901178b90147903d7857ed78d6c016f6cb3af30d024a22a8f9a"
|
||||
patched = "b0d18ef350ca7b6499b7de4b0182f4ac3be7288ad238ce888708643e750f7631"
|
||||
[profile.pixel_v2.hashes_streaming]
|
||||
original = "1b45235b58054009cc496f6c3ee11d3dc16ed5c388c861761e26a6fce83103a0"
|
||||
patched = "193b2dc70dd465d686f35c7b7f74d2cc1b06a55e48cf5c2e4df0f667e03032fc"
|
||||
|
||||
[profile.pixel_v2.hashes_seekable]
|
||||
original = "52284308fae10cbaf09ade14e92f3bbe6149751a42bff15432982fcef8d890ab"
|
||||
patched = "7ad74ac87ddcaf34938017e6149a646041d70926e31ecda93e156e9397467b3b"
|
||||
|
||||
+30
-5
@@ -3,10 +3,11 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::{ffi::OsString, path::PathBuf};
|
||||
|
||||
use avbroot::cli::args::{LogFormat, LogLevel};
|
||||
use clap::{Args, Parser, Subcommand};
|
||||
use avbroot::cli::args::LogFormat;
|
||||
use clap::{Args, Parser, Subcommand, ValueEnum};
|
||||
use tracing::Level;
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct ProfileGroup {
|
||||
@@ -69,10 +70,34 @@ pub struct Cli {
|
||||
pub command: Command,
|
||||
|
||||
/// Lowest log message severity to output.
|
||||
#[arg(long, global = true, value_name = "LEVEL", default_value_t)]
|
||||
pub log_level: LogLevel,
|
||||
#[arg(long, global = true, value_name = "LEVEL", default_value_t = Level::INFO)]
|
||||
pub log_level: Level,
|
||||
|
||||
/// Output format for log messages.
|
||||
#[arg(long, global = true, value_name = "FORMAT", default_value_t = LogFormat::Medium)]
|
||||
pub log_format: LogFormat,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
|
||||
pub enum PassSource {
|
||||
Env,
|
||||
File,
|
||||
}
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
pub struct HelperCli {
|
||||
/// Signature algorithm.
|
||||
pub algorithm: String,
|
||||
|
||||
/// Public key.
|
||||
#[arg(value_name = "FILE", value_parser)]
|
||||
pub public_key: PathBuf,
|
||||
|
||||
/// Non-interactive password source.
|
||||
#[arg(value_name = "SOURCE")]
|
||||
pub pass_source: PassSource,
|
||||
|
||||
/// Non-interactive password source value.
|
||||
#[arg(value_name = "VALUE", value_parser)]
|
||||
pub pass_source_value: OsString,
|
||||
}
|
||||
|
||||
+4
-1
@@ -6,6 +6,7 @@
|
||||
use std::{collections::BTreeMap, fs, path::Path};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use avbroot::format::payload::VabcAlgo;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use toml_edit::DocumentMut;
|
||||
|
||||
@@ -109,8 +110,10 @@ pub struct Partition {
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Profile {
|
||||
pub vabc_algo: Option<VabcAlgo>,
|
||||
pub partitions: BTreeMap<String, Partition>,
|
||||
pub hashes: Hashes,
|
||||
pub hashes_streaming: Hashes,
|
||||
pub hashes_seekable: Hashes,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
|
||||
+351
-127
@@ -9,10 +9,12 @@ mod config;
|
||||
|
||||
use std::{
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
ffi::OsStr,
|
||||
fs::{self, File},
|
||||
io::{self, BufReader, BufWriter, Cursor, Seek, SeekFrom, Write},
|
||||
path::Path,
|
||||
env,
|
||||
ffi::{OsStr, OsString},
|
||||
fs::{self, File, OpenOptions},
|
||||
io::{self, BufReader, Cursor, Read, Seek, SeekFrom, Write},
|
||||
path::{Path, PathBuf},
|
||||
slice,
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
@@ -22,7 +24,7 @@ use std::{
|
||||
use anyhow::{anyhow, bail, Context, Result};
|
||||
use avbroot::{
|
||||
cli::ota::{ExtractCli, PatchCli, VerifyCli},
|
||||
crypto::{self, PassphraseSource},
|
||||
crypto::{self, PassphraseSource, RsaSigningKey},
|
||||
format::{
|
||||
avb::{
|
||||
self, AlgorithmType, ChainPartitionDescriptor, Descriptor, Footer, HashDescriptor,
|
||||
@@ -32,9 +34,9 @@ use avbroot::{
|
||||
self, BootImage, BootImageV0Through2, BootImageV3Through4, RamdiskMeta, V1Extra,
|
||||
V2Extra, V4Extra, VendorBootImageV3Through4, VendorV4Extra,
|
||||
},
|
||||
compression::{CompressedFormat, CompressedWriter},
|
||||
compression::{CompressedFormat, CompressedReader, CompressedWriter},
|
||||
cpio::{self, CpioEntry, CpioEntryData},
|
||||
ota::{self, SigningWriter, ZipEntry},
|
||||
ota::{self, SigningWriter, ZipEntry, ZipMode},
|
||||
padding,
|
||||
payload::{self, PayloadHeader, PayloadWriter},
|
||||
},
|
||||
@@ -45,18 +47,18 @@ use avbroot::{
|
||||
DeltaArchiveManifest, DynamicPartitionGroup, DynamicPartitionMetadata, PartitionUpdate,
|
||||
},
|
||||
},
|
||||
stream::{self, CountingWriter, HashingReader, PSeekFile, Reopen, ToWriter},
|
||||
stream::{self, CountingWriter, FromReader, HashingReader, PSeekFile, Reopen, ToWriter},
|
||||
};
|
||||
use clap::Parser;
|
||||
use rsa::RsaPrivateKey;
|
||||
use tempfile::{NamedTempFile, TempDir};
|
||||
use rsa::{rand_core::OsRng, traits::PublicKeyParts, BigUint};
|
||||
use tempfile::TempDir;
|
||||
use topological_sort::TopologicalSort;
|
||||
use tracing::{info, info_span};
|
||||
use x509_cert::Certificate;
|
||||
use zip::{write::FileOptions, CompressionMethod, ZipWriter};
|
||||
|
||||
use crate::{
|
||||
cli::{Cli, Command, ListCli, ProfileGroup, TestCli},
|
||||
cli::{Cli, Command, HelperCli, ListCli, PassSource, ProfileGroup, TestCli},
|
||||
config::{
|
||||
Avb, BootData, BootVersion, Config, Data, DmVerityContent, DmVerityData, OtaInfo,
|
||||
Partition, Profile, RamdiskContent, VbmetaData,
|
||||
@@ -100,7 +102,7 @@ fn append_avb(
|
||||
avb: &Avb,
|
||||
hash_tree: bool,
|
||||
ota_info: &OtaInfo,
|
||||
key_avb: &RsaPrivateKey,
|
||||
key_avb: &RsaSigningKey,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<()> {
|
||||
let image_size = file.seek(SeekFrom::End(0))?;
|
||||
@@ -298,7 +300,7 @@ fn create_boot_image(
|
||||
avb: &Avb,
|
||||
boot_data: &BootData,
|
||||
ota_info: &OtaInfo,
|
||||
key_avb: &RsaPrivateKey,
|
||||
key_avb: &RsaSigningKey,
|
||||
cert_ota: &Certificate,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<()> {
|
||||
@@ -425,7 +427,7 @@ fn create_dm_verity_image(
|
||||
avb: &Avb,
|
||||
dm_verity_data: &DmVerityData,
|
||||
ota_info: &OtaInfo,
|
||||
key_avb: &RsaPrivateKey,
|
||||
key_avb: &RsaSigningKey,
|
||||
cert_ota: &Certificate,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<()> {
|
||||
@@ -454,7 +456,7 @@ fn create_vbmeta_image(
|
||||
avb: &Avb,
|
||||
vbmeta_data: &VbmetaData,
|
||||
inputs: &BTreeMap<String, PSeekFile>,
|
||||
key: &RsaPrivateKey,
|
||||
key: &RsaSigningKey,
|
||||
) -> Result<()> {
|
||||
let mut descriptors = Vec::new();
|
||||
|
||||
@@ -506,7 +508,7 @@ fn create_vbmeta_image(
|
||||
fn create_partition_images(
|
||||
partitions: &BTreeMap<String, Partition>,
|
||||
ota_info: &OtaInfo,
|
||||
key_avb: &RsaPrivateKey,
|
||||
key_avb: &RsaSigningKey,
|
||||
cert_ota: &Certificate,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<BTreeMap<String, PSeekFile>> {
|
||||
@@ -576,7 +578,8 @@ fn create_payload(
|
||||
partitions: &BTreeMap<String, Partition>,
|
||||
inputs: &BTreeMap<String, PSeekFile>,
|
||||
ota_info: &OtaInfo,
|
||||
key_ota: &RsaPrivateKey,
|
||||
profile: &Profile,
|
||||
key_ota: &RsaSigningKey,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<(String, u64)> {
|
||||
let dynamic_partitions_names = partitions
|
||||
@@ -593,8 +596,14 @@ fn create_payload(
|
||||
.map(PSeekFile::new)
|
||||
.with_context(|| format!("Failed to create temp file for: {name}"))?;
|
||||
|
||||
let (partition_info, operations) =
|
||||
payload::compress_image(file, &writer, name, 4096, cancel_signal)?;
|
||||
let vabc_algo = if dynamic_partitions_names.contains(name) {
|
||||
profile.vabc_algo
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let (partition_info, operations, cow_estimate) =
|
||||
payload::compress_image(file, &writer, name, 4096, vabc_algo, cancel_signal)?;
|
||||
|
||||
compressed.insert(name, writer);
|
||||
|
||||
@@ -617,7 +626,7 @@ fn create_payload(
|
||||
fec_roots: None,
|
||||
version: None,
|
||||
merge_operations: vec![],
|
||||
estimate_cow_size: None,
|
||||
estimate_cow_size: cow_estimate,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -638,7 +647,7 @@ fn create_payload(
|
||||
}],
|
||||
snapshot_enabled: Some(true),
|
||||
vabc_enabled: Some(true),
|
||||
vabc_compression_param: Some("gz".to_owned()),
|
||||
vabc_compression_param: profile.vabc_algo.map(|a| a.to_string()),
|
||||
cow_version: Some(2),
|
||||
vabc_feature_set: None,
|
||||
}),
|
||||
@@ -681,22 +690,26 @@ fn create_payload(
|
||||
.with_context(|| format!("Failed to copy from image: {name}"))?;
|
||||
}
|
||||
|
||||
let (_, properties, metadata_size) = payload_writer
|
||||
let (_, _, properties, metadata_size) = payload_writer
|
||||
.finish()
|
||||
.context("Failed to finalize payload")?;
|
||||
|
||||
Ok((properties, metadata_size))
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn create_ota(
|
||||
output: &Path,
|
||||
ota_info: &OtaInfo,
|
||||
profile: &Profile,
|
||||
key_avb: &RsaPrivateKey,
|
||||
key_ota: &RsaPrivateKey,
|
||||
zip_mode: ZipMode,
|
||||
key_avb: &RsaSigningKey,
|
||||
key_ota: &RsaSigningKey,
|
||||
cert_ota: &Certificate,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<()> {
|
||||
info!("Generating OTA: {output:?}");
|
||||
|
||||
let inputs = create_partition_images(
|
||||
&profile.partitions,
|
||||
ota_info,
|
||||
@@ -705,11 +718,23 @@ fn create_ota(
|
||||
cancel_signal,
|
||||
)?;
|
||||
|
||||
let raw_writer =
|
||||
File::create(output).with_context(|| format!("Failed to open for writing: {output:?}"))?;
|
||||
let buffered_writer = BufWriter::new(raw_writer);
|
||||
let signing_writer = SigningWriter::new(buffered_writer);
|
||||
let mut zip_writer = ZipWriter::new_streaming(signing_writer);
|
||||
let raw_writer = OpenOptions::new()
|
||||
.read(zip_mode == ZipMode::Seekable)
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(output)
|
||||
.with_context(|| format!("Failed to open for writing: {output:?}"))?;
|
||||
let mut zip_writer = match zip_mode {
|
||||
ZipMode::Streaming => {
|
||||
let signing_writer = SigningWriter::new_streaming(raw_writer);
|
||||
ZipWriter::new_streaming(signing_writer)
|
||||
}
|
||||
ZipMode::Seekable => {
|
||||
let signing_writer = SigningWriter::new_seekable(raw_writer);
|
||||
ZipWriter::new(signing_writer)
|
||||
}
|
||||
};
|
||||
let options = FileOptions::default()
|
||||
.compression_method(CompressionMethod::Stored)
|
||||
.large_file(false);
|
||||
@@ -739,6 +764,7 @@ fn create_ota(
|
||||
&profile.partitions,
|
||||
&inputs,
|
||||
ota_info,
|
||||
profile,
|
||||
key_ota,
|
||||
cancel_signal,
|
||||
)
|
||||
@@ -793,13 +819,18 @@ fn create_ota(
|
||||
spl_downgrade: false,
|
||||
};
|
||||
|
||||
let data_descriptor_size = match zip_mode {
|
||||
ZipMode::Streaming => 16,
|
||||
ZipMode::Seekable => 0,
|
||||
};
|
||||
ota::add_metadata(
|
||||
&entries,
|
||||
&mut zip_writer,
|
||||
// Offset where next entry would begin.
|
||||
entries.last().map(|e| e.offset + e.size).unwrap() + 16,
|
||||
entries.last().map(|e| e.offset + e.size).unwrap() + data_descriptor_size,
|
||||
&metadata,
|
||||
payload_metadata_size.unwrap(),
|
||||
zip_mode,
|
||||
)
|
||||
.context("Failed to write new OTA metadata")?;
|
||||
|
||||
@@ -807,7 +838,7 @@ fn create_ota(
|
||||
.finish()
|
||||
.context("Failed to finalize output zip")?;
|
||||
let mut buffered_writer = signing_writer
|
||||
.finish(key_ota, cert_ota)
|
||||
.finish(key_ota, cert_ota, cancel_signal)
|
||||
.context("Failed to sign output zip")?;
|
||||
buffered_writer
|
||||
.flush()
|
||||
@@ -823,12 +854,16 @@ fn create_fake_magisk(output: &Path) -> Result<()> {
|
||||
|
||||
for path in [
|
||||
"assets/stub.apk",
|
||||
"lib/arm64-v8a/libinit-ld.so",
|
||||
"lib/arm64-v8a/libmagisk64.so",
|
||||
"lib/arm64-v8a/libmagiskinit.so",
|
||||
"lib/armeabi-v7a/libinit-ld.so",
|
||||
"lib/armeabi-v7a/libmagisk32.so",
|
||||
"lib/armeabi-v7a/libmagiskinit.so",
|
||||
"lib/x86/libinit-ld.so",
|
||||
"lib/x86/libmagisk32.so",
|
||||
"lib/x86/libmagiskinit.so",
|
||||
"lib/x86_64/libinit-ld.so",
|
||||
"lib/x86_64/libmagisk64.so",
|
||||
"lib/x86_64/libmagiskinit.so",
|
||||
] {
|
||||
@@ -844,15 +879,18 @@ fn create_fake_magisk(output: &Path) -> Result<()> {
|
||||
}
|
||||
|
||||
struct KeySet {
|
||||
avb_key: RsaPrivateKey,
|
||||
ota_key: RsaPrivateKey,
|
||||
avb_key: RsaSigningKey,
|
||||
ota_key: RsaSigningKey,
|
||||
ota_cert: Certificate,
|
||||
avb_key_file: NamedTempFile,
|
||||
avb_pass_file: NamedTempFile,
|
||||
avb_pkmd_file: NamedTempFile,
|
||||
ota_key_file: NamedTempFile,
|
||||
ota_pass_file: NamedTempFile,
|
||||
ota_cert_file: NamedTempFile,
|
||||
_key_dir: TempDir,
|
||||
avb_key_file: PathBuf,
|
||||
avb_public_key_file: PathBuf,
|
||||
avb_pass_file: PathBuf,
|
||||
avb_pkmd_file: PathBuf,
|
||||
ota_key_file: PathBuf,
|
||||
ota_public_key_file: PathBuf,
|
||||
ota_pass_file: PathBuf,
|
||||
ota_cert_file: PathBuf,
|
||||
}
|
||||
|
||||
macro_rules! new_keys_with_prefix {
|
||||
@@ -901,13 +939,8 @@ macro_rules! new_keys_with_prefix {
|
||||
}
|
||||
|
||||
impl KeySet {
|
||||
fn write_temp(data: &[u8]) -> Result<NamedTempFile> {
|
||||
let mut temp_file =
|
||||
NamedTempFile::new().context("Failed to create temp file for test keys")?;
|
||||
temp_file
|
||||
.write_all(data)
|
||||
.with_context(|| format!("Failed to write test key data: {:?}", temp_file.path()))?;
|
||||
Ok(temp_file)
|
||||
fn write(path: &Path, data: &[u8]) -> Result<()> {
|
||||
fs::write(path, data).with_context(|| format!("Failed to write test key data: {path:?}"))
|
||||
}
|
||||
|
||||
fn new_with_data(
|
||||
@@ -918,36 +951,54 @@ impl KeySet {
|
||||
ota_pass: &[u8],
|
||||
ota_cert: &[u8],
|
||||
) -> Result<Self> {
|
||||
let avb_key_file = Self::write_temp(avb_key)?;
|
||||
let avb_pass_file = Self::write_temp(avb_pass)?;
|
||||
let avb_pkmd_file = Self::write_temp(avb_pkmd)?;
|
||||
let ota_key_file = Self::write_temp(ota_key)?;
|
||||
let ota_pass_file = Self::write_temp(ota_pass)?;
|
||||
let ota_cert_file = Self::write_temp(ota_cert)?;
|
||||
let key_dir = TempDir::new().context("Failed to create temp directory")?;
|
||||
let avb_key_file = key_dir.path().join("avb.key");
|
||||
let avb_public_key_file = key_dir.path().join("avb.public.key");
|
||||
let avb_pass_file = key_dir.path().join("avb.passphrase");
|
||||
let avb_pkmd_file = key_dir.path().join("avb_pkmd.bin");
|
||||
let ota_key_file = key_dir.path().join("ota.key");
|
||||
let ota_public_key_file = key_dir.path().join("ota.public.key");
|
||||
let ota_pass_file = key_dir.path().join("ota.passphrase");
|
||||
let ota_cert_file = key_dir.path().join("ota.crt");
|
||||
|
||||
Self::write(&avb_key_file, avb_key)?;
|
||||
Self::write(&avb_pass_file, avb_pass)?;
|
||||
Self::write(&avb_pkmd_file, avb_pkmd)?;
|
||||
Self::write(&ota_key_file, ota_key)?;
|
||||
Self::write(&ota_pass_file, ota_pass)?;
|
||||
Self::write(&ota_cert_file, ota_cert)?;
|
||||
|
||||
let avb_key = crypto::read_pem_key_file(
|
||||
avb_key_file.path(),
|
||||
&PassphraseSource::File(avb_pass_file.path().to_owned()),
|
||||
&avb_key_file,
|
||||
&PassphraseSource::File(avb_pass_file.clone()),
|
||||
)
|
||||
.map(RsaSigningKey::Internal)
|
||||
.context("Failed to load AVB test key")?;
|
||||
|
||||
let ota_key = crypto::read_pem_key_file(
|
||||
ota_key_file.path(),
|
||||
&PassphraseSource::File(ota_pass_file.path().to_owned()),
|
||||
&ota_key_file,
|
||||
&PassphraseSource::File(ota_pass_file.clone()),
|
||||
)
|
||||
.map(RsaSigningKey::Internal)
|
||||
.context("Failed to load OTA test key")?;
|
||||
|
||||
let ota_cert = crypto::read_pem_cert_file(ota_cert_file.path())
|
||||
.context("Failed to load OTA test cert")?;
|
||||
crypto::write_pem_public_key_file(&avb_public_key_file, &avb_key.to_public_key())?;
|
||||
crypto::write_pem_public_key_file(&ota_public_key_file, &ota_key.to_public_key())?;
|
||||
|
||||
let ota_cert =
|
||||
crypto::read_pem_cert_file(&ota_cert_file).context("Failed to load OTA test cert")?;
|
||||
|
||||
Ok(Self {
|
||||
avb_key,
|
||||
ota_key,
|
||||
ota_cert,
|
||||
_key_dir: key_dir,
|
||||
avb_key_file,
|
||||
avb_public_key_file,
|
||||
avb_pass_file,
|
||||
avb_pkmd_file,
|
||||
ota_key_file,
|
||||
ota_public_key_file,
|
||||
ota_pass_file,
|
||||
ota_cert_file,
|
||||
})
|
||||
@@ -957,15 +1008,27 @@ impl KeySet {
|
||||
new_keys_with_prefix!(new_for_test, "TEST_KEY_DO_NOT_USE_");
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn patch_image(
|
||||
input_file: &Path,
|
||||
output_file: &Path,
|
||||
system_image_file: &Path,
|
||||
zip_mode: ZipMode,
|
||||
extra_args: &[&OsStr],
|
||||
keys: &KeySet,
|
||||
signing_helper: bool,
|
||||
cancel_signal: &AtomicBool,
|
||||
) -> Result<()> {
|
||||
info!("Patching OTA: {input_file:?} -> {output_file:?}");
|
||||
|
||||
let (avb_key_file, ota_key_file) = if signing_helper {
|
||||
(&keys.avb_public_key_file, &keys.ota_public_key_file)
|
||||
} else {
|
||||
(&keys.avb_key_file, &keys.ota_key_file)
|
||||
};
|
||||
|
||||
let zip_mode_arg = zip_mode.to_string();
|
||||
|
||||
// We're intentionally using the CLI interface.
|
||||
let mut args: Vec<&OsStr> = vec![
|
||||
OsStr::new("patch"),
|
||||
@@ -973,18 +1036,31 @@ fn patch_image(
|
||||
input_file.as_os_str(),
|
||||
OsStr::new("--output"),
|
||||
output_file.as_os_str(),
|
||||
OsStr::new("--replace"),
|
||||
OsStr::new("system"),
|
||||
system_image_file.as_os_str(),
|
||||
OsStr::new("--key-avb"),
|
||||
keys.avb_key_file.path().as_os_str(),
|
||||
avb_key_file.as_os_str(),
|
||||
OsStr::new("--pass-avb-file"),
|
||||
keys.avb_pass_file.path().as_os_str(),
|
||||
keys.avb_pass_file.as_os_str(),
|
||||
OsStr::new("--key-ota"),
|
||||
keys.ota_key_file.path().as_os_str(),
|
||||
ota_key_file.as_os_str(),
|
||||
OsStr::new("--pass-ota-file"),
|
||||
keys.ota_pass_file.path().as_os_str(),
|
||||
keys.ota_pass_file.as_os_str(),
|
||||
OsStr::new("--cert-ota"),
|
||||
keys.ota_cert_file.path().as_os_str(),
|
||||
keys.ota_cert_file.as_os_str(),
|
||||
OsStr::new("--dsu"),
|
||||
OsStr::new("--zip-mode"),
|
||||
OsStr::new(&zip_mode_arg),
|
||||
];
|
||||
|
||||
let argv0: OsString;
|
||||
if signing_helper {
|
||||
argv0 = env::args_os().next().unwrap();
|
||||
args.push(OsStr::new("--signing-helper"));
|
||||
args.push(&argv0);
|
||||
}
|
||||
|
||||
args.extend_from_slice(extra_args);
|
||||
|
||||
let cli = PatchCli::try_parse_from(args)?;
|
||||
@@ -1016,15 +1092,65 @@ fn verify_image(input_file: &Path, keys: &KeySet, cancel_signal: &AtomicBool) ->
|
||||
OsStr::new("--input"),
|
||||
input_file.as_os_str(),
|
||||
OsStr::new("--public-key-avb"),
|
||||
keys.avb_pkmd_file.path().as_os_str(),
|
||||
keys.avb_pkmd_file.as_os_str(),
|
||||
OsStr::new("--cert-ota"),
|
||||
keys.ota_cert_file.path().as_os_str(),
|
||||
keys.ota_cert_file.as_os_str(),
|
||||
])?;
|
||||
avbroot::cli::ota::verify_subcommand(&cli, cancel_signal)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn clean_boot_image_certs(path: &Path, cancel_signal: &AtomicBool) -> Result<()> {
|
||||
info!("Removing OTA certs and DSU public keys: {path:?}");
|
||||
|
||||
let mut file = OpenOptions::new().read(true).write(true).open(path)?;
|
||||
let mut boot_image = BootImage::from_reader(&mut file)?;
|
||||
|
||||
let ramdisks = match &mut boot_image {
|
||||
BootImage::V0Through2(b) => slice::from_mut(&mut b.ramdisk),
|
||||
BootImage::V3Through4(b) => slice::from_mut(&mut b.ramdisk),
|
||||
BootImage::VendorV3Through4(b) => &mut b.ramdisks,
|
||||
};
|
||||
|
||||
if ramdisks.is_empty() {
|
||||
bail!("No ramdisk found: {path:?}");
|
||||
}
|
||||
|
||||
let raw_ramdisk_reader = Cursor::new(&ramdisks[0]);
|
||||
let ramdisk_reader = CompressedReader::new(raw_ramdisk_reader, false)?;
|
||||
let ramdisk_format = ramdisk_reader.format();
|
||||
let mut entries = cpio::load(ramdisk_reader, false, cancel_signal)?;
|
||||
|
||||
// Wipe out OTA certificates.
|
||||
if let Some(entry) = entries
|
||||
.iter_mut()
|
||||
.find(|e| e.path == b"system/etc/security/otacerts.zip")
|
||||
{
|
||||
let mut zip_writer = ZipWriter::new(Cursor::new(Vec::new()));
|
||||
let empty_zip = zip_writer.finish()?.into_inner();
|
||||
|
||||
entry.data = CpioEntryData::Data(empty_zip);
|
||||
}
|
||||
|
||||
// Wipe out DSU public keys.
|
||||
entries.retain(|e| !e.path.starts_with(b"first_stage_ramdisk/avb/"));
|
||||
|
||||
let raw_ramdisk_writer = Cursor::new(Vec::new());
|
||||
let mut ramdisk_writer = CompressedWriter::new(raw_ramdisk_writer, ramdisk_format)?;
|
||||
cpio::save(&mut ramdisk_writer, &entries, false, cancel_signal)?;
|
||||
|
||||
let raw_ramdisk_writer = ramdisk_writer.finish()?;
|
||||
ramdisks[0] = raw_ramdisk_writer.into_inner();
|
||||
|
||||
// We're creating an input for --prepatched, so no need for AVB metadata.
|
||||
file.set_len(0)?;
|
||||
file.rewind()?;
|
||||
boot_image.to_writer(&mut file)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn filter_profiles<'a>(config: &'a Config, cli: &'a ProfileGroup) -> Result<BTreeSet<&'a str>> {
|
||||
let mut profiles = config
|
||||
.profile
|
||||
@@ -1082,84 +1208,102 @@ fn test_subcommand(cli: &TestCli, cancel_signal: &AtomicBool) -> Result<()> {
|
||||
];
|
||||
|
||||
for name in profiles {
|
||||
let _span = info_span!("profile", name).entered();
|
||||
|
||||
if Path::new(name).file_name() != Some(OsStr::new(name)) {
|
||||
bail!("Unsafe profile name: {name}");
|
||||
}
|
||||
|
||||
info!("Generating OTA");
|
||||
|
||||
let profile = &config.profile[name];
|
||||
|
||||
// Can't used NamedTempFile because avbroot does atomic replaces.
|
||||
let profile_dir = work_dir.join(name);
|
||||
let out_original = profile_dir.join("ota.zip");
|
||||
let out_magisk = profile_dir.join("ota_magisk.zip");
|
||||
let out_prepatched = profile_dir.join("ota_prepatched.zip");
|
||||
for (zip_mode, hashes) in [
|
||||
(ZipMode::Streaming, &profile.hashes_streaming),
|
||||
(ZipMode::Seekable, &profile.hashes_seekable),
|
||||
] {
|
||||
let _span = info_span!("profile", name, %zip_mode).entered();
|
||||
|
||||
fs::create_dir_all(&profile_dir)
|
||||
.with_context(|| format!("Failed to create directory: {profile_dir:?}"))?;
|
||||
// Can't used NamedTempFile because avbroot does atomic replaces.
|
||||
let profile_dir = work_dir.join(name);
|
||||
let out_original = profile_dir.join("ota.zip");
|
||||
let out_magisk = profile_dir.join("ota_magisk.zip");
|
||||
let out_prepatched = profile_dir.join("ota_prepatched.zip");
|
||||
|
||||
create_ota(
|
||||
&out_original,
|
||||
&config.ota_info,
|
||||
profile,
|
||||
&orig_keys.avb_key,
|
||||
&orig_keys.ota_key,
|
||||
&orig_keys.ota_cert,
|
||||
cancel_signal,
|
||||
)
|
||||
.with_context(|| format!("[{name}] Failed to create OTA"))?;
|
||||
fs::create_dir_all(&profile_dir)
|
||||
.with_context(|| format!("Failed to create directory: {profile_dir:?}"))?;
|
||||
|
||||
verify_image(&out_original, &orig_keys, cancel_signal)
|
||||
.with_context(|| format!("[{name}] Failed to verify original OTA"))?;
|
||||
create_ota(
|
||||
&out_original,
|
||||
&config.ota_info,
|
||||
profile,
|
||||
zip_mode,
|
||||
&orig_keys.avb_key,
|
||||
&orig_keys.ota_key,
|
||||
&orig_keys.ota_cert,
|
||||
cancel_signal,
|
||||
)
|
||||
.with_context(|| format!("[{name}] Failed to create OTA"))?;
|
||||
|
||||
verify_hash(&out_original, &profile.hashes.original.0, cancel_signal)
|
||||
.with_context(|| format!("[{name}] Failed to verify original OTA hash"))?;
|
||||
verify_image(&out_original, &orig_keys, cancel_signal)
|
||||
.with_context(|| format!("[{name}] Failed to verify original OTA"))?;
|
||||
|
||||
// Patch once using Magisk.
|
||||
patch_image(
|
||||
&out_original,
|
||||
&out_magisk,
|
||||
&args_magisk,
|
||||
&test_keys,
|
||||
cancel_signal,
|
||||
)
|
||||
.with_context(|| format!("[{name}] Failed to patch OTA"))?;
|
||||
verify_hash(&out_original, &hashes.original.0, cancel_signal)
|
||||
.with_context(|| format!("[{name}] Failed to verify original OTA hash"))?;
|
||||
|
||||
verify_image(&out_magisk, &test_keys, cancel_signal)
|
||||
.with_context(|| format!("[{name}] Failed to verify patched OTA"))?;
|
||||
// Patch once using Magisk.
|
||||
extract_image(&out_original, &profile_dir, cancel_signal)
|
||||
.with_context(|| format!("[{name}] Failed to extract OTA"))?;
|
||||
|
||||
verify_hash(&out_magisk, &profile.hashes.patched.0, cancel_signal)
|
||||
.with_context(|| format!("[{name}] Failed to verify patched OTA hash"))?;
|
||||
let system_image = profile_dir.join("system.img");
|
||||
|
||||
// Patch again, but this time, use the previously patched boot image
|
||||
// instead of applying the Magisk patch.
|
||||
extract_image(&out_magisk, &profile_dir, cancel_signal)
|
||||
.with_context(|| format!("[{name}] Failed to extract OTA"))?;
|
||||
patch_image(
|
||||
&out_original,
|
||||
&out_magisk,
|
||||
&system_image,
|
||||
zip_mode,
|
||||
&args_magisk,
|
||||
&test_keys,
|
||||
false,
|
||||
cancel_signal,
|
||||
)
|
||||
.with_context(|| format!("[{name}] Failed to patch OTA"))?;
|
||||
|
||||
let mut magisk_image = profile_dir.join("init_boot.img");
|
||||
if !magisk_image.exists() {
|
||||
magisk_image = profile_dir.join("boot.img");
|
||||
verify_image(&out_magisk, &test_keys, cancel_signal)
|
||||
.with_context(|| format!("[{name}] Failed to verify patched OTA"))?;
|
||||
|
||||
verify_hash(&out_magisk, &hashes.patched.0, cancel_signal)
|
||||
.with_context(|| format!("[{name}] Failed to verify patched OTA hash"))?;
|
||||
|
||||
// Patch again, but this time, use the previously patched boot image
|
||||
// instead of applying the Magisk patch.
|
||||
extract_image(&out_magisk, &profile_dir, cancel_signal)
|
||||
.with_context(|| format!("[{name}] Failed to extract OTA"))?;
|
||||
|
||||
let mut magisk_image = profile_dir.join("init_boot.img");
|
||||
if !magisk_image.exists() {
|
||||
magisk_image = profile_dir.join("boot.img");
|
||||
}
|
||||
|
||||
// Wipe out changes that we expect to be made to prepatched images.
|
||||
clean_boot_image_certs(&magisk_image, cancel_signal)?;
|
||||
|
||||
let args_prepatched = [OsStr::new("--prepatched"), magisk_image.as_os_str()];
|
||||
|
||||
patch_image(
|
||||
&out_original,
|
||||
&out_prepatched,
|
||||
&system_image,
|
||||
zip_mode,
|
||||
&args_prepatched,
|
||||
&test_keys,
|
||||
true,
|
||||
cancel_signal,
|
||||
)
|
||||
.with_context(|| format!("[{name}] Failed to patch OTA"))?;
|
||||
|
||||
verify_image(&out_prepatched, &test_keys, cancel_signal)
|
||||
.with_context(|| format!("[{name}] Failed to verify patched OTA"))?;
|
||||
|
||||
verify_hash(&out_prepatched, &hashes.patched.0, cancel_signal)
|
||||
.with_context(|| format!("[{name}] Failed to verify patched OTA hash"))?;
|
||||
}
|
||||
|
||||
let args_prepatched = [OsStr::new("--prepatched"), magisk_image.as_os_str()];
|
||||
|
||||
patch_image(
|
||||
&out_original,
|
||||
&out_prepatched,
|
||||
&args_prepatched,
|
||||
&test_keys,
|
||||
cancel_signal,
|
||||
)
|
||||
.with_context(|| format!("[{name}] Failed to patch OTA"))?;
|
||||
|
||||
verify_image(&out_prepatched, &test_keys, cancel_signal)
|
||||
.with_context(|| format!("[{name}] Failed to verify patched OTA"))?;
|
||||
|
||||
verify_hash(&out_prepatched, &profile.hashes.patched.0, cancel_signal)
|
||||
.with_context(|| format!("[{name}] Failed to verify patched OTA hash"))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -1175,7 +1319,87 @@ fn list_subcommand(cli: &ListCli) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A basic --signing-helper implementation that just signs via RustCrypto.
|
||||
fn helper_mode() -> Result<()> {
|
||||
let cli = HelperCli::parse();
|
||||
|
||||
let private_key_path = {
|
||||
let parent = cli.public_key.parent().unwrap_or(Path::new("."));
|
||||
let name = cli
|
||||
.public_key
|
||||
.file_name()
|
||||
.and_then(|n| n.to_str())
|
||||
.ok_or_else(|| anyhow!("Bad filename: {:?}", cli.public_key))?;
|
||||
|
||||
parent.join(name.replace(".public", ""))
|
||||
};
|
||||
let source = match cli.pass_source {
|
||||
PassSource::Env => PassphraseSource::EnvVar(cli.pass_source_value),
|
||||
PassSource::File => PassphraseSource::File(cli.pass_source_value.into()),
|
||||
};
|
||||
let private_key = crypto::read_pem_key_file(&private_key_path, &source)
|
||||
.with_context(|| format!("Failed to load private key: {private_key_path:?}"))?;
|
||||
let public_key = crypto::read_pem_public_key_file(&cli.public_key)
|
||||
.with_context(|| format!("Failed to load public key: {:?}", cli.public_key))?;
|
||||
|
||||
if private_key.to_public_key() != public_key {
|
||||
bail!("Private key does not match public key");
|
||||
}
|
||||
|
||||
let (hash_algo, key_algo) = cli
|
||||
.algorithm
|
||||
.split_once('_')
|
||||
.ok_or_else(|| anyhow!("Unknown algorithm: {:?}", cli.algorithm))?;
|
||||
|
||||
if key_algo != format!("RSA{}", private_key.size() * 8) {
|
||||
bail!(
|
||||
"{key_algo} does not match key size ({})",
|
||||
private_key.size() * 8
|
||||
);
|
||||
} else if hash_algo != "SHA256" && hash_algo != "SHA512" {
|
||||
bail!("Unknown hash algorithm: {hash_algo}");
|
||||
}
|
||||
|
||||
let mut padded_digest = vec![];
|
||||
io::stdin()
|
||||
.read_to_end(&mut padded_digest)
|
||||
.context("Failed to read padded digest from stdin")?;
|
||||
|
||||
if padded_digest.len() != private_key.size() {
|
||||
bail!(
|
||||
"Padded digest size ({}) bytes does not match key size ({})",
|
||||
padded_digest.len(),
|
||||
private_key.size()
|
||||
);
|
||||
}
|
||||
|
||||
// The input is already padded, so perform a raw RSA signing operation.
|
||||
let mut signature = rsa::hazmat::rsa_decrypt_and_check(
|
||||
&private_key,
|
||||
None::<&mut OsRng>,
|
||||
&BigUint::from_bytes_be(&padded_digest),
|
||||
)
|
||||
.context("Failed to sign digest")?
|
||||
.to_bytes_le();
|
||||
signature.resize(private_key.size(), 0);
|
||||
signature.reverse();
|
||||
|
||||
io::stdout()
|
||||
.write_all(&signature)
|
||||
.context("Failed to write signature to stdout")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
const ENV_HELPER_MODE: &str = "E2E_HELPER_MODE";
|
||||
|
||||
// Re-invoking ourselves will execute as the helper script instead.
|
||||
if env::var_os(ENV_HELPER_MODE).is_some() {
|
||||
return helper_mode();
|
||||
}
|
||||
env::set_var(ENV_HELPER_MODE, "true");
|
||||
|
||||
// Set up a cancel signal so we can properly clean up any temporary files.
|
||||
let cancel_signal = Arc::new(AtomicBool::new(false));
|
||||
{
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
#[cfg(not(windows))]
|
||||
mod fuzz {
|
||||
use std::io::Cursor;
|
||||
|
||||
use avbroot::{format::lp::Metadata, stream::FromReader};
|
||||
use honggfuzz::fuzz;
|
||||
|
||||
pub fn main() {
|
||||
loop {
|
||||
fuzz!(|data: &[u8]| {
|
||||
let reader = Cursor::new(data);
|
||||
let _ = Metadata::from_reader(reader);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
#[cfg(not(windows))]
|
||||
fuzz::main();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#[cfg(not(windows))]
|
||||
mod fuzz {
|
||||
use std::io::{self, Cursor};
|
||||
|
||||
use avbroot::format::sparse::{ChunkData, CrcMode, SparseReader};
|
||||
use honggfuzz::fuzz;
|
||||
|
||||
pub fn main() {
|
||||
loop {
|
||||
fuzz!(|data: &[u8]| {
|
||||
let reader = Cursor::new(data);
|
||||
if let Ok(mut sparse_reader) = SparseReader::new(reader, CrcMode::Ignore) {
|
||||
while let Ok(Some(chunk)) = sparse_reader.next_chunk() {
|
||||
if chunk.data == ChunkData::Data {
|
||||
let _ = io::copy(&mut sparse_reader, &mut io::sink());
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
#[cfg(not(windows))]
|
||||
fuzz::main();
|
||||
}
|
||||
Reference in New Issue
Block a user