xref: /openbmc/u-boot/lib/libavb/avb_hashtree_descriptor.h (revision 897a1d947e7e50cf03113dcbe505ffb63dce45a9)
1*897a1d94STom Rini /* SPDX-License-Identifier: MIT */
2d8f9d2afSIgor Opaniuk /*
3d8f9d2afSIgor Opaniuk  * Copyright (C) 2016 The Android Open Source Project
4d8f9d2afSIgor Opaniuk  */
5d8f9d2afSIgor Opaniuk 
6d8f9d2afSIgor Opaniuk #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
7d8f9d2afSIgor Opaniuk #error "Never include this file directly, include libavb.h instead."
8d8f9d2afSIgor Opaniuk #endif
9d8f9d2afSIgor Opaniuk 
10d8f9d2afSIgor Opaniuk #ifndef AVB_HASHTREE_DESCRIPTOR_H_
11d8f9d2afSIgor Opaniuk #define AVB_HASHTREE_DESCRIPTOR_H_
12d8f9d2afSIgor Opaniuk 
13d8f9d2afSIgor Opaniuk #include "avb_descriptor.h"
14d8f9d2afSIgor Opaniuk 
15d8f9d2afSIgor Opaniuk #ifdef __cplusplus
16d8f9d2afSIgor Opaniuk extern "C" {
17d8f9d2afSIgor Opaniuk #endif
18d8f9d2afSIgor Opaniuk 
19d8f9d2afSIgor Opaniuk /* Flags for hashtree descriptors.
20d8f9d2afSIgor Opaniuk  *
21d8f9d2afSIgor Opaniuk  * AVB_HASHTREE_DESCRIPTOR_FLAGS_DO_NOT_USE_AB: Do not apply the default A/B
22d8f9d2afSIgor Opaniuk  *   partition logic to this partition. This is intentionally a negative boolean
23d8f9d2afSIgor Opaniuk  *   because A/B should be both the default and most used in practice.
24d8f9d2afSIgor Opaniuk  */
25d8f9d2afSIgor Opaniuk typedef enum {
26d8f9d2afSIgor Opaniuk   AVB_HASHTREE_DESCRIPTOR_FLAGS_DO_NOT_USE_AB = (1 << 0),
27d8f9d2afSIgor Opaniuk } AvbHashtreeDescriptorFlags;
28d8f9d2afSIgor Opaniuk 
29d8f9d2afSIgor Opaniuk /* A descriptor containing information about a dm-verity hashtree.
30d8f9d2afSIgor Opaniuk  *
31d8f9d2afSIgor Opaniuk  * Hash-trees are used to verify large partitions typically containing
32d8f9d2afSIgor Opaniuk  * file systems. See
33d8f9d2afSIgor Opaniuk  * https://gitlab.com/cryptsetup/cryptsetup/wikis/DMVerity for more
34d8f9d2afSIgor Opaniuk  * information about dm-verity.
35d8f9d2afSIgor Opaniuk  *
36d8f9d2afSIgor Opaniuk  * Following this struct are |partition_name_len| bytes of the
37d8f9d2afSIgor Opaniuk  * partition name (UTF-8 encoded), |salt_len| bytes of salt, and then
38d8f9d2afSIgor Opaniuk  * |root_digest_len| bytes of the root digest.
39d8f9d2afSIgor Opaniuk  *
40d8f9d2afSIgor Opaniuk  * The |reserved| field is for future expansion and must be set to NUL
41d8f9d2afSIgor Opaniuk  * bytes.
42d8f9d2afSIgor Opaniuk  *
43d8f9d2afSIgor Opaniuk  * Changes in v1.1:
44d8f9d2afSIgor Opaniuk  *   - flags field is added which supports AVB_HASHTREE_DESCRIPTOR_FLAGS_USE_AB
45d8f9d2afSIgor Opaniuk  *   - digest_len may be zero, which indicates the use of a persistent digest
46d8f9d2afSIgor Opaniuk  */
47d8f9d2afSIgor Opaniuk typedef struct AvbHashtreeDescriptor {
48d8f9d2afSIgor Opaniuk   AvbDescriptor parent_descriptor;
49d8f9d2afSIgor Opaniuk   uint32_t dm_verity_version;
50d8f9d2afSIgor Opaniuk   uint64_t image_size;
51d8f9d2afSIgor Opaniuk   uint64_t tree_offset;
52d8f9d2afSIgor Opaniuk   uint64_t tree_size;
53d8f9d2afSIgor Opaniuk   uint32_t data_block_size;
54d8f9d2afSIgor Opaniuk   uint32_t hash_block_size;
55d8f9d2afSIgor Opaniuk   uint32_t fec_num_roots;
56d8f9d2afSIgor Opaniuk   uint64_t fec_offset;
57d8f9d2afSIgor Opaniuk   uint64_t fec_size;
58d8f9d2afSIgor Opaniuk   uint8_t hash_algorithm[32];
59d8f9d2afSIgor Opaniuk   uint32_t partition_name_len;
60d8f9d2afSIgor Opaniuk   uint32_t salt_len;
61d8f9d2afSIgor Opaniuk   uint32_t root_digest_len;
62d8f9d2afSIgor Opaniuk   uint32_t flags;
63d8f9d2afSIgor Opaniuk   uint8_t reserved[60];
64d8f9d2afSIgor Opaniuk } AVB_ATTR_PACKED AvbHashtreeDescriptor;
65d8f9d2afSIgor Opaniuk 
66d8f9d2afSIgor Opaniuk /* Copies |src| to |dest| and validates, byte-swapping fields in the
67d8f9d2afSIgor Opaniuk  * process if needed. Returns true if valid, false if invalid.
68d8f9d2afSIgor Opaniuk  *
69d8f9d2afSIgor Opaniuk  * Data following the struct is not validated nor copied.
70d8f9d2afSIgor Opaniuk  */
71d8f9d2afSIgor Opaniuk bool avb_hashtree_descriptor_validate_and_byteswap(
72d8f9d2afSIgor Opaniuk     const AvbHashtreeDescriptor* src,
73d8f9d2afSIgor Opaniuk     AvbHashtreeDescriptor* dest) AVB_ATTR_WARN_UNUSED_RESULT;
74d8f9d2afSIgor Opaniuk 
75d8f9d2afSIgor Opaniuk #ifdef __cplusplus
76d8f9d2afSIgor Opaniuk }
77d8f9d2afSIgor Opaniuk #endif
78d8f9d2afSIgor Opaniuk 
79d8f9d2afSIgor Opaniuk #endif /* AVB_HASHTREE_DESCRIPTOR_H_ */
80