How to make sparse image file from original firmware?


Question

I accidently erased /system partition and now have my Gionee S10C phone bricked.


I downloaded original firmware, but I can see series of system image files.(This firmware is to be used by Qualcomm Flash Image Loader)


system_1.img, system_2.img, ..., system_53.img

When I'm trying to install the original firmware using QFIL by putting the phone in EDL mode, I get sahara fail error. (Same error in here and I tried most of the fixes I can find, but nothing works)


Now, is there any way to use the above 53 system images to restore my phone using fastboot flash command?


I'm sure only system partition is needed to be restored to get my phone working again, because I haven't modified any other partitions.


Answer

concatenate files with dd seek=$offset from xml and then sparse with img2simg


rawprogram0.xml contains


- partition name:
 filename="system.img"
- partition size: num_partition_sectors="8388608"
- partition offset: start_sector="1312768"

rawprogram_unsparse.xml contains system_1.img - system_53.img


- file name:

filename="system_1.img"
- file size: num_partition_sectors="262160"
- file offset: start_sector="1312768"

Note: we don't want the offset so we will subtract from start_sector


1312768 - 1312768 = 0


- file name:

filename="system_1.img"
- file size: num_partition_sectors="262160"
- file offset: start_sector="0"

1576968 - 1312768 = 264200


- file name:

filename="system_2.img"
- file size: num_partition_sectors="16"
- file offset: start_sector="264200"

1581024 - 1312768 = 268256


- file name:

filename="system_3.img"
- file size: num_partition_sectors="256048"
- file offset: start_sector="268256"

Now use the dd seek parameter to paste junks into proper offsets

(sparse is filled with zeros)


bs=$SECTOR_SIZE_IN_BYTES

seek=$start_sector

count=$num_partition_sectors


dd if=system_1.img count=262160 seek=0

 bs=512 of=system_ext4.img
dd if=system_2.img count=16 seek=264200 bs=512 of=system_ext4.img
dd if=system_3.img count=256048 seek=268256 bs=512 of=system_ext4.img
...
dd if=/dev/zero count=0 seek=8388608 bs=512 of=system_ext4.img

the result is mountable ext4 partition image


mkdir system
sudo mount -t ext4 -o loop system_ext4.img system
ls system

finally this file can be sparsed with img2simg


sudo apt install -y f2fs-tools android-tools-fsutils android-sdk-platform-tools-common
img2simg system_ext4.img system.img



GNU bash script with sed


#/bin/bash

shopt -s extglob

bs=512
label=system
input=rawprogram0.xml
output=rawprogram_unsparse.xml

source <(sed -nr "s,.*\s(filename=.${ label } \.img.)\s.*\s(num_partition_sectors=.[0-9]+.)\s.*\s(start_sector=.[0-9]+.).*,\1; \2; \3;,p" "$input" | head -n1)

out=$filename
off=${ start_sector:-0 }
end=${ num_partition_sectors:-0 }

printf '%s\n' "${ label } "_+([0-9]).img | cut -d. -f1 | sort -t_ -k2n | xargs -n1 -I_ echo _.img | \
while read -r file
do
source <(sed -nr "s,.*\s(filename=.${ file } .)\s.*\s(num_partition_sectors=.[0-9]+.)\s.*\s(start_sector=.[0-9]+.).*,\1; \2; \3;,p" "$output" | head -n1)
dd if="$filename" of="$out" bs=${ bs:-512 } count=${ num_partition_sectors:-0 } seek=$((start_sector-off)) status=none || exit 1
echo "filename=$filename num_partition_sectors=$num_partition_sectors start_sector=$((start_sector-off))"
done

dd if=/dev/zero of="$out" bs=${ bs:-512 } count=0 seek=$end status=none || exit 1
echo "filename=$out num_partition_sectors=$end start_sector=$off"

file "$out"
e2fsck -n "$out" | grep clean || exit 1
mv "$out" "${ out%.* } .raw"
img2simg "${ out%.* } .raw" "$out" || exit 1
file "$out"
echo "Done."

exit 0

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes