blob: 1da71debedc3e3533f1c3c3e00f691416bbf7763 [file] [log] [blame]
Jooyung Han017916b2021-04-20 03:57:19 +09001/*
2 * Copyright (C) 2021 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 <sys/stat.h>
18#include <sys/types.h>
19#include <unistd.h>
20
21#include <fstream>
22#include <iostream>
23#include <optional>
24#include <string>
25#include <vector>
26
27#include <android-base/file.h>
28#include <android-base/result.h>
29#include <com_android_apex.h>
30#include <image_aggregator.h>
31#include <json/json.h>
32
Jooyung Han74573482021-06-08 17:10:21 +090033#include "microdroid/metadata.h"
Jooyung Han017916b2021-04-20 03:57:19 +090034
35using android::base::Dirname;
36using android::base::ErrnoError;
37using android::base::Error;
38using android::base::Result;
Jooyung Hana54dcaf2021-05-13 21:57:02 +090039using android::base::unique_fd;
Jooyung Han74573482021-06-08 17:10:21 +090040using android::microdroid::ApexPayload;
41using android::microdroid::ApkPayload;
42using android::microdroid::Metadata;
43using android::microdroid::WriteMetadata;
Jooyung Han017916b2021-04-20 03:57:19 +090044
45using com::android::apex::ApexInfoList;
46using com::android::apex::readApexInfoList;
47
Jooyung Hana54dcaf2021-05-13 21:57:02 +090048using cuttlefish::AlignToPartitionSize;
Jooyung Han017916b2021-04-20 03:57:19 +090049using cuttlefish::CreateCompositeDisk;
Jooyung Han017916b2021-04-20 03:57:19 +090050using cuttlefish::kLinuxFilesystem;
Jooyung Hana54dcaf2021-05-13 21:57:02 +090051using cuttlefish::MultipleImagePartition;
Jooyung Han017916b2021-04-20 03:57:19 +090052
53Result<uint32_t> GetFileSize(const std::string& path) {
54 struct stat st;
55 if (lstat(path.c_str(), &st) == -1) {
56 return ErrnoError() << "Can't lstat " << path;
57 }
58 return static_cast<uint32_t>(st.st_size);
59}
60
61std::string ToAbsolute(const std::string& path, const std::string& dirname) {
62 bool is_absolute = !path.empty() && path[0] == '/';
63 if (is_absolute) {
64 return path;
65 } else {
66 return dirname + "/" + path;
67 }
68}
69
70// Returns `append` is appended to the end of filename preserving the extension.
71std::string AppendFileName(const std::string& filename, const std::string& append) {
72 size_t pos = filename.find_last_of('.');
73 if (pos == std::string::npos) {
74 return filename + append;
75 } else {
76 return filename.substr(0, pos) + append + filename.substr(pos);
77 }
78}
79
80struct ApexConfig {
81 std::string name; // the apex name
82 std::string path; // the path to the apex file
83 // absolute or relative to the config file
84 std::optional<std::string> public_key;
85 std::optional<std::string> root_digest;
86};
87
Jooyung Han57d895e2021-05-13 21:58:41 +090088struct ApkConfig {
89 std::string name;
90 // TODO(jooyung): find path/idsig with name
91 std::string path;
92};
93
Jooyung Han017916b2021-04-20 03:57:19 +090094struct Config {
95 std::string dirname; // config file's direname to resolve relative paths in the config
96
Jooyung Han74573482021-06-08 17:10:21 +090097 // TODO(b/185956069) remove this when VirtualizationService can provide apex paths
Jooyung Han017916b2021-04-20 03:57:19 +090098 std::vector<std::string> system_apexes;
Jooyung Han74573482021-06-08 17:10:21 +090099
Jooyung Han017916b2021-04-20 03:57:19 +0900100 std::vector<ApexConfig> apexes;
Jooyung Han57d895e2021-05-13 21:58:41 +0900101 std::optional<ApkConfig> apk;
Jooyung Han347d9f22021-05-28 00:05:14 +0900102 std::optional<std::string> payload_config_path;
Jooyung Han017916b2021-04-20 03:57:19 +0900103};
104
105#define DO(expr) \
106 if (auto res = (expr); !res.ok()) return res.error()
107
108Result<void> ParseJson(const Json::Value& value, std::string& s) {
109 if (!value.isString()) {
110 return Error() << "should be a string: " << value;
111 }
112 s = value.asString();
113 return {};
114}
115
Jooyung Han57d895e2021-05-13 21:58:41 +0900116template <typename T>
117Result<void> ParseJson(const Json::Value& value, std::optional<T>& s) {
Jooyung Han017916b2021-04-20 03:57:19 +0900118 if (value.isNull()) {
119 s.reset();
120 return {};
121 }
122 s.emplace();
123 return ParseJson(value, *s);
124}
125
Jooyung Han347d9f22021-05-28 00:05:14 +0900126template <typename T>
127Result<void> ParseJson(const Json::Value& values, std::vector<T>& parsed) {
128 for (const Json::Value& value : values) {
129 T t;
130 DO(ParseJson(value, t));
131 parsed.push_back(std::move(t));
132 }
133 return {};
134}
135
Jooyung Han017916b2021-04-20 03:57:19 +0900136Result<void> ParseJson(const Json::Value& value, ApexConfig& apex_config) {
137 DO(ParseJson(value["name"], apex_config.name));
138 DO(ParseJson(value["path"], apex_config.path));
139 DO(ParseJson(value["publicKey"], apex_config.public_key));
140 DO(ParseJson(value["rootDigest"], apex_config.root_digest));
141 return {};
142}
143
Jooyung Han57d895e2021-05-13 21:58:41 +0900144Result<void> ParseJson(const Json::Value& value, ApkConfig& apk_config) {
145 DO(ParseJson(value["name"], apk_config.name));
146 DO(ParseJson(value["path"], apk_config.path));
147 return {};
148}
149
Jooyung Han017916b2021-04-20 03:57:19 +0900150Result<void> ParseJson(const Json::Value& value, Config& config) {
151 DO(ParseJson(value["system_apexes"], config.system_apexes));
152 DO(ParseJson(value["apexes"], config.apexes));
Jooyung Han57d895e2021-05-13 21:58:41 +0900153 DO(ParseJson(value["apk"], config.apk));
Jooyung Han347d9f22021-05-28 00:05:14 +0900154 DO(ParseJson(value["payload_config_path"], config.payload_config_path));
Jooyung Han017916b2021-04-20 03:57:19 +0900155 return {};
156}
157
158Result<Config> LoadConfig(const std::string& config_file) {
159 std::ifstream in(config_file);
160 Json::CharReaderBuilder builder;
161 Json::Value root;
162 Json::String errs;
163 if (!parseFromStream(builder, in, &root, &errs)) {
164 return Error() << "bad config: " << errs;
165 }
166
167 Config config;
168 config.dirname = Dirname(config_file);
169 DO(ParseJson(root, config));
170 return config;
171}
172
173#undef DO
174
175Result<void> LoadSystemApexes(Config& config) {
176 static const char* kApexInfoListFile = "/apex/apex-info-list.xml";
177 std::optional<ApexInfoList> apex_info_list = readApexInfoList(kApexInfoListFile);
178 if (!apex_info_list.has_value()) {
179 return Error() << "Failed to read " << kApexInfoListFile;
180 }
181 auto get_apex_path = [&](const std::string& apex_name) -> std::optional<std::string> {
182 for (const auto& apex_info : apex_info_list->getApexInfo()) {
183 if (apex_info.getIsActive() && apex_info.getModuleName() == apex_name) {
184 return apex_info.getModulePath();
185 }
186 }
187 return std::nullopt;
188 };
189 for (const auto& apex_name : config.system_apexes) {
190 const auto& apex_path = get_apex_path(apex_name);
191 if (!apex_path.has_value()) {
192 return Error() << "Can't find the system apex: " << apex_name;
193 }
194 config.apexes.push_back(ApexConfig{
195 .name = apex_name,
196 .path = *apex_path,
197 .public_key = std::nullopt,
198 .root_digest = std::nullopt,
199 });
200 }
201 return {};
202}
203
Jooyung Han74573482021-06-08 17:10:21 +0900204Result<void> MakeMetadata(const Config& config, const std::string& filename) {
205 Metadata metadata;
206 metadata.set_version(1);
Jooyung Han017916b2021-04-20 03:57:19 +0900207
208 for (const auto& apex_config : config.apexes) {
Jooyung Han74573482021-06-08 17:10:21 +0900209 auto* apex = metadata.add_apexes();
Jooyung Han017916b2021-04-20 03:57:19 +0900210
211 // name
Jooyung Han74573482021-06-08 17:10:21 +0900212 apex->set_name(apex_config.name);
Jooyung Han017916b2021-04-20 03:57:19 +0900213
214 // publicKey
215 if (apex_config.public_key.has_value()) {
Jooyung Han74573482021-06-08 17:10:21 +0900216 apex->set_publickey(apex_config.public_key.value());
Jooyung Han017916b2021-04-20 03:57:19 +0900217 }
218
219 // rootDigest
220 if (apex_config.root_digest.has_value()) {
Jooyung Han74573482021-06-08 17:10:21 +0900221 apex->set_rootdigest(apex_config.root_digest.value());
Jooyung Han017916b2021-04-20 03:57:19 +0900222 }
223 }
224
Jooyung Han57d895e2021-05-13 21:58:41 +0900225 if (config.apk.has_value()) {
Jooyung Han74573482021-06-08 17:10:21 +0900226 auto* apk = metadata.mutable_apk();
227 apk->set_name(config.apk->name);
228 apk->set_payload_partition_name("microdroid-apk");
Jooyung Han57d895e2021-05-13 21:58:41 +0900229 // TODO(jooyung): set idsig partition as well
230 }
231
Jooyung Han347d9f22021-05-28 00:05:14 +0900232 if (config.payload_config_path.has_value()) {
Jooyung Han74573482021-06-08 17:10:21 +0900233 *metadata.mutable_payload_config_path() = config.payload_config_path.value();
Jooyung Han347d9f22021-05-28 00:05:14 +0900234 }
235
Jooyung Han017916b2021-04-20 03:57:19 +0900236 std::ofstream out(filename);
Jooyung Han74573482021-06-08 17:10:21 +0900237 return WriteMetadata(metadata, out);
Jooyung Han017916b2021-04-20 03:57:19 +0900238}
239
Jooyung Hana54dcaf2021-05-13 21:57:02 +0900240Result<void> GenerateFiller(const std::string& file_path, const std::string& filler_path) {
241 auto file_size = GetFileSize(file_path);
242 if (!file_size.ok()) {
243 return file_size.error();
244 }
245 auto disk_size = AlignToPartitionSize(*file_size + sizeof(uint32_t));
246
247 unique_fd fd(TEMP_FAILURE_RETRY(open(filler_path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0600)));
248 if (fd.get() == -1) {
249 return ErrnoError() << "open(" << filler_path << ") failed.";
250 }
251 uint32_t size = htobe32(static_cast<uint32_t>(*file_size));
252 if (ftruncate(fd.get(), disk_size - *file_size) == -1) {
253 return ErrnoError() << "ftruncate(" << filler_path << ") failed.";
254 }
255 if (lseek(fd.get(), -sizeof(size), SEEK_END) == -1) {
256 return ErrnoError() << "lseek(" << filler_path << ") failed.";
257 }
258 if (write(fd.get(), &size, sizeof(size)) <= 0) {
259 return ErrnoError() << "write(" << filler_path << ") failed.";
260 }
261 return {};
262}
263
Jooyung Han74573482021-06-08 17:10:21 +0900264Result<void> MakePayload(const Config& config, const std::string& metadata_file,
Jooyung Han017916b2021-04-20 03:57:19 +0900265 const std::string& output_file) {
Jooyung Hana54dcaf2021-05-13 21:57:02 +0900266 std::vector<MultipleImagePartition> partitions;
Jooyung Han017916b2021-04-20 03:57:19 +0900267
Jooyung Han74573482021-06-08 17:10:21 +0900268 // put metadata at the first partition
Jooyung Hana54dcaf2021-05-13 21:57:02 +0900269 partitions.push_back(MultipleImagePartition{
Jooyung Han74573482021-06-08 17:10:21 +0900270 .label = "metadata",
271 .image_file_paths = {metadata_file},
Jooyung Han017916b2021-04-20 03:57:19 +0900272 .type = kLinuxFilesystem,
273 .read_only = true,
274 });
275
Jooyung Han57d895e2021-05-13 21:58:41 +0900276 int filler_count = 0;
277 auto add_partition = [&](auto partition_name, auto file_path) -> Result<void> {
278 std::string filler_path = output_file + "." + std::to_string(filler_count++);
279 if (auto ret = GenerateFiller(file_path, filler_path); !ret.ok()) {
280 return ret.error();
281 }
282 partitions.push_back(MultipleImagePartition{
283 .label = partition_name,
284 .image_file_paths = {file_path, filler_path},
285 .type = kLinuxFilesystem,
286 .read_only = true,
287 });
288 return {};
289 };
290
Jooyung Hana54dcaf2021-05-13 21:57:02 +0900291 // put apexes at the subsequent partitions with "size" filler
Jooyung Han017916b2021-04-20 03:57:19 +0900292 for (size_t i = 0; i < config.apexes.size(); i++) {
293 const auto& apex_config = config.apexes[i];
Jooyung Hana54dcaf2021-05-13 21:57:02 +0900294 std::string apex_path = ToAbsolute(apex_config.path, config.dirname);
Jooyung Han57d895e2021-05-13 21:58:41 +0900295 if (auto ret = add_partition("microdroid-apex-" + std::to_string(i), apex_path);
296 !ret.ok()) {
Jooyung Hana54dcaf2021-05-13 21:57:02 +0900297 return ret.error();
298 }
Jooyung Han57d895e2021-05-13 21:58:41 +0900299 }
300 // put apk with "size" filler if necessary.
301 // TODO(jooyung): partition name("microdroid-apk") is TBD
302 if (config.apk.has_value()) {
303 std::string apk_path = ToAbsolute(config.apk->path, config.dirname);
304 if (auto ret = add_partition("microdroid-apk", apk_path); !ret.ok()) {
305 return ret.error();
306 }
Jooyung Han017916b2021-04-20 03:57:19 +0900307 }
308
309 const std::string gpt_header = AppendFileName(output_file, "-header");
310 const std::string gpt_footer = AppendFileName(output_file, "-footer");
311 CreateCompositeDisk(partitions, gpt_header, gpt_footer, output_file);
312 return {};
313}
314
315int main(int argc, char** argv) {
316 if (argc != 3) {
317 std::cerr << "Usage: " << argv[0] << " <config> <output>\n";
318 return 1;
319 }
320
321 auto config = LoadConfig(argv[1]);
322 if (!config.ok()) {
323 std::cerr << config.error() << '\n';
324 return 1;
325 }
326
327 if (const auto res = LoadSystemApexes(*config); !res.ok()) {
328 std::cerr << res.error() << '\n';
329 return 1;
330 }
331
332 const std::string output_file(argv[2]);
Jooyung Han74573482021-06-08 17:10:21 +0900333 const std::string metadata_file = AppendFileName(output_file, "-metadata");
Jooyung Han017916b2021-04-20 03:57:19 +0900334
Jooyung Han74573482021-06-08 17:10:21 +0900335 if (const auto res = MakeMetadata(*config, metadata_file); !res.ok()) {
Jooyung Han017916b2021-04-20 03:57:19 +0900336 std::cerr << res.error() << '\n';
337 return 1;
338 }
Jooyung Han74573482021-06-08 17:10:21 +0900339 if (const auto res = MakePayload(*config, metadata_file, output_file); !res.ok()) {
Jooyung Han017916b2021-04-20 03:57:19 +0900340 std::cerr << res.error() << '\n';
341 return 1;
342 }
343
344 return 0;
345}