Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
# Linux AMLogic Toolkit

Allows to unpack and repack AMLogic Android images on Linux systems without using the Customization Tool - works for Android 7.
Allows to unpack and repack AMLogic Android images on Linux systems without using the Customization Tool - works for Android 9.

# Features
* Unpack and repack any image
* Mount and edit `system` partition
* Mount and edit `system`, `vendor`, `product` and `odm` partitions
* Unpack and repack `logo` partition (for bootup and upgrading logos)
* Unpack and repack `boot` image and `initrd` ramdisk
* Unpack and repack `recovery` image
* Flash the image directly to a device without repacking it (faster than using the USB Burning Tool)
* Works for Android 7
* Works for Android 9
* No need to unpack the image each time you want to use the tool

# What it cannot do (yet)
* Edit other partitions of the image such as `recovery` (you can still replace the `PARTITION` files by hand)

# Dependencies
* `zlib1g-dev` for `simg2img` and `img2simg`
* `libblkid-dev` for `abootimg` (unpacking and repacking boot image)
Expand All @@ -28,23 +26,31 @@ Allows to unpack and repack AMLogic Android images on Linux systems without usin
* The result is :
* `output/image` : raw image files (`PARTITION` files)
* `output/system` : system partition files
* `output/vendor` : vendor partition files
* `output/product` : product partition files
* `output/odm` : odm partition files
* `output/logo` : logo partition files
* `output/boot` : boot partition files
* `output/recovery` : recovery partition files
* From now on you can edit the files of the `output` directory
* Note that those files will be overwritten when repacking :
* `output/image/system.PARTITION`
* `output/image/boot.PARTITION`
* `output/image/vendor.PARTITION`
* `output/image/product.PARTITION`
* `output/image/odm.PARTITION`
* `output/image/logo.PARTITION`
* `output/image/boot.PARTITION`
* `output/image/recovery.PARTITION`
* `output/boot/initrd.img` if using `./bin/extract_initrd`
* If you happen to loose the `output/system` mounting point (after a reboot for instance), just run `./bin/remount` to mount it again
* On the other hand, you can unmount the system partition using `./bin/unmount`
* If you happen to loose the `output/system` (or vendor, product, odm) mounting point (after a reboot for instance), just run `./bin/remount` to mount those again
* On the other hand, you can unmount the partitions using `./bin/unmount`
* If you want to extract the `initrd` ramdisk, use the `./bin/extract_initrd` and `./bin/recreate_initrd` scripts (output in `output/initrd`)
* **Be careful :**
* Don't break everything by chmod'ing the whole `output/system` folder, because it will be replicated in the image and it won't boot !
* Don't rename the files in `output/boot.img`
* If you extract and recreate the `initrd` ramdisk, its size will change and it will most likely break the boot image. To fix this, edit the `bootimg.cfg` file in `output/boot` to replicate the change in size (you can repack the image, let it fail and read the logs to see the new size).
* When you have finished editing the files, run `./bin/repack output.img` to repack the image to `output.img`
* Additionnaly, you can use `./bin/flash` to flash the image to a device through USB (you will need the udev rule, see https://github.com/Stane1983/aml-linux-usb-burn)
* Additionally, you can use `./bin/flash` to flash the image to a device through USB (you will need the udev rule, see https://github.com/Stane1983/aml-linux-usb-burn)
* The device type (`gxl`) is hardcoded into the flashing script, edit it if you're not using S905, S905X or S919
* Done !

Expand Down
3 changes: 3 additions & 0 deletions bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
./bin/cleanup

mkdir -p output/system
mkdir -p output/vendor
mkdir -p output/product
mkdir -p output/odm
mkdir -p output/image
mkdir -p output/logo

Expand Down
3 changes: 2 additions & 1 deletion bin/extract_initrd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ then

echo "Extracting initrd ramdisk..."
cd output/initrd
cat ../boot/initrd.img | gunzip | cpio -vid
cat ../boot/zImage | gunzip | cpio -vid
#cat ../boot/initrd.img | gunzip | cpio -vid
Comment on lines +9 to +10
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ignore this change. Added by mistake.

cd ../..

echo "Done"
Expand Down
Empty file modified bin/flash
100644 → 100755
Empty file.
38 changes: 37 additions & 1 deletion bin/recreate
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,45 @@ then
rm -f output/image/system.PARTITION
bin/img2simg output/image/system.img output/image/system.PARTITION

echo "Converting back vendor.img to vendor.PARTITION..."
rm -f output/image/vendor.PARTITION
bin/img2simg output/image/vendor.img output/image/vendor.PARTITION

echo "Converting back vendor.img to odm.PARTITION..."
rm -f output/image/odm.PARTITION
bin/img2simg output/image/odm.img output/image/odm.PARTITION

echo "Converting back product.img to product.PARTITION..."
rm -f output/image/product.PARTITION
bin/img2simg output/image/product.img output/image/product.PARTITION

echo "Repacking boot..."
rm -f output/image/boot.PARTITION
bin/abootimg --create output/image/boot.PARTITION -f output/boot/bootimg.cfg -k output/boot/zImage -r output/boot/initrd.img
if [ -e output/boot/initrd.img ]
then
if [ -e output/boot/stage2.img ]
then
bin/abootimg --create output/image/boot.PARTITION -f output/boot/bootimg.cfg -k output/boot/zImage -r output/boot/initrd.img -s output/boot/stage2.img
else
bin/abootimg --create output/image/boot.PARTITION -f output/boot/bootimg.cfg -k output/boot/zImage
fi
else
bin/abootimg --create output/image/boot.PARTITION -f output/boot/bootimg.cfg -k output/boot/zImage
fi

echo "Repacking recovery..."
rm -f output/image/recovery.PARTITION
if [ -e output/recovery/initrd.img ]
then
if [ -e output/recovery/stage2.img ]
then
bin/abootimg --create output/image/recovery.PARTITION -f output/recovery/bootimg.cfg -k output/recovery/zImage -r output/recovery/initrd.img -s output/recovery/stage2.img
else
bin/abootimg --create output/image/recovery.PARTITION -f output/recovery/bootimg.cfg -k output/recovery/zImage
fi
else
bin/abootimg --create output/image/recovery.PARTITION -f output/recovery/bootimg.cfg -k output/recovery/zImage
fi

sync

Expand Down
10 changes: 8 additions & 2 deletions bin/remount
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#!/bin/sh

echo "Unmounting previous image..."
echo "Unmounting previous images..."
sync
sudo umount output/system
sudo umount output/vendor
sudo umount output/odm
sudo umount output/product

echo "Remounting system image..."
echo "Remounting images..."
sudo mount -t ext4 -o loop,rw output/image/system.img output/system
sudo mount -t ext4 -o loop,rw output/image/vendor.img output/vendor
sudo mount -t ext4 -o loop,rw output/image/odm.img output/odm
sudo mount -t ext4 -o loop,rw output/image/product.img output/product

echo "Done"

36 changes: 21 additions & 15 deletions bin/src/abootimg/abootimg.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ void print_usage(void)
"\n"
" bootimg has to be valid Android Boot Image, or the update will abort.\n"
"\n"
" abootimg --create <bootimg> [-c \"param=value\"] [-f <bootimg.cfg>] -k <kernel> -r <ramdisk> [-s <secondstage>]\n"
" abootimg --create <bootimg> [-c \"param=value\"] [-f <bootimg.cfg>] -k <kernel> [-r <ramdisk>] [-s <secondstage>]\n"
"\n"
" create a new image from scratch.\n"
" if the boot image file is a block device, sanity check will be performed to avoid overwriting a existing\n"
Expand Down Expand Up @@ -291,7 +291,11 @@ int check_boot_img_header(t_abootimg* img)

if (!(img->header.ramdisk_size)) {
fprintf(stderr, "%s: ramdisk size is null\n", img->fname);
return 1;
/*
* On newer AOSP devices, system can be used as rootfs,
* resulting in no initrd being used. Thus this case should
* not be fatal.
*/
}

unsigned page_size = img->header.page_size;
Expand Down Expand Up @@ -569,16 +573,18 @@ void update_images(t_abootimg *img)
else if (img->kernel) {
// if kernel is updated, copy the ramdisk from original image
char* r = malloc(rsize);
if (!r)
abort_perror("");
if (fseek(img->stream, roffset, SEEK_SET))
abort_perror(img->fname);
size_t rb = fread(r, rsize, 1, img->stream);
if ((rb!=1) || ferror(img->stream))
abort_perror(img->fname);
else if (feof(img->stream))
abort_printf("%s: cannot read ramdisk\n", img->fname);
img->ramdisk = r;
if (rsize != 0) {
if (!r)
abort_perror("");
if (fseek(img->stream, roffset, SEEK_SET))
abort_perror(img->fname);
size_t rb = fread(r, rsize, 1, img->stream);
if ((rb!=1) || ferror(img->stream))
abort_perror(img->fname);
else if (feof(img->stream))
abort_printf("%s: cannot read ramdisk\n", img->fname);
img->ramdisk = r;
}
}

if (img->second_fname) {
Expand Down Expand Up @@ -718,7 +724,7 @@ void print_bootimg_info(t_abootimg* img)
printf ("* kernel size = %u bytes (%.2f MB)\n", kernel_size, (double)kernel_size/0x100000);
printf (" ramdisk size = %u bytes (%.2f MB)\n", ramdisk_size, (double)ramdisk_size/0x100000);
if (second_size)
printf (" second stage size = %u bytes (%.2f MB)\n", second_size, (double)second_size/0x100000);
printf (" second stage size = %u bytes (%.2f MB)\n", ramdisk_size, (double)ramdisk_size/0x100000);

printf ("\n* load addresses:\n");
printf (" kernel: 0x%08x\n", img->header.kernel_addr);
Expand Down Expand Up @@ -843,7 +849,7 @@ void extract_second(t_abootimg* img)
if (!ssize) // Second Stage not present
return;

unsigned n = (rsize + psize - 1)/psize + (ksize + psize - 1)/psize;
unsigned n = (rsize + ksize + psize - 1) / psize;
unsigned soffset = (1+n)*psize;

printf ("extracting second stage image in %s\n", img->second_fname);
Expand Down Expand Up @@ -932,7 +938,7 @@ int main(int argc, char** argv)
break;

case create:
if (!bootimg->kernel_fname || !bootimg->ramdisk_fname) {
if (!bootimg->kernel_fname) {
print_usage();
break;
}
Expand Down
12 changes: 12 additions & 0 deletions bin/unmount
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,16 @@ echo "Unmounting system image..."
sync
sudo umount output/system

echo "Unmounting vendor image..."
sync
sudo umount output/vendor

echo "Unmounting odm image..."
sync
sudo umount output/odm

echo "Unmounting product image..."
sync
sudo umount output/product

echo "Done"
32 changes: 32 additions & 0 deletions bin/unpack
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ then
then
echo "Cleaning up..."
sudo umount output/system
sudo umount output/vendor
sudo umount output/odm
sudo umount output/product
rm -rf output
mkdir -p output/system
mkdir -p output/vendor
mkdir -p output/odm
mkdir -p output/product
mkdir -p output/image
mkdir -p output/logo
mkdir -p output/boot
mkdir -p output/recovery

echo "Unpacking image $1..."
bin/aml_image_v2_packer -d $1 output/image
Expand All @@ -23,6 +30,24 @@ then
echo "Mounting system image..."
sudo mount -t ext4 -o loop,rw output/image/system.img output/system

echo "Converting vendor.PARTITION to vendor.img..."
bin/simg2img output/image/vendor.PARTITION output/image/vendor.img

echo "Mounting vendor image..."
sudo mount -t ext4 -o loop,rw output/image/vendor.img output/vendor

echo "Converting odm.PARTITION to odm.img..."
bin/simg2img output/image/odm.PARTITION output/image/odm.img

echo "Mounting odm image..."
sudo mount -t ext4 -o loop,rw output/image/odm.img output/odm

echo "Converting product.PARTITION to product.img..."
bin/simg2img output/image/product.PARTITION output/image/product.img

echo "Mounting product image..."
sudo mount -t ext4 -o loop,rw output/image/product.img output/product

echo "Unpacking logo..."
bin/logo_img_packer -d output/image/logo.PARTITION output/logo

Expand All @@ -33,6 +58,13 @@ then
cd ../..
rm -f output/boot/boot.img

echo "Unpacking recovery..."
cp output/image/recovery.PARTITION output/recovery/recovery.img
cd output/recovery
../../bin/abootimg -x recovery.img
cd ../..
rm -f output/recovery/recovery.img

echo "Done"
else
echo "File not found: $1"
Expand Down