blob: 966d8b91f5f8f166b998f81a7dadcd795db75111 [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
Doug Zongkerc4351c72010-02-22 14:46:32 -080022#include "applypatch.h"
23#include "edify/expr.h"
24#include "mincrypt/sha.h"
25
Tao Bao485b6372015-06-23 23:23:33 -070026static int CheckMode(int argc, char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080027 if (argc < 3) {
28 return 2;
29 }
30 return applypatch_check(argv[2], argc-3, argv+3);
31}
32
Tao Bao485b6372015-06-23 23:23:33 -070033static int SpaceMode(int argc, char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080034 if (argc != 3) {
35 return 2;
36 }
37 char* endptr;
38 size_t bytes = strtol(argv[2], &endptr, 10);
39 if (bytes == 0 && endptr == argv[2]) {
40 printf("can't parse \"%s\" as byte count\n\n", argv[2]);
41 return 1;
42 }
43 return CacheSizeCheck(bytes);
44}
45
46// Parse arguments (which should be of the form "<sha1>" or
47// "<sha1>:<filename>" into the new parallel arrays *sha1s and
Tao Bao485b6372015-06-23 23:23:33 -070048// *patches (loading file contents into the patches). Returns true on
Doug Zongkerc4351c72010-02-22 14:46:32 -080049// success.
Tao Bao68c5a672015-07-17 18:11:12 -070050static bool ParsePatchArgs(int argc, char** argv, char*** sha1s,
51 Value*** patches, int* num_patches) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080052 *num_patches = argc;
Tao Bao485b6372015-06-23 23:23:33 -070053 *sha1s = reinterpret_cast<char**>(malloc(*num_patches * sizeof(char*)));
54 *patches = reinterpret_cast<Value**>(malloc(*num_patches * sizeof(Value*)));
Doug Zongkerc4351c72010-02-22 14:46:32 -080055 memset(*patches, 0, *num_patches * sizeof(Value*));
56
57 uint8_t digest[SHA_DIGEST_SIZE];
58
Tao Bao485b6372015-06-23 23:23:33 -070059 for (int i = 0; i < *num_patches; ++i) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080060 char* colon = strchr(argv[i], ':');
61 if (colon != NULL) {
62 *colon = '\0';
63 ++colon;
64 }
65
66 if (ParseSha1(argv[i], digest) != 0) {
67 printf("failed to parse sha1 \"%s\"\n", argv[i]);
Tao Bao485b6372015-06-23 23:23:33 -070068 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080069 }
70
71 (*sha1s)[i] = argv[i];
72 if (colon == NULL) {
73 (*patches)[i] = NULL;
74 } else {
75 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -080076 if (LoadFileContents(colon, &fc) != 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080077 goto abort;
78 }
Tao Bao485b6372015-06-23 23:23:33 -070079 (*patches)[i] = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongkerc4351c72010-02-22 14:46:32 -080080 (*patches)[i]->type = VAL_BLOB;
81 (*patches)[i]->size = fc.size;
Tao Bao485b6372015-06-23 23:23:33 -070082 (*patches)[i]->data = reinterpret_cast<char*>(fc.data);
Doug Zongkerc4351c72010-02-22 14:46:32 -080083 }
84 }
85
Tao Bao485b6372015-06-23 23:23:33 -070086 return true;
Doug Zongkerc4351c72010-02-22 14:46:32 -080087
88 abort:
Tao Bao485b6372015-06-23 23:23:33 -070089 for (int i = 0; i < *num_patches; ++i) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080090 Value* p = (*patches)[i];
91 if (p != NULL) {
92 free(p->data);
93 free(p);
94 }
95 }
96 free(*sha1s);
97 free(*patches);
Tao Bao485b6372015-06-23 23:23:33 -070098 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080099}
100
Tao Bao68c5a672015-07-17 18:11:12 -0700101static int FlashMode(const char* src_filename, const char* tgt_filename,
102 const char* tgt_sha1, size_t tgt_size) {
103 return applypatch_flash(src_filename, tgt_filename, tgt_sha1, tgt_size);
104}
105
106static int PatchMode(int argc, char** argv) {
Doug Zongkera3ccba62012-08-20 15:28:02 -0700107 Value* bonus = NULL;
108 if (argc >= 3 && strcmp(argv[1], "-b") == 0) {
109 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -0800110 if (LoadFileContents(argv[2], &fc) != 0) {
Doug Zongkera3ccba62012-08-20 15:28:02 -0700111 printf("failed to load bonus file %s\n", argv[2]);
112 return 1;
113 }
Tao Bao485b6372015-06-23 23:23:33 -0700114 bonus = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongkera3ccba62012-08-20 15:28:02 -0700115 bonus->type = VAL_BLOB;
116 bonus->size = fc.size;
117 bonus->data = (char*)fc.data;
118 argc -= 2;
119 argv += 2;
120 }
121
Tao Bao68c5a672015-07-17 18:11:12 -0700122 if (argc < 4) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800123 return 2;
124 }
125
126 char* endptr;
127 size_t target_size = strtol(argv[4], &endptr, 10);
128 if (target_size == 0 && endptr == argv[4]) {
129 printf("can't parse \"%s\" as byte count\n\n", argv[4]);
130 return 1;
131 }
132
Tao Bao68c5a672015-07-17 18:11:12 -0700133 // If no <src-sha1>:<patch> is provided, it is in flash mode.
134 if (argc == 5) {
135 if (bonus != NULL) {
136 printf("bonus file not supported in flash mode\n");
137 return 1;
138 }
139 return FlashMode(argv[1], argv[2], argv[3], target_size);
140 }
141
142
Doug Zongkerc4351c72010-02-22 14:46:32 -0800143 char** sha1s;
144 Value** patches;
145 int num_patches;
Tao Bao485b6372015-06-23 23:23:33 -0700146 if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &patches, &num_patches)) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800147 printf("failed to parse patch args\n");
148 return 1;
149 }
150
151 int result = applypatch(argv[1], argv[2], argv[3], target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -0700152 num_patches, sha1s, patches, bonus);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800153
154 int i;
155 for (i = 0; i < num_patches; ++i) {
156 Value* p = patches[i];
157 if (p != NULL) {
158 free(p->data);
159 free(p);
160 }
161 }
Doug Zongkera3ccba62012-08-20 15:28:02 -0700162 if (bonus) {
163 free(bonus->data);
164 free(bonus);
165 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800166 free(sha1s);
167 free(patches);
168
169 return result;
170}
Doug Zongker512536a2010-02-17 16:11:44 -0800171
172// This program applies binary patches to files in a way that is safe
173// (the original file is not touched until we have the desired
174// replacement for it) and idempotent (it's okay to run this program
175// multiple times).
176//
177// - if the sha1 hash of <tgt-file> is <tgt-sha1>, does nothing and exits
178// successfully.
179//
Tao Bao68c5a672015-07-17 18:11:12 -0700180// - otherwise, if no <src-sha1>:<patch> is provided, flashes <tgt-file> with
181// <src-file>. <tgt-file> must be a partition name, while <src-file> must
182// be a regular image file. <src-file> will not be deleted on success.
183//
Doug Zongker512536a2010-02-17 16:11:44 -0800184// - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the
185// bsdiff <patch> to <src-file> to produce a new file (the type of patch
186// is automatically detected from the file header). If that new
187// file has sha1 hash <tgt-sha1>, moves it to replace <tgt-file>, and
188// exits successfully. Note that if <src-file> and <tgt-file> are
189// not the same, <src-file> is NOT deleted on success. <tgt-file>
190// may be the string "-" to mean "the same as src-file".
191//
192// - otherwise, or if any error is encountered, exits with non-zero
193// status.
194//
195// <src-file> (or <file> in check mode) may refer to an MTD partition
196// to read the source data. See the comments for the
197// LoadMTDContents() function above for the format of such a filename.
198
199int main(int argc, char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800200 if (argc < 2) {
201 usage:
202 printf(
Doug Zongkera3ccba62012-08-20 15:28:02 -0700203 "usage: %s [-b <bonus-file>] <src-file> <tgt-file> <tgt-sha1> <tgt-size> "
Doug Zongker512536a2010-02-17 16:11:44 -0800204 "[<src-sha1>:<patch> ...]\n"
205 " or %s -c <file> [<sha1> ...]\n"
206 " or %s -s <bytes>\n"
207 " or %s -l\n"
208 "\n"
209 "Filenames may be of the form\n"
210 " MTD:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n"
211 "to specify reading from or writing to an MTD partition.\n\n",
212 argv[0], argv[0], argv[0], argv[0]);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800213 return 2;
214 }
215
216 int result;
217
218 if (strncmp(argv[1], "-l", 3) == 0) {
219 result = ShowLicenses();
220 } else if (strncmp(argv[1], "-c", 3) == 0) {
221 result = CheckMode(argc, argv);
222 } else if (strncmp(argv[1], "-s", 3) == 0) {
223 result = SpaceMode(argc, argv);
224 } else {
225 result = PatchMode(argc, argv);
226 }
227
228 if (result == 2) {
229 goto usage;
230 }
231 return result;
Doug Zongker512536a2010-02-17 16:11:44 -0800232}