Skip to content

Instantly share code, notes, and snippets.

@davidofwatkins
Last active September 6, 2016 19:04
Show Gist options
  • Save davidofwatkins/0415f362ed8b9da0d3a31568800efc62 to your computer and use it in GitHub Desktop.
Save davidofwatkins/0415f362ed8b9da0d3a31568800efc62 to your computer and use it in GitHub Desktop.
A simple step-by-step guide for flashing a minor update and re-rooting for Nexus devices

Nexus Dirty Flash Checklist

This is a simple step-by-step guide for flashing a minor update (e.g., monthly security update) and re-rooting for Nexus devices.

This is meant to be more of a reminder checklist for those who do this semi-regularly; if you haven't done this before or don't know much about adb and fastboot, you should probably find a more detailed guide (see here for the Nexus 5X or here for the Nexus 6P).

Option 1: OTA Sideload (Recommended)

The easiest way to update your nexus device manually is through the OTA downloads provided by Google.

  1. Download the OTA Zip for your phone.

  2. Reboot into Recovery

    adb reboot recovery
  3. Put your device into sideload mode. For TWRP, choose "Advanced" > "ADB Sideload," and swipe to prepare for sideload.

  4. Sideload the OTA!

    adb sideload path/to/ota.zip
  5. You're done! Unfortunately, you will most likely need to reboot before sideloading SuperSU (see below).

Option 2: Dirty Flash

If you wish to "dirty" flash the old-fashioned way, you can download the full factory images and flash them without wiping user data.

  1. Download the latest image for your phone from Google Nexus Factory Images page

  2. Extract zip and edit flash-all.sh to remove -w from the update command:

    $ sed -i -e 's/ -w//g' flash-all.sh
  3. Reboot to the bootloader

    $ adb reboot bootloader
  4. Run the update script:

    $ ./flash-all.sh

Once the phone reboots, the update should be installed.

Alternatively, the below script will run the above in one command:

#!/bin/bash
# 
# A convenience script for flashing a minor update for a Nexus device. Only use this for minor updates 
# (e.g., monthly security updates). Download the latest image for your phone from:
# https://developers.google.com/android/nexus/images
# 
# Usage: ./flash-android.sh path/to/image.zip

set -e # exit on failure
zipfile=$1

(
    mkdir -p tmp-android-flash

    echo "Extracting $zipfile..."
    tar xzf $zipfile -C tmp-android-flash/      # extract tar!
    cd tmp-android-flash
    cd $(ls -d */)                              # cd to the only folder that was extracted

    echo "Removing -w option from flash-all script..."
    sed -i -e 's/ -w//g' flash-all.sh           # remove the -w from the `fastboot update` command
    sed -i -e 's/ -w//g' flash-all.bat          # might as well do the same for Windows

    echo ""
    read -p 'Ready to flash. Is the device connected and ready to go? [y/n] ' answer
    if [[ $answer == 'n' ]] || [[ $answer == 'no' ]]; then
        exit 1
    fi

    echo "Rebooting to bootloader..."
    adb reboot bootloader
    sleep 10

    echo "Flashing.................."
    ./flash-all.sh

    echo ""
    echo "Done flashing; cleaning up."
    cd ../../
    rm -rf tmp-android-flash;

)

Systemless Root

To (re-)root your device:

  1. Download TWRP Recovery for your phone model.

  2. Download SuperSU.

  3. Reboot to bootloader

    $ adb reboot bootloader
  4. Install TWRP Recovery and reboot bootloader.

    $ fastboot flash recovery path/to/TRWPRecovery.img
    $ fastboot reboot bootloader
  5. Use the volume and power keys to select "Recovery." If TWRP asks for a password or says it can't decrypt, press cancel and continue. Select "Advanced," then "ADB Sideload." If/when asked, accept to whipe cache and dalvik.

  6. Once ADB Sideloading is turned on, run the following and then reboot system:

    $ adb sideload path/to/superSU.zip

    Note: If TWRP asks to root device, do not accept. As of this writing, it is incompatible with Systemless Root and might break something.

  7. To ensure SafetyNet passes on systemless root, adb shell and run the following (per these instructions):

    su                                              # will need to accept SuperSU permission on device
    
    chmod 751 /su/bin
    rm /su/xbin_bind
    
    echo SYSTEMLESS=true > /data/.supersu           # if not done already
    echo BINDSYSTEMXBIN=false >> /data/.supersu     # if not done already

    Note: This may no longer be necessary with the lastest SuperSU

Simple Ad Blocking (via hosts file)

  1. Download a hosts file and save it to your phone

    curl https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts > hosts
    
    # Optional - remove any blocked hosts you want to allow, if any
    sed -i '/heapanalytics/d' hosts
    
    adb push -p hosts /sdcard/Download/
    rm hosts
  2. Update the phone's hosts file with this one:

    adb shell
    su                                  # become root (accept confirmation on device)
    mount -o remount,rw /system         # remount system as writeable
    
    cd /system/etc/
    cp hosts hosts.bkp                  # make a backup, just in case
    chmod a+r hosts.bkp                 # bkp file should have same permissions
    
    # Perform the udpate! Append everything in our new hosts file to our existing
    cat /sdcard/Download/hosts >> hosts
    
    mount -o remount,ro /system         # remount as normal
    exit                                # unroot
    exit                                # good bye
@rwarner
Copy link

rwarner commented Jun 7, 2016

Maybe want to change step 2 for Super SU for this link:
http://forum.xda-developers.com/apps/supersu/2014-09-02-supersu-v2-05-t2868133
Bugfixes and the like since 2.60

Also in the removal of /su/xbin_bind:

change "crm /su/xbin_bind" to "rm /su/xbin_bind"

@davidofwatkins
Copy link
Author

@kentoe Good call, updated. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment