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_HASH_DESCRIPTOR_H_
11d8f9d2afSIgor Opaniuk #define AVB_HASH_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 hash descriptors.
20d8f9d2afSIgor Opaniuk  *
21d8f9d2afSIgor Opaniuk  * AVB_HASH_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_HASH_DESCRIPTOR_FLAGS_DO_NOT_USE_AB = (1 << 0),
27d8f9d2afSIgor Opaniuk } AvbHashDescriptorFlags;
28d8f9d2afSIgor Opaniuk 
29d8f9d2afSIgor Opaniuk /* A descriptor containing information about hash for an image.
30d8f9d2afSIgor Opaniuk  *
31d8f9d2afSIgor Opaniuk  * This descriptor is typically used for boot partitions to verify the
32d8f9d2afSIgor Opaniuk  * entire kernel+initramfs image before executing it.
33d8f9d2afSIgor Opaniuk  *
34d8f9d2afSIgor Opaniuk  * Following this struct are |partition_name_len| bytes of the
35d8f9d2afSIgor Opaniuk  * partition name (UTF-8 encoded), |salt_len| bytes of salt, and then
36d8f9d2afSIgor Opaniuk  * |digest_len| bytes of the digest.
37d8f9d2afSIgor Opaniuk  *
38d8f9d2afSIgor Opaniuk  * The |reserved| field is for future expansion and must be set to NUL
39d8f9d2afSIgor Opaniuk  * bytes.
40d8f9d2afSIgor Opaniuk  *
41d8f9d2afSIgor Opaniuk  * Changes in v1.1:
42d8f9d2afSIgor Opaniuk  *   - flags field is added which supports AVB_HASH_DESCRIPTOR_FLAGS_USE_AB
43d8f9d2afSIgor Opaniuk  *   - digest_len may be zero, which indicates the use of a persistent digest
44d8f9d2afSIgor Opaniuk  */
45d8f9d2afSIgor Opaniuk typedef struct AvbHashDescriptor {
46d8f9d2afSIgor Opaniuk   AvbDescriptor parent_descriptor;
47d8f9d2afSIgor Opaniuk   uint64_t image_size;
48d8f9d2afSIgor Opaniuk   uint8_t hash_algorithm[32];
49d8f9d2afSIgor Opaniuk   uint32_t partition_name_len;
50d8f9d2afSIgor Opaniuk   uint32_t salt_len;
51d8f9d2afSIgor Opaniuk   uint32_t digest_len;
52d8f9d2afSIgor Opaniuk   uint32_t flags;
53d8f9d2afSIgor Opaniuk   uint8_t reserved[60];
54d8f9d2afSIgor Opaniuk } AVB_ATTR_PACKED AvbHashDescriptor;
55d8f9d2afSIgor Opaniuk 
56d8f9d2afSIgor Opaniuk /* Copies |src| to |dest| and validates, byte-swapping fields in the
57d8f9d2afSIgor Opaniuk  * process if needed. Returns true if valid, false if invalid.
58d8f9d2afSIgor Opaniuk  *
59d8f9d2afSIgor Opaniuk  * Data following the struct is not validated nor copied.
60d8f9d2afSIgor Opaniuk  */
61d8f9d2afSIgor Opaniuk bool avb_hash_descriptor_validate_and_byteswap(const AvbHashDescriptor* src,
62d8f9d2afSIgor Opaniuk                                                AvbHashDescriptor* dest)
63d8f9d2afSIgor Opaniuk     AVB_ATTR_WARN_UNUSED_RESULT;
64d8f9d2afSIgor Opaniuk 
65d8f9d2afSIgor Opaniuk #ifdef __cplusplus
66d8f9d2afSIgor Opaniuk }
67d8f9d2afSIgor Opaniuk #endif
68d8f9d2afSIgor Opaniuk 
69d8f9d2afSIgor Opaniuk #endif /* AVB_HASH_DESCRIPTOR_H_ */
70