Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 1 | /* |
| 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 Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 33 | #include "microdroid/metadata.h" |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 34 | |
| 35 | using android::base::Dirname; |
| 36 | using android::base::ErrnoError; |
| 37 | using android::base::Error; |
| 38 | using android::base::Result; |
Jooyung Han | a54dcaf | 2021-05-13 21:57:02 +0900 | [diff] [blame] | 39 | using android::base::unique_fd; |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 40 | using android::microdroid::ApexPayload; |
| 41 | using android::microdroid::ApkPayload; |
| 42 | using android::microdroid::Metadata; |
| 43 | using android::microdroid::WriteMetadata; |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 44 | |
| 45 | using com::android::apex::ApexInfoList; |
| 46 | using com::android::apex::readApexInfoList; |
| 47 | |
Jooyung Han | a54dcaf | 2021-05-13 21:57:02 +0900 | [diff] [blame] | 48 | using cuttlefish::AlignToPartitionSize; |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 49 | using cuttlefish::CreateCompositeDisk; |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 50 | using cuttlefish::kLinuxFilesystem; |
Jooyung Han | a54dcaf | 2021-05-13 21:57:02 +0900 | [diff] [blame] | 51 | using cuttlefish::MultipleImagePartition; |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 52 | |
| 53 | Result<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 | |
| 61 | std::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. |
| 71 | std::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 | |
| 80 | struct 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 Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 88 | struct ApkConfig { |
| 89 | std::string name; |
Jooyung Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 90 | std::string path; |
Jooyung Han | 7ce2e53 | 2021-06-16 16:52:02 +0900 | [diff] [blame^] | 91 | // TODO(jooyung) make this required? |
| 92 | std::optional<std::string> idsig_path; |
Jooyung Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 93 | }; |
| 94 | |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 95 | struct Config { |
| 96 | std::string dirname; // config file's direname to resolve relative paths in the config |
| 97 | |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 98 | // TODO(b/185956069) remove this when VirtualizationService can provide apex paths |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 99 | std::vector<std::string> system_apexes; |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 100 | |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 101 | std::vector<ApexConfig> apexes; |
Jooyung Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 102 | std::optional<ApkConfig> apk; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 103 | std::optional<std::string> payload_config_path; |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | #define DO(expr) \ |
| 107 | if (auto res = (expr); !res.ok()) return res.error() |
| 108 | |
| 109 | Result<void> ParseJson(const Json::Value& value, std::string& s) { |
| 110 | if (!value.isString()) { |
| 111 | return Error() << "should be a string: " << value; |
| 112 | } |
| 113 | s = value.asString(); |
| 114 | return {}; |
| 115 | } |
| 116 | |
Jooyung Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 117 | template <typename T> |
| 118 | Result<void> ParseJson(const Json::Value& value, std::optional<T>& s) { |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 119 | if (value.isNull()) { |
| 120 | s.reset(); |
| 121 | return {}; |
| 122 | } |
| 123 | s.emplace(); |
| 124 | return ParseJson(value, *s); |
| 125 | } |
| 126 | |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 127 | template <typename T> |
| 128 | Result<void> ParseJson(const Json::Value& values, std::vector<T>& parsed) { |
| 129 | for (const Json::Value& value : values) { |
| 130 | T t; |
| 131 | DO(ParseJson(value, t)); |
| 132 | parsed.push_back(std::move(t)); |
| 133 | } |
| 134 | return {}; |
| 135 | } |
| 136 | |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 137 | Result<void> ParseJson(const Json::Value& value, ApexConfig& apex_config) { |
| 138 | DO(ParseJson(value["name"], apex_config.name)); |
| 139 | DO(ParseJson(value["path"], apex_config.path)); |
| 140 | DO(ParseJson(value["publicKey"], apex_config.public_key)); |
| 141 | DO(ParseJson(value["rootDigest"], apex_config.root_digest)); |
| 142 | return {}; |
| 143 | } |
| 144 | |
Jooyung Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 145 | Result<void> ParseJson(const Json::Value& value, ApkConfig& apk_config) { |
| 146 | DO(ParseJson(value["name"], apk_config.name)); |
| 147 | DO(ParseJson(value["path"], apk_config.path)); |
Jooyung Han | 7ce2e53 | 2021-06-16 16:52:02 +0900 | [diff] [blame^] | 148 | DO(ParseJson(value["idsig_path"], apk_config.idsig_path)); |
Jooyung Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 149 | return {}; |
| 150 | } |
| 151 | |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 152 | Result<void> ParseJson(const Json::Value& value, Config& config) { |
| 153 | DO(ParseJson(value["system_apexes"], config.system_apexes)); |
| 154 | DO(ParseJson(value["apexes"], config.apexes)); |
Jooyung Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 155 | DO(ParseJson(value["apk"], config.apk)); |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 156 | DO(ParseJson(value["payload_config_path"], config.payload_config_path)); |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 157 | return {}; |
| 158 | } |
| 159 | |
| 160 | Result<Config> LoadConfig(const std::string& config_file) { |
| 161 | std::ifstream in(config_file); |
| 162 | Json::CharReaderBuilder builder; |
| 163 | Json::Value root; |
| 164 | Json::String errs; |
| 165 | if (!parseFromStream(builder, in, &root, &errs)) { |
| 166 | return Error() << "bad config: " << errs; |
| 167 | } |
| 168 | |
| 169 | Config config; |
| 170 | config.dirname = Dirname(config_file); |
| 171 | DO(ParseJson(root, config)); |
| 172 | return config; |
| 173 | } |
| 174 | |
| 175 | #undef DO |
| 176 | |
| 177 | Result<void> LoadSystemApexes(Config& config) { |
| 178 | static const char* kApexInfoListFile = "/apex/apex-info-list.xml"; |
| 179 | std::optional<ApexInfoList> apex_info_list = readApexInfoList(kApexInfoListFile); |
| 180 | if (!apex_info_list.has_value()) { |
| 181 | return Error() << "Failed to read " << kApexInfoListFile; |
| 182 | } |
| 183 | auto get_apex_path = [&](const std::string& apex_name) -> std::optional<std::string> { |
| 184 | for (const auto& apex_info : apex_info_list->getApexInfo()) { |
| 185 | if (apex_info.getIsActive() && apex_info.getModuleName() == apex_name) { |
| 186 | return apex_info.getModulePath(); |
| 187 | } |
| 188 | } |
| 189 | return std::nullopt; |
| 190 | }; |
| 191 | for (const auto& apex_name : config.system_apexes) { |
| 192 | const auto& apex_path = get_apex_path(apex_name); |
| 193 | if (!apex_path.has_value()) { |
| 194 | return Error() << "Can't find the system apex: " << apex_name; |
| 195 | } |
| 196 | config.apexes.push_back(ApexConfig{ |
| 197 | .name = apex_name, |
| 198 | .path = *apex_path, |
| 199 | .public_key = std::nullopt, |
| 200 | .root_digest = std::nullopt, |
| 201 | }); |
| 202 | } |
| 203 | return {}; |
| 204 | } |
| 205 | |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 206 | Result<void> MakeMetadata(const Config& config, const std::string& filename) { |
| 207 | Metadata metadata; |
| 208 | metadata.set_version(1); |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 209 | |
| 210 | for (const auto& apex_config : config.apexes) { |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 211 | auto* apex = metadata.add_apexes(); |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 212 | |
| 213 | // name |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 214 | apex->set_name(apex_config.name); |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 215 | |
| 216 | // publicKey |
| 217 | if (apex_config.public_key.has_value()) { |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 218 | apex->set_publickey(apex_config.public_key.value()); |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | // rootDigest |
| 222 | if (apex_config.root_digest.has_value()) { |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 223 | apex->set_rootdigest(apex_config.root_digest.value()); |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Jooyung Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 227 | if (config.apk.has_value()) { |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 228 | auto* apk = metadata.mutable_apk(); |
| 229 | apk->set_name(config.apk->name); |
| 230 | apk->set_payload_partition_name("microdroid-apk"); |
Jooyung Han | 7ce2e53 | 2021-06-16 16:52:02 +0900 | [diff] [blame^] | 231 | if (config.apk->idsig_path.has_value()) { |
| 232 | apk->set_idsig_partition_name("microdroid-apk-idsig"); |
| 233 | } |
Jooyung Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 234 | } |
| 235 | |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 236 | if (config.payload_config_path.has_value()) { |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 237 | *metadata.mutable_payload_config_path() = config.payload_config_path.value(); |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 238 | } |
| 239 | |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 240 | std::ofstream out(filename); |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 241 | return WriteMetadata(metadata, out); |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 242 | } |
| 243 | |
Jooyung Han | 7ce2e53 | 2021-06-16 16:52:02 +0900 | [diff] [blame^] | 244 | // fill (zeros + original file's size) with aligning BLOCK_SIZE(4096) boundary |
| 245 | // return true when the filler is generated. |
| 246 | Result<bool> SizeFiller(const std::string& file_path, const std::string& filler_path) { |
Jooyung Han | a54dcaf | 2021-05-13 21:57:02 +0900 | [diff] [blame] | 247 | auto file_size = GetFileSize(file_path); |
| 248 | if (!file_size.ok()) { |
| 249 | return file_size.error(); |
| 250 | } |
| 251 | auto disk_size = AlignToPartitionSize(*file_size + sizeof(uint32_t)); |
| 252 | |
| 253 | unique_fd fd(TEMP_FAILURE_RETRY(open(filler_path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0600))); |
| 254 | if (fd.get() == -1) { |
| 255 | return ErrnoError() << "open(" << filler_path << ") failed."; |
| 256 | } |
| 257 | uint32_t size = htobe32(static_cast<uint32_t>(*file_size)); |
| 258 | if (ftruncate(fd.get(), disk_size - *file_size) == -1) { |
| 259 | return ErrnoError() << "ftruncate(" << filler_path << ") failed."; |
| 260 | } |
| 261 | if (lseek(fd.get(), -sizeof(size), SEEK_END) == -1) { |
| 262 | return ErrnoError() << "lseek(" << filler_path << ") failed."; |
| 263 | } |
| 264 | if (write(fd.get(), &size, sizeof(size)) <= 0) { |
| 265 | return ErrnoError() << "write(" << filler_path << ") failed."; |
| 266 | } |
Jooyung Han | 7ce2e53 | 2021-06-16 16:52:02 +0900 | [diff] [blame^] | 267 | return true; |
Jooyung Han | a54dcaf | 2021-05-13 21:57:02 +0900 | [diff] [blame] | 268 | } |
| 269 | |
Jooyung Han | 7ce2e53 | 2021-06-16 16:52:02 +0900 | [diff] [blame^] | 270 | // fill zeros to align |file_path|'s size to BLOCK_SIZE(4096) boundary. |
| 271 | // return true when the filler is generated. |
| 272 | Result<bool> ZeroFiller(const std::string& file_path, const std::string& filler_path) { |
| 273 | auto file_size = GetFileSize(file_path); |
| 274 | if (!file_size.ok()) { |
| 275 | return file_size.error(); |
| 276 | } |
| 277 | auto disk_size = AlignToPartitionSize(*file_size); |
| 278 | if (disk_size <= *file_size) { |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | unique_fd fd(TEMP_FAILURE_RETRY(open(filler_path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0600))); |
| 283 | if (fd.get() == -1) { |
| 284 | return ErrnoError() << "open(" << filler_path << ") failed."; |
| 285 | } |
| 286 | if (!android::base::WriteStringToFd(std::string(disk_size - *file_size, '\0'), fd)) { |
| 287 | return ErrnoError() << "write(" << filler_path << ") failed."; |
| 288 | } |
| 289 | return true; |
| 290 | } |
| 291 | |
| 292 | // Do not generate any fillers |
| 293 | // Note that CreateCompositeDisk() handles gaps between partitions. |
| 294 | Result<bool> NoFiller(const std::string& file_path, const std::string& filler_path) { |
| 295 | (void)file_path; |
| 296 | (void)filler_path; |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | // fill zeros to align |file_path|'s size to BLOCK_SIZE(4096) boundary. |
| 301 | // return true when the filler is generated. |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 302 | Result<void> MakePayload(const Config& config, const std::string& metadata_file, |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 303 | const std::string& output_file) { |
Jooyung Han | a54dcaf | 2021-05-13 21:57:02 +0900 | [diff] [blame] | 304 | std::vector<MultipleImagePartition> partitions; |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 305 | |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 306 | // put metadata at the first partition |
Jooyung Han | a54dcaf | 2021-05-13 21:57:02 +0900 | [diff] [blame] | 307 | partitions.push_back(MultipleImagePartition{ |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 308 | .label = "metadata", |
| 309 | .image_file_paths = {metadata_file}, |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 310 | .type = kLinuxFilesystem, |
| 311 | .read_only = true, |
| 312 | }); |
| 313 | |
Jooyung Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 314 | int filler_count = 0; |
Jooyung Han | 7ce2e53 | 2021-06-16 16:52:02 +0900 | [diff] [blame^] | 315 | auto add_partition = [&](auto partition_name, auto file_path, auto filler) -> Result<void> { |
| 316 | std::vector<std::string> image_files{file_path}; |
| 317 | |
Jooyung Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 318 | std::string filler_path = output_file + "." + std::to_string(filler_count++); |
Jooyung Han | 7ce2e53 | 2021-06-16 16:52:02 +0900 | [diff] [blame^] | 319 | if (auto ret = filler(file_path, filler_path); !ret.ok()) { |
Jooyung Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 320 | return ret.error(); |
Jooyung Han | 7ce2e53 | 2021-06-16 16:52:02 +0900 | [diff] [blame^] | 321 | } else if (*ret) { |
| 322 | image_files.push_back(filler_path); |
Jooyung Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 323 | } |
| 324 | partitions.push_back(MultipleImagePartition{ |
| 325 | .label = partition_name, |
Jooyung Han | 7ce2e53 | 2021-06-16 16:52:02 +0900 | [diff] [blame^] | 326 | .image_file_paths = image_files, |
Jooyung Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 327 | .type = kLinuxFilesystem, |
| 328 | .read_only = true, |
| 329 | }); |
| 330 | return {}; |
| 331 | }; |
| 332 | |
Jooyung Han | a54dcaf | 2021-05-13 21:57:02 +0900 | [diff] [blame] | 333 | // put apexes at the subsequent partitions with "size" filler |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 334 | for (size_t i = 0; i < config.apexes.size(); i++) { |
| 335 | const auto& apex_config = config.apexes[i]; |
Jooyung Han | a54dcaf | 2021-05-13 21:57:02 +0900 | [diff] [blame] | 336 | std::string apex_path = ToAbsolute(apex_config.path, config.dirname); |
Jooyung Han | 7ce2e53 | 2021-06-16 16:52:02 +0900 | [diff] [blame^] | 337 | if (auto ret = add_partition("microdroid-apex-" + std::to_string(i), apex_path, SizeFiller); |
Jooyung Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 338 | !ret.ok()) { |
Jooyung Han | a54dcaf | 2021-05-13 21:57:02 +0900 | [diff] [blame] | 339 | return ret.error(); |
| 340 | } |
Jooyung Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 341 | } |
Jooyung Han | 7ce2e53 | 2021-06-16 16:52:02 +0900 | [diff] [blame^] | 342 | // put apk with "zero" filler. |
Jooyung Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 343 | // TODO(jooyung): partition name("microdroid-apk") is TBD |
| 344 | if (config.apk.has_value()) { |
| 345 | std::string apk_path = ToAbsolute(config.apk->path, config.dirname); |
Jooyung Han | 7ce2e53 | 2021-06-16 16:52:02 +0900 | [diff] [blame^] | 346 | if (auto ret = add_partition("microdroid-apk", apk_path, ZeroFiller); !ret.ok()) { |
Jooyung Han | 57d895e | 2021-05-13 21:58:41 +0900 | [diff] [blame] | 347 | return ret.error(); |
| 348 | } |
Jooyung Han | 7ce2e53 | 2021-06-16 16:52:02 +0900 | [diff] [blame^] | 349 | if (config.apk->idsig_path.has_value()) { |
| 350 | std::string idsig_path = ToAbsolute(config.apk->idsig_path.value(), config.dirname); |
| 351 | if (auto ret = add_partition("microdroid-apk-idsig", idsig_path, NoFiller); !ret.ok()) { |
| 352 | return ret.error(); |
| 353 | } |
| 354 | } |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | const std::string gpt_header = AppendFileName(output_file, "-header"); |
| 358 | const std::string gpt_footer = AppendFileName(output_file, "-footer"); |
| 359 | CreateCompositeDisk(partitions, gpt_header, gpt_footer, output_file); |
| 360 | return {}; |
| 361 | } |
| 362 | |
| 363 | int main(int argc, char** argv) { |
| 364 | if (argc != 3) { |
| 365 | std::cerr << "Usage: " << argv[0] << " <config> <output>\n"; |
| 366 | return 1; |
| 367 | } |
| 368 | |
| 369 | auto config = LoadConfig(argv[1]); |
| 370 | if (!config.ok()) { |
| 371 | std::cerr << config.error() << '\n'; |
| 372 | return 1; |
| 373 | } |
| 374 | |
| 375 | if (const auto res = LoadSystemApexes(*config); !res.ok()) { |
| 376 | std::cerr << res.error() << '\n'; |
| 377 | return 1; |
| 378 | } |
| 379 | |
| 380 | const std::string output_file(argv[2]); |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 381 | const std::string metadata_file = AppendFileName(output_file, "-metadata"); |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 382 | |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 383 | if (const auto res = MakeMetadata(*config, metadata_file); !res.ok()) { |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 384 | std::cerr << res.error() << '\n'; |
| 385 | return 1; |
| 386 | } |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 387 | if (const auto res = MakePayload(*config, metadata_file, output_file); !res.ok()) { |
Jooyung Han | 017916b | 2021-04-20 03:57:19 +0900 | [diff] [blame] | 388 | std::cerr << res.error() << '\n'; |
| 389 | return 1; |
| 390 | } |
| 391 | |
| 392 | return 0; |
| 393 | } |