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