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 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 22 | #include <memory> |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 23 | #include <string> |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 26 | #include <openssl/sha.h> |
| 27 | |
Tao Bao | d80a998 | 2016-03-03 11:43:47 -0800 | [diff] [blame] | 28 | #include "applypatch/applypatch.h" |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 29 | #include "edify/expr.h" |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 30 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 31 | static int CheckMode(int argc, char** argv) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 32 | if (argc < 3) { |
| 33 | return 2; |
| 34 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 35 | std::vector<std::string> sha1; |
| 36 | for (int i = 3; i < argc; i++) { |
| 37 | sha1.push_back(argv[i]); |
| 38 | } |
| 39 | |
| 40 | return applypatch_check(argv[2], sha1); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 43 | static int SpaceMode(int argc, char** argv) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 44 | if (argc != 3) { |
| 45 | return 2; |
| 46 | } |
| 47 | char* endptr; |
| 48 | size_t bytes = strtol(argv[2], &endptr, 10); |
| 49 | if (bytes == 0 && endptr == argv[2]) { |
| 50 | printf("can't parse \"%s\" as byte count\n\n", argv[2]); |
| 51 | return 1; |
| 52 | } |
| 53 | return CacheSizeCheck(bytes); |
| 54 | } |
| 55 | |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 56 | // Parse arguments (which should be of the form "<sha1>:<filename>" |
| 57 | // into the new parallel arrays *sha1s and *files.Returns true on |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 58 | // success. |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 59 | static bool ParsePatchArgs(int argc, char** argv, std::vector<std::string>* sha1s, |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 60 | std::vector<FileContents>* files) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 61 | if (sha1s == nullptr) { |
| 62 | return false; |
| 63 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 64 | for (int i = 0; i < argc; ++i) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 65 | uint8_t digest[SHA_DIGEST_LENGTH]; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 66 | char* colon = strchr(argv[i], ':'); |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 67 | if (colon == nullptr) { |
| 68 | printf("no ':' in patch argument \"%s\"\n", argv[i]); |
| 69 | return false; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 70 | } |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 71 | *colon = '\0'; |
| 72 | ++colon; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 73 | if (ParseSha1(argv[i], digest) != 0) { |
| 74 | printf("failed to parse sha1 \"%s\"\n", argv[i]); |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 75 | return false; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 78 | sha1s->push_back(argv[i]); |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 79 | FileContents fc; |
| 80 | if (LoadFileContents(colon, &fc) != 0) { |
| 81 | return false; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 82 | } |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 83 | files->push_back(std::move(fc)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 84 | } |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 85 | return true; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 88 | static int FlashMode(const char* src_filename, const char* tgt_filename, |
| 89 | const char* tgt_sha1, size_t tgt_size) { |
| 90 | return applypatch_flash(src_filename, tgt_filename, tgt_sha1, tgt_size); |
| 91 | } |
| 92 | |
| 93 | static int PatchMode(int argc, char** argv) { |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 94 | FileContents bonusFc; |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 95 | Value bonus(VAL_INVALID, ""); |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 96 | |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 97 | if (argc >= 3 && strcmp(argv[1], "-b") == 0) { |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 98 | if (LoadFileContents(argv[2], &bonusFc) != 0) { |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 99 | printf("failed to load bonus file %s\n", argv[2]); |
| 100 | return 1; |
| 101 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 102 | bonus.type = VAL_BLOB; |
Tao Bao | edf1b15 | 2016-10-24 16:08:28 -0700 | [diff] [blame] | 103 | bonus.data = std::string(bonusFc.data.cbegin(), bonusFc.data.cend()); |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 104 | argc -= 2; |
| 105 | argv += 2; |
| 106 | } |
| 107 | |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 108 | if (argc < 4) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 109 | return 2; |
| 110 | } |
| 111 | |
| 112 | char* endptr; |
| 113 | size_t target_size = strtol(argv[4], &endptr, 10); |
| 114 | if (target_size == 0 && endptr == argv[4]) { |
| 115 | printf("can't parse \"%s\" as byte count\n\n", argv[4]); |
| 116 | return 1; |
| 117 | } |
| 118 | |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 119 | // If no <src-sha1>:<patch> is provided, it is in flash mode. |
| 120 | if (argc == 5) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 121 | if (bonus.type != VAL_INVALID) { |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 122 | printf("bonus file not supported in flash mode\n"); |
| 123 | return 1; |
| 124 | } |
| 125 | return FlashMode(argv[1], argv[2], argv[3], target_size); |
| 126 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 127 | std::vector<std::string> sha1s; |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 128 | std::vector<FileContents> files; |
| 129 | if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &files)) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 130 | printf("failed to parse patch args\n"); |
| 131 | return 1; |
| 132 | } |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 133 | |
| 134 | std::vector<std::unique_ptr<Value>> patches; |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 135 | for (size_t i = 0; i < files.size(); ++i) { |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 136 | patches.push_back(std::make_unique<Value>( |
| 137 | VAL_BLOB, std::string(files[i].data.cbegin(), files[i].data.cend()))); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 138 | } |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 139 | return applypatch(argv[1], argv[2], argv[3], target_size, sha1s, patches, &bonus); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 140 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 141 | |
| 142 | // This program applies binary patches to files in a way that is safe |
| 143 | // (the original file is not touched until we have the desired |
| 144 | // replacement for it) and idempotent (it's okay to run this program |
| 145 | // multiple times). |
| 146 | // |
| 147 | // - if the sha1 hash of <tgt-file> is <tgt-sha1>, does nothing and exits |
| 148 | // successfully. |
| 149 | // |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 150 | // - otherwise, if no <src-sha1>:<patch> is provided, flashes <tgt-file> with |
| 151 | // <src-file>. <tgt-file> must be a partition name, while <src-file> must |
| 152 | // be a regular image file. <src-file> will not be deleted on success. |
| 153 | // |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 154 | // - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the |
| 155 | // bsdiff <patch> to <src-file> to produce a new file (the type of patch |
| 156 | // is automatically detected from the file header). If that new |
| 157 | // file has sha1 hash <tgt-sha1>, moves it to replace <tgt-file>, and |
| 158 | // exits successfully. Note that if <src-file> and <tgt-file> are |
| 159 | // not the same, <src-file> is NOT deleted on success. <tgt-file> |
| 160 | // may be the string "-" to mean "the same as src-file". |
| 161 | // |
| 162 | // - otherwise, or if any error is encountered, exits with non-zero |
| 163 | // status. |
| 164 | // |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 165 | // <src-file> (or <file> in check mode) may refer to an EMMC partition |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 166 | // to read the source data. See the comments for the |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 167 | // LoadPartitionContents() function for the format of such a filename. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 168 | |
| 169 | int main(int argc, char** argv) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 170 | if (argc < 2) { |
| 171 | usage: |
| 172 | printf( |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 173 | "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] | 174 | "[<src-sha1>:<patch> ...]\n" |
| 175 | " or %s -c <file> [<sha1> ...]\n" |
| 176 | " or %s -s <bytes>\n" |
| 177 | " or %s -l\n" |
| 178 | "\n" |
| 179 | "Filenames may be of the form\n" |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 180 | " EMMC:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n" |
| 181 | "to specify reading from or writing to an EMMC partition.\n\n", |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 182 | argv[0], argv[0], argv[0], argv[0]); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 183 | return 2; |
| 184 | } |
| 185 | |
| 186 | int result; |
| 187 | |
| 188 | if (strncmp(argv[1], "-l", 3) == 0) { |
| 189 | result = ShowLicenses(); |
| 190 | } else if (strncmp(argv[1], "-c", 3) == 0) { |
| 191 | result = CheckMode(argc, argv); |
| 192 | } else if (strncmp(argv[1], "-s", 3) == 0) { |
| 193 | result = SpaceMode(argc, argv); |
| 194 | } else { |
| 195 | result = PatchMode(argc, argv); |
| 196 | } |
| 197 | |
| 198 | if (result == 2) { |
| 199 | goto usage; |
| 200 | } |
| 201 | return result; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 202 | } |