Android-partition分析

android的存储分为两种

  一种叫做RAM,如emmc标准的ddr3/4,容量比较小

  一种叫做ROM,rom大小比较大,如8G/16G/32G/64G/128G。

我们给手机刷机,是把system.img/recovery.img/userdata.img等按照分区表烧写在对应地址的ROM上,这部分叫系统部分,如16G的手机,开机后只有11G,其中5G被这些分区占据了,用户能使用的存储大小为11G。

我们在bootloader阶段会从boot/recovery分区中读取kernel/ramdisk到ram上,然后引导kernel,加载虚拟文件系统ramdisk,启动init,然后读取rom中的文件系统的内容。

在boot.img/recovery.img的内容如下,bootloader会从boot header得到kernel/ramdisk/second/device的大小和要加载到ram上的地址,读取aboot/recovery分区的内容到ram上。

recovery.img/boot.img的构成是一样的,只是里面文件的大小和信息不同,文件名是一样的。

分析boot_img_hdr结构提
  kernel_size  kernel表示zImage的实际大小
  kernel_addr  kernel的zImage载入内存的物理地址,也是bootloader要跳转的地址
  ramdisk_size  ramdisk的实际大小
  ramdisk_addr  ramdisk加载到内存的实际物理地址,之后kernel会解压并把它挂载成根文件系统,我们的中枢神经-init.rc就隐藏于内
  tags_addr    tags_addr是传参数用的物理内存地址,它作用是把bootloader中的参数传递给kernel,参数放在这个地址上
  page_size    page_size是存储芯片(ram/emmc)的页大小,取决与存储芯片
  cmdline      command line它可以由bootloader向kernel传参的内容,存放在tag_addr地址
  second     可选
** +-----------------+ 
** | boot header     | 1 page
** +-----------------+
** | kernel          | n pages  
** +-----------------+
** | ramdisk         | m pages  
** +-----------------+
** | second stage    | o pages
** +-----------------+
** | device tree     | p pages
** +-----------------+
bootable/bootloader/lk/app/aboot/bootimg.h

#ifndef _BOOT_IMAGE_H_
#define _BOOT_IMAGE_H_

typedef struct boot_img_hdr boot_img_hdr;

#define BOOT_MAGIC "ANDROID!"
#define BOOT_MAGIC_SIZE 8
#define BOOT_NAME_SIZE  16
#define BOOT_ARGS_SIZE  512
#define BOOT_IMG_MAX_PAGE_SIZE 4096

struct boot_img_hdr
{
    unsigned char magic[BOOT_MAGIC_SIZE];

    unsigned kernel_size;  /* size in bytes */
    unsigned kernel_addr;  /* physical load addr */

    unsigned ramdisk_size; /* size in bytes */
    unsigned ramdisk_addr; /* physical load addr */

    unsigned second_size;  /* size in bytes */
    unsigned second_addr;  /* physical load addr */

    unsigned tags_addr;    /* physical addr for kernel tags */
    unsigned page_size;    /* flash page size we assume */
    unsigned dt_size;      /* device_tree in bytes */
    unsigned unused;    /* future expansion: should be 0 */

    unsigned char name[BOOT_NAME_SIZE]; /* asciiz product name */
    
    unsigned char cmdline[BOOT_ARGS_SIZE];

    unsigned id[8]; /* timestamp / checksum / sha1 / etc */
};

/*
** +-----------------+ 
** | boot header     | 1 page
** +-----------------+
** | kernel          | n pages  
** +-----------------+
** | ramdisk         | m pages  
** +-----------------+
** | second stage    | o pages
** +-----------------+
** | device tree     | p pages
** +-----------------+
**
** n = (kernel_size + page_size - 1) / page_size
** m = (ramdisk_size + page_size - 1) / page_size
** o = (second_size + page_size - 1) / page_size
** p = (dt_size + page_size - 1) / page_size
** 0. all entities are page_size aligned in flash
** 1. kernel and ramdisk are required (size != 0)
** 2. second is optional (second_size == 0 -> no second)
** 3. load each element (kernel, ramdisk, second) at
**    the specified physical address (kernel_addr, etc)
** 4. prepare tags at tag_addr.  kernel_args[] is
**    appended to the kernel commandline in the tags.
** 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
** 6. if second_size != 0: jump to second_addr
**    else: jump to kernel_addr
*/

boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size,
                        void *ramdisk, unsigned ramdisk_size,
                        void *second, unsigned second_size,
                        unsigned page_size,
                        unsigned *bootimg_size);

void bootimg_set_cmdline(boot_img_hdr *hdr, const char *cmdline);                

#define KERNEL64_HDR_MAGIC 0x644D5241 /* ARM64 */

struct kernel64_hdr
{
    uint32_t insn;
    uint32_t res1;
    uint64_t text_offset;
    uint64_t res2;
    uint64_t res3;
    uint64_t res4;
    uint64_t res5;
    uint64_t res6;
    uint32_t magic_64;
    uint32_t res7;
};

#endif
MTK分区表存放位置:device/mediatek/build/build/tools/ptgen/xxx/xxx.xls  
ptgen.pl文件会把xls文件解析成xxxAndroid_scatter.txt放在out/target/product/xxx/中
mtk的flashtool工具会读取这个文件把相关的镜像烧写到rom中。

高通分区表存放位置:不同项目,位置不同,用find搜索partition.xml  $find . -name "*partition*.xml",最后会生成rawprogram0.xml文件,高通的刷机工具会根据这个文件把相关的镜像烧写到rom中。

你可能感兴趣的:(Qualcomm)