blob: e29930631035d552e28c4dfae8d6fc91d098aed7 [file] [log] [blame]
Tom Rini421a5d02018-06-19 11:21:44 -04001// SPDX-License-Identifier: MIT
Igor Opaniuk8b23ae22018-06-03 21:56:36 +03002/*
3 * Copyright (C) 2016 The Android Open Source Project
Igor Opaniuk8b23ae22018-06-03 21:56:36 +03004 */
5
6#include "avb_chain_partition_descriptor.h"
7#include "avb_util.h"
8
9bool avb_chain_partition_descriptor_validate_and_byteswap(
10 const AvbChainPartitionDescriptor* src, AvbChainPartitionDescriptor* dest) {
11 uint64_t expected_size;
12
13 avb_memcpy(dest, src, sizeof(AvbChainPartitionDescriptor));
14
15 if (!avb_descriptor_validate_and_byteswap((const AvbDescriptor*)src,
16 (AvbDescriptor*)dest))
17 return false;
18
19 if (dest->parent_descriptor.tag != AVB_DESCRIPTOR_TAG_CHAIN_PARTITION) {
20 avb_error("Invalid tag for chain partition descriptor.\n");
21 return false;
22 }
23
24 dest->rollback_index_location = avb_be32toh(dest->rollback_index_location);
25 dest->partition_name_len = avb_be32toh(dest->partition_name_len);
26 dest->public_key_len = avb_be32toh(dest->public_key_len);
27
28 if (dest->rollback_index_location < 1) {
29 avb_error("Invalid rollback index location value.\n");
30 return false;
31 }
32
33 /* Check that partition_name and public_key are fully contained. */
34 expected_size = sizeof(AvbChainPartitionDescriptor) - sizeof(AvbDescriptor);
35 if (!avb_safe_add_to(&expected_size, dest->partition_name_len) ||
36 !avb_safe_add_to(&expected_size, dest->public_key_len)) {
37 avb_error("Overflow while adding up sizes.\n");
38 return false;
39 }
40 if (expected_size > dest->parent_descriptor.num_bytes_following) {
41 avb_error("Descriptor payload size overflow.\n");
42 return false;
43 }
44 return true;
45}