Update Your Rooted Nexus 5 to Android Marshmallow Without Wiping Data

Oct 23, 2015

Google recently released Android 6.0 (Marshmallow), and the factory images for the Nexus devices arrived shortly thereafter. However, one thing that annoys me is the fact that these images wipe your data by default.

I fail to comprehend why Google releases these factory images every time with a flash-all.sh script that wipes your entire phone, including anything in your /sdcard folder, such as photos you took and haven't backed up yet, or your Google Authenticator encryption keys, which aren't synced anywhere. If you were to wipe your device, you'd be locking yourself out of all of your 2-factor-authenticated accounts. And disabling 2-factor-authentication when you can no longer generate codes can be a real hassle.

So I set out to find a way to update to Marshmallow without wiping data. OTA wasn't an option since I rooted my device and modified some system files. So I researched and combined tips from a few places into one definitive guide for updating rooted Nexus 5 devices to Android Marshmallow.

Marshmallow

Rooting Complications

Rooting is a little more difficult in Android 6.0, due to the introduction of dm-verity in Android. Verity is a security feature, originally found in ChromeOS, designed to provide assured and trustworthy computing devices, preventing malicious software from modifying a device. Back in Android 4.4, Google announced verity for Android, and then all remained quiet. While there has been some research into using verity, for the most part, things have been quiet. Until now, that is.

If you want to root your Android Marshmallow device, you’re going to need to use a modified boot image (kernel) that disables verity. But what if you don't want any of the additional tweaks and modifications in these kernels (such as overclocking and other potentially dangerous features)? Luckily, one developer went ahead and released a nearly-stock kernel, simply making it possible to root the device, without any other modifications.

Make sure you back up your files first, in case anything goes wrong. Let's get started!

Download the Marshmallow Factory Image

First, we'll need to grab the Marshmallow factory image for Nexus 5 from the Google Developers site. Look for the hammerhead Android 6.0 image, as that is the build name of Nexus 5.

Here it is:
https://dl.google.com/dl/android/aosp/hammerhead-mra58k-factory-52364034.tgz

Install Fastboot

Fastboot is Google's utility for flashing factory images. It comes bundled with the Android SDK. Install the SDK (if you haven't already), and add its platform-tools folder to your PATH environment so you can type fastboot in the terminal.

Flash the Images

First off, let's unzip the hammerhead-mra58k-factory-52364034.tgz, cd into the unzipped directory, reboot into fastboot mode (adb reboot bootloader), and flash the radio and bootloader:

tar -xvzf hammerhead-mra58k-factory-52364034.tgz
cd hammerhead-mra58k-factory-52364034/hammerhead-mra58k

fastboot flash bootloader bootloader-hammerhead-hhz12k.img
fastboot reboot-bootloader

fastboot flash radio radio-hammerhead-m8974a-2.0.50.2.27.img
fastboot reboot-bootloader

Next, let's flash the cache.img, boot.img, recovery.img, and system.img with the following command:

Pro tip: Running fastboot update without the -w flag will not wipe your data, so no need to worry. :-)

fastboot update image-hammerhead-mra58k.zip

This will take some time -- the system.img is 995 MB. When that's done, we'll move on to flashing a custom recovery and rooting the device.

Flash Custom Recovery

Next, let's go ahead and grab the latest version of TWRP for hammerhead from here. TWRP is a custom recovery that makes it possible to create backups of your entire system, flash custom zip packages, wipe caches, and more. We'll need it for the next step.

Once you download the TWRP recovery image, let's go ahead and flash it:

fastboot flash recovery openrecovery-twrp-2.8.5.2-hammerhead.img

Install a Rootable Stock Kernel

If you want to root your Android Marshmallow device, you’re going to need to use a modified boot image (kernel).

Chainfire (the maker of SuperSU) comes to the rescue, providing a stock Marshmallow boot image, with only the following modifications:

  • patched sepolicy
  • disabled dmverity
  • disabled forceencrypt

Here is the download link for hammerhead:
http://forum.xda-developers.com/attachment.php?attachmentid=3499079&d=1444242551

Download it, unzip it, and flash it:

unzip hammerhead-mra58k-boot.zip
cd hammerhead-mra58k-boot
fastboot flash boot boot.img

Root the Device with SuperSU

Go ahead and grab the latest version of Chainfire's SuperSU here:
http://download.chainfire.eu/supersu

SuperSU

Let's reboot into recovery to flash Chainfire's SuperSU zip. Once TWRP has initialized, let's push the UPDATE-SuperSU-v2.46.zip to the device's /sdcard folder, so we can flash it via TWRP.

fastboot reboot-recovery
adb push UPDATE-SuperSU-v2.46.zip /sdcard/

Next, in the TWRP interface on your device, press Install, find and press the zip you just pushed, and swipe to confirm installation. When that's finished, all that's left is to reboot your device (press Reboot System in TWRP).

Nexus 5 Marshmallow

All done!

Enjoy your rooted Nexus 5 running Android 6.0 Marshmallow!