generate_delta_main.cc revision 720df3eeaecfc9ad229440c59e08c8d52f13dc1e
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 <errno.h>
18#include <fcntl.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21#include <unistd.h>
22
23#include <set>
24#include <string>
25#include <vector>
26
27#include <base/logging.h>
28#include <base/strings/string_number_conversions.h>
29#include <base/strings/string_split.h>
30#include <brillo/flag_helper.h>
31
32#include "update_engine/delta_performer.h"
33#include "update_engine/payload_constants.h"
34#include "update_engine/payload_generator/delta_diff_generator.h"
35#include "update_engine/payload_generator/delta_diff_utils.h"
36#include "update_engine/payload_generator/payload_generation_config.h"
37#include "update_engine/payload_generator/payload_signer.h"
38#include "update_engine/prefs.h"
39#include "update_engine/terminator.h"
40#include "update_engine/update_metadata.pb.h"
41#include "update_engine/utils.h"
42
43// This file contains a simple program that takes an old path, a new path,
44// and an output file as arguments and the path to an output file and
45// generates a delta that can be sent to Chrome OS clients.
46
47using std::set;
48using std::string;
49using std::vector;
50
51namespace chromeos_update_engine {
52
53namespace {
54
55void ParseSignatureSizes(const string& signature_sizes_flag,
56                         vector<int>* signature_sizes) {
57  signature_sizes->clear();
58  vector<string> split_strings;
59
60  base::SplitString(signature_sizes_flag, ':', &split_strings);
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)
65        << "Invalid signature size: " << str;
66
67    LOG_IF(FATAL, size != (2048 / 8)) <<
68        "Only signature sizes of 256 bytes are supported.";
69
70    signature_sizes->push_back(size);
71  }
72}
73
74bool ParseImageInfo(const string& channel,
75                    const string& board,
76                    const string& version,
77                    const string& key,
78                    const string& build_channel,
79                    const string& build_version,
80                    ImageInfo* image_info) {
81  // All of these arguments should be present or missing.
82  bool empty = channel.empty();
83
84  CHECK_EQ(channel.empty(), empty);
85  CHECK_EQ(board.empty(), empty);
86  CHECK_EQ(version.empty(), empty);
87  CHECK_EQ(key.empty(), empty);
88
89  if (empty)
90    return false;
91
92  image_info->set_channel(channel);
93  image_info->set_board(board);
94  image_info->set_version(version);
95  image_info->set_key(key);
96
97  image_info->set_build_channel(
98      build_channel.empty() ? channel : build_channel);
99
100  image_info->set_build_version(
101      build_version.empty() ? version : build_version);
102
103  return true;
104}
105
106void CalculateHashForSigning(const vector<int> &sizes,
107                             const string& out_hash_file,
108                             const string& out_metadata_hash_file,
109                             const string& in_file) {
110  LOG(INFO) << "Calculating hash for signing.";
111  LOG_IF(FATAL, in_file.empty())
112      << "Must pass --in_file to calculate hash for signing.";
113  LOG_IF(FATAL, out_hash_file.empty())
114      << "Must pass --out_hash_file to calculate hash for signing.";
115
116  brillo::Blob payload_hash, metadata_hash;
117  CHECK(PayloadSigner::HashPayloadForSigning(in_file, sizes, &payload_hash,
118                                             &metadata_hash));
119  CHECK(utils::WriteFile(out_hash_file.c_str(), payload_hash.data(),
120                         payload_hash.size()));
121  if (!out_metadata_hash_file.empty())
122    CHECK(utils::WriteFile(out_metadata_hash_file.c_str(), metadata_hash.data(),
123                           metadata_hash.size()));
124
125  LOG(INFO) << "Done calculating hash for signing.";
126}
127
128void SignPayload(const string& in_file,
129                 const string& out_file,
130                 const string& signature_file,
131                 const string& out_metadata_size_file) {
132  LOG(INFO) << "Signing payload.";
133  LOG_IF(FATAL, in_file.empty())
134      << "Must pass --in_file to sign payload.";
135  LOG_IF(FATAL, out_file.empty())
136      << "Must pass --out_file to sign payload.";
137  LOG_IF(FATAL, signature_file.empty())
138      << "Must pass --signature_file to sign payload.";
139  vector<brillo::Blob> signatures;
140  vector<string> signature_files;
141  base::SplitString(signature_file, ':', &signature_files);
142  for (const string& signature_file : signature_files) {
143    brillo::Blob signature;
144    CHECK(utils::ReadFile(signature_file, &signature));
145    signatures.push_back(signature);
146  }
147  uint64_t final_metadata_size;
148  CHECK(PayloadSigner::AddSignatureToPayload(
149      in_file, signatures, out_file, &final_metadata_size));
150  LOG(INFO) << "Done signing payload. Final metadata size = "
151            << final_metadata_size;
152  if (!out_metadata_size_file.empty()) {
153    string metadata_size_string = std::to_string(final_metadata_size);
154    CHECK(utils::WriteFile(out_metadata_size_file.c_str(),
155                           metadata_size_string.data(),
156                           metadata_size_string.size()));
157  }
158}
159
160void VerifySignedPayload(const string& in_file,
161                         const string& public_key) {
162  LOG(INFO) << "Verifying signed payload.";
163  LOG_IF(FATAL, in_file.empty())
164      << "Must pass --in_file to verify signed payload.";
165  LOG_IF(FATAL, public_key.empty())
166      << "Must pass --public_key to verify signed payload.";
167  CHECK(PayloadSigner::VerifySignedPayload(in_file, public_key));
168  LOG(INFO) << "Done verifying signed payload.";
169}
170
171// TODO(deymo): This function is likely broken for deltas minor version 2 or
172// newer. Move this function to a new file and make the delta_performer
173// integration tests use this instead.
174void ApplyDelta(const string& in_file,
175                const string& old_kernel,
176                const string& old_rootfs,
177                const string& prefs_dir) {
178  LOG(INFO) << "Applying delta.";
179  LOG_IF(FATAL, old_rootfs.empty())
180      << "Must pass --old_image to apply delta.";
181  Prefs prefs;
182  InstallPlan install_plan;
183  LOG(INFO) << "Setting up preferences under: " << prefs_dir;
184  LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
185      << "Failed to initialize preferences.";
186  // Get original checksums
187  LOG(INFO) << "Calculating original checksums";
188  ImageConfig old_image;
189  old_image.partitions.emplace_back(kLegacyPartitionNameRoot);
190  old_image.partitions.back().path = old_rootfs;
191  old_image.partitions.emplace_back(kLegacyPartitionNameKernel);
192  old_image.partitions.back().path = old_kernel;
193  CHECK(old_image.LoadImageSize());
194  for (const auto& old_part : old_image.partitions) {
195    PartitionInfo part_info;
196    CHECK(diff_utils::InitializePartitionInfo(old_part, &part_info));
197    InstallPlan::Partition part;
198    part.name = old_part.name;
199    part.source_hash.assign(part_info.hash().begin(),
200                            part_info.hash().end());
201    part.source_path = old_part.path;
202    // Apply the delta in-place to the old_part.
203    part.target_path = old_part.path;
204    install_plan.partitions.push_back(part);
205  }
206
207  DeltaPerformer performer(&prefs, nullptr, &install_plan);
208  brillo::Blob buf(1024 * 1024);
209  int fd = open(in_file.c_str(), O_RDONLY, 0);
210  CHECK_GE(fd, 0);
211  ScopedFdCloser fd_closer(&fd);
212  for (off_t offset = 0;; offset += buf.size()) {
213    ssize_t bytes_read;
214    CHECK(utils::PReadAll(fd, buf.data(), buf.size(), offset, &bytes_read));
215    if (bytes_read == 0)
216      break;
217    CHECK_EQ(performer.Write(buf.data(), bytes_read), bytes_read);
218  }
219  CHECK_EQ(performer.Close(), 0);
220  DeltaPerformer::ResetUpdateProgress(&prefs, false);
221  LOG(INFO) << "Done applying delta.";
222}
223
224int Main(int argc, char** argv) {
225  DEFINE_string(old_image, "", "Path to the old rootfs");
226  DEFINE_string(new_image, "", "Path to the new rootfs");
227  DEFINE_string(old_kernel, "", "Path to the old kernel partition image");
228  DEFINE_string(new_kernel, "", "Path to the new kernel partition image");
229  DEFINE_string(old_partitions, "",
230                "Path to the old partitions. To pass multiple partitions, use "
231                "a single argument with a colon between paths, e.g. "
232                "/path/to/part:/path/to/part2::/path/to/last_part . Path can "
233                "be empty, but it has to match the order of partition_names.");
234  DEFINE_string(new_partitions, "",
235                "Path to the new partitions. To pass multiple partitions, use "
236                "a single argument with a colon between paths, e.g. "
237                "/path/to/part:/path/to/part2:/path/to/last_part . Path has "
238                "to match the order of partition_names.");
239  DEFINE_string(partition_names,
240                string(kLegacyPartitionNameRoot) + ":" +
241                kLegacyPartitionNameKernel,
242                "Names of the partitions. To pass multiple names, use a single "
243                "argument with a colon between names, e.g. "
244                "name:name2:name3:last_name . Name can not be empty, and it "
245                "has to match the order of partitions.");
246  DEFINE_string(in_file, "",
247                "Path to input delta payload file used to hash/sign payloads "
248                "and apply delta over old_image (for debugging)");
249  DEFINE_string(out_file, "", "Path to output delta payload file");
250  DEFINE_string(out_hash_file, "", "Path to output hash file");
251  DEFINE_string(out_metadata_hash_file, "",
252                "Path to output metadata hash file");
253  DEFINE_string(out_metadata_size_file, "",
254                "Path to output metadata size file");
255  DEFINE_string(private_key, "", "Path to private key in .pem format");
256  DEFINE_string(public_key, "", "Path to public key in .pem format");
257  DEFINE_int32(public_key_version, -1,
258               "DEPRECATED. Key-check version # of client");
259  DEFINE_string(prefs_dir, "/tmp/update_engine_prefs",
260                "Preferences directory, used with apply_delta");
261  DEFINE_string(signature_size, "",
262                "Raw signature size used for hash calculation. "
263                "You may pass in multiple sizes by colon separating them. E.g. "
264                "2048:2048:4096 will assume 3 signatures, the first two with "
265                "2048 size and the last 4096.");
266  DEFINE_string(signature_file, "",
267                "Raw signature file to sign payload with. To pass multiple "
268                "signatures, use a single argument with a colon between paths, "
269                "e.g. /path/to/sig:/path/to/next:/path/to/last_sig . Each "
270                "signature will be assigned a client version, starting from "
271                "kSignatureOriginalVersion.");
272  DEFINE_string(metadata_signature_file, "",
273                "Raw signature file with the signature of the metadata hash. "
274                "To pass multiple signatures, use a single argument with a "
275                "colon between paths, "
276                "e.g. /path/to/sig:/path/to/next:/path/to/last_sig .");
277  DEFINE_int32(chunk_size, 200 * 1024 * 1024,
278               "Payload chunk size (-1 for whole files)");
279  DEFINE_uint64(rootfs_partition_size,
280               chromeos_update_engine::kRootFSPartitionSize,
281               "RootFS partition size for the image once installed");
282  DEFINE_uint64(major_version, 1,
283               "The major version of the payload being generated.");
284  DEFINE_int32(minor_version, -1,
285               "The minor version of the payload being generated "
286               "(-1 means autodetect).");
287
288  DEFINE_string(old_channel, "",
289                "The channel for the old image. 'dev-channel', 'npo-channel', "
290                "etc. Ignored, except during delta generation.");
291  DEFINE_string(old_board, "",
292                "The board for the old image. 'x86-mario', 'lumpy', "
293                "etc. Ignored, except during delta generation.");
294  DEFINE_string(old_version, "",
295                "The build version of the old image. 1.2.3, etc.");
296  DEFINE_string(old_key, "",
297                "The key used to sign the old image. 'premp', 'mp', 'mp-v3',"
298                " etc");
299  DEFINE_string(old_build_channel, "",
300                "The channel for the build of the old image. 'dev-channel', "
301                "etc, but will never contain special channels such as "
302                "'npo-channel'. Ignored, except during delta generation.");
303  DEFINE_string(old_build_version, "",
304                "The version of the build containing the old image.");
305
306  DEFINE_string(new_channel, "",
307                "The channel for the new image. 'dev-channel', 'npo-channel', "
308                "etc. Ignored, except during delta generation.");
309  DEFINE_string(new_board, "",
310                "The board for the new image. 'x86-mario', 'lumpy', "
311                "etc. Ignored, except during delta generation.");
312  DEFINE_string(new_version, "",
313                "The build version of the new image. 1.2.3, etc.");
314  DEFINE_string(new_key, "",
315                "The key used to sign the new image. 'premp', 'mp', 'mp-v3',"
316                " etc");
317  DEFINE_string(new_build_channel, "",
318                "The channel for the build of the new image. 'dev-channel', "
319                "etc, but will never contain special channels such as "
320                "'npo-channel'. Ignored, except during delta generation.");
321  DEFINE_string(new_build_version, "",
322                "The version of the build containing the new image.");
323
324  brillo::FlagHelper::Init(argc, argv,
325      "Generates a payload to provide to ChromeOS' update_engine.\n\n"
326      "This tool can create full payloads and also delta payloads if the src\n"
327      "image is provided. It also provides debugging options to apply, sign\n"
328      "and verify payloads.");
329  Terminator::Init();
330
331  logging::LoggingSettings log_settings;
332  log_settings.log_file     = "delta_generator.log";
333  log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
334  log_settings.lock_log     = logging::LOCK_LOG_FILE;
335  log_settings.delete_old   = logging::APPEND_TO_OLD_LOG_FILE;
336
337  logging::InitLogging(log_settings);
338
339  vector<int> signature_sizes;
340  ParseSignatureSizes(FLAGS_signature_size, &signature_sizes);
341
342  if (!FLAGS_out_hash_file.empty() || !FLAGS_out_metadata_hash_file.empty()) {
343    CHECK(FLAGS_out_metadata_size_file.empty());
344    CalculateHashForSigning(signature_sizes, FLAGS_out_hash_file,
345                            FLAGS_out_metadata_hash_file, FLAGS_in_file);
346    return 0;
347  }
348  if (!FLAGS_signature_file.empty()) {
349    SignPayload(FLAGS_in_file, FLAGS_out_file, FLAGS_signature_file,
350                FLAGS_out_metadata_size_file);
351    return 0;
352  }
353  if (!FLAGS_public_key.empty()) {
354    LOG_IF(WARNING, FLAGS_public_key_version != -1)
355        << "--public_key_version is deprecated and ignored.";
356    VerifySignedPayload(FLAGS_in_file, FLAGS_public_key);
357    return 0;
358  }
359  if (!FLAGS_in_file.empty()) {
360    ApplyDelta(FLAGS_in_file, FLAGS_old_kernel, FLAGS_old_image,
361               FLAGS_prefs_dir);
362    return 0;
363  }
364
365  // A payload generation was requested. Convert the flags to a
366  // PayloadGenerationConfig.
367  PayloadGenerationConfig payload_config;
368  vector<string> partition_names, old_partitions, new_partitions;
369
370  base::SplitString(FLAGS_partition_names, ':', &partition_names);
371  CHECK(!partition_names.empty());
372  if (FLAGS_major_version == kChromeOSMajorPayloadVersion ||
373      FLAGS_new_partitions.empty()) {
374    LOG_IF(FATAL, partition_names.size() != 2)
375        << "To support more than 2 partitions, please use the "
376        << "--new_partitions flag and major version 2.";
377    LOG_IF(FATAL, partition_names[0] != kLegacyPartitionNameRoot ||
378                  partition_names[1] != kLegacyPartitionNameKernel)
379        << "To support non-default partition name, please use the "
380        << "--new_partitions flag and major version 2.";
381  }
382
383  if (!FLAGS_new_partitions.empty()) {
384    LOG_IF(FATAL, !FLAGS_new_image.empty() || !FLAGS_new_kernel.empty())
385        << "--new_image and --new_kernel are deprecated, please use "
386        << "--new_partitions for all partitions.";
387    base::SplitString(FLAGS_new_partitions, ':', &new_partitions);
388    CHECK(partition_names.size() == new_partitions.size());
389
390    payload_config.is_delta = !FLAGS_old_partitions.empty();
391    LOG_IF(FATAL, !FLAGS_old_image.empty() || !FLAGS_old_kernel.empty())
392        << "--old_image and --old_kernel are deprecated, please use "
393        << "--old_partitions if you are using --new_partitions.";
394  } else {
395    new_partitions = {FLAGS_new_image, FLAGS_new_kernel};
396    LOG(WARNING) << "--new_partitions is empty, using deprecated --new_image "
397                 << "and --new_kernel flags.";
398
399    payload_config.is_delta = !FLAGS_old_image.empty() ||
400                              !FLAGS_old_kernel.empty();
401    LOG_IF(FATAL, !FLAGS_old_partitions.empty())
402        << "Please use --new_partitions if you are using --old_partitions.";
403  }
404  for (size_t i = 0; i < partition_names.size(); i++) {
405    LOG_IF(FATAL, partition_names[i].empty())
406        << "Partition name can't be empty, see --partition_names.";
407    payload_config.target.partitions.emplace_back(partition_names[i]);
408    payload_config.target.partitions.back().path = new_partitions[i];
409  }
410
411  if (payload_config.is_delta) {
412    if (!FLAGS_old_partitions.empty()) {
413      base::SplitString(FLAGS_old_partitions, ':', &old_partitions);
414      CHECK(old_partitions.size() == new_partitions.size());
415    } else {
416      old_partitions = {FLAGS_old_image, FLAGS_old_kernel};
417      LOG(WARNING) << "--old_partitions is empty, using deprecated --old_image "
418                   << "and --old_kernel flags.";
419    }
420    for (size_t i = 0; i < partition_names.size(); i++) {
421      payload_config.source.partitions.emplace_back(partition_names[i]);
422      payload_config.source.partitions.back().path = old_partitions[i];
423    }
424  }
425
426  // Use the default soft_chunk_size defined in the config.
427  payload_config.hard_chunk_size = FLAGS_chunk_size;
428  payload_config.block_size = kBlockSize;
429
430  // The partition size is never passed to the delta_generator, so we
431  // need to detect those from the provided files.
432  if (payload_config.is_delta) {
433    CHECK(payload_config.source.LoadImageSize());
434  }
435  CHECK(payload_config.target.LoadImageSize());
436
437  CHECK(!FLAGS_out_file.empty());
438
439  // Ignore failures. These are optional arguments.
440  ParseImageInfo(FLAGS_new_channel,
441                 FLAGS_new_board,
442                 FLAGS_new_version,
443                 FLAGS_new_key,
444                 FLAGS_new_build_channel,
445                 FLAGS_new_build_version,
446                 &payload_config.target.image_info);
447
448  // Ignore failures. These are optional arguments.
449  ParseImageInfo(FLAGS_old_channel,
450                 FLAGS_old_board,
451                 FLAGS_old_version,
452                 FLAGS_old_key,
453                 FLAGS_old_build_channel,
454                 FLAGS_old_build_version,
455                 &payload_config.source.image_info);
456
457  payload_config.rootfs_partition_size = FLAGS_rootfs_partition_size;
458
459  if (payload_config.is_delta) {
460    // Avoid opening the filesystem interface for full payloads.
461    for (PartitionConfig& part : payload_config.target.partitions)
462      CHECK(part.OpenFilesystem());
463    for (PartitionConfig& part : payload_config.source.partitions)
464      CHECK(part.OpenFilesystem());
465  }
466
467  payload_config.major_version = FLAGS_major_version;
468  LOG(INFO) << "Using provided major_version=" << FLAGS_major_version;
469
470  if (FLAGS_minor_version == -1) {
471    // Autodetect minor_version by looking at the update_engine.conf in the old
472    // image.
473    if (payload_config.is_delta) {
474      payload_config.minor_version = kInPlaceMinorPayloadVersion;
475      brillo::KeyValueStore store;
476      uint32_t minor_version;
477      for (const PartitionConfig& part : payload_config.source.partitions) {
478        if (part.fs_interface && part.fs_interface->LoadSettings(&store) &&
479            utils::GetMinorVersion(store, &minor_version)) {
480          payload_config.minor_version = minor_version;
481          break;
482        }
483      }
484    } else {
485      payload_config.minor_version = kFullPayloadMinorVersion;
486    }
487    LOG(INFO) << "Auto-detected minor_version=" << payload_config.minor_version;
488  } else {
489    payload_config.minor_version = FLAGS_minor_version;
490    LOG(INFO) << "Using provided minor_version=" << FLAGS_minor_version;
491  }
492
493  if (payload_config.is_delta) {
494    LOG(INFO) << "Generating delta update";
495  } else {
496    LOG(INFO) << "Generating full update";
497  }
498
499  // From this point, all the options have been parsed.
500  if (!payload_config.Validate()) {
501    LOG(ERROR) << "Invalid options passed. See errors above.";
502    return 1;
503  }
504
505  uint64_t metadata_size;
506  if (!GenerateUpdatePayloadFile(payload_config,
507                                 FLAGS_out_file,
508                                 FLAGS_private_key,
509                                 &metadata_size)) {
510    return 1;
511  }
512  if (!FLAGS_out_metadata_size_file.empty()) {
513    string metadata_size_string = std::to_string(metadata_size);
514    CHECK(utils::WriteFile(FLAGS_out_metadata_size_file.c_str(),
515                           metadata_size_string.data(),
516                           metadata_size_string.size()));
517  }
518  return 0;
519}
520
521}  // namespace
522
523}  // namespace chromeos_update_engine
524
525int main(int argc, char** argv) {
526  return chromeos_update_engine::Main(argc, argv);
527}
528