[Samsung Galaxy Tab A7 Lite] tom.android:
Part 2​

Modified sub-partition of super.img​

Step by Step​

CREATE CustomAP for Odin Flashing with Patching Dynamic Partitions.​

​

*** Uses GSIs with (Vndk)Lite Variant only.(No Slim)​

*** the continue part of Step by Step CREATE CustomAP framework file for GSI Flashing with Odin.

(Required Win10, 11 with wsl2 or Ubuntu or VirtualBox)
​

(Our scope)​

What are we doing now ?​

1) unpacking super.raw with imjtool. ( super.img.lz4 -->super.img-->super.raw)

2) resizing system.img (in order to insert our modifications to the rom)

3) mounting system.img (Read/Write System)

4) do our modifications

5) umounting system

6) shrink edited system.img to the minimal size

7) generating new super.img

8) Create customAP file ( for flashing it to our device with odin.)

( Essencial tools)​

Extract Sub-Partition inside super.img.lz4 tool​

## Download Samsung framework file. (Stock Firmware)

## Extract "super.img.lz4" from (AP framework file) with (unzip FILE.zip) command. or uses utility from win 10

## Decompress "super.img" from "super.img.lz4" with LZ4 command

## sparse format ""super.raw" from "super.img" with simg2img command

## Use imjtool to extract the partitions of super.raw. Notice that partitions in a super image are split into several groups.

(The exact grouping varies across devices/OS versions, so one should always use imjtool to check first.)

wget http://newandroidbook.com/tools/imjtool.tgz && tar xzvf imjtool.tgz

View attachment 5734125​

imjtool v2.0 beta1

View attachment 5734115​

*You may also use the official lpunpack for extraction but it can’t display the group schema.

./imjtool.ELF64 super.raw extract

output:

MMapped: 0x7f5f4a89d000, imgMeta 0x7f5f4a89e000

liblp dynamic partition (super.img) - Blocksize 0x1000, 2 slots

LP MD Header @0x3000, version 10.0, with 4 logical partitions on block device of 5812 GB, at partition super, first sector: 0x800

Partitions @0x3080 in 2 groups:

Group 0: default

Group 1: main

Name: system (read-only, Linux Ext2/3/4/? Filesystem Image, @0x100000 spanning 1 extents of 3 GB) - extracted

Name: product (read-only, Linux Ext2/3/4/? Filesystem Image, @0xe6700000 spanning 1 extents of 1 GB) - extracted

Name: vendor (read-only, Linux Ext2/3/4/? Filesystem Image, @0x12d500000 spanning 1 extents of 685 MB) - extracted

Name: odm (read-only, Linux Ext2/3/4/? Filesystem Image, @0x158300000 spanning 1 extents of 4 MB) - extracted

View attachment 5734131

Note: Notice file size of "system.img" =3GB

Download Samsung Firmware tool​

Install Bifort

Releases · zacharee/SamloaderKotlin

Contribute to zacharee/SamloaderKotlin development by creating an account on GitHub.

github.com

Download "Bifrost_Linux.zip"

Extract to home

change directory to

cd Bifrost/bin

give execute permission

chmod +x Bifrost

Run apps

./Bifrost

View attachment 5734169

CSC Samsung code https://technastic.com/csc-home-csc-samsung-odin/

​

Repack Partition Tools​

Download otatools

otatools.zip

drive.google.com

Extract to home

View attachment 5734153

Give its execute permission or 0755

cd otatools/bin

chmod +x lpmake

Interact with the internal partition’s file systems tools​

## Available in common Linux distributions

Extract lz4 archirve: lz4

Usage :

lz4 [arg] [input] [output]

input : a filename

with no FILE, or when FILE is - or stdin, read standard input

Arguments :

-1 : Fast compression (default)

-9 : High compression

-d : decompression (default for .lz4 extension)

-z : force compression

-D FILE: use FILE as dictionary

-f : overwrite output without prompting

-k : preserve source files(s) (default)

--rm : remove source file(s) after successful de/compression

-h/-H : display help/long help and exit

Sparse Format

simg2img​

Usage: simg2img <sparse_image_files> <raw_image_file>

fallocate, resize2fs, dumpe2fs, e2fsck​

(Inaction Steps, "system.img" modification)​

1.) Format of File​

