Skip to contentSkip to conversation

bootstd: android: rollback indexes and misc system space messages

PATCH In review Igor Opaniuk 3 participants 11 messages Nov 11 – Nov 19

lore
Share & tools
lore
#0 lore raw

cover bootstd: android: rollback indexes and misc system space messages

Two things the Android bootmeth does not do yet.

Patch 1 stores the rollback indexes of a known-good slot. Without it the stored indexes never advance and rollback protection has no effect.

Patches 2 and 3 handle the messages Android leaves for the bootloader in the misc system space. Patch 2 syncs bootloader_message.h with android-17.0.0_r1, and patch 3 applies the MTE and kcmdline messages to the kernel command line.

---
Igor Opaniuk (3):
bootstd: android: update the rollback indexes of a known-good slot
android: sync bootloader_message.h with AOSP (misc system space)
bootstd: android: act on the misc memtag and kcmdline messages

 MAINTAINERS                          |   2 +
 boot/Kconfig                         |  11 +++
 boot/Makefile                        |   2 +-
 boot/android_ab.c                    |  34 ++++++++
 boot/android_misc.c                  | 145 +++++++++++++++++++++++++++++++++++
 boot/bootmeth_android.c              | 132 +++++++++++++++++++++++++++++++
 common/avb_verify.c                  |  35 +++++++++
 include/android_ab.h                 |  19 +++++
 include/android_bootloader_message.h |  92 +++++++++++++++++++++-
 include/android_misc.h               |  56 ++++++++++++++
 include/avb_verify.h                 |  16 ++++
 11 files changed, 542 insertions(+), 2 deletions(-)

---
base-commit: 0e3fb22a5b2e330bd73014f0ced13f5df259d087
change-id: 20261028-upstream-android_rollback_misc_b4-7f16f8f69336

Best regards,

Signature · 1 line
Igor Opaniuk <[email protected]>
#1 lore raw

PATCH 1/3 bootstd: android: update the rollback indexes of a known-good slot

libavb only checks the rollback indexes of the images against the stored ones. Storing the new values is left to the bootloader [1], and the Android bootmeth never did it, so the stored indexes never advanced and older, vulnerable images kept verifying.

Raise the stored indexes to those of the verified slot before booting it. With A/B, wait until Android has marked the slot successful, so a failed update can still fall back to the previous slot. Without the OP-TEE AVB TA there is nowhere to store them and nothing is written.

[1] https://android.googlesource.com/platform/external/avb/+/refs/tags/android-17.0.0_r1/README.md#updating-stored-rollback-indexes

---
 boot/android_ab.c       | 34 ++++++++++++++++++++++++++++++++++
 boot/bootmeth_android.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 common/avb_verify.c     | 35 +++++++++++++++++++++++++++++++++++
 include/android_ab.h    | 19 +++++++++++++++++++
 include/avb_verify.h    | 16 ++++++++++++++++
 5 files changed, 152 insertions(+)
boot/android_ab.c +34 -0
index f2b1110aa24..48745f2c2d2 100644
@@ -376,6 +376,40 @@ int ab_select_slot(struct blk_desc *dev_desc, struct disk_partition *part_info,
 	return slot;
 }
 
+int ab_get_control(struct blk_desc *dev_desc,
+		   const struct disk_partition *part_info,
+		   struct bootloader_control *abc)
+{
+	struct bootloader_control *disk_abc;
+	int ret;
+
+	if (!dev_desc || !part_info || !abc)
+		return -EINVAL;
+
+	ret = ab_control_create_from_disk(dev_desc, part_info, &disk_abc, 0);
+	if (ret < 0)
+		return ret;
+
+	ret = -ENODATA;
+	if (disk_abc->crc32_le != ab_control_compute_crc(disk_abc))
+		log_debug("ANDROID: Invalid A/B metadata CRC-32\n");
+	else if (disk_abc->magic != BOOT_CTRL_MAGIC)
+		log_debug("ANDROID: Unknown A/B metadata: %.8x\n", disk_abc->magic);
+	else if (disk_abc->version > BOOT_CTRL_VERSION)
+		log_debug("ANDROID: Unsupported A/B metadata version: %u\n",
+			  disk_abc->version);
+	else if (disk_abc->nb_slot > ARRAY_SIZE(disk_abc->slot_info))
+		log_debug("ANDROID: Wrong number of slots %u\n", disk_abc->nb_slot);
+	else
+		ret = 0;
+
+	if (!ret)
+		memcpy(abc, disk_abc, sizeof(*abc));
+	free(disk_abc);
+
+	return ret;
+}
+
 int ab_dump_abc(struct blk_desc *dev_desc, struct disk_partition *part_info)
 {
 	struct bootloader_control *abc;
boot/bootmeth_android.c +48 -0

66 lines not on this page · raw message

common/avb_verify.c +35 -0
index 4168b2434b6..c61cc5ef244 100644
@@ -1233,6 +1233,41 @@ __weak AvbIOResult avb_read_root_key_digest(AvbOps *ops, uint8_t *digest)
 }
 #endif
 
