Saturday, November 30, 2013

How to flash android factory image without wiping your device - Unbrick your rooted android after a failed OTA update from 4.3 to 4.4 kitkat

I own a Nexus 7 WiFi (2012) on Android 4.3, unlocked and rooted, stock rom.
Long story short: I received the OTA update to android 4.4, I executed the update... and it bricked my Nexus 7.
I've been able to restore my tablet without wiping it by installing the 4.4 factory image.

Installing a factory image will wipe your device, but it's possible to modify the installation script so that it wont delete userdata, nor wipe the device.

About the rooting
When I received the OTA update for 4.4 id didn't unroot before installing it, after all I updated from 4.1 to 4.2 and from 4.2 t 4.3 without unrooting... but this time it didn't work out.
So, keep a note for myself: always unroot before applying system update.
(anyway, this kind of error happened even to person with stock unrooted tablet... so I will never know if the error I got during the update was due to my tablet being rooted)

Fixing the reboot loop
When I got the error my tablet then got stuck in a reboot cycle.
Tablet start up, and then after some time show a "no comand" error, and then reboot, and continue like this...
In this situation I wasn't unable to turn off the tablet, and I didn't want it to drain all the battery.
Looking for a fast and effective way to turn it of, I decided to open it up an detach it's battery: it's an extreme way to get the job done, and (obviously) it worked :-)
Then after some searching I found a better way that doesn't involve dismantling the tablet: simply  push Vol Down + Power at the same time, and if you push then just after the tablet started the boot sequence, this should take you to the Recovery Mode menu, from here you can turn off the tablet: push the VolDown button until you see the "Power Off device" command, and then press the power button.

Reinstalling the update
After some  searching I've decided to simply retry to update the tablet, by sideloading the update with adb.
I've followed this guide.
Here is what I did:
  • I updated my android SDK by using  SDK Manager.exe
    • this may update the driver of your device, so maybe the computer wont see the device when you connect it: in this case you need to go in the "device manager" and force it to update the driver  (you can find the driver in "sdk\extras\google\usb_driver") you need to install the "Android Composite ADB Interface" driver, more info here
  • I downloaded the file update for my tablet (see the linked guide for this)
  • I rebooted my pc
  • I disabled firewall
  • I disabled antivirus
  • I closed any running software (dropbox, skype, gTalk....)
Then I followed the guide: powered on the tablet, go to the recovery mode, open command  prompt on my PC, sideload the update with adb sideload command and... I got another error.
So I wiped the cache from the recovery menu, and tried one more time, just to get the same error again, something like:
Error!

bla...
bla...
bla...

Symlinks and permissions...
set_metadata_recursive: some changes failed
E:Error in /tmp/update.zip
(Status 7)
Installation aborted.
here is a screenshot of the full error message:


Reinstalling the update didn't work out, so I started looking for alternative solutions.
The only solution seemed to be to reinstall a factory image.
The issue with the factory image is that they will wipe your device, and I dont want to wipe my device because reinstalling and reconfiguring all the app take a tons of time (I have about 300 app on my tablet, with file manager, dropbox, synology cloudstation file sync, multiple account, multiple calendars, email, messaging...)

So I decided to try everything I can before doing a factory reset.

After much reading, I understood that it should be possible to install factory image without wiping the device. But to do this, you need to modify the installation script.
The procedure I'm going to describe here is a mix of the information I found on various website on the net.

Keep in mind that this procedure may not work, and it may brick your tablet/phone.
I'm not responsible of any damage/malfunction to your device.
Follow the described steps only if know what you are doing.

Steps to install a factory image without wiping your device
Download the factory image for your device from this page
Decompress it in a folder, and you'll have something like that:

Then you need to edit the "flash-all.bat"
The original "flash-all.bat" looks like this:
@ECHO OFF
:: Copyright 2012 The Android Open Source Project
::
:: Licensed under the Apache License, Version 2.0 (the "License");
:: you may not use this file except in compliance with the License.
:: You may obtain a copy of the License at
::
::      http://www.apache.org/licenses/LICENSE-2.0
::
:: Unless required by applicable law or agreed to in writing, software
:: distributed under the License is distributed on an "AS IS" BASIS,
:: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
:: See the License for the specific language governing permissions and
:: limitations under the License.

PATH=%PATH%;"%SYSTEMROOT%\System32"
fastboot oem unlock
fastboot erase boot
fastboot erase cache
fastboot erase recovery
fastboot erase system
fastboot erase userdata
fastboot flash bootloader bootloader-grouper-4.23.img
fastboot reboot-bootloader
ping -n 10 127.0.0.1 >nul
fastboot -w update image-nakasi-krt16s.zip

echo Press any key to exit...
pause >nul
exit
This installation script will totally wipe your device.
I've highlighted in orange the culprit of the wiping.

The first orange line "fastboot oem unlock" will unlock your device.
I removed this line because I know that my tablet is already unlocked.
I know that unlocking a locked tabled will wipe all user data.

The second orange line "fastboot erase userdata" will erase all user data, to my undestanding this mean deleting your data/image/music/apps. So I removed this line because I want to keep all my user data.

On the last line I've removed the "-w" from the update command, the -w parameter tell the update command to wipe your device. And I don't want to wipe my device.


Here is my modified "flash-all.bat" script
@ECHO OFF
:: Copyright 2012 The Android Open Source Project
::
:: Licensed under the Apache License, Version 2.0 (the "License");
:: you may not use this file except in compliance with the License.
:: You may obtain a copy of the License at
::
::      http://www.apache.org/licenses/LICENSE-2.0
::
:: Unless required by applicable law or agreed to in writing, software
:: distributed under the License is distributed on an "AS IS" BASIS,
:: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
:: See the License for the specific language governing permissions and
:: limitations under the License.

PATH=%PATH%;"%SYSTEMROOT%\System32"
fastboot erase boot
fastboot erase cache
fastboot erase recovery
fastboot erase system
fastboot flash bootloader bootloader-grouper-4.23.img
fastboot reboot-bootloader
ping -n 10 127.0.0.1 >nul
fastboot update image-nakasi-krt16s.zip

echo Press any key to exit...
pause >nul
exit
You CAN'T copy/paste this script, because it include path/filename/command specific for my device factory image (Nexus 7 WiFi v2012 Android 4.4): You should instead edit the script included in the factory image file for you device!
 
KEEP IN MIND THAT I'M NOT AN ANDROID EXPERT.
THIS SCRIPT MAY BRICK YOUR DEVICE

To my understanding maybe this script will work for you if:
  • you are doing a "small" upgrade, like from android 4.3 to android 4.4
  • you device is already unlocked
  • you know the meaning of the following words: adb, sdk, command prompt, bootloader, recovery, locked/unlocked device, flash, partition, cache, firmware, root, mod, rom, hack

 This script probably will not work if:
  • you are doing a big upgrade, like from Android 4.3 to Android 5.0
  • your device is locked

This script is modified to try to not wipe your data, so it's totally possible that it will leave some unneeded file somewhere on your tablet.
I dont know if future OTA update will work. Update: my tablet just received the OTA update to Android 4.4.2, and it worked! :-)

As of today, this modified script saved me a tons of work: now my Nexus 7 is once again up and running, on Android 4.4.
Now it's unrooted, maybe I will try to reroot it in future.

In case you need it, here is a link to the linux version of this script modified in the same way by James Finstrom: it seem he is the only one that did something similar.

Feel free to ask any question in the comments.