1 /*
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>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 
22 #include "applypatch.h"
23 #include "edify/expr.h"
24 #include "mincrypt/sha.h"
25 
CheckMode(int argc,char ** argv)26 int 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 
SpaceMode(int argc,char ** argv)33 int 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.
ParsePatchArgs(int argc,char ** argv,char *** sha1s,Value *** patches,int * num_patches)50 static 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;
77             if (LoadFileContents(colon, &fc) != 0) {
78                 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 
PatchMode(int argc,char ** argv)102 int PatchMode(int argc, char** argv) {
103     Value* bonus = NULL;
104     if (argc >= 3 && strcmp(argv[1], "-b") == 0) {
105         FileContents fc;
106         if (LoadFileContents(argv[2], &fc) != 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 
118     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,
138                             num_patches, sha1s, patches, bonus);
139 
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     }
148     if (bonus) {
149         free(bonus->data);
150         free(bonus);
151     }
152     free(sha1s);
153     free(patches);
154 
155     return result;
156 }
157 
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 
main(int argc,char ** argv)181 int main(int argc, char** argv) {
182     if (argc < 2) {
183       usage:
184         printf(
185             "usage: %s [-b <bonus-file>] <src-file> <tgt-file> <tgt-sha1> <tgt-size> "
186             "[<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]);
195         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;
214 }
215