Android A/B system系列
Achitecture
在Android A/B System概述中有讲到A升B的一个例子。下面这张图是想说明两个问题:
- 启动的时候是如何知道要从A启动还是B启动?
- 升级的时候是如何要更新A还是B?
图中有两个流程,和涉及的一些模块:
模块:
- bootloader:启动的早期阶段,叫preloader/lk都行
- misc:单独的一块分区,存放启动的信息,也是今天研究的重点
- update_verifier:当前slot启动成功,会将当前的slot设置为successful,表示这个slot是可以成功启动的
- update_engine:Android负责升级流程的守护进程
- bootctrl HAL:google规定的HAL,各个IC厂商有自己的底层实现,是获取misc信息的软件接口
流程:
- boot flow
- get active slot:读misc的信息,从而知道当前要从slot A启动还是slot B启动
- mark slot successful:启动成功的时候,标记当前slot为successful
- update flow
- trigger update and read update package
- get current slot:获取当前slot,假设当前slot为A,那么要更新slot B;当前为B,那么要更新slot A
- write data to another slot:更新
- set active slot:更新完成以后,切换slot,则下次启动从更新的slot启动
misc
关于misc分区的内容,各个IC厂商各自有不同的结构,但核心思想是类似的,所以这里拿google的结构来分析。
重要的部分为红色标记的:
- active_slot:当前正在运行的slot
- slot_info:每个slot的信息(实际的产品实现不会这么简单)
/system/extras/boot_control_copy/bootinfo.h
typedef struct BrilloSlotInfo {
uint8_t bootable : 1;
uint8_t reserved[3];
} BrilloSlotInfo;
typedef struct BrilloBootInfo {
// Used by fs_mgr. Must be NUL terminated.
char bootctrl_suffix[4];
// Magic for identification - must be 'B', 'C', 'c' (short for
// "boot_control copy" implementation).
uint8_t magic[3];
// Version of BrilloBootInfo struct, must be 0 or larger.
uint8_t version;
// Currently active slot.
uint8_t active_slot;
// Information about each slot.
BrilloSlotInfo slot_info[2];
uint8_t reserved[15];
} BrilloBootInfo;
根据这个结构,bootinfo.cpp实现了对BrilloBootInfo进行存取操作的接口,这里的操作比较简单,这里就不去研究了。
/system/extras/boot_control_copy/bootinfo.cpp
- 存取操作
bool boot_info_load(BrilloBootInfo *out_info)
b