blob: 5a2dda0d2544236ad3387242d2cee66a7d5a0179 [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;
145 LOG(INFO) << "Setting up preferences under: " << FLAGS_prefs_dir;
146 LOG_IF(ERROR, !prefs.Init(FilePath(FLAGS_prefs_dir)))
147 << "Failed to initialize preferences.";
148 // Get original checksums
149 LOG(INFO) << "Calculating original checksums";
150 PartitionInfo kern_info, root_info;
151 CHECK(DeltaDiffGenerator::InitializePartitionInfo(true, // is_kernel
152 FLAGS_old_kernel,
153 &kern_info));
154 CHECK(DeltaDiffGenerator::InitializePartitionInfo(false, // is_kernel
155 FLAGS_old_image,
156 &root_info));
157 vector<char> kern_hash(kern_info.hash().begin(),
158 kern_info.hash().end());
159 vector<char> root_hash(root_info.hash().begin(),
160 root_info.hash().end());
161 DeltaPerformer performer(&prefs);
162 performer.set_current_kernel_hash(kern_hash);
163 performer.set_current_rootfs_hash(root_hash);
164 CHECK_EQ(performer.Open(FLAGS_old_image.c_str(), 0, 0), 0);
165 CHECK(performer.OpenKernel(FLAGS_old_kernel.c_str()));
166 vector<char> buf(1024 * 1024);
167 int fd = open(FLAGS_in_file.c_str(), O_RDONLY, 0);
168 CHECK_GE(fd, 0);
169 ScopedFdCloser fd_closer(&fd);
170 for (off_t offset = 0;; offset += buf.size()) {
171 ssize_t bytes_read;
172 CHECK(utils::PReadAll(fd, &buf[0], buf.size(), offset, &bytes_read));
173 if (bytes_read == 0)
174 break;
175 CHECK_EQ(performer.Write(&buf[0], bytes_read), bytes_read);
176 }
177 CHECK_EQ(performer.Close(), 0);
178 DeltaPerformer::ResetUpdateProgress(&prefs, false);
179 LOG(INFO) << "Done applying delta.";
180}
181
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800182int Main(int argc, char** argv) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000183 g_thread_init(NULL);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700184 google::ParseCommandLineFlags(&argc, &argv, true);
185 CommandLine::Init(argc, argv);
Darin Petkov9c0baf82010-10-07 13:44:48 -0700186 Terminator::Init();
adlr@google.com3defe6a2009-12-04 20:57:17 +0000187 Subprocess::Init();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700188 logging::InitLogging("delta_generator.log",
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800189 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
190 logging::DONT_LOCK_LOG_FILE,
Chris Masoned903c3b2011-05-12 15:35:46 -0700191 logging::APPEND_TO_OLD_LOG_FILE,
192 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700193 if (!FLAGS_signature_size.empty() || !FLAGS_out_hash_file.empty()) {
Darin Petkovda8c1362011-01-13 14:04:24 -0800194 CalculatePayloadHashForSigning();
195 return 0;
196 }
197 if (!FLAGS_signature_file.empty()) {
198 SignPayload();
199 return 0;
200 }
Darin Petkovadb3cef2011-01-13 16:16:08 -0800201 if (!FLAGS_public_key.empty()) {
202 VerifySignedPayload();
203 return 0;
204 }
Darin Petkovda8c1362011-01-13 14:04:24 -0800205 if (!FLAGS_in_file.empty()) {
206 ApplyDelta();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700207 return 0;
208 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700209 CHECK(!FLAGS_new_image.empty());
210 CHECK(!FLAGS_out_file.empty());
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700211 CHECK(!FLAGS_new_kernel.empty());
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700212 if (FLAGS_old_image.empty()) {
213 LOG(INFO) << "Generating full update";
214 } else {
215 LOG(INFO) << "Generating delta update";
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700216 CHECK(!FLAGS_old_dir.empty());
217 CHECK(!FLAGS_new_dir.empty());
218 if ((!IsDir(FLAGS_old_dir.c_str())) || (!IsDir(FLAGS_new_dir.c_str()))) {
219 LOG(FATAL) << "old_dir or new_dir not directory";
220 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000221 }
Chris Sosac7c19cd2011-02-08 17:02:12 -0800222 if (!DeltaDiffGenerator::GenerateDeltaUpdateFile(FLAGS_old_dir,
223 FLAGS_old_image,
224 FLAGS_new_dir,
225 FLAGS_new_image,
226 FLAGS_old_kernel,
227 FLAGS_new_kernel,
228 FLAGS_out_file,
229 FLAGS_private_key)) {
230 return 1;
231 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000232 return 0;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800233}
234
235} // namespace {}
236
237} // namespace chromeos_update_engine
238
239int main(int argc, char** argv) {
240 return chromeos_update_engine::Main(argc, argv);
241}