[TUTORIAL] How to flash a factory image | Return to stock | Unroot/SAVE your Nexus 5

Search This thread

Soultaker226

Member
Apr 14, 2015
14
1
How to flash a factory image | Return to stock | Unroot your Nexus 5​




Hello Nexus 5 Users!

I see many users that don't know how to restore their Nexus 5 to stock. It may come in handy, either when you want to RMA your phone, or you want to fix your mistakes.

This process consists of wiping and flashing every partition on your phone.



Post-Divider-LG.jpg

WARNING!!!

This process will wipe your phone completely. Backup your storage before you continue.
I am also not responsible if anything goes wrong.
You can backup apps and data using this app before you unlock the bootloader. No root required.
https://play.google.com/store/apps/details?id=com.koushikdutta.backup

DO NOT USE TOOLKITS!!!
They have a high probability of something going wrong if you don't know how to use them (which, if you managed to ruin your phone, probably is true).
I don't mean to offend toolkit creators. They did a great job. But don't risk bricking your phone just because you're lazy.

FOLLOW EVERY SINGLE STEP
or you will end up in a bootloop, or with a bricked phone, and we don't want that

Let's get started!!



Post-Divider-LG.jpg

Setting up



For this process, you will need to:

Install adb and fastboot drivers. To do this, use this simple program:
http://xdaforums.com/showthread.php?t=2588979

For MAC users:
http://xdaforums.com/showthread.php?t=1917237
*Note: MAC users will have to write "./" before every command in this thread. For example : "./fastboot flash recovery recovery.img"

Linux users should know their stuff :D
http://xdaforums.com/showthread.php?t=921169

Download the latest factory images from here:
https://developers.google.com/android/nexus/images#hammerhead

★Extract the archive you downloaded (Use WinRAR). It should look like this:
Untitled.png


