blob: 42ae649cc007b34e44631c881079d47cef6f5169 [file] [log] [blame]
Doug Zongker76adfc52014-01-13 10:04:25 -08001/*
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// This program takes a file on an ext4 filesystem and produces a list
18// of the blocks that file occupies, which enables the file contents
19// to be read directly from the block device without mounting the
20// filesystem.
21//
22// If the filesystem is using an encrypted block device, it will also
23// read the file and rewrite it to the same blocks of the underlying
24// (unencrypted) block device, so the file contents can be read
25// without the need for the decryption key.
26//
27// The output of this program is a "block map" which looks like this:
28//
29// /dev/block/platform/msm_sdcc.1/by-name/userdata # block device
30// 49652 4096 # file size in bytes, block size
31// 3 # count of block ranges
32// 1000 1008 # block range 0
33// 2100 2102 # ... block range 1
34// 30 33 # ... block range 2
35//
36// Each block range represents a half-open interval; the line "30 33"
37// reprents the blocks [30, 31, 32].
38//
39// Recovery can take this block map file and retrieve the underlying
40// file data to use as an update package.
41
Elliott Hughesd4d4c242014-12-29 12:46:43 -080042#include <errno.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080043#include <stdio.h>
44#include <stdlib.h>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080045#include <string.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080046#include <stdarg.h>
47#include <sys/types.h>
48#include <sys/stat.h>
49#include <fcntl.h>
50#include <linux/fs.h>
51#include <sys/mman.h>
52
Doug Zongkerf449db22014-08-26 09:15:08 -070053#define LOG_TAG "uncrypt"
54#include <log/log.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080055#include <cutils/properties.h>
56#include <fs_mgr.h>
57
58#define WINDOW_SIZE 5
59#define RECOVERY_COMMAND_FILE "/cache/recovery/command"
60#define RECOVERY_COMMAND_FILE_TMP "/cache/recovery/command.tmp"
61#define CACHE_BLOCK_MAP "/cache/recovery/block.map"
62
Doug Zongker2efc9d92014-08-18 15:55:28 -070063static struct fstab* fstab = NULL;
64
Doug Zongker76adfc52014-01-13 10:04:25 -080065static int write_at_offset(unsigned char* buffer, size_t size,
66 int wfd, off64_t offset)
67{
Elliott Hughes7bad7c42015-04-28 17:24:24 -070068 if (TEMP_FAILURE_RETRY(lseek64(wfd, offset, SEEK_SET)) == -1) {
69 ALOGE("error seeking to offset %lld: %s\n", offset, strerror(errno));
70 return -1;
71 }
Doug Zongker76adfc52014-01-13 10:04:25 -080072 size_t written = 0;
73 while (written < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -070074 ssize_t wrote = TEMP_FAILURE_RETRY(write(wfd, buffer + written, size - written));
75 if (wrote == -1) {
76 ALOGE("error writing offset %lld: %s\n", (offset + written), strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -080077 return -1;
78 }
79 written += wrote;
80 }
81 return 0;
82}
83
84void add_block_to_ranges(int** ranges, int* range_alloc, int* range_used, int new_block)
85{
86 // If the current block start is < 0, set the start to the new
87 // block. (This only happens for the very first block of the very
88 // first range.)
89 if ((*ranges)[*range_used*2-2] < 0) {
90 (*ranges)[*range_used*2-2] = new_block;
91 (*ranges)[*range_used*2-1] = new_block;
92 }
93
94 if (new_block == (*ranges)[*range_used*2-1]) {
95 // If the new block comes immediately after the current range,
96 // all we have to do is extend the current range.
97 ++(*ranges)[*range_used*2-1];
98 } else {
99 // We need to start a new range.
100
101 // If there isn't enough room in the array, we need to expand it.
102 if (*range_used >= *range_alloc) {
103 *range_alloc *= 2;
104 *ranges = realloc(*ranges, *range_alloc * 2 * sizeof(int));
105 }
106
107 ++*range_used;
108 (*ranges)[*range_used*2-2] = new_block;
109 (*ranges)[*range_used*2-1] = new_block+1;
110 }
111}
112
Doug Zongker2efc9d92014-08-18 15:55:28 -0700113static struct fstab* read_fstab()
Doug Zongker76adfc52014-01-13 10:04:25 -0800114{
Doug Zongker2efc9d92014-08-18 15:55:28 -0700115 fstab = NULL;
116
Doug Zongker76adfc52014-01-13 10:04:25 -0800117 // The fstab path is always "/fstab.${ro.hardware}".
118 char fstab_path[PATH_MAX+1] = "/fstab.";
119 if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700120 ALOGE("failed to get ro.hardware\n");
Doug Zongker76adfc52014-01-13 10:04:25 -0800121 return NULL;
122 }
123
Doug Zongker2efc9d92014-08-18 15:55:28 -0700124 fstab = fs_mgr_read_fstab(fstab_path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800125 if (!fstab) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700126 ALOGE("failed to read %s\n", fstab_path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800127 return NULL;
128 }
129
Doug Zongker2efc9d92014-08-18 15:55:28 -0700130 return fstab;
131}
132
133const char* find_block_device(const char* path, int* encryptable, int* encrypted)
134{
Doug Zongker76adfc52014-01-13 10:04:25 -0800135 // Look for a volume whose mount point is the prefix of path and
136 // return its block device. Set encrypted if it's currently
137 // encrypted.
138 int i;
139 for (i = 0; i < fstab->num_entries; ++i) {
140 struct fstab_rec* v = &fstab->recs[i];
141 if (!v->mount_point) continue;
142 int len = strlen(v->mount_point);
143 if (strncmp(path, v->mount_point, len) == 0 &&
144 (path[len] == '/' || path[len] == 0)) {
145 *encrypted = 0;
146 *encryptable = 0;
147 if (fs_mgr_is_encryptable(v)) {
148 *encryptable = 1;
149 char buffer[PROPERTY_VALUE_MAX+1];
150 if (property_get("ro.crypto.state", buffer, "") &&
151 strcmp(buffer, "encrypted") == 0) {
152 *encrypted = 1;
153 }
154 }
155 return v->blk_device;
156 }
157 }
158
159 return NULL;
160}
161
Tao Baofb4ccef2015-05-04 10:10:13 -0700162// Parse the command file RECOVERY_COMMAND_FILE to find the update package
163// name. If it's on the /data partition, replace the package name with the
164// block map file name and store it temporarily in RECOVERY_COMMAND_FILE_TMP.
165// It will be renamed to RECOVERY_COMMAND_FILE if uncrypt finishes
166// successfully.
167static char* find_update_package()
Doug Zongker76adfc52014-01-13 10:04:25 -0800168{
Doug Zongker76adfc52014-01-13 10:04:25 -0800169 FILE* f = fopen(RECOVERY_COMMAND_FILE, "r");
Maxim Siniavinee7b28882014-02-13 15:48:53 -0800170 if (f == NULL) {
171 return NULL;
172 }
Sungmin Choia72512c2014-12-10 21:57:09 +0900173 int fd = open(RECOVERY_COMMAND_FILE_TMP, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR);
174 if (fd < 0) {
175 ALOGE("failed to open %s\n", RECOVERY_COMMAND_FILE_TMP);
176 return NULL;
177 }
Michael Runge4b542392014-11-21 16:00:45 -0800178 FILE* fo = fdopen(fd, "w");
Tao Baofb4ccef2015-05-04 10:10:13 -0700179 char* fn = NULL;
180 char* line = NULL;
181 size_t len = 0;
182 while (getline(&line, &len, f) != -1) {
183 if (strncmp(line, "--update_package=", strlen("--update_package=")) == 0) {
184 fn = strdup(line + strlen("--update_package="));
185 // Replace the package name with block map file if it's on /data partition.
186 if (strncmp(fn, "/data/", strlen("/data/")) == 0) {
187 fputs("--update_package=@" CACHE_BLOCK_MAP "\n", fo);
188 continue;
189 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800190 }
Tao Baofb4ccef2015-05-04 10:10:13 -0700191 fputs(line, fo);
Doug Zongker76adfc52014-01-13 10:04:25 -0800192 }
Tao Baofb4ccef2015-05-04 10:10:13 -0700193 free(line);
Doug Zongker76adfc52014-01-13 10:04:25 -0800194 fclose(f);
Tao Baofb4ccef2015-05-04 10:10:13 -0700195 if (fsync(fd) == -1) {
196 ALOGE("failed to fsync \"%s\": %s\n", RECOVERY_COMMAND_FILE_TMP, strerror(errno));
197 fclose(fo);
198 return NULL;
199 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800200 fclose(fo);
201
202 if (fn) {
203 char* newline = strchr(fn, '\n');
204 if (newline) *newline = 0;
205 }
206 return fn;
207}
208
209int produce_block_map(const char* path, const char* map_file, const char* blk_dev,
210 int encrypted)
211{
212 struct stat sb;
213 int ret;
214
Sungmin Choia72512c2014-12-10 21:57:09 +0900215 int mapfd = open(map_file, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR);
216 if (mapfd < 0) {
217 ALOGE("failed to open %s\n", map_file);
218 return -1;
219 }
Michael Runge4b542392014-11-21 16:00:45 -0800220 FILE* mapf = fdopen(mapfd, "w");
Doug Zongker76adfc52014-01-13 10:04:25 -0800221
222 ret = stat(path, &sb);
223 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700224 ALOGE("failed to stat %s\n", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800225 return -1;
226 }
227
Doug Zongkerf449db22014-08-26 09:15:08 -0700228 ALOGI(" block size: %ld bytes\n", (long)sb.st_blksize);
Doug Zongker76adfc52014-01-13 10:04:25 -0800229
230 int blocks = ((sb.st_size-1) / sb.st_blksize) + 1;
Doug Zongkerf449db22014-08-26 09:15:08 -0700231 ALOGI(" file size: %lld bytes, %d blocks\n", (long long)sb.st_size, blocks);
Doug Zongker76adfc52014-01-13 10:04:25 -0800232
233 int* ranges;
234 int range_alloc = 1;
235 int range_used = 1;
236 ranges = malloc(range_alloc * 2 * sizeof(int));
237 ranges[0] = -1;
238 ranges[1] = -1;
239
Mark Salyzyn2605dec2014-03-19 15:30:25 -0700240 fprintf(mapf, "%s\n%lld %lu\n", blk_dev, (long long)sb.st_size, (unsigned long)sb.st_blksize);
Doug Zongker76adfc52014-01-13 10:04:25 -0800241
242 unsigned char* buffers[WINDOW_SIZE];
243 int i;
244 if (encrypted) {
245 for (i = 0; i < WINDOW_SIZE; ++i) {
246 buffers[i] = malloc(sb.st_blksize);
247 }
248 }
249 int head_block = 0;
250 int head = 0, tail = 0;
251 size_t pos = 0;
252
253 int fd = open(path, O_RDONLY);
254 if (fd < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700255 ALOGE("failed to open fd for reading: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800256 return -1;
257 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800258
259 int wfd = -1;
260 if (encrypted) {
Michael Runge4b542392014-11-21 16:00:45 -0800261 wfd = open(blk_dev, O_WRONLY | O_SYNC);
Doug Zongker76adfc52014-01-13 10:04:25 -0800262 if (wfd < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700263 ALOGE("failed to open fd for writing: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800264 return -1;
265 }
266 }
267
268 while (pos < sb.st_size) {
269 if ((tail+1) % WINDOW_SIZE == head) {
270 // write out head buffer
271 int block = head_block;
272 ret = ioctl(fd, FIBMAP, &block);
273 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700274 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800275 return -1;
276 }
277 add_block_to_ranges(&ranges, &range_alloc, &range_used, block);
278 if (encrypted) {
279 if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) {
280 return -1;
281 }
282 }
283 head = (head + 1) % WINDOW_SIZE;
284 ++head_block;
285 }
286
287 // read next block to tail
288 if (encrypted) {
289 size_t so_far = 0;
290 while (so_far < sb.st_blksize && pos < sb.st_size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700291 ssize_t this_read =
292 TEMP_FAILURE_RETRY(read(fd, buffers[tail] + so_far, sb.st_blksize - so_far));
293 if (this_read == -1) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700294 ALOGE("failed to read: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800295 return -1;
296 }
297 so_far += this_read;
298 pos += this_read;
299 }
300 } else {
301 // If we're not encrypting; we don't need to actually read
302 // anything, just skip pos forward as if we'd read a
303 // block.
304 pos += sb.st_blksize;
305 }
306 tail = (tail+1) % WINDOW_SIZE;
307 }
308
309 while (head != tail) {
310 // write out head buffer
311 int block = head_block;
312 ret = ioctl(fd, FIBMAP, &block);
313 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700314 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800315 return -1;
316 }
317 add_block_to_ranges(&ranges, &range_alloc, &range_used, block);
318 if (encrypted) {
319 if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) {
320 return -1;
321 }
322 }
323 head = (head + 1) % WINDOW_SIZE;
324 ++head_block;
325 }
326
327 fprintf(mapf, "%d\n", range_used);
328 for (i = 0; i < range_used; ++i) {
329 fprintf(mapf, "%d %d\n", ranges[i*2], ranges[i*2+1]);
330 }
331
Tao Baofb4ccef2015-05-04 10:10:13 -0700332 if (fsync(mapfd) == -1) {
333 ALOGE("failed to fsync \"%s\": %s\n", map_file, strerror(errno));
334 return -1;
335 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800336 fclose(mapf);
337 close(fd);
338 if (encrypted) {
Tao Baofb4ccef2015-05-04 10:10:13 -0700339 if (fsync(wfd) == -1) {
340 ALOGE("failed to fsync \"%s\": %s\n", blk_dev, strerror(errno));
341 return -1;
342 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800343 close(wfd);
344 }
345
346 return 0;
347}
348
Doug Zongker2efc9d92014-08-18 15:55:28 -0700349void wipe_misc() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700350 ALOGI("removing old commands from misc");
Doug Zongker2efc9d92014-08-18 15:55:28 -0700351 int i;
352 for (i = 0; i < fstab->num_entries; ++i) {
353 struct fstab_rec* v = &fstab->recs[i];
354 if (!v->mount_point) continue;
355 if (strcmp(v->mount_point, "/misc") == 0) {
Michael Runge4b542392014-11-21 16:00:45 -0800356 int fd = open(v->blk_device, O_WRONLY | O_SYNC);
Doug Zongker2efc9d92014-08-18 15:55:28 -0700357 uint8_t zeroes[1088]; // sizeof(bootloader_message) from recovery
358 memset(zeroes, 0, sizeof(zeroes));
359
360 size_t written = 0;
361 size_t size = sizeof(zeroes);
362 while (written < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700363 ssize_t w = TEMP_FAILURE_RETRY(write(fd, zeroes, size-written));
364 if (w == -1) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700365 ALOGE("zero write failed: %s\n", strerror(errno));
Doug Zongker2efc9d92014-08-18 15:55:28 -0700366 return;
367 } else {
368 written += w;
369 }
370 }
Tao Baofb4ccef2015-05-04 10:10:13 -0700371 if (fsync(fd) == -1) {
372 ALOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno));
373 close(fd);
374 return;
375 }
Doug Zongker2efc9d92014-08-18 15:55:28 -0700376 close(fd);
377 }
378 }
379}
380
Doug Zongker76adfc52014-01-13 10:04:25 -0800381void reboot_to_recovery() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700382 ALOGI("rebooting to recovery");
Doug Zongker76adfc52014-01-13 10:04:25 -0800383 property_set("sys.powerctl", "reboot,recovery");
384 sleep(10);
Doug Zongkerf449db22014-08-26 09:15:08 -0700385 ALOGE("reboot didn't succeed?");
Doug Zongker76adfc52014-01-13 10:04:25 -0800386}
387
388int main(int argc, char** argv)
389{
390 const char* input_path;
391 const char* map_file;
392 int do_reboot = 1;
393
394 if (argc != 1 && argc != 3) {
395 fprintf(stderr, "usage: %s [<transform_path> <map_file>]\n", argv[0]);
396 return 2;
397 }
398
399 if (argc == 3) {
400 // when command-line args are given this binary is being used
401 // for debugging; don't reboot to recovery at the end.
402 input_path = argv[1];
403 map_file = argv[2];
404 do_reboot = 0;
405 } else {
Tao Baofb4ccef2015-05-04 10:10:13 -0700406 input_path = find_update_package();
Doug Zongker76adfc52014-01-13 10:04:25 -0800407 if (input_path == NULL) {
408 // if we're rebooting to recovery without a package (say,
409 // to wipe data), then we don't need to do anything before
410 // going to recovery.
Doug Zongkerf449db22014-08-26 09:15:08 -0700411 ALOGI("no recovery command file or no update package arg");
Doug Zongker76adfc52014-01-13 10:04:25 -0800412 reboot_to_recovery();
413 return 1;
414 }
415 map_file = CACHE_BLOCK_MAP;
416 }
417
Doug Zongkerf449db22014-08-26 09:15:08 -0700418 ALOGI("update package is %s", input_path);
419
Doug Zongker76adfc52014-01-13 10:04:25 -0800420 // Turn the name of the file we're supposed to convert into an
421 // absolute path, so we can find what filesystem it's on.
422 char path[PATH_MAX+1];
423 if (realpath(input_path, path) == NULL) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700424 ALOGE("failed to convert %s to absolute path: %s", input_path, strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800425 return 1;
426 }
427
428 int encryptable;
429 int encrypted;
Doug Zongker2efc9d92014-08-18 15:55:28 -0700430 if (read_fstab() == NULL) {
431 return 1;
432 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800433 const char* blk_dev = find_block_device(path, &encryptable, &encrypted);
434 if (blk_dev == NULL) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700435 ALOGE("failed to find block device for %s", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800436 return 1;
437 }
438
439 // If the filesystem it's on isn't encrypted, we only produce the
440 // block map, we don't rewrite the file contents (it would be
441 // pointless to do so).
Doug Zongkerf449db22014-08-26 09:15:08 -0700442 ALOGI("encryptable: %s\n", encryptable ? "yes" : "no");
443 ALOGI(" encrypted: %s\n", encrypted ? "yes" : "no");
Doug Zongker76adfc52014-01-13 10:04:25 -0800444
Doug Zongker574443d2014-09-05 08:22:12 -0700445 // Recovery supports installing packages from 3 paths: /cache,
446 // /data, and /sdcard. (On a particular device, other locations
447 // may work, but those are three we actually expect.)
448 //
449 // On /data we want to convert the file to a block map so that we
450 // can read the package without mounting the partition. On /cache
451 // and /sdcard we leave the file alone.
452 if (strncmp(path, "/data/", 6) != 0) {
453 // path does not start with "/data/"; leave it alone.
Doug Zongker76adfc52014-01-13 10:04:25 -0800454 unlink(RECOVERY_COMMAND_FILE_TMP);
Tao Baofb4ccef2015-05-04 10:10:13 -0700455 wipe_misc();
Doug Zongker76adfc52014-01-13 10:04:25 -0800456 } else {
Doug Zongkerf449db22014-08-26 09:15:08 -0700457 ALOGI("writing block map %s", map_file);
Doug Zongker76adfc52014-01-13 10:04:25 -0800458 if (produce_block_map(path, map_file, blk_dev, encrypted) != 0) {
459 return 1;
460 }
Tao Baofb4ccef2015-05-04 10:10:13 -0700461 wipe_misc();
462 rename(RECOVERY_COMMAND_FILE_TMP, RECOVERY_COMMAND_FILE);
Doug Zongker76adfc52014-01-13 10:04:25 -0800463 }
464
Doug Zongker2efc9d92014-08-18 15:55:28 -0700465 if (do_reboot) reboot_to_recovery();
Doug Zongker76adfc52014-01-13 10:04:25 -0800466 return 0;
467}