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