★Unlock your bootloader (In case you haven't already)
http://xdaforums.com/showthread.php?t=2507905







Post-Divider-LG.jpg

Flashing the factory images



There is more than one way to flash these images. Each give the same results, but the first is for lazy users :D

Method 1. Easy method.

★Power off your phone.

★Boot it in fastboot by pressing and holding Power Button + Volume Down at the same time.

★Connect your phone to your PC through your USB cable.

★Execute the flash-all.bat script (for Windows) or flash-all.sh(for MAC and LINUX) in the folder where you extracted the factory images.
**!!** For MAC: You'll have to edit the flash-all.sh, and add "./" before every fastboot command.

★Wait for everything to flash.

★After everything finished, select "Recovery" using the volume buttons.

★When a small dead Android appears, press Power Button + Volume UP.

★Select "Wipe data/factory data reset"

★Reboot. Your phone should be stock. :good:


Method 2. Long method (Same thing actually, but I'll mention it, just in case)

★Extract the image-hammerhead-krt16m.zip somewhere easy to access (On C:\image-hammerhead-krt16m for example). You will have the following files:

Untitled2.png


★In the same folder, from the original archive, extract "bootloader-hammerhead-XXXXXX.img" (rename it to bootloader.img) and "radio-hammerhead-XXXXXX.img" (rename it to radio.img)

★Power off your phone.

★Boot it in fastboot by pressing and holding Power Button + Volume Down at the same time.

★Connect your phone to your PC through your USB cable.

★Open a CMD window.

★Type the following commands, in this order (If you have a different folder, change the path)
Code:
fastboot flash bootloader C:\image-hammerhead-krt16m\bootloader.img
fastboot reboot-bootloader
fastboot flash radio C:\image-hammerhead-krt16m\radio.img
fastboot reboot-bootloader
fastboot flash system C:\image-hammerhead-krt16m\system.img
fastboot flash userdata C:\image-hammerhead-krt16m\userdata.img 
[B](Note: this command will wipe your device (including \sdcard), EVEN if your bootloader is already unlocked.)[/B]

fastboot flash boot C:\image-hammerhead-krt16m\boot.img
fastboot flash recovery C:\image-hammerhead-krt16m\recovery.img
fastboot erase cache
fastboot flash cache C:\androidimage\cache.img
★After everything finished, select "Recovery" using the volume buttons.

★When a small Android appears, press Power Button + Volume UP.

★Select "Wipe data/factory data reset"

★Reboot. Your phone should be stock. :good:

For both methods:
Optional, but necessary for warranty purposes - Locking your bootloader+ Resetting the tamper flag


The Nexus 5 has a thing implemented in the bootloader called a tamper flag. This shows if the bootloader was ever unlocked, kind of like the flash counter with Samsung devices. You can check it out by using this command
Code:
fastboot oem device-info
. If the tamper flag value is "true", then you'll have to reset it. The following steps will reset this flag, and lock your bootloader.

•So, first, go here: http://xdaforums.com/showthread.php?t=2239421 (Thanks @osm0sis for the wonderful script)
Under "Flashable Zips", you will find the Nexus BootUnlocker script. Download it.

•Put the zip on your internal storage.

•Download the latest TWRP recovery image for the Nexus 5 from here: http://techerrata.com/browse/twrp2/hammerhead. Rename it to TWRP.img.
Now boot it. You do this by booting your phone in fastboot, and typing the following command:
Code:
fastboot boot path\to\TWRP.img
*Replace path\to\TWRP.img with the path to where you placed the TWRP.img. For example: C:\Users\X\Desktop\TWRP.img
TWRP recovery should boot.

•When TWRP finished booting, select "Install", then navigate to the Nexus Bootunlocker zip, select and install it. After it finished, use the back arrow at the bottom of the screen to go back, and reboot.

Now your bootloader should be locked, and your tamper flag should show "false".





And now you have a brand new Nexus 5 (software wise, at least)




Post-Divider-LG.jpg

Video Guides

Thanks @techfanatic9.


For Windows:



For Mac:



Another easy and fast tutorial made by @iTzGavin96

These don't contain the tamper flag reset step. Follow the steps above to reset your tamper flag value to "False" for warranty purposes.

i was wondering when i connect my nexus 5 to the adb it shows null/empty when i type " adb devices" however when i tried "adb devices" when i was in twrp/recovery mode the phone was recognize and my serial number pops up ,,, can i flash factory image if i connect the phone when it was in twrp/recovery mode ?? or should i only flash it when adb recognize my nexus 5 in bootloader mode ??
 
Last edited by a moderator:
  • Like
Reactions: osm0sis

abaaaabbbb63

Senior Member
Nov 19, 2011
3,895
3,426
Somewhere Over The Rainbow
i was wondering when i connect my nexus 5 to the adb it shows null/empty when i type " adb devices" however when i tried "adb devices" when i was in twrp/recovery mode the phone was recognize and my serial number pops up ,,, can i flash factory image if i connect the phone when it was in twrp/recovery mode ?? or should i only flash it when adb recognize my nexus 5 in bootloader mode ??

You can only flash the factory images when your phone is in the bootloader.
 

audit13

Inactive Recognized Contributor
Jun 4, 2012
12,911
5,052
Toronto
You can only flash stock images with an unlocked bootloader using fastboot commands and with the phone in bootloader mode.
 

maRci002

Member
Jun 6, 2014
7
0
How can I root my Nexus 5 device

I have made it several times but when Marshmallow came out 6.0.0 (MRA58K) the SuperSu broke my phone (infinity bootloop) so I've reflashed my phone whithout rooting. I've read that I need to use a different kernel from the original one and then it might work.
So the question is: do I still need a differenet kernel to root my phone 6.0.1 (MMB29S)?
 

GioVIP

Senior Member
Jan 11, 2016
52
17
Here's what you should do if you want to flash the tamper reset zip file and still keep the stock recovery.

1. Download the tamper zip to your device
2. Google nexus 5 teamwin and download the twrp image file to your pc, we are not going to install it, only boot into it
3. Rename the twrp file you downloaded to twrp.img. This is for simplicity
4. Boot into the bootloader on your device and connect it to your pc
5. Open a command window in the same folder as twrp.img is in
6. In that command window type the command, fastboot boot twrp.img. This will boot you into twrp but not install it
7. When you're booted into twrp then install the tamper.zip you downloaded in step 1

Reboot and the tamper flag still be reset and you'll still have the stock recovery on your device.

Sent from my Nexus 9 using XDA Free mobile app


helllo bro, i see yo are good at nexus 5 development. can you help me ? in bootloader my N5 shows D821e 32GB, but in storage only 16 is available. i flashed factory image several times them did #post 2 , but still got 16gb. what can i do to fix this issue ? i tied flashing kit-kat, lollopop and marshmallow but still got 16 GB, than rooted and tried custom roms but still 16 :)
 

demonr6

Senior Member
Apr 20, 2012
296
76
Hey guys my wife's phone has been acting like crap since the update to MM with almost constant reboots. I wanted to go back to Lollipop because it never had issues with that flavor of the OS. Would I download the 5.1.1 from the factory images link and follow the same instructions in this OP? Thanks!
 

demonr6

Senior Member
Apr 20, 2012
296
76
One small issue I ran into using method 1.. I extracted the file that I downloaded from the link provided then ran the flash-all.bat file and the image below is the result. I unlocked the bootloader in advance and the phone is in the ready state. Not sure what I boned any suggestions?

ukFy4l3.png
 

GioVIP

Senior Member
Jan 11, 2016
52
17
One small issue I ran into using method 1.. I extracted the file that I downloaded from the link provided then ran the flash-all.bat file and the image below is the result. I unlocked the bootloader in advance and the phone is in the ready state. Not sure what I boned any suggestions?

ukFy4l3.png

try second method, manually write commands in cmd
 

demonr6

Senior Member
Apr 20, 2012
296
76
I was able to get into it with the second method. Unfortunately flashing back to Lollipop did not fix the rebooting issue. To make matters worse after I attempted to lock the bootloader again the handset went into a constant bootloop so the phone has moved on to a better place. *hint tonight it will burn in a funeral pyre with a great big F* you to LG* Thanks for the help though, sucks this only lasted barely two years before taking a dump.
 

Spud37

Senior Member
Dec 1, 2011
430
14
Is there a way to flash the stock from TWRP? I don't care about changing the recovery I just want Stock ROM
 
Last edited:

GioVIP

Senior Member
Jan 11, 2016
52
17

Attachments

  • 12419366_975277052509985_2250906644974955011_o.jpg
    12419366_975277052509985_2250906644974955011_o.jpg
    216.9 KB · Views: 206
  • 12495047_975276142510076_1084929592641832414_n.jpg
    12495047_975276142510076_1084929592641832414_n.jpg
    23.9 KB · Views: 213
  • Screenshot_20160128-173114.png
    Screenshot_20160128-173114.png
    40.4 KB · Views: 212

tedrogers61

Senior Member
Aug 18, 2012
520
83
That is most peculiar! The OS is not recognising that you have a 32gb, yet a root level app (presumably root level) detects capacity correctly. If it is unrooted, then I'd be tempted to try re-rooting and un-rooting, then try whole process again.

Sent from my Nexus 5 using XDA Premium 4 mobile app
 

GioVIP

Senior Member
Jan 11, 2016
52
17
Boot in recovery (Stock one), clear cache and factory reset.

If that didn't work, boot in fastboot and write the following commands:
Code:
fastboot erase boot
fastboot erase cache
fastboot erase recovery
fastboot erase system
fastboot erase userdata

Then proceed by using the "flash-all.bat" in the factory images again.

Edit: Added fix to post #2.
i did just like you said but got again 16 instead of 32 :(
 

Top Liked Posts

  • There are no posts matching your filters.
  • 455
    How to flash a factory image | Return to stock | Unroot your Nexus 5​




    Hello Nexus 5 Users!

    I see many users that don't know how to restore their Nexus 5 to stock. It may come in handy, either when you want to RMA your phone, or you want to fix your mistakes.

    This process consists of wiping and flashing every partition on your phone.



    Post-Divider-LG.jpg

    WARNING!!!

    This process will wipe your phone completely. Backup your storage before you continue.
    I am also not responsible if anything goes wrong.
    You can backup apps and data using this app before you unlock the bootloader. No root required.
    https://play.google.com/store/apps/details?id=com.koushikdutta.backup

    DO NOT USE TOOLKITS!!!
    They have a high probability of something going wrong if you don't know how to use them (which, if you managed to ruin your phone, probably is true).
    I don't mean to offend toolkit creators. They did a great job. But don't risk bricking your phone just because you're lazy.

    FOLLOW EVERY SINGLE STEP
    or you will end up in a bootloop, or with a bricked phone, and we don't want that

    Let's get started!!



    Post-Divider-LG.jpg

    Setting up



    For this process, you will need to:

    Install adb and fastboot drivers. To do this, use this simple program:
    http://xdaforums.com/showthread.php?t=2588979

    For MAC users:
    http://xdaforums.com/showthread.php?t=1917237
    *Note: MAC users will have to write "./" before every command in this thread. For example : "./fastboot flash recovery recovery.img"

    Linux users should know their stuff :D
    http://xdaforums.com/showthread.php?t=921169

    Download the latest factory images from here:
    https://developers.google.com/android/nexus/images#hammerhead

    ★Extract the archive you downloaded (Use WinRAR). It should look like this:
    Untitled.png


    ★Unlock your bootloader (In case you haven't already)
    http://xdaforums.com/showthread.php?t=2507905







    Post-Divider-LG.jpg

    Flashing the factory images



    There is more than one way to flash these images. Each give the same results, but the first is for lazy users :D

    Method 1. Easy method.

    ★Power off your phone.

    ★Boot it in fastboot by pressing and holding Power Button + Volume Down at the same time.

    ★Connect your phone to your PC through your USB cable.

    ★Execute the flash-all.bat script (for Windows) or flash-all.sh(for MAC and LINUX) in the folder where you extracted the factory images.
    **!!** For MAC: You'll have to edit the flash-all.sh, and add "./" before every fastboot command.

    ★Wait for everything to flash.

    ★After everything finished, select "Recovery" using the volume buttons.

    ★When a small dead Android appears, press Power Button + Volume UP.

    ★Select "Wipe data/factory data reset"

    ★Reboot. Your phone should be stock. :good:


    Method 2. Long method (Same thing actually, but I'll mention it, just in case)

    ★Extract the image-hammerhead-krt16m.zip somewhere easy to access (On C:\image-hammerhead-krt16m for example). You will have the following files:

    Untitled2.png


    ★In the same folder, from the original archive, extract "bootloader-hammerhead-XXXXXX.img" (rename it to bootloader.img) and "radio-hammerhead-XXXXXX.img" (rename it to radio.img)

    ★Power off your phone.

    ★Boot it in fastboot by pressing and holding Power Button + Volume Down at the same time.

    ★Connect your phone to your PC through your USB cable.

    ★Open a CMD window.

    ★Type the following commands, in this order (If you have a different folder, change the path)
    Code:
    fastboot flash bootloader C:\image-hammerhead-krt16m\bootloader.img
    fastboot reboot-bootloader
    fastboot flash radio C:\image-hammerhead-krt16m\radio.img
    fastboot reboot-bootloader
    fastboot flash system C:\image-hammerhead-krt16m\system.img
    fastboot flash userdata C:\image-hammerhead-krt16m\userdata.img 
    [B](Note: this command will wipe your device (including \sdcard), EVEN if your bootloader is already unlocked.)[/B]
    
    fastboot flash boot C:\image-hammerhead-krt16m\boot.img
    fastboot flash recovery C:\image-hammerhead-krt16m\recovery.img
    fastboot erase cache
    fastboot flash cache C:\androidimage\cache.img
    ★After everything finished, select "Recovery" using the volume buttons.

    ★When a small Android appears, press Power Button + Volume UP.

    ★Select "Wipe data/factory data reset"

    ★Reboot. Your phone should be stock. :good:

    For both methods:
    Optional, but necessary for warranty purposes - Locking your bootloader+ Resetting the tamper flag


    The Nexus 5 has a thing implemented in the bootloader called a tamper flag. This shows if the bootloader was ever unlocked, kind of like the flash counter with Samsung devices. You can check it out by using this command
    Code:
    fastboot oem device-info
    . If the tamper flag value is "true", then you'll have to reset it. The following steps will reset this flag, and lock your bootloader.

    •So, first, go here: http://xdaforums.com/showthread.php?t=2239421 (Thanks @osm0sis for the wonderful script)
    Under "Flashable Zips", you will find the Nexus BootUnlocker script. Download it.

    •Put the zip on your internal storage.

    •Download the latest TWRP recovery image for the Nexus 5 from here: http://techerrata.com/browse/twrp2/hammerhead. Rename it to TWRP.img.
    Now boot it. You do this by booting your phone in fastboot, and typing the following command:
    Code:
    fastboot boot path\to\TWRP.img
    *Replace path\to\TWRP.img with the path to where you placed the TWRP.img. For example: C:\Users\X\Desktop\TWRP.img
    TWRP recovery should boot.

    •When TWRP finished booting, select "Install", then navigate to the Nexus Bootunlocker zip, select and install it. After it finished, use the back arrow at the bottom of the screen to go back, and reboot.

    Now your bootloader should be locked, and your tamper flag should show "false".





    And now you have a brand new Nexus 5 (software wise, at least)




    Post-Divider-LG.jpg

    Video Guides

    Thanks @techfanatic9.


    For Windows:



    For Mac:



    Another easy and fast tutorial made by @iTzGavin96



    These don't contain the tamper flag reset step. Follow the steps above to reset your tamper flag value to "False" for warranty purposes.






    If you now have 12 GB of storage instead of 28 GB, go to post #2
    112
    Post-Divider-LG.jpg

    32 GB phone now shows 16GB?





    This is a common problem with Nexus devices. Minor error, don't panic. Here is how to fix it

    ★Boot into STOCK RECOVERY (If you are running a custom recovery you must flash to stock)
    ★Clear cache
    ★Factory reset

    If that didn't work, proceed with doing this:

    Open a CMD window, boot your phone is fastboot, and write the following commands, in this order (Considering that you have extracted the images in C:\image-hammerhead-krt16m\)
    Code:
    fastboot erase boot
    fastboot erase cache
    fastboot erase recovery
    fastboot erase system
    fastboot erase userdata
    
    fastboot flash bootloader C:\image-hammerhead-krt16m\bootloader.img
    fastboot reboot-bootloader
    fastboot flash radio C:\image-hammerhead-krt16m\radio.img
    fastboot reboot-bootloader
    fastboot flash system C:\image-hammerhead-krt16m\system.img
    fastboot flash userdata C:\image-hammerhead-krt16m\userdata.img 
    (Note: this command will wipe your device (including \sdcard), EVEN if your bootloader is already unlocked.)
    
    fastboot flash boot C:\image-hammerhead-krt16m\boot.img
    fastboot flash recovery C:\image-hammerhead-krt16m\recovery.img
    49
    Post-Divider-LG.jpg

    Credits and Thanks:

    @efrant
    For his thread in the N4 section (http://xdaforums.com/showthread.php?t=2010312)

    @osm0sis
    For contributing a lot with the tamper flag part

    @dombera
    For inspiring me to make this guide

    @Chromium_
    For not replying to my Hangouts messages

    @jd1639
    For helping me out with this thread.

    @Prodeje79
    For pointing problems out.


    7
    OMG! DUDE THANK YOU!! im so noob at this lol:p valuable lesson learned READ INSTRUCTION!

    Your welcome! Have a nice day :D
    6
    Because I don't know better, why do you consider method 2 safer?

    Well, safer in the sense that you are more in control, and can only flash certain images, not all. I'll delete de safe part.





    Is there an easier "hide root" or any way to unroot where AirwatchMDM or other MDM corporate software control crap is hidden?

    I'm wanting to try register on airwatch then lock down airwatch and see if I could still use touchdown.

    If you have root through the "SuperSU" app, it has an unroot option.