blob: 70df2a09efffcc644b5964801a1df4778e6ae72b [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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//
adlr@google.com3defe6a2009-12-04 20:57:17 +000016
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080017#include <errno.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070018#include <fcntl.h>
Alex Deymo161c4a12014-05-16 15:56:21 -070019#include <sys/stat.h>
20#include <sys/types.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000021#include <unistd.h>
Darin Petkov73058b42010-10-06 16:32:19 -070022
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080023#include <set>
adlr@google.com3defe6a2009-12-04 20:57:17 +000024#include <string>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070025#include <vector>
Darin Petkov73058b42010-10-06 16:32:19 -070026
Darin Petkov73058b42010-10-06 16:32:19 -070027#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070028#include <base/strings/string_number_conversions.h>
29#include <base/strings/string_split.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070030#include <brillo/flag_helper.h>
Sen Jiang05feee02015-11-11 15:59:49 -080031#include <brillo/key_value_store.h>
Darin Petkov73058b42010-10-06 16:32:19 -070032
Alex Deymo39910dc2015-11-09 17:04:30 -080033#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/delta_performer.h"
37#include "update_engine/payload_consumer/payload_constants.h"
Alex Deymo161c4a12014-05-16 15:56:21 -070038#include "update_engine/payload_generator/delta_diff_generator.h"
Alex Deymo14158572015-06-13 03:37:08 -070039#include "update_engine/payload_generator/delta_diff_utils.h"
Alex Deymof1cbe172015-03-05 15:58:37 -080040#include "update_engine/payload_generator/payload_generation_config.h"
Alex Deymo923d8fa2014-07-15 17:58:51 -070041#include "update_engine/payload_generator/payload_signer.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000042#include "update_engine/update_metadata.pb.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000043
44// This file contains a simple program that takes an old path, a new path,
45// and an output file as arguments and the path to an output file and
46// generates a delta that can be sent to Chrome OS clients.
47
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080048using std::set;
49using std::string;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070050using std::vector;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080051
adlr@google.com3defe6a2009-12-04 20:57:17 +000052namespace chromeos_update_engine {
53
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080054namespace {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080055
Don Garrettc2e9f5f2013-10-18 16:42:40 -070056void ParseSignatureSizes(const string& signature_sizes_flag,
57 vector<int>* signature_sizes) {
58 signature_sizes->clear();
Alex Vakulenko0103c362016-01-20 07:56:15 -080059 vector<string> split_strings =
60 base::SplitString(signature_sizes_flag, ":", base::TRIM_WHITESPACE,
61 base::SPLIT_WANT_ALL);
Alex Deymo020600d2014-11-05 21:05:55 -080062 for (const string& str : split_strings) {
Jay Srinivasanf4318702012-09-24 11:56:24 -070063 int size = 0;
Alex Deymo020600d2014-11-05 21:05:55 -080064 bool parsing_successful = base::StringToInt(str, &size);
Jay Srinivasanf4318702012-09-24 11:56:24 -070065 LOG_IF(FATAL, !parsing_successful)
Alex Deymo020600d2014-11-05 21:05:55 -080066 << "Invalid signature size: " << str;
Don Garrettc2e9f5f2013-10-18 16:42:40 -070067
68 LOG_IF(FATAL, size != (2048 / 8)) <<
69 "Only signature sizes of 256 bytes are supported.";
70
71 signature_sizes->push_back(size);
Jay Srinivasanf4318702012-09-24 11:56:24 -070072 }
73}
74
Don Garrett0dd39852013-04-03 16:55:42 -070075bool ParseImageInfo(const string& channel,
76 const string& board,
77 const string& version,
78 const string& key,
79 const string& build_channel,
80 const string& build_version,
81 ImageInfo* image_info) {
Don Garrett0dd39852013-04-03 16:55:42 -070082 // All of these arguments should be present or missing.
83 bool empty = channel.empty();
84
85 CHECK_EQ(channel.empty(), empty);
86 CHECK_EQ(board.empty(), empty);
87 CHECK_EQ(version.empty(), empty);
88 CHECK_EQ(key.empty(), empty);
89
90 if (empty)
91 return false;
92
93 image_info->set_channel(channel);
94 image_info->set_board(board);
95 image_info->set_version(version);
96 image_info->set_key(key);
97
98 image_info->set_build_channel(
99 build_channel.empty() ? channel : build_channel);
100
101 image_info->set_build_version(
102 build_version.empty() ? version : build_version);
103
104 return true;
105}
106
Sen Jiang720df3e2015-10-01 13:10:44 -0700107void CalculateHashForSigning(const vector<int> &sizes,
108 const string& out_hash_file,
109 const string& out_metadata_hash_file,
110 const string& in_file) {
111 LOG(INFO) << "Calculating hash for signing.";
Steve Fung97b6f5a2014-10-07 12:39:51 -0700112 LOG_IF(FATAL, in_file.empty())
Darin Petkovda8c1362011-01-13 14:04:24 -0800113 << "Must pass --in_file to calculate hash for signing.";
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700114 LOG_IF(FATAL, out_hash_file.empty())
Darin Petkovda8c1362011-01-13 14:04:24 -0800115 << "Must pass --out_hash_file to calculate hash for signing.";
Jay Srinivasanf4318702012-09-24 11:56:24 -0700116
Sen Jiang720df3e2015-10-01 13:10:44 -0700117 brillo::Blob payload_hash, metadata_hash;
118 CHECK(PayloadSigner::HashPayloadForSigning(in_file, sizes, &payload_hash,
119 &metadata_hash));
120 CHECK(utils::WriteFile(out_hash_file.c_str(), payload_hash.data(),
121 payload_hash.size()));
122 if (!out_metadata_hash_file.empty())
123 CHECK(utils::WriteFile(out_metadata_hash_file.c_str(), metadata_hash.data(),
124 metadata_hash.size()));
Jay Srinivasanf4318702012-09-24 11:56:24 -0700125
Sen Jiang720df3e2015-10-01 13:10:44 -0700126 LOG(INFO) << "Done calculating hash for signing.";
Jay Srinivasanf4318702012-09-24 11:56:24 -0700127}
128
Sen Jiang644f6182015-10-06 16:45:57 -0700129void SignatureFileFlagToBlobs(const string& signature_file_flag,
130 vector<brillo::Blob>* signatures) {
Alex Vakulenko0103c362016-01-20 07:56:15 -0800131 vector<string> signature_files =
132 base::SplitString(signature_file_flag, ":", base::TRIM_WHITESPACE,
133 base::SPLIT_WANT_ALL);
Sen Jiang644f6182015-10-06 16:45:57 -0700134 for (const string& signature_file : signature_files) {
135 brillo::Blob signature;
136 CHECK(utils::ReadFile(signature_file, &signature));
137 signatures->push_back(signature);
138 }
139}
140
Steve Fung97b6f5a2014-10-07 12:39:51 -0700141void SignPayload(const string& in_file,
142 const string& out_file,
Sen Jiang644f6182015-10-06 16:45:57 -0700143 const string& payload_signature_file,
144 const string& metadata_signature_file,
Sen Jiangd8ae8722015-10-08 15:21:09 -0700145 const string& out_metadata_size_file) {
Darin Petkovda8c1362011-01-13 14:04:24 -0800146 LOG(INFO) << "Signing payload.";
Steve Fung97b6f5a2014-10-07 12:39:51 -0700147 LOG_IF(FATAL, in_file.empty())
Darin Petkovda8c1362011-01-13 14:04:24 -0800148 << "Must pass --in_file to sign payload.";
Steve Fung97b6f5a2014-10-07 12:39:51 -0700149 LOG_IF(FATAL, out_file.empty())
Darin Petkovda8c1362011-01-13 14:04:24 -0800150 << "Must pass --out_file to sign payload.";
Sen Jiang644f6182015-10-06 16:45:57 -0700151 LOG_IF(FATAL, payload_signature_file.empty())
Darin Petkovda8c1362011-01-13 14:04:24 -0800152 << "Must pass --signature_file to sign payload.";
Sen Jiang644f6182015-10-06 16:45:57 -0700153 vector<brillo::Blob> signatures, metadata_signatures;
154 SignatureFileFlagToBlobs(payload_signature_file, &signatures);
155 SignatureFileFlagToBlobs(metadata_signature_file, &metadata_signatures);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800156 uint64_t final_metadata_size;
Sen Jiang644f6182015-10-06 16:45:57 -0700157 CHECK(PayloadSigner::AddSignatureToPayload(in_file, signatures,
158 metadata_signatures, out_file, &final_metadata_size));
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800159 LOG(INFO) << "Done signing payload. Final metadata size = "
160 << final_metadata_size;
Sen Jiangd8ae8722015-10-08 15:21:09 -0700161 if (!out_metadata_size_file.empty()) {
162 string metadata_size_string = std::to_string(final_metadata_size);
163 CHECK(utils::WriteFile(out_metadata_size_file.c_str(),
164 metadata_size_string.data(),
165 metadata_size_string.size()));
166 }
Darin Petkovda8c1362011-01-13 14:04:24 -0800167}
168
Steve Fung97b6f5a2014-10-07 12:39:51 -0700169void VerifySignedPayload(const string& in_file,
Alex Deymob552a682015-09-30 09:36:49 -0700170 const string& public_key) {
Darin Petkovadb3cef2011-01-13 16:16:08 -0800171 LOG(INFO) << "Verifying signed payload.";
Steve Fung97b6f5a2014-10-07 12:39:51 -0700172 LOG_IF(FATAL, in_file.empty())
Darin Petkovadb3cef2011-01-13 16:16:08 -0800173 << "Must pass --in_file to verify signed payload.";
Steve Fung97b6f5a2014-10-07 12:39:51 -0700174 LOG_IF(FATAL, public_key.empty())
Darin Petkovadb3cef2011-01-13 16:16:08 -0800175 << "Must pass --public_key to verify signed payload.";
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700176 CHECK(PayloadSigner::VerifySignedPayload(in_file, public_key));
Darin Petkovadb3cef2011-01-13 16:16:08 -0800177 LOG(INFO) << "Done verifying signed payload.";
178}
179
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700180// TODO(deymo): This function is likely broken for deltas minor version 2 or
181// newer. Move this function to a new file and make the delta_performer
182// integration tests use this instead.
Steve Fung97b6f5a2014-10-07 12:39:51 -0700183void ApplyDelta(const string& in_file,
184 const string& old_kernel,
Alex Deymo35589c22015-06-07 17:33:18 +0200185 const string& old_rootfs,
Steve Fung97b6f5a2014-10-07 12:39:51 -0700186 const string& prefs_dir) {
Darin Petkovda8c1362011-01-13 14:04:24 -0800187 LOG(INFO) << "Applying delta.";
Alex Deymo35589c22015-06-07 17:33:18 +0200188 LOG_IF(FATAL, old_rootfs.empty())
Darin Petkovda8c1362011-01-13 14:04:24 -0800189 << "Must pass --old_image to apply delta.";
190 Prefs prefs;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700191 InstallPlan install_plan;
Steve Fung97b6f5a2014-10-07 12:39:51 -0700192 LOG(INFO) << "Setting up preferences under: " << prefs_dir;
193 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Darin Petkovda8c1362011-01-13 14:04:24 -0800194 << "Failed to initialize preferences.";
195 // Get original checksums
196 LOG(INFO) << "Calculating original checksums";
Alex Deymo35589c22015-06-07 17:33:18 +0200197 ImageConfig old_image;
Sen Jiang981eb112015-08-25 17:03:18 -0700198 old_image.partitions.emplace_back(kLegacyPartitionNameRoot);
199 old_image.partitions.back().path = old_rootfs;
200 old_image.partitions.emplace_back(kLegacyPartitionNameKernel);
201 old_image.partitions.back().path = old_kernel;
Alex Deymo35589c22015-06-07 17:33:18 +0200202 CHECK(old_image.LoadImageSize());
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700203 for (const auto& old_part : old_image.partitions) {
204 PartitionInfo part_info;
205 CHECK(diff_utils::InitializePartitionInfo(old_part, &part_info));
206 InstallPlan::Partition part;
207 part.name = old_part.name;
208 part.source_hash.assign(part_info.hash().begin(),
209 part_info.hash().end());
210 part.source_path = old_part.path;
211 // Apply the delta in-place to the old_part.
212 part.target_path = old_part.path;
213 install_plan.partitions.push_back(part);
214 }
215
Alex Deymo542c19b2015-12-03 07:43:31 -0300216 DeltaPerformer performer(&prefs, nullptr, nullptr, nullptr, &install_plan);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700217 brillo::Blob buf(1024 * 1024);
Steve Fung97b6f5a2014-10-07 12:39:51 -0700218 int fd = open(in_file.c_str(), O_RDONLY, 0);
Darin Petkovda8c1362011-01-13 14:04:24 -0800219 CHECK_GE(fd, 0);
220 ScopedFdCloser fd_closer(&fd);
221 for (off_t offset = 0;; offset += buf.size()) {
222 ssize_t bytes_read;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800223 CHECK(utils::PReadAll(fd, buf.data(), buf.size(), offset, &bytes_read));
Darin Petkovda8c1362011-01-13 14:04:24 -0800224 if (bytes_read == 0)
225 break;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800226 CHECK_EQ(performer.Write(buf.data(), bytes_read), bytes_read);
Darin Petkovda8c1362011-01-13 14:04:24 -0800227 }
228 CHECK_EQ(performer.Close(), 0);
229 DeltaPerformer::ResetUpdateProgress(&prefs, false);
230 LOG(INFO) << "Done applying delta.";
231}
232
Alex Deymo98e691c2016-02-04 21:05:45 -0800233int ExtractProperties(const string& payload_path, const string& props_file) {
234 brillo::KeyValueStore properties;
235 TEST_AND_RETURN_FALSE(
236 PayloadSigner::ExtractPayloadProperties(payload_path, &properties));
237 if (props_file == "-") {
238 printf("%s", properties.SaveToString().c_str());
239 } else {
240 properties.Save(base::FilePath(props_file));
241 LOG(INFO) << "Generated properties file at " << props_file;
242 }
243 return true;
244}
245
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800246int Main(int argc, char** argv) {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700247 DEFINE_string(old_image, "", "Path to the old rootfs");
248 DEFINE_string(new_image, "", "Path to the new rootfs");
249 DEFINE_string(old_kernel, "", "Path to the old kernel partition image");
250 DEFINE_string(new_kernel, "", "Path to the new kernel partition image");
Sen Jiang5df68362015-09-22 11:35:51 -0700251 DEFINE_string(old_partitions, "",
252 "Path to the old partitions. To pass multiple partitions, use "
253 "a single argument with a colon between paths, e.g. "
254 "/path/to/part:/path/to/part2::/path/to/last_part . Path can "
255 "be empty, but it has to match the order of partition_names.");
256 DEFINE_string(new_partitions, "",
257 "Path to the new partitions. To pass multiple partitions, use "
258 "a single argument with a colon between paths, e.g. "
259 "/path/to/part:/path/to/part2:/path/to/last_part . Path has "
260 "to match the order of partition_names.");
261 DEFINE_string(partition_names,
262 string(kLegacyPartitionNameRoot) + ":" +
263 kLegacyPartitionNameKernel,
264 "Names of the partitions. To pass multiple names, use a single "
265 "argument with a colon between names, e.g. "
266 "name:name2:name3:last_name . Name can not be empty, and it "
267 "has to match the order of partitions.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700268 DEFINE_string(in_file, "",
269 "Path to input delta payload file used to hash/sign payloads "
270 "and apply delta over old_image (for debugging)");
271 DEFINE_string(out_file, "", "Path to output delta payload file");
272 DEFINE_string(out_hash_file, "", "Path to output hash file");
273 DEFINE_string(out_metadata_hash_file, "",
274 "Path to output metadata hash file");
Sen Jiangd8ae8722015-10-08 15:21:09 -0700275 DEFINE_string(out_metadata_size_file, "",
276 "Path to output metadata size file");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700277 DEFINE_string(private_key, "", "Path to private key in .pem format");
278 DEFINE_string(public_key, "", "Path to public key in .pem format");
Alex Deymob552a682015-09-30 09:36:49 -0700279 DEFINE_int32(public_key_version, -1,
280 "DEPRECATED. Key-check version # of client");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700281 DEFINE_string(prefs_dir, "/tmp/update_engine_prefs",
282 "Preferences directory, used with apply_delta");
283 DEFINE_string(signature_size, "",
284 "Raw signature size used for hash calculation. "
285 "You may pass in multiple sizes by colon separating them. E.g. "
286 "2048:2048:4096 will assume 3 signatures, the first two with "
287 "2048 size and the last 4096.");
288 DEFINE_string(signature_file, "",
289 "Raw signature file to sign payload with. To pass multiple "
290 "signatures, use a single argument with a colon between paths, "
291 "e.g. /path/to/sig:/path/to/next:/path/to/last_sig . Each "
292 "signature will be assigned a client version, starting from "
293 "kSignatureOriginalVersion.");
Alex Deymoef366e12015-09-10 18:06:46 -0700294 DEFINE_string(metadata_signature_file, "",
295 "Raw signature file with the signature of the metadata hash. "
296 "To pass multiple signatures, use a single argument with a "
297 "colon between paths, "
298 "e.g. /path/to/sig:/path/to/next:/path/to/last_sig .");
Alex Deymocfe8c152015-07-17 18:56:37 -0700299 DEFINE_int32(chunk_size, 200 * 1024 * 1024,
300 "Payload chunk size (-1 for whole files)");
Alex Deymo9b244df2015-03-11 21:51:18 -0700301 DEFINE_uint64(rootfs_partition_size,
Steve Fung97b6f5a2014-10-07 12:39:51 -0700302 chromeos_update_engine::kRootFSPartitionSize,
303 "RootFS partition size for the image once installed");
Sen Jiang46e9b172015-08-31 14:11:01 -0700304 DEFINE_uint64(major_version, 1,
305 "The major version of the payload being generated.");
Alex Deymob42b98d2015-07-06 17:42:38 -0700306 DEFINE_int32(minor_version, -1,
307 "The minor version of the payload being generated "
308 "(-1 means autodetect).");
Alex Deymo98e691c2016-02-04 21:05:45 -0800309 DEFINE_string(properties_file, "",
310 "If passed, dumps the payload properties of the payload passed "
311 "in --in_file and exits.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700312
313 DEFINE_string(old_channel, "",
314 "The channel for the old image. 'dev-channel', 'npo-channel', "
315 "etc. Ignored, except during delta generation.");
316 DEFINE_string(old_board, "",
317 "The board for the old image. 'x86-mario', 'lumpy', "
318 "etc. Ignored, except during delta generation.");
319 DEFINE_string(old_version, "",
320 "The build version of the old image. 1.2.3, etc.");
321 DEFINE_string(old_key, "",
322 "The key used to sign the old image. 'premp', 'mp', 'mp-v3',"
323 " etc");
324 DEFINE_string(old_build_channel, "",
325 "The channel for the build of the old image. 'dev-channel', "
326 "etc, but will never contain special channels such as "
327 "'npo-channel'. Ignored, except during delta generation.");
328 DEFINE_string(old_build_version, "",
329 "The version of the build containing the old image.");
330
331 DEFINE_string(new_channel, "",
332 "The channel for the new image. 'dev-channel', 'npo-channel', "
333 "etc. Ignored, except during delta generation.");
334 DEFINE_string(new_board, "",
335 "The board for the new image. 'x86-mario', 'lumpy', "
336 "etc. Ignored, except during delta generation.");
337 DEFINE_string(new_version, "",
338 "The build version of the new image. 1.2.3, etc.");
339 DEFINE_string(new_key, "",
340 "The key used to sign the new image. 'premp', 'mp', 'mp-v3',"
341 " etc");
342 DEFINE_string(new_build_channel, "",
343 "The channel for the build of the new image. 'dev-channel', "
344 "etc, but will never contain special channels such as "
345 "'npo-channel'. Ignored, except during delta generation.");
346 DEFINE_string(new_build_version, "",
347 "The version of the build containing the new image.");
Sen Jiang05feee02015-11-11 15:59:49 -0800348 DEFINE_string(new_postinstall_config_file, "",
349 "A config file specifying postinstall related metadata. "
350 "Only allowed in major version 2 or newer.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700351
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700352 brillo::FlagHelper::Init(argc, argv,
Alex Deymo1f023712014-05-20 19:19:23 -0700353 "Generates a payload to provide to ChromeOS' update_engine.\n\n"
354 "This tool can create full payloads and also delta payloads if the src\n"
355 "image is provided. It also provides debugging options to apply, sign\n"
356 "and verify payloads.");
Darin Petkov9c0baf82010-10-07 13:44:48 -0700357 Terminator::Init();
Alex Vakulenko75039d72014-03-25 12:36:28 -0700358
359 logging::LoggingSettings log_settings;
360 log_settings.log_file = "delta_generator.log";
361 log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
Alex Deymo7fad7b72015-07-21 22:22:47 -0700362 log_settings.lock_log = logging::LOCK_LOG_FILE;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700363 log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700364
365 logging::InitLogging(log_settings);
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700366
367 vector<int> signature_sizes;
368 ParseSignatureSizes(FLAGS_signature_size, &signature_sizes);
369
370 if (!FLAGS_out_hash_file.empty() || !FLAGS_out_metadata_hash_file.empty()) {
Sen Jiangd8ae8722015-10-08 15:21:09 -0700371 CHECK(FLAGS_out_metadata_size_file.empty());
Sen Jiang720df3e2015-10-01 13:10:44 -0700372 CalculateHashForSigning(signature_sizes, FLAGS_out_hash_file,
373 FLAGS_out_metadata_hash_file, FLAGS_in_file);
Darin Petkovda8c1362011-01-13 14:04:24 -0800374 return 0;
375 }
376 if (!FLAGS_signature_file.empty()) {
Sen Jiangd8ae8722015-10-08 15:21:09 -0700377 SignPayload(FLAGS_in_file, FLAGS_out_file, FLAGS_signature_file,
Sen Jiang644f6182015-10-06 16:45:57 -0700378 FLAGS_metadata_signature_file, FLAGS_out_metadata_size_file);
Darin Petkovda8c1362011-01-13 14:04:24 -0800379 return 0;
380 }
Darin Petkovadb3cef2011-01-13 16:16:08 -0800381 if (!FLAGS_public_key.empty()) {
Alex Deymob552a682015-09-30 09:36:49 -0700382 LOG_IF(WARNING, FLAGS_public_key_version != -1)
383 << "--public_key_version is deprecated and ignored.";
384 VerifySignedPayload(FLAGS_in_file, FLAGS_public_key);
Darin Petkovadb3cef2011-01-13 16:16:08 -0800385 return 0;
386 }
Alex Deymo98e691c2016-02-04 21:05:45 -0800387 if (!FLAGS_properties_file.empty()) {
388 return ExtractProperties(FLAGS_in_file, FLAGS_properties_file) ? 0 : 1;
389 }
Darin Petkovda8c1362011-01-13 14:04:24 -0800390 if (!FLAGS_in_file.empty()) {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700391 ApplyDelta(FLAGS_in_file, FLAGS_old_kernel, FLAGS_old_image,
392 FLAGS_prefs_dir);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700393 return 0;
394 }
Alex Deymof1cbe172015-03-05 15:58:37 -0800395
396 // A payload generation was requested. Convert the flags to a
397 // PayloadGenerationConfig.
398 PayloadGenerationConfig payload_config;
Sen Jiang5df68362015-09-22 11:35:51 -0700399 vector<string> partition_names, old_partitions, new_partitions;
Alex Deymof1cbe172015-03-05 15:58:37 -0800400
Alex Vakulenko0103c362016-01-20 07:56:15 -0800401 partition_names =
402 base::SplitString(FLAGS_partition_names, ":", base::TRIM_WHITESPACE,
403 base::SPLIT_WANT_ALL);
Sen Jiang5df68362015-09-22 11:35:51 -0700404 CHECK(!partition_names.empty());
405 if (FLAGS_major_version == kChromeOSMajorPayloadVersion ||
406 FLAGS_new_partitions.empty()) {
407 LOG_IF(FATAL, partition_names.size() != 2)
408 << "To support more than 2 partitions, please use the "
409 << "--new_partitions flag and major version 2.";
410 LOG_IF(FATAL, partition_names[0] != kLegacyPartitionNameRoot ||
411 partition_names[1] != kLegacyPartitionNameKernel)
412 << "To support non-default partition name, please use the "
413 << "--new_partitions flag and major version 2.";
Sen Jiang981eb112015-08-25 17:03:18 -0700414 }
415
Sen Jiang5df68362015-09-22 11:35:51 -0700416 if (!FLAGS_new_partitions.empty()) {
417 LOG_IF(FATAL, !FLAGS_new_image.empty() || !FLAGS_new_kernel.empty())
418 << "--new_image and --new_kernel are deprecated, please use "
419 << "--new_partitions for all partitions.";
Alex Vakulenko0103c362016-01-20 07:56:15 -0800420 new_partitions =
421 base::SplitString(FLAGS_new_partitions, ":", base::TRIM_WHITESPACE,
422 base::SPLIT_WANT_ALL);
Sen Jiang5df68362015-09-22 11:35:51 -0700423 CHECK(partition_names.size() == new_partitions.size());
424
425 payload_config.is_delta = !FLAGS_old_partitions.empty();
426 LOG_IF(FATAL, !FLAGS_old_image.empty() || !FLAGS_old_kernel.empty())
427 << "--old_image and --old_kernel are deprecated, please use "
428 << "--old_partitions if you are using --new_partitions.";
429 } else {
430 new_partitions = {FLAGS_new_image, FLAGS_new_kernel};
431 LOG(WARNING) << "--new_partitions is empty, using deprecated --new_image "
432 << "and --new_kernel flags.";
433
434 payload_config.is_delta = !FLAGS_old_image.empty() ||
435 !FLAGS_old_kernel.empty();
436 LOG_IF(FATAL, !FLAGS_old_partitions.empty())
437 << "Please use --new_partitions if you are using --old_partitions.";
438 }
439 for (size_t i = 0; i < partition_names.size(); i++) {
440 LOG_IF(FATAL, partition_names[i].empty())
441 << "Partition name can't be empty, see --partition_names.";
442 payload_config.target.partitions.emplace_back(partition_names[i]);
443 payload_config.target.partitions.back().path = new_partitions[i];
444 }
445
446 if (payload_config.is_delta) {
447 if (!FLAGS_old_partitions.empty()) {
Alex Vakulenko0103c362016-01-20 07:56:15 -0800448 old_partitions =
449 base::SplitString(FLAGS_old_partitions, ":", base::TRIM_WHITESPACE,
450 base::SPLIT_WANT_ALL);
Sen Jiang5df68362015-09-22 11:35:51 -0700451 CHECK(old_partitions.size() == new_partitions.size());
452 } else {
453 old_partitions = {FLAGS_old_image, FLAGS_old_kernel};
454 LOG(WARNING) << "--old_partitions is empty, using deprecated --old_image "
455 << "and --old_kernel flags.";
456 }
457 for (size_t i = 0; i < partition_names.size(); i++) {
458 payload_config.source.partitions.emplace_back(partition_names[i]);
459 payload_config.source.partitions.back().path = old_partitions[i];
460 }
461 }
Alex Deymof1cbe172015-03-05 15:58:37 -0800462
Sen Jiang05feee02015-11-11 15:59:49 -0800463 if (!FLAGS_new_postinstall_config_file.empty()) {
464 LOG_IF(FATAL, FLAGS_major_version == kChromeOSMajorPayloadVersion)
465 << "Postinstall config is only allowed in major version 2 or newer.";
466 brillo::KeyValueStore store;
467 CHECK(store.Load(base::FilePath(FLAGS_new_postinstall_config_file)));
468 CHECK(payload_config.target.LoadPostInstallConfig(store));
469 }
470
Alex Deymo2d3b2d62015-07-17 17:34:36 -0700471 // Use the default soft_chunk_size defined in the config.
472 payload_config.hard_chunk_size = FLAGS_chunk_size;
Alex Deymof1cbe172015-03-05 15:58:37 -0800473 payload_config.block_size = kBlockSize;
474
Sen Jiang5df68362015-09-22 11:35:51 -0700475 // The partition size is never passed to the delta_generator, so we
Alex Deymof1cbe172015-03-05 15:58:37 -0800476 // need to detect those from the provided files.
Sen Jiang5df68362015-09-22 11:35:51 -0700477 if (payload_config.is_delta) {
Alex Deymof1cbe172015-03-05 15:58:37 -0800478 CHECK(payload_config.source.LoadImageSize());
479 }
Sen Jiang5df68362015-09-22 11:35:51 -0700480 CHECK(payload_config.target.LoadImageSize());
Alex Deymof1cbe172015-03-05 15:58:37 -0800481
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700482 CHECK(!FLAGS_out_file.empty());
Don Garrett0dd39852013-04-03 16:55:42 -0700483
Don Garrett60fc59c2013-10-18 11:43:52 -0700484 // Ignore failures. These are optional arguments.
Don Garrett0dd39852013-04-03 16:55:42 -0700485 ParseImageInfo(FLAGS_new_channel,
486 FLAGS_new_board,
487 FLAGS_new_version,
488 FLAGS_new_key,
489 FLAGS_new_build_channel,
490 FLAGS_new_build_version,
Alex Deymof1cbe172015-03-05 15:58:37 -0800491 &payload_config.target.image_info);
Don Garrett0dd39852013-04-03 16:55:42 -0700492
Don Garrett60fc59c2013-10-18 11:43:52 -0700493 // Ignore failures. These are optional arguments.
494 ParseImageInfo(FLAGS_old_channel,
495 FLAGS_old_board,
496 FLAGS_old_version,
497 FLAGS_old_key,
498 FLAGS_old_build_channel,
499 FLAGS_old_build_version,
Alex Deymof1cbe172015-03-05 15:58:37 -0800500 &payload_config.source.image_info);
Don Garrett0dd39852013-04-03 16:55:42 -0700501
Alex Deymo9b244df2015-03-11 21:51:18 -0700502 payload_config.rootfs_partition_size = FLAGS_rootfs_partition_size;
Allie Woodb8ccad02015-02-09 14:33:40 -0800503
Alex Deymof1cbe172015-03-05 15:58:37 -0800504 if (payload_config.is_delta) {
Alex Deymob42b98d2015-07-06 17:42:38 -0700505 // Avoid opening the filesystem interface for full payloads.
Sen Jiang981eb112015-08-25 17:03:18 -0700506 for (PartitionConfig& part : payload_config.target.partitions)
507 CHECK(part.OpenFilesystem());
508 for (PartitionConfig& part : payload_config.source.partitions)
509 CHECK(part.OpenFilesystem());
Alex Deymob42b98d2015-07-06 17:42:38 -0700510 }
511
Sen Jiang46e9b172015-08-31 14:11:01 -0700512 payload_config.major_version = FLAGS_major_version;
513 LOG(INFO) << "Using provided major_version=" << FLAGS_major_version;
514
Alex Deymob42b98d2015-07-06 17:42:38 -0700515 if (FLAGS_minor_version == -1) {
516 // Autodetect minor_version by looking at the update_engine.conf in the old
517 // image.
518 if (payload_config.is_delta) {
Sen Jiang981eb112015-08-25 17:03:18 -0700519 payload_config.minor_version = kInPlaceMinorPayloadVersion;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700520 brillo::KeyValueStore store;
Alex Deymob42b98d2015-07-06 17:42:38 -0700521 uint32_t minor_version;
Sen Jiang981eb112015-08-25 17:03:18 -0700522 for (const PartitionConfig& part : payload_config.source.partitions) {
523 if (part.fs_interface && part.fs_interface->LoadSettings(&store) &&
524 utils::GetMinorVersion(store, &minor_version)) {
525 payload_config.minor_version = minor_version;
526 break;
527 }
Alex Deymob42b98d2015-07-06 17:42:38 -0700528 }
529 } else {
Alex Deymocbf09892015-09-11 16:13:16 -0700530 payload_config.minor_version = kFullPayloadMinorVersion;
Alex Deymob42b98d2015-07-06 17:42:38 -0700531 }
532 LOG(INFO) << "Auto-detected minor_version=" << payload_config.minor_version;
533 } else {
534 payload_config.minor_version = FLAGS_minor_version;
535 LOG(INFO) << "Using provided minor_version=" << FLAGS_minor_version;
536 }
537
Sen Jiang55c4f9b2016-02-10 11:26:20 -0800538 if (payload_config.minor_version >= kImgdiffMinorPayloadVersion) {
539 // TODO(senj): Check if the zlib version in source and target are the same.
540 payload_config.imgdiff_allowed = true;
541 }
542
Alex Deymob42b98d2015-07-06 17:42:38 -0700543 if (payload_config.is_delta) {
Alex Deymof1cbe172015-03-05 15:58:37 -0800544 LOG(INFO) << "Generating delta update";
Allie Woodb8ccad02015-02-09 14:33:40 -0800545 } else {
Alex Deymof1cbe172015-03-05 15:58:37 -0800546 LOG(INFO) << "Generating full update";
547 }
548
549 // From this point, all the options have been parsed.
550 if (!payload_config.Validate()) {
Alex Deymoa26432a2015-03-12 16:08:04 -0700551 LOG(ERROR) << "Invalid options passed. See errors above.";
552 return 1;
Alex Deymof1cbe172015-03-05 15:58:37 -0800553 }
554
555 uint64_t metadata_size;
Alex Deymo477aec22015-03-24 23:40:48 -0700556 if (!GenerateUpdatePayloadFile(payload_config,
557 FLAGS_out_file,
558 FLAGS_private_key,
559 &metadata_size)) {
Alex Deymof1cbe172015-03-05 15:58:37 -0800560 return 1;
Allie Woodb8ccad02015-02-09 14:33:40 -0800561 }
Sen Jiangd8ae8722015-10-08 15:21:09 -0700562 if (!FLAGS_out_metadata_size_file.empty()) {
563 string metadata_size_string = std::to_string(metadata_size);
564 CHECK(utils::WriteFile(FLAGS_out_metadata_size_file.c_str(),
565 metadata_size_string.data(),
566 metadata_size_string.size()));
567 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000568 return 0;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800569}
570
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700571} // namespace
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800572
573} // namespace chromeos_update_engine
574
575int main(int argc, char** argv) {
576 return chromeos_update_engine::Main(argc, argv);
577}