+AvbIOResult avb_update_rollback_indexes(AvbOps *ops,
+					const AvbSlotVerifyData *data)
+{
+	AvbIOResult rc;
+	u64 stored;
+	int i;
+
+	/* without the TA there is no store for the indexes (reads give 0) */
+	if (!IS_ENABLED(CONFIG_OPTEE_TA_AVB))
+		return AVB_IO_RESULT_OK;
+
+	if (!ops->read_rollback_index || !ops->write_rollback_index)
+		return AVB_IO_RESULT_ERROR_IO;
+
+	for (i = 0; i < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; i++) {
+		/* unused locations stay 0 and never need an update */
+		if (!data->rollback_indexes[i])
+			continue;
+
+		rc = ops->read_rollback_index(ops, i, &stored);
+		if (rc != AVB_IO_RESULT_OK)
+			return rc;
+		if (stored >= data->rollback_indexes[i])
+			continue;
+
+		printf("Rollback index %d: %llu -> %llu\n", i, stored,
+		       data->rollback_indexes[i]);
+		rc = ops->write_rollback_index(ops, i, data->rollback_indexes[i]);
+		if (rc != AVB_IO_RESULT_OK)
+			return rc;
+	}
+
+	return AVB_IO_RESULT_OK;
+}
+
 /**
  * ============================================================================
  * AVB2.0 AvbOps alloc/initialisation/free
include/android_ab.h +19 -0
index 838230e06f8..6cfc62b824c 100644
@@ -9,6 +9,7 @@
 #include <stdbool.h>
 
 struct blk_desc;
+struct bootloader_control;
 struct disk_partition;
 
 /* Android standard boot slot names are 'a', 'b', 'c', ... */
@@ -36,6 +37,24 @@ struct disk_partition;
 int ab_select_slot(struct blk_desc *dev_desc, struct disk_partition *part_info,
                    bool dec_tries);
 
