Skip to contentSkip to conversation

android: implement A/B boot process

PATCH Superseded Igor Opaniuk 4 participants 17 messages Jul 5, 2019 – Jul 24, 2019

lore
Share & tools
lore
#0 lore raw

v5 cover android: implement A/B boot process

*** This patch series depends on [4], [5], [6] and should be applied after these paticular patches.

This patch series adds support for Android A/B boot process [1]. Main steps of A/B boot process are:
- A/B metadata integrity check
- looking for the current slot (where the system should be booting from)
- getting the name of the current boot partition (boot_a or boot_b) and loading the corresponding Android boot image
- getting the name of the current system partition (system_a or system_b) and passing of its full name via kernel command line (like 'root=/dev/mmcblk1p11')
- passing current slot via kernel command line (like 'androidboot.slot_suffix=_a') and via A/B metadata (e.g. via misc partition)
- A/B metadata processing: setting the boot success flag for current slot, handling the retry counter, etc

A/B metadata is organized according to Android reference [2] and stored on 'misc' partition. On the first A/B boot process, when 'misc' partition doesn't contain required data, default A/B metadata will be created and stored in 'misc' partition. In the end of the Android boot, 'update_verifier' and 'update_engine' services are processing the A/B metadata through the Boot Control HAL. To confirm the boot was successful using current slot, "boot success" flag must be set on Android side.

To enable Android A/B support in U-Boot:
1. Set the following config options:

CONFIG_ANDROID_AB=y
CONFIG_CMD_AB_SELECT=y

2. Change the disk layout so that it has sloted boot partitions. E.g. instead of 'boot' and 'system' partitions there should be 'boot_a', 'boot_b', 'system_a' and 'system_b' partitions.

To be able to actually test this patch series, the A/B features must be implemented and enabled in Android as well (see [1] for details).

