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