Let’s say we want to modify system.img. (May be from any GSI )

Firstly, check the file format.

sudo file system.img

Ex.Output

system.img: Linux rev 1.0 ext2 filesystem data, UUID=6a979985-101a-4fa4-bd87-f3ff81ebaaba (extents) (large files) (huge files)

Although it shows ext2, but it’s in fact an ext4 image.

​

2.) Resizing < system.img>​

All imges have been shrunk to their minimum sizes. In other words, there’s zero space left in the partition of which the image file holds. As we would like to add more files to it, we need to firstly enlarge the file to create some space, then expand the file system accordingly.

# Enlarge the file to 2G. (It can be any size)

# allocates more space for system.img 2GB

sudo fallocate -l 2G system.img

ls -lah system.img

Ex.Output

---------- 1 fortuner6898 fortuner6898 2.0G Oct 12 13:18 system.img

# Resize the filesystem

this command increases the file system size of the partion to ....xG

Try

resize2fs system.img 2G

resize2fs 1.46.5 (30-Dec-2021)

resize2fs: New size smaller than minimum (871879)

*Note not fit

TRY

sudo resize2fs system.img 3G

Ex.Output

resize2fs 1.45.5 (07-Jan-2020)

resize2fs: New size smaller than minimum (890902)

*Note not fit

FINAL TRY

resize2fs system.img 4G

resize2fs 1.46.5 (30-Dec-2021)

Resizing the filesystem on system.img to 1048576 (4k) blocks.

The filesystem on system.img is now 1048576 (4k) blocks long.

*Note this time OK

​

3.) Mounting & Problems.​

create mount point directory

mkdir system

sudo mount -t ext4 -o loop system.img system

Ex.Output Error!!

mount: system: mount failed: Operation not permitted.

3.1) Problem 1. came from no loop command.​

*** ERROR MUST BE FIXED​

if you used (wsl1) linux from win 10 "mount" command, changes it to wsl2.​

Resource: Windows Subsystem for Linux.

Loop devices · Issue #4980 · microsoft/WSL

Please implement loop devices. They are SO useful.

github.com

>>>Howto Fix Ubuntu loop device command---> Open Spoiler

Spoiler content hidden.

​

3.2.) Problem 2: came from SHARED_BLOCKS ​

It turned out that system imgage in Android 10+ is formated with EXT4_FEATURE_RO_COMPAT_SHARED_BLOCKS, found by @topjohnwu.

In other words, it’s read-only.

We can run dumpe2fs and check “Filesystem features” to confirm that the image has shared_blocks turned on.

sudo dumpe2fs system.img

Ex.Output

dumpe2fs 1.45.5 (07-Jan-2020)

dumpe2fs: Permission denied while trying to open system.img

Couldn't find valid filesystem superblock.

​

## Solution:

Luckily shared_blocks is merely a restriction at the file level. We can simply run e2fsck to remove it.

sudo e2fsck -E unshare_blocks system.img

​

Ex.Output

e2fsck 1.45.5 (07-Jan-2020)

Pass 1: Checking inodes, blocks, and sizes

Pass 2: Checking directory structure

Pass 3: Checking directory connectivity

Pass 3A: Optimizing directories

Pass 4: Checking reference counts

Pass 5: Checking group summary information

/: ***** FILE SYSTEM WAS MODIFIED *****

/: 7953/8064 files (0.5% non-contiguous), 890901/898552 blocks

​

# # Check that the shared_block feature is gone.

sudo dumpe2fs system.img

Ex.Out

Spoiler content hidden.

​

4.) Now we can mount the partition and do whatever modifications we like.

create folder mount point

mkdir system

sudo mount -t ext4 -o loop system.img system

ls system

Ex.Output

at system folder , manipulate everything from system.img with loop.

View attachment 5733447​

​

4.1) How to modify system​

in order to make changes you should use superuser:

you can use the file explorer with superuser permission by typing;

sudo nautilus

​

when finished your modify task.

sudo umount system

​

5.) Prepare for repacking ​

Once finished, we can start to prepare the image for repacking. The idea is simple: we firstly check and fix the errors in the file system (there’ll always be some, probably due to bugs in the previous tools), then shrink the image file back to its minimum size.

​

sudo e2fsck -yf system.img

