blob: 3917f86edd16348ef15df96a6a365673ac564374 [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;
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
102int PatchMode(int argc, char** argv) {
103 if (argc < 6) {
104 return 2;
105 }
106
107 char* endptr;
108 size_t target_size = strtol(argv[4], &endptr, 10);
109 if (target_size == 0 && endptr == argv[4]) {
110 printf("can't parse \"%s\" as byte count\n\n", argv[4]);
111 return 1;
112 }
113
114 char** sha1s;
115 Value** patches;
116 int num_patches;
117 if (ParsePatchArgs(argc-5, argv+5, &sha1s, &patches, &num_patches) != 0) {
118 printf("failed to parse patch args\n");
119 return 1;
120 }
121
122 int result = applypatch(argv[1], argv[2], argv[3], target_size,
123 num_patches, sha1s, patches);
124
125 int i;
126 for (i = 0; i < num_patches; ++i) {
127 Value* p = patches[i];
128 if (p != NULL) {
129 free(p->data);
130 free(p);
131 }
132 }
133 free(sha1s);
134 free(patches);
135
136 return result;
137}
Doug Zongker512536a2010-02-17 16:11:44 -0800138
139// This program applies binary patches to files in a way that is safe
140// (the original file is not touched until we have the desired
141// replacement for it) and idempotent (it's okay to run this program
142// multiple times).
143//
144// - if the sha1 hash of <tgt-file> is <tgt-sha1>, does nothing and exits
145// successfully.
146//
147// - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the
148// bsdiff <patch> to <src-file> to produce a new file (the type of patch
149// is automatically detected from the file header). If that new
150// file has sha1 hash <tgt-sha1>, moves it to replace <tgt-file>, and
151// exits successfully. Note that if <src-file> and <tgt-file> are
152// not the same, <src-file> is NOT deleted on success. <tgt-file>
153// may be the string "-" to mean "the same as src-file".
154//
155// - otherwise, or if any error is encountered, exits with non-zero
156// status.
157//
158// <src-file> (or <file> in check mode) may refer to an MTD partition
159// to read the source data. See the comments for the
160// LoadMTDContents() function above for the format of such a filename.
161
162int main(int argc, char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800163 if (argc < 2) {
164 usage:
165 printf(
Doug Zongker512536a2010-02-17 16:11:44 -0800166 "usage: %s <src-file> <tgt-file> <tgt-sha1> <tgt-size> "
167 "[<src-sha1>:<patch> ...]\n"
168 " or %s -c <file> [<sha1> ...]\n"
169 " or %s -s <bytes>\n"
170 " or %s -l\n"
171 "\n"
172 "Filenames may be of the form\n"
173 " MTD:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n"
174 "to specify reading from or writing to an MTD partition.\n\n",
175 argv[0], argv[0], argv[0], argv[0]);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800176 return 2;
177 }
178
179 int result;
180
181 if (strncmp(argv[1], "-l", 3) == 0) {
182 result = ShowLicenses();
183 } else if (strncmp(argv[1], "-c", 3) == 0) {
184 result = CheckMode(argc, argv);
185 } else if (strncmp(argv[1], "-s", 3) == 0) {
186 result = SpaceMode(argc, argv);
187 } else {
188 result = PatchMode(argc, argv);
189 }
190
191 if (result == 2) {
192 goto usage;
193 }
194 return result;
Doug Zongker512536a2010-02-17 16:11:44 -0800195}