blob: 3460998293e739c2b6d0c6b2214a10de20050abf [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 Reyesb10320d2010-03-31 16:44:44 -070017#include <gflags/gflags.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000018#include <glib.h>
Darin Petkov73058b42010-10-06 16:32:19 -070019
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080020#include "update_engine/delta_diff_generator.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070021#include "update_engine/delta_performer.h"
Darin Petkovda8c1362011-01-13 14:04:24 -080022#include "update_engine/payload_signer.h"
Darin Petkov73058b42010-10-06 16:32:19 -070023#include "update_engine/prefs.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000024#include "update_engine/subprocess.h"
Darin Petkov9c0baf82010-10-07 13:44:48 -070025#include "update_engine/terminator.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000026#include "update_engine/update_metadata.pb.h"
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080027#include "update_engine/utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000028
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070029DEFINE_string(old_dir, "",
30 "Directory where the old rootfs is loop mounted read-only");
31DEFINE_string(new_dir, "",
32 "Directory where the new rootfs is loop mounted read-only");
33DEFINE_string(old_image, "", "Path to the old rootfs");
34DEFINE_string(new_image, "", "Path to the new rootfs");
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070035DEFINE_string(old_kernel, "", "Path to the old kernel partition image");
36DEFINE_string(new_kernel, "", "Path to the new kernel partition image");
Darin Petkovda8c1362011-01-13 14:04:24 -080037DEFINE_string(in_file, "",
38 "Path to input delta payload file used to hash/sign payloads "
39 "and apply delta over old_image (for debugging)");
40DEFINE_string(out_file, "", "Path to output delta payload file");
41DEFINE_string(out_hash_file, "", "Path to output hash file");
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070042DEFINE_string(private_key, "", "Path to private key in .pem format");
Darin Petkovadb3cef2011-01-13 16:16:08 -080043DEFINE_string(public_key, "", "Path to public key in .pem format");
Darin Petkov73058b42010-10-06 16:32:19 -070044DEFINE_string(prefs_dir, "/tmp/update_engine_prefs",
Darin Petkovda8c1362011-01-13 14:04:24 -080045 "Preferences directory, used with apply_delta");
46DEFINE_int32(signature_size, 0, "Raw signature size used for hash calculation");
47DEFINE_string(signature_file, "", "Raw signature file to sign payload with");
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070048
adlr@google.com3defe6a2009-12-04 20:57:17 +000049// This file contains a simple program that takes an old path, a new path,
50// and an output file as arguments and the path to an output file and
51// generates a delta that can be sent to Chrome OS clients.
52
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080053using std::set;
54using std::string;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070055using std::vector;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080056
adlr@google.com3defe6a2009-12-04 20:57:17 +000057namespace chromeos_update_engine {
58
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080059namespace {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080060
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080061bool IsDir(const char* path) {
62 struct stat stbuf;
63 TEST_AND_RETURN_FALSE_ERRNO(lstat(path, &stbuf) == 0);
64 return S_ISDIR(stbuf.st_mode);
65}
66
Darin Petkovda8c1362011-01-13 14:04:24 -080067void CalculatePayloadHashForSigning() {
68 LOG(INFO) << "Calculating payload hash for signing.";
69 LOG_IF(FATAL, FLAGS_in_file.empty())
70 << "Must pass --in_file to calculate hash for signing.";
71 LOG_IF(FATAL, FLAGS_out_hash_file.empty())
72 << "Must pass --out_hash_file to calculate hash for signing.";
73 LOG_IF(FATAL, FLAGS_signature_size <= 0)
74 << "Must pass --signature_size to calculate hash for signing.";
75 vector<char> hash;
76 CHECK(PayloadSigner::HashPayloadForSigning(
77 FLAGS_in_file, FLAGS_signature_size, &hash));
78 CHECK(utils::WriteFile(
79 FLAGS_out_hash_file.c_str(), hash.data(), hash.size()));
80 LOG(INFO) << "Done calculating payload hash for signing.";
81}
82
83void SignPayload() {
84 LOG(INFO) << "Signing payload.";
85 LOG_IF(FATAL, FLAGS_in_file.empty())
86 << "Must pass --in_file to sign payload.";
87 LOG_IF(FATAL, FLAGS_out_file.empty())
88 << "Must pass --out_file to sign payload.";
89 LOG_IF(FATAL, FLAGS_signature_file.empty())
90 << "Must pass --signature_file to sign payload.";
91 vector<char> signature;
92 CHECK(utils::ReadFile(FLAGS_signature_file, &signature));
93 CHECK(PayloadSigner::AddSignatureToPayload(
94 FLAGS_in_file, signature, FLAGS_out_file));
95 LOG(INFO) << "Done signing payload.";
96}
97
Darin Petkovadb3cef2011-01-13 16:16:08 -080098void VerifySignedPayload() {
99 LOG(INFO) << "Verifying signed payload.";
100 LOG_IF(FATAL, FLAGS_in_file.empty())
101 << "Must pass --in_file to verify signed payload.";
102 LOG_IF(FATAL, FLAGS_public_key.empty())
103 << "Must pass --public_key to verify signed payload.";
104 CHECK(PayloadSigner::VerifySignedPayload(FLAGS_in_file, FLAGS_public_key));
105 LOG(INFO) << "Done verifying signed payload.";
106}
107
Darin Petkovda8c1362011-01-13 14:04:24 -0800108void ApplyDelta() {
109 LOG(INFO) << "Applying delta.";
110 LOG_IF(FATAL, FLAGS_old_image.empty())
111 << "Must pass --old_image to apply delta.";
112 Prefs prefs;
113 LOG(INFO) << "Setting up preferences under: " << FLAGS_prefs_dir;
114 LOG_IF(ERROR, !prefs.Init(FilePath(FLAGS_prefs_dir)))
115 << "Failed to initialize preferences.";
116 // Get original checksums
117 LOG(INFO) << "Calculating original checksums";
118 PartitionInfo kern_info, root_info;
119 CHECK(DeltaDiffGenerator::InitializePartitionInfo(true, // is_kernel
120 FLAGS_old_kernel,
121 &kern_info));
122 CHECK(DeltaDiffGenerator::InitializePartitionInfo(false, // is_kernel
123 FLAGS_old_image,
124 &root_info));
125 vector<char> kern_hash(kern_info.hash().begin(),
126 kern_info.hash().end());
127 vector<char> root_hash(root_info.hash().begin(),
128 root_info.hash().end());
129 DeltaPerformer performer(&prefs);
130 performer.set_current_kernel_hash(kern_hash);
131 performer.set_current_rootfs_hash(root_hash);
132 CHECK_EQ(performer.Open(FLAGS_old_image.c_str(), 0, 0), 0);
133 CHECK(performer.OpenKernel(FLAGS_old_kernel.c_str()));
134 vector<char> buf(1024 * 1024);
135 int fd = open(FLAGS_in_file.c_str(), O_RDONLY, 0);
136 CHECK_GE(fd, 0);
137 ScopedFdCloser fd_closer(&fd);
138 for (off_t offset = 0;; offset += buf.size()) {
139 ssize_t bytes_read;
140 CHECK(utils::PReadAll(fd, &buf[0], buf.size(), offset, &bytes_read));
141 if (bytes_read == 0)
142 break;
143 CHECK_EQ(performer.Write(&buf[0], bytes_read), bytes_read);
144 }
145 CHECK_EQ(performer.Close(), 0);
146 DeltaPerformer::ResetUpdateProgress(&prefs, false);
147 LOG(INFO) << "Done applying delta.";
148}
149
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800150int Main(int argc, char** argv) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000151 g_thread_init(NULL);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700152 google::ParseCommandLineFlags(&argc, &argv, true);
153 CommandLine::Init(argc, argv);
Darin Petkov9c0baf82010-10-07 13:44:48 -0700154 Terminator::Init();
adlr@google.com3defe6a2009-12-04 20:57:17 +0000155 Subprocess::Init();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700156 logging::InitLogging("delta_generator.log",
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800157 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
158 logging::DONT_LOCK_LOG_FILE,
159 logging::APPEND_TO_OLD_LOG_FILE);
Darin Petkovda8c1362011-01-13 14:04:24 -0800160 if (FLAGS_signature_size > 0 || !FLAGS_out_hash_file.empty()) {
161 CalculatePayloadHashForSigning();
162 return 0;
163 }
164 if (!FLAGS_signature_file.empty()) {
165 SignPayload();
166 return 0;
167 }
Darin Petkovadb3cef2011-01-13 16:16:08 -0800168 if (!FLAGS_public_key.empty()) {
169 VerifySignedPayload();
170 return 0;
171 }
Darin Petkovda8c1362011-01-13 14:04:24 -0800172 if (!FLAGS_in_file.empty()) {
173 ApplyDelta();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700174 return 0;
175 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700176 CHECK(!FLAGS_new_image.empty());
177 CHECK(!FLAGS_out_file.empty());
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700178 CHECK(!FLAGS_new_kernel.empty());
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700179 if (FLAGS_old_image.empty()) {
180 LOG(INFO) << "Generating full update";
181 } else {
182 LOG(INFO) << "Generating delta update";
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700183 CHECK(!FLAGS_old_dir.empty());
184 CHECK(!FLAGS_new_dir.empty());
185 if ((!IsDir(FLAGS_old_dir.c_str())) || (!IsDir(FLAGS_new_dir.c_str()))) {
186 LOG(FATAL) << "old_dir or new_dir not directory";
187 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000188 }
Chris Sosac7c19cd2011-02-08 17:02:12 -0800189 if (!DeltaDiffGenerator::GenerateDeltaUpdateFile(FLAGS_old_dir,
190 FLAGS_old_image,
191 FLAGS_new_dir,
192 FLAGS_new_image,
193 FLAGS_old_kernel,
194 FLAGS_new_kernel,
195 FLAGS_out_file,
196 FLAGS_private_key)) {
197 return 1;
198 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000199 return 0;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800200}
201
202} // namespace {}
203
204} // namespace chromeos_update_engine
205
206int main(int argc, char** argv) {
207 return chromeos_update_engine::Main(argc, argv);
208}