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