Installing Magisk on Raspberry Pi 4 OmniROM Android 11

Written 2021-07-15

Tags:OmniROM Android Magisk 

Magisk is a great tool for rooted devices, but the installation process for Raspberry Pi 4 running Android is a little different than most.

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:

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:

Here are the steps, a little more verbose, starting from scratch, and assuming we're installing from a Linux computer.
  1. Insert the microSD card into the Linux workstation
  2. If the first microSD card partition does not mount, mount it manually. Mine was under /media/rsaxvc/rpi4/boot
  3. 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
  4. 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
  5. Unmount all filesystems on the microSD card, eject it, insert into Raspberry Pi 4
  6. Boot the Raspberry Pi 4. When it comes up, launch the Magisk App
  7. 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
  8. Once the patching is complete and succeeds, shutdown the Raspberry Pi 4
  9. 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
  10. Backup the original ramdisk.img from the microSD card /boot partition
  11. 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
  12. fix the ownership/permissions if needed
  13. Unpack contents into a folder with with ./unpackbootimg -i magisk_patched-[random_strings].img
  14. Inside the folder will be a file magisk_patched-[random-strings].img-ramdisk.gz - move to the microSD card's /boot/ramdisk.img
  15. Unmount all microSD card partitions eject the card, and insert into Raspberry Pi 4
  16. Boot up the device, Magisk should now be fully installed - this did take a minute or two for it to start the first time