Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ignoring boot image checks for --prepatched #112

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ avbroot can replace the boot image with a prepatched image instead of applying t

For KernelSU, also pass in `--boot-partition @gki_kernel` for both the `patch` and `extract` commands. avbroot defaults to Magisk's semantics where the boot image containing the GKI ramdisk is needed, whereas KernelSU requires the boot image containing the GKI kernel. This only affects devices launching with Android 13, where the GKI kernel and ramdisk are in different partitions (`boot` vs. `init_boot`), but it is safe and recommended to always use this option for KernelSU.

Note that avbroot will validate that the prepatched image is compatible with the original. If, for example, the header fields do not match or a boot image section is missing, then the patching process will abort. This check is not foolproof, but should help protect against accidental use of the wrong boot image.
Note that avbroot will validate that the prepatched image is compatible with the original. If, for example, the header fields do not match or a boot image section is missing, then the patching process will abort. The checks are not foolproof, but should help protect against accidental use of the wrong boot image. To bypass a somewhat "safe" subset of the checks, use `--ignore-prepatched-compat`. To ignore all checks (strongly discouraged!), pass it in twice.

### Skipping root patches

Expand Down
53 changes: 40 additions & 13 deletions avbroot/boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,16 @@ class PrepatchedImage(BootImagePatch):
be higher than the original image.
'''

MIN_LEVEL = 0
MAX_LEVEL = 2

VERSION_REGEX = re.compile(
b'Linux version (\d+\.\d+).\d+-(android\d+)-(\d+)-')

def __init__(self, prepatched):
def __init__(self, prepatched, fatal_level, warning_fn):
self.prepatched = prepatched
self.fatal_level = fatal_level
self.warning_fn = warning_fn

def patch(self, image_file, boot_image):
with open(self.prepatched, 'r+b') as f:
Expand All @@ -305,40 +310,62 @@ def patch(self, image_file, boot_image):
old_header = boot_image.to_dict()
new_header = prepatched_image.to_dict()

errors = []
# Level 0: Warnings that don't affect booting
# Level 1: Warnings that may affect booting
# Level 2: Warnings that are very likely to affect booting
issues = [[], [], []]

for k in new_header.keys() - old_header.keys():
errors.append(f'{k} header field was added')
issues[2].append(f'{k} header field was added')

for k in old_header.keys() - new_header.keys():
errors.append(f'{k} header field was removed')
issues[2].append(f'{k} header field was removed')

for k in old_header.keys() & new_header.keys():
if old_header[k] != new_header[k]:
errors.append(f'{k} header field was changed: '
f'{old_header[k]} -> {new_header[k]}')
if k in ('id', 'os_version'):
level = 0
elif k in ('cmdline', 'extra_cmdline'):
level = 1
else:
level = 2

issues[level].append(f'{k} header field was changed: '
f'{old_header[k]} -> {new_header[k]}')

for attr in 'kernel', 'second', 'recovery_dtbo', 'dtb', 'bootconfig':
original_val = getattr(boot_image, attr)
prepatched_val = getattr(prepatched_image, attr)

if original_val is None and prepatched_val is not None:
errors.append(f'{attr} section was added')
issues[1].append(f'{attr} section was added')
elif original_val is not None and prepatched_val is None:
errors.append(f'{attr} section was removed')
issues[2].append(f'{attr} section was removed')

if len(prepatched_image.ramdisks) < len(boot_image.ramdisks):
errors.append('Number of ramdisk sections decreased: '
f'{len(boot_image.ramdisks)} -> '
f'{len(prepatched_image.ramdisks)}')
issues[2].append('Number of ramdisk sections decreased: '
f'{len(boot_image.ramdisks)} -> '
f'{len(prepatched_image.ramdisks)}')

if boot_image.kernel is not None:
old_kmi = self._get_kmi_version(boot_image)
new_kmi = self._get_kmi_version(prepatched_image)

if old_kmi != new_kmi:
errors.append('Kernel module interface version changed: '
f'{old_kmi} -> {new_kmi}')
issues[2].append('Kernel module interface version changed: '
f'{old_kmi} -> {new_kmi}')

warnings = [e for i in range(self.MIN_LEVEL,
min(self.MAX_LEVEL + 1, self.fatal_level))
for e in issues[i]]
errors = [e for i in range(max(self.MIN_LEVEL, self.fatal_level),
self.MAX_LEVEL + 1)
for e in issues[i]]

if warnings:
self.warning_fn('The prepatched boot image may not be compatible '
'with the original:\n' +
'\n'.join(f'- {w}' for w in warnings))

if errors:
raise ValueError('The prepatched boot image is not compatible '
Expand Down
30 changes: 22 additions & 8 deletions avbroot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,11 @@ def patch_subcommand(args):
else:
raise e
else:
root_patch = boot.PrepatchedImage(args.prepatched)
root_patch = boot.PrepatchedImage(
args.prepatched,
args.ignore_prepatched_compat + 1,
print_warning,
)

# Get passphrases for keys
passphrase_avb = openssl.prompt_passphrase(
Expand Down Expand Up @@ -542,6 +546,12 @@ def parse_args(argv=None):
action='store_true',
help='Ignore Magisk compatibility/version warnings',
)
patch.add_argument(
'--ignore-prepatched-compat',
default=0,
action='count',
help='Ignore compatibility issues with prepatched boot images',
)

patch.add_argument(
'--clear-vbmeta-flags',
Expand Down Expand Up @@ -595,13 +605,17 @@ def parse_args(argv=None):

args = parser.parse_args(args=argv)

if args.subcommand == 'patch' and args.magisk is None:
if args.magisk_preinit_device:
parser.error('--magisk-preinit-device requires --magisk')
elif args.magisk_random_seed:
parser.error('--magisk-random-seed requires --magisk')
elif args.ignore_magisk_warnings:
parser.error('--ignore-magisk-warnings requires --magisk')
if args.subcommand == 'patch':
if args.magisk is None:
if args.magisk_preinit_device:
parser.error('--magisk-preinit-device requires --magisk')
elif args.magisk_random_seed:
parser.error('--magisk-random-seed requires --magisk')
elif args.ignore_magisk_warnings:
parser.error('--ignore-magisk-warnings requires --magisk')
elif args.prepatched is None:
if args.ignore_prepatched_compat:
parser.error('--ignore-prepatched-compat requires --prepatched')

return args

Expand Down