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)
bool boot_info_save(BrilloBootInfo *info)
- 校驗和復位操作
bool boot_info_validate(BrilloBootInfo* info)
void boot_info_reset(BrilloBootInfo* info)
- 指定分區的打開操作
int boot_info_open_partition(const char *name, uint64_t *out_size, int flags)
bootctrl
再來看對應bootctrl HAL接口的function是如何實現的
module_getCurrentSlot
unsigned module_getCurrentSlot(boot_control_module_t *module)
{
struct stat statbuf;
dev_t system_a_dev, system_b_dev;
if (stat("/system", &statbuf) != 0) {
fprintf(stderr, "WARNING: Error getting information about /system: %s\n",
strerror(errno));
return 0;
}
if (!get_dev_t_for_partition("system_a", &system_a_dev) ||
!get_dev_t_for_partition("system_b", &system_b_dev))
return 0;
if (statbuf.st_dev == system_a_dev) {
return 0;
} else if (statbuf.st_dev == system_b_dev) {
return 1;
} else {
fprintf(stderr, "WARNING: Error determining current slot "
"(/system dev_t of %d:%d does not match a=%d:%d or b=%d:%d)\n",
major(statbuf.st_dev), minor(statbuf.st_dev),
major(system_a_dev), minor(system_a_dev),
major(system_b_dev), minor(system_b_dev));
return 0;
}
}
module_setActiveBootSlot
- 取出BrilloBootInfo的信息,並校驗
- 設置active_slot爲傳入的slot
- 設置該slot爲bootable
- 將對應分區的內容copy到boot中(這裏是指partition有三份,boot/boot_a/boot_b,系統每次只從boot啓動,這樣是不符合A/B system的精神,如果boot啓動失敗,是不會去切換,那麼A/B system的優勢是沒辦法體現的)
int module_setActiveBootSlot(boot_control_module_t *module, unsigned slot)
{
BrilloBootInfo info;
int src_fd, dst_fd;
uint64_t src_size, dst_size;
char src_name[32];
if (slot >= 2)
return -EINVAL;
if (!boot_info_load(&info)) {
fprintf(stderr, "WARNING: Error loading boot-info. Resetting.\n");
boot_info_reset(&info);
} else {
if (!boot_info_validate(&info)) {
fprintf(stderr, "WARNING: boot-info is invalid. Resetting.\n");
boot_info_reset(&info);
}
}
info.active_slot = slot;
info.slot_info[slot].bootable = true;
snprintf(info.bootctrl_suffix,
sizeof(info.bootctrl_suffix),
"_%c", slot + 'a');
if (!boot_info_save(&info)) {
fprintf(stderr, "Error saving boot-info.\n");
return -errno;
}
// Finally copy the contents of boot_X into boot.
snprintf(src_name, sizeof(src_name), "boot_%c", slot + 'a');
src_fd = boot_info_open_partition(src_name, &src_size, O_RDONLY);
if (src_fd == -1) {
fprintf(stderr, "Error opening \"%s\" partition.\n", src_name);
return -errno;
}
dst_fd = boot_info_open_partition("boot", &dst_size, O_RDWR);
if (dst_fd == -1) {
fprintf(stderr, "Error opening \"boot\" partition.\n");
close(src_fd);
return -errno;
}
if (src_size != dst_size) {
fprintf(stderr,
"src (%" PRIu64 " bytes) and dst (%" PRIu64 " bytes) "
"have different sizes.\n",
src_size, dst_size);
close(src_fd);
close(dst_fd);
return -EINVAL;
}
if (!copy_data(src_fd, dst_fd, src_size)) {
close(src_fd);
close(dst_fd);
return -errno;
}
if (fsync(dst_fd) != 0) {
fprintf(stderr, "Error calling fsync on destination: %s\n",
strerror(errno));
return -errno;
}
close(src_fd);
close(dst_fd);
return 0;
}