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