blob: ec95325fc8d969d8a3e440afb0bfe65e528f381e [file] [log] [blame]
Doug Zongker512536a2010-02-17 16:11:44 -08001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tao Bao36c35112016-10-25 14:17:26 -070017#include "applypatch_modes.h"
18
Doug Zongker512536a2010-02-17 16:11:44 -080019#include <stdio.h>
Doug Zongkerc4351c72010-02-22 14:46:32 -080020#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
Doug Zongker512536a2010-02-17 16:11:44 -080023
Yabin Cuid483c202016-02-03 17:08:52 -080024#include <memory>
Tianjie Xuaced5d92016-10-12 10:55:04 -070025#include <string>
Yabin Cuid483c202016-02-03 17:08:52 -080026#include <vector>
27
Tao Bao859bfc52018-04-25 23:00:27 -070028#include <android-base/logging.h>
Tao Bao36c35112016-10-25 14:17:26 -070029#include <android-base/parseint.h>
30#include <android-base/strings.h>
Tao Baofada91c2016-10-27 18:16:06 -070031#include <openssl/sha.h>
32
Tao Baod80a9982016-03-03 11:43:47 -080033#include "applypatch/applypatch.h"
Doug Zongkerc4351c72010-02-22 14:46:32 -080034#include "edify/expr.h"
Doug Zongkerc4351c72010-02-22 14:46:32 -080035
Tao Bao36c35112016-10-25 14:17:26 -070036static int CheckMode(int argc, const char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080037 if (argc < 3) {
38 return 2;
39 }
Tianjie Xuaced5d92016-10-12 10:55:04 -070040 std::vector<std::string> sha1;
41 for (int i = 3; i < argc; i++) {
42 sha1.push_back(argv[i]);
43 }
44
45 return applypatch_check(argv[2], sha1);
Doug Zongkerc4351c72010-02-22 14:46:32 -080046}
47
Tao Bao36c35112016-10-25 14:17:26 -070048// Parse arguments (which should be of the form "<sha1>:<filename>" into the
49// new parallel arrays *sha1s and *files. Returns true on success.
50static bool ParsePatchArgs(int argc, const char** argv, std::vector<std::string>* sha1s,
Yabin Cuid6c93af2016-02-10 16:41:10 -080051 std::vector<FileContents>* files) {
Tianjie Xuaced5d92016-10-12 10:55:04 -070052 if (sha1s == nullptr) {
53 return false;
54 }
Yabin Cuid483c202016-02-03 17:08:52 -080055 for (int i = 0; i < argc; ++i) {
Tao Bao36c35112016-10-25 14:17:26 -070056 std::vector<std::string> pieces = android::base::Split(argv[i], ":");
57 if (pieces.size() != 2) {
Tao Bao859bfc52018-04-25 23:00:27 -070058 LOG(ERROR) << "Failed to parse patch argument \"" << argv[i] << "\"";
59 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080060 }
Tao Bao36c35112016-10-25 14:17:26 -070061
62 uint8_t digest[SHA_DIGEST_LENGTH];
Tao Bao8dc70492018-06-20 10:14:40 -070063 if (ParseSha1(pieces[0], digest) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -070064 LOG(ERROR) << "Failed to parse SHA-1 \"" << argv[i] << "\"";
65 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080066 }
67
Tao Bao36c35112016-10-25 14:17:26 -070068 sha1s->push_back(pieces[0]);
Yabin Cuid6c93af2016-02-10 16:41:10 -080069 FileContents fc;
Tao Bao36c35112016-10-25 14:17:26 -070070 if (LoadFileContents(pieces[1].c_str(), &fc) != 0) {
Yabin Cuid6c93af2016-02-10 16:41:10 -080071 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080072 }
Yabin Cuid6c93af2016-02-10 16:41:10 -080073 files->push_back(std::move(fc));
Doug Zongkerc4351c72010-02-22 14:46:32 -080074 }
Tao Baoba9a42a2015-06-23 23:23:33 -070075 return true;
Doug Zongkerc4351c72010-02-22 14:46:32 -080076}
77
Tao Baoabba55b2015-07-17 18:11:12 -070078static int FlashMode(const char* src_filename, const char* tgt_filename,
79 const char* tgt_sha1, size_t tgt_size) {
80 return applypatch_flash(src_filename, tgt_filename, tgt_sha1, tgt_size);
81}
82
Tao Bao36c35112016-10-25 14:17:26 -070083static int PatchMode(int argc, const char** argv) {
Tao Bao511d7592018-06-19 15:56:49 -070084 std::unique_ptr<Value> bonus;
85 if (argc >= 3 && strcmp(argv[1], "-b") == 0) {
86 FileContents bonus_fc;
87 if (LoadFileContents(argv[2], &bonus_fc) != 0) {
88 LOG(ERROR) << "Failed to load bonus file " << argv[2];
Tao Bao859bfc52018-04-25 23:00:27 -070089 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -080090 }
Tao Bao511d7592018-06-19 15:56:49 -070091 bonus = std::make_unique<Value>(Value::Type::BLOB,
92 std::string(bonus_fc.data.cbegin(), bonus_fc.data.cend()));
93 argc -= 2;
94 argv += 2;
95 }
Doug Zongkerc4351c72010-02-22 14:46:32 -080096
Tao Bao511d7592018-06-19 15:56:49 -070097 if (argc < 4) {
98 return 2;
99 }
Tao Bao36c35112016-10-25 14:17:26 -0700100
Tao Bao511d7592018-06-19 15:56:49 -0700101 size_t target_size;
102 if (!android::base::ParseUint(argv[4], &target_size) || target_size == 0) {
103 LOG(ERROR) << "Failed to parse \"" << argv[4] << "\" as byte count";
104 return 1;
105 }
106
107 // If no <src-sha1>:<patch> is provided, it is in flash mode.
108 if (argc == 5) {
109 if (bonus) {
110 LOG(ERROR) << "bonus file not supported in flash mode";
Tao Bao859bfc52018-04-25 23:00:27 -0700111 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800112 }
Tao Bao511d7592018-06-19 15:56:49 -0700113 return FlashMode(argv[1], argv[2], argv[3], target_size);
114 }
Tao Baofada91c2016-10-27 18:16:06 -0700115
Tao Bao511d7592018-06-19 15:56:49 -0700116 std::vector<std::string> sha1s;
117 std::vector<FileContents> files;
118 if (!ParsePatchArgs(argc - 5, argv + 5, &sha1s, &files)) {
119 LOG(ERROR) << "Failed to parse patch args";
120 return 1;
121 }
122
123 std::vector<std::unique_ptr<Value>> patches;
124 for (const auto& file : files) {
125 patches.push_back(std::make_unique<Value>(Value::Type::BLOB,
126 std::string(file.data.cbegin(), file.data.cend())));
127 }
128 return applypatch(argv[1], argv[2], argv[3], target_size, sha1s, patches, bonus.get());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800129}
Doug Zongker512536a2010-02-17 16:11:44 -0800130
Tao Bao36c35112016-10-25 14:17:26 -0700131// This program (applypatch) applies binary patches to files in a way that
132// is safe (the original file is not touched until we have the desired
Doug Zongker512536a2010-02-17 16:11:44 -0800133// replacement for it) and idempotent (it's okay to run this program
134// multiple times).
135//
136// - if the sha1 hash of <tgt-file> is <tgt-sha1>, does nothing and exits
137// successfully.
138//
Tao Baoabba55b2015-07-17 18:11:12 -0700139// - otherwise, if no <src-sha1>:<patch> is provided, flashes <tgt-file> with
140// <src-file>. <tgt-file> must be a partition name, while <src-file> must
141// be a regular image file. <src-file> will not be deleted on success.
142//
Doug Zongker512536a2010-02-17 16:11:44 -0800143// - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the
144// bsdiff <patch> to <src-file> to produce a new file (the type of patch
145// is automatically detected from the file header). If that new
146// file has sha1 hash <tgt-sha1>, moves it to replace <tgt-file>, and
147// exits successfully. Note that if <src-file> and <tgt-file> are
148// not the same, <src-file> is NOT deleted on success. <tgt-file>
149// may be the string "-" to mean "the same as src-file".
150//
151// - otherwise, or if any error is encountered, exits with non-zero
152// status.
153//
Elliott Hughes63a31922016-06-09 17:41:22 -0700154// <src-file> (or <file> in check mode) may refer to an EMMC partition
Doug Zongker512536a2010-02-17 16:11:44 -0800155// to read the source data. See the comments for the
Elliott Hughes63a31922016-06-09 17:41:22 -0700156// LoadPartitionContents() function for the format of such a filename.
Doug Zongker512536a2010-02-17 16:11:44 -0800157
Tao Bao36c35112016-10-25 14:17:26 -0700158int applypatch_modes(int argc, const char** argv) {
Tao Bao859bfc52018-04-25 23:00:27 -0700159 if (argc < 2) {
160 usage:
161 // clang-format off
162 LOG(INFO) << "Usage: \n"
163 << " " << argv[0] << " [-b <bonus-file>] <src-file> <tgt-file> <tgt-sha1> "
164 "<tgt-size> [<src-sha1>:<patch> ...]\n"
165 << " " << argv[0] << " -c <file> [<sha1> ...]\n"
166 << " " << argv[0] << " -l\n"
167 << "\n"
168 << "Filenames may be of the form\n"
169 << " EMMC:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n"
170 << "to specify reading from or writing to an EMMC partition.\n\n";
171 // clang-format on
172 return 2;
173 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800174
Tao Bao859bfc52018-04-25 23:00:27 -0700175 int result;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800176
Tao Bao859bfc52018-04-25 23:00:27 -0700177 if (strncmp(argv[1], "-l", 3) == 0) {
178 result = ShowLicenses();
179 } else if (strncmp(argv[1], "-c", 3) == 0) {
180 result = CheckMode(argc, argv);
181 } else {
182 result = PatchMode(argc, argv);
183 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800184
Tao Bao859bfc52018-04-25 23:00:27 -0700185 if (result == 2) {
186 goto usage;
187 }
188 return result;
Doug Zongker512536a2010-02-17 16:11:44 -0800189}