1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "fs_avb/types.h"
18
19 namespace android {
20 namespace fs_mgr {
21
22 // Helper functions to print enum class VBMetaVerifyResult.
VBMetaVerifyResultToString(VBMetaVerifyResult result)23 const char* VBMetaVerifyResultToString(VBMetaVerifyResult result) {
24 // clang-format off
25 static const char* const name[] = {
26 "ResultSuccess",
27 "ResultError",
28 "ResultErrorVerification",
29 "ResultUnknown",
30 };
31 // clang-format on
32
33 uint32_t index = static_cast<uint32_t>(result);
34 uint32_t unknown_index = sizeof(name) / sizeof(char*) - 1;
35 if (index >= unknown_index) {
36 index = unknown_index;
37 }
38
39 return name[index];
40 }
41
operator <<(std::ostream & os,VBMetaVerifyResult result)42 std::ostream& operator<<(std::ostream& os, VBMetaVerifyResult result) {
43 os << VBMetaVerifyResultToString(result);
44 return os;
45 }
46
47 // Helper functions to dump enum class AvbHandleStatus.
AvbHandleStatusToString(AvbHandleStatus status)48 const char* AvbHandleStatusToString(AvbHandleStatus status) {
49 // clang-format off
50 static const char* const name[] = {
51 "Success",
52 "Uninitialized",
53 "HashtreeDisabled",
54 "VerificationDisabled",
55 "VerificationError",
56 "Unknown",
57 };
58 // clang-format on
59
60 uint32_t index = static_cast<uint32_t>(status);
61 uint32_t unknown_index = sizeof(name) / sizeof(char*) - 1;
62 if (index >= unknown_index) {
63 index = unknown_index;
64 }
65
66 return name[index];
67 }
68
operator <<(std::ostream & os,AvbHandleStatus status)69 std::ostream& operator<<(std::ostream& os, AvbHandleStatus status) {
70 os << AvbHandleStatusToString(status);
71 return os;
72 }
73
74 // class VBMetaData
75 // ----------------
GetVBMetaHeader(bool update_vbmeta_size)76 std::unique_ptr<AvbVBMetaImageHeader> VBMetaData::GetVBMetaHeader(bool update_vbmeta_size) {
77 auto vbmeta_header = std::make_unique<AvbVBMetaImageHeader>();
78
79 if (!vbmeta_header) return nullptr;
80
81 /* Byteswap the header. */
82 avb_vbmeta_image_header_to_host_byte_order((AvbVBMetaImageHeader*)vbmeta_ptr_.get(),
83 vbmeta_header.get());
84 if (update_vbmeta_size) {
85 vbmeta_size_ = sizeof(AvbVBMetaImageHeader) +
86 vbmeta_header->authentication_data_block_size +
87 vbmeta_header->auxiliary_data_block_size;
88 }
89
90 return vbmeta_header;
91 }
92
93 } // namespace fs_mgr
94 } // namespace android
95