Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 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> |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <unistd.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 21 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 22 | #include "applypatch.h" |
| 23 | #include "edify/expr.h" |
| 24 | #include "mincrypt/sha.h" |
| 25 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 26 | static int CheckMode(int argc, char** argv) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 27 | if (argc < 3) { |
| 28 | return 2; |
| 29 | } |
| 30 | return applypatch_check(argv[2], argc-3, argv+3); |
| 31 | } |
| 32 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 33 | static int SpaceMode(int argc, char** argv) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 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 |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 48 | // *patches (loading file contents into the patches). Returns true on |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 49 | // success. |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 50 | static bool ParsePatchArgs(int argc, char** argv, |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 51 | char*** sha1s, Value*** patches, int* num_patches) { |
| 52 | *num_patches = argc; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 53 | *sha1s = reinterpret_cast<char**>(malloc(*num_patches * sizeof(char*))); |
| 54 | *patches = reinterpret_cast<Value**>(malloc(*num_patches * sizeof(Value*))); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 55 | memset(*patches, 0, *num_patches * sizeof(Value*)); |
| 56 | |
| 57 | uint8_t digest[SHA_DIGEST_SIZE]; |
| 58 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 59 | for (int i = 0; i < *num_patches; ++i) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 60 | 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 Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 68 | return false; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | (*sha1s)[i] = argv[i]; |
| 72 | if (colon == NULL) { |
| 73 | (*patches)[i] = NULL; |
| 74 | } else { |
| 75 | FileContents fc; |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 76 | if (LoadFileContents(colon, &fc) != 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 77 | goto abort; |
| 78 | } |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 79 | (*patches)[i] = reinterpret_cast<Value*>(malloc(sizeof(Value))); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 80 | (*patches)[i]->type = VAL_BLOB; |
| 81 | (*patches)[i]->size = fc.size; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 82 | (*patches)[i]->data = reinterpret_cast<char*>(fc.data); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 86 | return true; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 87 | |
| 88 | abort: |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 89 | for (int i = 0; i < *num_patches; ++i) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 90 | Value* p = (*patches)[i]; |
| 91 | if (p != NULL) { |
| 92 | free(p->data); |
| 93 | free(p); |
| 94 | } |
| 95 | } |
| 96 | free(*sha1s); |
| 97 | free(*patches); |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 98 | return false; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | int PatchMode(int argc, char** argv) { |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 102 | Value* bonus = NULL; |
| 103 | if (argc >= 3 && strcmp(argv[1], "-b") == 0) { |
| 104 | FileContents fc; |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 105 | if (LoadFileContents(argv[2], &fc) != 0) { |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 106 | printf("failed to load bonus file %s\n", argv[2]); |
| 107 | return 1; |
| 108 | } |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 109 | bonus = reinterpret_cast<Value*>(malloc(sizeof(Value))); |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 110 | bonus->type = VAL_BLOB; |
| 111 | bonus->size = fc.size; |
| 112 | bonus->data = (char*)fc.data; |
| 113 | argc -= 2; |
| 114 | argv += 2; |
| 115 | } |
| 116 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 117 | 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 Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 131 | if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &patches, &num_patches)) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 132 | 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 Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 137 | num_patches, sha1s, patches, bonus); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 138 | |
| 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 Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 147 | if (bonus) { |
| 148 | free(bonus->data); |
| 149 | free(bonus); |
| 150 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 151 | free(sha1s); |
| 152 | free(patches); |
| 153 | |
| 154 | return result; |
| 155 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 156 | |
| 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 | |
| 180 | int main(int argc, char** argv) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 181 | if (argc < 2) { |
| 182 | usage: |
| 183 | printf( |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 184 | "usage: %s [-b <bonus-file>] <src-file> <tgt-file> <tgt-sha1> <tgt-size> " |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 185 | "[<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 Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 194 | 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 Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 213 | } |