Converting images to ext4 format and back creates bad flash file



  • I am trying to create a Rockchip custom ROM by getting an existing custom ROM, unpackaging it, making small modifications, then packaging it back up again.

    There is a problem with me converting the system.img file from sparse android format to extformat and back again. There are no errors, but when I flash the resulting pacakge, the device fails to start.

    Here's an experiment I've done:

    simg2img system.img system-raw.img
    img2simg system-raw.img converted-back.img
    

    If I type file system.img:

    system.img: Android sparse image, version: 1.0, Total of 1048576 4096-byte output blocks in 697 input chunks.

    If I type file converted-back.img:

    converted-back.img: Android sparse image, version: 1.0, Total of 1048576 4096-byte output blocks in 9674 input chunks.

    Should I be adding some parameter to img2simg, or should I be using a different tool to create the sparse image file?

    To confirm it is actually the sparse image conversion that's the problem and not something else, I have successfully done the following:

    • Used rkImageMaker to unpack the original firmware image (which produces a bootloader and a new package file with the header 'RKAF')
    • Used afptool to unpack this package file to a number of different files, including system.img
    • MODIFIED NOTHING
    • Used afptool to repackage these image files into an RKAF file
    • Used rkImageMaker to package the bootloader and the RKAF file into a RKFW file.

    This newly create RKFW image file can be flashed to the device, no problem. It's only when I add simg2img and img2simg into the mix that things go wrong.

    More details:

    • I am getting aftptool and rkImageMaker from here: https://github.com/rockchip-android/RKTools
    • I got simg2img and img2simg by typing sudo apt-get install android-sdk-libsparse-utils


  • Do not use the tool img2simg to convert the raw image back to a sparse image. (Even though it seems logical to do so).

    The tool that you use to create the sparse system img file is make_ext4fs.

    I got this tool by building the entire android source tree for the version and target architecture. (Which took days to figure out)

    Here's how I finally invoked the make_ext4fs application. (The build was in the subfolder ~/aosp_build).

    sudo mkdir /mnt/system
    sudo mount -O pipe system-raw.img /mnt/system
    sudo ~/aosp_build/out/host-linux-x86/bin/make_ext4fs -s -l 4096M -a system -S ~/aosp_build/out/target/product/generic_arm64/root/file_contexts.bin ./converted-back.img /mnt/system 
    

    Here is my sketchy understanding of the parameters:

    +-----------------------------+------------------------------------------+
    |  Parameter                  |  Meaning                                 |
    +=============================+==========================================+
    | -s                          | Output to be in sparse image format      |
    +-----------------------------+------------------------------------------+
    | -l 4096M                    | Target size of the output image          |
    |                             | (I wanted it to be the same size as      |
    |                             | the source image, to avoid packaging     |
    |                             | complications)                           |
    +-----------------------------+------------------------------------------+
    | -a system                   | Something about setting an android mount |
    |                             | point.                                   |
    +-----------------------------+------------------------------------------+
    | -S /file_contexts.bin | Something to do with permissions         |
    +-----------------------------+------------------------------------------+
    | ./converted-back.img        | The output image name                    |
    +-----------------------------+------------------------------------------+
    | /mnt/system                 | The source folder (generated by mounting |
    |                             | the raw image)                           |
    +-----------------------------+------------------------------------------+ 
    

    Other things I discovered:

    • There is a Ubuntu package https://packages.ubuntu.com/bionic/android-sdk-ext4-utils which includes make_ext4fs. But this doesn't work! It was built without Android support. (See https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=741510 )
    • There are a number of forks of the make_ext4fs packages on github. (eg https://github.com/iglunix/make_ext4fs.git ). These are pinned to a specific android version. I don't know if the tool differs much from version to version. I still needed the file_contexts.bin file, so these were not useful to me.
    • I opened up the device and saw the motherboard had a serial DEBUG output. After buying the necessary USB adapter I could read the bootloader debug output through PuTTY. This was extremely useful.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2