1 //
2 // Copyright (C) 2010 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 <string>
18 #include <vector>
19
20 #include <base/files/file_path.h>
21 #include <base/files/file_util.h>
22 #include <base/logging.h>
23 #include <base/strings/string_number_conversions.h>
24 #include <base/strings/string_split.h>
25 #include <brillo/flag_helper.h>
26 #include <brillo/key_value_store.h>
27 #include <brillo/message_loops/base_message_loop.h>
28 #include <xz.h>
29
30 #include "update_engine/common/fake_boot_control.h"
31 #include "update_engine/common/fake_hardware.h"
32 #include "update_engine/common/file_fetcher.h"
33 #include "update_engine/common/prefs.h"
34 #include "update_engine/common/terminator.h"
35 #include "update_engine/common/utils.h"
36 #include "update_engine/payload_consumer/download_action.h"
37 #include "update_engine/payload_consumer/filesystem_verifier_action.h"
38 #include "update_engine/payload_consumer/payload_constants.h"
39 #include "update_engine/payload_generator/delta_diff_generator.h"
40 #include "update_engine/payload_generator/payload_generation_config.h"
41 #include "update_engine/payload_generator/payload_signer.h"
42 #include "update_engine/payload_generator/xz.h"
43 #include "update_engine/update_metadata.pb.h"
44
45 // This file contains a simple program that takes an old path, a new path,
46 // and an output file as arguments and the path to an output file and
47 // generates a delta that can be sent to Chrome OS clients.
48
49 using std::string;
50 using std::vector;
51
52 namespace chromeos_update_engine {
53
54 namespace {
55
ParseSignatureSizes(const string & signature_sizes_flag,vector<int> * signature_sizes)56 void ParseSignatureSizes(const string& signature_sizes_flag,
57 vector<int>* signature_sizes) {
58 signature_sizes->clear();
59 vector<string> split_strings = base::SplitString(
60 signature_sizes_flag, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
61 for (const string& str : split_strings) {
62 int size = 0;
63 bool parsing_successful = base::StringToInt(str, &size);
64 LOG_IF(FATAL, !parsing_successful) << "Invalid signature size: " << str;
65
66 LOG_IF(FATAL, size != 256 && size != 512)
67 << "Only signature sizes of 256 or 512 bytes are supported.";
68
69 signature_sizes->push_back(size);
70 }
71 }
72
ParseImageInfo(const string & channel,const string & board,const string & version,const string & key,const string & build_channel,const string & build_version,ImageInfo * image_info)73 bool ParseImageInfo(const string& channel,
74 const string& board,
75 const string& version,
76 const string& key,
77 const string& build_channel,
78 const string& build_version,
79 ImageInfo* image_info) {
80 // All of these arguments should be present or missing.
81 bool empty = channel.empty();
82
83 CHECK_EQ(channel.empty(), empty);
84 CHECK_EQ(board.empty(), empty);
85 CHECK_EQ(version.empty(), empty);
86 CHECK_EQ(key.empty(), empty);
87
88 if (empty)
89 return false;
90
91 image_info->set_channel(channel);
92 image_info->set_board(board);
93 image_info->set_version(version);
94 image_info->set_key(key);
95
96 image_info->set_build_channel(build_channel.empty() ? channel
97 : build_channel);
98
99 image_info->set_build_version(build_version.empty() ? version
100 : build_version);
101
102 return true;
103 }
104
CalculateHashForSigning(const vector<int> & sizes,const string & out_hash_file,const string & out_metadata_hash_file,const string & in_file)105 void CalculateHashForSigning(const vector<int>& sizes,
106 const string& out_hash_file,
107 const string& out_metadata_hash_file,
108 const string& in_file) {
109 LOG(INFO) << "Calculating hash for signing.";
110 LOG_IF(FATAL, in_file.empty())
111 << "Must pass --in_file to calculate hash for signing.";
112 LOG_IF(FATAL, out_hash_file.empty())
113 << "Must pass --out_hash_file to calculate hash for signing.";
114
115 brillo::Blob payload_hash, metadata_hash;
116 CHECK(PayloadSigner::HashPayloadForSigning(
117 in_file, sizes, &payload_hash, &metadata_hash));
118 CHECK(utils::WriteFile(
119 out_hash_file.c_str(), payload_hash.data(), payload_hash.size()));
120 if (!out_metadata_hash_file.empty())
121 CHECK(utils::WriteFile(out_metadata_hash_file.c_str(),
122 metadata_hash.data(),
123 metadata_hash.size()));
124
125 LOG(INFO) << "Done calculating hash for signing.";
126 }
127
SignatureFileFlagToBlobs(const string & signature_file_flag,vector<brillo::Blob> * signatures)128 void SignatureFileFlagToBlobs(const string& signature_file_flag,
129 vector<brillo::Blob>* signatures) {
130 vector<string> signature_files = base::SplitString(
131 signature_file_flag, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
132 for (const string& signature_file : signature_files) {
133 brillo::Blob signature;
134 CHECK(utils::ReadFile(signature_file, &signature));
135 signatures->push_back(signature);
136 }
137 }
138
SignPayload(const string & in_file,const string & out_file,const string & payload_signature_file,const string & metadata_signature_file,const string & out_metadata_size_file)139 void SignPayload(const string& in_file,
140 const string& out_file,
141 const string& payload_signature_file,
142 const string& metadata_signature_file,
143 const string& out_metadata_size_file) {
144 LOG(INFO) << "Signing payload.";
145 LOG_IF(FATAL, in_file.empty()) << "Must pass --in_file to sign payload.";
146 LOG_IF(FATAL, out_file.empty()) << "Must pass --out_file to sign payload.";
147 LOG_IF(FATAL, payload_signature_file.empty())
148 << "Must pass --payload_signature_file to sign payload.";
149 vector<brillo::Blob> payload_signatures, metadata_signatures;
150 SignatureFileFlagToBlobs(payload_signature_file, &payload_signatures);
151 SignatureFileFlagToBlobs(metadata_signature_file, &metadata_signatures);
152 uint64_t final_metadata_size;
153 CHECK(PayloadSigner::AddSignatureToPayload(in_file,
154 payload_signatures,
155 metadata_signatures,
156 out_file,
157 &final_metadata_size));
158 LOG(INFO) << "Done signing payload. Final metadata size = "
159 << final_metadata_size;
160 if (!out_metadata_size_file.empty()) {
161 string metadata_size_string = std::to_string(final_metadata_size);
162 CHECK(utils::WriteFile(out_metadata_size_file.c_str(),
163 metadata_size_string.data(),
164 metadata_size_string.size()));
165 }
166 }
167
VerifySignedPayload(const string & in_file,const string & public_key)168 int VerifySignedPayload(const string& in_file, const string& public_key) {
169 LOG(INFO) << "Verifying signed payload.";
170 LOG_IF(FATAL, in_file.empty())
171 << "Must pass --in_file to verify signed payload.";
172 LOG_IF(FATAL, public_key.empty())
173 << "Must pass --public_key to verify signed payload.";
174 if (!PayloadSigner::VerifySignedPayload(in_file, public_key)) {
175 LOG(INFO) << "VerifySignedPayload failed";
176 return 1;
177 }
178
179 LOG(INFO) << "Done verifying signed payload.";
180 return 0;
181 }
182
183 class ApplyPayloadProcessorDelegate : public ActionProcessorDelegate {
184 public:
ProcessingDone(const ActionProcessor * processor,ErrorCode code)185 void ProcessingDone(const ActionProcessor* processor,
186 ErrorCode code) override {
187 brillo::MessageLoop::current()->BreakLoop();
188 code_ = code;
189 }
ProcessingStopped(const ActionProcessor * processor)190 void ProcessingStopped(const ActionProcessor* processor) override {
191 brillo::MessageLoop::current()->BreakLoop();
192 }
193 ErrorCode code_;
194 };
195
196 // TODO(deymo): Move this function to a new file and make the delta_performer
197 // integration tests use this instead.
ApplyPayload(const string & payload_file,const PayloadGenerationConfig & config)198 bool ApplyPayload(const string& payload_file,
199 // Simply reuses the payload config used for payload
200 // generation.
201 const PayloadGenerationConfig& config) {
202 LOG(INFO) << "Applying delta.";
203 FakeBootControl fake_boot_control;
204 FakeHardware fake_hardware;
205 MemoryPrefs prefs;
206 InstallPlan install_plan;
207 InstallPlan::Payload payload;
208 install_plan.source_slot =
209 config.is_delta ? 0 : BootControlInterface::kInvalidSlot;
210 install_plan.target_slot = 1;
211 payload.type =
212 config.is_delta ? InstallPayloadType::kDelta : InstallPayloadType::kFull;
213 payload.size = utils::FileSize(payload_file);
214 // TODO(senj): This hash is only correct for unsigned payload, need to support
215 // signed payload using PayloadSigner.
216 HashCalculator::RawHashOfFile(payload_file, payload.size, &payload.hash);
217 install_plan.payloads = {payload};
218 install_plan.download_url =
219 "file://" +
220 base::MakeAbsoluteFilePath(base::FilePath(payload_file)).value();
221
222 for (size_t i = 0; i < config.target.partitions.size(); i++) {
223 const string& part_name = config.target.partitions[i].name;
224 const string& target_path = config.target.partitions[i].path;
225 fake_boot_control.SetPartitionDevice(
226 part_name, install_plan.target_slot, target_path);
227
228 string source_path;
229 if (config.is_delta) {
230 TEST_AND_RETURN_FALSE(config.target.partitions.size() ==
231 config.source.partitions.size());
232 source_path = config.source.partitions[i].path;
233 fake_boot_control.SetPartitionDevice(
234 part_name, install_plan.source_slot, source_path);
235 }
236
237 LOG(INFO) << "Install partition:"
238 << " source: " << source_path << "\ttarget: " << target_path;
239 }
240
241 xz_crc32_init();
242 brillo::BaseMessageLoop loop;
243 loop.SetAsCurrent();
244 auto install_plan_action = std::make_unique<InstallPlanAction>(install_plan);
245 auto download_action =
246 std::make_unique<DownloadAction>(&prefs,
247 &fake_boot_control,
248 &fake_hardware,
249 nullptr,
250 new FileFetcher(),
251 true /* interactive */);
252 auto filesystem_verifier_action =
253 std::make_unique<FilesystemVerifierAction>();
254
255 BondActions(install_plan_action.get(), download_action.get());
256 BondActions(download_action.get(), filesystem_verifier_action.get());
257 ActionProcessor processor;
258 ApplyPayloadProcessorDelegate delegate;
259 processor.set_delegate(&delegate);
260 processor.EnqueueAction(std::move(install_plan_action));
261 processor.EnqueueAction(std::move(download_action));
262 processor.EnqueueAction(std::move(filesystem_verifier_action));
263 processor.StartProcessing();
264 loop.Run();
265 CHECK_EQ(delegate.code_, ErrorCode::kSuccess);
266 LOG(INFO) << "Completed applying " << (config.is_delta ? "delta" : "full")
267 << " payload.";
268 return true;
269 }
270
ExtractProperties(const string & payload_path,const string & props_file)271 int ExtractProperties(const string& payload_path, const string& props_file) {
272 brillo::KeyValueStore properties;
273 TEST_AND_RETURN_FALSE(
274 PayloadSigner::ExtractPayloadProperties(payload_path, &properties));
275 if (props_file == "-") {
276 printf("%s", properties.SaveToString().c_str());
277 } else {
278 properties.Save(base::FilePath(props_file));
279 LOG(INFO) << "Generated properties file at " << props_file;
280 }
281 return true;
282 }
283
Main(int argc,char ** argv)284 int Main(int argc, char** argv) {
285 DEFINE_string(old_image, "", "Path to the old rootfs");
286 DEFINE_string(new_image, "", "Path to the new rootfs");
287 DEFINE_string(old_kernel, "", "Path to the old kernel partition image");
288 DEFINE_string(new_kernel, "", "Path to the new kernel partition image");
289 DEFINE_string(old_partitions,
290 "",
291 "Path to the old partitions. To pass multiple partitions, use "
292 "a single argument with a colon between paths, e.g. "
293 "/path/to/part:/path/to/part2::/path/to/last_part . Path can "
294 "be empty, but it has to match the order of partition_names.");
295 DEFINE_string(new_partitions,
296 "",
297 "Path to the new partitions. To pass multiple partitions, use "
298 "a single argument with a colon between paths, e.g. "
299 "/path/to/part:/path/to/part2:/path/to/last_part . Path has "
300 "to match the order of partition_names.");
301 DEFINE_string(old_mapfiles,
302 "",
303 "Path to the .map files associated with the partition files "
304 "in the old partition. The .map file is normally generated "
305 "when creating the image in Android builds. Only recommended "
306 "for unsupported filesystem. Pass multiple files separated by "
307 "a colon as with -old_partitions.");
308 DEFINE_string(new_mapfiles,
309 "",
310 "Path to the .map files associated with the partition files "
311 "in the new partition, similar to the -old_mapfiles flag.");
312 DEFINE_string(partition_names,
313 string(kPartitionNameRoot) + ":" + kPartitionNameKernel,
314 "Names of the partitions. To pass multiple names, use a single "
315 "argument with a colon between names, e.g. "
316 "name:name2:name3:last_name . Name can not be empty, and it "
317 "has to match the order of partitions.");
318 DEFINE_string(in_file,
319 "",
320 "Path to input delta payload file used to hash/sign payloads "
321 "and apply delta over old_image (for debugging)");
322 DEFINE_string(out_file, "", "Path to output delta payload file");
323 DEFINE_string(out_hash_file, "", "Path to output hash file");
324 DEFINE_string(
325 out_metadata_hash_file, "", "Path to output metadata hash file");
326 DEFINE_string(
327 out_metadata_size_file, "", "Path to output metadata size file");
328 DEFINE_string(private_key, "", "Path to private key in .pem format");
329 DEFINE_string(public_key, "", "Path to public key in .pem format");
330 DEFINE_int32(
331 public_key_version, -1, "DEPRECATED. Key-check version # of client");
332 DEFINE_string(signature_size,
333 "",
334 "Raw signature size used for hash calculation. "
335 "You may pass in multiple sizes by colon separating them. E.g. "
336 "2048:2048:4096 will assume 3 signatures, the first two with "
337 "2048 size and the last 4096.");
338 DEFINE_string(payload_signature_file,
339 "",
340 "Raw signature file to sign payload with. To pass multiple "
341 "signatures, use a single argument with a colon between paths, "
342 "e.g. /path/to/sig:/path/to/next:/path/to/last_sig . Each "
343 "signature will be assigned a client version, starting from "
344 "kSignatureOriginalVersion.");
345 DEFINE_string(metadata_signature_file,
346 "",
347 "Raw signature file with the signature of the metadata hash. "
348 "To pass multiple signatures, use a single argument with a "
349 "colon between paths, "
350 "e.g. /path/to/sig:/path/to/next:/path/to/last_sig .");
351 DEFINE_int32(
352 chunk_size, 200 * 1024 * 1024, "Payload chunk size (-1 for whole files)");
353 DEFINE_uint64(rootfs_partition_size,
354 chromeos_update_engine::kRootFSPartitionSize,
355 "RootFS partition size for the image once installed");
356 DEFINE_uint64(
357 major_version, 2, "The major version of the payload being generated.");
358 DEFINE_int32(minor_version,
359 -1,
360 "The minor version of the payload being generated "
361 "(-1 means autodetect).");
362 DEFINE_string(properties_file,
363 "",
364 "If passed, dumps the payload properties of the payload passed "
365 "in --in_file and exits.");
366 DEFINE_int64(max_timestamp,
367 0,
368 "The maximum timestamp of the OS allowed to apply this "
369 "payload.");
370
371 DEFINE_string(old_channel,
372 "",
373 "The channel for the old image. 'dev-channel', 'npo-channel', "
374 "etc. Ignored, except during delta generation.");
375 DEFINE_string(old_board,
376 "",
377 "The board for the old image. 'x86-mario', 'lumpy', "
378 "etc. Ignored, except during delta generation.");
379 DEFINE_string(
380 old_version, "", "The build version of the old image. 1.2.3, etc.");
381 DEFINE_string(old_key,
382 "",
383 "The key used to sign the old image. 'premp', 'mp', 'mp-v3',"
384 " etc");
385 DEFINE_string(old_build_channel,
386 "",
387 "The channel for the build of the old image. 'dev-channel', "
388 "etc, but will never contain special channels such as "
389 "'npo-channel'. Ignored, except during delta generation.");
390 DEFINE_string(old_build_version,
391 "",
392 "The version of the build containing the old image.");
393
394 DEFINE_string(new_channel,
395 "",
396 "The channel for the new image. 'dev-channel', 'npo-channel', "
397 "etc. Ignored, except during delta generation.");
398 DEFINE_string(new_board,
399 "",
400 "The board for the new image. 'x86-mario', 'lumpy', "
401 "etc. Ignored, except during delta generation.");
402 DEFINE_string(
403 new_version, "", "The build version of the new image. 1.2.3, etc.");
404 DEFINE_string(new_key,
405 "",
406 "The key used to sign the new image. 'premp', 'mp', 'mp-v3',"
407 " etc");
408 DEFINE_string(new_build_channel,
409 "",
410 "The channel for the build of the new image. 'dev-channel', "
411 "etc, but will never contain special channels such as "
412 "'npo-channel'. Ignored, except during delta generation.");
413 DEFINE_string(new_build_version,
414 "",
415 "The version of the build containing the new image.");
416 DEFINE_string(new_postinstall_config_file,
417 "",
418 "A config file specifying postinstall related metadata. "
419 "Only allowed in major version 2 or newer.");
420 DEFINE_string(dynamic_partition_info_file,
421 "",
422 "An info file specifying dynamic partition metadata. "
423 "Only allowed in major version 2 or newer.");
424
425 brillo::FlagHelper::Init(
426 argc,
427 argv,
428 "Generates a payload to provide to ChromeOS' update_engine.\n\n"
429 "This tool can create full payloads and also delta payloads if the src\n"
430 "image is provided. It also provides debugging options to apply, sign\n"
431 "and verify payloads.");
432 Terminator::Init();
433
434 logging::LoggingSettings log_settings;
435 log_settings.log_file = "delta_generator.log";
436 log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
437 log_settings.lock_log = logging::LOCK_LOG_FILE;
438 log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE;
439
440 logging::InitLogging(log_settings);
441
442 // Initialize the Xz compressor.
443 XzCompressInit();
444
445 vector<int> signature_sizes;
446 ParseSignatureSizes(FLAGS_signature_size, &signature_sizes);
447
448 if (!FLAGS_out_hash_file.empty() || !FLAGS_out_metadata_hash_file.empty()) {
449 CHECK(FLAGS_out_metadata_size_file.empty());
450 CalculateHashForSigning(signature_sizes,
451 FLAGS_out_hash_file,
452 FLAGS_out_metadata_hash_file,
453 FLAGS_in_file);
454 return 0;
455 }
456 if (!FLAGS_payload_signature_file.empty()) {
457 SignPayload(FLAGS_in_file,
458 FLAGS_out_file,
459 FLAGS_payload_signature_file,
460 FLAGS_metadata_signature_file,
461 FLAGS_out_metadata_size_file);
462 return 0;
463 }
464 if (!FLAGS_public_key.empty()) {
465 LOG_IF(WARNING, FLAGS_public_key_version != -1)
466 << "--public_key_version is deprecated and ignored.";
467 return VerifySignedPayload(FLAGS_in_file, FLAGS_public_key);
468 }
469 if (!FLAGS_properties_file.empty()) {
470 return ExtractProperties(FLAGS_in_file, FLAGS_properties_file) ? 0 : 1;
471 }
472
473 // A payload generation was requested. Convert the flags to a
474 // PayloadGenerationConfig.
475 PayloadGenerationConfig payload_config;
476 vector<string> partition_names, old_partitions, new_partitions;
477 vector<string> old_mapfiles, new_mapfiles;
478
479 if (!FLAGS_old_mapfiles.empty()) {
480 old_mapfiles = base::SplitString(
481 FLAGS_old_mapfiles, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
482 }
483 if (!FLAGS_new_mapfiles.empty()) {
484 new_mapfiles = base::SplitString(
485 FLAGS_new_mapfiles, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
486 }
487
488 partition_names = base::SplitString(
489 FLAGS_partition_names, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
490 CHECK(!partition_names.empty());
491 if (FLAGS_major_version == kChromeOSMajorPayloadVersion ||
492 FLAGS_new_partitions.empty()) {
493 LOG_IF(FATAL, partition_names.size() != 2)
494 << "To support more than 2 partitions, please use the "
495 << "--new_partitions flag and major version 2.";
496 LOG_IF(FATAL,
497 partition_names[0] != kPartitionNameRoot ||
498 partition_names[1] != kPartitionNameKernel)
499 << "To support non-default partition name, please use the "
500 << "--new_partitions flag and major version 2.";
501 }
502
503 if (!FLAGS_new_partitions.empty()) {
504 LOG_IF(FATAL, !FLAGS_new_image.empty() || !FLAGS_new_kernel.empty())
505 << "--new_image and --new_kernel are deprecated, please use "
506 << "--new_partitions for all partitions.";
507 new_partitions = base::SplitString(
508 FLAGS_new_partitions, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
509 CHECK(partition_names.size() == new_partitions.size());
510
511 payload_config.is_delta = !FLAGS_old_partitions.empty();
512 LOG_IF(FATAL, !FLAGS_old_image.empty() || !FLAGS_old_kernel.empty())
513 << "--old_image and --old_kernel are deprecated, please use "
514 << "--old_partitions if you are using --new_partitions.";
515 } else {
516 new_partitions = {FLAGS_new_image, FLAGS_new_kernel};
517 LOG(WARNING) << "--new_partitions is empty, using deprecated --new_image "
518 << "and --new_kernel flags.";
519
520 payload_config.is_delta =
521 !FLAGS_old_image.empty() || !FLAGS_old_kernel.empty();
522 LOG_IF(FATAL, !FLAGS_old_partitions.empty())
523 << "Please use --new_partitions if you are using --old_partitions.";
524 }
525 for (size_t i = 0; i < partition_names.size(); i++) {
526 LOG_IF(FATAL, partition_names[i].empty())
527 << "Partition name can't be empty, see --partition_names.";
528 payload_config.target.partitions.emplace_back(partition_names[i]);
529 payload_config.target.partitions.back().path = new_partitions[i];
530 if (i < new_mapfiles.size())
531 payload_config.target.partitions.back().mapfile_path = new_mapfiles[i];
532 }
533
534 if (payload_config.is_delta) {
535 if (!FLAGS_old_partitions.empty()) {
536 old_partitions = base::SplitString(FLAGS_old_partitions,
537 ":",
538 base::TRIM_WHITESPACE,
539 base::SPLIT_WANT_ALL);
540 CHECK(old_partitions.size() == new_partitions.size());
541 } else {
542 old_partitions = {FLAGS_old_image, FLAGS_old_kernel};
543 LOG(WARNING) << "--old_partitions is empty, using deprecated --old_image "
544 << "and --old_kernel flags.";
545 }
546 for (size_t i = 0; i < partition_names.size(); i++) {
547 payload_config.source.partitions.emplace_back(partition_names[i]);
548 payload_config.source.partitions.back().path = old_partitions[i];
549 if (i < old_mapfiles.size())
550 payload_config.source.partitions.back().mapfile_path = old_mapfiles[i];
551 }
552 }
553
554 if (!FLAGS_in_file.empty()) {
555 return ApplyPayload(FLAGS_in_file, payload_config) ? 0 : 1;
556 }
557
558 if (!FLAGS_new_postinstall_config_file.empty()) {
559 LOG_IF(FATAL, FLAGS_major_version == kChromeOSMajorPayloadVersion)
560 << "Postinstall config is only allowed in major version 2 or newer.";
561 brillo::KeyValueStore store;
562 CHECK(store.Load(base::FilePath(FLAGS_new_postinstall_config_file)));
563 CHECK(payload_config.target.LoadPostInstallConfig(store));
564 }
565
566 // Use the default soft_chunk_size defined in the config.
567 payload_config.hard_chunk_size = FLAGS_chunk_size;
568 payload_config.block_size = kBlockSize;
569
570 // The partition size is never passed to the delta_generator, so we
571 // need to detect those from the provided files.
572 if (payload_config.is_delta) {
573 CHECK(payload_config.source.LoadImageSize());
574 }
575 CHECK(payload_config.target.LoadImageSize());
576
577 if (!FLAGS_dynamic_partition_info_file.empty()) {
578 LOG_IF(FATAL, FLAGS_major_version == kChromeOSMajorPayloadVersion)
579 << "Dynamic partition info is only allowed in major version 2 or "
580 "newer.";
581 brillo::KeyValueStore store;
582 CHECK(store.Load(base::FilePath(FLAGS_dynamic_partition_info_file)));
583 CHECK(payload_config.target.LoadDynamicPartitionMetadata(store));
584 CHECK(payload_config.target.ValidateDynamicPartitionMetadata());
585 }
586
587 CHECK(!FLAGS_out_file.empty());
588
589 // Ignore failures. These are optional arguments.
590 ParseImageInfo(FLAGS_new_channel,
591 FLAGS_new_board,
592 FLAGS_new_version,
593 FLAGS_new_key,
594 FLAGS_new_build_channel,
595 FLAGS_new_build_version,
596 &payload_config.target.image_info);
597
598 // Ignore failures. These are optional arguments.
599 ParseImageInfo(FLAGS_old_channel,
600 FLAGS_old_board,
601 FLAGS_old_version,
602 FLAGS_old_key,
603 FLAGS_old_build_channel,
604 FLAGS_old_build_version,
605 &payload_config.source.image_info);
606
607 payload_config.rootfs_partition_size = FLAGS_rootfs_partition_size;
608
609 if (payload_config.is_delta) {
610 // Avoid opening the filesystem interface for full payloads.
611 for (PartitionConfig& part : payload_config.target.partitions)
612 CHECK(part.OpenFilesystem());
613 for (PartitionConfig& part : payload_config.source.partitions)
614 CHECK(part.OpenFilesystem());
615 }
616
617 payload_config.version.major = FLAGS_major_version;
618 LOG(INFO) << "Using provided major_version=" << FLAGS_major_version;
619
620 if (FLAGS_minor_version == -1) {
621 // Autodetect minor_version by looking at the update_engine.conf in the old
622 // image.
623 if (payload_config.is_delta) {
624 payload_config.version.minor = kInPlaceMinorPayloadVersion;
625 brillo::KeyValueStore store;
626 uint32_t minor_version;
627 for (const PartitionConfig& part : payload_config.source.partitions) {
628 if (part.fs_interface && part.fs_interface->LoadSettings(&store) &&
629 utils::GetMinorVersion(store, &minor_version)) {
630 payload_config.version.minor = minor_version;
631 break;
632 }
633 }
634 } else {
635 payload_config.version.minor = kFullPayloadMinorVersion;
636 }
637 LOG(INFO) << "Auto-detected minor_version=" << payload_config.version.minor;
638 } else {
639 payload_config.version.minor = FLAGS_minor_version;
640 LOG(INFO) << "Using provided minor_version=" << FLAGS_minor_version;
641 }
642
643 payload_config.max_timestamp = FLAGS_max_timestamp;
644
645 if (payload_config.version.minor >= kVerityMinorPayloadVersion)
646 CHECK(payload_config.target.LoadVerityConfig());
647
648 LOG(INFO) << "Generating " << (payload_config.is_delta ? "delta" : "full")
649 << " update";
650
651 // From this point, all the options have been parsed.
652 if (!payload_config.Validate()) {
653 LOG(ERROR) << "Invalid options passed. See errors above.";
654 return 1;
655 }
656
657 uint64_t metadata_size;
658 if (!GenerateUpdatePayloadFile(
659 payload_config, FLAGS_out_file, FLAGS_private_key, &metadata_size)) {
660 return 1;
661 }
662 if (!FLAGS_out_metadata_size_file.empty()) {
663 string metadata_size_string = std::to_string(metadata_size);
664 CHECK(utils::WriteFile(FLAGS_out_metadata_size_file.c_str(),
665 metadata_size_string.data(),
666 metadata_size_string.size()));
667 }
668 return 0;
669 }
670
671 } // namespace
672
673 } // namespace chromeos_update_engine
674
main(int argc,char ** argv)675 int main(int argc, char** argv) {
676 return chromeos_update_engine::Main(argc, argv);
677 }
678