Commit Graph

69 Commits

Author SHA1 Message Date
Andrew Gunnerson 8ab265e08f Merge pull request #40 from chenxiaolong/patch
Use unittest.mock.patch for replacing classes and variables
2023-02-11 20:39:14 -05:00
Andrew Gunnerson 505ef91bc9 Use unittest.mock.patch for replacing classes and variables
The mechanism is still the same (modifies global state), but the code is
a bit cleaner.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-02-11 20:38:01 -05:00
Andrew Gunnerson d1ac305d41 Merge pull request #39 from chenxiaolong/executable
Minor quality of life improvements for avbroot.py
2023-02-11 18:16:01 -05:00
Andrew Gunnerson a709854c3a avbroot/ota.py: Improve silencing of irrelevant AOSP warning
common.py really wants sys.argv[0] to be just an executable name with no
path or extension.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-02-11 18:12:47 -05:00
Andrew Gunnerson 6a5ce3815e avbroot.py: extract: Create output directory if it doesn't already exist
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-02-11 18:12:04 -05:00
Andrew Gunnerson 2c4ab013b0 avbroot.py: Make script executable
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-02-11 18:11:12 -05:00
Andrew Gunnerson 4c21a94aa7 Merge pull request #38 from chenxiaolong/docs
Improve documentation for installing dependencies
2023-02-11 18:08:52 -05:00
Andrew Gunnerson 3c3b70bcd7 Improve documentation for installing dependencies
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-02-11 18:08:09 -05:00
Andrew Gunnerson a5384a7bfb Merge pull request #37 from chenxiaolong/boot_image_cpio2
Remove dependency on magiskboot
2023-02-11 17:34:14 -05:00
Andrew Gunnerson dc18244d12 avbroot/util.py: Atomic file replacement does not work on Windows
It's not worth adding a new dependency just to use FileRenameInfoEx
with FILE_RENAME_FLAG_POSIX_SEMANTICS.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-02-11 17:06:27 -05:00
Andrew Gunnerson 333297c7a1 avbroot/openssl.py: Pass in the passphrase via env vars on Windows
openssl does not support reading the passphrase from file descriptors on
Windows. Using an environment variable is the next best option.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-02-11 16:49:57 -05:00
Andrew Gunnerson 6c116f0dba Remove dependency on magiskboot
This commit reimplements the magiskboot patching process natively in
Python. By doing so, avbroot gains cross platform support and there's no
longer a need to execute an Android binary on a non-Android Linux system,
which had pitfalls that already had to be worked around.

Boot images and cpio archives are handled by new custom parsers added to
avbroot. Legacy lz4 compression is done by a wrapper around the python
lz4 library (a new dependency). Gzip compression is handled natively by
python. Other compression methods and OEM-specific boot image formats
are not supported because devices that use the modern Android A/B OTA
scheme do not use those.

Output files are still bit-for-bit reproducible across runs, but they
are different from what prior avbroot commits produced:

* avbroot's cpio writer follows GNU cpio and libarchive's behavior of
  setting the mode field to 0 in the trailer entry. magiskboot sets the
  mode to 0o755.
* When patching a vendor boot v4 image, the ramdisk table entries are
  now updated correctly. Prior vendor boot images were only bootable
  because:

  * the image only contained a single ramdisk
  * the single ramdisk shrunk in size, stayed the same, or grew little
    enough to not exceed a page boundary
  * the bootloader is lenient in validating boot image fields

  magiskboot does not handle vendor boot v4 images correctly, but it
  doesn't need to just for the regular root patch. avbroot made use of
  it in an unsupported way to patch vendor boot ramdisks.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-02-11 01:22:24 -05:00
Andrew Gunnerson 1cbfd96067 Merge pull request #36 from chenxiaolong/passphrase
Pass encrypted private keys to openssl directly
2023-02-09 22:22:19 -05:00
Andrew Gunnerson 17ee5ee37b Pass encrypted private keys to openssl directly
openssl supports passing the passphrase via a file descriptor, so let's
do that and avoid ever putting plaintext private keys on the filesystem,
even if it is a tmpfs.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-02-09 22:20:06 -05:00
Andrew Gunnerson d62516b2e0 Merge pull request #35 from pascallj/patch-1
Add unused return object when extracting
2023-02-07 17:29:48 -05:00
Pascal Roeleven 9618c0bc36 Add unused return object when extracting
Fixes #34
2023-02-07 22:17:04 +01:00
Andrew Gunnerson 2e27d9eb12 Merge pull request #33 from chenxiaolong/docs
README.md: Improve documentation
2023-02-06 23:42:44 -05:00
Andrew Gunnerson 6272ca573d README.md: Improve documentation
* Clarify boot image partitions. It is no longer the case that only
  `boot` and `vendor_boot` are patched.
