Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 <errno.h> |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 18 | #include <fcntl.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 19 | #include <libgen.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/statfs.h> |
| 25 | #include <sys/types.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 28 | #include <memory> |
| 29 | #include <string> |
| 30 | |
Elliott Hughes | 4b166f0 | 2015-12-04 15:30:20 -0800 | [diff] [blame] | 31 | #include <android-base/strings.h> |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 32 | |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 33 | #include "openssl/sha.h" |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 34 | #include "applypatch.h" |
Conn O'Griofa | d920104 | 2014-03-25 01:26:49 +0000 | [diff] [blame] | 35 | #include "bmlutils/bmlutils.h" |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 36 | #include "mtdutils/mtdutils.h" |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 37 | #include "edify/expr.h" |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 38 | #include "ota_io.h" |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 39 | #include "print_sha1.h" |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 40 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 41 | static int LoadPartitionContents(const char* filename, FileContents* file); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 42 | static ssize_t FileSink(const unsigned char* data, ssize_t len, void* token); |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 43 | static int GenerateTarget(FileContents* source_file, |
| 44 | const Value* source_patch_value, |
| 45 | FileContents* copy_file, |
| 46 | const Value* copy_patch_value, |
| 47 | const char* source_filename, |
| 48 | const char* target_filename, |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 49 | const uint8_t target_sha1[SHA_DIGEST_LENGTH], |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 50 | size_t target_size, |
| 51 | const Value* bonus_data); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 52 | |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 53 | static bool mtd_partitions_scanned = false; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 54 | |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 55 | // Read a file into memory; store the file contents and associated |
Hristo Bojinov | db314d6 | 2010-08-02 10:29:49 -0700 | [diff] [blame] | 56 | // metadata in *file. |
| 57 | // |
| 58 | // Return 0 on success. |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 59 | int LoadFileContents(const char* filename, FileContents* file) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 60 | // A special 'filename' beginning with "MTD:" or "EMMC:" means to |
| 61 | // load the contents of a partition. |
| 62 | if (strncmp(filename, "MTD:", 4) == 0 || |
Conn O'Griofa | d920104 | 2014-03-25 01:26:49 +0000 | [diff] [blame] | 63 | strncmp(filename, "EMMC:", 5) == 0 || |
| 64 | strncmp(filename, "BML:", 4) == 0) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 65 | return LoadPartitionContents(filename, file); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 66 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 67 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 68 | if (stat(filename, &file->st) != 0) { |
| 69 | printf("failed to stat \"%s\": %s\n", filename, strerror(errno)); |
| 70 | return -1; |
| 71 | } |
| 72 | |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 73 | std::vector<unsigned char> data(file->st.st_size); |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 74 | FILE* f = ota_fopen(filename, "rb"); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 75 | if (f == NULL) { |
| 76 | printf("failed to open \"%s\": %s\n", filename, strerror(errno)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 77 | return -1; |
| 78 | } |
| 79 | |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 80 | size_t bytes_read = ota_fread(data.data(), 1, data.size(), f); |
| 81 | if (bytes_read != data.size()) { |
| 82 | printf("short read of \"%s\" (%zu bytes of %zd)\n", filename, bytes_read, data.size()); |
| 83 | ota_fclose(f); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 84 | return -1; |
| 85 | } |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 86 | ota_fclose(f); |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 87 | file->data = std::move(data); |
| 88 | SHA1(file->data.data(), file->data.size(), file->sha1); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 89 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 92 | // Load the contents of an MTD or EMMC partition into the provided |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 93 | // FileContents. filename should be a string of the form |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 94 | // "MTD:<partition_name>:<size_1>:<sha1_1>:<size_2>:<sha1_2>:..." (or |
| 95 | // "EMMC:<partition_device>:..."). The smallest size_n bytes for |
| 96 | // which that prefix of the partition contents has the corresponding |
| 97 | // sha1 hash will be loaded. It is acceptable for a size value to be |
| 98 | // repeated with different sha1s. Will return 0 on success. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 99 | // |
| 100 | // This complexity is needed because if an OTA installation is |
| 101 | // interrupted, the partition might contain either the source or the |
| 102 | // target data, which might be of different lengths. We need to know |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 103 | // the length in order to read from a partition (there is no |
| 104 | // "end-of-file" marker), so the caller must specify the possible |
| 105 | // lengths and the hash of the data, and we'll do the load expecting |
| 106 | // to find one of those hashes. |
| 107 | enum PartitionType { MTD, EMMC }; |
| 108 | |
| 109 | static int LoadPartitionContents(const char* filename, FileContents* file) { |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 110 | std::string copy(filename); |
| 111 | std::vector<std::string> pieces = android::base::Split(copy, ":"); |
| 112 | if (pieces.size() < 4 || pieces.size() % 2 != 0) { |
| 113 | printf("LoadPartitionContents called with bad filename (%s)\n", filename); |
| 114 | return -1; |
| 115 | } |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 116 | |
| 117 | enum PartitionType type; |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 118 | if (pieces[0] == "MTD") { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 119 | type = MTD; |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 120 | } else if (pieces[0] == "EMMC") { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 121 | type = EMMC; |
Ethan Yonker | b8e985c | 2016-08-31 13:42:11 -0500 | [diff] [blame] | 122 | } else if (pieces[0] == "BML") { |
Conn O'Griofa | d920104 | 2014-03-25 01:26:49 +0000 | [diff] [blame] | 123 | type = EMMC; |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 124 | } else { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 125 | printf("LoadPartitionContents called with bad filename (%s)\n", filename); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 126 | return -1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 127 | } |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 128 | const char* partition = pieces[1].c_str(); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 129 | |
Ethan Yonker | b8e985c | 2016-08-31 13:42:11 -0500 | [diff] [blame] | 130 | if (pieces[0] == "BML") { |
Conn O'Griofa | d920104 | 2014-03-25 01:26:49 +0000 | [diff] [blame] | 131 | if (strcmp(partition, "boot") == 0) { |
| 132 | partition = BOARD_BML_BOOT; |
| 133 | } else if (strcmp(partition, "recovery") == 0) { |
| 134 | partition = BOARD_BML_RECOVERY; |
| 135 | } |
| 136 | } |
| 137 | |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 138 | size_t pairs = (pieces.size() - 2) / 2; // # of (size, sha1) pairs in filename |
| 139 | std::vector<size_t> index(pairs); |
| 140 | std::vector<size_t> size(pairs); |
| 141 | std::vector<std::string> sha1sum(pairs); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 142 | |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 143 | for (size_t i = 0; i < pairs; ++i) { |
| 144 | size[i] = strtol(pieces[i*2+2].c_str(), NULL, 10); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 145 | if (size[i] == 0) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 146 | printf("LoadPartitionContents called with bad size (%s)\n", filename); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 147 | return -1; |
| 148 | } |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 149 | sha1sum[i] = pieces[i*2+3].c_str(); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 150 | index[i] = i; |
| 151 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 152 | |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 153 | // Sort the index[] array so it indexes the pairs in order of increasing size. |
| 154 | sort(index.begin(), index.end(), |
| 155 | [&](const size_t& i, const size_t& j) { |
| 156 | return (size[i] < size[j]); |
| 157 | } |
| 158 | ); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 159 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 160 | MtdReadContext* ctx = NULL; |
| 161 | FILE* dev = NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 162 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 163 | switch (type) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 164 | case MTD: { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 165 | if (!mtd_partitions_scanned) { |
| 166 | mtd_scan_partitions(); |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 167 | mtd_partitions_scanned = true; |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 168 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 169 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 170 | const MtdPartition* mtd = mtd_find_partition_by_name(partition); |
| 171 | if (mtd == NULL) { |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 172 | printf("mtd partition \"%s\" not found (loading %s)\n", partition, filename); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | ctx = mtd_read_partition(mtd); |
| 177 | if (ctx == NULL) { |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 178 | printf("failed to initialize read of mtd partition \"%s\"\n", partition); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 179 | return -1; |
| 180 | } |
| 181 | break; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 182 | } |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 183 | |
| 184 | case EMMC: |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 185 | dev = ota_fopen(partition, "rb"); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 186 | if (dev == NULL) { |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 187 | printf("failed to open emmc partition \"%s\": %s\n", partition, strerror(errno)); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 188 | return -1; |
| 189 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 190 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 191 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 192 | SHA_CTX sha_ctx; |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 193 | SHA1_Init(&sha_ctx); |
| 194 | uint8_t parsed_sha[SHA_DIGEST_LENGTH]; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 195 | |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 196 | // Allocate enough memory to hold the largest size. |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 197 | std::vector<unsigned char> data(size[index[pairs-1]]); |
| 198 | char* p = reinterpret_cast<char*>(data.data()); |
| 199 | size_t data_size = 0; // # bytes read so far |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 200 | bool found = false; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 201 | |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 202 | for (size_t i = 0; i < pairs; ++i) { |
| 203 | // Read enough additional bytes to get us up to the next size. (Again, |
| 204 | // we're trying the possibilities in order of increasing size). |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 205 | size_t next = size[index[i]] - data_size; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 206 | if (next > 0) { |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 207 | size_t read = 0; |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 208 | switch (type) { |
| 209 | case MTD: |
| 210 | read = mtd_read_data(ctx, p, next); |
| 211 | break; |
| 212 | |
| 213 | case EMMC: |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 214 | read = ota_fread(p, 1, next, dev); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 215 | break; |
| 216 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 217 | if (next != read) { |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 218 | printf("short read (%zu bytes of %zu) for partition \"%s\"\n", |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 219 | read, next, partition); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 220 | return -1; |
| 221 | } |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 222 | SHA1_Update(&sha_ctx, p, read); |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 223 | data_size += read; |
| 224 | p += read; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // Duplicate the SHA context and finalize the duplicate so we can |
| 228 | // check it against this pair's expected hash. |
| 229 | SHA_CTX temp_ctx; |
| 230 | memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX)); |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 231 | uint8_t sha_so_far[SHA_DIGEST_LENGTH]; |
| 232 | SHA1_Final(sha_so_far, &temp_ctx); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 233 | |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 234 | if (ParseSha1(sha1sum[index[i]].c_str(), parsed_sha) != 0) { |
| 235 | printf("failed to parse sha1 %s in %s\n", sha1sum[index[i]].c_str(), filename); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 236 | return -1; |
| 237 | } |
| 238 | |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 239 | if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_LENGTH) == 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 240 | // we have a match. stop reading the partition; we'll return |
| 241 | // the data we've read so far. |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 242 | printf("partition read matched size %zu sha %s\n", |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 243 | size[index[i]], sha1sum[index[i]].c_str()); |
| 244 | found = true; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 245 | break; |
| 246 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 249 | switch (type) { |
| 250 | case MTD: |
| 251 | mtd_read_close(ctx); |
| 252 | break; |
| 253 | |
| 254 | case EMMC: |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 255 | ota_fclose(dev); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 256 | break; |
| 257 | } |
| 258 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 259 | |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 260 | if (!found) { |
| 261 | // Ran off the end of the list of (size,sha1) pairs without finding a match. |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 262 | printf("contents of partition \"%s\" didn't match %s\n", partition, filename); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 263 | return -1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 266 | SHA1_Final(file->sha1, &sha_ctx); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 267 | |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 268 | data.resize(data_size); |
| 269 | file->data = std::move(data); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 270 | // Fake some stat() info. |
| 271 | file->st.st_mode = 0644; |
| 272 | file->st.st_uid = 0; |
| 273 | file->st.st_gid = 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 274 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 275 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | |
| 279 | // Save the contents of the given FileContents object under the given |
| 280 | // filename. Return 0 on success. |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 281 | int SaveFileContents(const char* filename, const FileContents* file) { |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 282 | int fd = ota_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 283 | if (fd < 0) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 284 | printf("failed to open \"%s\" for write: %s\n", filename, strerror(errno)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 285 | return -1; |
| 286 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 287 | |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 288 | ssize_t bytes_written = FileSink(file->data.data(), file->data.size(), &fd); |
| 289 | if (bytes_written != static_cast<ssize_t>(file->data.size())) { |
| 290 | printf("short write of \"%s\" (%zd bytes of %zu) (%s)\n", |
| 291 | filename, bytes_written, file->data.size(), strerror(errno)); |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 292 | ota_close(fd); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 293 | return -1; |
| 294 | } |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 295 | if (ota_fsync(fd) != 0) { |
Michael Runge | cddb68b | 2014-10-29 12:42:15 -0700 | [diff] [blame] | 296 | printf("fsync of \"%s\" failed: %s\n", filename, strerror(errno)); |
| 297 | return -1; |
| 298 | } |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 299 | if (ota_close(fd) != 0) { |
Michael Runge | cddb68b | 2014-10-29 12:42:15 -0700 | [diff] [blame] | 300 | printf("close of \"%s\" failed: %s\n", filename, strerror(errno)); |
| 301 | return -1; |
| 302 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 303 | |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 304 | if (chmod(filename, file->st.st_mode) != 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 305 | printf("chmod of \"%s\" failed: %s\n", filename, strerror(errno)); |
| 306 | return -1; |
| 307 | } |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 308 | if (chown(filename, file->st.st_uid, file->st.st_gid) != 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 309 | printf("chown of \"%s\" failed: %s\n", filename, strerror(errno)); |
| 310 | return -1; |
| 311 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 312 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 313 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 316 | // Write a memory buffer to 'target' partition, a string of the form |
Tao Bao | 1ce7a2a | 2015-07-24 15:29:12 -0700 | [diff] [blame] | 317 | // "MTD:<partition>[:...]" or "EMMC:<partition_device>[:...]". The target name |
| 318 | // might contain multiple colons, but WriteToPartition() only uses the first |
| 319 | // two and ignores the rest. Return 0 on success. |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 320 | int WriteToPartition(const unsigned char* data, size_t len, const char* target) { |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 321 | std::string copy(target); |
| 322 | std::vector<std::string> pieces = android::base::Split(copy, ":"); |
| 323 | |
Tao Bao | 1ce7a2a | 2015-07-24 15:29:12 -0700 | [diff] [blame] | 324 | if (pieces.size() < 2) { |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 325 | printf("WriteToPartition called with bad target (%s)\n", target); |
| 326 | return -1; |
| 327 | } |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 328 | |
| 329 | enum PartitionType type; |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 330 | if (pieces[0] == "MTD") { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 331 | type = MTD; |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 332 | } else if (pieces[0] == "EMMC") { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 333 | type = EMMC; |
Ethan Yonker | b8e985c | 2016-08-31 13:42:11 -0500 | [diff] [blame] | 334 | } else if (pieces[0] == "BML") { |
Conn O'Griofa | d920104 | 2014-03-25 01:26:49 +0000 | [diff] [blame] | 335 | type = EMMC; |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 336 | } else { |
| 337 | printf("WriteToPartition called with bad target (%s)\n", target); |
| 338 | return -1; |
| 339 | } |
Ethan Yonker | 34ae483 | 2016-08-24 15:32:18 -0500 | [diff] [blame] | 340 | |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 341 | const char* partition = pieces[1].c_str(); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 342 | |
Ethan Yonker | b8e985c | 2016-08-31 13:42:11 -0500 | [diff] [blame] | 343 | if (pieces[0] == "BML") { |
Conn O'Griofa | d920104 | 2014-03-25 01:26:49 +0000 | [diff] [blame] | 344 | if (strcmp(partition, "boot") == 0) { |
| 345 | partition = BOARD_BML_BOOT; |
| 346 | } else if (strcmp(partition, "recovery") == 0) { |
| 347 | partition = BOARD_BML_RECOVERY; |
| 348 | } |
| 349 | |
| 350 | int bmlpartition = open(partition, O_RDWR | O_LARGEFILE); |
| 351 | if (bmlpartition < 0) |
| 352 | return -1; |
| 353 | if (ioctl(bmlpartition, BML_UNLOCK_ALL, 0)) { |
| 354 | printf("failed to unlock BML partition: (%s)\n", partition); |
| 355 | return -1; |
| 356 | } |
| 357 | close(bmlpartition); |
| 358 | } |
| 359 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 360 | if (partition == NULL) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 361 | printf("bad partition target name \"%s\"\n", target); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 362 | return -1; |
| 363 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 364 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 365 | switch (type) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 366 | case MTD: { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 367 | if (!mtd_partitions_scanned) { |
| 368 | mtd_scan_partitions(); |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 369 | mtd_partitions_scanned = true; |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | const MtdPartition* mtd = mtd_find_partition_by_name(partition); |
| 373 | if (mtd == NULL) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 374 | printf("mtd partition \"%s\" not found for writing\n", partition); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 375 | return -1; |
| 376 | } |
| 377 | |
| 378 | MtdWriteContext* ctx = mtd_write_partition(mtd); |
| 379 | if (ctx == NULL) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 380 | printf("failed to init mtd partition \"%s\" for writing\n", partition); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 381 | return -1; |
| 382 | } |
| 383 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 384 | size_t written = mtd_write_data(ctx, reinterpret_cast<const char*>(data), len); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 385 | if (written != len) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 386 | printf("only wrote %zu of %zu bytes to MTD %s\n", written, len, partition); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 387 | mtd_write_close(ctx); |
| 388 | return -1; |
| 389 | } |
| 390 | |
| 391 | if (mtd_erase_blocks(ctx, -1) < 0) { |
| 392 | printf("error finishing mtd write of %s\n", partition); |
| 393 | mtd_write_close(ctx); |
| 394 | return -1; |
| 395 | } |
| 396 | |
| 397 | if (mtd_write_close(ctx)) { |
| 398 | printf("error closing mtd write of %s\n", partition); |
| 399 | return -1; |
| 400 | } |
| 401 | break; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 402 | } |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 403 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 404 | case EMMC: { |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 405 | size_t start = 0; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 406 | bool success = false; |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 407 | int fd = ota_open(partition, O_RDWR | O_SYNC); |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 408 | if (fd < 0) { |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 409 | printf("failed to open %s: %s\n", partition, strerror(errno)); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 410 | return -1; |
| 411 | } |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 412 | |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 413 | for (size_t attempt = 0; attempt < 2; ++attempt) { |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 414 | if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 415 | printf("failed seek on %s: %s\n", partition, strerror(errno)); |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 416 | return -1; |
| 417 | } |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 418 | while (start < len) { |
| 419 | size_t to_write = len - start; |
Doug Zongker | 168724c | 2013-12-19 15:16:57 -0800 | [diff] [blame] | 420 | if (to_write > 1<<20) to_write = 1<<20; |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 421 | |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 422 | ssize_t written = TEMP_FAILURE_RETRY(ota_write(fd, data+start, to_write)); |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 423 | if (written == -1) { |
| 424 | printf("failed write writing to %s: %s\n", partition, strerror(errno)); |
| 425 | return -1; |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 426 | } |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 427 | start += written; |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 428 | } |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 429 | if (ota_fsync(fd) != 0) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 430 | printf("failed to sync to %s (%s)\n", partition, strerror(errno)); |
Michael Runge | cddb68b | 2014-10-29 12:42:15 -0700 | [diff] [blame] | 431 | return -1; |
| 432 | } |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 433 | if (ota_close(fd) != 0) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 434 | printf("failed to close %s (%s)\n", partition, strerror(errno)); |
Michael Runge | cddb68b | 2014-10-29 12:42:15 -0700 | [diff] [blame] | 435 | return -1; |
| 436 | } |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 437 | fd = ota_open(partition, O_RDONLY); |
Michael Runge | cddb68b | 2014-10-29 12:42:15 -0700 | [diff] [blame] | 438 | if (fd < 0) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 439 | printf("failed to reopen %s for verify (%s)\n", partition, strerror(errno)); |
Michael Runge | cddb68b | 2014-10-29 12:42:15 -0700 | [diff] [blame] | 440 | return -1; |
| 441 | } |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 442 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 443 | // Drop caches so our subsequent verification read |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 444 | // won't just be reading the cache. |
| 445 | sync(); |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 446 | int dc = ota_open("/proc/sys/vm/drop_caches", O_WRONLY); |
| 447 | if (TEMP_FAILURE_RETRY(ota_write(dc, "3\n", 2)) == -1) { |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 448 | printf("write to /proc/sys/vm/drop_caches failed: %s\n", strerror(errno)); |
| 449 | } else { |
| 450 | printf(" caches dropped\n"); |
| 451 | } |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 452 | ota_close(dc); |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 453 | sleep(1); |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 454 | |
| 455 | // verify |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 456 | if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) { |
| 457 | printf("failed to seek back to beginning of %s: %s\n", |
| 458 | partition, strerror(errno)); |
| 459 | return -1; |
| 460 | } |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 461 | unsigned char buffer[4096]; |
| 462 | start = len; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 463 | for (size_t p = 0; p < len; p += sizeof(buffer)) { |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 464 | size_t to_read = len - p; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 465 | if (to_read > sizeof(buffer)) { |
| 466 | to_read = sizeof(buffer); |
| 467 | } |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 468 | |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 469 | size_t so_far = 0; |
| 470 | while (so_far < to_read) { |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 471 | ssize_t read_count = |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 472 | TEMP_FAILURE_RETRY(ota_read(fd, buffer+so_far, to_read-so_far)); |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 473 | if (read_count == -1) { |
| 474 | printf("verify read error %s at %zu: %s\n", |
| 475 | partition, p, strerror(errno)); |
| 476 | return -1; |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 477 | } |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 478 | if (static_cast<size_t>(read_count) < to_read) { |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 479 | printf("short verify read %s at %zu: %zd %zu %s\n", |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 480 | partition, p, read_count, to_read, strerror(errno)); |
| 481 | } |
| 482 | so_far += read_count; |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 485 | if (memcmp(buffer, data+p, to_read) != 0) { |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 486 | printf("verification failed starting at %zu\n", p); |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 487 | start = p; |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | if (start == len) { |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 493 | printf("verification read succeeded (attempt %zu)\n", attempt+1); |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 494 | success = true; |
| 495 | break; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | if (!success) { |
| 500 | printf("failed to verify after all attempts\n"); |
| 501 | return -1; |
| 502 | } |
| 503 | |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 504 | if (ota_close(fd) != 0) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 505 | printf("error closing %s (%s)\n", partition, strerror(errno)); |
| 506 | return -1; |
| 507 | } |
Doug Zongker | bf4a69a | 2013-07-10 13:39:50 -0700 | [diff] [blame] | 508 | sync(); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 509 | break; |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 510 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 511 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 512 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 513 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | |
| 517 | // Take a string 'str' of 40 hex digits and parse it into the 20 |
| 518 | // byte array 'digest'. 'str' may contain only the digest or be of |
| 519 | // the form "<digest>:<anything>". Return 0 on success, -1 on any |
| 520 | // error. |
| 521 | int ParseSha1(const char* str, uint8_t* digest) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 522 | const char* ps = str; |
| 523 | uint8_t* pd = digest; |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 524 | for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 525 | int digit; |
| 526 | if (*ps >= '0' && *ps <= '9') { |
| 527 | digit = *ps - '0'; |
| 528 | } else if (*ps >= 'a' && *ps <= 'f') { |
| 529 | digit = *ps - 'a' + 10; |
| 530 | } else if (*ps >= 'A' && *ps <= 'F') { |
| 531 | digit = *ps - 'A' + 10; |
| 532 | } else { |
| 533 | return -1; |
| 534 | } |
| 535 | if (i % 2 == 0) { |
| 536 | *pd = digit << 4; |
| 537 | } else { |
| 538 | *pd |= digit; |
| 539 | ++pd; |
| 540 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 541 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 542 | if (*ps != '\0') return -1; |
| 543 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 544 | } |
| 545 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 546 | // Search an array of sha1 strings for one matching the given sha1. |
| 547 | // Return the index of the match on success, or -1 if no match is |
| 548 | // found. |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 549 | int FindMatchingPatch(uint8_t* sha1, char* const * const patch_sha1_str, |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 550 | int num_patches) { |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 551 | uint8_t patch_sha1[SHA_DIGEST_LENGTH]; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 552 | for (int i = 0; i < num_patches; ++i) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 553 | if (ParseSha1(patch_sha1_str[i], patch_sha1) == 0 && |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 554 | memcmp(patch_sha1, sha1, SHA_DIGEST_LENGTH) == 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 555 | return i; |
| 556 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 557 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 558 | return -1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | // Returns 0 if the contents of the file (argv[2]) or the cached file |
| 562 | // match any of the sha1's on the command line (argv[3:]). Returns |
| 563 | // nonzero otherwise. |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 564 | int applypatch_check(const char* filename, int num_patches, |
| 565 | char** const patch_sha1_str) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 566 | FileContents file; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 567 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 568 | // It's okay to specify no sha1s; the check will pass if the |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 569 | // LoadFileContents is successful. (Useful for reading |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 570 | // partitions, where the filename encodes the sha1s; no need to |
| 571 | // check them twice.) |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 572 | if (LoadFileContents(filename, &file) != 0 || |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 573 | (num_patches > 0 && |
| 574 | FindMatchingPatch(file.sha1, patch_sha1_str, num_patches) < 0)) { |
| 575 | printf("file \"%s\" doesn't have any of expected " |
| 576 | "sha1 sums; checking cache\n", filename); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 577 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 578 | // If the source file is missing or corrupted, it might be because |
| 579 | // we were killed in the middle of patching it. A copy of it |
| 580 | // should have been made in CACHE_TEMP_SOURCE. If that file |
| 581 | // exists and matches the sha1 we're looking for, the check still |
| 582 | // passes. |
| 583 | |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 584 | if (LoadFileContents(CACHE_TEMP_SOURCE, &file) != 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 585 | printf("failed to load cache file\n"); |
| 586 | return 1; |
| 587 | } |
| 588 | |
| 589 | if (FindMatchingPatch(file.sha1, patch_sha1_str, num_patches) < 0) { |
| 590 | printf("cache bits don't match any sha1 for \"%s\"\n", filename); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 591 | return 1; |
| 592 | } |
| 593 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 594 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | int ShowLicenses() { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 598 | ShowBSDiffLicense(); |
| 599 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 600 | } |
| 601 | |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 602 | ssize_t FileSink(const unsigned char* data, ssize_t len, void* token) { |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 603 | int fd = *static_cast<int*>(token); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 604 | ssize_t done = 0; |
| 605 | ssize_t wrote; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 606 | while (done < len) { |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 607 | wrote = TEMP_FAILURE_RETRY(ota_write(fd, data+done, len-done)); |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 608 | if (wrote == -1) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 609 | printf("error writing %zd bytes: %s\n", (len-done), strerror(errno)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 610 | return done; |
| 611 | } |
| 612 | done += wrote; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 613 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 614 | return done; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 615 | } |
| 616 | |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 617 | ssize_t MemorySink(const unsigned char* data, ssize_t len, void* token) { |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 618 | std::string* s = static_cast<std::string*>(token); |
| 619 | s->append(reinterpret_cast<const char*>(data), len); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 620 | return len; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | // Return the amount of free space (in bytes) on the filesystem |
| 624 | // containing filename. filename must exist. Return -1 on error. |
| 625 | size_t FreeSpaceForFile(const char* filename) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 626 | struct statfs sf; |
| 627 | if (statfs(filename, &sf) != 0) { |
| 628 | printf("failed to statfs %s: %s\n", filename, strerror(errno)); |
| 629 | return -1; |
| 630 | } |
caozhiyuan | cb9450e | 2015-05-19 17:21:00 +0800 | [diff] [blame] | 631 | return sf.f_bsize * sf.f_bavail; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 632 | } |
| 633 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 634 | int CacheSizeCheck(size_t bytes) { |
| 635 | if (MakeFreeSpaceOnCache(bytes) < 0) { |
| 636 | printf("unable to make %ld bytes available on /cache\n", (long)bytes); |
| 637 | return 1; |
| 638 | } else { |
| 639 | return 0; |
| 640 | } |
| 641 | } |
| 642 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 643 | // This function applies binary patches to files in a way that is safe |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 644 | // (the original file is not touched until we have the desired |
| 645 | // replacement for it) and idempotent (it's okay to run this program |
| 646 | // multiple times). |
| 647 | // |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 648 | // - if the sha1 hash of <target_filename> is <target_sha1_string>, |
| 649 | // does nothing and exits successfully. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 650 | // |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 651 | // - otherwise, if the sha1 hash of <source_filename> is one of the |
| 652 | // entries in <patch_sha1_str>, the corresponding patch from |
| 653 | // <patch_data> (which must be a VAL_BLOB) is applied to produce a |
| 654 | // new file (the type of patch is automatically detected from the |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 655 | // blob data). If that new file has sha1 hash <target_sha1_str>, |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 656 | // moves it to replace <target_filename>, and exits successfully. |
| 657 | // Note that if <source_filename> and <target_filename> are not the |
| 658 | // same, <source_filename> is NOT deleted on success. |
| 659 | // <target_filename> may be the string "-" to mean "the same as |
| 660 | // source_filename". |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 661 | // |
| 662 | // - otherwise, or if any error is encountered, exits with non-zero |
| 663 | // status. |
| 664 | // |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 665 | // <source_filename> may refer to a partition to read the source data. |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 666 | // See the comments for the LoadPartitionContents() function above |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 667 | // for the format of such a filename. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 668 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 669 | int applypatch(const char* source_filename, |
| 670 | const char* target_filename, |
| 671 | const char* target_sha1_str, |
| 672 | size_t target_size, |
| 673 | int num_patches, |
| 674 | char** const patch_sha1_str, |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 675 | Value** patch_data, |
| 676 | Value* bonus_data) { |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 677 | printf("patch %s: ", source_filename); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 678 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 679 | if (target_filename[0] == '-' && target_filename[1] == '\0') { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 680 | target_filename = source_filename; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 681 | } |
| 682 | |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 683 | uint8_t target_sha1[SHA_DIGEST_LENGTH]; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 684 | if (ParseSha1(target_sha1_str, target_sha1) != 0) { |
| 685 | printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 686 | return 1; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 687 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 688 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 689 | FileContents copy_file; |
| 690 | FileContents source_file; |
| 691 | const Value* source_patch_value = NULL; |
| 692 | const Value* copy_patch_value = NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 693 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 694 | // We try to load the target file into the source_file object. |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 695 | if (LoadFileContents(target_filename, &source_file) == 0) { |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 696 | if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 697 | // The early-exit case: the patch was already applied, this file |
| 698 | // has the desired hash, nothing for us to do. |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 699 | printf("already %s\n", short_sha1(target_sha1).c_str()); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 700 | return 0; |
| 701 | } |
| 702 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 703 | |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 704 | if (source_file.data.empty() || |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 705 | (target_filename != source_filename && |
| 706 | strcmp(target_filename, source_filename) != 0)) { |
| 707 | // Need to load the source file: either we failed to load the |
| 708 | // target file, or we did but it's different from the source file. |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 709 | source_file.data.clear(); |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 710 | LoadFileContents(source_filename, &source_file); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 711 | } |
| 712 | |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 713 | if (!source_file.data.empty()) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 714 | int to_use = FindMatchingPatch(source_file.sha1, patch_sha1_str, num_patches); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 715 | if (to_use >= 0) { |
| 716 | source_patch_value = patch_data[to_use]; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | if (source_patch_value == NULL) { |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 721 | source_file.data.clear(); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 722 | printf("source file is bad; trying copy\n"); |
| 723 | |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 724 | if (LoadFileContents(CACHE_TEMP_SOURCE, ©_file) < 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 725 | // fail. |
| 726 | printf("failed to read copy file\n"); |
| 727 | return 1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 728 | } |
| 729 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 730 | int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1_str, num_patches); |
Doug Zongker | 8cd9e4f | 2010-08-12 17:38:09 -0700 | [diff] [blame] | 731 | if (to_use >= 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 732 | copy_patch_value = patch_data[to_use]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 733 | } |
| 734 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 735 | if (copy_patch_value == NULL) { |
| 736 | // fail. |
| 737 | printf("copy file doesn't match source SHA-1s either\n"); |
| 738 | return 1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 739 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 740 | } |
| 741 | |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 742 | return GenerateTarget(&source_file, source_patch_value, |
| 743 | ©_file, copy_patch_value, |
| 744 | source_filename, target_filename, |
| 745 | target_sha1, target_size, bonus_data); |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 746 | } |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 747 | |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 748 | /* |
| 749 | * This function flashes a given image to the target partition. It verifies |
| 750 | * the target cheksum first, and will return if target has the desired hash. |
| 751 | * It checks the checksum of the given source image before flashing, and |
| 752 | * verifies the target partition afterwards. The function is idempotent. |
| 753 | * Returns zero on success. |
| 754 | */ |
| 755 | int applypatch_flash(const char* source_filename, const char* target_filename, |
| 756 | const char* target_sha1_str, size_t target_size) { |
| 757 | printf("flash %s: ", target_filename); |
| 758 | |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 759 | uint8_t target_sha1[SHA_DIGEST_LENGTH]; |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 760 | if (ParseSha1(target_sha1_str, target_sha1) != 0) { |
| 761 | printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str); |
| 762 | return 1; |
| 763 | } |
| 764 | |
| 765 | FileContents source_file; |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 766 | std::string target_str(target_filename); |
| 767 | |
| 768 | std::vector<std::string> pieces = android::base::Split(target_str, ":"); |
| 769 | if (pieces.size() != 2 || (pieces[0] != "MTD" && pieces[0] != "EMMC")) { |
| 770 | printf("invalid target name \"%s\"", target_filename); |
| 771 | return 1; |
| 772 | } |
| 773 | |
| 774 | // Load the target into the source_file object to see if already applied. |
| 775 | pieces.push_back(std::to_string(target_size)); |
| 776 | pieces.push_back(target_sha1_str); |
| 777 | std::string fullname = android::base::Join(pieces, ':'); |
| 778 | if (LoadPartitionContents(fullname.c_str(), &source_file) == 0 && |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 779 | memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) { |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 780 | // The early-exit case: the image was already applied, this partition |
| 781 | // has the desired hash, nothing for us to do. |
| 782 | printf("already %s\n", short_sha1(target_sha1).c_str()); |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | if (LoadFileContents(source_filename, &source_file) == 0) { |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 787 | if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) { |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 788 | // The source doesn't have desired checksum. |
| 789 | printf("source \"%s\" doesn't have expected sha1 sum\n", source_filename); |
| 790 | printf("expected: %s, found: %s\n", short_sha1(target_sha1).c_str(), |
| 791 | short_sha1(source_file.sha1).c_str()); |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 792 | return 1; |
| 793 | } |
| 794 | } |
| 795 | |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 796 | if (WriteToPartition(source_file.data.data(), target_size, target_filename) != 0) { |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 797 | printf("write of copied data to %s failed\n", target_filename); |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 798 | return 1; |
| 799 | } |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 800 | return 0; |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | static int GenerateTarget(FileContents* source_file, |
| 804 | const Value* source_patch_value, |
| 805 | FileContents* copy_file, |
| 806 | const Value* copy_patch_value, |
| 807 | const char* source_filename, |
| 808 | const char* target_filename, |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 809 | const uint8_t target_sha1[SHA_DIGEST_LENGTH], |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 810 | size_t target_size, |
| 811 | const Value* bonus_data) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 812 | int retry = 1; |
| 813 | SHA_CTX ctx; |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 814 | std::string memory_sink_str; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 815 | FileContents* source_to_use; |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 816 | int made_copy = 0; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 817 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 818 | bool target_is_partition = (strncmp(target_filename, "MTD:", 4) == 0 || |
Ethan Yonker | 34ae483 | 2016-08-24 15:32:18 -0500 | [diff] [blame] | 819 | strncmp(target_filename, "EMMC:", 5) == 0 || |
| 820 | strncmp(target_filename, "BML:", 4) == 0); |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 821 | const std::string tmp_target_filename = std::string(target_filename) + ".patch"; |
| 822 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 823 | // assume that target_filename (eg "/system/app/Foo.apk") is located |
| 824 | // on the same filesystem as its top-level directory ("/system"). |
| 825 | // We need something that exists for calling statfs(). |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 826 | std::string target_fs = target_filename; |
| 827 | auto slash_pos = target_fs.find('/', 1); |
| 828 | if (slash_pos != std::string::npos) { |
| 829 | target_fs.resize(slash_pos); |
| 830 | } |
| 831 | |
| 832 | const Value* patch; |
| 833 | if (source_patch_value != NULL) { |
| 834 | source_to_use = source_file; |
| 835 | patch = source_patch_value; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 836 | } else { |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 837 | source_to_use = copy_file; |
| 838 | patch = copy_patch_value; |
| 839 | } |
| 840 | if (patch->type != VAL_BLOB) { |
| 841 | printf("patch is not a blob\n"); |
| 842 | return 1; |
| 843 | } |
| 844 | char* header = patch->data; |
| 845 | ssize_t header_bytes_read = patch->size; |
| 846 | bool use_bsdiff = false; |
| 847 | if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) { |
| 848 | use_bsdiff = true; |
| 849 | } else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) { |
| 850 | use_bsdiff = false; |
| 851 | } else { |
| 852 | printf("Unknown patch file format\n"); |
| 853 | return 1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 854 | } |
| 855 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 856 | do { |
| 857 | // Is there enough room in the target filesystem to hold the patched |
| 858 | // file? |
| 859 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 860 | if (target_is_partition) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 861 | // If the target is a partition, we're actually going to |
| 862 | // write the output to /tmp and then copy it to the |
| 863 | // partition. statfs() always returns 0 blocks free for |
| 864 | // /tmp, so instead we'll just assume that /tmp has enough |
| 865 | // space to hold the file. |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 866 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 867 | // We still write the original source to cache, in case |
| 868 | // the partition write is interrupted. |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 869 | if (MakeFreeSpaceOnCache(source_file->data.size()) < 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 870 | printf("not enough free space on /cache\n"); |
| 871 | return 1; |
| 872 | } |
| 873 | if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) { |
| 874 | printf("failed to back up source file\n"); |
| 875 | return 1; |
| 876 | } |
| 877 | made_copy = 1; |
| 878 | retry = 0; |
| 879 | } else { |
| 880 | int enough_space = 0; |
| 881 | if (retry > 0) { |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 882 | size_t free_space = FreeSpaceForFile(target_fs.c_str()); |
Doug Zongker | 201cd46 | 2010-08-13 09:41:21 -0700 | [diff] [blame] | 883 | enough_space = |
| 884 | (free_space > (256 << 10)) && // 256k (two-block) minimum |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 885 | (free_space > (target_size * 3 / 2)); // 50% margin of error |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 886 | if (!enough_space) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 887 | printf("target %zu bytes; free space %zu bytes; retry %d; enough %d\n", |
| 888 | target_size, free_space, retry, enough_space); |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 889 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | if (!enough_space) { |
| 893 | retry = 0; |
| 894 | } |
| 895 | |
| 896 | if (!enough_space && source_patch_value != NULL) { |
| 897 | // Using the original source, but not enough free space. First |
| 898 | // copy the source file to cache, then delete it from the original |
| 899 | // location. |
| 900 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 901 | if (strncmp(source_filename, "MTD:", 4) == 0 || |
Conn O'Griofa | d920104 | 2014-03-25 01:26:49 +0000 | [diff] [blame] | 902 | strncmp(source_filename, "EMMC:", 5) == 0 || |
| 903 | strncmp(source_filename, "BML:", 4) == 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 904 | // It's impossible to free space on the target filesystem by |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 905 | // deleting the source if the source is a partition. If |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 906 | // we're ever in a state where we need to do this, fail. |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 907 | printf("not enough free space for target but source is partition\n"); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 908 | return 1; |
| 909 | } |
| 910 | |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 911 | if (MakeFreeSpaceOnCache(source_file->data.size()) < 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 912 | printf("not enough free space on /cache\n"); |
| 913 | return 1; |
| 914 | } |
| 915 | |
| 916 | if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) { |
| 917 | printf("failed to back up source file\n"); |
| 918 | return 1; |
| 919 | } |
| 920 | made_copy = 1; |
| 921 | unlink(source_filename); |
| 922 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 923 | size_t free_space = FreeSpaceForFile(target_fs.c_str()); |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 924 | printf("(now %zu bytes free for target) ", free_space); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 928 | |
| 929 | SinkFn sink = NULL; |
| 930 | void* token = NULL; |
Ethan Yonker | 34ae483 | 2016-08-24 15:32:18 -0500 | [diff] [blame] | 931 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 932 | int output_fd = -1; |
| 933 | if (target_is_partition) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 934 | // We store the decoded output in memory. |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 935 | sink = MemorySink; |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 936 | token = &memory_sink_str; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 937 | } else { |
| 938 | // We write the decoded output to "<tgt-file>.patch". |
Yabin Cui | ca78c9f | 2016-02-05 15:27:52 -0800 | [diff] [blame] | 939 | output_fd = ota_open(tmp_target_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 940 | S_IRUSR | S_IWUSR); |
| 941 | if (output_fd < 0) { |
| 942 | printf("failed to open output file %s: %s\n", tmp_target_filename.c_str(), |
| 943 | strerror(errno)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 944 | return 1; |
| 945 | } |
| 946 | sink = FileSink; |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 947 | token = &output_fd; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 948 | } |
| 949 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 950 | |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 951 | SHA1_Init(&ctx); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 952 | |
| 953 | int result; |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 954 | if (use_bsdiff) { |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 955 | result = ApplyBSDiffPatch(source_to_use->data.data(), source_to_use->data.size(), |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 956 | patch, 0, sink, token, &ctx); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 957 | } else { |
Yabin Cui | 1c522df | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 958 | result = ApplyImagePatch(source_to_use->data.data(), source_to_use->data.size(), |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 959 | patch, sink, token, &ctx, bonus_data); |
| 960 | } |
| 961 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 962 | if (!target_is_partition) { |
Yabin Cui | ca78c9f | 2016-02-05 15:27:52 -0800 | [diff] [blame] | 963 | if (ota_fsync(output_fd) != 0) { |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 964 | printf("failed to fsync file \"%s\" (%s)\n", tmp_target_filename.c_str(), |
| 965 | strerror(errno)); |
Michael Runge | cddb68b | 2014-10-29 12:42:15 -0700 | [diff] [blame] | 966 | result = 1; |
| 967 | } |
Yabin Cui | ca78c9f | 2016-02-05 15:27:52 -0800 | [diff] [blame] | 968 | if (ota_close(output_fd) != 0) { |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 969 | printf("failed to close file \"%s\" (%s)\n", tmp_target_filename.c_str(), |
| 970 | strerror(errno)); |
Michael Runge | cddb68b | 2014-10-29 12:42:15 -0700 | [diff] [blame] | 971 | result = 1; |
| 972 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | if (result != 0) { |
| 976 | if (retry == 0) { |
| 977 | printf("applying patch failed\n"); |
| 978 | return result != 0; |
| 979 | } else { |
| 980 | printf("applying patch failed; retrying\n"); |
| 981 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 982 | if (!target_is_partition) { |
| 983 | unlink(tmp_target_filename.c_str()); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 984 | } |
| 985 | } else { |
| 986 | // succeeded; no need to retry |
| 987 | break; |
| 988 | } |
| 989 | } while (retry-- > 0); |
| 990 | |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 991 | uint8_t current_target_sha1[SHA_DIGEST_LENGTH]; |
| 992 | SHA1_Final(current_target_sha1, &ctx); |
| 993 | if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 994 | printf("patch did not produce expected sha1\n"); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 995 | return 1; |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 996 | } else { |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 997 | printf("now %s\n", short_sha1(target_sha1).c_str()); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 998 | } |
| 999 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 1000 | if (target_is_partition) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 1001 | // Copy the temp file to the partition. |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 1002 | if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()), |
| 1003 | memory_sink_str.size(), target_filename) != 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1004 | printf("write of patched data to %s failed\n", target_filename); |
| 1005 | return 1; |
| 1006 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1007 | } else { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1008 | // Give the .patch file the same owner, group, and mode of the |
| 1009 | // original source file. |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 1010 | if (chmod(tmp_target_filename.c_str(), source_to_use->st.st_mode) != 0) { |
| 1011 | printf("chmod of \"%s\" failed: %s\n", tmp_target_filename.c_str(), strerror(errno)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1012 | return 1; |
| 1013 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 1014 | if (chown(tmp_target_filename.c_str(), source_to_use->st.st_uid, source_to_use->st.st_gid) != 0) { |
| 1015 | printf("chown of \"%s\" failed: %s\n", tmp_target_filename.c_str(), strerror(errno)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1016 | return 1; |
| 1017 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1018 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1019 | // Finally, rename the .patch file to replace the target file. |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 1020 | if (rename(tmp_target_filename.c_str(), target_filename) != 0) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1021 | printf("rename of .patch to \"%s\" failed: %s\n", target_filename, strerror(errno)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1022 | return 1; |
| 1023 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1024 | } |
| 1025 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1026 | // If this run of applypatch created the copy, and we're here, we |
| 1027 | // can delete it. |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1028 | if (made_copy) { |
| 1029 | unlink(CACHE_TEMP_SOURCE); |
| 1030 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1031 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1032 | // Success! |
| 1033 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1034 | } |