@@ -69,6 +69,34 @@ 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_following; // size of descriptor body (excludes this header); should always be a multiple of 8.
84+ } __attribute__((packed));
85+
86+ // https://android.googlesource.com/platform/external/avb/+/refs/heads/android11-release/libavb/avb_hash_descriptor.h
87+ // for AvbDescriptor.tag == AVB_DESCRIPTOR_TAG_HASH
88+ struct AvbHashDescriptor {
89+ AvbDescriptor header;
90+ uint64_t image_size;
91+ uint8_t hash_algorithm[32 ];
92+ uint32_t partition_name_len;
93+ uint32_t salt_len;
94+ uint32_t digest_len;
95+ uint32_t flags;
96+ uint8_t reserved[60 ];
97+ // followed by: partition_name, salt, digest (variable length)
98+ } __attribute__((packed));
99+
72100// https://android.googlesource.com/platform/external/avb/+/refs/heads/android11-release/libavb/avb_vbmeta_image.h
73101struct AvbVBMetaImageHeader {
74102 uint8_t magic[AVB_MAGIC_LEN];
@@ -92,8 +120,48 @@ struct AvbVBMetaImageHeader {
92120 uint32_t rollback_index_location;
93121 uint8_t release_string[AVB_RELEASE_STRING_SIZE];
94122 uint8_t reserved[80 ];
123+
124+ struct AvbDescriptorRange descriptors ();
95125} __attribute__((packed));
96126
127+ struct AvbDescriptorIterator {
128+ AvbDescriptor *ptr;
129+ AvbDescriptor &operator *() const { return *ptr; }
130+ AvbDescriptor *operator ->() const { return ptr; }
131+ bool operator !=(const AvbDescriptorIterator &o) const {
132+ if (ptr == nullptr ) return false ;
133+ return ptr != o.ptr ;
134+ }
135+ AvbDescriptorIterator &operator ++() {
136+ if (ptr->num_bytes_following % 8 != 0 ) {
137+ // This is an error, a malformed image.
138+ // https://android.googlesource.com/platform/external/avb/+/refs/heads/android11-release/libavb/avb_descriptor.h#60
139+ // > For padding, |num_bytes_following| is always a multiple of 8.
140+ // we can't signal an error easily, but we can stop the iteration.
141+ ptr = nullptr ;
142+ return *this ;
143+ }
144+ ptr = reinterpret_cast <AvbDescriptor *>(
145+ reinterpret_cast <uint8_t *>(ptr) + sizeof (AvbDescriptor) + __builtin_bswap64 (ptr->num_bytes_following ));
146+ return *this ;
147+ }
148+ };
149+
150+ struct AvbDescriptorRange {
151+ AvbDescriptor *first, *last;
152+ AvbDescriptorIterator begin () const { return {first}; }
153+ AvbDescriptorIterator end () const { return {last}; }
154+ };
155+
156+ inline AvbDescriptorRange AvbVBMetaImageHeader::descriptors () {
157+ auto *base = reinterpret_cast <const uint8_t *>(this ) + sizeof (AvbVBMetaImageHeader);
158+ base += __builtin_bswap64 (authentication_data_block_size);
159+ base += __builtin_bswap64 (descriptors_offset);
160+ auto *first = reinterpret_cast <AvbDescriptor *>(const_cast <uint8_t *>(base));
161+ auto *last = reinterpret_cast <AvbDescriptor *>(const_cast <uint8_t *>(base) + __builtin_bswap64 (descriptors_size));
162+ return {first, last};
163+ }
164+
97165/* ********************
98166 * Boot Image Headers
99167 *********************/
0 commit comments