* Describe how to check if an OTA file is A/B.
* Remove mentions of signapk now that avbroot signs zips itself.
* Minor grammar changes.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-02-06 23:39:05 -05:00
Andrew Gunnerson 2319ce8cac Merge pull request #32 from chenxiaolong/oneplus
Add support for OnePlus OTAs
2023-02-06 22:57:59 -05:00
Andrew Gunnerson 3c843856e1 Add support for OnePlus OTAs
Only OxygenOS 12 and 13 can work because older builds use the legacy
metadata file instead of the newer protobuf format.

Differences from Pixel devices:

* There's a recovery image. It contains otacerts.zip instead of
  vendor_boot or boot. The image uses a version 4 `ANDROID!` header, not
  `VNDRBOOT`.
* The payload.bin uses ZERO blocks with no sha256 checksum.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-02-06 22:52:05 -05:00
Andrew Gunnerson fe79cc852d Merge pull request #31 from chenxiaolong/typo
avbroot/util.py: Fix variable name typo in zero_n()
2023-02-03 17:45:31 -05:00
Andrew Gunnerson e687eeed24 avbroot/util.py: Fix variable name typo in zero_n()
This hasn't broken anything in the past because the Pixel OTA payloads
haven't been using ZERO or DISCARD blocks.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-02-03 17:44:01 -05:00
Andrew Gunnerson 4bd6c48b8f Merge pull request #30 from chenxiaolong/temp_dir
Stop setting default temporary directory to the output directory
2023-02-03 17:40:24 -05:00
Andrew Gunnerson 2d4ff61531 Stop setting default temporary directory to the output directory
Now that avbroot no longer needs to create huge temp files, just use the
OS's default temp directory.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-02-03 17:38:18 -05:00
Andrew Gunnerson d9bfa98ea1 Merge pull request #29 from chenxiaolong/x86_64
avbroot/boot.py: Document why the x86 magiskboot is used instead of x86_64
2023-02-03 17:34:12 -05:00
Andrew Gunnerson 0a3b7ff5f7 avbroot/boot.py: Document why the x86 magiskboot is used instead of x86_64
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-02-03 17:13:20 -05:00
Andrew Gunnerson 99dd8ba5ff Merge pull request #28 from chenxiaolong/zip_signing
ota: Reimplement zip signing ourselves more efficiently
2023-02-03 16:57:30 -05:00
Andrew Gunnerson e81f17cabc avbroot/ota.py: Ensure all protobuf serialization is deterministic
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-02-01 22:29:04 -05:00
Andrew Gunnerson 806d8117f0 Perform all OTA signing ourselves
With this commit, avbroot now performs the entire OTA signing process
itself. This removes the 4 full .zip builds needed for signing before,
which means that avbroot can now do the full patching process in a
single pass.

Since signapk is no longer needed, the java dependency has been dropped.
This also means that avbroot no longer depends on glibc-based Linux
distros, which was the case before due to signapk's conscrypt
dependency.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-02-01 22:29:01 -05:00
Andrew Gunnerson 1057c2c5fc avbroot/boot.py: Ensure otacerts.zip certificate filename is consistent
It was originally stored as <certificate basename>/ota.x509.pem due to a
brain fart. It was always intended to just be ota.x509.pem.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-01-31 15:08:48 -05:00
Andrew Gunnerson df887f90e7 ota: Reimplement zip signing ourselves more efficiently
The original AOSP ota_utils implementation is quite disk and memory heavy:

* ota_utils.FinalizeMetadata() performs metadata file replacement as
  separate delete + add operations, which require an extra intermediate
  zip to be written.
* ota_utils.FinalizeMetadata() keeps unneeded intermediate temporary
  files around until common.Cleanup() is called.
* common.ZipDelete() deletes files by excluding entries when copying
  from one zip to another. The data copy reads entire entries (including
  the huge payload) into memory instead of copying chunks.

Additionally, ota_utils is a bit exception/crash-unsafe in general.
There are several places where temp files won't be cleaned up unless
everything is following the happy path.

This commit's reimplementation tries to use streaming and chunked
operations where possible and only keeps temporary files around as long
as needed. This is about as efficient as we can get while still relying
on AOSP's signapk.

