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