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 // 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, 50 std::vector<FileContents>* files) { 51 if (sha1s == nullptr) { 52 return false; 53 } 54 for (int i = 0; i < argc; ++i) { 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]); 58 return false; 59 } 60 61 uint8_t digest[SHA_DIGEST_LENGTH]; 62 if (ParseSha1(pieces[0].c_str(), digest) != 0) { 63 printf("failed to parse sha1 \"%s\"\n", argv[i]); 64 return false; 65 } 66 67 sha1s->push_back(pieces[0]); 68 FileContents fc; 69 if (LoadFileContents(pieces[1].c_str(), &fc) != 0) { 70 return false; 71 } 72 files->push_back(std::move(fc)); 73 } 74 return true; 75 } 76 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 82 static int PatchMode(int argc, const char** argv) { 83 FileContents bonusFc; 84 Value bonus(VAL_INVALID, ""); 85 86 if (argc >= 3 && strcmp(argv[1], "-b") == 0) { 87 if (LoadFileContents(argv[2], &bonusFc) != 0) { 88 printf("failed to load bonus file %s\n", argv[2]); 89 return 1; 90 } 91 bonus.type = VAL_BLOB; 92 bonus.data = std::string(bonusFc.data.cbegin(), bonusFc.data.cend()); 93 argc -= 2; 94 argv += 2; 95 } 96 97 if (argc < 4) { 98 return 2; 99 } 100 101 size_t target_size; 102 if (!android::base::ParseUint(argv[4], &target_size) || target_size == 0) { 103 printf("can't parse \"%s\" as byte count\n\n", argv[4]); 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.type != VAL_INVALID) { 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 } 115 116 std::vector<std::string> sha1s; 117 std::vector<FileContents> files; 118 if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &files)) { 119 printf("failed to parse patch args\n"); 120 return 1; 121 } 122 123 std::vector<std::unique_ptr<Value>> patches; 124 for (size_t i = 0; i < files.size(); ++i) { 125 patches.push_back(std::make_unique<Value>( 126 VAL_BLOB, std::string(files[i].data.cbegin(), files[i].data.cend()))); 127 } 128 return applypatch(argv[1], argv[2], argv[3], target_size, sha1s, patches, &bonus); 129 } 130 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 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 // 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 // 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 // 154 // <src-file> (or <file> in check mode) may refer to an EMMC partition 155 // to read the source data. See the comments for the 156 // LoadPartitionContents() function for the format of such a filename. 157 158 int applypatch_modes(int argc, const char** argv) { 159 if (argc < 2) { 160 usage: 161 printf( 162 "usage: %s [-b <bonus-file>] <src-file> <tgt-file> <tgt-sha1> <tgt-size> " 163 "[<src-sha1>:<patch> ...]\n" 164 " or %s -c <file> [<sha1> ...]\n" 165 " or %s -l\n" 166 "\n" 167 "Filenames may be of the form\n" 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", 170 argv[0], argv[0], argv[0]); 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); 180 } else { 181 result = PatchMode(argc, argv); 182 } 183 184 if (result == 2) { 185 goto usage; 186 } 187 return result; 188 } 189