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