Ex.Output

e2fsck 1.46.5 (30-Dec-2021)

Pass 1: Checking inodes, blocks, and sizes

Pass 2: Checking directory structure

Pass 3: Checking directory connectivity

Pass 3A: Optimizing directories

Pass 4: Checking reference counts

Pass 5: Checking group summary information

/: ***** FILE SYSTEM WAS MODIFIED *****

/: 7575/7776 files (0.6% non-contiguous), 871878/880082 blocks

​

# # Shrink the file system to minimum

ls -lah system.img

output

-rw-rw-r-- 1 tom tom 4.0G ต.ค. 13 14:51 system.img

​

sudo resize2fs -M system.img

output

resize2fs 1.46.5 (30-Dec-2021)

Resizing the filesystem on system.img to 871894 (4k) blocks.

The filesystem on system.img is now 871894 (4k) blocks long.

ls -lah system.img

output

-rw-rw-r-- 1 tom tom 3.4G ต.ค. 13 14:54 system.img

​

6.) Prepare formular file-size of LPMAKE​

6.1) # Get the size of all images.​

Before we repack, we need to know the sizes of all images in bytes.

stat -c '%n %s' *.img

Output:

1.) Group main 4 imgs​

odm.img 4349952

product.img 1188118528

vendor.img 718503936

Sum of the group-main​

(fixed size) = 1,910,972,416 ​

+ ​

(variable size) = system.img size​

( 3571277824 <------ This is variable size.)

TOTAL = 5482250240

2.) super.raw (fixed size)= 6094323712​

​

6.2.) Generating new super.img​

The same as Part 1 Command.

Our last quest is to craft a lengthy lpmake command to build the new super.img. Essentially that translates to three things:

-Define the groups

-Define which partition goes into which group

-Assign image files that correspond to each partition

​

Most information can be obtained from the previous imjtool.

Some of the flags to take note of are:

--metadata-slots: same as imjtool’s output

--metadata-size: The maximum size that partition metadata may consume. A partition entry uses 64 bytes and an extent entry uses 16 bytes. I think 65536 should work in most cases.

--group: format of <name>:<size>. The size should be the sum of all sub-partitions under the group

--partition: format of <name>:<attributes>:<size>[:group], attrs must be ’none’ or ‘readonly’.

--image: for each partition, specify a corresponding image file

Once done, you should now have the new super image.

​

[explanation: Not copy and paste]

Prepared command "LPMAKE" Parameter.

sudo ./otatools/bin/lpmake --metadata-size 65536 \

--super-name super \

--metadata-slots 2 \

--device super:ORIGINAL_SUPER_IMG_SIZE \

--group main:SUM_OF_ALL_PARTITIONS_SIZES \

--partition odm:readonly:ODM_PARTITION_SIZE:main \

--image odm=./odm.img \

--partition product:readonly: PRODUCT_PARTITION_SIZE:main \

--image product=./product.img \

--partition system:readonly:SYSTEM_PARTITION_SIZE:main \

--image system=./system.img \

--partition vendor:readonly:VENDOR_PARTITION_SIZE:main \

--image vendor=./vendor.img \

--sparse \

--output ./super_new.img

--‐-‐-------------------------

New command

sudo ./otatools/bin/lpmake --metadata-size 65536 \
--super-name super \
--metadata-slots 2 \
--device super:6094323712 \
--group main:5482250240 \
--partition odm:readonly:4349952:main \
--image odm=./odm.img \
--partition product:readonly:1188118528:main \
--image product=./product.img \
--partition vendor:readonly:718503936:main \
--image vendor=./vendor.img \
--partition system:readonly:3864137728:main \
--image system=./system.img \
--sparse \
--output ./super_new.img

( continue doing along with Part1 )

...

...

...

7.) Flashing by Odin

Resource:​

✓Patching Dynamic Partitions in Android Super Image​

https://blog.senyuuri.info/posts/2022-04-27-patching-android-super-images/

✓Microsoft/WSL/Loop Device

Loop devices · Issue #4980 · microsoft/WSL

Please implement loop devices. They are SO useful.

github.com

✓How to install Ubuntu sub-system with WSL2 ?​

Loop devices · Issue #4980 · microsoft/WSL

Please implement loop devices. They are SO useful.

github.com