blob: 33e91b9b629903350ac62aeb0e31821f0c0f0759 [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>
Jooyung Han017916b2021-04-20 03:57:19 +090029#include <image_aggregator.h>
30#include <json/json.h>
31
Jooyung Han74573482021-06-08 17:10:21 +090032#include "microdroid/metadata.h"
Jooyung Han017916b2021-04-20 03:57:19 +090033
34using android::base::Dirname;
35using android::base::ErrnoError;
36using android::base::Error;
37using android::base::Result;
Jooyung Hana54dcaf2021-05-13 21:57:02 +090038using android::base::unique_fd;
Jooyung Han74573482021-06-08 17:10:21 +090039using android::microdroid::ApexPayload;
40using android::microdroid::ApkPayload;
41using android::microdroid::Metadata;
42using android::microdroid::WriteMetadata;
Jooyung Han017916b2021-04-20 03:57:19 +090043
Jooyung Hana54dcaf2021-05-13 21:57:02 +090044using cuttlefish::AlignToPartitionSize;
Jooyung Han017916b2021-04-20 03:57:19 +090045using cuttlefish::CreateCompositeDisk;
Jooyung Han017916b2021-04-20 03:57:19 +090046using cuttlefish::kLinuxFilesystem;
Jooyung Hana54dcaf2021-05-13 21:57:02 +090047using cuttlefish::MultipleImagePartition;
Jooyung Han017916b2021-04-20 03:57:19 +090048
49Result<uint32_t> GetFileSize(const std::string& path) {
50 struct stat st;
51 if (lstat(path.c_str(), &st) == -1) {
52 return ErrnoError() << "Can't lstat " << path;
53 }
54 return static_cast<uint32_t>(st.st_size);
55}
56
Jooyung Han2e7eefd2021-08-05 11:25:23 +090057std::string RelativeTo(const std::string& path, const std::string& dirname) {
Jooyung Han017916b2021-04-20 03:57:19 +090058 bool is_absolute = !path.empty() && path[0] == '/';
Jooyung Han2e7eefd2021-08-05 11:25:23 +090059 if (is_absolute || dirname == ".") {
Jooyung Han017916b2021-04-20 03:57:19 +090060 return path;
61 } else {
62 return dirname + "/" + path;
63 }
64}
65
66// Returns `append` is appended to the end of filename preserving the extension.
67std::string AppendFileName(const std::string& filename, const std::string& append) {
68 size_t pos = filename.find_last_of('.');
69 if (pos == std::string::npos) {
70 return filename + append;
71 } else {
72 return filename.substr(0, pos) + append + filename.substr(pos);
73 }
74}
75
76struct ApexConfig {
77 std::string name; // the apex name
78 std::string path; // the path to the apex file
79 // absolute or relative to the config file
Jooyung Han017916b2021-04-20 03:57:19 +090080};
81
Jooyung Han57d895e2021-05-13 21:58:41 +090082struct ApkConfig {
83 std::string name;
Jooyung Han57d895e2021-05-13 21:58:41 +090084 std::string path;
Jooyung Han2e7eefd2021-08-05 11:25:23 +090085 std::string idsig_path;
Jooyung Han57d895e2021-05-13 21:58:41 +090086};
87
Jooyung Han017916b2021-04-20 03:57:19 +090088struct Config {
89 std::string dirname; // config file's direname to resolve relative paths in the config
90
Jooyung Han017916b2021-04-20 03:57:19 +090091 std::vector<ApexConfig> apexes;
Jooyung Han57d895e2021-05-13 21:58:41 +090092 std::optional<ApkConfig> apk;
Jooyung Han2e7eefd2021-08-05 11:25:23 +090093 // This is a path in the guest side
Jooyung Han347d9f22021-05-28 00:05:14 +090094 std::optional<std::string> payload_config_path;
Jooyung Han017916b2021-04-20 03:57:19 +090095};
96
97#define DO(expr) \
98 if (auto res = (expr); !res.ok()) return res.error()
99
100Result<void> ParseJson(const Json::Value& value, std::string& s) {
101 if (!value.isString()) {
102 return Error() << "should be a string: " << value;
103 }
104 s = value.asString();
105 return {};
106}
107
Jooyung Han57d895e2021-05-13 21:58:41 +0900108template <typename T>
109Result<void> ParseJson(const Json::Value& value, std::optional<T>& s) {
Jooyung Han017916b2021-04-20 03:57:19 +0900110 if (value.isNull()) {
111 s.reset();
112 return {};
113 }
114 s.emplace();
115 return ParseJson(value, *s);
116}
117
Jooyung Han347d9f22021-05-28 00:05:14 +0900118template <typename T>
119Result<void> ParseJson(const Json::Value& values, std::vector<T>& parsed) {
120 for (const Json::Value& value : values) {
121 T t;
122 DO(ParseJson(value, t));
123 parsed.push_back(std::move(t));
124 }
125 return {};
126}
127
Jooyung Han017916b2021-04-20 03:57:19 +0900128Result<void> ParseJson(const Json::Value& value, ApexConfig& apex_config) {
129 DO(ParseJson(value["name"], apex_config.name));
130 DO(ParseJson(value["path"], apex_config.path));
Jooyung Han017916b2021-04-20 03:57:19 +0900131 return {};
132}
133
Jooyung Han57d895e2021-05-13 21:58:41 +0900134Result<void> ParseJson(const Json::Value& value, ApkConfig& apk_config) {
135 DO(ParseJson(value["name"], apk_config.name));
136 DO(ParseJson(value["path"], apk_config.path));
Jooyung Han7ce2e532021-06-16 16:52:02 +0900137 DO(ParseJson(value["idsig_path"], apk_config.idsig_path));
Jooyung Han57d895e2021-05-13 21:58:41 +0900138 return {};
139}
140
Jooyung Han017916b2021-04-20 03:57:19 +0900141Result<void> ParseJson(const Json::Value& value, Config& config) {
Jooyung Han017916b2021-04-20 03:57:19 +0900142 DO(ParseJson(value["apexes"], config.apexes));
Jooyung Han57d895e2021-05-13 21:58:41 +0900143 DO(ParseJson(value["apk"], config.apk));
Jooyung Han347d9f22021-05-28 00:05:14 +0900144 DO(ParseJson(value["payload_config_path"], config.payload_config_path));
Jooyung Han017916b2021-04-20 03:57:19 +0900145 return {};
146}
147
148Result<Config> LoadConfig(const std::string& config_file) {
149 std::ifstream in(config_file);
150 Json::CharReaderBuilder builder;
151 Json::Value root;
152 Json::String errs;
153 if (!parseFromStream(builder, in, &root, &errs)) {
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900154 return Error() << errs;
Jooyung Han017916b2021-04-20 03:57:19 +0900155 }
156
157 Config config;
158 config.dirname = Dirname(config_file);
159 DO(ParseJson(root, config));
160 return config;
161}
162
163#undef DO
164
Jooyung Han74573482021-06-08 17:10:21 +0900165Result<void> MakeMetadata(const Config& config, const std::string& filename) {
166 Metadata metadata;
167 metadata.set_version(1);
Jooyung Han017916b2021-04-20 03:57:19 +0900168
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900169 int apex_index = 0;
Jooyung Han017916b2021-04-20 03:57:19 +0900170 for (const auto& apex_config : config.apexes) {
Jooyung Han74573482021-06-08 17:10:21 +0900171 auto* apex = metadata.add_apexes();
Jooyung Han74573482021-06-08 17:10:21 +0900172 apex->set_name(apex_config.name);
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900173 apex->set_partition_name("microdroid-apex-" + std::to_string(apex_index++));
Jooyung Han017916b2021-04-20 03:57:19 +0900174 }
175
Jooyung Han57d895e2021-05-13 21:58:41 +0900176 if (config.apk.has_value()) {
Jooyung Han74573482021-06-08 17:10:21 +0900177 auto* apk = metadata.mutable_apk();
178 apk->set_name(config.apk->name);
179 apk->set_payload_partition_name("microdroid-apk");
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900180 apk->set_idsig_partition_name("microdroid-apk-idsig");
Jooyung Han57d895e2021-05-13 21:58:41 +0900181 }
182
Jooyung Han347d9f22021-05-28 00:05:14 +0900183 if (config.payload_config_path.has_value()) {
Jooyung Han74573482021-06-08 17:10:21 +0900184 *metadata.mutable_payload_config_path() = config.payload_config_path.value();
Jooyung Han347d9f22021-05-28 00:05:14 +0900185 }
186
Jooyung Han017916b2021-04-20 03:57:19 +0900187 std::ofstream out(filename);
Jooyung Han74573482021-06-08 17:10:21 +0900188 return WriteMetadata(metadata, out);
Jooyung Han017916b2021-04-20 03:57:19 +0900189}
190
Jooyung Han7ce2e532021-06-16 16:52:02 +0900191// fill zeros to align |file_path|'s size to BLOCK_SIZE(4096) boundary.
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900192// return true when the filler is needed.
Jooyung Han7ce2e532021-06-16 16:52:02 +0900193Result<bool> ZeroFiller(const std::string& file_path, const std::string& filler_path) {
194 auto file_size = GetFileSize(file_path);
195 if (!file_size.ok()) {
196 return file_size.error();
197 }
198 auto disk_size = AlignToPartitionSize(*file_size);
199 if (disk_size <= *file_size) {
200 return false;
201 }
Jooyung Han7ce2e532021-06-16 16:52:02 +0900202 unique_fd fd(TEMP_FAILURE_RETRY(open(filler_path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0600)));
203 if (fd.get() == -1) {
204 return ErrnoError() << "open(" << filler_path << ") failed.";
205 }
Jooyung Hanf0f76822021-06-17 22:00:54 +0900206 if (ftruncate(fd.get(), disk_size - *file_size) == -1) {
207 return ErrnoError() << "ftruncate(" << filler_path << ") failed.";
Jooyung Han7ce2e532021-06-16 16:52:02 +0900208 }
209 return true;
210}
211
Jooyung Han74573482021-06-08 17:10:21 +0900212Result<void> MakePayload(const Config& config, const std::string& metadata_file,
Jooyung Han017916b2021-04-20 03:57:19 +0900213 const std::string& output_file) {
Jooyung Hana54dcaf2021-05-13 21:57:02 +0900214 std::vector<MultipleImagePartition> partitions;
Jooyung Han017916b2021-04-20 03:57:19 +0900215
Jooyung Han57d895e2021-05-13 21:58:41 +0900216 int filler_count = 0;
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900217 auto add_partition = [&](auto partition_name, auto file_path) -> Result<void> {
Jooyung Han7ce2e532021-06-16 16:52:02 +0900218 std::vector<std::string> image_files{file_path};
219
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900220 std::string filler_path =
221 AppendFileName(output_file, "-filler-" + std::to_string(filler_count++));
222 if (auto ret = ZeroFiller(file_path, filler_path); !ret.ok()) {
Jooyung Han57d895e2021-05-13 21:58:41 +0900223 return ret.error();
Jooyung Han7ce2e532021-06-16 16:52:02 +0900224 } else if (*ret) {
225 image_files.push_back(filler_path);
Jooyung Han57d895e2021-05-13 21:58:41 +0900226 }
227 partitions.push_back(MultipleImagePartition{
228 .label = partition_name,
Jooyung Han7ce2e532021-06-16 16:52:02 +0900229 .image_file_paths = image_files,
Jooyung Han57d895e2021-05-13 21:58:41 +0900230 .type = kLinuxFilesystem,
231 .read_only = true,
232 });
233 return {};
234 };
235
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900236 // put metadata at the first partition
237 partitions.push_back(MultipleImagePartition{
238 .label = "payload-metadata",
239 .image_file_paths = {metadata_file},
240 .type = kLinuxFilesystem,
241 .read_only = true,
242 });
243 // put apexes at the subsequent partitions
Jooyung Han017916b2021-04-20 03:57:19 +0900244 for (size_t i = 0; i < config.apexes.size(); i++) {
245 const auto& apex_config = config.apexes[i];
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900246 std::string apex_path = RelativeTo(apex_config.path, config.dirname);
247 if (auto ret = add_partition("microdroid-apex-" + std::to_string(i), apex_path);
Jooyung Han57d895e2021-05-13 21:58:41 +0900248 !ret.ok()) {
Jooyung Hana54dcaf2021-05-13 21:57:02 +0900249 return ret.error();
250 }
Jooyung Han57d895e2021-05-13 21:58:41 +0900251 }
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900252 // put apk and its idsig
Jooyung Han57d895e2021-05-13 21:58:41 +0900253 if (config.apk.has_value()) {
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900254 std::string apk_path = RelativeTo(config.apk->path, config.dirname);
255 if (auto ret = add_partition("microdroid-apk", apk_path); !ret.ok()) {
Jooyung Han57d895e2021-05-13 21:58:41 +0900256 return ret.error();
257 }
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900258 std::string idsig_path = RelativeTo(config.apk->idsig_path, config.dirname);
259 if (auto ret = add_partition("microdroid-apk-idsig", idsig_path); !ret.ok()) {
260 return ret.error();
Jooyung Han7ce2e532021-06-16 16:52:02 +0900261 }
Jooyung Han017916b2021-04-20 03:57:19 +0900262 }
263
264 const std::string gpt_header = AppendFileName(output_file, "-header");
265 const std::string gpt_footer = AppendFileName(output_file, "-footer");
266 CreateCompositeDisk(partitions, gpt_header, gpt_footer, output_file);
267 return {};
268}
269
270int main(int argc, char** argv) {
271 if (argc != 3) {
272 std::cerr << "Usage: " << argv[0] << " <config> <output>\n";
273 return 1;
274 }
275
276 auto config = LoadConfig(argv[1]);
277 if (!config.ok()) {
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900278 std::cerr << "bad config: " << config.error() << '\n';
Jooyung Han017916b2021-04-20 03:57:19 +0900279 return 1;
280 }
281
282 const std::string output_file(argv[2]);
Jooyung Han74573482021-06-08 17:10:21 +0900283 const std::string metadata_file = AppendFileName(output_file, "-metadata");
Jooyung Han017916b2021-04-20 03:57:19 +0900284
Jooyung Han74573482021-06-08 17:10:21 +0900285 if (const auto res = MakeMetadata(*config, metadata_file); !res.ok()) {
Jooyung Han017916b2021-04-20 03:57:19 +0900286 std::cerr << res.error() << '\n';
287 return 1;
288 }
Jooyung Han74573482021-06-08 17:10:21 +0900289 if (const auto res = MakePayload(*config, metadata_file, output_file); !res.ok()) {
Jooyung Han017916b2021-04-20 03:57:19 +0900290 std::cerr << res.error() << '\n';
291 return 1;
292 }
293
294 return 0;
295}