Skip to content

Commit fe7c726

Browse files
kousuclaude
andcommitted
Update AVB hash header image_size to match unpacked original_image_size.
Fixes #8389 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 9d0b529 commit fe7c726

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

native/src/boot/bootimg.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,10 +1019,22 @@ void repack(Utf8CStr src_img, Utf8CStr out_img, bool skip_comp) {
10191019
memcpy(footer, boot.avb_footer, sizeof(AvbFooter));
10201020
footer->original_image_size = __builtin_bswap64(aosp_img_size);
10211021
footer->vbmeta_offset = __builtin_bswap64(off.vbmeta);
1022+
1023+
auto vbmeta = reinterpret_cast<AvbVBMetaImageHeader*>(out.data() + off.vbmeta);
1024+
10221025
if (check_env("PATCHVBMETAFLAG")) {
1023-
auto vbmeta = reinterpret_cast<AvbVBMetaImageHeader*>(out.data() + off.vbmeta);
10241026
vbmeta->flags = __builtin_bswap32(3);
10251027
}
1028+
1029+
// Sync hash descriptor image_size with the new AOSP portion size.
1030+
// Without this, some bootloaders (e.g. Motorola) reject images.
1031+
for (auto &desc : vbmeta->descriptors()) {
1032+
if (__builtin_bswap64(desc.tag) != AVB_DESCRIPTOR_TAG_HASH)
1033+
continue;
1034+
auto &hd = reinterpret_cast<AvbHashDescriptor &>(desc);
1035+
hd.image_size = __builtin_bswap64(aosp_img_size);
1036+
break;
1037+
}
10261038
}
10271039

10281040
if (boot.flags[DHTB_FLAG]) {

native/src/boot/bootimg.hpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,33 @@ struct AvbFooter {
6969
uint8_t reserved[28];
7070
} __attribute__((packed));
7171

72+
// https://android.googlesource.com/platform/external/avb/+/refs/heads/android11-release/libavb/avb_descriptor.h
73+
enum AvbDescriptorTag : uint64_t {
74+
AVB_DESCRIPTOR_TAG_PROPERTY = 0,
75+
AVB_DESCRIPTOR_TAG_HASHTREE = 1,
76+
AVB_DESCRIPTOR_TAG_HASH = 2,
77+
AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE = 3,
78+
AVB_DESCRIPTOR_TAG_CHAIN_PARTITION = 4,
79+
};
80+
81+
struct AvbDescriptor {
82+
uint64_t tag;
83+
uint64_t num_bytes; // size of descriptor body (excludes this header)
84+
} __attribute__((packed));
85+
86+
// https://android.googlesource.com/platform/external/avb/+/refs/heads/android11-release/libavb/avb_hash_descriptor.h
87+
struct AvbHashDescriptor {
88+
AvbDescriptor header; // tag == 2
89+
uint64_t image_size;
90+
uint8_t hash_algorithm[32];
91+
uint32_t partition_name_len;
92+
uint32_t salt_len;
93+
uint32_t digest_len;
94+
uint32_t flags;
95+
uint8_t reserved[60];
96+
// followed by: partition_name, salt, digest (variable length)
97+
} __attribute__((packed));
98+
7299
// https://android.googlesource.com/platform/external/avb/+/refs/heads/android11-release/libavb/avb_vbmeta_image.h
73100
struct AvbVBMetaImageHeader {
74101
uint8_t magic[AVB_MAGIC_LEN];
@@ -92,8 +119,37 @@ struct AvbVBMetaImageHeader {
92119
uint32_t rollback_index_location;
93120
uint8_t release_string[AVB_RELEASE_STRING_SIZE];
94121
uint8_t reserved[80];
122+
123+
struct AvbDescriptorRange descriptors() const;
95124
} __attribute__((packed));
96125

126+
struct AvbDescriptorIterator {
127+
AvbDescriptor *ptr;
128+
AvbDescriptor &operator*() const { return *ptr; }
129+
AvbDescriptor *operator->() const { return ptr; }
130+
bool operator!=(const AvbDescriptorIterator &o) const { return ptr != o.ptr; }
131+
AvbDescriptorIterator &operator++() {
132+
ptr = reinterpret_cast<AvbDescriptor *>(
133+
reinterpret_cast<uint8_t *>(ptr) + sizeof(AvbDescriptor) + __builtin_bswap64(ptr->num_bytes));
134+
return *this;
135+
}
136+
};
137+
138+
struct AvbDescriptorRange {
139+
AvbDescriptor *first, *last;
140+
AvbDescriptorIterator begin() const { return {first}; }
141+
AvbDescriptorIterator end() const { return {last}; }
142+
};
143+
144+
inline AvbDescriptorRange AvbVBMetaImageHeader::descriptors() const {
145+
auto *base = reinterpret_cast<const uint8_t *>(this) + sizeof(AvbVBMetaImageHeader);
146+
base += __builtin_bswap64(authentication_data_block_size);
147+
base += __builtin_bswap64(descriptors_offset);
148+
auto *first = reinterpret_cast<AvbDescriptor *>(const_cast<uint8_t *>(base));
149+
auto *last = reinterpret_cast<AvbDescriptor *>(const_cast<uint8_t *>(base) + __builtin_bswap64(descriptors_size));
150+
return {first, last};
151+
}
152+
97153
/*********************
98154
* Boot Image Headers
99155
*********************/

0 commit comments

Comments
 (0)