Sunday, February 5, 2017

Moto G4 Root from Linux

Here are the steps I followed to root my Moto G4.  There were some guides on xda that involved reflashing boot.img.  While it seems many have done this with success, flashing the boot.img is not something I like doing and I try to avoid it if at all possible.  The below method will will modify the boot image, but the SuperSU installation script does it for you.  In my opinion this is still risky but perhaps less risky than flashing a boot.img that you found on the internet.

I performed all of the below steps from my Linux machine; there is no need to use Windows.

Unlocking the bootloader is the same as performing a system reset; you will lose all of your data, so make a backup first.

If you brick your phone following these steps, well, that's on you.

Preliminary Steps

  1. Enable developer options on the phone
  2. Inside developer options, enable "OEM Unlocking"
  3. Enable "USB debugging"
  4. On the computer: have the Android SDK installed (at least adb and fastboot utilities)
  5. Back up important data/pictures/etc from the phone.

Unlock Bootloader

Reboot the phone into the bootloader:

wskellenger@marquette ~ $ adb reboot bootloader

See if the device is visible:

wskellenger@marquette ~ $ fastboot devices

no permissions (verify udev rules); see [] fastboot
For me, it was visible but with no permissions.  Try it with root:
wskellenger@marquette ~ $ sudo $(which fastboot) devices

ZY223R9KC9 fastboot

Obtain the unlock (unique to your phone):

wskellenger@marquette ~ $ sudo $(which fastboot) oem get_unlock_data

(bootloader) slot-count: not found
(bootloader) slot-suffixes: not found
(bootloader) slot-suffixes: not found
(bootloader) Unlock data:
(bootloader) 3A45210437945222#
(bootloader) 5A5932323352394B4339004D6F746F2047200000#
(bootloader) 5342C312F53218107391149534B38F6928DBBD07#
(bootloader) C6506801000000000000000000000000
OKAY [  0.103s]
finished. total time: 0.103s

Format the unlock seed:

Copy the output lines and paste them into vim.  Then do:

(note the search/replace command at the bottom)
:%s/(bootloader) //gc

Accept all replacements.  You now have this:

Place your cursor on the first line and type "4gJ" to join all of the lines without spaces:

Get your unlock key from Motorola:

Go here, and paste the string you created above from vim into Motorola's webpage:

A friendly/pleasant (really!) email will arrive with the unlock code:



If you are comfortable with this, unlock it with:

sudo $(which fastboot) oem unlock [your key here]

You have to run the command twice:

Install TWRP

Get what you need

This post from xda-developers describes what you need.

Download the TWRP image

The TWRP image is available here.

Verify image integrity

You should confirm integrity of the download by also downloading the md5 file, and checking that the first few/last few characters of each md5 agree:
wskellenger@marquette ~/Downloads $ md5sum twrp-3.0.2-0-athene.img
1c3ed996a8e978c05bfd25da47eb1e47  twrp-3.0.2-0-athene.img

wskellenger@marquette ~/Downloads $ cat twrp-3.0.2-0-athene.img.md5
1c3ed996a8e978c05bfd25da47eb1e47  twrp-3.0.2-0-athene.img

Use mfastboot to flash the recovery image

mfastboot is a motorola specific fastboot utility.  You can find it on  Use it to flash the recovery image.  Here I'm also updating the logo so I don't have the annoying bright white warning about the unlocked bootloader.

Reboot into TWRP recovery

While you are still in the bootloader, you can use the up/down arrows to highlight "recovery" and the press the power button to go into recovery.

When you see TWRP recovery appear, great!  The updated recovery works.  You can reboot the system and set up your Google account, restore your backups, etc.  Note: you aren't rooted yet.

Install SuperSU

Thanks to this post on XDA.

From the phone's browser, download the SuperSU zip from here:

Shut the phone down, and reboot into TWRP recovery.  Go into the terminal by selecting Advanced-->Terminal.

Inside the terminal, type the following (exactly as written, obeying all spaces, etc.) and press enter:
echo SYSTEMLESS=true>>/data/.supersu

In TWRP, exit the terminal.  Press the home button.  

Install the SuperSU zip from your phone's download directory.  The exact version I used:

Reboot the phone.  

It may reboot itself once after this.  When the phone boots, SuperSU will be in the app list.