blob: f61db5d9ebf3ed000fda0522ff9b0810026812a5 [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
26int CheckMode(int argc, char** argv) {
27 if (argc < 3) {
28 return 2;
29 }
30 return applypatch_check(argv[2], argc-3, argv+3);
31}
32
33int SpaceMode(int argc, char** argv) {
34 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
48// *patches (loading file contents into the patches). Returns 0 on
49// success.
50static int ParsePatchArgs(int argc, char** argv,
51 char*** sha1s, Value*** patches, int* num_patches) {
52 *num_patches = argc;
53 *sha1s = malloc(*num_patches * sizeof(char*));
54 *patches = malloc(*num_patches * sizeof(Value*));
55 memset(*patches, 0, *num_patches * sizeof(Value*));
56
57 uint8_t digest[SHA_DIGEST_SIZE];
58
59 int i;
60 for (i = 0; i < *num_patches; ++i) {
61 char* colon = strchr(argv[i], ':');
62 if (colon != NULL) {
63 *colon = '\0';
64 ++colon;
65 }
66
67 if (ParseSha1(argv[i], digest) != 0) {
68 printf("failed to parse sha1 \"%s\"\n", argv[i]);
69 return -1;
70 }
71
72 (*sha1s)[i] = argv[i];
73 if (colon == NULL) {
74 (*patches)[i] = NULL;
75 } else {
76 FileContents fc;
Hristo Bojinovdb314d62010-08-02 10:29:49 -070077 if (LoadFileContents(colon, &fc, RETOUCH_DONT_MASK) != 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080078 goto abort;
79 }
80 (*patches)[i] = malloc(sizeof(Value));
81 (*patches)[i]->type = VAL_BLOB;
82 (*patches)[i]->size = fc.size;
83 (*patches)[i]->data = (char*)fc.data;
84 }
85 }
86
87 return 0;
88
89 abort:
90 for (i = 0; i < *num_patches; ++i) {
91 Value* p = (*patches)[i];
92 if (p != NULL) {
93 free(p->data);
94 free(p);
95 }
96 }
97 free(*sha1s);
98 free(*patches);
99 return -1;
100}
101
102int PatchMode(int argc, char** argv) {
Doug Zongkera3ccba62012-08-20 15:28:02 -0700103 Value* bonus = NULL;
104 if (argc >= 3 && strcmp(argv[1], "-b") == 0) {
105 FileContents fc;
106 if (LoadFileContents(argv[2], &fc, RETOUCH_DONT_MASK) != 0) {
107 printf("failed to load bonus file %s\n", argv[2]);
108 return 1;
109 }
110 bonus = malloc(sizeof(Value));
111 bonus->type = VAL_BLOB;
112 bonus->size = fc.size;
113 bonus->data = (char*)fc.data;
114 argc -= 2;
115 argv += 2;
116 }
117
Doug Zongkerc4351c72010-02-22 14:46:32 -0800118 if (argc < 6) {
119 return 2;
120 }
121
122 char* endptr;
123 size_t target_size = strtol(argv[4], &endptr, 10);
124 if (target_size == 0 && endptr == argv[4]) {
125 printf("can't parse \"%s\" as byte count\n\n", argv[4]);
126 return 1;
127 }
128
129 char** sha1s;
130 Value** patches;
131 int num_patches;
132 if (ParsePatchArgs(argc-5, argv+5, &sha1s, &patches, &num_patches) != 0) {
133 printf("failed to parse patch args\n");
134 return 1;
135 }
136
137 int result = applypatch(argv[1], argv[2], argv[3], target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -0700138 num_patches, sha1s, patches, bonus);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800139
140 int i;
141 for (i = 0; i < num_patches; ++i) {
142 Value* p = patches[i];
143 if (p != NULL) {
144 free(p->data);
145 free(p);
146 }
147 }
Doug Zongkera3ccba62012-08-20 15:28:02 -0700148 if (bonus) {
149 free(bonus->data);
150 free(bonus);
151 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800152 free(sha1s);
153 free(patches);
154
155 return result;
156}
Doug Zongker512536a2010-02-17 16:11:44 -0800157
158// This program applies binary patches to files in a way that is safe
159// (the original file is not touched until we have the desired
160// replacement for it) and idempotent (it's okay to run this program
161// multiple times).
162//
163// - if the sha1 hash of <tgt-file> is <tgt-sha1>, does nothing and exits
164// successfully.
165//
166// - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the
167// bsdiff <patch> to <src-file> to produce a new file (the type of patch
168// is automatically detected from the file header). If that new
169// file has sha1 hash <tgt-sha1>, moves it to replace <tgt-file>, and
170// exits successfully. Note that if <src-file> and <tgt-file> are
171// not the same, <src-file> is NOT deleted on success. <tgt-file>
172// may be the string "-" to mean "the same as src-file".
173//
174// - otherwise, or if any error is encountered, exits with non-zero
175// status.
176//
177// <src-file> (or <file> in check mode) may refer to an MTD partition
178// to read the source data. See the comments for the
179// LoadMTDContents() function above for the format of such a filename.
180
181int main(int argc, char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800182 if (argc < 2) {
183 usage:
184 printf(
Doug Zongkera3ccba62012-08-20 15:28:02 -0700185 "usage: %s [-b <bonus-file>] <src-file> <tgt-file> <tgt-sha1> <tgt-size> "
Doug Zongker512536a2010-02-17 16:11:44 -0800186 "[<src-sha1>:<patch> ...]\n"
187 " or %s -c <file> [<sha1> ...]\n"
188 " or %s -s <bytes>\n"
189 " or %s -l\n"
190 "\n"
191 "Filenames may be of the form\n"
192 " MTD:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n"
193 "to specify reading from or writing to an MTD partition.\n\n",
194 argv[0], argv[0], argv[0], argv[0]);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800195 return 2;
196 }
197
198 int result;
199
200 if (strncmp(argv[1], "-l", 3) == 0) {
201 result = ShowLicenses();
202 } else if (strncmp(argv[1], "-c", 3) == 0) {
203 result = CheckMode(argc, argv);
204 } else if (strncmp(argv[1], "-s", 3) == 0) {
205 result = SpaceMode(argc, argv);
206 } else {
207 result = PatchMode(argc, argv);
208 }
209
210 if (result == 2) {
211 goto usage;
212 }
213 return result;
Doug Zongker512536a2010-02-17 16:11:44 -0800214}