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