blob: 6e3f5260d77adb201b5e2d04a8d231732a9ae992 [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++));
Richard Fung815ff932022-02-09 18:56:03 +0000174 apex->set_is_factory(true);
Jooyung Han017916b2021-04-20 03:57:19 +0900175 }
176
Jooyung Han57d895e2021-05-13 21:58:41 +0900177 if (config.apk.has_value()) {
Jooyung Han74573482021-06-08 17:10:21 +0900178 auto* apk = metadata.mutable_apk();
179 apk->set_name(config.apk->name);
180 apk->set_payload_partition_name("microdroid-apk");
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900181 apk->set_idsig_partition_name("microdroid-apk-idsig");
Jooyung Han57d895e2021-05-13 21:58:41 +0900182 }
183
Jooyung Han347d9f22021-05-28 00:05:14 +0900184 if (config.payload_config_path.has_value()) {
Jooyung Han74573482021-06-08 17:10:21 +0900185 *metadata.mutable_payload_config_path() = config.payload_config_path.value();
Jooyung Han347d9f22021-05-28 00:05:14 +0900186 }
187
Jooyung Han017916b2021-04-20 03:57:19 +0900188 std::ofstream out(filename);
Jooyung Han74573482021-06-08 17:10:21 +0900189 return WriteMetadata(metadata, out);
Jooyung Han017916b2021-04-20 03:57:19 +0900190}
191
Jooyung Han7ce2e532021-06-16 16:52:02 +0900192// fill zeros to align |file_path|'s size to BLOCK_SIZE(4096) boundary.
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900193// return true when the filler is needed.
Jooyung Han7ce2e532021-06-16 16:52:02 +0900194Result<bool> ZeroFiller(const std::string& file_path, const std::string& filler_path) {
195 auto file_size = GetFileSize(file_path);
196 if (!file_size.ok()) {
197 return file_size.error();
198 }
199 auto disk_size = AlignToPartitionSize(*file_size);
200 if (disk_size <= *file_size) {
201 return false;
202 }
Jooyung Han7ce2e532021-06-16 16:52:02 +0900203 unique_fd fd(TEMP_FAILURE_RETRY(open(filler_path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0600)));
204 if (fd.get() == -1) {
205 return ErrnoError() << "open(" << filler_path << ") failed.";
206 }
Jooyung Hanf0f76822021-06-17 22:00:54 +0900207 if (ftruncate(fd.get(), disk_size - *file_size) == -1) {
208 return ErrnoError() << "ftruncate(" << filler_path << ") failed.";
Jooyung Han7ce2e532021-06-16 16:52:02 +0900209 }
210 return true;
211}
212
Jooyung Han74573482021-06-08 17:10:21 +0900213Result<void> MakePayload(const Config& config, const std::string& metadata_file,
Jooyung Han017916b2021-04-20 03:57:19 +0900214 const std::string& output_file) {
Jooyung Hana54dcaf2021-05-13 21:57:02 +0900215 std::vector<MultipleImagePartition> partitions;
Jooyung Han017916b2021-04-20 03:57:19 +0900216
Jooyung Han57d895e2021-05-13 21:58:41 +0900217 int filler_count = 0;
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900218 auto add_partition = [&](auto partition_name, auto file_path) -> Result<void> {
Jooyung Han7ce2e532021-06-16 16:52:02 +0900219 std::vector<std::string> image_files{file_path};
220
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900221 std::string filler_path =
222 AppendFileName(output_file, "-filler-" + std::to_string(filler_count++));
223 if (auto ret = ZeroFiller(file_path, filler_path); !ret.ok()) {
Jooyung Han57d895e2021-05-13 21:58:41 +0900224 return ret.error();
Jooyung Han7ce2e532021-06-16 16:52:02 +0900225 } else if (*ret) {
226 image_files.push_back(filler_path);
Jooyung Han57d895e2021-05-13 21:58:41 +0900227 }
228 partitions.push_back(MultipleImagePartition{
229 .label = partition_name,
Jooyung Han7ce2e532021-06-16 16:52:02 +0900230 .image_file_paths = image_files,
Jooyung Han57d895e2021-05-13 21:58:41 +0900231 .type = kLinuxFilesystem,
232 .read_only = true,
233 });
234 return {};
235 };
236
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900237 // put metadata at the first partition
238 partitions.push_back(MultipleImagePartition{
239 .label = "payload-metadata",
240 .image_file_paths = {metadata_file},
241 .type = kLinuxFilesystem,
242 .read_only = true,
243 });
244 // put apexes at the subsequent partitions
Jooyung Han017916b2021-04-20 03:57:19 +0900245 for (size_t i = 0; i < config.apexes.size(); i++) {
246 const auto& apex_config = config.apexes[i];
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900247 std::string apex_path = RelativeTo(apex_config.path, config.dirname);
248 if (auto ret = add_partition("microdroid-apex-" + std::to_string(i), apex_path);
Jooyung Han57d895e2021-05-13 21:58:41 +0900249 !ret.ok()) {
Jooyung Hana54dcaf2021-05-13 21:57:02 +0900250 return ret.error();
251 }
Jooyung Han57d895e2021-05-13 21:58:41 +0900252 }
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900253 // put apk and its idsig
Jooyung Han57d895e2021-05-13 21:58:41 +0900254 if (config.apk.has_value()) {
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900255 std::string apk_path = RelativeTo(config.apk->path, config.dirname);
256 if (auto ret = add_partition("microdroid-apk", apk_path); !ret.ok()) {
Jooyung Han57d895e2021-05-13 21:58:41 +0900257 return ret.error();
258 }
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900259 std::string idsig_path = RelativeTo(config.apk->idsig_path, config.dirname);
260 if (auto ret = add_partition("microdroid-apk-idsig", idsig_path); !ret.ok()) {
261 return ret.error();
Jooyung Han7ce2e532021-06-16 16:52:02 +0900262 }
Jooyung Han017916b2021-04-20 03:57:19 +0900263 }
264
265 const std::string gpt_header = AppendFileName(output_file, "-header");
266 const std::string gpt_footer = AppendFileName(output_file, "-footer");
267 CreateCompositeDisk(partitions, gpt_header, gpt_footer, output_file);
268 return {};
269}
270
271int main(int argc, char** argv) {
Jooyung Han6afd6672022-02-22 05:22:23 +0900272 if (argc < 3 || argc > 4) {
273 std::cerr << "Usage: " << argv[0] << " [--metadata-only] <config> <output>\n";
Jooyung Han017916b2021-04-20 03:57:19 +0900274 return 1;
275 }
Jooyung Han6afd6672022-02-22 05:22:23 +0900276 int arg_index = 1;
277 bool metadata_only = false;
278 if (strcmp(argv[arg_index], "--metadata-only") == 0) {
279 metadata_only = true;
280 arg_index++;
281 }
Jooyung Han017916b2021-04-20 03:57:19 +0900282
Jooyung Han6afd6672022-02-22 05:22:23 +0900283 auto config = LoadConfig(argv[arg_index++]);
Jooyung Han017916b2021-04-20 03:57:19 +0900284 if (!config.ok()) {
Jooyung Han2e7eefd2021-08-05 11:25:23 +0900285 std::cerr << "bad config: " << config.error() << '\n';
Jooyung Han017916b2021-04-20 03:57:19 +0900286 return 1;
287 }
288
Jooyung Han6afd6672022-02-22 05:22:23 +0900289 const std::string output_file(argv[arg_index++]);
290 const std::string metadata_file =
291 metadata_only ? output_file : AppendFileName(output_file, "-metadata");
Jooyung Han017916b2021-04-20 03:57:19 +0900292
Jooyung Han74573482021-06-08 17:10:21 +0900293 if (const auto res = MakeMetadata(*config, metadata_file); !res.ok()) {
Jooyung Han017916b2021-04-20 03:57:19 +0900294 std::cerr << res.error() << '\n';
295 return 1;
296 }
Jooyung Han6afd6672022-02-22 05:22:23 +0900297 if (metadata_only) {
298 return 0;
299 }
Jooyung Han74573482021-06-08 17:10:21 +0900300 if (const auto res = MakePayload(*config, metadata_file, output_file); !res.ok()) {
Jooyung Han017916b2021-04-20 03:57:19 +0900301 std::cerr << res.error() << '\n';
302 return 1;
303 }
304
305 return 0;
306}