[Moto G 2014 LTE Q&A, Help & Troubleshooting] ErvinsAndroid:
After a lot of trial and error and experiments with custom roms I decided to post my own guide to to restoring Moto G XT1039 to stock firmware. I take no responsibility if you brick or do whatever damage to your device using this guide. Use it at your own risk: It was tested on my xt1039 and worked for me.

What you will need;

A stock firmware (rom) for Moto G XT1039 or Peregrine, or Moto G 4G LTE as it is often called. Make sure you get the right one for your region and device as well.

The best place to get one is from here http://firmware.center/firmware/Motorola/Moto%20G%20%281st%20gen-2013%29/Stock/XT1039/

The website is called firmware center http://firmware.center/ and maybe you can get other useful firmware there too for your devices.

To get the right firmware, check your model number (e.g xt1039) check the one you want for your region, Brasil, Eu (Europe), US,, others.

Check your device's cid to ensure compatibility, Mine is cid7. To do that reboot your device to bootloader by pressing power and volume down when turning the device on. Connect the device to your computer, open a terminal or a cmd (if in windows) and type "fastboot getvar all". For fastboot to work properly you should use fastbooot commands as root in Linux based systems and probably open cmd as administrator in Windows. After pressing lines will appear with info about your device, among those lines there is the cid value as well, for example something like, cid: 0x0007. You can use the cid getter from google play if you can use google play in your device.

Now that we got the cid and the model, choose one firmware from the link above, make sure it is in xml.zip format, has your cid value, for example 7 is the 0x0007 and select one for the region that you live and want to use your device in.

Next is setting up your computer, obviously you need a usb cable, and you need to have configured fastboot and adb drivers in your device correctly. For myself I use Fedora so I just installed android-tools. You can installed them from the terminal as root or your can install from yumex or yumex-dnf. You can't find them in gnome-software. I have no experience for Windows, or Mac, so do your own search about those devices.

So after downloading your firmware, which should be in xml.zip format, extract it and note the folder where you extracted it. For example in downloads, or documents or other folder.

Create a directory - Point the terminal to the extracted xml.zip folder.

It should be something like this, cd ~/Downloads/nameoffolder.xml.zip or as root cd /home/yourusername/Downloads/folder.xml.zip

It could be downloads, it could documents, you should know where you saved your xml.zip firmware file.

And now type these commands

su

yourrootpassword

fastboot devices

If you see a number a space and a name, it means your device has been recognized by fastboot.

Note: For the Indian version, the initial command might be fastboot.exe oem lock begin but I am not sure if this applies to xt1039 or not.

fastboot oem lock begin

fastboot flash partition gpt.bin

fastboot flash motoboot motoboot.img

fastboot flash logo logo.bin

fastboot flash boot boot.img

fastboot flash recovery recovery.img

Now at the next step, you should look into the extracted xm.zip folder and see how the sparsechunks are named and how many are there. Some people including myself made mistakes here by simply copying commands from tutorials which said for instance

fastboot flash system system.img_sparsechunk1 while in fact it was fastboot flash system system.img_sparsechunk.0

So you should flash the names of the sparsechunks found in your extracted folder according to their names from the lower number to the highest. In my Eu firmware there were four sparsechunks names sparsechunk.0 .1 .2 and .3

So you should type

fastboot flash system system.img_sparsechunk.0

fastboot flash system system.img_sparsechunk..1

fastboot flash system system.img_sparsechunk..2

fastboot flash system system.img_sparsechunk..3

If it is the same in the folder your extracted, maybe they are named sparsechunk1 or there are not four of them, maybe there are three, you should look into that and make sure.

Ok once done with the sparsechunks

Continue with these commands

fastboot flash modem NON-HLOS.bin

fastboot erase modemst1

fastboot erase modemst2

fastboot flash fsg fsg.mbn

Optional (Recommended to avoid any possible errors but make sure you don't delete any important data. However it is always a good idea to backup what you need to backup before you flash anything on your device)

fastboot erase cache

fastboot erase userdata

And finally

fastboot oem lock.

There was an error message, I forgot what it was, but my device actually got locked.

Note: In some places you will see the mfastboot command. In Fedora that command does not exist. So I figure that is for Windows. mfastboot and fastboot do the same thing.

Now even after locking the device the bootloader unlocked warning message will remain. I think that is because Motorola wants to tell others that there is a chance that critical components of the device could have been altered and the firmware might be not original because it's bootloader got unlocked.

If you get annoyed by this message, and your phone is working perfect and perhaps you would like to sell it, there is a solution for this.

The solution is shown here. All you need to do is to flash an alternative or modified version of logo.bin. You can do this even when the device is locked again.

fastboot flash logo logo.bin.

Many sites give you a zip to install, but unless you have twrp or a custom recovery, this method won't work when you are doing a full stock firmware recovery like in this guide.

First go to your extracted.xml.zip folder and copy the logo.bin and save it somewhere else.

I installed hexedit in my Fedora computer and edited the logo.bin file (the original one inside the extracted xm.zip) using hexedit. Simply to make the unlocked bootloader logo and motoboot logo the same.

The guide for this is here https://www.reddit.com/r/MotoG/comments/2jucwe/removing_the_unlock_warning_on_boot_miniguide/

To edit wit hexedit (which only works as a terminal command)

cd to the path where logo.bin is, the same as cd-iing when flashing the firmware above.

type hexedit

type logo.bin and edit the logo.bin file according to the instructions set above. No need to do the pulling of logo.bin from the device using the command "# dd if=/dev/block/platform/msm_sdcc.1/by-name/logo of=/sdcard/logo.bin count=1 bs=1085857" described above.

For your convenience I have also attached the logo.bin file, the modified one that I used to remove the bootloader locked warning in my own phone.

Download the logo.bin from here and flash it using fastboot flash logo logo.bin.

https://mega.nz/#!XFRGxYhR!YApyTk1oY-aSKCHt_1r3TJWkO4FmrJfOzLl08X-Pmc8