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 | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 28 | #include <android-base/parseint.h> |
| 29 | #include <android-base/strings.h> |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 30 | #include <openssl/sha.h> |
| 31 | |
Tao Bao | d80a998 | 2016-03-03 11:43:47 -0800 | [diff] [blame] | 32 | #include "applypatch/applypatch.h" |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 33 | #include "edify/expr.h" |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 34 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 35 | static int CheckMode(int argc, const char** argv) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 36 | if (argc < 3) { |
| 37 | return 2; |
| 38 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 39 | std::vector<std::string> sha1; |
| 40 | for (int i = 3; i < argc; i++) { |
| 41 | sha1.push_back(argv[i]); |
| 42 | } |
| 43 | |
| 44 | return applypatch_check(argv[2], sha1); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 47 | // Parse arguments (which should be of the form "<sha1>:<filename>" into the |
| 48 | // new parallel arrays *sha1s and *files. Returns true on success. |
| 49 | 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] | 50 | std::vector<FileContents>* files) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 51 | if (sha1s == nullptr) { |
| 52 | return false; |
| 53 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 54 | for (int i = 0; i < argc; ++i) { |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 55 | std::vector<std::string> pieces = android::base::Split(argv[i], ":"); |
| 56 | if (pieces.size() != 2) { |
| 57 | printf("failed to parse patch argument \"%s\"\n", argv[i]); |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 58 | return false; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 59 | } |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 60 | |
| 61 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 62 | if (ParseSha1(pieces[0].c_str(), digest) != 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 63 | printf("failed to parse sha1 \"%s\"\n", argv[i]); |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 64 | return false; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 67 | sha1s->push_back(pieces[0]); |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 68 | FileContents fc; |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 69 | if (LoadFileContents(pieces[1].c_str(), &fc) != 0) { |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 70 | return false; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 71 | } |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 72 | files->push_back(std::move(fc)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 73 | } |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 74 | return true; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 77 | static int FlashMode(const char* src_filename, const char* tgt_filename, |
| 78 | const char* tgt_sha1, size_t tgt_size) { |
| 79 | return applypatch_flash(src_filename, tgt_filename, tgt_sha1, tgt_size); |
| 80 | } |
| 81 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 82 | static int PatchMode(int argc, const char** argv) { |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 83 | FileContents bonusFc; |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 84 | Value bonus(VAL_INVALID, ""); |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 85 | |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 86 | if (argc >= 3 && strcmp(argv[1], "-b") == 0) { |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 87 | if (LoadFileContents(argv[2], &bonusFc) != 0) { |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 88 | printf("failed to load bonus file %s\n", argv[2]); |
| 89 | return 1; |
| 90 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 91 | bonus.type = VAL_BLOB; |
Tao Bao | edf1b15 | 2016-10-24 16:08:28 -0700 | [diff] [blame] | 92 | bonus.data = std::string(bonusFc.data.cbegin(), bonusFc.data.cend()); |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 93 | argc -= 2; |
| 94 | argv += 2; |
| 95 | } |
| 96 | |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 97 | if (argc < 4) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 98 | return 2; |
| 99 | } |
| 100 | |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 101 | size_t target_size; |
| 102 | if (!android::base::ParseUint(argv[4], &target_size) || target_size == 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 103 | printf("can't parse \"%s\" as byte count\n\n", argv[4]); |
| 104 | return 1; |
| 105 | } |
| 106 | |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 107 | // If no <src-sha1>:<patch> is provided, it is in flash mode. |
| 108 | if (argc == 5) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 109 | if (bonus.type != VAL_INVALID) { |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 110 | printf("bonus file not supported in flash mode\n"); |
| 111 | return 1; |
| 112 | } |
| 113 | return FlashMode(argv[1], argv[2], argv[3], target_size); |
| 114 | } |
Tao Bao | 36c3511 | 2016-10-25 14:17:26 -0700 | [diff] [blame] | 115 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 116 | std::vector<std::string> sha1s; |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 117 | std::vector<FileContents> files; |
| 118 | if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &files)) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 119 | printf("failed to parse patch args\n"); |
| 120 | return 1; |
| 121 | } |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 122 | |
| 123 | std::vector<std::unique_ptr<Value>> patches; |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 124 | for (size_t i = 0; i < files.size(); ++i) { |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 125 | patches.push_back(std::make_unique<Value>( |
| 126 | VAL_BLOB, std::string(files[i].data.cbegin(), files[i].data.cend()))); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 127 | } |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 128 | 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] | 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) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 159 | if (argc < 2) { |
| 160 | usage: |
| 161 | printf( |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 162 | "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] | 163 | "[<src-sha1>:<patch> ...]\n" |
| 164 | " or %s -c <file> [<sha1> ...]\n" |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 165 | " or %s -l\n" |
| 166 | "\n" |
| 167 | "Filenames may be of the form\n" |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 168 | " EMMC:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n" |
| 169 | "to specify reading from or writing to an EMMC partition.\n\n", |
Tao Bao | 8ab2808 | 2017-04-25 21:13:21 -0700 | [diff] [blame] | 170 | argv[0], argv[0], argv[0]); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 171 | return 2; |
| 172 | } |
| 173 | |
| 174 | int result; |
| 175 | |
| 176 | if (strncmp(argv[1], "-l", 3) == 0) { |
| 177 | result = ShowLicenses(); |
| 178 | } else if (strncmp(argv[1], "-c", 3) == 0) { |
| 179 | result = CheckMode(argc, argv); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 180 | } else { |
| 181 | result = PatchMode(argc, argv); |
| 182 | } |
| 183 | |
| 184 | if (result == 2) { |
| 185 | goto usage; |
| 186 | } |
| 187 | return result; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 188 | } |