blob: 63ff5c2c04d22fb784f6a3c6dfd85f9fd9c5f575 [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 Bao485b6372015-06-23 23:23:33 -070050static bool ParsePatchArgs(int argc, char** argv,
Doug Zongkerc4351c72010-02-22 14:46:32 -080051 char*** sha1s, Value*** patches, int* num_patches) {
52 *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
101int PatchMode(int argc, char** argv) {
Doug Zongkera3ccba62012-08-20 15:28:02 -0700102 Value* bonus = NULL;
103 if (argc >= 3 && strcmp(argv[1], "-b") == 0) {
104 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -0800105 if (LoadFileContents(argv[2], &fc) != 0) {
Doug Zongkera3ccba62012-08-20 15:28:02 -0700106 printf("failed to load bonus file %s\n", argv[2]);
107 return 1;
108 }
Tao Bao485b6372015-06-23 23:23:33 -0700109 bonus = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongkera3ccba62012-08-20 15:28:02 -0700110 bonus->type = VAL_BLOB;
111 bonus->size = fc.size;
112 bonus->data = (char*)fc.data;
113 argc -= 2;
114 argv += 2;
115 }
116
Doug Zongkerc4351c72010-02-22 14:46:32 -0800117 if (argc < 6) {
118 return 2;
119 }
120
121 char* endptr;
122 size_t target_size = strtol(argv[4], &endptr, 10);
123 if (target_size == 0 && endptr == argv[4]) {
124 printf("can't parse \"%s\" as byte count\n\n", argv[4]);
125 return 1;
126 }
127
128 char** sha1s;
129 Value** patches;
130 int num_patches;
Tao Bao485b6372015-06-23 23:23:33 -0700131 if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &patches, &num_patches)) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800132 printf("failed to parse patch args\n");
133 return 1;
134 }
135
136 int result = applypatch(argv[1], argv[2], argv[3], target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -0700137 num_patches, sha1s, patches, bonus);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800138
139 int i;
140 for (i = 0; i < num_patches; ++i) {
141 Value* p = patches[i];
142 if (p != NULL) {
143 free(p->data);
144 free(p);
145 }
146 }
Doug Zongkera3ccba62012-08-20 15:28:02 -0700147 if (bonus) {
148 free(bonus->data);
149 free(bonus);
150 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800151 free(sha1s);
152 free(patches);
153
154 return result;
155}
Doug Zongker512536a2010-02-17 16:11:44 -0800156
157// This program applies binary patches to files in a way that is safe
158// (the original file is not touched until we have the desired
159// replacement for it) and idempotent (it's okay to run this program
160// multiple times).
161//
162// - if the sha1 hash of <tgt-file> is <tgt-sha1>, does nothing and exits
163// successfully.
164//
165// - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the
166// bsdiff <patch> to <src-file> to produce a new file (the type of patch
167// is automatically detected from the file header). If that new
168// file has sha1 hash <tgt-sha1>, moves it to replace <tgt-file>, and
169// exits successfully. Note that if <src-file> and <tgt-file> are
170// not the same, <src-file> is NOT deleted on success. <tgt-file>
171// may be the string "-" to mean "the same as src-file".
172//
173// - otherwise, or if any error is encountered, exits with non-zero
174// status.
175//
176// <src-file> (or <file> in check mode) may refer to an MTD partition
177// to read the source data. See the comments for the
178// LoadMTDContents() function above for the format of such a filename.
179
180int main(int argc, char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800181 if (argc < 2) {
182 usage:
183 printf(
Doug Zongkera3ccba62012-08-20 15:28:02 -0700184 "usage: %s [-b <bonus-file>] <src-file> <tgt-file> <tgt-sha1> <tgt-size> "
Doug Zongker512536a2010-02-17 16:11:44 -0800185 "[<src-sha1>:<patch> ...]\n"
186 " or %s -c <file> [<sha1> ...]\n"
187 " or %s -s <bytes>\n"
188 " or %s -l\n"
189 "\n"
190 "Filenames may be of the form\n"
191 " MTD:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n"
192 "to specify reading from or writing to an MTD partition.\n\n",
193 argv[0], argv[0], argv[0], argv[0]);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800194 return 2;
195 }
196
197 int result;
198
199 if (strncmp(argv[1], "-l", 3) == 0) {
200 result = ShowLicenses();
201 } else if (strncmp(argv[1], "-c", 3) == 0) {
202 result = CheckMode(argc, argv);
203 } else if (strncmp(argv[1], "-s", 3) == 0) {
204 result = SpaceMode(argc, argv);
205 } else {
206 result = PatchMode(argc, argv);
207 }
208
209 if (result == 2) {
210 goto usage;
211 }
212 return result;
Doug Zongker512536a2010-02-17 16:11:44 -0800213}