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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
/*
* 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 <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <android-base/file.h>
#include <gtest/gtest.h>
#include <openssl/sha.h>
#include <sparse/sparse.h>
#include "reader.h"
#include "super_vbmeta_format.h"
#include "utility.h"
#include "writer.h"
#define FAKE_DATA_SIZE 40960
#define FAKE_PARTITION_SIZE FAKE_DATA_SIZE * 25
using android::base::Result;
using android::fs_mgr::GetFileSize;
using android::fs_mgr::ReadVBMetaImage;
using SparsePtr = std::unique_ptr<sparse_file, decltype(&sparse_file_destroy)>;
void GeneratePartitionImage(int fd, const std::string& file_name,
const std::string& partition_name) {
std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(FAKE_DATA_SIZE);
for (size_t c = 0; c < FAKE_DATA_SIZE; c++) {
buffer[c] = uint8_t(c);
}
SparsePtr file(sparse_file_new(512 /* block size */, FAKE_DATA_SIZE), sparse_file_destroy);
EXPECT_TRUE(file);
EXPECT_EQ(0, sparse_file_add_data(file.get(), buffer.get(), FAKE_DATA_SIZE,
0 /* offset in blocks */));
EXPECT_EQ(0, sparse_file_write(file.get(), fd, false /* gz */, true /* sparse */,
false /* crc */));
std::stringstream cmd;
cmd << "avbtool add_hashtree_footer"
<< " --image " << file_name << " --partition_name " << partition_name
<< " --partition_size " << FAKE_PARTITION_SIZE << " --algorithm SHA256_RSA2048"
<< " --key data/testkey_rsa2048.pem";
int rc = system(cmd.str().c_str());
EXPECT_TRUE(WIFEXITED(rc));
EXPECT_EQ(WEXITSTATUS(rc), 0);
}
void GenerateVBMetaImage(const std::string& vbmeta_file_name,
const std::string& include_file_name) {
std::stringstream cmd;
cmd << "avbtool make_vbmeta_image"
<< " --output " << vbmeta_file_name << " --include_descriptors_from_image "
<< include_file_name;
int rc = system(cmd.str().c_str());
EXPECT_TRUE(WIFEXITED(rc));
EXPECT_EQ(WEXITSTATUS(rc), 0);
}
std::string ReadVBMetaImageFromFile(const std::string& file) {
android::base::unique_fd fd(open(file.c_str(), O_RDONLY | O_CLOEXEC));
EXPECT_GT(fd, 0);
Result<uint64_t> file_size = GetFileSize(fd);
EXPECT_RESULT_OK(file_size);
std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(VBMETA_IMAGE_MAX_SIZE);
EXPECT_TRUE(android::base::ReadFully(fd, buffer.get(), file_size.value()));
return std::string(reinterpret_cast<char*>(buffer.get()), VBMETA_IMAGE_MAX_SIZE);
}
TEST(VBMetaTableTest, VBMetaTableBasic) {
TemporaryDir td;
// Generate Partition Image
TemporaryFile system_tf(std::string(td.path));
std::string system_path(system_tf.path);
GeneratePartitionImage(system_tf.fd, system_path, "system");
system_tf.release();
TemporaryFile vendor_tf(std::string(td.path));
std::string vendor_path(vendor_tf.path);
GeneratePartitionImage(vendor_tf.fd, vendor_path, "vendor");
vendor_tf.release();
TemporaryFile product_tf(std::string(td.path));
std::string product_path(product_tf.path);
GeneratePartitionImage(product_tf.fd, product_path, "product");
product_tf.release();
// Generate VBMeta Image
std::string vbmeta_system_path(td.path);
vbmeta_system_path.append("/vbmeta_system.img");
GenerateVBMetaImage(vbmeta_system_path, system_path);
std::string vbmeta_vendor_path(td.path);
vbmeta_vendor_path.append("/vbmeta_vendor.img");
GenerateVBMetaImage(vbmeta_vendor_path, vendor_path);
std::string vbmeta_product_path(td.path);
vbmeta_product_path.append("/vbmeta_product.img");
GenerateVBMetaImage(vbmeta_product_path, product_path);
// Generate Super VBMeta Image
std::string super_vbmeta_path(td.path);
super_vbmeta_path.append("/super_vbmeta.img");
std::stringstream cmd;
cmd << "vbmake"
<< " --image "
<< "vbmeta_system"
<< "=" << vbmeta_system_path << " --image "
<< "vbmeta_vendor"
<< "=" << vbmeta_vendor_path << " --image "
<< "vbmeta_product"
<< "=" << vbmeta_product_path << " --output=" << super_vbmeta_path;
int rc = system(cmd.str().c_str());
ASSERT_TRUE(WIFEXITED(rc));
ASSERT_EQ(WEXITSTATUS(rc), 0);
android::base::unique_fd fd(open(super_vbmeta_path.c_str(), O_RDONLY | O_CLOEXEC));
EXPECT_GT(fd, 0);
// Check the size of vbmeta table
Result<uint64_t> super_vbmeta_size = GetFileSize(fd);
EXPECT_RESULT_OK(super_vbmeta_size);
EXPECT_EQ(super_vbmeta_size.value(),
SUPER_VBMETA_TABLE_MAX_SIZE * 2 + VBMETA_IMAGE_MAX_SIZE * 3);
// Check Primary vbmeta table is equal to Backup one
VBMetaTable table;
EXPECT_RESULT_OK(android::fs_mgr::ReadPrimaryVBMetaTable(fd, &table));
VBMetaTable table_backup;
EXPECT_RESULT_OK(android::fs_mgr::ReadBackupVBMetaTable(fd, &table_backup));
EXPECT_EQ(android::fs_mgr::SerializeVBMetaTable(table),
android::fs_mgr::SerializeVBMetaTable(table_backup));
// Check vbmeta table Header Checksum
std::string serial_table = android::fs_mgr::SerializeVBMetaTable(table);
std::string serial_removed_checksum(serial_table);
// Replace checksum 32 bytes (starts at 16th byte) with 0
serial_removed_checksum.replace(16, 32, 32, 0);
uint8_t test_checksum[32];
::SHA256(reinterpret_cast<const uint8_t*>(serial_removed_checksum.c_str()),
table.header.total_size, &test_checksum[0]);
EXPECT_EQ(memcmp(table.header.checksum, test_checksum, 32), 0);
// Check vbmeta table descriptors and vbmeta images
EXPECT_EQ(table.descriptors.size(), 3);
EXPECT_EQ(table.descriptors[0].vbmeta_index, 0);
EXPECT_EQ(table.descriptors[0].vbmeta_name_length, 14);
EXPECT_EQ(table.descriptors[0].vbmeta_name, "vbmeta_product");
Result<std::string> vbmeta_product_content = ReadVBMetaImage(fd, 0);
EXPECT_RESULT_OK(vbmeta_product_content);
EXPECT_EQ(ReadVBMetaImageFromFile(vbmeta_product_path), vbmeta_product_content.value());
EXPECT_EQ(table.descriptors[1].vbmeta_index, 1);
EXPECT_EQ(table.descriptors[1].vbmeta_name_length, 13);
EXPECT_EQ(table.descriptors[1].vbmeta_name, "vbmeta_system");
Result<std::string> vbmeta_system_content = ReadVBMetaImage(fd, 1);
EXPECT_RESULT_OK(vbmeta_system_content);
EXPECT_EQ(ReadVBMetaImageFromFile(vbmeta_system_path), vbmeta_system_content.value());
EXPECT_EQ(table.descriptors[2].vbmeta_index, 2);
EXPECT_EQ(table.descriptors[2].vbmeta_name_length, 13);
EXPECT_EQ(table.descriptors[2].vbmeta_name, "vbmeta_vendor");
Result<std::string> vbmeta_vendor_content = ReadVBMetaImage(fd, 2);
EXPECT_RESULT_OK(vbmeta_vendor_content);
EXPECT_EQ(ReadVBMetaImageFromFile(vbmeta_vendor_path), vbmeta_vendor_content.value());
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
|