android_ab: Fix backup offset calculation
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
@@ -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
@@ -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
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_OFFSETis in bytes and must be a multiple of the block size.
Nice finding !
Quoted 12 lines
Signed-off-by Joshua Watt <[email protected]>--- boot/android_ab.c | 4 ++-- common/Kconfig | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-)boot/android_ab.c +0
-0
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@@ -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. endmenuSignature · 1 line
2.45.2
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_OFFSETis in bytes and must be a multiple of the block size.Signed-off-by Joshua Watt <[email protected]>---
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@@ -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@@ -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
PATCH v2 1/1 android_ab: Fixes: Fix backup offset calculation
In reply to Joshua WattThe 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
@@ -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
@@ -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
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_OFFSETis in bytes and must be a multiple of the block size, and add a runtime check to validate the offset.Signed-off-by Joshua Watt <[email protected]>
As Igor suggested, I will add:
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@@ -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@@ -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. endmenuSignature · 1 line
2.45.2
Re: [PATCH v2] android_ab: Fixes: Fix backup offset calculation
In reply to Joshua WattHi,
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_OFFSETis 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