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 "reader.h"
18
19 #include <android-base/file.h>
20
21 using android::base::ErrnoError;
22 using android::base::Error;
23 using android::base::Result;
24
25 namespace android {
26 namespace fs_mgr {
27
LoadAndVerifySuperVBMetaHeader(const void * buffer,SuperVBMetaHeader * header)28 Result<void> LoadAndVerifySuperVBMetaHeader(const void* buffer, SuperVBMetaHeader* header) {
29 memcpy(header, buffer, sizeof(*header));
30
31 // Do basic validation of super vbmeta.
32 if (header->magic != SUPER_VBMETA_MAGIC) {
33 return Error() << "Super VBMeta has invalid magic value";
34 }
35
36 // Check that the version is compatible.
37 if (header->major_version != SUPER_VBMETA_MAJOR_VERSION ||
38 header->minor_version > SUPER_VBMETA_MINOR_VERSION) {
39 return Error() << "Super VBMeta has incompatible version";
40 }
41 return {};
42 }
43
LoadVBMetaDescriptors(const void * buffer,uint32_t size,std::vector<InternalVBMetaDescriptor> * descriptors)44 void LoadVBMetaDescriptors(const void* buffer, uint32_t size,
45 std::vector<InternalVBMetaDescriptor>* descriptors) {
46 for (int p = 0; p < size;) {
47 InternalVBMetaDescriptor descriptor;
48 memcpy(&descriptor, (char*)buffer + p, SUPER_VBMETA_DESCRIPTOR_SIZE);
49 p += SUPER_VBMETA_DESCRIPTOR_SIZE;
50
51 descriptor.vbmeta_name = std::string((char*)buffer + p, descriptor.vbmeta_name_length);
52 p += descriptor.vbmeta_name_length;
53
54 descriptors->emplace_back(std::move(descriptor));
55 }
56 }
57
ReadVBMetaTable(int fd,uint64_t offset,VBMetaTable * table)58 Result<void> ReadVBMetaTable(int fd, uint64_t offset, VBMetaTable* table) {
59 std::unique_ptr<uint8_t[]> header_buffer =
60 std::make_unique<uint8_t[]>(SUPER_VBMETA_HEADER_SIZE);
61 if (!android::base::ReadFullyAtOffset(fd, header_buffer.get(), SUPER_VBMETA_HEADER_SIZE,
62 offset)) {
63 return ErrnoError() << "Couldn't read super vbmeta header at offset " << offset;
64 }
65
66 Result<void> rv_header = LoadAndVerifySuperVBMetaHeader(header_buffer.get(), &table->header);
67 if (!rv_header.ok()) {
68 return rv_header;
69 }
70
71 const uint64_t descriptors_offset = offset + table->header.header_size;
72 std::unique_ptr<uint8_t[]> descriptors_buffer =
73 std::make_unique<uint8_t[]>(table->header.descriptors_size);
74 if (!android::base::ReadFullyAtOffset(fd, descriptors_buffer.get(),
75 table->header.descriptors_size, descriptors_offset)) {
76 return ErrnoError() << "Couldn't read super vbmeta descriptors at offset "
77 << descriptors_offset;
78 }
79
80 LoadVBMetaDescriptors(descriptors_buffer.get(), table->header.descriptors_size,
81 &table->descriptors);
82 return {};
83 }
84
ReadPrimaryVBMetaTable(int fd,VBMetaTable * table)85 Result<void> ReadPrimaryVBMetaTable(int fd, VBMetaTable* table) {
86 uint64_t offset = PRIMARY_SUPER_VBMETA_TABLE_OFFSET;
87 return ReadVBMetaTable(fd, offset, table);
88 }
89
ReadBackupVBMetaTable(int fd,VBMetaTable * table)90 Result<void> ReadBackupVBMetaTable(int fd, VBMetaTable* table) {
91 uint64_t offset = BACKUP_SUPER_VBMETA_TABLE_OFFSET;
92 return ReadVBMetaTable(fd, offset, table);
93 }
94
ReadVBMetaImage(int fd,int slot)95 Result<std::string> ReadVBMetaImage(int fd, int slot) {
96 const uint64_t offset = 2 * SUPER_VBMETA_TABLE_MAX_SIZE + slot * VBMETA_IMAGE_MAX_SIZE;
97 std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(VBMETA_IMAGE_MAX_SIZE);
98 if (!android::base::ReadFullyAtOffset(fd, buffer.get(), VBMETA_IMAGE_MAX_SIZE, offset)) {
99 return ErrnoError() << "Couldn't read vbmeta image at offset " << offset;
100 }
101 return std::string(reinterpret_cast<char*>(buffer.get()), VBMETA_IMAGE_MAX_SIZE);
102 }
103
ValidateVBMetaImage(int super_vbmeta_fd,int vbmeta_index,const std::string & vbmeta_image)104 Result<void> ValidateVBMetaImage(int super_vbmeta_fd, int vbmeta_index,
105 const std::string& vbmeta_image) {
106 Result<std::string> content = ReadVBMetaImage(super_vbmeta_fd, vbmeta_index);
107 if (!content.ok()) {
108 return content.error();
109 }
110
111 if (vbmeta_image != content.value()) {
112 return Error() << "VBMeta Image in Super VBMeta differ from the original one.";
113 }
114 return {};
115 }
116
117 } // namespace fs_mgr
118 } // namespace android
119