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 | 485b637 | 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 | 485b637 | 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 | 485b637 | 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 | 68c5a67 | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 50 | static bool ParsePatchArgs(int argc, char** argv, char*** sha1s, |
| 51 | Value*** patches, int* num_patches) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 52 | *num_patches = argc; |
Tao Bao | 485b637 | 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 | 485b637 | 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 | 485b637 | 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 | 485b637 | 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 | 485b637 | 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 | 485b637 | 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 | 485b637 | 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 | 485b637 | 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 | |
Tao Bao | 68c5a67 | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 101 | static int FlashMode(const char* src_filename, const char* tgt_filename, |
| 102 | const char* tgt_sha1, size_t tgt_size) { |
| 103 | return applypatch_flash(src_filename, tgt_filename, tgt_sha1, tgt_size); |
| 104 | } |
| 105 | |
| 106 | static int PatchMode(int argc, char** argv) { |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 107 | Value* bonus = NULL; |
| 108 | if (argc >= 3 && strcmp(argv[1], "-b") == 0) { |
| 109 | FileContents fc; |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 110 | if (LoadFileContents(argv[2], &fc) != 0) { |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 111 | printf("failed to load bonus file %s\n", argv[2]); |
| 112 | return 1; |
| 113 | } |
Tao Bao | 485b637 | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 114 | bonus = reinterpret_cast<Value*>(malloc(sizeof(Value))); |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 115 | bonus->type = VAL_BLOB; |
| 116 | bonus->size = fc.size; |
| 117 | bonus->data = (char*)fc.data; |
| 118 | argc -= 2; |
| 119 | argv += 2; |
| 120 | } |
| 121 | |
Tao Bao | 68c5a67 | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 122 | if (argc < 4) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 123 | return 2; |
| 124 | } |
| 125 | |
| 126 | char* endptr; |
| 127 | size_t target_size = strtol(argv[4], &endptr, 10); |
| 128 | if (target_size == 0 && endptr == argv[4]) { |
| 129 | printf("can't parse \"%s\" as byte count\n\n", argv[4]); |
| 130 | return 1; |
| 131 | } |
| 132 | |
Tao Bao | 68c5a67 | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 133 | // If no <src-sha1>:<patch> is provided, it is in flash mode. |
| 134 | if (argc == 5) { |
| 135 | if (bonus != NULL) { |
| 136 | printf("bonus file not supported in flash mode\n"); |
| 137 | return 1; |
| 138 | } |
| 139 | return FlashMode(argv[1], argv[2], argv[3], target_size); |
| 140 | } |
| 141 | |
| 142 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 143 | char** sha1s; |
| 144 | Value** patches; |
| 145 | int num_patches; |
Tao Bao | 485b637 | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 146 | if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &patches, &num_patches)) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 147 | printf("failed to parse patch args\n"); |
| 148 | return 1; |
| 149 | } |
| 150 | |
| 151 | int result = applypatch(argv[1], argv[2], argv[3], target_size, |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 152 | num_patches, sha1s, patches, bonus); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 153 | |
| 154 | int i; |
| 155 | for (i = 0; i < num_patches; ++i) { |
| 156 | Value* p = patches[i]; |
| 157 | if (p != NULL) { |
| 158 | free(p->data); |
| 159 | free(p); |
| 160 | } |
| 161 | } |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 162 | if (bonus) { |
| 163 | free(bonus->data); |
| 164 | free(bonus); |
| 165 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 166 | free(sha1s); |
| 167 | free(patches); |
| 168 | |
| 169 | return result; |
| 170 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 171 | |
| 172 | // This program applies binary patches to files in a way that is safe |
| 173 | // (the original file is not touched until we have the desired |
| 174 | // replacement for it) and idempotent (it's okay to run this program |
| 175 | // multiple times). |
| 176 | // |
| 177 | // - if the sha1 hash of <tgt-file> is <tgt-sha1>, does nothing and exits |
| 178 | // successfully. |
| 179 | // |
Tao Bao | 68c5a67 | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 180 | // - otherwise, if no <src-sha1>:<patch> is provided, flashes <tgt-file> with |
| 181 | // <src-file>. <tgt-file> must be a partition name, while <src-file> must |
| 182 | // be a regular image file. <src-file> will not be deleted on success. |
| 183 | // |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 184 | // - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the |
| 185 | // bsdiff <patch> to <src-file> to produce a new file (the type of patch |
| 186 | // is automatically detected from the file header). If that new |
| 187 | // file has sha1 hash <tgt-sha1>, moves it to replace <tgt-file>, and |
| 188 | // exits successfully. Note that if <src-file> and <tgt-file> are |
| 189 | // not the same, <src-file> is NOT deleted on success. <tgt-file> |
| 190 | // may be the string "-" to mean "the same as src-file". |
| 191 | // |
| 192 | // - otherwise, or if any error is encountered, exits with non-zero |
| 193 | // status. |
| 194 | // |
| 195 | // <src-file> (or <file> in check mode) may refer to an MTD partition |
| 196 | // to read the source data. See the comments for the |
| 197 | // LoadMTDContents() function above for the format of such a filename. |
| 198 | |
| 199 | int main(int argc, char** argv) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 200 | if (argc < 2) { |
| 201 | usage: |
| 202 | printf( |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 203 | "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] | 204 | "[<src-sha1>:<patch> ...]\n" |
| 205 | " or %s -c <file> [<sha1> ...]\n" |
| 206 | " or %s -s <bytes>\n" |
| 207 | " or %s -l\n" |
| 208 | "\n" |
| 209 | "Filenames may be of the form\n" |
| 210 | " MTD:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n" |
| 211 | "to specify reading from or writing to an MTD partition.\n\n", |
| 212 | argv[0], argv[0], argv[0], argv[0]); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 213 | return 2; |
| 214 | } |
| 215 | |
| 216 | int result; |
| 217 | |
| 218 | if (strncmp(argv[1], "-l", 3) == 0) { |
| 219 | result = ShowLicenses(); |
| 220 | } else if (strncmp(argv[1], "-c", 3) == 0) { |
| 221 | result = CheckMode(argc, argv); |
| 222 | } else if (strncmp(argv[1], "-s", 3) == 0) { |
| 223 | result = SpaceMode(argc, argv); |
| 224 | } else { |
| 225 | result = PatchMode(argc, argv); |
| 226 | } |
| 227 | |
| 228 | if (result == 2) { |
| 229 | goto usage; |
| 230 | } |
| 231 | return result; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 232 | } |