Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 <ctype.h> |
| 18 | #include <errno.h> |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 19 | #include <dirent.h> |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <inttypes.h> |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 22 | #include <linux/fs.h> |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 23 | #include <pthread.h> |
| 24 | #include <stdarg.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 28 | #include <sys/stat.h> |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <sys/wait.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <time.h> |
| 33 | #include <unistd.h> |
Sami Tolvanen | 0a7b473 | 2015-06-25 10:25:36 +0100 | [diff] [blame] | 34 | #include <fec/io.h> |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 35 | |
Tianjie Xu | 0188935 | 2016-03-22 18:08:12 -0700 | [diff] [blame] | 36 | #include <map> |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 37 | #include <memory> |
| 38 | #include <string> |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 39 | #include <vector> |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 40 | |
Elliott Hughes | 4b166f0 | 2015-12-04 15:30:20 -0800 | [diff] [blame] | 41 | #include <android-base/parseint.h> |
| 42 | #include <android-base/strings.h> |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 43 | |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 44 | #include "applypatch/applypatch.h" |
| 45 | #include "edify/expr.h" |
Tianjie Xu | 30bf476 | 2015-12-15 11:47:30 -0800 | [diff] [blame] | 46 | #include "install.h" |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 47 | #include "openssl/sha.h" |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 48 | #include "minzip/Hash.h" |
Jed Estep | f73abf3 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 49 | #include "ota_io.h" |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 50 | #include "print_sha1.h" |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 51 | #include "unique_fd.h" |
| 52 | #include "updater.h" |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 53 | |
| 54 | #define BLOCKSIZE 4096 |
| 55 | |
Sami Tolvanen | e82fa18 | 2015-06-10 15:58:12 +0000 | [diff] [blame] | 56 | // Set this to 0 to interpret 'erase' transfers to mean do a |
| 57 | // BLKDISCARD ioctl (the normal behavior). Set to 1 to interpret |
| 58 | // erase to mean fill the region with zeroes. |
| 59 | #define DEBUG_ERASE 0 |
| 60 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 61 | #define STASH_DIRECTORY_BASE "/cache/recovery" |
| 62 | #define STASH_DIRECTORY_MODE 0700 |
| 63 | #define STASH_FILE_MODE 0600 |
| 64 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 65 | struct RangeSet { |
| 66 | size_t count; // Limit is INT_MAX. |
Shrinivas Sahukar | a6153df | 2015-08-19 13:01:45 +0530 | [diff] [blame] | 67 | size_t size; |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 68 | std::vector<size_t> pos; // Actual limit is INT_MAX. |
| 69 | }; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 70 | |
Tianjie Xu | 0188935 | 2016-03-22 18:08:12 -0700 | [diff] [blame] | 71 | static std::map<std::string, RangeSet> stash_map; |
| 72 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 73 | static void parse_range(const std::string& range_text, RangeSet& rs) { |
Sami Tolvanen | f2bac04 | 2015-05-12 12:48:46 +0100 | [diff] [blame] | 74 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 75 | std::vector<std::string> pieces = android::base::Split(range_text, ","); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 76 | if (pieces.size() < 3) { |
Sami Tolvanen | f2bac04 | 2015-05-12 12:48:46 +0100 | [diff] [blame] | 77 | goto err; |
| 78 | } |
| 79 | |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 80 | size_t num; |
| 81 | if (!android::base::ParseUint(pieces[0].c_str(), &num, static_cast<size_t>(INT_MAX))) { |
Sami Tolvanen | f2bac04 | 2015-05-12 12:48:46 +0100 | [diff] [blame] | 82 | goto err; |
| 83 | } |
| 84 | |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 85 | if (num == 0 || num % 2) { |
| 86 | goto err; // must be even |
| 87 | } else if (num != pieces.size() - 1) { |
| 88 | goto err; |
| 89 | } |
Sami Tolvanen | f2bac04 | 2015-05-12 12:48:46 +0100 | [diff] [blame] | 90 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 91 | rs.pos.resize(num); |
| 92 | rs.count = num / 2; |
| 93 | rs.size = 0; |
Sami Tolvanen | f2bac04 | 2015-05-12 12:48:46 +0100 | [diff] [blame] | 94 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 95 | for (size_t i = 0; i < num; i += 2) { |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 96 | if (!android::base::ParseUint(pieces[i+1].c_str(), &rs.pos[i], |
| 97 | static_cast<size_t>(INT_MAX))) { |
Sami Tolvanen | f2bac04 | 2015-05-12 12:48:46 +0100 | [diff] [blame] | 98 | goto err; |
| 99 | } |
| 100 | |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 101 | if (!android::base::ParseUint(pieces[i+2].c_str(), &rs.pos[i+1], |
| 102 | static_cast<size_t>(INT_MAX))) { |
Shrinivas Sahukar | a6153df | 2015-08-19 13:01:45 +0530 | [diff] [blame] | 103 | goto err; |
| 104 | } |
| 105 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 106 | if (rs.pos[i] >= rs.pos[i+1]) { |
Shrinivas Sahukar | a6153df | 2015-08-19 13:01:45 +0530 | [diff] [blame] | 107 | goto err; // empty or negative range |
| 108 | } |
| 109 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 110 | size_t sz = rs.pos[i+1] - rs.pos[i]; |
| 111 | if (rs.size > SIZE_MAX - sz) { |
Shrinivas Sahukar | a6153df | 2015-08-19 13:01:45 +0530 | [diff] [blame] | 112 | goto err; // overflow |
| 113 | } |
| 114 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 115 | rs.size += sz; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 118 | return; |
Sami Tolvanen | f2bac04 | 2015-05-12 12:48:46 +0100 | [diff] [blame] | 119 | |
| 120 | err: |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 121 | fprintf(stderr, "failed to parse range '%s'\n", range_text.c_str()); |
Sami Tolvanen | f2bac04 | 2015-05-12 12:48:46 +0100 | [diff] [blame] | 122 | exit(1); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 125 | static bool range_overlaps(const RangeSet& r1, const RangeSet& r2) { |
Shrinivas Sahukar | a6153df | 2015-08-19 13:01:45 +0530 | [diff] [blame] | 126 | for (size_t i = 0; i < r1.count; ++i) { |
| 127 | size_t r1_0 = r1.pos[i * 2]; |
| 128 | size_t r1_1 = r1.pos[i * 2 + 1]; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 129 | |
Shrinivas Sahukar | a6153df | 2015-08-19 13:01:45 +0530 | [diff] [blame] | 130 | for (size_t j = 0; j < r2.count; ++j) { |
| 131 | size_t r2_0 = r2.pos[j * 2]; |
| 132 | size_t r2_1 = r2.pos[j * 2 + 1]; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 133 | |
Tao Bao | c0f56ad | 2015-06-25 14:00:31 -0700 | [diff] [blame] | 134 | if (!(r2_0 >= r1_1 || r1_0 >= r2_1)) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 135 | return true; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 140 | return false; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static int read_all(int fd, uint8_t* data, size_t size) { |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 144 | size_t so_far = 0; |
| 145 | while (so_far < size) { |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 146 | ssize_t r = TEMP_FAILURE_RETRY(ota_read(fd, data+so_far, size-so_far)); |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 147 | if (r == -1) { |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 148 | fprintf(stderr, "read failed: %s\n", strerror(errno)); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 149 | return -1; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 150 | } |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 151 | so_far += r; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 152 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 153 | return 0; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 156 | static int read_all(int fd, std::vector<uint8_t>& buffer, size_t size) { |
| 157 | return read_all(fd, buffer.data(), size); |
| 158 | } |
| 159 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 160 | static int write_all(int fd, const uint8_t* data, size_t size) { |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 161 | size_t written = 0; |
| 162 | while (written < size) { |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 163 | ssize_t w = TEMP_FAILURE_RETRY(ota_write(fd, data+written, size-written)); |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 164 | if (w == -1) { |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 165 | fprintf(stderr, "write failed: %s\n", strerror(errno)); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 166 | return -1; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 167 | } |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 168 | written += w; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 169 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 170 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 171 | return 0; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 174 | static int write_all(int fd, const std::vector<uint8_t>& buffer, size_t size) { |
| 175 | return write_all(fd, buffer.data(), size); |
| 176 | } |
| 177 | |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 178 | static bool check_lseek(int fd, off64_t offset, int whence) { |
| 179 | off64_t rc = TEMP_FAILURE_RETRY(lseek64(fd, offset, whence)); |
| 180 | if (rc == -1) { |
| 181 | fprintf(stderr, "lseek64 failed: %s\n", strerror(errno)); |
| 182 | return false; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 183 | } |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 184 | return true; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 187 | static void allocate(size_t size, std::vector<uint8_t>& buffer) { |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 188 | // if the buffer's big enough, reuse it. |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 189 | if (size <= buffer.size()) return; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 190 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 191 | buffer.resize(size); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 194 | struct RangeSinkState { |
| 195 | RangeSinkState(RangeSet& rs) : tgt(rs) { }; |
| 196 | |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 197 | int fd; |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 198 | const RangeSet& tgt; |
Shrinivas Sahukar | a6153df | 2015-08-19 13:01:45 +0530 | [diff] [blame] | 199 | size_t p_block; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 200 | size_t p_remain; |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 201 | }; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 202 | |
| 203 | static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 204 | RangeSinkState* rss = reinterpret_cast<RangeSinkState*>(token); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 205 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 206 | if (rss->p_remain == 0) { |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 207 | fprintf(stderr, "range sink write overrun"); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 208 | return 0; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | ssize_t written = 0; |
| 212 | while (size > 0) { |
| 213 | size_t write_now = size; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 214 | |
| 215 | if (rss->p_remain < write_now) { |
| 216 | write_now = rss->p_remain; |
| 217 | } |
| 218 | |
| 219 | if (write_all(rss->fd, data, write_now) == -1) { |
| 220 | break; |
| 221 | } |
| 222 | |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 223 | data += write_now; |
| 224 | size -= write_now; |
| 225 | |
| 226 | rss->p_remain -= write_now; |
| 227 | written += write_now; |
| 228 | |
| 229 | if (rss->p_remain == 0) { |
| 230 | // move to the next block |
| 231 | ++rss->p_block; |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 232 | if (rss->p_block < rss->tgt.count) { |
| 233 | rss->p_remain = (rss->tgt.pos[rss->p_block * 2 + 1] - |
| 234 | rss->tgt.pos[rss->p_block * 2]) * BLOCKSIZE; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 235 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 236 | if (!check_lseek(rss->fd, (off64_t)rss->tgt.pos[rss->p_block*2] * BLOCKSIZE, |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 237 | SEEK_SET)) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 238 | break; |
| 239 | } |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 240 | } else { |
| 241 | // we can't write any more; return how many bytes have |
| 242 | // been written so far. |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 243 | break; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | return written; |
| 249 | } |
| 250 | |
| 251 | // All of the data for all the 'new' transfers is contained in one |
| 252 | // file in the update package, concatenated together in the order in |
| 253 | // which transfers.list will need it. We want to stream it out of the |
| 254 | // archive (it's compressed) without writing it to a temp file, but we |
| 255 | // can't write each section until it's that transfer's turn to go. |
| 256 | // |
| 257 | // To achieve this, we expand the new data from the archive in a |
| 258 | // background thread, and block that threads 'receive uncompressed |
| 259 | // data' function until the main thread has reached a point where we |
| 260 | // want some new data to be written. We signal the background thread |
| 261 | // with the destination for the data and block the main thread, |
| 262 | // waiting for the background thread to complete writing that section. |
| 263 | // Then it signals the main thread to wake up and goes back to |
| 264 | // blocking waiting for a transfer. |
| 265 | // |
| 266 | // NewThreadInfo is the struct used to pass information back and forth |
| 267 | // between the two threads. When the main thread wants some data |
| 268 | // written, it sets rss to the destination location and signals the |
| 269 | // condition. When the background thread is done writing, it clears |
| 270 | // rss and signals the condition again. |
| 271 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 272 | struct NewThreadInfo { |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 273 | ZipArchive* za; |
| 274 | const ZipEntry* entry; |
| 275 | |
| 276 | RangeSinkState* rss; |
| 277 | |
| 278 | pthread_mutex_t mu; |
| 279 | pthread_cond_t cv; |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 280 | }; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 281 | |
| 282 | static bool receive_new_data(const unsigned char* data, int size, void* cookie) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 283 | NewThreadInfo* nti = reinterpret_cast<NewThreadInfo*>(cookie); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 284 | |
| 285 | while (size > 0) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 286 | // Wait for nti->rss to be non-null, indicating some of this |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 287 | // data is wanted. |
| 288 | pthread_mutex_lock(&nti->mu); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 289 | while (nti->rss == nullptr) { |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 290 | pthread_cond_wait(&nti->cv, &nti->mu); |
| 291 | } |
| 292 | pthread_mutex_unlock(&nti->mu); |
| 293 | |
| 294 | // At this point nti->rss is set, and we own it. The main |
| 295 | // thread is waiting for it to disappear from nti. |
| 296 | ssize_t written = RangeSinkWrite(data, size, nti->rss); |
| 297 | data += written; |
| 298 | size -= written; |
| 299 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 300 | if (nti->rss->p_block == nti->rss->tgt.count) { |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 301 | // we have written all the bytes desired by this rss. |
| 302 | |
| 303 | pthread_mutex_lock(&nti->mu); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 304 | nti->rss = nullptr; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 305 | pthread_cond_broadcast(&nti->cv); |
| 306 | pthread_mutex_unlock(&nti->mu); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | static void* unzip_new_data(void* cookie) { |
| 314 | NewThreadInfo* nti = (NewThreadInfo*) cookie; |
| 315 | mzProcessZipEntryContents(nti->za, nti->entry, receive_new_data, nti); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 316 | return nullptr; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 319 | static int ReadBlocks(const RangeSet& src, std::vector<uint8_t>& buffer, int fd) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 320 | size_t p = 0; |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 321 | uint8_t* data = buffer.data(); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 322 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 323 | for (size_t i = 0; i < src.count; ++i) { |
| 324 | if (!check_lseek(fd, (off64_t) src.pos[i * 2] * BLOCKSIZE, SEEK_SET)) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 325 | return -1; |
| 326 | } |
| 327 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 328 | size_t size = (src.pos[i * 2 + 1] - src.pos[i * 2]) * BLOCKSIZE; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 329 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 330 | if (read_all(fd, data + p, size) == -1) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 331 | return -1; |
| 332 | } |
| 333 | |
| 334 | p += size; |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 340 | static int WriteBlocks(const RangeSet& tgt, const std::vector<uint8_t>& buffer, int fd) { |
| 341 | const uint8_t* data = buffer.data(); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 342 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 343 | size_t p = 0; |
| 344 | for (size_t i = 0; i < tgt.count; ++i) { |
| 345 | if (!check_lseek(fd, (off64_t) tgt.pos[i * 2] * BLOCKSIZE, SEEK_SET)) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 346 | return -1; |
| 347 | } |
| 348 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 349 | size_t size = (tgt.pos[i * 2 + 1] - tgt.pos[i * 2]) * BLOCKSIZE; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 350 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 351 | if (write_all(fd, data + p, size) == -1) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 352 | return -1; |
| 353 | } |
| 354 | |
| 355 | p += size; |
| 356 | } |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 361 | // Parameters for transfer list command functions |
| 362 | struct CommandParameters { |
| 363 | std::vector<std::string> tokens; |
| 364 | size_t cpos; |
| 365 | const char* cmdname; |
| 366 | const char* cmdline; |
| 367 | std::string freestash; |
| 368 | std::string stashbase; |
| 369 | bool canwrite; |
| 370 | int createdstash; |
| 371 | int fd; |
| 372 | bool foundwrites; |
| 373 | bool isunresumable; |
| 374 | int version; |
| 375 | size_t written; |
| 376 | NewThreadInfo nti; |
| 377 | pthread_t thread; |
| 378 | std::vector<uint8_t> buffer; |
| 379 | uint8_t* patch_start; |
| 380 | }; |
| 381 | |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 382 | // Do a source/target load for move/bsdiff/imgdiff in version 1. |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 383 | // We expect to parse the remainder of the parameter tokens as: |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 384 | // |
| 385 | // <src_range> <tgt_range> |
| 386 | // |
| 387 | // The source range is loaded into the provided buffer, reallocating |
Tao Bao | 34847b2 | 2015-09-08 11:05:49 -0700 | [diff] [blame] | 388 | // it to make it larger if necessary. |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 389 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 390 | static int LoadSrcTgtVersion1(CommandParameters& params, RangeSet& tgt, size_t& src_blocks, |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 391 | std::vector<uint8_t>& buffer, int fd) { |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 392 | |
| 393 | if (params.cpos + 1 >= params.tokens.size()) { |
| 394 | fprintf(stderr, "invalid parameters\n"); |
| 395 | return -1; |
| 396 | } |
| 397 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 398 | // <src_range> |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 399 | RangeSet src; |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 400 | parse_range(params.tokens[params.cpos++], src); |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 401 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 402 | // <tgt_range> |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 403 | parse_range(params.tokens[params.cpos++], tgt); |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 404 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 405 | allocate(src.size * BLOCKSIZE, buffer); |
| 406 | int rc = ReadBlocks(src, buffer, fd); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 407 | src_blocks = src.size; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 408 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 409 | return rc; |
| 410 | } |
| 411 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 412 | static int VerifyBlocks(const std::string& expected, const std::vector<uint8_t>& buffer, |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 413 | const size_t blocks, bool printerror) { |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 414 | uint8_t digest[SHA_DIGEST_LENGTH]; |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 415 | const uint8_t* data = buffer.data(); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 416 | |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 417 | SHA1(data, blocks * BLOCKSIZE, digest); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 418 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 419 | std::string hexdigest = print_sha1(digest); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 420 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 421 | if (hexdigest != expected) { |
| 422 | if (printerror) { |
| 423 | fprintf(stderr, "failed to verify blocks (expected %s, read %s)\n", |
| 424 | expected.c_str(), hexdigest.c_str()); |
| 425 | } |
| 426 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 429 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 432 | static std::string GetStashFileName(const std::string& base, const std::string& id, |
| 433 | const std::string& postfix) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 434 | if (base.empty()) { |
| 435 | return ""; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 438 | std::string fn(STASH_DIRECTORY_BASE); |
| 439 | fn += "/" + base + "/" + id + postfix; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 440 | |
| 441 | return fn; |
| 442 | } |
| 443 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 444 | typedef void (*StashCallback)(const std::string&, void*); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 445 | |
| 446 | // Does a best effort enumeration of stash files. Ignores possible non-file |
| 447 | // items in the stash directory and continues despite of errors. Calls the |
| 448 | // 'callback' function for each file and passes 'data' to the function as a |
| 449 | // parameter. |
| 450 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 451 | static void EnumerateStash(const std::string& dirname, StashCallback callback, void* data) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 452 | if (dirname.empty() || callback == nullptr) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 456 | std::unique_ptr<DIR, int(*)(DIR*)> directory(opendir(dirname.c_str()), closedir); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 457 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 458 | if (directory == nullptr) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 459 | if (errno != ENOENT) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 460 | fprintf(stderr, "opendir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno)); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 461 | } |
| 462 | return; |
| 463 | } |
| 464 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 465 | struct dirent* item; |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 466 | while ((item = readdir(directory.get())) != nullptr) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 467 | if (item->d_type != DT_REG) { |
| 468 | continue; |
| 469 | } |
| 470 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 471 | std::string fn = dirname + "/" + std::string(item->d_name); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 472 | callback(fn, data); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 476 | static void UpdateFileSize(const std::string& fn, void* data) { |
| 477 | if (fn.empty() || !data) { |
| 478 | return; |
| 479 | } |
| 480 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 481 | struct stat sb; |
| 482 | if (stat(fn.c_str(), &sb) == -1) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 483 | fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno)); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 484 | return; |
| 485 | } |
| 486 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 487 | int* size = reinterpret_cast<int*>(data); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 488 | *size += sb.st_size; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | // Deletes the stash directory and all files in it. Assumes that it only |
| 492 | // contains files. There is nothing we can do about unlikely, but possible |
| 493 | // errors, so they are merely logged. |
| 494 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 495 | static void DeleteFile(const std::string& fn, void* /* data */) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 496 | if (!fn.empty()) { |
| 497 | fprintf(stderr, "deleting %s\n", fn.c_str()); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 498 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 499 | if (unlink(fn.c_str()) == -1 && errno != ENOENT) { |
| 500 | fprintf(stderr, "unlink \"%s\" failed: %s\n", fn.c_str(), strerror(errno)); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 505 | static void DeletePartial(const std::string& fn, void* data) { |
| 506 | if (android::base::EndsWith(fn, ".partial")) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 507 | DeleteFile(fn, data); |
| 508 | } |
| 509 | } |
| 510 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 511 | static void DeleteStash(const std::string& base) { |
| 512 | if (base.empty()) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 513 | return; |
| 514 | } |
| 515 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 516 | fprintf(stderr, "deleting stash %s\n", base.c_str()); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 517 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 518 | std::string dirname = GetStashFileName(base, "", ""); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 519 | EnumerateStash(dirname, DeleteFile, nullptr); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 520 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 521 | if (rmdir(dirname.c_str()) == -1) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 522 | if (errno != ENOENT && errno != ENOTDIR) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 523 | fprintf(stderr, "rmdir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno)); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Tianjie Xu | 0188935 | 2016-03-22 18:08:12 -0700 | [diff] [blame] | 528 | static int LoadStash(CommandParameters& params, const std::string& base, const std::string& id, |
| 529 | bool verify, size_t* blocks, std::vector<uint8_t>& buffer, bool printnoent) { |
| 530 | // In verify mode, if source range_set was saved for the given hash, |
| 531 | // check contents in the source blocks first. If the check fails, |
| 532 | // search for the stashed files on /cache as usual. |
| 533 | if (!params.canwrite) { |
| 534 | if (stash_map.find(id) != stash_map.end()) { |
| 535 | const RangeSet& src = stash_map[id]; |
| 536 | allocate(src.size * BLOCKSIZE, buffer); |
| 537 | |
| 538 | if (ReadBlocks(src, buffer, params.fd) == -1) { |
| 539 | fprintf(stderr, "failed to read source blocks in stash map.\n"); |
| 540 | return -1; |
| 541 | } |
| 542 | if (VerifyBlocks(id, buffer, src.size, true) != 0) { |
| 543 | fprintf(stderr, "failed to verify loaded source blocks in stash map.\n"); |
| 544 | return -1; |
| 545 | } |
| 546 | return 0; |
| 547 | } |
| 548 | } |
| 549 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 550 | if (base.empty()) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 551 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 554 | size_t blockcount = 0; |
| 555 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 556 | if (!blocks) { |
| 557 | blocks = &blockcount; |
| 558 | } |
| 559 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 560 | std::string fn = GetStashFileName(base, id, ""); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 561 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 562 | struct stat sb; |
| 563 | int res = stat(fn.c_str(), &sb); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 564 | |
| 565 | if (res == -1) { |
| 566 | if (errno != ENOENT || printnoent) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 567 | fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno)); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 568 | } |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 569 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 572 | fprintf(stderr, " loading %s\n", fn.c_str()); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 573 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 574 | if ((sb.st_size % BLOCKSIZE) != 0) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 575 | fprintf(stderr, "%s size %" PRId64 " not multiple of block size %d", |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 576 | fn.c_str(), static_cast<int64_t>(sb.st_size), BLOCKSIZE); |
| 577 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 580 | int fd = TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY)); |
| 581 | unique_fd fd_holder(fd); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 582 | |
| 583 | if (fd == -1) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 584 | fprintf(stderr, "open \"%s\" failed: %s\n", fn.c_str(), strerror(errno)); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 585 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 588 | allocate(sb.st_size, buffer); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 589 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 590 | if (read_all(fd, buffer, sb.st_size) == -1) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 591 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 594 | *blocks = sb.st_size / BLOCKSIZE; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 595 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 596 | if (verify && VerifyBlocks(id, buffer, *blocks, true) != 0) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 597 | fprintf(stderr, "unexpected contents in %s\n", fn.c_str()); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 598 | DeleteFile(fn, nullptr); |
| 599 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 602 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 605 | static int WriteStash(const std::string& base, const std::string& id, int blocks, |
| 606 | std::vector<uint8_t>& buffer, bool checkspace, bool *exists) { |
| 607 | if (base.empty()) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 608 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | if (checkspace && CacheSizeCheck(blocks * BLOCKSIZE) != 0) { |
| 612 | fprintf(stderr, "not enough space to write stash\n"); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 613 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 616 | std::string fn = GetStashFileName(base, id, ".partial"); |
| 617 | std::string cn = GetStashFileName(base, id, ""); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 618 | |
Sami Tolvanen | 43b748f | 2015-04-17 12:50:31 +0100 | [diff] [blame] | 619 | if (exists) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 620 | struct stat sb; |
| 621 | int res = stat(cn.c_str(), &sb); |
Sami Tolvanen | 43b748f | 2015-04-17 12:50:31 +0100 | [diff] [blame] | 622 | |
| 623 | if (res == 0) { |
| 624 | // The file already exists and since the name is the hash of the contents, |
| 625 | // it's safe to assume the contents are identical (accidental hash collisions |
| 626 | // are unlikely) |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 627 | fprintf(stderr, " skipping %d existing blocks in %s\n", blocks, cn.c_str()); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 628 | *exists = true; |
| 629 | return 0; |
Sami Tolvanen | 43b748f | 2015-04-17 12:50:31 +0100 | [diff] [blame] | 630 | } |
| 631 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 632 | *exists = false; |
Sami Tolvanen | 43b748f | 2015-04-17 12:50:31 +0100 | [diff] [blame] | 633 | } |
| 634 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 635 | fprintf(stderr, " writing %d blocks to %s\n", blocks, cn.c_str()); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 636 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 637 | int fd = TEMP_FAILURE_RETRY(open(fn.c_str(), O_WRONLY | O_CREAT | O_TRUNC, STASH_FILE_MODE)); |
| 638 | unique_fd fd_holder(fd); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 639 | |
| 640 | if (fd == -1) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 641 | fprintf(stderr, "failed to create \"%s\": %s\n", fn.c_str(), strerror(errno)); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 642 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | if (write_all(fd, buffer, blocks * BLOCKSIZE) == -1) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 646 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 649 | if (ota_fsync(fd) == -1) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 650 | fprintf(stderr, "fsync \"%s\" failed: %s\n", fn.c_str(), strerror(errno)); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 651 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 654 | if (rename(fn.c_str(), cn.c_str()) == -1) { |
| 655 | fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", fn.c_str(), cn.c_str(), |
| 656 | strerror(errno)); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 657 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 660 | std::string dname = GetStashFileName(base, "", ""); |
| 661 | int dfd = TEMP_FAILURE_RETRY(open(dname.c_str(), O_RDONLY | O_DIRECTORY)); |
| 662 | unique_fd dfd_holder(dfd); |
Tao Bao | dc39226 | 2015-07-31 15:56:44 -0700 | [diff] [blame] | 663 | |
| 664 | if (dfd == -1) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 665 | fprintf(stderr, "failed to open \"%s\" failed: %s\n", dname.c_str(), strerror(errno)); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 666 | return -1; |
Tao Bao | dc39226 | 2015-07-31 15:56:44 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 669 | if (ota_fsync(dfd) == -1) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 670 | fprintf(stderr, "fsync \"%s\" failed: %s\n", dname.c_str(), strerror(errno)); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 671 | return -1; |
Tao Bao | dc39226 | 2015-07-31 15:56:44 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 674 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | // Creates a directory for storing stash files and checks if the /cache partition |
| 678 | // hash enough space for the expected amount of blocks we need to store. Returns |
| 679 | // >0 if we created the directory, zero if it existed already, and <0 of failure. |
| 680 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 681 | static int CreateStash(State* state, int maxblocks, const char* blockdev, std::string& base) { |
| 682 | if (blockdev == nullptr) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 683 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | // Stash directory should be different for each partition to avoid conflicts |
| 687 | // when updating multiple partitions at the same time, so we use the hash of |
| 688 | // the block device name as the base directory |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 689 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 690 | SHA1(reinterpret_cast<const uint8_t*>(blockdev), strlen(blockdev), digest); |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 691 | base = print_sha1(digest); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 692 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 693 | std::string dirname = GetStashFileName(base, "", ""); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 694 | struct stat sb; |
| 695 | int res = stat(dirname.c_str(), &sb); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 696 | |
| 697 | if (res == -1 && errno != ENOENT) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 698 | ErrorAbort(state, "stat \"%s\" failed: %s\n", dirname.c_str(), strerror(errno)); |
| 699 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 700 | } else if (res != 0) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 701 | fprintf(stderr, "creating stash %s\n", dirname.c_str()); |
| 702 | res = mkdir(dirname.c_str(), STASH_DIRECTORY_MODE); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 703 | |
| 704 | if (res != 0) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 705 | ErrorAbort(state, "mkdir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno)); |
| 706 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | if (CacheSizeCheck(maxblocks * BLOCKSIZE) != 0) { |
| 710 | ErrorAbort(state, "not enough space for stash\n"); |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 711 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 714 | return 1; // Created directory |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 717 | fprintf(stderr, "using existing stash %s\n", dirname.c_str()); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 718 | |
| 719 | // If the directory already exists, calculate the space already allocated to |
| 720 | // stash files and check if there's enough for all required blocks. Delete any |
| 721 | // partially completed stash files first. |
| 722 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 723 | EnumerateStash(dirname, DeletePartial, nullptr); |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 724 | int size = 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 725 | EnumerateStash(dirname, UpdateFileSize, &size); |
| 726 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 727 | size = maxblocks * BLOCKSIZE - size; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 728 | |
| 729 | if (size > 0 && CacheSizeCheck(size) != 0) { |
| 730 | ErrorAbort(state, "not enough space for stash (%d more needed)\n", size); |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 731 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 734 | return 0; // Using existing directory |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 737 | static int SaveStash(CommandParameters& params, const std::string& base, |
| 738 | std::vector<uint8_t>& buffer, int fd, bool usehash) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 739 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 740 | // <stash_id> <src_range> |
| 741 | if (params.cpos + 1 >= params.tokens.size()) { |
| 742 | fprintf(stderr, "missing id and/or src range fields in stash command\n"); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 743 | return -1; |
| 744 | } |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 745 | const std::string& id = params.tokens[params.cpos++]; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 746 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 747 | size_t blocks = 0; |
Tianjie Xu | 0188935 | 2016-03-22 18:08:12 -0700 | [diff] [blame] | 748 | if (usehash && LoadStash(params, base, id, true, &blocks, buffer, false) == 0) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 749 | // Stash file already exists and has expected contents. Do not |
| 750 | // read from source again, as the source may have been already |
| 751 | // overwritten during a previous attempt. |
| 752 | return 0; |
| 753 | } |
| 754 | |
Tao Bao | 34847b2 | 2015-09-08 11:05:49 -0700 | [diff] [blame] | 755 | RangeSet src; |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 756 | parse_range(params.tokens[params.cpos++], src); |
Tao Bao | 34847b2 | 2015-09-08 11:05:49 -0700 | [diff] [blame] | 757 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 758 | allocate(src.size * BLOCKSIZE, buffer); |
| 759 | if (ReadBlocks(src, buffer, fd) == -1) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 760 | return -1; |
| 761 | } |
Tao Bao | 34847b2 | 2015-09-08 11:05:49 -0700 | [diff] [blame] | 762 | blocks = src.size; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 763 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 764 | if (usehash && VerifyBlocks(id, buffer, blocks, true) != 0) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 765 | // Source blocks have unexpected contents. If we actually need this |
| 766 | // data later, this is an unrecoverable error. However, the command |
| 767 | // that uses the data may have already completed previously, so the |
| 768 | // possible failure will occur during source block verification. |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 769 | fprintf(stderr, "failed to load source blocks for stash %s\n", id.c_str()); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 770 | return 0; |
| 771 | } |
| 772 | |
Tianjie Xu | 0188935 | 2016-03-22 18:08:12 -0700 | [diff] [blame] | 773 | // In verify mode, save source range_set instead of stashing blocks. |
| 774 | if (!params.canwrite && usehash) { |
| 775 | stash_map[id] = src; |
| 776 | return 0; |
| 777 | } |
| 778 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 779 | fprintf(stderr, "stashing %zu blocks to %s\n", blocks, id.c_str()); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 780 | return WriteStash(base, id, blocks, buffer, false, nullptr); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 783 | static int FreeStash(const std::string& base, const std::string& id) { |
| 784 | if (base.empty() || id.empty()) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 785 | return -1; |
| 786 | } |
| 787 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 788 | std::string fn = GetStashFileName(base, id, ""); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 789 | DeleteFile(fn, nullptr); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 790 | |
| 791 | return 0; |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 794 | static void MoveRange(std::vector<uint8_t>& dest, const RangeSet& locs, |
| 795 | const std::vector<uint8_t>& source) { |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 796 | // source contains packed data, which we want to move to the |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 797 | // locations given in locs in the dest buffer. source and dest |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 798 | // may be the same buffer. |
| 799 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 800 | const uint8_t* from = source.data(); |
| 801 | uint8_t* to = dest.data(); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 802 | size_t start = locs.size; |
| 803 | for (int i = locs.count-1; i >= 0; --i) { |
| 804 | size_t blocks = locs.pos[i*2+1] - locs.pos[i*2]; |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 805 | start -= blocks; |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 806 | memmove(to + (locs.pos[i*2] * BLOCKSIZE), from + (start * BLOCKSIZE), |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 807 | blocks * BLOCKSIZE); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | // Do a source/target load for move/bsdiff/imgdiff in version 2. |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 812 | // We expect to parse the remainder of the parameter tokens as one of: |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 813 | // |
| 814 | // <tgt_range> <src_block_count> <src_range> |
| 815 | // (loads data from source image only) |
| 816 | // |
| 817 | // <tgt_range> <src_block_count> - <[stash_id:stash_range] ...> |
| 818 | // (loads data from stashes only) |
| 819 | // |
| 820 | // <tgt_range> <src_block_count> <src_range> <src_loc> <[stash_id:stash_range] ...> |
| 821 | // (loads data from both source image and stashes) |
| 822 | // |
| 823 | // On return, buffer is filled with the loaded source data (rearranged |
| 824 | // and combined with stashed data as necessary). buffer may be |
| 825 | // reallocated if needed to accommodate the source data. *tgt is the |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 826 | // target RangeSet. Any stashes required are loaded using LoadStash. |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 827 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 828 | static int LoadSrcTgtVersion2(CommandParameters& params, RangeSet& tgt, size_t& src_blocks, |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 829 | std::vector<uint8_t>& buffer, int fd, const std::string& stashbase, bool* overlap) { |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 830 | |
| 831 | // At least it needs to provide three parameters: <tgt_range>, |
| 832 | // <src_block_count> and "-"/<src_range>. |
| 833 | if (params.cpos + 2 >= params.tokens.size()) { |
| 834 | fprintf(stderr, "invalid parameters\n"); |
| 835 | return -1; |
| 836 | } |
| 837 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 838 | // <tgt_range> |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 839 | parse_range(params.tokens[params.cpos++], tgt); |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 840 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 841 | // <src_block_count> |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 842 | const std::string& token = params.tokens[params.cpos++]; |
| 843 | if (!android::base::ParseUint(token.c_str(), &src_blocks)) { |
| 844 | fprintf(stderr, "invalid src_block_count \"%s\"\n", token.c_str()); |
| 845 | return -1; |
| 846 | } |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 847 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 848 | allocate(src_blocks * BLOCKSIZE, buffer); |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 849 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 850 | // "-" or <src_range> [<src_loc>] |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 851 | if (params.tokens[params.cpos] == "-") { |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 852 | // no source ranges, only stashes |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 853 | params.cpos++; |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 854 | } else { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 855 | RangeSet src; |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 856 | parse_range(params.tokens[params.cpos++], src); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 857 | int res = ReadBlocks(src, buffer, fd); |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 858 | |
Tao Bao | 34847b2 | 2015-09-08 11:05:49 -0700 | [diff] [blame] | 859 | if (overlap) { |
| 860 | *overlap = range_overlaps(src, tgt); |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 861 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 862 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 863 | if (res == -1) { |
| 864 | return -1; |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 867 | if (params.cpos >= params.tokens.size()) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 868 | // no stashes, only source range |
| 869 | return 0; |
| 870 | } |
| 871 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 872 | RangeSet locs; |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 873 | parse_range(params.tokens[params.cpos++], locs); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 874 | MoveRange(buffer, locs, buffer); |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 877 | // <[stash_id:stash_range]> |
| 878 | while (params.cpos < params.tokens.size()) { |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 879 | // Each word is a an index into the stash table, a colon, and |
| 880 | // then a rangeset describing where in the source block that |
| 881 | // stashed data should go. |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 882 | std::vector<std::string> tokens = android::base::Split(params.tokens[params.cpos++], ":"); |
| 883 | if (tokens.size() != 2) { |
| 884 | fprintf(stderr, "invalid parameter\n"); |
| 885 | return -1; |
| 886 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 887 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 888 | std::vector<uint8_t> stash; |
Tianjie Xu | 0188935 | 2016-03-22 18:08:12 -0700 | [diff] [blame] | 889 | int res = LoadStash(params, stashbase, tokens[0], false, nullptr, stash, true); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 890 | |
| 891 | if (res == -1) { |
| 892 | // These source blocks will fail verification if used later, but we |
| 893 | // will let the caller decide if this is a fatal failure |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 894 | fprintf(stderr, "failed to load stash %s\n", tokens[0].c_str()); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 895 | continue; |
| 896 | } |
| 897 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 898 | RangeSet locs; |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 899 | parse_range(tokens[1], locs); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 900 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 901 | MoveRange(buffer, locs, stash); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | return 0; |
| 905 | } |
| 906 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 907 | // Do a source/target load for move/bsdiff/imgdiff in version 3. |
| 908 | // |
| 909 | // Parameters are the same as for LoadSrcTgtVersion2, except for 'onehash', which |
| 910 | // tells the function whether to expect separate source and targe block hashes, or |
| 911 | // if they are both the same and only one hash should be expected, and |
| 912 | // 'isunresumable', which receives a non-zero value if block verification fails in |
| 913 | // a way that the update cannot be resumed anymore. |
| 914 | // |
| 915 | // If the function is unable to load the necessary blocks or their contents don't |
| 916 | // match the hashes, the return value is -1 and the command should be aborted. |
| 917 | // |
| 918 | // If the return value is 1, the command has already been completed according to |
| 919 | // the contents of the target blocks, and should not be performed again. |
| 920 | // |
| 921 | // If the return value is 0, source blocks have expected content and the command |
| 922 | // can be performed. |
| 923 | |
Tao Bao | 34847b2 | 2015-09-08 11:05:49 -0700 | [diff] [blame] | 924 | static int LoadSrcTgtVersion3(CommandParameters& params, RangeSet& tgt, size_t& src_blocks, |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 925 | bool onehash, bool& overlap) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 926 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 927 | if (params.cpos >= params.tokens.size()) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 928 | fprintf(stderr, "missing source hash\n"); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 929 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 932 | std::string srchash = params.tokens[params.cpos++]; |
| 933 | std::string tgthash; |
| 934 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 935 | if (onehash) { |
| 936 | tgthash = srchash; |
| 937 | } else { |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 938 | if (params.cpos >= params.tokens.size()) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 939 | fprintf(stderr, "missing target hash\n"); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 940 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 941 | } |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 942 | tgthash = params.tokens[params.cpos++]; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 945 | if (LoadSrcTgtVersion2(params, tgt, src_blocks, params.buffer, params.fd, params.stashbase, |
| 946 | &overlap) == -1) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 947 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Tao Bao | 34847b2 | 2015-09-08 11:05:49 -0700 | [diff] [blame] | 950 | std::vector<uint8_t> tgtbuffer(tgt.size * BLOCKSIZE); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 951 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 952 | if (ReadBlocks(tgt, tgtbuffer, params.fd) == -1) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 953 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 956 | if (VerifyBlocks(tgthash, tgtbuffer, tgt.size, false) == 0) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 957 | // Target blocks already have expected content, command should be skipped |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 958 | return 1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 961 | if (VerifyBlocks(srchash, params.buffer, src_blocks, true) == 0) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 962 | // If source and target blocks overlap, stash the source blocks so we can |
Tianjie Xu | 0188935 | 2016-03-22 18:08:12 -0700 | [diff] [blame] | 963 | // resume from possible write errors. In verify mode, we can skip stashing |
| 964 | // because the source blocks won't be overwritten. |
| 965 | if (overlap && params.canwrite) { |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 966 | fprintf(stderr, "stashing %zu overlapping blocks to %s\n", src_blocks, |
| 967 | srchash.c_str()); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 968 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 969 | bool stash_exists = false; |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 970 | if (WriteStash(params.stashbase, srchash, src_blocks, params.buffer, true, |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 971 | &stash_exists) != 0) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 972 | fprintf(stderr, "failed to stash overlapping source blocks\n"); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 973 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | // Can be deleted when the write has completed |
Sami Tolvanen | 43b748f | 2015-04-17 12:50:31 +0100 | [diff] [blame] | 977 | if (!stash_exists) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 978 | params.freestash = srchash; |
Sami Tolvanen | 43b748f | 2015-04-17 12:50:31 +0100 | [diff] [blame] | 979 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | // Source blocks have expected content, command can proceed |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 983 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Tianjie Xu | 0188935 | 2016-03-22 18:08:12 -0700 | [diff] [blame] | 986 | if (overlap && LoadStash(params, params.stashbase, srchash, true, nullptr, params.buffer, |
| 987 | true) == 0) { |
Sami Tolvanen | 43b748f | 2015-04-17 12:50:31 +0100 | [diff] [blame] | 988 | // Overlapping source blocks were previously stashed, command can proceed. |
| 989 | // We are recovering from an interrupted command, so we don't know if the |
| 990 | // stash can safely be deleted after this command. |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 991 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | // Valid source data not available, update cannot be resumed |
| 995 | fprintf(stderr, "partition has unexpected contents\n"); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 996 | params.isunresumable = true; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 997 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 998 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1001 | static int PerformCommandMove(CommandParameters& params) { |
| 1002 | size_t blocks = 0; |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 1003 | bool overlap = false; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1004 | int status = 0; |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1005 | RangeSet tgt; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1006 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1007 | if (params.version == 1) { |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1008 | status = LoadSrcTgtVersion1(params, tgt, blocks, params.buffer, params.fd); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1009 | } else if (params.version == 2) { |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1010 | status = LoadSrcTgtVersion2(params, tgt, blocks, params.buffer, params.fd, |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1011 | params.stashbase, nullptr); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1012 | } else if (params.version >= 3) { |
Tao Bao | 34847b2 | 2015-09-08 11:05:49 -0700 | [diff] [blame] | 1013 | status = LoadSrcTgtVersion3(params, tgt, blocks, true, overlap); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | if (status == -1) { |
| 1017 | fprintf(stderr, "failed to read blocks for move\n"); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1018 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | if (status == 0) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1022 | params.foundwrites = true; |
| 1023 | } else if (params.foundwrites) { |
| 1024 | fprintf(stderr, "warning: commands executed out of order [%s]\n", params.cmdname); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1027 | if (params.canwrite) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1028 | if (status == 0) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1029 | fprintf(stderr, " moving %zu blocks\n", blocks); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1030 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1031 | if (WriteBlocks(tgt, params.buffer, params.fd) == -1) { |
| 1032 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1033 | } |
| 1034 | } else { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1035 | fprintf(stderr, "skipping %zu already moved blocks\n", blocks); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | } |
| 1039 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1040 | if (!params.freestash.empty()) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1041 | FreeStash(params.stashbase, params.freestash); |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1042 | params.freestash.clear(); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1045 | params.written += tgt.size; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1046 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1047 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1050 | static int PerformCommandStash(CommandParameters& params) { |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1051 | return SaveStash(params, params.stashbase, params.buffer, params.fd, |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1052 | (params.version >= 3)); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1055 | static int PerformCommandFree(CommandParameters& params) { |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1056 | // <stash_id> |
| 1057 | if (params.cpos >= params.tokens.size()) { |
| 1058 | fprintf(stderr, "missing stash id in free command\n"); |
| 1059 | return -1; |
| 1060 | } |
| 1061 | |
Tianjie Xu | 0188935 | 2016-03-22 18:08:12 -0700 | [diff] [blame] | 1062 | const std::string& id = params.tokens[params.cpos++]; |
| 1063 | |
| 1064 | if (!params.canwrite && stash_map.find(id) != stash_map.end()) { |
| 1065 | stash_map.erase(id); |
| 1066 | return 0; |
| 1067 | } |
| 1068 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1069 | if (params.createdstash || params.canwrite) { |
Tianjie Xu | 0188935 | 2016-03-22 18:08:12 -0700 | [diff] [blame] | 1070 | return FreeStash(params.stashbase, id); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1076 | static int PerformCommandZero(CommandParameters& params) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1077 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1078 | if (params.cpos >= params.tokens.size()) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1079 | fprintf(stderr, "missing target blocks for zero\n"); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1080 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1083 | RangeSet tgt; |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1084 | parse_range(params.tokens[params.cpos++], tgt); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1085 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1086 | fprintf(stderr, " zeroing %zu blocks\n", tgt.size); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1087 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1088 | allocate(BLOCKSIZE, params.buffer); |
| 1089 | memset(params.buffer.data(), 0, BLOCKSIZE); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1090 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1091 | if (params.canwrite) { |
| 1092 | for (size_t i = 0; i < tgt.count; ++i) { |
| 1093 | if (!check_lseek(params.fd, (off64_t) tgt.pos[i * 2] * BLOCKSIZE, SEEK_SET)) { |
| 1094 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1097 | for (size_t j = tgt.pos[i * 2]; j < tgt.pos[i * 2 + 1]; ++j) { |
| 1098 | if (write_all(params.fd, params.buffer, BLOCKSIZE) == -1) { |
| 1099 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | } |
| 1104 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1105 | if (params.cmdname[0] == 'z') { |
Sami Tolvanen | e82fa18 | 2015-06-10 15:58:12 +0000 | [diff] [blame] | 1106 | // Update only for the zero command, as the erase command will call |
| 1107 | // this if DEBUG_ERASE is defined. |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1108 | params.written += tgt.size; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1111 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1114 | static int PerformCommandNew(CommandParameters& params) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1115 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1116 | if (params.cpos >= params.tokens.size()) { |
| 1117 | fprintf(stderr, "missing target blocks for new\n"); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1118 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1121 | RangeSet tgt; |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1122 | parse_range(params.tokens[params.cpos++], tgt); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1123 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1124 | if (params.canwrite) { |
| 1125 | fprintf(stderr, " writing %zu blocks of new data\n", tgt.size); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1126 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1127 | RangeSinkState rss(tgt); |
| 1128 | rss.fd = params.fd; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1129 | rss.p_block = 0; |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1130 | rss.p_remain = (tgt.pos[1] - tgt.pos[0]) * BLOCKSIZE; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1131 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1132 | if (!check_lseek(params.fd, (off64_t) tgt.pos[0] * BLOCKSIZE, SEEK_SET)) { |
| 1133 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1136 | pthread_mutex_lock(¶ms.nti.mu); |
| 1137 | params.nti.rss = &rss; |
| 1138 | pthread_cond_broadcast(¶ms.nti.cv); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1139 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1140 | while (params.nti.rss) { |
| 1141 | pthread_cond_wait(¶ms.nti.cv, ¶ms.nti.mu); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1144 | pthread_mutex_unlock(¶ms.nti.mu); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1147 | params.written += tgt.size; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1148 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1149 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1152 | static int PerformCommandDiff(CommandParameters& params) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1153 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1154 | // <offset> <length> |
| 1155 | if (params.cpos + 1 >= params.tokens.size()) { |
| 1156 | fprintf(stderr, "missing patch offset or length for %s\n", params.cmdname); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1157 | return -1; |
| 1158 | } |
| 1159 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1160 | size_t offset; |
| 1161 | if (!android::base::ParseUint(params.tokens[params.cpos++].c_str(), &offset)) { |
| 1162 | fprintf(stderr, "invalid patch offset\n"); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1163 | return -1; |
| 1164 | } |
| 1165 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1166 | size_t len; |
| 1167 | if (!android::base::ParseUint(params.tokens[params.cpos++].c_str(), &len)) { |
| 1168 | fprintf(stderr, "invalid patch offset\n"); |
| 1169 | return -1; |
| 1170 | } |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1171 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1172 | RangeSet tgt; |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1173 | size_t blocks = 0; |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1174 | bool overlap = false; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1175 | int status = 0; |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1176 | if (params.version == 1) { |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1177 | status = LoadSrcTgtVersion1(params, tgt, blocks, params.buffer, params.fd); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1178 | } else if (params.version == 2) { |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1179 | status = LoadSrcTgtVersion2(params, tgt, blocks, params.buffer, params.fd, |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1180 | params.stashbase, nullptr); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1181 | } else if (params.version >= 3) { |
Tao Bao | 34847b2 | 2015-09-08 11:05:49 -0700 | [diff] [blame] | 1182 | status = LoadSrcTgtVersion3(params, tgt, blocks, false, overlap); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | if (status == -1) { |
| 1186 | fprintf(stderr, "failed to read blocks for diff\n"); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1187 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | if (status == 0) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1191 | params.foundwrites = true; |
| 1192 | } else if (params.foundwrites) { |
| 1193 | fprintf(stderr, "warning: commands executed out of order [%s]\n", params.cmdname); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1196 | if (params.canwrite) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1197 | if (status == 0) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1198 | fprintf(stderr, "patching %zu blocks to %zu\n", blocks, tgt.size); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1199 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1200 | Value patch_value; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1201 | patch_value.type = VAL_BLOB; |
| 1202 | patch_value.size = len; |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1203 | patch_value.data = (char*) (params.patch_start + offset); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1204 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1205 | RangeSinkState rss(tgt); |
| 1206 | rss.fd = params.fd; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1207 | rss.p_block = 0; |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1208 | rss.p_remain = (tgt.pos[1] - tgt.pos[0]) * BLOCKSIZE; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1209 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1210 | if (!check_lseek(params.fd, (off64_t) tgt.pos[0] * BLOCKSIZE, SEEK_SET)) { |
| 1211 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1214 | if (params.cmdname[0] == 'i') { // imgdiff |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1215 | ApplyImagePatch(params.buffer.data(), blocks * BLOCKSIZE, &patch_value, |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1216 | &RangeSinkWrite, &rss, nullptr, nullptr); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1217 | } else { |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1218 | ApplyBSDiffPatch(params.buffer.data(), blocks * BLOCKSIZE, &patch_value, 0, |
| 1219 | &RangeSinkWrite, &rss, nullptr); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | // We expect the output of the patcher to fill the tgt ranges exactly. |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1223 | if (rss.p_block != tgt.count || rss.p_remain != 0) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1224 | fprintf(stderr, "range sink underrun?\n"); |
| 1225 | } |
| 1226 | } else { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1227 | fprintf(stderr, "skipping %zu blocks already patched to %zu [%s]\n", |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1228 | blocks, tgt.size, params.cmdline); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1232 | if (!params.freestash.empty()) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1233 | FreeStash(params.stashbase, params.freestash); |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1234 | params.freestash.clear(); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1237 | params.written += tgt.size; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1238 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1239 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1242 | static int PerformCommandErase(CommandParameters& params) { |
Sami Tolvanen | e82fa18 | 2015-06-10 15:58:12 +0000 | [diff] [blame] | 1243 | if (DEBUG_ERASE) { |
| 1244 | return PerformCommandZero(params); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1247 | struct stat sb; |
| 1248 | if (fstat(params.fd, &sb) == -1) { |
Elliott Hughes | 1fdd452 | 2015-03-23 13:33:57 -0700 | [diff] [blame] | 1249 | fprintf(stderr, "failed to fstat device to erase: %s\n", strerror(errno)); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1250 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1253 | if (!S_ISBLK(sb.st_mode)) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1254 | fprintf(stderr, "not a block device; skipping erase\n"); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1255 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1258 | if (params.cpos >= params.tokens.size()) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1259 | fprintf(stderr, "missing target blocks for erase\n"); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1260 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1263 | RangeSet tgt; |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1264 | parse_range(params.tokens[params.cpos++], tgt); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1265 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1266 | if (params.canwrite) { |
| 1267 | fprintf(stderr, " erasing %zu blocks\n", tgt.size); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1268 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1269 | for (size_t i = 0; i < tgt.count; ++i) { |
| 1270 | uint64_t blocks[2]; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1271 | // offset in bytes |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1272 | blocks[0] = tgt.pos[i * 2] * (uint64_t) BLOCKSIZE; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1273 | // length in bytes |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1274 | blocks[1] = (tgt.pos[i * 2 + 1] - tgt.pos[i * 2]) * (uint64_t) BLOCKSIZE; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1275 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1276 | if (ioctl(params.fd, BLKDISCARD, &blocks) == -1) { |
Elliott Hughes | 1fdd452 | 2015-03-23 13:33:57 -0700 | [diff] [blame] | 1277 | fprintf(stderr, "BLKDISCARD ioctl failed: %s\n", strerror(errno)); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1278 | return -1; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1283 | return 0; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | // Definitions for transfer list command functions |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1287 | typedef int (*CommandFunction)(CommandParameters&); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1288 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1289 | struct Command { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1290 | const char* name; |
| 1291 | CommandFunction f; |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1292 | }; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1293 | |
| 1294 | // CompareCommands and CompareCommandNames are for the hash table |
| 1295 | |
| 1296 | static int CompareCommands(const void* c1, const void* c2) { |
| 1297 | return strcmp(((const Command*) c1)->name, ((const Command*) c2)->name); |
| 1298 | } |
| 1299 | |
| 1300 | static int CompareCommandNames(const void* c1, const void* c2) { |
| 1301 | return strcmp(((const Command*) c1)->name, (const char*) c2); |
| 1302 | } |
| 1303 | |
| 1304 | // HashString is used to hash command names for the hash table |
| 1305 | |
| 1306 | static unsigned int HashString(const char *s) { |
| 1307 | unsigned int hash = 0; |
| 1308 | if (s) { |
| 1309 | while (*s) { |
| 1310 | hash = hash * 33 + *s++; |
| 1311 | } |
| 1312 | } |
| 1313 | return hash; |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 1314 | } |
| 1315 | |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1316 | // args: |
| 1317 | // - block device (or file) to modify in-place |
| 1318 | // - transfer list (blob) |
| 1319 | // - new data stream (filename within package.zip) |
| 1320 | // - patch stream (filename within package.zip, must be uncompressed) |
| 1321 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1322 | static Value* PerformBlockImageUpdate(const char* name, State* state, int /* argc */, Expr* argv[], |
| 1323 | const Command* commands, size_t cmdcount, bool dryrun) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1324 | CommandParameters params; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1325 | memset(¶ms, 0, sizeof(params)); |
| 1326 | params.canwrite = !dryrun; |
| 1327 | |
| 1328 | fprintf(stderr, "performing %s\n", dryrun ? "verification" : "update"); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1329 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1330 | Value* blockdev_filename = nullptr; |
| 1331 | Value* transfer_list_value = nullptr; |
| 1332 | Value* new_data_fn = nullptr; |
| 1333 | Value* patch_data_fn = nullptr; |
Doug Zongker | 1d5d609 | 2014-08-21 10:47:24 -0700 | [diff] [blame] | 1334 | if (ReadValueArgs(state, argv, 4, &blockdev_filename, &transfer_list_value, |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1335 | &new_data_fn, &patch_data_fn) < 0) { |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1336 | return StringValue(strdup("")); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1337 | } |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1338 | std::unique_ptr<Value, decltype(&FreeValue)> blockdev_filename_holder(blockdev_filename, |
| 1339 | FreeValue); |
| 1340 | std::unique_ptr<Value, decltype(&FreeValue)> transfer_list_value_holder(transfer_list_value, |
| 1341 | FreeValue); |
| 1342 | std::unique_ptr<Value, decltype(&FreeValue)> new_data_fn_holder(new_data_fn, FreeValue); |
| 1343 | std::unique_ptr<Value, decltype(&FreeValue)> patch_data_fn_holder(patch_data_fn, FreeValue); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1344 | |
| 1345 | if (blockdev_filename->type != VAL_STRING) { |
| 1346 | ErrorAbort(state, "blockdev_filename argument to %s must be string", name); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1347 | return StringValue(strdup("")); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1348 | } |
Doug Zongker | 1d5d609 | 2014-08-21 10:47:24 -0700 | [diff] [blame] | 1349 | if (transfer_list_value->type != VAL_BLOB) { |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1350 | ErrorAbort(state, "transfer_list argument to %s must be blob", name); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1351 | return StringValue(strdup("")); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1352 | } |
| 1353 | if (new_data_fn->type != VAL_STRING) { |
| 1354 | ErrorAbort(state, "new_data_fn argument to %s must be string", name); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1355 | return StringValue(strdup("")); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1356 | } |
| 1357 | if (patch_data_fn->type != VAL_STRING) { |
| 1358 | ErrorAbort(state, "patch_data_fn argument to %s must be string", name); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1359 | return StringValue(strdup("")); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1362 | UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1363 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1364 | if (ui == nullptr) { |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1365 | return StringValue(strdup("")); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1366 | } |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1367 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1368 | FILE* cmd_pipe = ui->cmd_pipe; |
| 1369 | ZipArchive* za = ui->package_zip; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1370 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1371 | if (cmd_pipe == nullptr || za == nullptr) { |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1372 | return StringValue(strdup("")); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1375 | const ZipEntry* patch_entry = mzFindZipEntry(za, patch_data_fn->data); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1376 | if (patch_entry == nullptr) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1377 | fprintf(stderr, "%s(): no file \"%s\" in package", name, patch_data_fn->data); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1378 | return StringValue(strdup("")); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1381 | params.patch_start = ui->package_zip_addr + mzGetZipEntryOffset(patch_entry); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1382 | const ZipEntry* new_entry = mzFindZipEntry(za, new_data_fn->data); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1383 | if (new_entry == nullptr) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1384 | fprintf(stderr, "%s(): no file \"%s\" in package", name, new_data_fn->data); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1385 | return StringValue(strdup("")); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1386 | } |
| 1387 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1388 | params.fd = TEMP_FAILURE_RETRY(open(blockdev_filename->data, O_RDWR)); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1389 | unique_fd fd_holder(params.fd); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1390 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1391 | if (params.fd == -1) { |
Elliott Hughes | 1fdd452 | 2015-03-23 13:33:57 -0700 | [diff] [blame] | 1392 | fprintf(stderr, "open \"%s\" failed: %s\n", blockdev_filename->data, strerror(errno)); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1393 | return StringValue(strdup("")); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1394 | } |
| 1395 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1396 | if (params.canwrite) { |
| 1397 | params.nti.za = za; |
| 1398 | params.nti.entry = new_entry; |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1399 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1400 | pthread_mutex_init(¶ms.nti.mu, nullptr); |
| 1401 | pthread_cond_init(¶ms.nti.cv, nullptr); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1402 | pthread_attr_t attr; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1403 | pthread_attr_init(&attr); |
| 1404 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
| 1405 | |
Elliott Hughes | 1fdd452 | 2015-03-23 13:33:57 -0700 | [diff] [blame] | 1406 | int error = pthread_create(¶ms.thread, &attr, unzip_new_data, ¶ms.nti); |
| 1407 | if (error != 0) { |
| 1408 | fprintf(stderr, "pthread_create failed: %s\n", strerror(error)); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1409 | return StringValue(strdup("")); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1413 | // Copy all the lines in transfer_list_value into std::string for |
| 1414 | // processing. |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1415 | const std::string transfer_list(transfer_list_value->data, transfer_list_value->size); |
| 1416 | std::vector<std::string> lines = android::base::Split(transfer_list, "\n"); |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1417 | if (lines.size() < 2) { |
| 1418 | ErrorAbort(state, "too few lines in the transfer list [%zd]\n", lines.size()); |
| 1419 | return StringValue(strdup("")); |
| 1420 | } |
Doug Zongker | 1d5d609 | 2014-08-21 10:47:24 -0700 | [diff] [blame] | 1421 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1422 | // First line in transfer list is the version number |
Tao Bao | 1fdec86 | 2015-10-21 14:57:44 -0700 | [diff] [blame] | 1423 | if (!android::base::ParseInt(lines[0].c_str(), ¶ms.version, 1, 4)) { |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1424 | fprintf(stderr, "unexpected transfer list version [%s]\n", lines[0].c_str()); |
| 1425 | return StringValue(strdup("")); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1428 | fprintf(stderr, "blockimg version is %d\n", params.version); |
| 1429 | |
| 1430 | // Second line in transfer list is the total number of blocks we expect to write |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 1431 | int total_blocks; |
| 1432 | if (!android::base::ParseInt(lines[1].c_str(), &total_blocks, 0)) { |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1433 | ErrorAbort(state, "unexpected block count [%s]\n", lines[1].c_str()); |
| 1434 | return StringValue(strdup("")); |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | if (total_blocks == 0) { |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1438 | return StringValue(strdup("t")); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1441 | size_t start = 2; |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1442 | if (params.version >= 2) { |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1443 | if (lines.size() < 4) { |
| 1444 | ErrorAbort(state, "too few lines in the transfer list [%zu]\n", lines.size()); |
| 1445 | return StringValue(strdup("")); |
| 1446 | } |
| 1447 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1448 | // Third line is how many stash entries are needed simultaneously |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1449 | fprintf(stderr, "maximum stash entries %s\n", lines[2].c_str()); |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 1450 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1451 | // Fourth line is the maximum number of blocks that will be stashed simultaneously |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 1452 | int stash_max_blocks; |
| 1453 | if (!android::base::ParseInt(lines[3].c_str(), &stash_max_blocks, 0)) { |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1454 | ErrorAbort(state, "unexpected maximum stash blocks [%s]\n", lines[3].c_str()); |
| 1455 | return StringValue(strdup("")); |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 1458 | int res = CreateStash(state, stash_max_blocks, blockdev_filename->data, params.stashbase); |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 1459 | if (res == -1) { |
| 1460 | return StringValue(strdup("")); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1461 | } |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1462 | |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 1463 | params.createdstash = res; |
| 1464 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1465 | start += 2; |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 1466 | } |
| 1467 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1468 | // Build a hash table of the available commands |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1469 | HashTable* cmdht = mzHashTableCreate(cmdcount, nullptr); |
| 1470 | std::unique_ptr<HashTable, decltype(&mzHashTableFree)> cmdht_holder(cmdht, mzHashTableFree); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1471 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1472 | for (size_t i = 0; i < cmdcount; ++i) { |
| 1473 | unsigned int cmdhash = HashString(commands[i].name); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1474 | mzHashTableLookup(cmdht, cmdhash, (void*) &commands[i], CompareCommands, true); |
| 1475 | } |
| 1476 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1477 | int rc = -1; |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 1478 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1479 | // Subsequent lines are all individual transfer commands |
| 1480 | for (auto it = lines.cbegin() + start; it != lines.cend(); it++) { |
| 1481 | const std::string& line_str(*it); |
Tao Bao | 6a47dff | 2015-09-25 17:12:28 -0700 | [diff] [blame] | 1482 | if (line_str.empty()) { |
| 1483 | continue; |
| 1484 | } |
| 1485 | |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1486 | params.tokens = android::base::Split(line_str, " "); |
| 1487 | params.cpos = 0; |
| 1488 | params.cmdname = params.tokens[params.cpos++].c_str(); |
| 1489 | params.cmdline = line_str.c_str(); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1490 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1491 | unsigned int cmdhash = HashString(params.cmdname); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1492 | const Command* cmd = reinterpret_cast<const Command*>(mzHashTableLookup(cmdht, cmdhash, |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1493 | const_cast<char*>(params.cmdname), CompareCommandNames, |
| 1494 | false)); |
Doug Zongker | 52ae67d | 2014-09-08 12:22:09 -0700 | [diff] [blame] | 1495 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1496 | if (cmd == nullptr) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1497 | fprintf(stderr, "unexpected command [%s]\n", params.cmdname); |
| 1498 | goto pbiudone; |
| 1499 | } |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1500 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1501 | if (cmd->f != nullptr && cmd->f(params) == -1) { |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1502 | fprintf(stderr, "failed to execute command [%s]\n", line_str.c_str()); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1503 | goto pbiudone; |
| 1504 | } |
| 1505 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1506 | if (params.canwrite) { |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 1507 | if (ota_fsync(params.fd) == -1) { |
Tao Bao | 187efff | 2015-07-27 14:07:08 -0700 | [diff] [blame] | 1508 | fprintf(stderr, "fsync failed: %s\n", strerror(errno)); |
| 1509 | goto pbiudone; |
| 1510 | } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1511 | fprintf(cmd_pipe, "set_progress %.4f\n", (double) params.written / total_blocks); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1512 | fflush(cmd_pipe); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1513 | } |
| 1514 | } |
| 1515 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1516 | if (params.canwrite) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1517 | pthread_join(params.thread, nullptr); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1518 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1519 | fprintf(stderr, "wrote %zu blocks; expected %d\n", params.written, total_blocks); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1520 | fprintf(stderr, "max alloc needed was %zu\n", params.buffer.size()); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1521 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1522 | // Delete stash only after successfully completing the update, as it |
| 1523 | // may contain blocks needed to complete the update later. |
| 1524 | DeleteStash(params.stashbase); |
| 1525 | } else { |
| 1526 | fprintf(stderr, "verified partition contents; update may be resumed\n"); |
| 1527 | } |
| 1528 | |
| 1529 | rc = 0; |
| 1530 | |
| 1531 | pbiudone: |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 1532 | if (ota_fsync(params.fd) == -1) { |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1533 | fprintf(stderr, "fsync failed: %s\n", strerror(errno)); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1534 | } |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1535 | // params.fd will be automatically closed because of the fd_holder above. |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1536 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1537 | // Only delete the stash if the update cannot be resumed, or it's |
| 1538 | // a verification run and we created the stash. |
| 1539 | if (params.isunresumable || (!params.canwrite && params.createdstash)) { |
| 1540 | DeleteStash(params.stashbase); |
| 1541 | } |
| 1542 | |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1543 | return StringValue(rc == 0 ? strdup("t") : strdup("")); |
| 1544 | } |
| 1545 | |
| 1546 | // The transfer list is a text file containing commands to |
| 1547 | // transfer data from one place to another on the target |
| 1548 | // partition. We parse it and execute the commands in order: |
| 1549 | // |
| 1550 | // zero [rangeset] |
| 1551 | // - fill the indicated blocks with zeros |
| 1552 | // |
| 1553 | // new [rangeset] |
| 1554 | // - fill the blocks with data read from the new_data file |
| 1555 | // |
| 1556 | // erase [rangeset] |
| 1557 | // - mark the given blocks as empty |
| 1558 | // |
| 1559 | // move <...> |
| 1560 | // bsdiff <patchstart> <patchlen> <...> |
| 1561 | // imgdiff <patchstart> <patchlen> <...> |
| 1562 | // - read the source blocks, apply a patch (or not in the |
| 1563 | // case of move), write result to target blocks. bsdiff or |
| 1564 | // imgdiff specifies the type of patch; move means no patch |
| 1565 | // at all. |
| 1566 | // |
| 1567 | // The format of <...> differs between versions 1 and 2; |
| 1568 | // see the LoadSrcTgtVersion{1,2}() functions for a |
| 1569 | // description of what's expected. |
| 1570 | // |
| 1571 | // stash <stash_id> <src_range> |
| 1572 | // - (version 2+ only) load the given source range and stash |
| 1573 | // the data in the given slot of the stash table. |
| 1574 | // |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1575 | // free <stash_id> |
| 1576 | // - (version 3+ only) free the given stash data. |
| 1577 | // |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1578 | // The creator of the transfer list will guarantee that no block |
| 1579 | // is read (ie, used as the source for a patch or move) after it |
| 1580 | // has been written. |
| 1581 | // |
| 1582 | // In version 2, the creator will guarantee that a given stash is |
| 1583 | // loaded (with a stash command) before it's used in a |
| 1584 | // move/bsdiff/imgdiff command. |
| 1585 | // |
| 1586 | // Within one command the source and target ranges may overlap so |
| 1587 | // in general we need to read the entire source into memory before |
| 1588 | // writing anything to the target blocks. |
| 1589 | // |
| 1590 | // All the patch data is concatenated into one patch_data file in |
| 1591 | // the update package. It must be stored uncompressed because we |
| 1592 | // memory-map it in directly from the archive. (Since patches are |
| 1593 | // already compressed, we lose very little by not compressing |
| 1594 | // their concatenation.) |
| 1595 | // |
| 1596 | // In version 3, commands that read data from the partition (i.e. |
| 1597 | // move/bsdiff/imgdiff/stash) have one or more additional hashes |
| 1598 | // before the range parameters, which are used to check if the |
| 1599 | // command has already been completed and verify the integrity of |
| 1600 | // the source data. |
| 1601 | |
| 1602 | Value* BlockImageVerifyFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1603 | // Commands which are not tested are set to nullptr to skip them completely |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1604 | const Command commands[] = { |
| 1605 | { "bsdiff", PerformCommandDiff }, |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1606 | { "erase", nullptr }, |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1607 | { "free", PerformCommandFree }, |
| 1608 | { "imgdiff", PerformCommandDiff }, |
| 1609 | { "move", PerformCommandMove }, |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1610 | { "new", nullptr }, |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1611 | { "stash", PerformCommandStash }, |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1612 | { "zero", nullptr } |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1613 | }; |
| 1614 | |
| 1615 | // Perform a dry run without writing to test if an update can proceed |
| 1616 | return PerformBlockImageUpdate(name, state, argc, argv, commands, |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 1617 | sizeof(commands) / sizeof(commands[0]), true); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1621 | const Command commands[] = { |
| 1622 | { "bsdiff", PerformCommandDiff }, |
| 1623 | { "erase", PerformCommandErase }, |
| 1624 | { "free", PerformCommandFree }, |
| 1625 | { "imgdiff", PerformCommandDiff }, |
| 1626 | { "move", PerformCommandMove }, |
| 1627 | { "new", PerformCommandNew }, |
| 1628 | { "stash", PerformCommandStash }, |
| 1629 | { "zero", PerformCommandZero } |
| 1630 | }; |
| 1631 | |
| 1632 | return PerformBlockImageUpdate(name, state, argc, argv, commands, |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 1633 | sizeof(commands) / sizeof(commands[0]), false); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1634 | } |
| 1635 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1636 | Value* RangeSha1Fn(const char* name, State* state, int /* argc */, Expr* argv[]) { |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1637 | Value* blockdev_filename; |
| 1638 | Value* ranges; |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1639 | |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1640 | if (ReadValueArgs(state, argv, 2, &blockdev_filename, &ranges) < 0) { |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1641 | return StringValue(strdup("")); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1642 | } |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1643 | std::unique_ptr<Value, decltype(&FreeValue)> ranges_holder(ranges, FreeValue); |
| 1644 | std::unique_ptr<Value, decltype(&FreeValue)> blockdev_filename_holder(blockdev_filename, |
| 1645 | FreeValue); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1646 | |
| 1647 | if (blockdev_filename->type != VAL_STRING) { |
| 1648 | ErrorAbort(state, "blockdev_filename argument to %s must be string", name); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1649 | return StringValue(strdup("")); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1650 | } |
| 1651 | if (ranges->type != VAL_STRING) { |
| 1652 | ErrorAbort(state, "ranges argument to %s must be string", name); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1653 | return StringValue(strdup("")); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1654 | } |
| 1655 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1656 | int fd = open(blockdev_filename->data, O_RDWR); |
| 1657 | unique_fd fd_holder(fd); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1658 | if (fd < 0) { |
Elliott Hughes | 1fdd452 | 2015-03-23 13:33:57 -0700 | [diff] [blame] | 1659 | ErrorAbort(state, "open \"%s\" failed: %s", blockdev_filename->data, strerror(errno)); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1660 | return StringValue(strdup("")); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1663 | RangeSet rs; |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1664 | parse_range(ranges->data, rs); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1665 | |
| 1666 | SHA_CTX ctx; |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 1667 | SHA1_Init(&ctx); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1668 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1669 | std::vector<uint8_t> buffer(BLOCKSIZE); |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1670 | for (size_t i = 0; i < rs.count; ++i) { |
| 1671 | if (!check_lseek(fd, (off64_t)rs.pos[i*2] * BLOCKSIZE, SEEK_SET)) { |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1672 | ErrorAbort(state, "failed to seek %s: %s", blockdev_filename->data, strerror(errno)); |
| 1673 | return StringValue(strdup("")); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1676 | for (size_t j = rs.pos[i*2]; j < rs.pos[i*2+1]; ++j) { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1677 | if (read_all(fd, buffer, BLOCKSIZE) == -1) { |
| 1678 | ErrorAbort(state, "failed to read %s: %s", blockdev_filename->data, |
Tao Bao | 0940fe1 | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1679 | strerror(errno)); |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1680 | return StringValue(strdup("")); |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 1683 | SHA1_Update(&ctx, buffer.data(), BLOCKSIZE); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1684 | } |
| 1685 | } |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 1686 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 1687 | SHA1_Final(digest, &ctx); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1688 | |
Tao Bao | 612336d | 2015-08-27 16:41:21 -0700 | [diff] [blame] | 1689 | return StringValue(strdup(print_sha1(digest).c_str())); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1690 | } |
| 1691 | |
Tianjie Xu | 30bf476 | 2015-12-15 11:47:30 -0800 | [diff] [blame] | 1692 | // This function checks if a device has been remounted R/W prior to an incremental |
| 1693 | // OTA update. This is an common cause of update abortion. The function reads the |
| 1694 | // 1st block of each partition and check for mounting time/count. It return string "t" |
| 1695 | // if executes successfully and an empty string otherwise. |
| 1696 | |
| 1697 | Value* CheckFirstBlockFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1698 | Value* arg_filename; |
| 1699 | |
| 1700 | if (ReadValueArgs(state, argv, 1, &arg_filename) < 0) { |
| 1701 | return nullptr; |
| 1702 | } |
| 1703 | std::unique_ptr<Value, decltype(&FreeValue)> filename(arg_filename, FreeValue); |
| 1704 | |
| 1705 | if (filename->type != VAL_STRING) { |
| 1706 | ErrorAbort(state, "filename argument to %s must be string", name); |
| 1707 | return StringValue(strdup("")); |
| 1708 | } |
| 1709 | |
| 1710 | int fd = open(arg_filename->data, O_RDONLY); |
| 1711 | unique_fd fd_holder(fd); |
| 1712 | if (fd == -1) { |
| 1713 | ErrorAbort(state, "open \"%s\" failed: %s", arg_filename->data, strerror(errno)); |
| 1714 | return StringValue(strdup("")); |
| 1715 | } |
| 1716 | |
| 1717 | RangeSet blk0 {1 /*count*/, 1/*size*/, std::vector<size_t> {0, 1}/*position*/}; |
| 1718 | std::vector<uint8_t> block0_buffer(BLOCKSIZE); |
| 1719 | |
| 1720 | if (ReadBlocks(blk0, block0_buffer, fd) == -1) { |
| 1721 | ErrorAbort(state, "failed to read %s: %s", arg_filename->data, |
| 1722 | strerror(errno)); |
| 1723 | return StringValue(strdup("")); |
| 1724 | } |
| 1725 | |
| 1726 | // https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout |
| 1727 | // Super block starts from block 0, offset 0x400 |
| 1728 | // 0x2C: len32 Mount time |
| 1729 | // 0x30: len32 Write time |
| 1730 | // 0x34: len16 Number of mounts since the last fsck |
| 1731 | // 0x38: len16 Magic signature 0xEF53 |
| 1732 | |
| 1733 | time_t mount_time = *reinterpret_cast<uint32_t*>(&block0_buffer[0x400+0x2C]); |
| 1734 | uint16_t mount_count = *reinterpret_cast<uint16_t*>(&block0_buffer[0x400+0x34]); |
| 1735 | |
| 1736 | if (mount_count > 0) { |
| 1737 | uiPrintf(state, "Device was remounted R/W %d times\n", mount_count); |
| 1738 | uiPrintf(state, "Last remount happened on %s", ctime(&mount_time)); |
| 1739 | } |
| 1740 | |
| 1741 | return StringValue(strdup("t")); |
| 1742 | } |
| 1743 | |
| 1744 | |
Sami Tolvanen | 0a7b473 | 2015-06-25 10:25:36 +0100 | [diff] [blame] | 1745 | Value* BlockImageRecoverFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1746 | Value* arg_filename; |
| 1747 | Value* arg_ranges; |
| 1748 | |
| 1749 | if (ReadValueArgs(state, argv, 2, &arg_filename, &arg_ranges) < 0) { |
| 1750 | return NULL; |
| 1751 | } |
| 1752 | |
| 1753 | std::unique_ptr<Value, decltype(&FreeValue)> filename(arg_filename, FreeValue); |
| 1754 | std::unique_ptr<Value, decltype(&FreeValue)> ranges(arg_ranges, FreeValue); |
| 1755 | |
| 1756 | if (filename->type != VAL_STRING) { |
| 1757 | ErrorAbort(state, "filename argument to %s must be string", name); |
| 1758 | return StringValue(strdup("")); |
| 1759 | } |
| 1760 | if (ranges->type != VAL_STRING) { |
| 1761 | ErrorAbort(state, "ranges argument to %s must be string", name); |
| 1762 | return StringValue(strdup("")); |
| 1763 | } |
| 1764 | |
Tianjie Xu | b686ba2 | 2015-12-09 15:29:45 -0800 | [diff] [blame] | 1765 | // Output notice to log when recover is attempted |
| 1766 | fprintf(stderr, "%s image corrupted, attempting to recover...\n", filename->data); |
| 1767 | |
Sami Tolvanen | 0a7b473 | 2015-06-25 10:25:36 +0100 | [diff] [blame] | 1768 | // When opened with O_RDWR, libfec rewrites corrupted blocks when they are read |
| 1769 | fec::io fh(filename->data, O_RDWR); |
| 1770 | |
| 1771 | if (!fh) { |
| 1772 | ErrorAbort(state, "fec_open \"%s\" failed: %s", filename->data, strerror(errno)); |
| 1773 | return StringValue(strdup("")); |
| 1774 | } |
| 1775 | |
| 1776 | if (!fh.has_ecc() || !fh.has_verity()) { |
| 1777 | ErrorAbort(state, "unable to use metadata to correct errors"); |
| 1778 | return StringValue(strdup("")); |
| 1779 | } |
| 1780 | |
| 1781 | fec_status status; |
| 1782 | |
| 1783 | if (!fh.get_status(status)) { |
| 1784 | ErrorAbort(state, "failed to read FEC status"); |
| 1785 | return StringValue(strdup("")); |
| 1786 | } |
| 1787 | |
| 1788 | RangeSet rs; |
| 1789 | parse_range(ranges->data, rs); |
| 1790 | |
| 1791 | uint8_t buffer[BLOCKSIZE]; |
| 1792 | |
| 1793 | for (size_t i = 0; i < rs.count; ++i) { |
| 1794 | for (size_t j = rs.pos[i * 2]; j < rs.pos[i * 2 + 1]; ++j) { |
| 1795 | // Stay within the data area, libfec validates and corrects metadata |
| 1796 | if (status.data_size <= (uint64_t)j * BLOCKSIZE) { |
| 1797 | continue; |
| 1798 | } |
| 1799 | |
| 1800 | if (fh.pread(buffer, BLOCKSIZE, (off64_t)j * BLOCKSIZE) != BLOCKSIZE) { |
Tao Bao | baad2d4 | 2015-12-06 16:56:27 -0800 | [diff] [blame] | 1801 | ErrorAbort(state, "failed to recover %s (block %zu): %s", filename->data, |
Sami Tolvanen | 0a7b473 | 2015-06-25 10:25:36 +0100 | [diff] [blame] | 1802 | j, strerror(errno)); |
| 1803 | return StringValue(strdup("")); |
| 1804 | } |
| 1805 | |
| 1806 | // If we want to be able to recover from a situation where rewriting a corrected |
| 1807 | // block doesn't guarantee the same data will be returned when re-read later, we |
| 1808 | // can save a copy of corrected blocks to /cache. Note: |
| 1809 | // |
| 1810 | // 1. Maximum space required from /cache is the same as the maximum number of |
| 1811 | // corrupted blocks we can correct. For RS(255, 253) and a 2 GiB partition, |
| 1812 | // this would be ~16 MiB, for example. |
| 1813 | // |
| 1814 | // 2. To find out if this block was corrupted, call fec_get_status after each |
| 1815 | // read and check if the errors field value has increased. |
| 1816 | } |
| 1817 | } |
Tianjie Xu | b686ba2 | 2015-12-09 15:29:45 -0800 | [diff] [blame] | 1818 | fprintf(stderr, "...%s image recovered successfully.\n", filename->data); |
Sami Tolvanen | 0a7b473 | 2015-06-25 10:25:36 +0100 | [diff] [blame] | 1819 | return StringValue(strdup("t")); |
| 1820 | } |
| 1821 | |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1822 | void RegisterBlockImageFunctions() { |
Sami Tolvanen | 9022120 | 2014-12-09 16:39:47 +0000 | [diff] [blame] | 1823 | RegisterFunction("block_image_verify", BlockImageVerifyFn); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1824 | RegisterFunction("block_image_update", BlockImageUpdateFn); |
Sami Tolvanen | 0a7b473 | 2015-06-25 10:25:36 +0100 | [diff] [blame] | 1825 | RegisterFunction("block_image_recover", BlockImageRecoverFn); |
Tianjie Xu | 30bf476 | 2015-12-15 11:47:30 -0800 | [diff] [blame] | 1826 | RegisterFunction("check_first_block", CheckFirstBlockFn); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 1827 | RegisterFunction("range_sha1", RangeSha1Fn); |
| 1828 | } |