blob: 6437e1be6dfc0213d6907e44841b97cad008aa7c [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) {
Yabin Cuid6c93af2016-02-10 16:41:10 -080084 FileContents bonusFc;
Tianjie Xuaced5d92016-10-12 10:55:04 -070085 Value bonus(VAL_INVALID, "");
Yabin Cuid6c93af2016-02-10 16:41:10 -080086
Doug Zongkera3ccba62012-08-20 15:28:02 -070087 if (argc >= 3 && strcmp(argv[1], "-b") == 0) {
Yabin Cuid6c93af2016-02-10 16:41:10 -080088 if (LoadFileContents(argv[2], &bonusFc) != 0) {
Tao Bao859bfc52018-04-25 23:00:27 -070089 LOG(ERROR) << "Failed to load bonus file " << argv[2];
90 return 1;
Doug Zongkera3ccba62012-08-20 15:28:02 -070091 }
Tianjie Xuaced5d92016-10-12 10:55:04 -070092 bonus.type = VAL_BLOB;
Tao Baoedf1b152016-10-24 16:08:28 -070093 bonus.data = std::string(bonusFc.data.cbegin(), bonusFc.data.cend());
Doug Zongkera3ccba62012-08-20 15:28:02 -070094 argc -= 2;
95 argv += 2;
96 }
97
Tao Baoabba55b2015-07-17 18:11:12 -070098 if (argc < 4) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080099 return 2;
100 }
101
Tao Bao36c35112016-10-25 14:17:26 -0700102 size_t target_size;
103 if (!android::base::ParseUint(argv[4], &target_size) || target_size == 0) {
Tao Bao859bfc52018-04-25 23:00:27 -0700104 LOG(ERROR) << "Failed to parse \"" << argv[4] << "\" as byte count";
105 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800106 }
107
Tao Baoabba55b2015-07-17 18:11:12 -0700108 // If no <src-sha1>:<patch> is provided, it is in flash mode.
109 if (argc == 5) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700110 if (bonus.type != VAL_INVALID) {
Tao Bao859bfc52018-04-25 23:00:27 -0700111 LOG(ERROR) << "bonus file not supported in flash mode";
112 return 1;
Tao Baoabba55b2015-07-17 18:11:12 -0700113 }
114 return FlashMode(argv[1], argv[2], argv[3], target_size);
115 }
Tao Bao36c35112016-10-25 14:17:26 -0700116
Tianjie Xuaced5d92016-10-12 10:55:04 -0700117 std::vector<std::string> sha1s;
Yabin Cuid6c93af2016-02-10 16:41:10 -0800118 std::vector<FileContents> files;
119 if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &files)) {
Tao Bao859bfc52018-04-25 23:00:27 -0700120 LOG(ERROR) << "Failed to parse patch args";
121 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800122 }
Tao Baofada91c2016-10-27 18:16:06 -0700123
124 std::vector<std::unique_ptr<Value>> patches;
Yabin Cuid6c93af2016-02-10 16:41:10 -0800125 for (size_t i = 0; i < files.size(); ++i) {
Tao Baofada91c2016-10-27 18:16:06 -0700126 patches.push_back(std::make_unique<Value>(
127 VAL_BLOB, std::string(files[i].data.cbegin(), files[i].data.cend())));
Doug Zongkerc4351c72010-02-22 14:46:32 -0800128 }
Tao Baofada91c2016-10-27 18:16:06 -0700129 return applypatch(argv[1], argv[2], argv[3], target_size, sha1s, patches, &bonus);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800130}
Doug Zongker512536a2010-02-17 16:11:44 -0800131
Tao Bao36c35112016-10-25 14:17:26 -0700132// This program (applypatch) applies binary patches to files in a way that
133// is safe (the original file is not touched until we have the desired
Doug Zongker512536a2010-02-17 16:11:44 -0800134// replacement for it) and idempotent (it's okay to run this program
135// multiple times).
136//
137// - if the sha1 hash of <tgt-file> is <tgt-sha1>, does nothing and exits
138// successfully.
139//
Tao Baoabba55b2015-07-17 18:11:12 -0700140// - otherwise, if no <src-sha1>:<patch> is provided, flashes <tgt-file> with
141// <src-file>. <tgt-file> must be a partition name, while <src-file> must
142// be a regular image file. <src-file> will not be deleted on success.
143//
Doug Zongker512536a2010-02-17 16:11:44 -0800144// - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the
145// bsdiff <patch> to <src-file> to produce a new file (the type of patch
146// is automatically detected from the file header). If that new
147// file has sha1 hash <tgt-sha1>, moves it to replace <tgt-file>, and
148// exits successfully. Note that if <src-file> and <tgt-file> are
149// not the same, <src-file> is NOT deleted on success. <tgt-file>
150// may be the string "-" to mean "the same as src-file".
151//
152// - otherwise, or if any error is encountered, exits with non-zero
153// status.
154//
Elliott Hughes63a31922016-06-09 17:41:22 -0700155// <src-file> (or <file> in check mode) may refer to an EMMC partition
Doug Zongker512536a2010-02-17 16:11:44 -0800156// to read the source data. See the comments for the
Elliott Hughes63a31922016-06-09 17:41:22 -0700157// LoadPartitionContents() function for the format of such a filename.
Doug Zongker512536a2010-02-17 16:11:44 -0800158
Tao Bao36c35112016-10-25 14:17:26 -0700159int applypatch_modes(int argc, const char** argv) {
Tao Bao859bfc52018-04-25 23:00:27 -0700160 if (argc < 2) {
161 usage:
162 // clang-format off
163 LOG(INFO) << "Usage: \n"
164 << " " << argv[0] << " [-b <bonus-file>] <src-file> <tgt-file> <tgt-sha1> "
165 "<tgt-size> [<src-sha1>:<patch> ...]\n"
166 << " " << argv[0] << " -c <file> [<sha1> ...]\n"
167 << " " << argv[0] << " -l\n"
168 << "\n"
169 << "Filenames may be of the form\n"
170 << " EMMC:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n"
171 << "to specify reading from or writing to an EMMC partition.\n\n";
172 // clang-format on
173 return 2;
174 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800175
Tao Bao859bfc52018-04-25 23:00:27 -0700176 int result;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800177
Tao Bao859bfc52018-04-25 23:00:27 -0700178 if (strncmp(argv[1], "-l", 3) == 0) {
179 result = ShowLicenses();
180 } else if (strncmp(argv[1], "-c", 3) == 0) {
181 result = CheckMode(argc, argv);
182 } else {
183 result = PatchMode(argc, argv);
184 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800185
Tao Bao859bfc52018-04-25 23:00:27 -0700186 if (result == 2) {
187 goto usage;
188 }
189 return result;
Doug Zongker512536a2010-02-17 16:11:44 -0800190}