blob: 9fc7fd7786d9c0117f485c0cde9148a9137ac363 [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 Petkov73058b42010-10-06 16:32:19 -070043DEFINE_string(prefs_dir, "/tmp/update_engine_prefs",
Darin Petkovda8c1362011-01-13 14:04:24 -080044 "Preferences directory, used with apply_delta");
45DEFINE_int32(signature_size, 0, "Raw signature size used for hash calculation");
46DEFINE_string(signature_file, "", "Raw signature file to sign payload with");
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070047
adlr@google.com3defe6a2009-12-04 20:57:17 +000048// This file contains a simple program that takes an old path, a new path,
49// and an output file as arguments and the path to an output file and
50// generates a delta that can be sent to Chrome OS clients.
51
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080052using std::set;
53using std::string;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070054using std::vector;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080055
adlr@google.com3defe6a2009-12-04 20:57:17 +000056namespace chromeos_update_engine {
57
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080058namespace {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080059
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080060bool IsDir(const char* path) {
61 struct stat stbuf;
62 TEST_AND_RETURN_FALSE_ERRNO(lstat(path, &stbuf) == 0);
63 return S_ISDIR(stbuf.st_mode);
64}
65
Darin Petkovda8c1362011-01-13 14:04:24 -080066void CalculatePayloadHashForSigning() {
67 LOG(INFO) << "Calculating payload hash for signing.";
68 LOG_IF(FATAL, FLAGS_in_file.empty())
69 << "Must pass --in_file to calculate hash for signing.";
70 LOG_IF(FATAL, FLAGS_out_hash_file.empty())
71 << "Must pass --out_hash_file to calculate hash for signing.";
72 LOG_IF(FATAL, FLAGS_signature_size <= 0)
73 << "Must pass --signature_size to calculate hash for signing.";
74 vector<char> hash;
75 CHECK(PayloadSigner::HashPayloadForSigning(
76 FLAGS_in_file, FLAGS_signature_size, &hash));
77 CHECK(utils::WriteFile(
78 FLAGS_out_hash_file.c_str(), hash.data(), hash.size()));
79 LOG(INFO) << "Done calculating payload hash for signing.";
80}
81
82void SignPayload() {
83 LOG(INFO) << "Signing payload.";
84 LOG_IF(FATAL, FLAGS_in_file.empty())
85 << "Must pass --in_file to sign payload.";
86 LOG_IF(FATAL, FLAGS_out_file.empty())
87 << "Must pass --out_file to sign payload.";
88 LOG_IF(FATAL, FLAGS_signature_file.empty())
89 << "Must pass --signature_file to sign payload.";
90 vector<char> signature;
91 CHECK(utils::ReadFile(FLAGS_signature_file, &signature));
92 CHECK(PayloadSigner::AddSignatureToPayload(
93 FLAGS_in_file, signature, FLAGS_out_file));
94 LOG(INFO) << "Done signing payload.";
95}
96
97void ApplyDelta() {
98 LOG(INFO) << "Applying delta.";
99 LOG_IF(FATAL, FLAGS_old_image.empty())
100 << "Must pass --old_image to apply delta.";
101 Prefs prefs;
102 LOG(INFO) << "Setting up preferences under: " << FLAGS_prefs_dir;
103 LOG_IF(ERROR, !prefs.Init(FilePath(FLAGS_prefs_dir)))
104 << "Failed to initialize preferences.";
105 // Get original checksums
106 LOG(INFO) << "Calculating original checksums";
107 PartitionInfo kern_info, root_info;
108 CHECK(DeltaDiffGenerator::InitializePartitionInfo(true, // is_kernel
109 FLAGS_old_kernel,
110 &kern_info));
111 CHECK(DeltaDiffGenerator::InitializePartitionInfo(false, // is_kernel
112 FLAGS_old_image,
113 &root_info));
114 vector<char> kern_hash(kern_info.hash().begin(),
115 kern_info.hash().end());
116 vector<char> root_hash(root_info.hash().begin(),
117 root_info.hash().end());
118 DeltaPerformer performer(&prefs);
119 performer.set_current_kernel_hash(kern_hash);
120 performer.set_current_rootfs_hash(root_hash);
121 CHECK_EQ(performer.Open(FLAGS_old_image.c_str(), 0, 0), 0);
122 CHECK(performer.OpenKernel(FLAGS_old_kernel.c_str()));
123 vector<char> buf(1024 * 1024);
124 int fd = open(FLAGS_in_file.c_str(), O_RDONLY, 0);
125 CHECK_GE(fd, 0);
126 ScopedFdCloser fd_closer(&fd);
127 for (off_t offset = 0;; offset += buf.size()) {
128 ssize_t bytes_read;
129 CHECK(utils::PReadAll(fd, &buf[0], buf.size(), offset, &bytes_read));
130 if (bytes_read == 0)
131 break;
132 CHECK_EQ(performer.Write(&buf[0], bytes_read), bytes_read);
133 }
134 CHECK_EQ(performer.Close(), 0);
135 DeltaPerformer::ResetUpdateProgress(&prefs, false);
136 LOG(INFO) << "Done applying delta.";
137}
138
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800139int Main(int argc, char** argv) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000140 g_thread_init(NULL);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700141 google::ParseCommandLineFlags(&argc, &argv, true);
142 CommandLine::Init(argc, argv);
Darin Petkov9c0baf82010-10-07 13:44:48 -0700143 Terminator::Init();
adlr@google.com3defe6a2009-12-04 20:57:17 +0000144 Subprocess::Init();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700145 logging::InitLogging("delta_generator.log",
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800146 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
147 logging::DONT_LOCK_LOG_FILE,
148 logging::APPEND_TO_OLD_LOG_FILE);
Darin Petkovda8c1362011-01-13 14:04:24 -0800149 if (FLAGS_signature_size > 0 || !FLAGS_out_hash_file.empty()) {
150 CalculatePayloadHashForSigning();
151 return 0;
152 }
153 if (!FLAGS_signature_file.empty()) {
154 SignPayload();
155 return 0;
156 }
157 if (!FLAGS_in_file.empty()) {
158 ApplyDelta();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700159 return 0;
160 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700161 CHECK(!FLAGS_new_image.empty());
162 CHECK(!FLAGS_out_file.empty());
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700163 CHECK(!FLAGS_new_kernel.empty());
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700164 if (FLAGS_old_image.empty()) {
165 LOG(INFO) << "Generating full update";
166 } else {
167 LOG(INFO) << "Generating delta update";
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700168 CHECK(!FLAGS_old_dir.empty());
169 CHECK(!FLAGS_new_dir.empty());
170 if ((!IsDir(FLAGS_old_dir.c_str())) || (!IsDir(FLAGS_new_dir.c_str()))) {
171 LOG(FATAL) << "old_dir or new_dir not directory";
172 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000173 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700174 DeltaDiffGenerator::GenerateDeltaUpdateFile(FLAGS_old_dir,
175 FLAGS_old_image,
176 FLAGS_new_dir,
177 FLAGS_new_image,
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700178 FLAGS_old_kernel,
179 FLAGS_new_kernel,
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700180 FLAGS_out_file,
181 FLAGS_private_key);
Andrew de los Reyes1e338b82010-01-22 14:57:27 -0800182
adlr@google.com3defe6a2009-12-04 20:57:17 +0000183 return 0;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800184}
185
186} // namespace {}
187
188} // namespace chromeos_update_engine
189
190int main(int argc, char** argv) {
191 return chromeos_update_engine::Main(argc, argv);
192}