Skip to contentSkip to conversation

android_ab: Fix backup offset calculation

PATCH Superseded Joshua Watt 3 participants 6 messages Aug 7, 2024 – Sep 5, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 android_ab: Fix backup offset calculation

The backup offset is in bytes, but was incorrectly be interpreted as blocks, leading to it being written to the wrong location. Fix the calculation and clarify that ANDROID_AB_BACKUP_OFFSET is in bytes and must be a multiple of the block size.

---
 boot/android_ab.c | 4 ++--
 common/Kconfig    | 3 ++-
 2 files changed, 4 insertions(+), 3 deletions(-)
boot/android_ab.c +2 -2
index 143f373aae..b253200161 100644
@@ -139,8 +139,8 @@ static int ab_control_store(struct blk_desc *dev_desc,
 {
 	ulong abc_offset, abc_blocks, ret;
 
-	abc_offset = offset +
-		     offsetof(struct bootloader_message_ab, slot_suffix) /
+	abc_offset = (offset +
+		      offsetof(struct bootloader_message_ab, slot_suffix)) /
 		     part_info->blksz;
 	abc_blocks = DIV_ROUND_UP(sizeof(struct bootloader_control),
 				  part_info->blksz);
common/Kconfig +2 -1
index 83c81edac2..e1b8557e0c 100644
@@ -986,7 +986,8 @@ config ANDROID_AB_BACKUP_OFFSET
 	help
 	  If non-zero, a backup bootloader message starting at this offset in
 	  the partition will tried in the event that the primary one (starting
-	  at offset 0) fails its checksum.
+	  at offset 0) fails its checksum. The offset is in bytes and must be
+	  multiple of the block size.
 
 endmenu
 
Signature · 1 line
2.45.2
#1 lore raw

Hi Joshua,

Thank you for the patch.

Quoted 4 lines · Joshua Watt

The backup offset is in bytes, but was incorrectly be interpreted as blocks, leading to it being written to the wrong location. Fix the calculation and clarify that ANDROID_AB_BACKUP_OFFSET is in bytes and must be a multiple of the block size.

Nice finding !

Quoted 12 lines
---
 boot/android_ab.c | 4 ++--
 common/Kconfig    | 3 ++-
 2 files changed, 4 insertions(+), 3 deletions(-)
boot/android_ab.c +0 -0
index 143f373aae..b253200161 100644

boot/android_ab.c static int ab_control_store(struct blk_desc *dev_desc, · line 141

Quoted 3 lines
boot/android_ab.c +0 -0
@@ -139,8 +139,8 @@ static int ab_control_store(struct blk_desc *dev_desc,
 {
 	ulong abc_offset, abc_blocks, ret;
 

Can we add a safeguard in here to make sure that offset is always blksz aligned ?

We could do something similar to ab_control_create_from_disk():

	if (offset % part_info->blksz) {
		log_err("ANDROID: offset not block aligned.\n");
		return -EINVAL;
	}

boot/android_ab.c static int ab_control_store(struct blk_desc *dev_desc, · line 146

Quoted 7 lines
boot/android_ab.c +2 -2
-	abc_offset = offset +
-		     offsetof(struct bootloader_message_ab, slot_suffix) /
+	abc_offset = (offset +
+		      offsetof(struct bootloader_message_ab, slot_suffix)) /
 		     part_info->blksz;
 	abc_blocks = DIV_ROUND_UP(sizeof(struct bootloader_control),
 				  part_info->blksz);

I've also noticed that ab_control_store()'s documentation does not document the @param[in] offset.

Could we add that as well (either in a seperate patch or in this one) ?

Quoted 16 lines
common/Kconfig +2 -1
index 83c81edac2..e1b8557e0c 100644
@@ -986,7 +986,8 @@ config ANDROID_AB_BACKUP_OFFSET
 	help
 	  If non-zero, a backup bootloader message starting at this offset in
 	  the partition will tried in the event that the primary one (starting
-	  at offset 0) fails its checksum.
+	  at offset 0) fails its checksum. The offset is in bytes and must be
+	  multiple of the block size.
 
 endmenu
 
Signature · 1 line
2.45.2
#2 lore raw
In reply to Joshua Watt
Quoted 8 lines · Joshua Watt

The backup offset is in bytes, but was incorrectly be interpreted as blocks, leading to it being written to the wrong location. Fix the calculation and clarify that ANDROID_AB_BACKUP_OFFSET is in bytes and must be a multiple of the block size.

---

In addition to what Mattijs said, I think it makes sense to add "Fixes:" tag for this patch.

Quoted 36 lines
 boot/android_ab.c | 4 ++--
 common/Kconfig    | 3 ++-
 2 files changed, 4 insertions(+), 3 deletions(-)
boot/android_ab.c +2 -2
index 143f373aae..b253200161 100644
@@ -139,8 +139,8 @@ static int ab_control_store(struct blk_desc *dev_desc,
 {
        ulong abc_offset, abc_blocks, ret;
 
-       abc_offset = offset +
-                    offsetof(struct bootloader_message_ab, slot_suffix) /
+       abc_offset = (offset +
+                     offsetof(struct bootloader_message_ab, slot_suffix)) /
                     part_info->blksz;
        abc_blocks = DIV_ROUND_UP(sizeof(struct bootloader_control),
                                  part_info->blksz);
common/Kconfig +2 -2
index 83c81edac2..e1b8557e0c 100644
@@ -986,7 +986,8 @@ config ANDROID_AB_BACKUP_OFFSET
        help
          If non-zero, a backup bootloader message starting at this offset in
          the partition will tried in the event that the primary one (starting
-         at offset 0) fails its checksum.
+         at offset 0) fails its checksum. The offset is in bytes and must be
+         multiple of the block size.
 
 endmenu
 
--

2.45.2

#3 lore raw

PATCH v2 1/1 android_ab: Fixes: Fix backup offset calculation

In reply to Joshua Watt

The backup offset is in bytes, but was incorrectly be interpreted as blocks, leading to it being written to the wrong location. Fix the calculation, clarify that ANDROID_AB_BACKUP_OFFSET is in bytes and must be a multiple of the block size, and add a runtime check to validate the offset.

---
 boot/android_ab.c | 9 +++++++--
 common/Kconfig    | 3 ++-
 2 files changed, 9 insertions(+), 3 deletions(-)
boot/android_ab.c +7 -2
index 143f373aae..1196a189ed 100644
@@ -139,8 +139,13 @@ static int ab_control_store(struct blk_desc *dev_desc,
 {
 	ulong abc_offset, abc_blocks, ret;
 
-	abc_offset = offset +
-		     offsetof(struct bootloader_message_ab, slot_suffix) /
+	if (offset % part_info->blksz) {
+		log_err("ANDROID: offset not block aligned\n");
+		return -EINVAL;
+	}
+
+	abc_offset = (offset +
+		      offsetof(struct bootloader_message_ab, slot_suffix)) /
 		     part_info->blksz;
 	abc_blocks = DIV_ROUND_UP(sizeof(struct bootloader_control),
 				  part_info->blksz);
common/Kconfig +2 -1
index 83c81edac2..e1b8557e0c 100644
@@ -986,7 +986,8 @@ config ANDROID_AB_BACKUP_OFFSET
 	help
 	  If non-zero, a backup bootloader message starting at this offset in
 	  the partition will tried in the event that the primary one (starting
-	  at offset 0) fails its checksum.
+	  at offset 0) fails its checksum. The offset is in bytes and must be
+	  multiple of the block size.
 
 endmenu
 
Signature · 1 line
2.45.2
Reviewed-by on v2 1/1
#4 lore raw

Re: [PATCH v2] android_ab: Fixes: Fix backup offset calculation

Hi Joshua,

Thank you for the patch.

Quoted 7 lines · Joshua Watt

The backup offset is in bytes, but was incorrectly be interpreted as blocks, leading to it being written to the wrong location. Fix the calculation, clarify that ANDROID_AB_BACKUP_OFFSET is in bytes and must be a multiple of the block size, and add a runtime check to validate the offset.

As Igor suggested, I will add:

Fixes 3430f24bc69d ("android_ab: Try backup booloader_message")

When applying.

Quoted 41 lines
---
 boot/android_ab.c | 9 +++++++--
 common/Kconfig    | 3 ++-
 2 files changed, 9 insertions(+), 3 deletions(-)
boot/android_ab.c +7 -2
index 143f373aae..1196a189ed 100644
@@ -139,8 +139,13 @@ static int ab_control_store(struct blk_desc *dev_desc,
 {
 	ulong abc_offset, abc_blocks, ret;
 
-	abc_offset = offset +
-		     offsetof(struct bootloader_message_ab, slot_suffix) /
+	if (offset % part_info->blksz) {
+		log_err("ANDROID: offset not block aligned\n");
+		return -EINVAL;
+	}
+
+	abc_offset = (offset +
+		      offsetof(struct bootloader_message_ab, slot_suffix)) /
 		     part_info->blksz;
 	abc_blocks = DIV_ROUND_UP(sizeof(struct bootloader_control),
 				  part_info->blksz);
common/Kconfig +2 -1
index 83c81edac2..e1b8557e0c 100644
@@ -986,7 +986,8 @@ config ANDROID_AB_BACKUP_OFFSET
 	help
 	  If non-zero, a backup bootloader message starting at this offset in
 	  the partition will tried in the event that the primary one (starting
-	  at offset 0) fails its checksum.
+	  at offset 0) fails its checksum. The offset is in bytes and must be
+	  multiple of the block size.
 
 endmenu
 
Signature · 1 line
2.45.2
#5 lore raw

Re: [PATCH v2] android_ab: Fixes: Fix backup offset calculation

In reply to Joshua Watt

Hi,

Quoted 8 lines · Joshua Watt

The backup offset is in bytes, but was incorrectly be interpreted as blocks, leading to it being written to the wrong location. Fix the calculation, clarify that ANDROID_AB_BACKUP_OFFSET is in bytes and must be a multiple of the block size, and add a runtime check to validate the offset.

[...]

Thanks, Applied to https://source.denx.de/u-boot/custodians/u-boot-dfu (u-boot-dfu)

[1/1] android_ab: Fixes: Fix backup offset calculation

      https://source.denx.de/u-boot/custodians/u-boot-dfu/-/commit/cc2f60c13f6aaf1bd277cf10c8b375c2e4a695b7

--
Mattijs