Documentation and corresponding test for A/B boot is present here. The last patch in this series integrates A/B boot support on AM57xx based boards (though it's not enabled by default). Future users of A/B boot feature can use it as a reference.

This series is a part of previous submission [3] by Alex Deymo. It contains only A/B feature that was stripped out from there with some modifications for using with "bootm" command preferred in upstream.

Changes in v5:
* Move tests to android subdir
* In new version of sgdisk (0.8.10.2) short options don't work. That can be a regression in sgdisk, but anyway let's use long options, because otherwise the test fails.
* Fix boot part size (preserve the new value).
* Move doc to proper place
* Handle ret values properly for blk_dread()/blk_dwrite properly using IS_ERR_VALUE() macro

Changes in v4:
* Removed android_bl_msg.h and re-used the one, which was introduced by Eugeniu Rosca in [4] and [5].
* Initially we had intention to use "bcb" command and scripting for providing proper boot sequence logic (slot selection, decrementing the counter for each slot), but found out that it's better to keep using a dedicated command ab_select to avoid huge amount of scripting in the headers in includes/config for each platform.
* Removed ANDROID_ prefix in multiple macros.
* Addressed comments from Simon

Changes in v3:
* Minor fixes in the ab metadata handling (added additional sanity checks).
* As Ruslan Trofymenko left Linaro and won't address comments anymore, continue (added my S-b tag) upstreaming patches on my own.

Changes in v2:
* 'android_ab_select' command is renamed to 'ab_select' command and moved to separate 'Android support commands' menu
* For am57xx boards slotted sections (e.g. system_a and system_b) are added to the default sections if CONFIG_CMD_AB_SELECT flag is defined
* Returned function error codes are clarified (errno using)
* Some types constants and files are renamed
* Assertion condition is clarified in test case
* 'debug' calls are changed to 'log_debug'
* The Guide is clarified by the results of changes

[1] https://source.android.com/devices/tech/ota/ab/ab_implement
[2] bootable/recovery/bootloader_message/include/bootloader_message/bootloader_message.h
[3] https://lists.denx.de/pipermail/u-boot/2017-April/285841.html
[4] https://patchwork.ozlabs.org/patch/1080394/
[5] https://patchwork.ozlabs.org/patch/1104243/
[6] https://patchwork.ozlabs.org/patch/1116012/

Ruslan Trofymenko (6):
disk: part: Extend API to get partition info
common: Implement A/B metadata
cmd: Add 'ab_select' command
test/py: Add base test case for A/B updates
doc: android: Add simple guide for A/B updates
env: am57xx: Implement A/B boot process

 cmd/Kconfig                           |  15 ++
 cmd/Makefile                          |   1 +
 cmd/ab_select.c                       |  52 +++++
 common/Kconfig                        |  10 +
 common/Makefile                       |   1 +
 common/android_ab.c                   | 300 ++++++++++++++++++++++++++
 configs/sandbox_defconfig             |   2 +
 disk/part.c                           |  68 ++++++
 doc/android/ab.txt                    |  67 ++++++
 include/android_ab.h                  |  34 +++
 include/environment/ti/boot.h         |  58 ++++-
 include/part.h                        |  21 ++
 test/py/tests/test_android/test_ab.py |  75 +++++++
 13 files changed, 698 insertions(+), 6 deletions(-)
 create mode 100644 cmd/ab_select.c
 create mode 100644 common/android_ab.c
 create mode 100644 doc/android/ab.txt
 create mode 100644 include/android_ab.h
 create mode 100644 test/py/tests/test_android/test_ab.py
Signature · 1 line
2.17.1
#1 lore raw

PATCH v5 1/6 disk: part: Extend API to get partition info

From: Ruslan Trofymenko <[email protected]>

This patch adds part_get_info_by_dev_and_name_or_num() function which allows us to get partition info from its number or name. Partition of interest is specified by string like "device_num:partition_number" or "device_num#partition_name".

The patch was extracted from [1].

[1] https://android-review.googlesource.com/c/platform/external/u-boot/+/729880/2

---

 disk/part.c    | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++
 include/part.h | 21 ++++++++++++++++
 2 files changed, 89 insertions(+)
disk/part.c +68 -0
index f14bc22b6d..7e84214731 100644
@@ -674,6 +674,74 @@ int part_get_info_by_name(struct blk_desc *dev_desc, const char *name,
 	return part_get_info_by_name_type(dev_desc, name, info, PART_TYPE_ALL);
 }
 
+/**
+ * Get partition info from device number and partition name.
+ *
+ * Parse a device number and partition name string in the form of
+ * "device_num#partition_name", for example "0#misc". If the partition
+ * is found, sets dev_desc and part_info accordingly with the information
+ * of the partition with the given partition_name.
+ *
+ * @param[in] dev_iface Device interface
+ * @param[in] dev_part_str Input string argument, like "0#misc"
+ * @param[out] dev_desc Place to store the device description pointer
+ * @param[out] part_info Place to store the partition information
+ * @return 0 on success, or a negative on error
+ */
+static int part_get_info_by_dev_and_name(const char *dev_iface,
+					 const char *dev_part_str,
+					 struct blk_desc **dev_desc,
+					 disk_partition_t *part_info)
+{
+	char *ep;
+	const char *part_str;
+	int dev_num;
+
+	part_str = strchr(dev_part_str, '#');
+	if (!part_str || part_str == dev_part_str)
+		return -EINVAL;
+
+	dev_num = simple_strtoul(dev_part_str, &ep, 16);
+	if (ep != part_str) {
+		/* Not all the first part before the # was parsed. */
+		return -EINVAL;
+	}
+	part_str++;
+
+	*dev_desc = blk_get_dev(dev_iface, dev_num);
+	if (!*dev_desc) {
+		printf("Could not find %s %d\n", dev_iface, dev_num);
+		return -EINVAL;
+	}
+	if (part_get_info_by_name(*dev_desc, part_str, part_info) < 0) {
+		printf("Could not find \"%s\" partition\n", part_str);
+		return -EINVAL;
+	}
+	return 0;
+}
+
+int part_get_info_by_dev_and_name_or_num(const char *dev_iface,
+					 const char *dev_part_str,
+					 struct blk_desc **dev_desc,
+					 disk_partition_t *part_info)
+{
+	/* Split the part_name if passed as "$dev_num#part_name". */
+	if (!part_get_info_by_dev_and_name(dev_iface, dev_part_str,
+					   dev_desc, part_info))
+		return 0;
+	/*
+	 * Couldn't lookup by name, try looking up the partition description
+	 * directly.
+	 */
+	if (blk_get_device_part_str(dev_iface, dev_part_str,
+				    dev_desc, part_info, 1) < 0) {
+		printf("Couldn't find partition %s %s\n",
+		       dev_iface, dev_part_str);
+		return -EINVAL;
+	}
+	return 0;
+}
+
 void part_set_generic_name(const struct blk_desc *dev_desc,
 	int part_num, char *name)
 {
include/part.h +21 -0
index ebca546db5..0b5cf3d5e8 100644
@@ -201,6 +201,27 @@ int part_get_info_by_name_type(struct blk_desc *dev_desc, const char *name,
 int part_get_info_by_name(struct blk_desc *dev_desc,
 			      const char *name, disk_partition_t *info);
 
+/**
+ * Get partition info from dev number + part name, or dev number + part number.
+ *
+ * Parse a device number and partition description (either name or number)
+ * in the form of device number plus partition name separated by a "#"
+ * (like "device_num#partition_name") or a device number plus a partition number
+ * separated by a ":". For example both "0#misc" and "0:1" can be valid
+ * partition descriptions for a given interface. If the partition is found, sets
+ * dev_desc and part_info accordingly with the information of the partition.
+ *
+ * @param[in] dev_iface	Device interface
+ * @param[in] dev_part_str Input partition description, like "0#misc" or "0:1"
+ * @param[out] dev_desc	Place to store the device description pointer
+ * @param[out] part_info Place to store the partition information
+ * @return 0 on success, or a negative on error
+ */
+int part_get_info_by_dev_and_name_or_num(const char *dev_iface,
+					 const char *dev_part_str,
+					 struct blk_desc **dev_desc,
+					 disk_partition_t *part_info);
+
 /**
  * part_set_generic_name() - create generic partition like hda1 or sdb2
  *
Signature · 1 line
2.17.1
#2 lore raw

PATCH v5 1/6 disk: part: Extend API to get partition info

Quoted 9 lines · Igor Opaniuk

From: Ruslan Trofymenko <[email protected]>

This patch adds part_get_info_by_dev_and_name_or_num() function which allows us to get partition info from its number or name. Partition of interest is specified by string like "device_num:partition_number" or "device_num#partition_name".

The patch was extracted from [1].

Quoted 7 lines

[1] https://android-review.googlesource.com/c/platform/external/u-boot/+/729880/2

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190724/613d2bdc/attachment.sig>
#3 lore raw

PATCH v5 2/6 common: Implement A/B metadata

In reply to Igor Opaniuk

From: Ruslan Trofymenko <[email protected]>

This patch determines the A/B-specific bootloader message structure that is the basis for implementation of recovery and A/B update functions. A/B metadata is stored in this structure and used to decide which slot should we use to boot the device. Also some basic functions for A/B metadata manipulation are implemented (like slot selection).

The patch was extracted from commits [1], [2] with some coding style fixes.

[1] https://android-review.googlesource.com/c/platform/external/u-boot/+/729878/2
[2] https://android-review.googlesource.com/c/platform/external/u-boot/+/729880/2

---

 common/Kconfig       |  10 ++
 common/Makefile      |   1 +
 common/android_ab.c  | 300 +++++++++++++++++++++++++++++++++++++++++++
 include/android_ab.h |  34 +++++
 4 files changed, 345 insertions(+)
 create mode 100644 common/android_ab.c
 create mode 100644 include/android_ab.h
common/Kconfig +10 -0
index af66496e75..63287865ef 100644
@@ -814,6 +814,16 @@ config UPDATE_TFTP_MSEC_MAX
 	default 100
 	depends on UPDATE_TFTP
 
+config ANDROID_AB
+	bool "Android A/B updates"
+	default n
+	help
+	  If enabled, adds support for the new Android A/B update model. This
+	  allows the bootloader to select which slot to boot from based on the
+	  information provided by userspace via the Android boot_ctrl HAL. This
+	  allows a bootloader to try a new version of the system but roll back
+	  to previous version if the new one didn't boot all the way.
+
 endmenu
 
 menu "Blob list"
common/Makefile +1 -0
index c7e41ef307..302d8beaf3 100644
@@ -107,6 +107,7 @@ endif
 endif
 
 obj-y += image.o
+obj-$(CONFIG_ANDROID_AB) += android_ab.o
 obj-$(CONFIG_ANDROID_BOOT_IMAGE) += image-android.o
 obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) += image-fdt.o
 obj-$(CONFIG_$(SPL_TPL_)FIT) += image-fit.o
common/android_ab.c new file +300 -0

300 lines not on this page · raw message

include/android_ab.h new file +34 -0
new file mode 100644
index 0000000000..810906d22b
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ */
+
+#ifndef __ANDROID_AB_H
+#define __ANDROID_AB_H
+
+#include <common.h>
+
+/* Android standard boot slot names are 'a', 'b', 'c', ... */
+#define BOOT_SLOT_NAME(slot_num) ('a' + (slot_num))
+
+/* Number of slots */
+#define NUM_SLOTS 2
+
+/**
+ * Select the slot where to boot from.
+ *
+ * On Android devices with more than one boot slot (multiple copies of the
+ * kernel and system images) selects which slot should be used to boot from and
+ * registers the boot attempt. This is used in by the new A/B update model where
+ * one slot is updated in the background while running from the other slot. If
+ * the selected slot did not successfully boot in the past, a boot attempt is
+ * registered before returning from this function so it isn't selected
+ * indefinitely.
+ *
+ * @param[in] dev_desc Place to store the device description pointer
+ * @param[in] part_info Place to store the partition information
+ * @return The slot number (>= 0) on success, or a negative on error
+ */
+int ab_select_slot(struct blk_desc *dev_desc, disk_partition_t *part_info);
+
+#endif /* __ANDROID_AB_H */
Signature · 1 line
2.17.1
#4 lore raw

PATCH v5 2/6 common: Implement A/B metadata

Quoted 11 lines · Igor Opaniuk

From: Ruslan Trofymenko <[email protected]>

This patch determines the A/B-specific bootloader message structure that is the basis for implementation of recovery and A/B update functions. A/B metadata is stored in this structure and used to decide which slot should we use to boot the device. Also some basic functions for A/B metadata manipulation are implemented (like slot selection).

The patch was extracted from commits [1], [2] with some coding style fixes.

Quoted 7 lines

[1] https://android-review.googlesource.com/c/platform/external/u-boot/+/729878/2
[2] https://android-review.googlesource.com/c/platform/external/u-boot/+/729880/2

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190724/47d787cb/attachment.sig>
#5 lore raw

PATCH v5 3/6 cmd: Add 'ab_select' command

In reply to Igor Opaniuk

From: Ruslan Trofymenko <[email protected]>

For A/B system update support the Android boot process requires to send 'androidboot.slot_suffix' parameter as a command line argument. This patch implementes 'ab_select' command which allows us to obtain current slot by processing the A/B metadata.

The patch was extracted from commit [1] with one modification: the separator for specifying the name of metadata partition was changed from ';' to '#', because ';' is used for commands separation.

[1] https://android-review.googlesource.com/c/platform/external/u-boot/+/729880/2

---

 cmd/Kconfig     | 15 ++++++++++++++
 cmd/Makefile    |  1 +
 cmd/ab_select.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+)
 create mode 100644 cmd/ab_select.c
cmd/Kconfig +15 -0
index 3afb760a81..380edd0136 100644
@@ -1179,6 +1179,21 @@ config CMD_SETEXPR
 
 endmenu
 
+menu "Android support commands"
+
+config CMD_AB_SELECT
+	bool "ab_select"
+	default n
+	depends on ANDROID_AB
+	help
+	  On Android devices with more than one boot slot (multiple copies of
+	  the kernel and system images) this provides a command to select which
+	  slot should be used to boot from and register the boot attempt. This
+	  is used by the new A/B update model where one slot is updated in the
+	  background while running from the other slot.
+
+endmenu
+
 if NET
 
 menuconfig CMD_NET
cmd/Makefile +1 -0
index 49e64cde1d..500147a2ac 100644
@@ -12,6 +12,7 @@ obj-y += version.o
 
 # command
 obj-$(CONFIG_CMD_AES) += aes.o
+obj-$(CONFIG_CMD_AB_SELECT) += ab_select.o
 obj-$(CONFIG_CMD_ADC) += adc.o
 obj-$(CONFIG_CMD_ARMFLASH) += armflash.o
 obj-y += blk_common.o
cmd/ab_select.c new file +52 -0

52 lines not on this page · raw message

Signature · 1 line
2.17.1
#6 lore raw

PATCH v5 3/6 cmd: Add 'ab_select' command

Quoted 11 lines · Igor Opaniuk

From: Ruslan Trofymenko <[email protected]>

For A/B system update support the Android boot process requires to send 'androidboot.slot_suffix' parameter as a command line argument. This patch implementes 'ab_select' command which allows us to obtain current slot by processing the A/B metadata.

The patch was extracted from commit [1] with one modification: the separator for specifying the name of metadata partition was changed from ';' to '#', because ';' is used for commands separation.

Quoted 7 lines

[1] https://android-review.googlesource.com/c/platform/external/u-boot/+/729880/2

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190724/b3cc6846/attachment.sig>
#7 lore raw

PATCH v5 4/6 test/py: Add base test case for A/B updates

In reply to Igor Opaniuk

From: Ruslan Trofymenko <[email protected]>

Add sandbox test for 'ab_select' command.

Test: ./test/py/test.py --bd sandbox --build -k test_ab

---

 configs/sandbox_defconfig             |  2 +
 test/py/tests/test_android/test_ab.py | 75 +++++++++++++++++++++++++++
 2 files changed, 77 insertions(+)
 create mode 100644 test/py/tests/test_android/test_ab.py
configs/sandbox_defconfig +2 -0
index 6894262b89..fd51a75b01 100644
@@ -20,6 +20,7 @@ CONFIG_PRE_CON_BUF_ADDR=0xf0000
 CONFIG_LOG_MAX_LEVEL=6
 CONFIG_LOG_ERROR_RETURN=y
 CONFIG_DISPLAY_BOARDINFO_LATE=y
+CONFIG_ANDROID_AB=y
 CONFIG_CMD_CPU=y
 CONFIG_CMD_LICENSE=y
 CONFIG_CMD_BOOTZ=y
@@ -47,6 +48,7 @@ CONFIG_CMD_REMOTEPROC=y
 CONFIG_CMD_SPI=y
 CONFIG_CMD_USB=y
 CONFIG_CMD_AXI=y
+CONFIG_CMD_AB_SELECT=y
 CONFIG_CMD_TFTPPUT=y
 CONFIG_CMD_TFTPSRV=y
 CONFIG_CMD_RARP=y
test/py/tests/test_android/test_ab.py new file +75 -0

75 lines not on this page · raw message

Signature · 1 line
2.17.1
#8 lore raw

PATCH v5 4/6 test/py: Add base test case for A/B updates

Quoted 4 lines · Igor Opaniuk

From: Ruslan Trofymenko <[email protected]>

Add sandbox test for 'ab_select' command.

Quoted 8 lines

Test: ./test/py/test.py --bd sandbox --build -k test_ab

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190724/de51923c/attachment.sig>
#9 lore raw

PATCH v5 5/6 doc: android: Add simple guide for A/B updates

In reply to Igor Opaniuk

From: Ruslan Trofymenko <[email protected]>

Add a short documentation for A/B enablement and 'ab_select' command usage.

---

 doc/android/ab.txt | 67 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
 create mode 100644 doc/android/ab.txt
doc/android/ab.txt new file +67 -0
new file mode 100644
index 0000000000..9f37ed5c58
@@ -0,0 +1,67 @@
+Android A/B updates
+===================
+
+Overview
+--------
+
+A/B system updates ensures modern approach for system update. This feature
+allows one to use two sets (or more) of partitions referred to as slots
+(normally slot A and slot B). The system runs from the current slot while the
+partitions in the unused slot can be updated [1].
+
+A/B enablement
+--------------
+
+The A/B updates support can be activated by specifying next options in
+your board configuration file:
+
+    CONFIG_ANDROID_AB=y
+    CONFIG_CMD_AB_SELECT=y
+
+The disk space on target device must be partitioned in a way so that each
+partition which needs to be updated has two or more instances. The name of
+each instance must be formed by adding suffixes: _a, _b, _c, etc.
+For example: boot_a, boot_b, system_a, system_b, vendor_a, vendor_b.
+
+As a result you can use 'ab_select' command to ensure A/B boot process in your
+boot script. This command analyzes and processes A/B metadata stored on a
+special partition (e.g. "misc") and determines which slot should be used for
+booting up.
+
+Command usage
+-------------
+
+    ab_select <slot_var_name> <interface> <dev[:part_number|#part_name]>
+
+for example:
+
+    => ab_select slot_name mmc 1:4
+
+or
+
+    => ab_select slot_name mmc 1#misc
+
+Result:
+
+    => printenv slot_name
+    slot_name=a
+
+Based on this slot information, the current boot partition should be defined,
+and next kernel command line parameters should be generated:
+
+ - androidboot.slot_suffix=
+ - root=
+
+For example:
+
+    androidboot.slot_suffix=_a root=/dev/mmcblk1p12
+
+A/B metadata is organized according to AOSP reference [2]. On the first system
+start with A/B enabled, when 'misc' partition doesn't contain required data,
+the default A/B metadata will be created and written to 'misc' partition.
+
+References
+----------
+
+[1] https://source.android.com/devices/tech/ota/ab
+[2] bootable/recovery/bootloader_message/include/bootloader_message/bootloader_message.h
Signature · 1 line
2.17.1
#10 lore raw

PATCH v5 5/6 doc: android: Add simple guide for A/B updates

Quoted 11 lines · Igor Opaniuk

From: Ruslan Trofymenko <[email protected]>

Add a short documentation for A/B enablement and 'ab_select' command usage.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190724/222d7ecc/attachment.sig>
#11 lore raw

PATCH v5 6/6 env: am57xx: Implement A/B boot process

In reply to Igor Opaniuk

From: Ruslan Trofymenko <[email protected]>

Add support for A/B boot process on AM57xx based boards:

1. Define 'slot_suffix' variable (using 'ab_select' command)
2. Extend 'emmc_android_boot' boot command (add commands for A/B boot process)

'ab_select' command is used to decide which slot should be used for booting up. A/B metadata resides in 'misc' partition.

To activate the A/B boot process, the following config options must be set:

CONFIG_ANDROID_AB=y
CONFIG_CMD_AB_SELECT=y

For successful A/B boot, the corresponding A/B infrastructure must be involved on Android side [1] (including mounting system as root), and disk must be partitioned accordingly.

When A/B boot is enabled, there are some known limitations currently exist (not related to A/B patches, need to be implemented later):

1. The 'Verified Boot' sequence is not supported
2. dev path to system partition (system_a or system_b) is passed via 'bootargs' as 'root=' argument like 'root=/dev/mmcblk1p12', but further we'll need to rework it with respect to dm-verity requirements [2]

In case when A/B partitions are not present in system (and A/B boot is enabled), boot up process will be terminated and next message will be shown:

"boot_a(b) partition not found"

[1] https://source.android.com/devices/tech/ota/ab
[2] https://source.android.com/devices/tech/ota/ab/ab_implement#kernel

---

 include/environment/ti/boot.h | 58 +++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 6 deletions(-)
include/environment/ti/boot.h +52 -6
index 943adcad40..db6e7513c0 100644
@@ -23,6 +23,18 @@
 #define VBMETA_PART			""
 #endif
 
+#if defined(CONFIG_CMD_AB_SELECT)
+#define COMMON_PARTS \
+	"name=boot_a,size=20M,uuid=${uuid_gpt_boot_a};" \
+	"name=boot_b,size=20M,uuid=${uuid_gpt_boot_b};" \
+	"name=system_a,size=1024M,uuid=${uuid_gpt_system_a};" \
+	"name=system_b,size=1024M,uuid=${uuid_gpt_system_b};"
+#else
+#define COMMON_PARTS \
+	"name=boot,size=20M,uuid=${uuid_gpt_boot};" \
+	"name=system,size=1024M,uuid=${uuid_gpt_system};"
+#endif
+
 #ifndef PARTS_DEFAULT
 /* Define the default GPT table for eMMC */
 #define PARTS_DEFAULT \
@@ -38,8 +50,7 @@
 	"name=uboot-env,start=2432K,size=256K,uuid=${uuid_gpt_reserved};" \
 	"name=misc,size=128K,uuid=${uuid_gpt_misc};" \
 	"name=recovery,size=40M,uuid=${uuid_gpt_recovery};" \
-	"name=boot,size=20M,uuid=${uuid_gpt_boot};" \
-	"name=system,size=1024M,uuid=${uuid_gpt_system};" \
+	COMMON_PARTS \
 	"name=vendor,size=256M,uuid=${uuid_gpt_vendor};" \
 	VBMETA_PART \
 	"name=userdata,size=-,uuid=${uuid_gpt_userdata}"
@@ -58,6 +69,35 @@
 #define AVB_VERIFY_CMD ""
 #endif
 
+#define CONTROL_PARTITION "misc"
+
+#if defined(CONFIG_CMD_AB_SELECT)
+#define AB_SELECT \
+	"if part number mmc 1 " CONTROL_PARTITION " control_part_number; " \
+	"then " \
+		"echo " CONTROL_PARTITION \
+			" partition number:${control_part_number};" \
+		"ab_select slot_name mmc ${mmcdev}:${control_part_number};" \
+	"else " \
+		"echo " CONTROL_PARTITION " partition not found;" \
+		"exit;" \
+	"fi;" \
+	"setenv slot_suffix _${slot_name};" \
+	"if part number mmc ${mmcdev} system${slot_suffix} " \
+	"system_part_number; then " \
+		"setenv bootargs_ab " \
+		"ro root=/dev/mmcblk${mmcdev}p${system_part_number} " \
+		"rootwait init=/init skip_initramfs " \
+		"androidboot.slot_suffix=${slot_suffix};" \
+		"echo A/B cmdline addition: ${bootargs_ab};" \
+		"setenv bootargs ${bootargs} ${bootargs_ab};" \
+	"else " \
+		"echo system${slot_suffix} partition not found;" \
+	"fi;"
+#else
+#define AB_SELECT ""
+#endif
+
 #define DEFAULT_COMMON_BOOT_TI_ARGS \
 	"console=" CONSOLEDEV ",115200n8\0" \
 	"fdtfile=undefined\0" \
@@ -86,10 +126,16 @@
 		"mmc dev $mmcdev; " \
 		"mmc rescan; " \
 		AVB_VERIFY_CHECK \
-		"part start mmc ${mmcdev} boot boot_start; " \
-		"part size mmc ${mmcdev} boot boot_size; " \
-		"mmc read ${loadaddr} ${boot_start} ${boot_size}; " \
-		"bootm ${loadaddr}#${fdtfile};\0 "
+		AB_SELECT \
+		"if part start mmc ${mmcdev} boot${slot_suffix} boot_start; " \
+		"then " \
+			"part size mmc ${mmcdev} boot${slot_suffix} " \
+				"boot_size; " \
+			"mmc read ${loadaddr} ${boot_start} ${boot_size}; " \
+			"bootm ${loadaddr}#${fdtfile}; " \
+		"else " \
+			"echo boot${slot_suffix} partition not found; " \
+		"fi;\0"
 
 #ifdef CONFIG_OMAP54XX
 
Signature · 1 line
2.17.1
#12 lore raw

PATCH v5 6/6 env: am57xx: Implement A/B boot process

Hi Igor,

On Fri, Jul 05, 2019 at 03:37:36PM +0300, Igor Opaniuk wrote:
[..]

Quoted 1 line
 include/environment/ti/boot.h | 58 +++++++++++++++++++++++++++++++----

[..]

include/environment/ti/boot.h line 41

Quoted 1 line
include/environment/ti/boot.h +0 -1
-	"name=boot,size=20M,uuid=${uuid_gpt_boot};" \

FWIW this patch doesn't apply to u-boot/master due to the above line. FWIW the size of the boot partition defined in this file has always been equal to 10M in u-boot/master, so most likely this patch depends on a local 10M->20M increase of the boot partition size.

Signature · 2 lines
Best Regards,
Eugeniu.
#13 lore raw

PATCH v5 6/6 env: am57xx: Implement A/B boot process

Hi Eugeniu,

Quoted 5 lines · Eugeniu Rosca

Hi Igor,

On Fri, Jul 05, 2019 at 03:37:36PM +0300, Igor Opaniuk wrote:
[..]

Quoted 9 lines
Quoted 1 line
 include/environment/ti/boot.h | 58 +++++++++++++++++++++++++++++++----

[..]

Quoted 1 line

- "name=boot,size=20M,uuid=${uuid_gpt_boot};" \

FWIW this patch doesn't apply to u-boot/master due to the above line. FWIW the size of the boot partition defined in this file has always been equal to 10M in u-boot/master, so most likely this patch depends on a local 10M->20M increase of the boot partition size.

It was me who asked Igor to do so, as I suggest this patch will be applied on top of [1], and I guess it's specified in PATCH #0. I have an integration branch [2] which contains all Android-related patch I want to be merged during the merge window, and A/B series is applied on top of [1] there. There is inevitable dependency clash between patches, so I figured it doesn't matter what the order is, we just should keep that order somewhere, and for me it's [2], for Igor it's PATCH #0 which mentions dependency patches for this series. Otherwise we risk this patch to overwrite the changes I did in [1], and I'd like to avoid it.

Not sure what is the commonly adopted mechanism for this (specifying dependencies), but in this case I guess I will ask the custodians to apply patches in correct order, or apply patches according to [2], that's the best I came up with. Please tell me if you see more standard/wise choice for this.

[1] https://patchwork.ozlabs.org/patch/1126434/
[2] https://github.com/joe-skb7/u-boot-misc/commits/android-next

Quoted 3 lines

--
Best Regards,
Eugeniu.

#14 lore raw

PATCH v5 6/6 env: am57xx: Implement A/B boot process

Hello Sam,

Quoted 1 line · Sam Protsenko

On Fri, Jul 5, 2019 at 9:52 PM Eugeniu Rosca <[email protected]> wrote:

[..]

Quoted 1 line
Quoted 1 line

FWIW this patch doesn't apply to u-boot/master due to the above line.

[..]

Quoted 2 lines

this patch will be
applied on top of [1], and I guess it's specified in PATCH #0.

Oops. My bad. I didn't pay enough attention to the cover letter. Sorry for the noise.

Quoted 1 line

[1] https://patchwork.ozlabs.org/patch/1126434/

Signature · 2 lines
Best Regards,
Eugeniu.
#15 lore raw

PATCH v5 6/6 env: am57xx: Implement A/B boot process

In reply to Igor Opaniuk
Quoted 39 lines · Igor Opaniuk

From: Ruslan Trofymenko <[email protected]>

Add support for A/B boot process on AM57xx based boards:

1. Define 'slot_suffix' variable (using 'ab_select' command)
2. Extend 'emmc_android_boot' boot command (add commands for A/B boot process)

'ab_select' command is used to decide which slot should be used for booting up. A/B metadata resides in 'misc' partition.

To activate the A/B boot process, the following config options must be set:

CONFIG_ANDROID_AB=y
CONFIG_CMD_AB_SELECT=y

For successful A/B boot, the corresponding A/B infrastructure must be involved on Android side [1] (including mounting system as root), and disk must be partitioned accordingly.

When A/B boot is enabled, there are some known limitations currently exist (not related to A/B patches, need to be implemented later):

1. The 'Verified Boot' sequence is not supported
2. dev path to system partition (system_a or system_b) is passed via 'bootargs' as 'root=' argument like 'root=/dev/mmcblk1p12', but further we'll need to rework it with respect to dm-verity requirements [2]

In case when A/B partitions are not present in system (and A/B boot is enabled), boot up process will be terminated and next message will be shown:

"boot_a(b) partition not found"

[1] https://source.android.com/devices/tech/ota/ab
[2] https://source.android.com/devices/tech/ota/ab/ab_implement#kernel

Quoted 6 lines

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190724/ec4aa684/attachment-0001.sig>
#16 lore raw

v5 cover android: implement A/B boot process

In reply to Igor Opaniuk
Quoted 125 lines · Igor Opaniuk

*** This patch series depends on [4], [5], [6] and should be applied after these paticular patches.

This patch series adds support for Android A/B boot process [1]. Main steps of A/B boot process are:
- A/B metadata integrity check
- looking for the current slot (where the system should be booting from)
- getting the name of the current boot partition (boot_a or boot_b) and loading the corresponding Android boot image
- getting the name of the current system partition (system_a or system_b) and passing of its full name via kernel command line (like 'root=/dev/mmcblk1p11')
- passing current slot via kernel command line (like 'androidboot.slot_suffix=_a') and via A/B metadata (e.g. via misc partition)
- A/B metadata processing: setting the boot success flag for current slot, handling the retry counter, etc

A/B metadata is organized according to Android reference [2] and stored on 'misc' partition. On the first A/B boot process, when 'misc' partition doesn't contain required data, default A/B metadata will be created and stored in 'misc' partition. In the end of the Android boot, 'update_verifier' and 'update_engine' services are processing the A/B metadata through the Boot Control HAL. To confirm the boot was successful using current slot, "boot success" flag must be set on Android side.

To enable Android A/B support in U-Boot:
1. Set the following config options:

CONFIG_ANDROID_AB=y
CONFIG_CMD_AB_SELECT=y

2. Change the disk layout so that it has sloted boot partitions. E.g. instead of 'boot' and 'system' partitions there should be 'boot_a', 'boot_b', 'system_a' and 'system_b' partitions.

To be able to actually test this patch series, the A/B features must be implemented and enabled in Android as well (see [1] for details).

Documentation and corresponding test for A/B boot is present here. The last patch in this series integrates A/B boot support on AM57xx based boards (though it's not enabled by default). Future users of A/B boot feature can use it as a reference.

This series is a part of previous submission [3] by Alex Deymo. It contains only A/B feature that was stripped out from there with some modifications for using with "bootm" command preferred in upstream.

Changes in v5:
* Move tests to android subdir
* In new version of sgdisk (0.8.10.2) short options don't work. That can be a regression in sgdisk, but anyway let's use long options, because otherwise the test fails.
* Fix boot part size (preserve the new value).
* Move doc to proper place
* Handle ret values properly for blk_dread()/blk_dwrite properly using IS_ERR_VALUE() macro

Changes in v4:
* Removed android_bl_msg.h and re-used the one, which was introduced by Eugeniu Rosca in [4] and [5].
* Initially we had intention to use "bcb" command and scripting for providing proper boot sequence logic (slot selection, decrementing the counter for each slot), but found out that it's better to keep using a dedicated command ab_select to avoid huge amount of scripting in the headers in includes/config for each platform.
* Removed ANDROID_ prefix in multiple macros.
* Addressed comments from Simon

Changes in v3:
* Minor fixes in the ab metadata handling (added additional sanity checks).
* As Ruslan Trofymenko left Linaro and won't address comments anymore, continue (added my S-b tag) upstreaming patches on my own.

Changes in v2:
* 'android_ab_select' command is renamed to 'ab_select' command and moved to separate 'Android support commands' menu
* For am57xx boards slotted sections (e.g. system_a and system_b) are added to the default sections if CONFIG_CMD_AB_SELECT flag is defined
* Returned function error codes are clarified (errno using)
* Some types constants and files are renamed
* Assertion condition is clarified in test case
* 'debug' calls are changed to 'log_debug'
* The Guide is clarified by the results of changes

[1] https://source.android.com/devices/tech/ota/ab/ab_implement
[2] bootable/recovery/bootloader_message/include/bootloader_message/bootloader_message.h
[3] https://lists.denx.de/pipermail/u-boot/2017-April/285841.html
[4] https://patchwork.ozlabs.org/patch/1080394/
[5] https://patchwork.ozlabs.org/patch/1104243/
[6] https://patchwork.ozlabs.org/patch/1116012/

Ruslan Trofymenko (6):
disk: part: Extend API to get partition info
common: Implement A/B metadata
cmd: Add 'ab_select' command
test/py: Add base test case for A/B updates
doc: android: Add simple guide for A/B updates
env: am57xx: Implement A/B boot process

 cmd/Kconfig                           |  15 ++
 cmd/Makefile                          |   1 +
 cmd/ab_select.c                       |  52 +++++
 common/Kconfig                        |  10 +
 common/Makefile                       |   1 +
 common/android_ab.c                   | 300 ++++++++++++++++++++++++++
 configs/sandbox_defconfig             |   2 +
 disk/part.c                           |  68 ++++++
 doc/android/ab.txt                    |  67 ++++++
 include/android_ab.h                  |  34 +++
 include/environment/ti/boot.h         |  58 ++++-
 include/part.h                        |  21 ++
 test/py/tests/test_android/test_ab.py |  75 +++++++
 13 files changed, 698 insertions(+), 6 deletions(-)
 create mode 100644 cmd/ab_select.c
 create mode 100644 common/android_ab.c
 create mode 100644 doc/android/ab.txt
 create mode 100644 include/android_ab.h
 create mode 100644 test/py/tests/test_android/test_ab.py
Quoted 3 lines

--
2.17.1

FYI:
I've checked the latest master today, almost all needed dependent patches are merged, except [1] and [2]. I've just left my R-b tag for Sam's patch, so now
we will be ready to go with A/B patch series after these two are merged.

[1] https://patchwork.ozlabs.org/patch/1126434/
[2] https://patchwork.ozlabs.org/patch/1116012/

Signature · 8 lines
Best regards - Freundliche Grüsse - Meilleures salutations

Igor Opaniuk

mailto: igor.opaniuk at gmail.com
skype: igor.opanyuk
+380 (93) 836 40 67
http://ua.linkedin.com/in/iopaniuk