Installing Magisk on Raspberry Pi 4 OmniROM Android 11
Written 2021-07-15
Tags:OmniROM Android Magisk
Magisk Installation Process
Magisk is normally installed by installing the Magisk App APK, then using it to patch a boot.img. See the upstream instructions here. Notably, this won't work with any files you'll see on a Raspberry Pi 4 /boot filesystem.
It was puzzling because the Magisk app shows "Ramdisk: Yes" on the Raspberry Pi 4, but /boot does not have one, and the usual sources for patching images don't have one either.
Android Boot Process
See this overview and this list of Android image files, but the important part is that Android usually boots by having the bootloader loads the kernel and ramdisk from a single boot.img, then jumps to the kernel.Raspbery Pi 4 Boot Mechanism
From this stack overflow post, we can see that the Raspberry Pi 4 boots the GPU first, loads start.elf into the GPU, which reads a config.txt from the SD (or other sources) filesystem, and follows the instructions in config.txt.
From the OmniROM for RPi4 boot image, we can see it is only slightly different from the usual config.txt, in that a few files are renamed, here are their lines:
kernel=Image initramfs ramdisk.img followkernel
TLDR: The solution
Since Magisk is expecting a boot.img, we need to create one in the format Android usually uses, patch it using the Magisk App, extract the patched recovery.img out of the boot.img, then install it.
Here are the tools we'll need:
- A Raspberry Pi 4 with OmniROM 11 installed to the microSD card
- A Linux workstation with SD card slot - may be doable on Windows as well
- A copy of mkbootimg and unpackbootimg
- A copy of the Magisk app
- Insert the microSD card into the Linux workstation
- If the first microSD card partition does not mount, mount it manually. Mine was under /media/rsaxvc/rpi4/boot
- Combine the Kernel Image file with the ramdisk using mkbootimg. Here was my command to create image.bin ./mkbootimg --kernel /media/rsaxvc/rpi4/boot/Image --ramdisk /media/rsaxvc/rpi4/boot/ramdisk.img --output image.bin
- We will need to transfer the image.bin somewhere the Magisk app can read it like the downloads folder, but I just pushed it to a webserver and downloaded it later
- Unmount all filesystems on the microSD card, eject it, insert into Raspberry Pi 4
- Boot the Raspberry Pi 4. When it comes up, launch the Magisk App
- Patch as usual but stop when it tells you to copy the patched image back to the PC. I will note that the file picker on OmniROM seemed to crash loading .img files - I think I had to right-click the boot.img icon to toggle selection
- Once the patching is complete and succeeds, shutdown the Raspberry Pi 4
- Move the microSD card back to the workstation and mount at least the first(/boot) partition and the last partition which contains the Downloads folder
- Backup the original ramdisk.img from the microSD card /boot partition
- In the Downloads folder, the Magisk app will have written magisk_patched_[random_strings].img. Copy it to a local temporary directory, this may need to be done as root due to permissions
- fix the ownership/permissions if needed
- Unpack contents into a folder with with ./unpackbootimg -i magisk_patched-[random_strings].img
- Inside the folder will be a file magisk_patched-[random-strings].img-ramdisk.gz - move to the microSD card's /boot/ramdisk.img
- Unmount all microSD card partitions eject the card, and insert into Raspberry Pi 4
- Boot up the device, Magisk should now be fully installed - this did take a minute or two for it to start the first time