[ASUS ZenFone 8] bnsmb:
How to process Android boot image files on a PC running the Linux OS

To unpack and repack the boot image for phones running the Android OS the Magisk binary magiskboot is necessary (well, there might be other tools for this task but magiskboot seems to be one of the best tools for this task). Therefor these kind of tasks will usually be done on a phone running the Android OS.

But there is also a magiskboot binary for x86 CPUs in the Magisk apk file and that executable can be used to work with boot images for Android phones on a PC running the Linux OS on a machine with x86 CPU. To retrieve the x86 version of magiskboot from the Magisk apk file do:

# to use the 32 bit version

unzip Magisk-v25.2.apk lib/x86/libmagiskboot.so
mv lib/x86/libmagiskboot.so ./magiskboot
chmod 755 ./magiskboot

or

# to use the 64 bit version

unzip Magisk-v25.2.apk lib/x86_64/libmagiskboot.so
mv lib/x86_64/libmagiskboot.so ./magiskboot
chmod 755 ./magiskboot

Example:

[xtrnaw7@t15g /data/develop/android/magisk]$ unzip Magisk-v25.2.apk lib/x86_64/libmagiskboot.so
Archive: Magisk-v25.2.apk
version=25.2
versionCode=25200
inflating: lib/x86_64/libmagiskboot.so
[xtrnaw7@t15g /data/develop/android/magisk]$

[xtrnaw7@t15g /data/develop/android/magisk]$ mv lib/x86_64/libmagiskboot.so ./magiskboot
[xtrnaw7@t15g /data/develop/android/magisk]$

[xtrnaw7@t15g /data/develop/android/magisk]$ chmod 755 magiskboot
[xtrnaw7@t15g /data/develop/android/magisk]$

[xtrnaw7@t15g /data/develop/android/magisk]$ file ./magiskboot
./magiskboot: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, BuildID[sha1]=b2d42d7bab2f70a0d66fc606a1fb2726a0559fed, stripped
[xtrnaw7@t15g /data/develop/android/magisk]$

[xtrnaw7@t15g /data/develop/android/magisk]$ ldd ./magiskboot
not a dynamic executable
[xtrnaw7@t15g /data/develop/android/magisk]$

[xtrnaw7@t15g /data/develop/android/magisk]$ ./magiskboot
MagiskBoot - Boot Image Modification Tool

Usage: ./magiskboot <action> [args...]
...

[xtrnaw7@t15g /data/develop/android/magisk]$

The binary magiskboot from the Magisk apk file is a statically linked executable either for 32 Bit or 64 Bit. Therefor it should run on any Linux distribution that supports executing executables for x86. I tested the binary from the apk file with Magisk v25.2 on these distributions:

Fedora 36 (64 Bit, kernel 6.x)

Redhat 8.6 (64 Bit, kernel 4.x)

Redhat 6.10 (64 Bit, kernel 2.x)

GRML LiveCD (32 Bit, kernel 5.x, the 32 bit version only)

RockyLinux 9.0 (64 Bit, kernel 5.x)

openSUSE LEAP 15.2 (64 Bit, kernel 5.x)

Unpacking a boot image file using magiskboot on a PC running the Linux OS can be done using this command;

./magiskboot unpack boot_a.img

To change files in the ramdisk from the boot image use the magiskboot parameter cpio:

[xtrnaw7@t15g /data/develop/android/test2]$ ./magiskboot
MagiskBoot - Boot Image Modification Tool

Usage: ./magiskboot <action> [args...]

Supported actions:

...

cpio <incpio> [commands...]
Do cpio commands to <incpio> (modifications are done in-place)
Each command is a single argument, add quotes for each command
Supported commands:
exists ENTRY
Return 0 if ENTRY exists, else return 1
rm [-r] ENTRY
Remove ENTRY, specify [-r] to remove recursively
mkdir MODE ENTRY
Create directory ENTRY in permissions MODE
ln TARGET ENTRY
Create a symlink to TARGET with the name ENTRY
mv SOURCE DEST
Move SOURCE to DEST
add MODE ENTRY INFILE
Add INFILE as ENTRY in permissions MODE; replaces ENTRY if exists
extract [ENTRY OUT]
Extract ENTRY to OUT, or extract all entries to current directory
test
Test the current cpio's status
Return value is 0 or bitwise or-ed of following values:
0x1:Magisk 0x2:unsupported 0x4:Sony
patch
Apply ramdisk patches
Configure with env variables: KEEPVERITY KEEPFORCEENCRYPT
backup ORIG
Create ramdisk backups from ORIG
restore
Restore ramdisk from ramdisk backup stored within incpio
sha1
Print stock boot SHA1 if previously backed up in ramdisk
...

[xtrnaw7@t15g /data/develop/android/test2]$

E.g. to change the file system/etc/ramdisk/build.prop in the ramdisk, extract the file from the ramdisk

./magiskboot cpio ramdisk.cpio "extract system/etc/ramdisk/build.prop build.prop"

edit the file, and add the file back to the ramdisk using this command:

./magiskboot cpio ramdisk.cpio "add 0644 system/etc/ramdisk/build.prop build.prop"

Notes

Note that I did not yet manage to create a working boot image with magiskboot running in the Linux OS after extracting, changing, and recreating the ramdisk via the Linux cpio command (as Linux user root, of course) :

Creating a new boot image this way with magiskboot running in the LInux OS works without error but booting the new created boot image always ends with a ramdump error on the phone. So extracting the ramdisk via Linux cpio command can only be used to view the files in a boot image file for an Android Phone from wthin the Linux OS.

Trouble Shooting

To test the new boot image without installing it use this approach:

# boot the phone into the bootloader
#
adb reboot bootloader

# boot the phone from the patched image file
#
sudo fastboot boot ./new-boot.img

If booting the phone works and everything is fine flash the image to the active boot partition

In case you get a ramdump error after booting the phone from the new boot partition just restart the phone into the bootloader mode and flash the old image back to the boot partition.