Sunday, May 24, 2015

Moto G OTA update error

If you get an OTA (over-the-air) update for your Moto G and it fails, here's what you can do. First, a few requirements:

  • A Moto G. This may work with the Moto E or X as well. YMMV.
  • Retail Android. This goes without saying, but you won't get an OTA update unless you're running one of the stock/retail ROMs.
  • Your phone has been rooted or otherwise customized (custom boot logo, custom recovery, etc). This isn't a strict requirement, but it's probably the reason the update is failing in the first place.
  • These instructions were written for Linux, but they should run with minor adjustments on Mac and a few not-so-minor adjustments on Windows.
There are two main ways to go about resolving this:

Method 1: recommended
This method is the easiest and probably the fastest:
  1. (Recommended) Back up your phone in case something goes wrong

  2. Get the model number for your phone
    Settings → About phone → Hardware SKU


  3. Get the build number for your phone
    SettingsAbout phoneBuild number

  4. Download the retail firmware for your phone corresponding to the phone's model and build numbers. You can download them from here:

    For me, I had the Moto G XT1034 with build number KXB21.14-L1.61, so I downloaded this file:

  5. Once the retail firmware has finished downloading, extract it

  6. Install the Android SDK tools

  7. Put your phone into fastboot mode. To do so, power off the phone, press and hold the volume down button, press power button, hold both for about 3 seconds and then let go

  8. Go to the folder where you extracted the retail firmware. Now we're going to restore the stock firmware without erasing the data partition (where all of your apps, settings, etc are stored):

    sudo fastboot flash boot boot.img
    sudo fastboot flash recovery recovery.img
    for file in system.img_sparsechunk.*; do sudo fastboot flash system $file; done
    sudo fastboot reboot

    If you're using Windows, you'll have to flash the system.img_sparsechunk files on at a time, for example:

    fastboot flash system system.img_sparsechunk.0
    fastboot flash system system.img_sparsechunk.1

    ... and so on.

  9. Now try the update again
    SettingsAbout phone → System updates

  10. If the update fails again, you have one of two options. If you don't mind wiping your phone and starting from scratch (which will erase everything), the easiest thing to do is to do fully restore the stock firmware. You'll put your phone into fastboot mode as before, but this time you're going to overwrite everything:

    sudo fastboot flash partition gpt.bin
    sudo fastboot flash motoboot motoboot.img
    sudo fastboot flash logo logo.bin
    sudo fastboot flash boot boot.img
    sudo fastboot flash bootloader bootloader.img
    sudo fastboot flash recovery recovery.img
    for file in system.img_sparsechunk.*; do sudo fastboot flash system $file; done
    sudo fastboot flash modem NON-HLOS.bin
    sudo fastboot erase modemst1
    sudo fastboot erase modemst2
    sudo fastboot flash fsg fsg.mbn
    sudo fastboot erase cache
    sudo fastboot erase userdata
    sudo fastboot reboot

    If, however, you don't want to completely wipe your phone and you don't mind getting your hands dirty, read on.

Method 2: advanced
  1. At a minimum, you will need to make sure the stock recovery is flashed. Follow the instructions above, flashing only the recovery:

    sudo fastboot flash recovery recovery.img
    sudo fastboot reboot

  2. Now try to apply the OTA update. If it fails, now the fun begins! First of all, if your phone isn't rooted, you'll need to root it to get access to the update log files. If you flash a custom recovery to root, then flash the stock recovery back (the OTA will fail with a custom recovery).

  3. Once your phone is rooted, connect to it using adb and look at the update log, which will be located in /cache/recovery/last_log

    adb shell
    more last_log

  4. Look particularly at the end of the log file to see what went wrong. This is what mine said:

    script aborted: "/system/etc/" has unexpected contents.
    "/system/etc/" has unexpected contents.
    E:Error in /cache/
    (Status 7)
    Installation aborted.

    You can see that apparently the file /system/etc/ had been modified, probably from when I rooted the phone.

  5. If the file causing errors is in this list:
    • /system/bin/app_process32
    • /system/bin/app_process64
    • /system/bin/
    • /system/etc/

    • Then the problem is SuperSU itself, which modifies a bunch of files when it's installed. You'll need to restore the files from backup:

      cp /system/bin/app_process32_original /system/bin/app_process32
      cp /system/bin/app_process64_original /system/bin/app_process64
      cp /system/bin/ /system/bin/
      cp /system/etc/ /system/etc/

  6. If another file has been modified, you need to replace the original file. This will take some work. First, you'll need a special utility called simg2img. You can probably find a pre-built binary for your system. I just compiled it myself:

    sudo apt-get -y install build-essential git zlib1g-dev
    git clone
    cd core/libsparse
    gcc -o simg2img -Iinclude simg2img.c sparse_crc32.c backed_block.c output_file.c sparse.c sparse_err.c sparse_read.c -lz
    sudo cp simg2img /usr/local/bin

  7. Go to the folder where you extracted the retail firmware, and you should have one or more files starting with system.img_sparsechunk. The next step is to combine them using simg2img:

    simg2img system.img_sparsechunk* system.raw.img.tmp

  8. Now we're going to extract the ext4 partition from the image file we made:

    offset=`LANG=C grep -aobP -m1 '\x53\xEF' system.raw.img.tmp | head -1 | awk '{print $1 - 1080}'`
    dd if=system.raw.img.tmp of=system.raw.img ibs=$offset skip=1

  9. Getting close! Now, we'll mount the image we just created:

    sudo mkdir /mnt/system
    sudo mount system.raw.img /mnt/system

  10. Now we should have access to all of the original files from the retail firmware. The next step is to copy them to the device. Here's what I did to copy /system/etc/

    adb push /mnt/system/etc/ /sdcard/

  11. Now connect to the device and get the mount point for the /system partition:

    adb shell
    mount | grep system

  12. Finally, remount /system as read-write, copy the file, and then remount /system as read-only. Don't forget to replace the mountpoints in my example (/dev/block/platform/msm_sdcc.1/by-name/system) with the mountpoint you got from the previous step:

    mount -o rw,remount /dev/block/platform/msm_sdcc.1/by-name/system /system
    cp /sdcard/ /system/etc/
    mount -o ro,remount /dev/block/platform/msm_sdcc.1/by-name/system /system

  13. If you've made it this far, congratulations! Try the update again. Hopefully, as in my case, it will now work. If not, you'll have to look at the update log file again and see if there's anything else you missed. Thankfully it'll go much quicker now that you've already done all the prep work.


Post a Comment