Bring up Android A/B system (Part 1)

1. Prepatation

Before you start, be sure to read Google’s official guide first: https://source.android.com/devices/tech/ota/ab_updates.html and https://source.android.com/devices/tech/ota/ab_implement.

This post will introduce a sample Android A/B system update implementation which stores information in /misc partition, similar as Intel reference product https://android.googlesource.com/platform/hardware/bsp/intel/+/android-7.0.0_r35/soc/common/bootctrl

You can also save such misc information in any other tamper-evident storage.

1.1 Requirement for kernel

Make sure your kernel support A/B features as described in the guide.

1.2 Requirement for bootloader

  • bootloader read active_slot from /misc partition(value is 0 or 1), then translates the value to _a or _b,

Newer android has updated the BCB data structure from bootloader_message to bootloader_message_ab.

bootloader_message_ab -> slot_suffix(BrilloBootInfo) -> active_slot
  • bootloader locates partitions for /system_a or /system_b as /dev/XX, assemble kernel command line, then load kernel from partition /boot_a or /boot_b.

Kernel command line should have this such info:

ro root=/dev/XX skip_initramfs
  • kernel boots up w/o ramdisk inside boot partition, instead it will run with rootfs at /dev/XX. fs_mgr then reads /misc for active_slot, and mounts /system_a or /system_b to /.

2. make the change on Android

2.1 update partition table

All partitions which need to support A/B must be named in the new style, like boot_a, boot_b, vendor_a, vendor_b, vendor1_a, vendor1_b etc.

2.2 update makefiles and config files

BoardConfig.mk

-BOARD_KERNEL_CMDLINE := root=/dev/ram0
+BOARD_KERNEL_CMDLINE := ro root=/dev/mmcblk0p18 skip_initramfs

-BOARD_RECOVERYIMAGE_PARTITION_SIZE := 33554432
-BOARD_CACHEIMAGE_PARTITION_SIZE := 536870912
-BOARD_CACHEIMAGE_FILE_SYSTEM_TYPE := ext4
-BOARD_USES_FULL_RECOVERY_IMAGE := true

/cache and /recovery is osbolete, remove then both.

fstab.{ro.hardware}

-/dev/block/by-name/system /system ext4 ro wait
+/dev/block/by-name/system /       ext4 ro,barrier=1,discard wait,slotselect
-/dev/block/by-name/cache  /cache  ext4 noatime,nosuid,nodev,rw  check

device.mk

AB_OTA_UPDATER := true
BOARD_BUILD_SYSTEM_ROOT_IMAGE := true
BOARD_USES_RECOVERY_AS_BOOT := true
TARGET_NO_RECOVERY := true

makefile won’t generate recovery.img, instead boot.img contains recovery mode ramdisk.

AB_OTA_PARTITIONS += \
    boot \
    system

boot.img and system.img have A/B slots.

PRODUCT_PACKAGES += \
    update_engine \
    update_engine_sideload

“update_engine” is a daemon manipulating background updates,

“update_engine_sideload “ is a static binary equivalent to update_engine daemon that installs an update from a local file directly instead of running in the background.

PRODUCT_PACKAGES_DEBUG += \
    update_engine_client

“update_engine_client” is update_engine console client

PRODUCT_PACKAGES += \
    update_verifier

“update_verifier” checks the integrity of the updated system and vendor partitions on the first boot post an A/B OTA update. It marks the current slot as having booted successfully if it passes the verification.

# bootctrl HAL
PRODUCT_PACKAGES += \
    bootctrl.default \
    bootctrl.$(TARGET_BOARD_PLATFORM)
    bootctl

bootctrl.$(TARGET_BOARD_PLATFORM) is bootctrl HAL, “bootctl” is Command-line wrapper for boot_control HAL. bootctrl.default is only a reference which should never appear in production images.

# A/B OTA post actions
PRODUCT_PACKAGES += cfigPostInstall
AB_OTA_POSTINSTALL_CONFIG += \
    RUN_POSTINSTALL_system=true \
    POSTINSTALL_PATH_system=bin/cfigPostInstall \
    FILESYSTEM_TYPE_system=ext4 \
    POSTINSTALL_OPTIONAL_system=true

# App compilation in background
PRODUCT_PACKAGES += otapreopt_script
AB_OTA_POSTINSTALL_CONFIG += \
  RUN_POSTINSTALL_system=true \
  POSTINSTALL_PATH_system=system/bin/otapreopt_script \
  FILESYSTEM_TYPE_system=ext4 \
  POSTINSTALL_OPTIONAL_system=true

3. output

With all the changes, you can get your production images and OTA packages with “make dist”.

I will explain the details of OTA packages later.