[Moto G5 Plus Guides, News, & Discussion] NZedPred:
Create TWRP from full fastboot image

Create TWRP Flashable from Fastboot Image

First off, make sure you have the SparseConverter and TWRP template zip from the second post. These weren't needed for building from an existing TWRP flashable zip, but are necessary for this.

1) Get an appropriate starting firmware

At the time of writing this, the April 2018 security patches are available for the Indian variant (NPNS25.137-92-10) and US variant (NPNS25.137-93-10). Note that these firmwares are used on other regions as well (e.g. the US firmware can be used on retapac). Refer to other threads in the forum if you aren't sure. Ideally you should stick with the firmware for your region, as e.g. there may be differences in the modems, etc.

Indian (NPNS25.137-92-10) https://mirrors.lolinet.com/firmware/moto/potter/official/RETAIL/POTTER_RETAIL_7.0_NPNS25.137-92-10_cid50_subsidy-DEFAULT_regulatory-DEFAULT_CFC.xml.zip

US (NPNS25.137-93-10) - refer to this thread https://xdaforums.com/g5-plus/development/april-security-patch-xt1687-npns25-137-t3796797

Extract the zip file and enter the folder (rename it if you want for convenience).

2a) Convert the sparse chunk files into a (Motorola) image file

Use SparseConverter to convert the sparse chunks into an image file:

SparseConverter.exe /decompress system.img_sparsechunk.0 system.img.moto

The first parameter says that we are decompressing (obvious). The second is the name of the very first sparse chunk file - we only need to specify the first. The third and last parameter is the destination file to be created.

The reason for naming it system.img.moto (i.e. with moto on the end) is because Motorola have added a 128KB header and 4KB trailer. These will need to be removed later.

2b) Convert oem.img from sparse image to raw image

Confusingly, sparse image files and raw image files typically have the same extension - img. oem.img is a sparse image file, and must be converted to raw image. Similarly to the system image, it has a 128KB header and 4KB trailer.

mv oem.img oem.simg.moto
simg2img oem.simg.moto oem.img.moto

2c) Convert the modem (NON-HLOS.bin) to raw image

The modem file, although it has a .bin extension, is in sparse image format.

simg2img NON-HLOS.bin modem.img

This particular file does not have a header or trailer.

3) Remove header and trailer from image files

We will use dd and truncate to remove the header and trailer respectively from the system and oem image files:

dd if=system.img.moto of=system.img bs=131072 skip=1
truncate -s -4096 system.img

dd if=oem.img.moto of=oem.img bs=131072 skip=1
truncate -s -4096 oem.img

4) Extract template and copy files

In a separate folder, extract the TWRP Template zip file into a folder, and rename the folder to something more reasonable, e.g. twrp-flashable-NPNS.25.137-xx-xx.

Copy each of the following files into the template folder:

system.img

oem.img

modem.img

boot.img

fsg.mbn

adspso.bin

logo.bin

E.g. use a command like the following, adjusting the paths as necessary (the last parameter is the location of the template folder that I renamed in this case):

cp -fv adspso.bin boot.img fsg.mbn logo.bin modem.img oem.img system.img ../twrp-flashable-NPNS25.137-92-10/

Note that of those above, the first three were created in our previous steps, the others (boot.img etc) were already there with the other fastboot files.

The updater-script, located at META-INF/com/google/android/updater-script is already set up to take the steps that are done in the fastboot images, except it does not update the bootloader-related files.

At this stage, you could just go and zip up all of the files and flash. However, there are a few things you may want to do first. Ensure you are in the template folder before doing any of the following.

5) Disable dm-verity and forced encryption in boot

To do this, follow the same instructions as the previous post - step 4. Note the two different methods, one for Nougat, one for Oreo.

6) Change the logo image

Note that the logo.bin file is from stock, and therefore it will have the unlocked bootloader warning. You could replace it with the logo.bin from the other TWRP flashables, or you may want to grab one from the Themes forum, and (as I did) pick a nice one that hides the N/A.

7) Convert images to sparse images, then to sparse data

This step is the same as the previous post, step 5. It is repeated here for convenience.

Make a temporary folder for the sparse image files that we will create:

mkdir tmp
img2simg system.img tmp/system.img
img2simg oem.img tmp/oem.img
img2simg modem.img tmp/modem.img

Create the sparse data files from the sparse images:

img2sdat.py tmp/system.img -v 4 -p system
img2sdat.py tmp/oem.img -v 4 -p oem
img2sdat.py tmp/modem.img -v 4 -p modem

8) Remove working files and folders and zip

The folders we created in previous steps can now be removed:

rm -rf tmp bootimg

The temporary img files can now be removed:

rm -fv system.img oem.img modem.img

9) Create the zip file

Ensuring that you are in the template folder, execute the following command to create the zip:

zip twrp-flashable-NPNS25.137-92-10.zip -r *

Make sure to change the zip filename as appropriate.

10) Flash

Copy to your phone's SD Card

BACKUP BACKUP BACKUP Yes, the same warning as per usual!

Flash the zip file as usual.

Edit log

2018-06-29 - fixed references to logo.img, as they are actually logo.bin. Added -fv to remove temporary files.