How to patch boot.img rom file with Magisk

As we know, Magisk is an another rooting method, similar to SuperSU but the main differences is Magisk can hide its root properties so apps with no-root policy still can be accessed. Although Magisk had its boot.img root-patching feature, most of rooting guides still insists of using TWRP or other custom recovery roms and installing the zip file rather than patching the boot image file.

So, I'm gonna write on how to root your Android device by using Magisk via boot.img root-patching method. Before that, you will need these:

1. ADB flash tools (ADB driver must be installed).
2. A working device with unlocked bootloader.
3. Boot.img file from your stock firmware or extracted from TWRP.
4. A brain.

How to patch boot.img file with Magisk:

1. Unlock the bootloader of your device. To check whether your device bootloader is locked or not, simply enter bootloader mode and run the command:
fastboot oem device-info
and you will see the bootloader status. If locked, try to unlock it, there is a various kind of method on unlocking it depends on your device type.

2. Obtain the boot.img file either from the official stock firmware or just extracted from TWRP custom recovery. There are various ways on how to obtain it. If using TWRP on very latest Android device, you might need to wipe the data first to remove encryption temporarily then reboot to TWRP back when prompted to perform a nandroid backup which needed to extract the ROM file from the device and edit the filename to boot.img. 

3. You will need to edit the boot.img file by using bootimg.exe tool, download it from here. Then, simply run the command below to extract the boot.img file:
bootimg.exe --unpack-bootimg boot.img

4. Then, open up Notepad++ and edit default.prop file inside the initrd folder and adjust the line like this:

5. After editing default.prop file, you will need to repack it back by running the command:
bootimg.exe --repack-bootimg

6. Transfer the repacked boot.img file into your device and install Magisk Manager on your device. Run Magisk Manager and install Magisk root on the boot.img file via patch boot image file.

7. After patching successfully, you will need to archive the root-patched boot.img file by using file archiving app like ZArchiver or etc. in order to transfer the file out from the device.

8. Extract the compressed root-patched boot image file and reboot your device in bootloader mode and flash the root-patched boot image file with command below:
fastboot flash boot patched_boot.img 

9. Run the command below to reboot your device:
fastboot reboot 

10. If your device reboots normally, then your device is now rooted.