Android super.img结构及解包和重新组包

发布于:2024-07-05 ⋅ 阅读:(17) ⋅ 点赞:(0)

Android super.img结构及解包和重新组包

从Android10版本开始,Android系统使用动态分区,system、vendor、 odm等都包含在super.img里面,编译后的最终镜像不再有这些单独的 image,取而代之的是一个总的 super.img.

1. 基础知识

1.1 为什么用 super 分区代替独立的分区?

传统的分区方式将 system、vendor、product、分区各自独立,在实际的项目开发中,通常每个分区都会留有部分空间,三个(可能没有 product) 分区累计就需要预留不少的空间,并且各个分区的大小被固定,不能灵活调整,比如 system 分区容量不够了,但是 vendor 分区又有多余的空间。

统一合并成一个分区后,使用 super 动态调整分区功能可以灵活调整内部分区大小,从而能更好的利用存储空间。

1.2 支持的动态分区包括

  • system

  • Vendor

  • Product

  • System Ext

  • ODM

对于 A/B 设备,super 分区的大小需要包括两个槽位的大小,super 分区会在内部处理 A/B 槽位,因此 A/B 设备不需要单独的 super_a 和 super_b 分区。

1.3 super分区结构

可以通过lpdump命令dump出super.img的结构:

# lpdump super_ext4.img
$ sudo linux-x86/bin/lpdump super_ext4.img
Slot 0:
Metadata version: 10.2
Metadata size: 1232 bytes
Metadata max size: 65536 bytes
Metadata slot count: 3
Header flags: virtual_ab_device
Partition table:
------------------------
  Name: product_a
  Group: qti_dynamic_partitions_a
  Attributes: readonly
  Extents:
    0 .. 487015 linear super 2048
------------------------
  Name: product_b
  Group: qti_dynamic_partitions_b
  Attributes: readonly
  Extents:
------------------------
  Name: system_a
  Group: qti_dynamic_partitions_a
  Attributes: readonly
  Extents:
    0 .. 4051775 linear super 489472
------------------------
  Name: system_b
  Group: qti_dynamic_partitions_b
  Attributes: readonly
  Extents:
------------------------
  Name: system_dlkm_a
  Group: qti_dynamic_partitions_a
  Attributes: readonly
  Extents:
    0 .. 24071 linear super 4542464
------------------------
  Name: system_dlkm_b
  Group: qti_dynamic_partitions_b
  Attributes: readonly
  Extents:
------------------------
  Name: system_ext_a
  Group: qti_dynamic_partitions_a
  Attributes: readonly
  Extents:
    0 .. 550263 linear super 4567040
------------------------
  Name: system_ext_b
  Group: qti_dynamic_partitions_b
  Attributes: readonly
  Extents:
------------------------
  Name: vendor_a
  Group: qti_dynamic_partitions_a
  Attributes: readonly
  Extents:
    0 .. 747055 linear super 5117952
------------------------
  Name: vendor_b
  Group: qti_dynamic_partitions_b
  Attributes: readonly
  Extents:
------------------------
  Name: vendor_dlkm_a
  Group: qti_dynamic_partitions_a
  Attributes: readonly
  Extents:
    0 .. 3036791 linear super 5865472
------------------------
  Name: vendor_dlkm_b
  Group: qti_dynamic_partitions_b
  Attributes: readonly
  Extents:
------------------------
Super partition layout:
------------------------
super: 2048 .. 489064: product_a (487016 sectors)
super: 489472 .. 4541248: system_a (4051776 sectors)
super: 4542464 .. 4566536: system_dlkm_a (24072 sectors)
super: 4567040 .. 5117304: system_ext_a (550264 sectors)
super: 5117952 .. 5865008: vendor_a (747056 sectors)
super: 5865472 .. 8902264: vendor_dlkm_a (3036792 sectors)
------------------------
Block device table:
------------------------
  Partition name: super
  First sector: 2048
  Size: 12884901888 bytes
  Flags: none
------------------------
Group table:
------------------------
  Name: default
  Maximum size: 0 bytes
  Flags: none
------------------------
  Name: qti_dynamic_partitions_a
  Maximum size: 6438256640 bytes
  Flags: none
------------------------
  Name: qti_dynamic_partitions_b
  Maximum size: 6438256640 bytes
  Flags: none
------------------------

lpdump 只能解析非 sparse 格式的 image,而系统编译的 super.img 是 sparse 格式,需要使用 simg2img 将 Android 编译生成的 sparse 格式的 super.img 转换成 raw 格式。

simg2img super.img super_ext4.img

2. super.img解包和组包

可以按如下方式对super.img进行解包和组包。

2.1 super.img解包

super的解包需要工具lpunpack,但是默认没有编译,源码目录位于:system/extras/partition_tools/ ,需要手动编译生成;

在android根目录下执行

make lpunpack

编译后生成物位置:

out/host/linux-86/bin/lpunpack

开始解包:

第一步,格式转换,转化为ext4

simg2img super.img super_ext4.img

第二步,创建目录super_ext4/ 存放解包后的文件

mkdir super_ext4

第三步,解包super_ext4.img

out/host/linux-86/bin/lpunpack super_ext4.img super_ext4/

解包后在super_ext4/存放着是哪个完整的system.img vendor.img product.img 是ext4格式的,也可以通过mount挂载为文件目录

2.2 打包super.img

打包需要的工具lpmake,工具位置:

out/host/linux-86/bin/lpmake

第一步,确认各个img的大小

上面解包出来super_ext4目录下的img文件,需要重新打包成super.img,在super_ext4/目录下执行

$ stat -c '%n %s' *
odm.img 626688
product.img 213348352
system_ext.img 123666432
system.img 980586496
vendor.img 315723776

第二步,开始打包super.img

按如下命令打包

out/host/linux-86/bin/lpmake \
--metadata-size 65536 --super-name super \
--metadata-slots 2 \
--device super:3263168512 \
--group rockchip_dynamic_partitions:3258974208 \
--partition system:readonly:980586496:rockchip_dynamic_partitions --image system=out/target/product/rk3568_r/system.img \
--partition system_ext:readonly:123666432:rockchip_dynamic_partitions --image system_ext=out/target/product/rk3568_r/system_ext.img \
--partition vendor:readonly:315723776:rockchip_dynamic_partitions --image vendor=out/target/product/rk3568_r/vendor.img \
--partition product:readonly:213348352:rockchip_dynamic_partitions --image product=out/target/product/rk3568_r/product.img \
--partition odm:readonly:626688:rockchip_dynamic_partitions --image odm=out/target/product/rk3568_r/odm.img \
--sparse \
--output out/target/product/rk3568_r/super.img

这个命令可以在编译android的时候通过编译log获取,log保存在out/verbose.log.gz中,可以通过如下命令显示所有log,然后在里面找lpmake

gzip -cd out/verbose.log.gz|less


网站公告

今日签到

点亮在社区的每一天
去签到