+/**
+ * ab_get_control() - Read and validate the bootloader control block.
+ *
+ * @dev_desc: Device description pointer
+ * @part_info: Partition information (normally "misc")
+ * @abc: Where to store the bootloader_control (sizeof(struct
+ *	bootloader_control) bytes)
+ *
+ * Reads the A/B metadata without modifying it, for callers that only need
+ * to look at it (slot flags, active slot). A bad CRC, magic or version is
+ * an error, the metadata is not re-initialized.
+ *
+ * Return: 0 on success, or a negative on error
+ */
+int ab_get_control(struct blk_desc *dev_desc,
+		   const struct disk_partition *part_info,
+		   struct bootloader_control *abc);
+
 /**
  * ab_dump_abc() - Dump ABC information for specific partition.
  *
include/avb_verify.h +16 -0
index 2b5b24958ae..8479e327559 100644
@@ -83,7 +83,23 @@ int avb_ops_add_preloaded(AvbOps *ops, const char *partition, void *addr,
 			  size_t size, size_t max_size);
 #ifdef CONFIG_OPTEE_TA_AVB
 AvbIOResult avb_write_lock_state(AvbOps *ops, bool locked);
+
 #endif
+
+/**
+ * avb_update_rollback_indexes() - Store the rollback indexes of a verified slot
+ *
+ * libavb never writes the rollback indexes: once the bootloader knows the
+ * slot is good (it verified and, with A/B, booted successfully before), it
+ * raises every stored index that is below the one in the slot's vbmeta
+ * images, so that older images are refused from then on.
+ *
+ * @ops: AvbOps from avb_ops_alloc()
+ * @data: result of a successful avb_slot_verify()
+ * Return: AVB_IO_RESULT_OK, or the error of the first failed read or write
+ */
+AvbIOResult avb_update_rollback_indexes(AvbOps *ops,
+					const AvbSlotVerifyData *data);
 void avb_ops_free(AvbOps *ops);
 
 /**
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 1/3] bootstd: android: update the rollback indexes of a known-good slot

On Wed, 11 Nov 2026 10:13:32 +0100, Igor Opaniuk <[email protected]> wrote:

Hi,

Thanks for your patch.

Quoted 6 lines
boot/bootmeth_android.c +0 -0
index 6f7e232d2c3c..3d1e64aa2b41 100644

boot/bootmeth_android.c static int run_avb_verification(struct bootflow *bflow) · line 581

Quoted 10 lines
boot/bootmeth_android.c +7 -0
@@ -539,6 +572,21 @@ static int run_avb_verification(struct bootflow *bflow)
 		}
 	}
 
+	/*
+	 * Rollback protection: libavb only compares the rollback indexes, the
+	 * bootloader stores them. Raise the stored indexes to those of the
+	 * verified images once the slot is known good, so that a bad update
+	 * which never boots cannot lock out the images it replaced.
+	 */
+	if (result == AVB_SLOT_VERIFY_RESULT_OK && slot_is_known_good(bflow)) {

Linked to your lock/unlock support (04d89df5c259), this needs an rollback index than the stored one, the unlocked branch above fails the boot. This is a false positive, rollback index failures are allowed when unlocked [1].

[1] https://android.googlesource.com/platform/external/avb/+/refs/tags/android-17.0.0_r1/README.md#recommended-bootflow

include/avb_verify.h int avb_ops_add_preloaded(AvbOps *ops, const char *partition, void *addr, · line 86

Quoted 10 lines
include/avb_verify.h +1 -0
index 2b5b24958ae4..8479e3275593 100644
@@ -83,7 +83,23 @@ int avb_ops_add_preloaded(AvbOps *ops, const char *partition, void *addr,
 			  size_t size, size_t max_size);
 #ifdef CONFIG_OPTEE_TA_AVB
 AvbIOResult avb_write_lock_state(AvbOps *ops, bool locked);
+

not needed

update: when an unlocked device is flashed with images that have a lower

update: when an unlocked device is flashed with images that have a lower

Signature · 1 line
Guillaume La Roque <[email protected]>
#3 lore raw

Re: [PATCH 1/3] bootstd: android: update the rollback indexes of a known-good slot

In reply to Igor Opaniuk

Hi Igor,

Thank you for the patch.

Quoted 79 lines

libavb only checks the rollback indexes of the images against the stored ones. Storing the new values is left to the bootloader [1], and the Android bootmeth never did it, so the stored indexes never advanced and older, vulnerable images kept verifying.

Raise the stored indexes to those of the verified slot before booting it. With A/B, wait until Android has marked the slot successful, so a failed update can still fall back to the previous slot. Without the OP-TEE AVB TA there is nowhere to store them and nothing is written.

[1] https://android.googlesource.com/platform/external/avb/+/refs/tags/android-17.0.0_r1/README.md#updating-stored-rollback-indexes

---
 boot/android_ab.c       | 34 ++++++++++++++++++++++++++++++++++
 boot/bootmeth_android.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 common/avb_verify.c     | 35 +++++++++++++++++++++++++++++++++++
 include/android_ab.h    | 19 +++++++++++++++++++
 include/avb_verify.h    | 16 ++++++++++++++++
 5 files changed, 152 insertions(+)
boot/android_ab.c +34 -0
index f2b1110aa24b..48745f2c2d2d 100644
@@ -376,6 +376,40 @@ int ab_select_slot(struct blk_desc *dev_desc, struct disk_partition *part_info,
 	return slot;
 }
 
+int ab_get_control(struct blk_desc *dev_desc,
+		   const struct disk_partition *part_info,
+		   struct bootloader_control *abc)
+{
+	struct bootloader_control *disk_abc;
+	int ret;
+
+	if (!dev_desc || !part_info || !abc)
+		return -EINVAL;
+
+	ret = ab_control_create_from_disk(dev_desc, part_info, &disk_abc, 0);
+	if (ret < 0)
+		return ret;
+
+	ret = -ENODATA;
+	if (disk_abc->crc32_le != ab_control_compute_crc(disk_abc))
+		log_debug("ANDROID: Invalid A/B metadata CRC-32\n");
+	else if (disk_abc->magic != BOOT_CTRL_MAGIC)
+		log_debug("ANDROID: Unknown A/B metadata: %.8x\n", disk_abc->magic);
+	else if (disk_abc->version > BOOT_CTRL_VERSION)
+		log_debug("ANDROID: Unsupported A/B metadata version: %u\n",
+			  disk_abc->version);
+	else if (disk_abc->nb_slot > ARRAY_SIZE(disk_abc->slot_info))
+		log_debug("ANDROID: Wrong number of slots %u\n", disk_abc->nb_slot);
+	else
+		ret = 0;
+
+	if (!ret)
+		memcpy(abc, disk_abc, sizeof(*abc));
+	free(disk_abc);
+
+	return ret;
+}
+
 int ab_dump_abc(struct blk_desc *dev_desc, struct disk_partition *part_info)
 {
 	struct bootloader_control *abc;
boot/bootmeth_android.c +1 -0
index 6f7e232d2c3c..3d1e64aa2b41 100644
@@ -8,6 +8,7 @@
 #define LOG_CATEGORY UCLASS_BOOTSTD
 
 #include <android_ab.h>
+#include <android_bootloader_message.h>
 #include <android_image.h>
 #if CONFIG_IS_ENABLED(AVB_VERIFY)
 #include <avb_verify.h>

boot/bootmeth_android.c static int avb_append_commandline(struct bootflow *bflow, char *cmdline) · line 443

Quoted 5 lines
boot/bootmeth_android.c +2 -0
@@ -438,6 +439,38 @@ static int avb_append_commandline(struct bootflow *bflow, char *cmdline)
 	return 0;
 }
 
+/**
+ * slot_is_known_good() - Whether the selected slot has booted successfully

nitpick: Why can't have successful in the function name? This matches slot_info[slot].successful_boot so it seems more appropriate than 'good'.

For example slot_booted_successfully()? Feel free to pick another one

Quoted 40 lines
boot/bootmeth_android.c +33 -0
+ *
+ * With A/B, Android marks a slot successful (boot control HAL) once it has
+ * booted far enough; until then a new update is still on probation. Without
+ * A/B there is no such signal and the slot counts as good.
+ *
+ * @bflow: Bootflow being booted
+ * Return: true if the slot is known good
+ */
+static bool slot_is_known_good(struct bootflow *bflow)
+{
+	struct blk_desc *desc = dev_get_uclass_plat(bflow->blk);
+	struct android_priv *priv = bflow->bootmeth_priv;
+	struct disk_partition misc;
+	struct bootloader_control abc;
+	int slot;
+
+	if (!IS_ENABLED(CONFIG_ANDROID_AB) || !priv->slot)
+		return true;
+
+	slot = priv->slot[0] - 'a';
+	if (part_get_info_by_name(desc, BCB_PART_NAME, &misc) < 0)
+		return false;
+	if (ab_get_control(desc, &misc, &abc) < 0)
+		return false;
+	if (slot < 0 || slot >= abc.nb_slot)
+		return false;
+
+	return abc.slot_info[slot].successful_boot;
+}
+
 static int run_avb_verification(struct bootflow *bflow)
 {
 	struct blk_desc *desc = dev_get_uclass_plat(bflow->blk);
@@ -539,6 +572,21 @@ static int run_avb_verification(struct bootflow *bflow)
 		}
 	}
 
+	/*
+	 * Rollback protection: libavb only compares the rollback indexes, the
+	 * bootloader stores them. Raise the stored indexes to those of the

boot/bootmeth_android.c static int run_avb_verification(struct bootflow *bflow) · line 587

Quoted 10 lines
boot/bootmeth_android.c +10 -0
+	 * verified images once the slot is known good, so that a bad update
+	 * which never boots cannot lock out the images it replaced.
+	 */
+	if (result == AVB_SLOT_VERIFY_RESULT_OK && slot_is_known_good(bflow)) {
+		AvbIOResult rc;
+
+		rc = avb_update_rollback_indexes(avb_ops, out_data);
+		if (rc != AVB_IO_RESULT_OK)
+			log_warning("rollback indexes not updated: %s\n",
+				    str_avb_io_error(rc));

So this is ran on every boot? is the index incremented each time? Sorry if I don't follow, this is a bit unclear to me.

Signature · 1 line
Mattijs Korpershoek <[email protected]>
#4 lore raw

PATCH 2/3 android: sync bootloader_message.h with AOSP (misc system space)

In reply to Igor Opaniuk

Android uses the "system space" of the misc partition (32 KiB in) to pass messages to the bootloader: the Virtual A/B merge status, the Arm MTE mode and kernel command line flags. Add their definitions from bootloader_message_core.h at android-17.0.0_r1 [1], copied verbatim like the rest of the file so that later syncs stay a plain diff.

[1] https://android.googlesource.com/platform/bootable/recovery/+/refs/tags/android-17.0.0_r1/bootloader_message/include/bootloader_message/bootloader_message_core.h

---
 include/android_bootloader_message.h | 92 +++++++++++++++++++++++++++++++++++-
 1 file changed, 91 insertions(+), 1 deletion(-)
include/android_bootloader_message.h +91 -1
index 286d7ab0f31..9092f74c4db 100644
@@ -27,11 +27,15 @@
 // 0   - 2K     For bootloader_message
 // 2K  - 16K    Used by Vendor's bootloader (the 2K - 4K range may be optionally used
 //              as bootloader_message_ab struct)
-// 16K - 64K    Used by uncrypt and recovery to store wipe_package for A/B devices
+// 16K - 32K    Used by uncrypt and recovery to store wipe_package for A/B devices
+// 32K - 64K    System space, used for miscellanious AOSP features. See below.
 // Note that these offsets are admitted by bootloader,recovery and uncrypt, so they
 // are not configurable without changing all of them.
 static const size_t BOOTLOADER_MESSAGE_OFFSET_IN_MISC = 0;
+static const size_t VENDOR_SPACE_OFFSET_IN_MISC = 2 * 1024;
 static const size_t WIPE_PACKAGE_OFFSET_IN_MISC = 16 * 1024;
+static const size_t SYSTEM_SPACE_OFFSET_IN_MISC = 32 * 1024;
+static const size_t SYSTEM_SPACE_SIZE_IN_MISC = 32 * 1024;
 
 /* Bootloader Message (2-KiB)
  *
@@ -81,6 +85,92 @@ struct bootloader_message {
     char reserved[1184];
 };
 
+// Holds Virtual A/B merge status information. Current version is 1. New fields
+// must be added to the end.
+struct misc_virtual_ab_message {
+  uint8_t version;
+  uint32_t magic;
+  uint8_t merge_status;  // IBootControl 1.1, MergeStatus enum.
+  uint8_t source_slot;   // Slot number when merge_status was written.
+  uint8_t reserved[57];
+} __attribute__((packed));
+
+struct misc_memtag_message {
+  uint8_t version;
+  uint32_t magic; // magic string for treble compat
+  uint32_t memtag_mode;
+  uint8_t reserved[55];
+} __attribute__((packed));
+
+struct misc_kcmdline_message {
+  uint8_t version;
+  uint32_t magic;
+  uint64_t kcmdline_flags;
+  uint8_t reserved[51];
+} __attribute__((packed));
+
+// holds generic platform info, managed by misctrl
+struct misc_control_message {
+  uint8_t version;
+  uint32_t magic;
+  uint64_t misctrl_flags;
+  uint8_t reserved[51];
+} __attribute__((packed));
+
+#define MISC_VIRTUAL_AB_MESSAGE_VERSION 2
+#define MISC_VIRTUAL_AB_MAGIC_HEADER 0x56740AB0
+
+#define MISC_MEMTAG_MESSAGE_VERSION 1
+#define MISC_MEMTAG_MAGIC_HEADER 0x5afefe5a
+#define MISC_MEMTAG_MODE_MEMTAG 0x1
+#define MISC_MEMTAG_MODE_MEMTAG_ONCE 0x2
+#define MISC_MEMTAG_MODE_MEMTAG_KERNEL 0x4
+#define MISC_MEMTAG_MODE_MEMTAG_KERNEL_ONCE 0x8
+#define MISC_MEMTAG_MODE_MEMTAG_OFF 0x10
+// This is set when the state was overridden forcibly. This does not need to be
+// interpreted by the bootloader but is only for bookkeeping purposes so
+// userspace knows what to do when the override is undone.
+// See system/extras/mtectrl in AOSP for more information.
+#define MISC_MEMTAG_MODE_FORCED 0x20
+
+#define MISC_KCMDLINE_MESSAGE_VERSION 1
+#define MISC_KCMDLINE_MAGIC_HEADER 0x6ab5110c
+#define MISC_KCMDLINE_BINDER_FORCE_RUST 0x1
+#define MISC_KCMDLINE_BINDER_FORCE_C 0x2
+
+#define MISC_CONTROL_MESSAGE_VERSION 1
+#define MISC_CONTROL_MAGIC_HEADER 0x736d6f72
+#define MISC_CONTROL_16KB_BEFORE 0x1
+
+#ifndef __UBOOT__
+#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
+static_assert(sizeof(struct misc_virtual_ab_message) == 64,
+              "struct misc_virtual_ab_message has wrong size");
+static_assert(sizeof(struct misc_memtag_message) == 64,
+              "struct misc_memtag_message has wrong size");
+static_assert(sizeof(struct misc_kcmdline_message) == 64,
+              "struct misc_kcmdline_message has wrong size");
+static_assert(sizeof(struct misc_control_message) == 64,
+              "struct misc_control_message has wrong size");
+#endif
+#endif /* __UBOOT__ */
+
+// This struct is not meant to be used directly, rather, it is to make
+// computation of offsets easier. New fields must be added to the end.
+struct misc_system_space_layout {
+  struct misc_virtual_ab_message virtual_ab_message;
+  struct misc_memtag_message memtag_message;
+  struct misc_kcmdline_message kcmdline_message;
+  struct misc_control_message control_message;
+} __attribute__((packed));
+
+#ifndef __UBOOT__
+#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
+static_assert(sizeof(struct misc_system_space_layout) % 64 == 0,
+              "prefer to extend by 64 byte chunks, for consistency");
+#endif
+#endif /* __UBOOT__ */
+
 /**
  * We must be cautious when changing the bootloader_message struct size,
  * because A/B-specific fields may end up with different offsets.
Signature · 1 line
2.53.0
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] android: sync bootloader_message.h with AOSP (misc system space)

Quoted 9 lines · Igor Opaniuk

Android uses the "system space" of the misc partition (32 KiB in) to pass messages to the bootloader: the Virtual A/B merge status, the Arm MTE mode and kernel command line flags. Add their definitions from bootloader_message_core.h at android-17.0.0_r1 [1], copied verbatim like the rest of the file so that later syncs stay a plain diff.

[1] https://android.googlesource.com/platform/bootable/recovery/+/refs/tags/android-17.0.0_r1/bootloader_message/include/bootloader_message/bootloader_message_core.h

[...]

Signature · 1 line
Guillaume La Roque <[email protected]>
#6 lore raw

Re: [PATCH 2/3] android: sync bootloader_message.h with AOSP (misc system space)

In reply to Igor Opaniuk

Hi Igor,

Thank you for the patch.

Quoted 7 lines

Android uses the "system space" of the misc partition (32 KiB in) to pass messages to the bootloader: the Virtual A/B merge status, the Arm MTE mode and kernel command line flags. Add their definitions from bootloader_message_core.h at android-17.0.0_r1 [1], copied verbatim like the rest of the file so that later syncs stay a plain diff.

[1] https://android.googlesource.com/platform/bootable/recovery/+/refs/tags/android-17.0.0_r1/bootloader_message/include/bootloader_message/bootloader_message_core.h

I've downloaded a copy of the above repo, and using the android-17.0.0_r1 tag, I see some differences between the AOSP file and the copy we have here.

I do think that these diffs make sense (for example, using static const instead of constexpr).
However, it feels misleading to me that we say it's a verbatim copy because it's not.

Please reword the commit message.

More details below

Quoted 14 lines
---
 include/android_bootloader_message.h | 92 +++++++++++++++++++++++++++++++++++-
 1 file changed, 91 insertions(+), 1 deletion(-)
include/android_bootloader_message.h +0 -0
index 286d7ab0f31e..9092f74c4db8 100644
@@ -27,11 +27,15 @@
 // 0   - 2K     For bootloader_message
 // 2K  - 16K    Used by Vendor's bootloader (the 2K - 4K range may be optionally used
 //              as bootloader_message_ab struct)

include/android_bootloader_message.h line 38

Quoted 10 lines
include/android_bootloader_message.h +5 -1
-// 16K - 64K    Used by uncrypt and recovery to store wipe_package for A/B devices
+// 16K - 32K    Used by uncrypt and recovery to store wipe_package for A/B devices
+// 32K - 64K    System space, used for miscellanious AOSP features. See below.
 // Note that these offsets are admitted by bootloader,recovery and uncrypt, so they
 // are not configurable without changing all of them.
 static const size_t BOOTLOADER_MESSAGE_OFFSET_IN_MISC = 0;
+static const size_t VENDOR_SPACE_OFFSET_IN_MISC = 2 * 1024;
 static const size_t WIPE_PACKAGE_OFFSET_IN_MISC = 16 * 1024;
+static const size_t SYSTEM_SPACE_OFFSET_IN_MISC = 32 * 1024;
+static const size_t SYSTEM_SPACE_SIZE_IN_MISC = 32 * 1024;

In the original file, these are constexpr:
https://android.googlesource.com/platform/bootable/recovery/+/refs/tags/android-17.0.0_r1/bootloader_message/include/bootloader_message/bootloader_message_core.h#50

I understand that we can't have that in U-Boot, but we should mention in the commit message that some U-Boot specifics adaptations were needed.

Quoted 10 lines
include/android_bootloader_message.h +3 -0
 /* Bootloader Message (2-KiB)
  *
@@ -81,6 +85,92 @@ struct bootloader_message {
     char reserved[1184];
 };
 
+// Holds Virtual A/B merge status information. Current version is 1. New fields
+// must be added to the end.
+struct misc_virtual_ab_message {

include/android_bootloader_message.h struct bootloader_message { · line 100

Quoted 10 lines
include/android_bootloader_message.h +10 -0
+  uint8_t version;
+  uint32_t magic;
+  uint8_t merge_status;  // IBootControl 1.1, MergeStatus enum.
+  uint8_t source_slot;   // Slot number when merge_status was written.
+  uint8_t reserved[57];
+} __attribute__((packed));
+
+struct misc_memtag_message {
+  uint8_t version;
+  uint32_t magic; // magic string for treble compat

This is indentented differently in the original file. Why did we change it?

Quoted 35 lines
include/android_bootloader_message.h +35 -0
+  uint32_t memtag_mode;
+  uint8_t reserved[55];
+} __attribute__((packed));
+
+struct misc_kcmdline_message {
+  uint8_t version;
+  uint32_t magic;
+  uint64_t kcmdline_flags;
+  uint8_t reserved[51];
+} __attribute__((packed));
+
+// holds generic platform info, managed by misctrl
+struct misc_control_message {
+  uint8_t version;
+  uint32_t magic;
+  uint64_t misctrl_flags;
+  uint8_t reserved[51];
+} __attribute__((packed));
+
+#define MISC_VIRTUAL_AB_MESSAGE_VERSION 2
+#define MISC_VIRTUAL_AB_MAGIC_HEADER 0x56740AB0
+
+#define MISC_MEMTAG_MESSAGE_VERSION 1
+#define MISC_MEMTAG_MAGIC_HEADER 0x5afefe5a
+#define MISC_MEMTAG_MODE_MEMTAG 0x1
+#define MISC_MEMTAG_MODE_MEMTAG_ONCE 0x2
+#define MISC_MEMTAG_MODE_MEMTAG_KERNEL 0x4
+#define MISC_MEMTAG_MODE_MEMTAG_KERNEL_ONCE 0x8
+#define MISC_MEMTAG_MODE_MEMTAG_OFF 0x10
+// This is set when the state was overridden forcibly. This does not need to be
+// interpreted by the bootloader but is only for bookkeeping purposes so
+// userspace knows what to do when the override is undone.
+// See system/extras/mtectrl in AOSP for more information.
+#define MISC_MEMTAG_MODE_FORCED 0x20
+

include/android_bootloader_message.h struct bootloader_message { · line 145

Quoted 10 lines
include/android_bootloader_message.h +10 -0
+#define MISC_KCMDLINE_MESSAGE_VERSION 1
+#define MISC_KCMDLINE_MAGIC_HEADER 0x6ab5110c
+#define MISC_KCMDLINE_BINDER_FORCE_RUST 0x1
+#define MISC_KCMDLINE_BINDER_FORCE_C 0x2
+
+#define MISC_CONTROL_MESSAGE_VERSION 1
+#define MISC_CONTROL_MAGIC_HEADER 0x736d6f72
+#define MISC_CONTROL_16KB_BEFORE 0x1
+
+#ifndef __UBOOT__

I understand why we need the #ifndef, but again this is not a verbatim copy

Quoted 9 lines
include/android_bootloader_message.h +9 -0
+#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
+static_assert(sizeof(struct misc_virtual_ab_message) == 64,
+              "struct misc_virtual_ab_message has wrong size");
+static_assert(sizeof(struct misc_memtag_message) == 64,
+              "struct misc_memtag_message has wrong size");
+static_assert(sizeof(struct misc_kcmdline_message) == 64,
+              "struct misc_kcmdline_message has wrong size");
+static_assert(sizeof(struct misc_control_message) == 64,
+              "struct misc_control_message has wrong size");

include/android_bootloader_message.h struct bootloader_message { · line 164

Quoted 10 lines
include/android_bootloader_message.h +10 -0
+#endif
+#endif /* __UBOOT__ */
+
+// This struct is not meant to be used directly, rather, it is to make
+// computation of offsets easier. New fields must be added to the end.
+struct misc_system_space_layout {
+  struct misc_virtual_ab_message virtual_ab_message;
+  struct misc_memtag_message memtag_message;
+  struct misc_kcmdline_message kcmdline_message;
+  struct misc_control_message control_message;

Same here, original file does not have "struct" prefix

include/android_bootloader_message.h struct bootloader_message { · line 176

Quoted 12 lines
include/android_bootloader_message.h +9 -0
+} __attribute__((packed));
+
+#ifndef __UBOOT__
+#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
+static_assert(sizeof(struct misc_system_space_layout) % 64 == 0,
+              "prefer to extend by 64 byte chunks, for consistency");
+#endif
+#endif /* __UBOOT__ */
+
 /**
  * We must be cautious when changing the bootloader_message struct size,
  * because A/B-specific fields may end up with different offsets.

Side note: the ifndef is now #ifndef _BOOTLOADER_MESSAGE_CORE_H in AOSP. Should that be changed here as well?

Signature · 1 line
Mattijs Korpershoek <[email protected]>
#7 lore raw

PATCH 3/3 bootstd: android: act on the misc memtag and kcmdline messages

In reply to Igor Opaniuk

Android changes some kernel command line options through messages in the misc system space, which only the bootloader can act on:

- mtectrl writes misc_memtag_message to turn Arm MTE on or off. The bootloader passes arm64.nomte and kasan=on/off accordingly and clears the one-shot bits [1].
- kcmdlinectrl writes misc_kcmdline_message to force the Rust or the C binder driver, passed as binder.impl= [2].

Read both messages and apply them to the bootflow command line. BOOTMETH_ANDROID_MEMTAG_DEFAULT sets the MTE default when nothing was requested. A missing message is normal on a device that never used these features.

[1] https://source.android.com/docs/security/test/memory-safety/bootloader-support
[2] https://android.googlesource.com/platform/system/extras/+/refs/tags/android-17.0.0_r1/kcmdlinectrl/kcmdlinectrl.cc

---
 MAINTAINERS             |   2 +
 boot/Kconfig            |  11 ++++
 boot/Makefile           |   2 +-
 boot/android_misc.c     | 145 ++++++++++++++++++++++++++++++++++++++++++++++++
 boot/bootmeth_android.c |  84 ++++++++++++++++++++++++++++
 include/android_misc.h  |  56 +++++++++++++++++++
 6 files changed, 299 insertions(+), 1 deletion(-)
MAINTAINERS +2 -0
index 76a06fbf7ab..338c29fc841 100644
@@ -858,9 +858,11 @@ M:	Mattijs Korpershoek <[email protected]>
 R:	Guillaume La Roque <[email protected]>
 S:	Maintained
 T:	git https://git.u-boot-project.org/u-boot/custodians/u-boot-dfu.git
+F:	boot/android_misc.c
 F:	boot/bootmeth_android.c
 F:	boot/bootmeth_android.h
 F:	doc/develop/bootstd/android.rst
+F:	include/android_misc.h
 
 BTRFS
 M:	Marek Behún <[email protected]>
boot/Kconfig +11 -0
index 8fcdc36c931..50cdacfca98 100644
@@ -608,6 +608,17 @@ config BOOTMETH_ANDROID
 	  Note that only MMC bootdevs are supported at present. This is caused
 	  by AVB being limited to MMC devices only.
 
+config BOOTMETH_ANDROID_MEMTAG_DEFAULT
+	bool "Enable Arm MTE by default on Android"
+	depends on BOOTMETH_ANDROID && ARM64
+	help
+	  Whether the kernel is booted with the Arm Memory Tagging Extension
+	  enabled when the OS has not asked for a specific mode in the misc
+	  partition (misc_memtag_message, written by mtectrl). Select this on
+	  MTE capable devices that ship with MTE on; the OS can still turn it
+	  off with MISC_MEMTAG_MODE_MEMTAG_OFF. Without it the bootloader
+	  passes arm64.nomte unless the OS asked for MTE.
+
 config BOOTMETH_CROS
 	bool "Bootdev support for Chromium OS"
 	depends on X86 || ARM || SANDBOX
boot/Makefile +1 -1
index f2cbda176a0..f19bd11b132 100644
@@ -73,7 +73,7 @@ obj-$(CONFIG_$(PHASE_)BOOTMETH_VBE_SIMPLE) += vbe_simple.o vbe_common.o
 obj-$(CONFIG_$(PHASE_)BOOTMETH_VBE_SIMPLE_FW) += vbe_simple_fw.o
 obj-$(CONFIG_$(PHASE_)BOOTMETH_VBE_SIMPLE_OS) += vbe_simple_os.o
 
-obj-$(CONFIG_$(PHASE_)BOOTMETH_ANDROID) += bootmeth_android.o
+obj-$(CONFIG_$(PHASE_)BOOTMETH_ANDROID) += bootmeth_android.o android_misc.o
 
 obj-$(CONFIG_$(PHASE_)IMAGEMAP) += imagemap.o
 
boot/android_misc.c new file +145 -0

145 lines not on this page · raw message

boot/bootmeth_android.c +84 -0

102 lines not on this page · raw message

include/android_misc.h new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.53.0
#8 lore raw

Re: [PATCH 3/3] bootstd: android: act on the misc memtag and kcmdline messages

On Wed, 11 Nov 2026 10:13:34 +0100, Igor Opaniuk <[email protected]> wrote:

Hi,

boot/bootmeth_android.c static int android_read_slot_from_bcb(struct bootflow *bflow, bool decrement) · line 227

Quoted 13 lines
boot/bootmeth_android.c +0 -0
index 3d1e64aa2b41..9f5563751a73 100644
@@ -198,6 +199,88 @@ static int android_read_slot_from_bcb(struct bootflow *bflow, bool decrement)

[ ... skip 23 lines ... ]

boot/bootmeth_android.c +6 -0
+{
+	u32 mode;
+	u64 flags;
+	int ret;
+
+	ret = android_misc_read_memtag(desc, misc, &mode);

Shouldn't arm64.nomte be set when this function returns an error (e.g. -ENOENT, no message written yet) and MEMTAG_DEFAULT is not set? Today nothing is added in that case, so MTE stays enabled.

boot/bootmeth_android.c static int android_read_bootflow(struct udevice *dev, struct bootflow *bflow) · line 398

Quoted 6 lines
boot/bootmeth_android.c +1 -0
@@ -311,6 +394,7 @@ static int android_read_bootflow(struct udevice *dev, struct bootflow *bflow)
 	 */
 	configure_serialno(bflow);
 	configure_bootloader_version(bflow);
+	configure_misc_messages(bflow, desc, &misc);
 

If we run a bootflow command (scan, list) without booting, the MTE ONCE bits are already cleared and the next boot loses the request. Not sure it's the right place for this call, could the clear be done in the boot path instead?

Signature · 1 line
Guillaume La Roque <[email protected]>
#9 lore raw

Re: [PATCH 3/3] bootstd: android: act on the misc memtag and kcmdline messages

In reply to Igor Opaniuk

Hi Igor,

Thank you for the patch.

Quoted 17 lines

Android changes some kernel command line options through messages in the misc system space, which only the bootloader can act on:

- mtectrl writes misc_memtag_message to turn Arm MTE on or off. The bootloader passes arm64.nomte and kasan=on/off accordingly and clears the one-shot bits [1].
- kcmdlinectrl writes misc_kcmdline_message to force the Rust or the C binder driver, passed as binder.impl= [2].

Read both messages and apply them to the bootflow command line. BOOTMETH_ANDROID_MEMTAG_DEFAULT sets the MTE default when nothing was requested. A missing message is normal on a device that never used these features.

[1] https://source.android.com/docs/security/test/memory-safety/bootloader-support
[2] https://android.googlesource.com/platform/system/extras/+/refs/tags/android-17.0.0_r1/kcmdlinectrl/kcmdlinectrl.cc

Quoted 6 lines
---
 MAINTAINERS             |   2 +
 boot/Kconfig            |  11 ++++
 boot/Makefile           |   2 +-
 boot/android_misc.c     | 145 ++++++++++++++++++++++++++++++++++++++++++++++++

We already have something that reads the misc partition (cmd/bcb.c)

bcb is also very useful for debugging. Why can't we extend the bcb command to also handle the new regions (such as misc_memtag_message and misc_kcmdline_message) ?

Signature · 1 line
Mattijs Korpershoek <[email protected]>