File: reader.cpp

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (118 lines) | stat: -rw-r--r-- 4,514 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "reader.h"

#include <android-base/file.h>

using android::base::ErrnoError;
using android::base::Error;
using android::base::Result;

namespace android {
namespace fs_mgr {

Result<void> LoadAndVerifySuperVBMetaHeader(const void* buffer, SuperVBMetaHeader* header) {
    memcpy(header, buffer, sizeof(*header));

    // Do basic validation of super vbmeta.
    if (header->magic != SUPER_VBMETA_MAGIC) {
        return Error() << "Super VBMeta has invalid magic value";
    }

    // Check that the version is compatible.
    if (header->major_version != SUPER_VBMETA_MAJOR_VERSION ||
        header->minor_version > SUPER_VBMETA_MINOR_VERSION) {
        return Error() << "Super VBMeta has incompatible version";
    }
    return {};
}

void LoadVBMetaDescriptors(const void* buffer, uint32_t size,
                           std::vector<InternalVBMetaDescriptor>* descriptors) {
    for (int p = 0; p < size;) {
        InternalVBMetaDescriptor descriptor;
        memcpy(&descriptor, (char*)buffer + p, SUPER_VBMETA_DESCRIPTOR_SIZE);
        p += SUPER_VBMETA_DESCRIPTOR_SIZE;

        descriptor.vbmeta_name = std::string((char*)buffer + p, descriptor.vbmeta_name_length);
        p += descriptor.vbmeta_name_length;

        descriptors->emplace_back(std::move(descriptor));
    }
}

Result<void> ReadVBMetaTable(int fd, uint64_t offset, VBMetaTable* table) {
    std::unique_ptr<uint8_t[]> header_buffer =
            std::make_unique<uint8_t[]>(SUPER_VBMETA_HEADER_SIZE);
    if (!android::base::ReadFullyAtOffset(fd, header_buffer.get(), SUPER_VBMETA_HEADER_SIZE,
                                          offset)) {
        return ErrnoError() << "Couldn't read super vbmeta header at offset " << offset;
    }

    Result<void> rv_header = LoadAndVerifySuperVBMetaHeader(header_buffer.get(), &table->header);
    if (!rv_header.ok()) {
        return rv_header;
    }

    const uint64_t descriptors_offset = offset + table->header.header_size;
    std::unique_ptr<uint8_t[]> descriptors_buffer =
            std::make_unique<uint8_t[]>(table->header.descriptors_size);
    if (!android::base::ReadFullyAtOffset(fd, descriptors_buffer.get(),
                                          table->header.descriptors_size, descriptors_offset)) {
        return ErrnoError() << "Couldn't read super vbmeta descriptors at offset "
                            << descriptors_offset;
    }

    LoadVBMetaDescriptors(descriptors_buffer.get(), table->header.descriptors_size,
                          &table->descriptors);
    return {};
}

Result<void> ReadPrimaryVBMetaTable(int fd, VBMetaTable* table) {
    uint64_t offset = PRIMARY_SUPER_VBMETA_TABLE_OFFSET;
    return ReadVBMetaTable(fd, offset, table);
}

Result<void> ReadBackupVBMetaTable(int fd, VBMetaTable* table) {
    uint64_t offset = BACKUP_SUPER_VBMETA_TABLE_OFFSET;
    return ReadVBMetaTable(fd, offset, table);
}

Result<std::string> ReadVBMetaImage(int fd, int slot) {
    const uint64_t offset = 2 * SUPER_VBMETA_TABLE_MAX_SIZE + slot * VBMETA_IMAGE_MAX_SIZE;
    std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(VBMETA_IMAGE_MAX_SIZE);
    if (!android::base::ReadFullyAtOffset(fd, buffer.get(), VBMETA_IMAGE_MAX_SIZE, offset)) {
        return ErrnoError() << "Couldn't read vbmeta image at offset " << offset;
    }
    return std::string(reinterpret_cast<char*>(buffer.get()), VBMETA_IMAGE_MAX_SIZE);
}

Result<void> ValidateVBMetaImage(int super_vbmeta_fd, int vbmeta_index,
                                 const std::string& vbmeta_image) {
    Result<std::string> content = ReadVBMetaImage(super_vbmeta_fd, vbmeta_index);
    if (!content.ok()) {
        return content.error();
    }

    if (vbmeta_image != content.value()) {
        return Error() << "VBMeta Image in Super VBMeta differ from the original one.";
    }
    return {};
}

}  // namespace fs_mgr
}  // namespace android