blob: f5fe5e78a0c8763cc400b8833390ba66d172c3dd [file] [log] [blame]
Darin Petkov73058b42010-10-06 16:32:19 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <sys/types.h>
6#include <sys/stat.h>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -08007#include <errno.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07008#include <fcntl.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +00009#include <unistd.h>
Darin Petkov73058b42010-10-06 16:32:19 -070010
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080011#include <set>
adlr@google.com3defe6a2009-12-04 20:57:17 +000012#include <string>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070013#include <vector>
Darin Petkov73058b42010-10-06 16:32:19 -070014
15#include <base/command_line.h>
16#include <base/logging.h>
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070017#include <base/string_number_conversions.h>
18#include <base/string_split.h>
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070019#include <gflags/gflags.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000020#include <glib.h>
Darin Petkov73058b42010-10-06 16:32:19 -070021
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080022#include "update_engine/delta_diff_generator.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070023#include "update_engine/delta_performer.h"
Darin Petkovda8c1362011-01-13 14:04:24 -080024#include "update_engine/payload_signer.h"
Darin Petkov73058b42010-10-06 16:32:19 -070025#include "update_engine/prefs.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000026#include "update_engine/subprocess.h"
Darin Petkov9c0baf82010-10-07 13:44:48 -070027#include "update_engine/terminator.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000028#include "update_engine/update_metadata.pb.h"
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080029#include "update_engine/utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000030
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070031DEFINE_string(old_dir, "",
32 "Directory where the old rootfs is loop mounted read-only");
33DEFINE_string(new_dir, "",
34 "Directory where the new rootfs is loop mounted read-only");
35DEFINE_string(old_image, "", "Path to the old rootfs");
36DEFINE_string(new_image, "", "Path to the new rootfs");
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070037DEFINE_string(old_kernel, "", "Path to the old kernel partition image");
38DEFINE_string(new_kernel, "", "Path to the new kernel partition image");
Darin Petkovda8c1362011-01-13 14:04:24 -080039DEFINE_string(in_file, "",
40 "Path to input delta payload file used to hash/sign payloads "
41 "and apply delta over old_image (for debugging)");
42DEFINE_string(out_file, "", "Path to output delta payload file");
43DEFINE_string(out_hash_file, "", "Path to output hash file");
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070044DEFINE_string(private_key, "", "Path to private key in .pem format");
Darin Petkovadb3cef2011-01-13 16:16:08 -080045DEFINE_string(public_key, "", "Path to public key in .pem format");
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070046DEFINE_int32(public_key_version,
47 chromeos_update_engine::kSignatureMessageCurrentVersion,
48 "Key-check version # of client");
Darin Petkov73058b42010-10-06 16:32:19 -070049DEFINE_string(prefs_dir, "/tmp/update_engine_prefs",
Darin Petkovda8c1362011-01-13 14:04:24 -080050 "Preferences directory, used with apply_delta");
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070051DEFINE_string(signature_size, "",
52 "Raw signature size used for hash calculation. "
53 "You may pass in multiple sizes by colon separating them. E.g. "
54 "2048:2048:4096 will assume 3 signatures, the first two with "
55 "2048 size and the last 4096.");
56DEFINE_string(signature_file, "",
57 "Raw signature file to sign payload with. To pass multiple "
58 "signatures, use a single argument with a colon between paths, "
59 "e.g. /path/to/sig:/path/to/next:/path/to/last_sig . Each "
60 "signature will be assigned a client version, starting from "
61 "kSignatureOriginalVersion.");
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070062
adlr@google.com3defe6a2009-12-04 20:57:17 +000063// This file contains a simple program that takes an old path, a new path,
64// and an output file as arguments and the path to an output file and
65// generates a delta that can be sent to Chrome OS clients.
66
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080067using std::set;
68using std::string;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070069using std::vector;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080070
adlr@google.com3defe6a2009-12-04 20:57:17 +000071namespace chromeos_update_engine {
72
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080073namespace {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080074
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080075bool IsDir(const char* path) {
76 struct stat stbuf;
77 TEST_AND_RETURN_FALSE_ERRNO(lstat(path, &stbuf) == 0);
78 return S_ISDIR(stbuf.st_mode);
79}
80
Darin Petkovda8c1362011-01-13 14:04:24 -080081void CalculatePayloadHashForSigning() {
82 LOG(INFO) << "Calculating payload hash for signing.";
83 LOG_IF(FATAL, FLAGS_in_file.empty())
84 << "Must pass --in_file to calculate hash for signing.";
85 LOG_IF(FATAL, FLAGS_out_hash_file.empty())
86 << "Must pass --out_hash_file to calculate hash for signing.";
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070087 LOG_IF(FATAL, FLAGS_signature_size.empty())
Darin Petkovda8c1362011-01-13 14:04:24 -080088 << "Must pass --signature_size to calculate hash for signing.";
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070089 vector<int> sizes;
90 vector<string> strsizes;
91 base::SplitString(FLAGS_signature_size, ':', &strsizes);
92 for (vector<string>::iterator it = strsizes.begin(), e = strsizes.end();
93 it != e; ++it) {
94 int size = 0;
95 LOG_IF(FATAL, !base::StringToInt(*it, &size))
96 << "Not an integer: " << *it;
97 sizes.push_back(size);
98 }
Darin Petkovda8c1362011-01-13 14:04:24 -080099 vector<char> hash;
100 CHECK(PayloadSigner::HashPayloadForSigning(
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700101 FLAGS_in_file, sizes, &hash));
Darin Petkovda8c1362011-01-13 14:04:24 -0800102 CHECK(utils::WriteFile(
103 FLAGS_out_hash_file.c_str(), hash.data(), hash.size()));
104 LOG(INFO) << "Done calculating payload hash for signing.";
105}
106
107void SignPayload() {
108 LOG(INFO) << "Signing payload.";
109 LOG_IF(FATAL, FLAGS_in_file.empty())
110 << "Must pass --in_file to sign payload.";
111 LOG_IF(FATAL, FLAGS_out_file.empty())
112 << "Must pass --out_file to sign payload.";
113 LOG_IF(FATAL, FLAGS_signature_file.empty())
114 << "Must pass --signature_file to sign payload.";
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700115 vector<vector<char> > signatures;
116 vector<string> signature_files;
117 base::SplitString(FLAGS_signature_file, ':', &signature_files);
118 for (vector<string>::iterator it = signature_files.begin(),
119 e = signature_files.end(); it != e; ++it) {
120 vector<char> signature;
121 CHECK(utils::ReadFile(*it, &signature));
122 signatures.push_back(signature);
123 }
Darin Petkovda8c1362011-01-13 14:04:24 -0800124 CHECK(PayloadSigner::AddSignatureToPayload(
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700125 FLAGS_in_file, signatures, FLAGS_out_file));
Darin Petkovda8c1362011-01-13 14:04:24 -0800126 LOG(INFO) << "Done signing payload.";
127}
128
Darin Petkovadb3cef2011-01-13 16:16:08 -0800129void VerifySignedPayload() {
130 LOG(INFO) << "Verifying signed payload.";
131 LOG_IF(FATAL, FLAGS_in_file.empty())
132 << "Must pass --in_file to verify signed payload.";
133 LOG_IF(FATAL, FLAGS_public_key.empty())
134 << "Must pass --public_key to verify signed payload.";
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700135 CHECK(PayloadSigner::VerifySignedPayload(FLAGS_in_file, FLAGS_public_key,
136 FLAGS_public_key_version));
Darin Petkovadb3cef2011-01-13 16:16:08 -0800137 LOG(INFO) << "Done verifying signed payload.";
138}
139
Darin Petkovda8c1362011-01-13 14:04:24 -0800140void ApplyDelta() {
141 LOG(INFO) << "Applying delta.";
142 LOG_IF(FATAL, FLAGS_old_image.empty())
143 << "Must pass --old_image to apply delta.";
144 Prefs prefs;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700145 InstallPlan install_plan;
Darin Petkovda8c1362011-01-13 14:04:24 -0800146 LOG(INFO) << "Setting up preferences under: " << FLAGS_prefs_dir;
147 LOG_IF(ERROR, !prefs.Init(FilePath(FLAGS_prefs_dir)))
148 << "Failed to initialize preferences.";
149 // Get original checksums
150 LOG(INFO) << "Calculating original checksums";
151 PartitionInfo kern_info, root_info;
152 CHECK(DeltaDiffGenerator::InitializePartitionInfo(true, // is_kernel
153 FLAGS_old_kernel,
154 &kern_info));
155 CHECK(DeltaDiffGenerator::InitializePartitionInfo(false, // is_kernel
156 FLAGS_old_image,
157 &root_info));
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700158 install_plan.kernel_hash.assign(kern_info.hash().begin(),
159 kern_info.hash().end());
160 install_plan.rootfs_hash.assign(root_info.hash().begin(),
161 root_info.hash().end());
162 DeltaPerformer performer(&prefs, &install_plan);
Darin Petkovda8c1362011-01-13 14:04:24 -0800163 CHECK_EQ(performer.Open(FLAGS_old_image.c_str(), 0, 0), 0);
164 CHECK(performer.OpenKernel(FLAGS_old_kernel.c_str()));
165 vector<char> buf(1024 * 1024);
166 int fd = open(FLAGS_in_file.c_str(), O_RDONLY, 0);
167 CHECK_GE(fd, 0);
168 ScopedFdCloser fd_closer(&fd);
169 for (off_t offset = 0;; offset += buf.size()) {
170 ssize_t bytes_read;
171 CHECK(utils::PReadAll(fd, &buf[0], buf.size(), offset, &bytes_read));
172 if (bytes_read == 0)
173 break;
174 CHECK_EQ(performer.Write(&buf[0], bytes_read), bytes_read);
175 }
176 CHECK_EQ(performer.Close(), 0);
177 DeltaPerformer::ResetUpdateProgress(&prefs, false);
178 LOG(INFO) << "Done applying delta.";
179}
180
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800181int Main(int argc, char** argv) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000182 g_thread_init(NULL);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700183 google::ParseCommandLineFlags(&argc, &argv, true);
184 CommandLine::Init(argc, argv);
Darin Petkov9c0baf82010-10-07 13:44:48 -0700185 Terminator::Init();
adlr@google.com3defe6a2009-12-04 20:57:17 +0000186 Subprocess::Init();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700187 logging::InitLogging("delta_generator.log",
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800188 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
189 logging::DONT_LOCK_LOG_FILE,
Chris Masoned903c3b2011-05-12 15:35:46 -0700190 logging::APPEND_TO_OLD_LOG_FILE,
191 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700192 if (!FLAGS_signature_size.empty() || !FLAGS_out_hash_file.empty()) {
Darin Petkovda8c1362011-01-13 14:04:24 -0800193 CalculatePayloadHashForSigning();
194 return 0;
195 }
196 if (!FLAGS_signature_file.empty()) {
197 SignPayload();
198 return 0;
199 }
Darin Petkovadb3cef2011-01-13 16:16:08 -0800200 if (!FLAGS_public_key.empty()) {
201 VerifySignedPayload();
202 return 0;
203 }
Darin Petkovda8c1362011-01-13 14:04:24 -0800204 if (!FLAGS_in_file.empty()) {
205 ApplyDelta();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700206 return 0;
207 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700208 CHECK(!FLAGS_new_image.empty());
209 CHECK(!FLAGS_out_file.empty());
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700210 CHECK(!FLAGS_new_kernel.empty());
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700211 if (FLAGS_old_image.empty()) {
212 LOG(INFO) << "Generating full update";
213 } else {
214 LOG(INFO) << "Generating delta update";
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700215 CHECK(!FLAGS_old_dir.empty());
216 CHECK(!FLAGS_new_dir.empty());
217 if ((!IsDir(FLAGS_old_dir.c_str())) || (!IsDir(FLAGS_new_dir.c_str()))) {
218 LOG(FATAL) << "old_dir or new_dir not directory";
219 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000220 }
Chris Sosac7c19cd2011-02-08 17:02:12 -0800221 if (!DeltaDiffGenerator::GenerateDeltaUpdateFile(FLAGS_old_dir,
222 FLAGS_old_image,
223 FLAGS_new_dir,
224 FLAGS_new_image,
225 FLAGS_old_kernel,
226 FLAGS_new_kernel,
227 FLAGS_out_file,
228 FLAGS_private_key)) {
229 return 1;
230 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000231 return 0;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800232}
233
234} // namespace {}
235
236} // namespace chromeos_update_engine
237
238int main(int argc, char** argv) {
239 return chromeos_update_engine::Main(argc, argv);
240}