LG K40 (lg-mmh4x)

Contributors

 * BenTheTechGuy

How to enter flash mode
The LG K40 doesn't have a useful recovery mode (it's just a factory reset button), and fastboot doesn't exist at all on the device; the only possible mode is Download Mode, but that requires a signed image to flash.

Luckily, there's a tool called mtkclient that makes use of vulnerabilities in the MediaTek SoC itself, and that can be used to unlock the bootloader and flash images. It's also packaged as on some distributions (Alpine, AUR, soon Debian/Ubuntu).

In the below examples, the command  is used, but if you installed from source, you'll need to run   inside the repo.

To enter into a mode that mtkclient can be used in, follow these steps:
 * 1) Power off the device.
 * 2) Run an mtkclient command, like.
 * 3) While mtkclient waits for the device, hold  and plug it into the computer.
 * 4) mtkclient should output a lot of text at this point. The button can be released.
 * 5) As long as mtkclient did its job properly, the command will succeed. Now, other commands can be run with mtkclient.

Unlocking the bootloader
Note: If metadata or md_udc don't exist, just remove them from the command.
 * 1) Enable developer options, and OEM Unlocking from there.
 * 2) Follow the steps in.
 * 3) Erase metadata, userdata, and md_udc:
 * 1) Unlock the bootloader:
 * 1) Reset mtkclient:
 * 1) The bootloader should now be unlocked. From here, the device can be unplugged and powered on to go through setup.

Installation
Since recovery or fastboot can't be used, the kernel and rootfs must be flashed manually with mtkclient. First, find out which slot is active, A or B. Normally this would be done with fastboot, but that cannot be accessed, so adb must be used. adb shell getprop ro.boot.slot_suffix # will return either _a or _b, current active slot

pmbootstrap init # DRM is not supported yet, so Xfce4 is the best graphical option for now pmbootstrap install # can also install to an SD card and skip flashing to userdata later pmbootstrap export # symlink kernel and rootfs to /tmp for easy manual flashing

mtk w boot_a /tmp/postmarketOS-export/boot.img # flash kernel to boot_a or boot_b depending on active slot mtk w userdata /tmp/postmarketOS-export/lg-mmh4x.img # flash rootfs to userdata partition

mtk reset
 * 1) unplug the device, turn it back on, and enjoy postmarketOS :)