blob: 7606d5d3cdf8358847e270d59dfdf12866a56afd [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
17#include <stdio.h>
Doug Zongkerc4351c72010-02-22 14:46:32 -080018#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
Doug Zongker512536a2010-02-17 16:11:44 -080021
Yabin Cuid483c202016-02-03 17:08:52 -080022#include <memory>
23#include <vector>
24
Doug Zongkerc4351c72010-02-22 14:46:32 -080025#include "applypatch.h"
26#include "edify/expr.h"
Sen Jiangc48cb5e2016-02-04 16:23:21 +080027#include "openssl/sha.h"
Doug Zongkerc4351c72010-02-22 14:46:32 -080028
Tao Baoba9a42a2015-06-23 23:23:33 -070029static int CheckMode(int argc, char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080030 if (argc < 3) {
31 return 2;
32 }
33 return applypatch_check(argv[2], argc-3, argv+3);
34}
35
Tao Baoba9a42a2015-06-23 23:23:33 -070036static int SpaceMode(int argc, char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080037 if (argc != 3) {
38 return 2;
39 }
40 char* endptr;
41 size_t bytes = strtol(argv[2], &endptr, 10);
42 if (bytes == 0 && endptr == argv[2]) {
43 printf("can't parse \"%s\" as byte count\n\n", argv[2]);
44 return 1;
45 }
46 return CacheSizeCheck(bytes);
47}
48
49// Parse arguments (which should be of the form "<sha1>" or
50// "<sha1>:<filename>" into the new parallel arrays *sha1s and
Tao Baoba9a42a2015-06-23 23:23:33 -070051// *patches (loading file contents into the patches). Returns true on
Doug Zongkerc4351c72010-02-22 14:46:32 -080052// success.
Yabin Cuid483c202016-02-03 17:08:52 -080053static bool ParsePatchArgs(int argc, char** argv, std::vector<char*>* sha1s,
54 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>>* patches) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +080055 uint8_t digest[SHA_DIGEST_LENGTH];
Doug Zongkerc4351c72010-02-22 14:46:32 -080056
Yabin Cuid483c202016-02-03 17:08:52 -080057 for (int i = 0; i < argc; ++i) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080058 char* colon = strchr(argv[i], ':');
59 if (colon != NULL) {
60 *colon = '\0';
61 ++colon;
62 }
63
64 if (ParseSha1(argv[i], digest) != 0) {
65 printf("failed to parse sha1 \"%s\"\n", argv[i]);
Tao Baoba9a42a2015-06-23 23:23:33 -070066 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080067 }
68
Yabin Cuid483c202016-02-03 17:08:52 -080069 sha1s->push_back(argv[i]);
Doug Zongkerc4351c72010-02-22 14:46:32 -080070 if (colon == NULL) {
Yabin Cuid483c202016-02-03 17:08:52 -080071 patches->emplace_back(nullptr, FreeValue);
Doug Zongkerc4351c72010-02-22 14:46:32 -080072 } else {
73 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -080074 if (LoadFileContents(colon, &fc) != 0) {
Yabin Cuid483c202016-02-03 17:08:52 -080075 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080076 }
Yabin Cuid483c202016-02-03 17:08:52 -080077 std::unique_ptr<Value, decltype(&FreeValue)> value(new Value, FreeValue);
78 value->type = VAL_BLOB;
79 value->size = fc.size;
80 value->data = reinterpret_cast<char*>(fc.data);
81 patches->push_back(std::move(value));
Doug Zongkerc4351c72010-02-22 14:46:32 -080082 }
83 }
Tao Baoba9a42a2015-06-23 23:23:33 -070084 return true;
Doug Zongkerc4351c72010-02-22 14:46:32 -080085}
86
Tao Baoabba55b2015-07-17 18:11:12 -070087static int FlashMode(const char* src_filename, const char* tgt_filename,
88 const char* tgt_sha1, size_t tgt_size) {
89 return applypatch_flash(src_filename, tgt_filename, tgt_sha1, tgt_size);
90}
91
92static int PatchMode(int argc, char** argv) {
Yabin Cuid483c202016-02-03 17:08:52 -080093 std::unique_ptr<Value, decltype(&FreeValue)> bonus(nullptr, FreeValue);
Doug Zongkera3ccba62012-08-20 15:28:02 -070094 if (argc >= 3 && strcmp(argv[1], "-b") == 0) {
95 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -080096 if (LoadFileContents(argv[2], &fc) != 0) {
Doug Zongkera3ccba62012-08-20 15:28:02 -070097 printf("failed to load bonus file %s\n", argv[2]);
98 return 1;
99 }
Yabin Cuid483c202016-02-03 17:08:52 -0800100 bonus.reset(new Value);
Doug Zongkera3ccba62012-08-20 15:28:02 -0700101 bonus->type = VAL_BLOB;
102 bonus->size = fc.size;
Yabin Cuid483c202016-02-03 17:08:52 -0800103 bonus->data = reinterpret_cast<char*>(fc.data);
Doug Zongkera3ccba62012-08-20 15:28:02 -0700104 argc -= 2;
105 argv += 2;
106 }
107
Tao Baoabba55b2015-07-17 18:11:12 -0700108 if (argc < 4) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800109 return 2;
110 }
111
112 char* endptr;
113 size_t target_size = strtol(argv[4], &endptr, 10);
114 if (target_size == 0 && endptr == argv[4]) {
115 printf("can't parse \"%s\" as byte count\n\n", argv[4]);
116 return 1;
117 }
118
Tao Baoabba55b2015-07-17 18:11:12 -0700119 // If no <src-sha1>:<patch> is provided, it is in flash mode.
120 if (argc == 5) {
121 if (bonus != NULL) {
122 printf("bonus file not supported in flash mode\n");
123 return 1;
124 }
125 return FlashMode(argv[1], argv[2], argv[3], target_size);
126 }
127
128
Yabin Cuid483c202016-02-03 17:08:52 -0800129 std::vector<char*> sha1s;
130 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches;
131 if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &patches)) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800132 printf("failed to parse patch args\n");
133 return 1;
134 }
135
Yabin Cuid483c202016-02-03 17:08:52 -0800136 std::vector<Value*> patch_ptrs;
137 for (const auto& p : patches) {
138 patch_ptrs.push_back(p.get());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800139 }
Yabin Cuid483c202016-02-03 17:08:52 -0800140 return applypatch(argv[1], argv[2], argv[3], target_size,
141 patch_ptrs.size(), sha1s.data(),
142 patch_ptrs.data(), bonus.get());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800143}
Doug Zongker512536a2010-02-17 16:11:44 -0800144
145// This program applies binary patches to files in a way that is safe
146// (the original file is not touched until we have the desired
147// replacement for it) and idempotent (it's okay to run this program
148// multiple times).
149//
150// - if the sha1 hash of <tgt-file> is <tgt-sha1>, does nothing and exits
151// successfully.
152//
Tao Baoabba55b2015-07-17 18:11:12 -0700153// - otherwise, if no <src-sha1>:<patch> is provided, flashes <tgt-file> with
154// <src-file>. <tgt-file> must be a partition name, while <src-file> must
155// be a regular image file. <src-file> will not be deleted on success.
156//
Doug Zongker512536a2010-02-17 16:11:44 -0800157// - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the
158// bsdiff <patch> to <src-file> to produce a new file (the type of patch
159// is automatically detected from the file header). If that new
160// file has sha1 hash <tgt-sha1>, moves it to replace <tgt-file>, and
161// exits successfully. Note that if <src-file> and <tgt-file> are
162// not the same, <src-file> is NOT deleted on success. <tgt-file>
163// may be the string "-" to mean "the same as src-file".
164//
165// - otherwise, or if any error is encountered, exits with non-zero
166// status.
167//
168// <src-file> (or <file> in check mode) may refer to an MTD partition
169// to read the source data. See the comments for the
170// LoadMTDContents() function above for the format of such a filename.
171
172int main(int argc, char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800173 if (argc < 2) {
174 usage:
175 printf(
Doug Zongkera3ccba62012-08-20 15:28:02 -0700176 "usage: %s [-b <bonus-file>] <src-file> <tgt-file> <tgt-sha1> <tgt-size> "
Doug Zongker512536a2010-02-17 16:11:44 -0800177 "[<src-sha1>:<patch> ...]\n"
178 " or %s -c <file> [<sha1> ...]\n"
179 " or %s -s <bytes>\n"
180 " or %s -l\n"
181 "\n"
182 "Filenames may be of the form\n"
183 " MTD:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n"
184 "to specify reading from or writing to an MTD partition.\n\n",
185 argv[0], argv[0], argv[0], argv[0]);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800186 return 2;
187 }
188
189 int result;
190
191 if (strncmp(argv[1], "-l", 3) == 0) {
192 result = ShowLicenses();
193 } else if (strncmp(argv[1], "-c", 3) == 0) {
194 result = CheckMode(argc, argv);
195 } else if (strncmp(argv[1], "-s", 3) == 0) {
196 result = SpaceMode(argc, argv);
197 } else {
198 result = PatchMode(argc, argv);
199 }
200
201 if (result == 2) {
202 goto usage;
203 }
204 return result;
Doug Zongker512536a2010-02-17 16:11:44 -0800205}