(And we can finally get rid of the common.OPTIONS hackery!)

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-01-30 19:52:00 -05:00
Andrew Gunnerson 437cfc23d0 Merge pull request #27 from chenxiaolong/time
avbroot.py: Add final status message to indicate the elapsed time
2023-01-30 13:56:21 -05:00
Andrew Gunnerson c2f64a6587 avbroot.py: Add final status message to indicate the elapsed time
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-01-30 13:54:59 -05:00
Andrew Gunnerson 00f903e85f Merge pull request #26 from chenxiaolong/deps
Update external submodules
2023-01-30 01:15:21 -05:00
Andrew Gunnerson 26526ff235 Update external submodules
There are two noteworthy changes:

1. https://android.googlesource.com/platform/build/+/bf01f8b3f0ce70047840999d95a449bf03b407c6%5E%21/

   This reduces the number of fields that we have to apply the hacky
   common.OPTIONS override for.

2. https://android.googlesource.com/platform/build/+/8895f9eceac4ad568bcca724c4dcf8faf5b865f4%5E%21/

   This removes the dependency on the host having the zip command
   installed.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-01-30 00:47:33 -05:00
Andrew Gunnerson 19a559508f Merge pull request #24 from chenxiaolong/linux_only
README.md: Clarify that only Linux is supported
2023-01-28 16:24:21 -05:00
Andrew Gunnerson 9c52dbd8f9 README.md: Clarify that only Linux is supported
Issue: #15

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-01-28 16:24:12 -05:00
Andrew Gunnerson 909d42cd4b Merge pull request #23 from chenxiaolong/temp_override
Respect standard TMPDIR/TEMP/TMP environment variables
2023-01-28 16:21:35 -05:00
Andrew Gunnerson d67cc7fe11 Respect standard TMPDIR/TEMP/TMP environment variables
If any standard temp directory environment variables are set, then don't
use the output directory as the temp directory.

Issue: #22

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2023-01-28 16:20:48 -05:00
Andrew Gunnerson 2afdc2953c Merge pull request #21 from pascallj/patch-1
Use sys.platform to determine if system is Linux
2023-01-28 15:39:10 -05:00
Pascal Roeleven 3dd7c0f5ef Use sys.platform to determine if system is Linux 2023-01-28 14:03:27 +01:00
Andrew Gunnerson 1bc6cfb6d3 Merge pull request #19 from chenxiaolong/output_temp
avbroot.py: Use the output directory as the default temporary directory path
2023-01-24 19:53:30 -05:00
Andrew Gunnerson 41e7d347e2 avbroot.py: Use the output directory as the default temporary directory path
Systems where /tmp is on a tmpfs and have less free RAM may have trouble
with the patching process.

Signed-off-by: Andrew Gunnerson <chillermillerlong@hotmail.com>
2023-01-24 19:52:57 -05:00
Andrew Gunnerson 53eddd5f73 Merge pull request #18 from chenxiaolong/keys
avbroot: Decrypt keys to tmpfs on Linux if available
2023-01-24 19:25:39 -05:00
Andrew Gunnerson aed3ce6494 avbroot: Decrypt keys to tmpfs on Linux if available
Signed-off-by: Andrew Gunnerson <chillermillerlong@hotmail.com>
2023-01-24 18:47:52 -05:00
Andrew Gunnerson e7f8d3737e Merge pull request #16 from chenxiaolong/windows
avbroot.py: Decrypt keys to standard temp directory
2023-01-21 01:21:34 -05:00
Andrew Gunnerson 6c8333a866 avbroot.py: Decrypt keys to standard temp directory
Using /dev/shm breaks compatibility with non-Linux operating systems. On
Linux, most distros mount /tmp as a tmpfs anyway.

Fixes: #15

Signed-off-by: Andrew Gunnerson <chillermillerlong@hotmail.com>
2023-01-21 01:21:12 -05:00
Andrew Gunnerson c22a443edd Merge pull request #12 from chenxiaolong/magisk
avbroot/boot.py: Add support for separate stub.apk
2023-01-13 19:22:17 -05:00
Andrew Gunnerson 74020079ed avbroot/boot.py: Add support for separate stub.apk
Magisk commit ad0e6511e11ebec65aa9b5b916e1397342850319 stopped embedding
stub.apk into magiskinit, so we need to ensure it is extracted and
available to boot_patch.sh.

Signed-off-by: Andrew Gunnerson <chillermillerlong@hotmail.com>
2023-01-13 19:19:43 -05:00
Andrew Gunnerson 0364f02cc8 Merge pull request #10 from chenxiaolong/older_pixel
avbroot/vbmeta.py: Force signing algorithm from SHA256_RSA2048 to SHA256_RSA4096
2023-01-02 15:22:49 -05:00