From 76adfc5309936a07218ce53b5ab284d5746fa84c Mon Sep 17 00:00:00 2001 From: Doug Zongker Date: Mon, 13 Jan 2014 10:04:25 -0800 Subject: program to store unencrypted files in an encrypted filesystem uncrypt can read a file on an encrypted filesystem and rewrite it to the same blocks on the underlying (unencrypted) block device. This destroys the contents of the file as far as the encrypted filesystem is concerned, but allows the data to be read without the encryption key if you know which blocks of the raw device to access. uncrypt produces a "block map" file which lists the blocks that contain the file. For unencrypted filesystem, uncrypt will produce the block map without touching the data. Bug: 12188746 Change-Id: Ib7259b9e14dac8af406796b429d58378a00c7c63 --- uncrypt/uncrypt.c | 377 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 377 insertions(+) create mode 100644 uncrypt/uncrypt.c (limited to 'uncrypt/uncrypt.c') diff --git a/uncrypt/uncrypt.c b/uncrypt/uncrypt.c new file mode 100644 index 000000000..1f0f59dba --- /dev/null +++ b/uncrypt/uncrypt.c @@ -0,0 +1,377 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// This program takes a file on an ext4 filesystem and produces a list +// of the blocks that file occupies, which enables the file contents +// to be read directly from the block device without mounting the +// filesystem. +// +// If the filesystem is using an encrypted block device, it will also +// read the file and rewrite it to the same blocks of the underlying +// (unencrypted) block device, so the file contents can be read +// without the need for the decryption key. +// +// The output of this program is a "block map" which looks like this: +// +// /dev/block/platform/msm_sdcc.1/by-name/userdata # block device +// 49652 4096 # file size in bytes, block size +// 3 # count of block ranges +// 1000 1008 # block range 0 +// 2100 2102 # ... block range 1 +// 30 33 # ... block range 2 +// +// Each block range represents a half-open interval; the line "30 33" +// reprents the blocks [30, 31, 32]. +// +// Recovery can take this block map file and retrieve the underlying +// file data to use as an update package. + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define WINDOW_SIZE 5 +#define RECOVERY_COMMAND_FILE "/cache/recovery/command" +#define RECOVERY_COMMAND_FILE_TMP "/cache/recovery/command.tmp" +#define CACHE_BLOCK_MAP "/cache/recovery/block.map" + +static int write_at_offset(unsigned char* buffer, size_t size, + int wfd, off64_t offset) +{ + lseek64(wfd, offset, SEEK_SET); + size_t written = 0; + while (written < size) { + ssize_t wrote = write(wfd, buffer + written, size - written); + if (wrote < 0) { + fprintf(stderr, "error writing offset %lld: %s\n", offset, strerror(errno)); + return -1; + } + written += wrote; + } + return 0; +} + +void add_block_to_ranges(int** ranges, int* range_alloc, int* range_used, int new_block) +{ + // If the current block start is < 0, set the start to the new + // block. (This only happens for the very first block of the very + // first range.) + if ((*ranges)[*range_used*2-2] < 0) { + (*ranges)[*range_used*2-2] = new_block; + (*ranges)[*range_used*2-1] = new_block; + } + + if (new_block == (*ranges)[*range_used*2-1]) { + // If the new block comes immediately after the current range, + // all we have to do is extend the current range. + ++(*ranges)[*range_used*2-1]; + } else { + // We need to start a new range. + + // If there isn't enough room in the array, we need to expand it. + if (*range_used >= *range_alloc) { + *range_alloc *= 2; + *ranges = realloc(*ranges, *range_alloc * 2 * sizeof(int)); + } + + ++*range_used; + (*ranges)[*range_used*2-2] = new_block; + (*ranges)[*range_used*2-1] = new_block+1; + } +} + +const char* find_block_device(const char* path, int* encryptable, int* encrypted) +{ + // The fstab path is always "/fstab.${ro.hardware}". + char fstab_path[PATH_MAX+1] = "/fstab."; + if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) { + fprintf(stderr, "failed to get ro.hardware\n"); + return NULL; + } + + struct fstab* fstab = fs_mgr_read_fstab(fstab_path); + if (!fstab) { + fprintf(stderr, "failed to read %s\n", fstab_path); + return NULL; + } + + // Look for a volume whose mount point is the prefix of path and + // return its block device. Set encrypted if it's currently + // encrypted. + int i; + for (i = 0; i < fstab->num_entries; ++i) { + struct fstab_rec* v = &fstab->recs[i]; + if (!v->mount_point) continue; + int len = strlen(v->mount_point); + if (strncmp(path, v->mount_point, len) == 0 && + (path[len] == '/' || path[len] == 0)) { + *encrypted = 0; + *encryptable = 0; + if (fs_mgr_is_encryptable(v)) { + *encryptable = 1; + char buffer[PROPERTY_VALUE_MAX+1]; + if (property_get("ro.crypto.state", buffer, "") && + strcmp(buffer, "encrypted") == 0) { + *encrypted = 1; + } + } + return v->blk_device; + } + } + + return NULL; +} + +char* parse_recovery_command_file() +{ + char* fn = NULL; + int count = 0; + char temp[1024]; + + FILE* fo = fopen(RECOVERY_COMMAND_FILE_TMP, "w"); + + FILE* f = fopen(RECOVERY_COMMAND_FILE, "r"); + while (fgets(temp, sizeof(temp), f)) { + printf("read: %s", temp); + if (strncmp(temp, "--update_package=", strlen("--update_package=")) == 0) { + fn = strdup(temp + strlen("--update_package=")); + strcpy(temp, "--update_package=@" CACHE_BLOCK_MAP "\n"); + } + fputs(temp, fo); + } + fclose(f); + fclose(fo); + + if (fn) { + char* newline = strchr(fn, '\n'); + if (newline) *newline = 0; + } + return fn; +} + +int produce_block_map(const char* path, const char* map_file, const char* blk_dev, + int encrypted) +{ + struct stat sb; + int ret; + + FILE* mapf = fopen(map_file, "w"); + + ret = stat(path, &sb); + if (ret != 0) { + fprintf(stderr, "failed to stat %s\n", path); + return -1; + } + + printf(" block size: %ld bytes\n", sb.st_blksize); + + int blocks = ((sb.st_size-1) / sb.st_blksize) + 1; + printf(" file size: %lld bytes, %d blocks\n", sb.st_size, blocks); + + int* ranges; + int range_alloc = 1; + int range_used = 1; + ranges = malloc(range_alloc * 2 * sizeof(int)); + ranges[0] = -1; + ranges[1] = -1; + + fprintf(mapf, "%s\n%lld %lu\n", blk_dev, sb.st_size, sb.st_blksize); + + unsigned char* buffers[WINDOW_SIZE]; + int i; + if (encrypted) { + for (i = 0; i < WINDOW_SIZE; ++i) { + buffers[i] = malloc(sb.st_blksize); + } + } + int head_block = 0; + int head = 0, tail = 0; + size_t pos = 0; + + int fd = open(path, O_RDONLY); + if (fd < 0) { + fprintf(stderr, "failed to open fd for reading: %s\n", strerror(errno)); + return -1; + } + fsync(fd); + + int wfd = -1; + if (encrypted) { + wfd = open(blk_dev, O_WRONLY); + if (wfd < 0) { + fprintf(stderr, "failed to open fd for writing: %s\n", strerror(errno)); + return -1; + } + } + + while (pos < sb.st_size) { + if ((tail+1) % WINDOW_SIZE == head) { + // write out head buffer + int block = head_block; + ret = ioctl(fd, FIBMAP, &block); + if (ret != 0) { + fprintf(stderr, "failed to find block %d\n", head_block); + return -1; + } + add_block_to_ranges(&ranges, &range_alloc, &range_used, block); + if (encrypted) { + if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) { + return -1; + } + } + head = (head + 1) % WINDOW_SIZE; + ++head_block; + } + + // read next block to tail + if (encrypted) { + size_t so_far = 0; + while (so_far < sb.st_blksize && pos < sb.st_size) { + ssize_t this_read = read(fd, buffers[tail] + so_far, sb.st_blksize - so_far); + if (this_read < 0) { + fprintf(stderr, "failed to read: %s\n", strerror(errno)); + return -1; + } + so_far += this_read; + pos += this_read; + } + } else { + // If we're not encrypting; we don't need to actually read + // anything, just skip pos forward as if we'd read a + // block. + pos += sb.st_blksize; + } + tail = (tail+1) % WINDOW_SIZE; + } + + while (head != tail) { + // write out head buffer + int block = head_block; + ret = ioctl(fd, FIBMAP, &block); + if (ret != 0) { + fprintf(stderr, "failed to find block %d\n", head_block); + return -1; + } + add_block_to_ranges(&ranges, &range_alloc, &range_used, block); + if (encrypted) { + if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) { + return -1; + } + } + head = (head + 1) % WINDOW_SIZE; + ++head_block; + } + + fprintf(mapf, "%d\n", range_used); + for (i = 0; i < range_used; ++i) { + fprintf(mapf, "%d %d\n", ranges[i*2], ranges[i*2+1]); + } + + fclose(mapf); + close(fd); + if (encrypted) { + close(wfd); + } + + return 0; +} + +void reboot_to_recovery() { + property_set("sys.powerctl", "reboot,recovery"); + sleep(10); +} + +int main(int argc, char** argv) +{ + const char* input_path; + const char* map_file; + int do_reboot = 1; + + if (argc != 1 && argc != 3) { + fprintf(stderr, "usage: %s [ ]\n", argv[0]); + return 2; + } + + if (argc == 3) { + // when command-line args are given this binary is being used + // for debugging; don't reboot to recovery at the end. + input_path = argv[1]; + map_file = argv[2]; + do_reboot = 0; + } else { + input_path = parse_recovery_command_file(); + if (input_path == NULL) { + // if we're rebooting to recovery without a package (say, + // to wipe data), then we don't need to do anything before + // going to recovery. + fprintf(stderr, "no recovery command file or no update package arg"); + reboot_to_recovery(); + return 1; + } + map_file = CACHE_BLOCK_MAP; + } + + // Turn the name of the file we're supposed to convert into an + // absolute path, so we can find what filesystem it's on. + char path[PATH_MAX+1]; + if (realpath(input_path, path) == NULL) { + fprintf(stderr, "failed to convert %s to absolute path: %s\n", input_path, strerror(errno)); + return 1; + } + + int encryptable; + int encrypted; + const char* blk_dev = find_block_device(path, &encryptable, &encrypted); + if (blk_dev == NULL) { + fprintf(stderr, "failed to find block device for %s\n", path); + return 1; + } + + // If the filesystem it's on isn't encrypted, we only produce the + // block map, we don't rewrite the file contents (it would be + // pointless to do so). + printf("encryptable: %s\n", encryptable ? "yes" : "no"); + printf(" encrypted: %s\n", encrypted ? "yes" : "no"); + + if (!encryptable) { + // If the file is on a filesystem that doesn't support + // encryption (eg, /cache), then leave it alone. + // + // TODO: change this to be !encrypted -- if the file is on + // /data but /data isn't encrypted, we don't need to use the + // block map mechanism. We do for now so as to get more + // testing of it (since most dogfood devices aren't + // encrypted). + + unlink(RECOVERY_COMMAND_FILE_TMP); + } else { + if (produce_block_map(path, map_file, blk_dev, encrypted) != 0) { + return 1; + } + } + + rename(RECOVERY_COMMAND_FILE_TMP, RECOVERY_COMMAND_FILE); + reboot_to_recovery(); + return 0; +} -- cgit v1.2.3 From e7b288824589b2828d83e1d47f6f12b0bd0fb353 Mon Sep 17 00:00:00 2001 From: Maxim Siniavine Date: Thu, 13 Feb 2014 15:48:53 -0800 Subject: Fix a crash when going into recovery mode. When going into recovery mode withoug recovery command file present, uncrypt crashes and the device gets stuck and eventually shuts down. Check that the command file is present before trying to read from it. Change-Id: If0192d597032be0067738e437188d92993ce56f7 --- uncrypt/uncrypt.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'uncrypt/uncrypt.c') diff --git a/uncrypt/uncrypt.c b/uncrypt/uncrypt.c index 1f0f59dba..7c2d99477 100644 --- a/uncrypt/uncrypt.c +++ b/uncrypt/uncrypt.c @@ -149,9 +149,14 @@ char* parse_recovery_command_file() int count = 0; char temp[1024]; - FILE* fo = fopen(RECOVERY_COMMAND_FILE_TMP, "w"); + FILE* f = fopen(RECOVERY_COMMAND_FILE, "r"); + if (f == NULL) { + return NULL; + } + FILE* fo = fopen(RECOVERY_COMMAND_FILE_TMP, "w"); + while (fgets(temp, sizeof(temp), f)) { printf("read: %s", temp); if (strncmp(temp, "--update_package=", strlen("--update_package=")) == 0) { -- cgit v1.2.3 From 2605dec597f7ebabf31b7e9430f19ab888b2919a Mon Sep 17 00:00:00 2001 From: Mark Salyzyn Date: Wed, 19 Mar 2014 15:30:25 -0700 Subject: recovery: 64 bit build issues Change-Id: Ie88c49dea13cce5f4eb428e97f5a0956f2656a30 --- uncrypt/uncrypt.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'uncrypt/uncrypt.c') diff --git a/uncrypt/uncrypt.c b/uncrypt/uncrypt.c index 7c2d99477..24d1ffc2a 100644 --- a/uncrypt/uncrypt.c +++ b/uncrypt/uncrypt.c @@ -189,10 +189,10 @@ int produce_block_map(const char* path, const char* map_file, const char* blk_de return -1; } - printf(" block size: %ld bytes\n", sb.st_blksize); + printf(" block size: %ld bytes\n", (long)sb.st_blksize); int blocks = ((sb.st_size-1) / sb.st_blksize) + 1; - printf(" file size: %lld bytes, %d blocks\n", sb.st_size, blocks); + printf(" file size: %lld bytes, %d blocks\n", (long long)sb.st_size, blocks); int* ranges; int range_alloc = 1; @@ -201,7 +201,7 @@ int produce_block_map(const char* path, const char* map_file, const char* blk_de ranges[0] = -1; ranges[1] = -1; - fprintf(mapf, "%s\n%lld %lu\n", blk_dev, sb.st_size, sb.st_blksize); + fprintf(mapf, "%s\n%lld %lu\n", blk_dev, (long long)sb.st_size, (unsigned long)sb.st_blksize); unsigned char* buffers[WINDOW_SIZE]; int i; -- cgit v1.2.3 From eaf33654c1817bd665831a13c5bd0c04daabee02 Mon Sep 17 00:00:00 2001 From: Doug Zongker Date: Thu, 31 Jul 2014 14:59:01 -0700 Subject: only do uncryption on packages in /data If recovery is invoked with a package somewhere other than /data, leave it alone. Change-Id: Ief358b53df467ae24a65e30e7a631da59bf13683 --- uncrypt/uncrypt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'uncrypt/uncrypt.c') diff --git a/uncrypt/uncrypt.c b/uncrypt/uncrypt.c index 24d1ffc2a..bce53dbb9 100644 --- a/uncrypt/uncrypt.c +++ b/uncrypt/uncrypt.c @@ -159,7 +159,7 @@ char* parse_recovery_command_file() while (fgets(temp, sizeof(temp), f)) { printf("read: %s", temp); - if (strncmp(temp, "--update_package=", strlen("--update_package=")) == 0) { + if (strncmp(temp, "--update_package=/data/", strlen("--update_package=/data/")) == 0) { fn = strdup(temp + strlen("--update_package=")); strcpy(temp, "--update_package=@" CACHE_BLOCK_MAP "\n"); } -- cgit v1.2.3 From 2efc9d994ce59f9ebfc2290c2adc5d760e8939c2 Mon Sep 17 00:00:00 2001 From: Doug Zongker Date: Mon, 18 Aug 2014 15:55:28 -0700 Subject: clear BCB in misc partition before rebooting Something is leaving behind wipe commands in the BCB area of the /misc partition. We don't know what is doing that. It should always be safe to zero out that area from uncrypt, though (because if uncrypt is running then it's got the command we want in the recovery command file rather than the BCB). Bug: 16715412 Change-Id: Iad01124287f13b80ff71d6371db6371f43c43211 --- uncrypt/uncrypt.c | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) (limited to 'uncrypt/uncrypt.c') diff --git a/uncrypt/uncrypt.c b/uncrypt/uncrypt.c index bce53dbb9..77bfdc207 100644 --- a/uncrypt/uncrypt.c +++ b/uncrypt/uncrypt.c @@ -56,6 +56,8 @@ #define RECOVERY_COMMAND_FILE_TMP "/cache/recovery/command.tmp" #define CACHE_BLOCK_MAP "/cache/recovery/block.map" +static struct fstab* fstab = NULL; + static int write_at_offset(unsigned char* buffer, size_t size, int wfd, off64_t offset) { @@ -101,8 +103,10 @@ void add_block_to_ranges(int** ranges, int* range_alloc, int* range_used, int ne } } -const char* find_block_device(const char* path, int* encryptable, int* encrypted) +static struct fstab* read_fstab() { + fstab = NULL; + // The fstab path is always "/fstab.${ro.hardware}". char fstab_path[PATH_MAX+1] = "/fstab."; if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) { @@ -110,12 +114,17 @@ const char* find_block_device(const char* path, int* encryptable, int* encrypted return NULL; } - struct fstab* fstab = fs_mgr_read_fstab(fstab_path); + fstab = fs_mgr_read_fstab(fstab_path); if (!fstab) { fprintf(stderr, "failed to read %s\n", fstab_path); return NULL; } + return fstab; +} + +const char* find_block_device(const char* path, int* encryptable, int* encrypted) +{ // Look for a volume whose mount point is the prefix of path and // return its block device. Set encrypted if it's currently // encrypted. @@ -302,6 +311,33 @@ int produce_block_map(const char* path, const char* map_file, const char* blk_de return 0; } +void wipe_misc() { + int i; + for (i = 0; i < fstab->num_entries; ++i) { + struct fstab_rec* v = &fstab->recs[i]; + if (!v->mount_point) continue; + if (strcmp(v->mount_point, "/misc") == 0) { + int fd = open(v->blk_device, O_RDWR); + uint8_t zeroes[1088]; // sizeof(bootloader_message) from recovery + memset(zeroes, 0, sizeof(zeroes)); + + size_t written = 0; + size_t size = sizeof(zeroes); + while (written < size) { + ssize_t w = write(fd, zeroes, size-written); + if (w < 0 && errno != EINTR) { + fprintf(stderr, "zero write failed: %s\n", strerror(errno)); + return; + } else { + written += w; + } + } + + close(fd); + } + } +} + void reboot_to_recovery() { property_set("sys.powerctl", "reboot,recovery"); sleep(10); @@ -347,6 +383,9 @@ int main(int argc, char** argv) int encryptable; int encrypted; + if (read_fstab() == NULL) { + return 1; + } const char* blk_dev = find_block_device(path, &encryptable, &encrypted); if (blk_dev == NULL) { fprintf(stderr, "failed to find block device for %s\n", path); @@ -376,7 +415,8 @@ int main(int argc, char** argv) } } + wipe_misc(); rename(RECOVERY_COMMAND_FILE_TMP, RECOVERY_COMMAND_FILE); - reboot_to_recovery(); + if (do_reboot) reboot_to_recovery(); return 0; } -- cgit v1.2.3 From f449db2f30235a0c2fef4bc7bc41776e271a60a0 Mon Sep 17 00:00:00 2001 From: Doug Zongker Date: Tue, 26 Aug 2014 09:15:08 -0700 Subject: open misc device in write-only mode Opening the misc block device in read-write mode runs afoul of SELinux, which keeps the wipe code from working. Fix. Also change various things to log to logcat so we can see them happening, for future debugging. Bug: 16715412 Change-Id: Ia14066f0a371cd605fcb544547b58a41acca70b9 --- uncrypt/uncrypt.c | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) (limited to 'uncrypt/uncrypt.c') diff --git a/uncrypt/uncrypt.c b/uncrypt/uncrypt.c index 77bfdc207..07e5ae665 100644 --- a/uncrypt/uncrypt.c +++ b/uncrypt/uncrypt.c @@ -48,6 +48,8 @@ #include #include +#define LOG_TAG "uncrypt" +#include #include #include @@ -66,7 +68,7 @@ static int write_at_offset(unsigned char* buffer, size_t size, while (written < size) { ssize_t wrote = write(wfd, buffer + written, size - written); if (wrote < 0) { - fprintf(stderr, "error writing offset %lld: %s\n", offset, strerror(errno)); + ALOGE("error writing offset %lld: %s\n", offset, strerror(errno)); return -1; } written += wrote; @@ -110,13 +112,13 @@ static struct fstab* read_fstab() // The fstab path is always "/fstab.${ro.hardware}". char fstab_path[PATH_MAX+1] = "/fstab."; if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) { - fprintf(stderr, "failed to get ro.hardware\n"); + ALOGE("failed to get ro.hardware\n"); return NULL; } fstab = fs_mgr_read_fstab(fstab_path); if (!fstab) { - fprintf(stderr, "failed to read %s\n", fstab_path); + ALOGE("failed to read %s\n", fstab_path); return NULL; } @@ -194,14 +196,14 @@ int produce_block_map(const char* path, const char* map_file, const char* blk_de ret = stat(path, &sb); if (ret != 0) { - fprintf(stderr, "failed to stat %s\n", path); + ALOGE("failed to stat %s\n", path); return -1; } - printf(" block size: %ld bytes\n", (long)sb.st_blksize); + ALOGI(" block size: %ld bytes\n", (long)sb.st_blksize); int blocks = ((sb.st_size-1) / sb.st_blksize) + 1; - printf(" file size: %lld bytes, %d blocks\n", (long long)sb.st_size, blocks); + ALOGI(" file size: %lld bytes, %d blocks\n", (long long)sb.st_size, blocks); int* ranges; int range_alloc = 1; @@ -225,7 +227,7 @@ int produce_block_map(const char* path, const char* map_file, const char* blk_de int fd = open(path, O_RDONLY); if (fd < 0) { - fprintf(stderr, "failed to open fd for reading: %s\n", strerror(errno)); + ALOGE("failed to open fd for reading: %s\n", strerror(errno)); return -1; } fsync(fd); @@ -234,7 +236,7 @@ int produce_block_map(const char* path, const char* map_file, const char* blk_de if (encrypted) { wfd = open(blk_dev, O_WRONLY); if (wfd < 0) { - fprintf(stderr, "failed to open fd for writing: %s\n", strerror(errno)); + ALOGE("failed to open fd for writing: %s\n", strerror(errno)); return -1; } } @@ -245,7 +247,7 @@ int produce_block_map(const char* path, const char* map_file, const char* blk_de int block = head_block; ret = ioctl(fd, FIBMAP, &block); if (ret != 0) { - fprintf(stderr, "failed to find block %d\n", head_block); + ALOGE("failed to find block %d\n", head_block); return -1; } add_block_to_ranges(&ranges, &range_alloc, &range_used, block); @@ -264,7 +266,7 @@ int produce_block_map(const char* path, const char* map_file, const char* blk_de while (so_far < sb.st_blksize && pos < sb.st_size) { ssize_t this_read = read(fd, buffers[tail] + so_far, sb.st_blksize - so_far); if (this_read < 0) { - fprintf(stderr, "failed to read: %s\n", strerror(errno)); + ALOGE("failed to read: %s\n", strerror(errno)); return -1; } so_far += this_read; @@ -284,7 +286,7 @@ int produce_block_map(const char* path, const char* map_file, const char* blk_de int block = head_block; ret = ioctl(fd, FIBMAP, &block); if (ret != 0) { - fprintf(stderr, "failed to find block %d\n", head_block); + ALOGE("failed to find block %d\n", head_block); return -1; } add_block_to_ranges(&ranges, &range_alloc, &range_used, block); @@ -312,12 +314,13 @@ int produce_block_map(const char* path, const char* map_file, const char* blk_de } void wipe_misc() { + ALOGI("removing old commands from misc"); int i; for (i = 0; i < fstab->num_entries; ++i) { struct fstab_rec* v = &fstab->recs[i]; if (!v->mount_point) continue; if (strcmp(v->mount_point, "/misc") == 0) { - int fd = open(v->blk_device, O_RDWR); + int fd = open(v->blk_device, O_WRONLY); uint8_t zeroes[1088]; // sizeof(bootloader_message) from recovery memset(zeroes, 0, sizeof(zeroes)); @@ -326,7 +329,7 @@ void wipe_misc() { while (written < size) { ssize_t w = write(fd, zeroes, size-written); if (w < 0 && errno != EINTR) { - fprintf(stderr, "zero write failed: %s\n", strerror(errno)); + ALOGE("zero write failed: %s\n", strerror(errno)); return; } else { written += w; @@ -339,8 +342,10 @@ void wipe_misc() { } void reboot_to_recovery() { + ALOGI("rebooting to recovery"); property_set("sys.powerctl", "reboot,recovery"); sleep(10); + ALOGE("reboot didn't succeed?"); } int main(int argc, char** argv) @@ -366,18 +371,20 @@ int main(int argc, char** argv) // if we're rebooting to recovery without a package (say, // to wipe data), then we don't need to do anything before // going to recovery. - fprintf(stderr, "no recovery command file or no update package arg"); + ALOGI("no recovery command file or no update package arg"); reboot_to_recovery(); return 1; } map_file = CACHE_BLOCK_MAP; } + ALOGI("update package is %s", input_path); + // Turn the name of the file we're supposed to convert into an // absolute path, so we can find what filesystem it's on. char path[PATH_MAX+1]; if (realpath(input_path, path) == NULL) { - fprintf(stderr, "failed to convert %s to absolute path: %s\n", input_path, strerror(errno)); + ALOGE("failed to convert %s to absolute path: %s", input_path, strerror(errno)); return 1; } @@ -388,15 +395,15 @@ int main(int argc, char** argv) } const char* blk_dev = find_block_device(path, &encryptable, &encrypted); if (blk_dev == NULL) { - fprintf(stderr, "failed to find block device for %s\n", path); + ALOGE("failed to find block device for %s", path); return 1; } // If the filesystem it's on isn't encrypted, we only produce the // block map, we don't rewrite the file contents (it would be // pointless to do so). - printf("encryptable: %s\n", encryptable ? "yes" : "no"); - printf(" encrypted: %s\n", encrypted ? "yes" : "no"); + ALOGI("encryptable: %s\n", encryptable ? "yes" : "no"); + ALOGI(" encrypted: %s\n", encrypted ? "yes" : "no"); if (!encryptable) { // If the file is on a filesystem that doesn't support @@ -410,6 +417,7 @@ int main(int argc, char** argv) unlink(RECOVERY_COMMAND_FILE_TMP); } else { + ALOGI("writing block map %s", map_file); if (produce_block_map(path, map_file, blk_dev, encrypted) != 0) { return 1; } -- cgit v1.2.3 From 574443d8956802f35347cac7fae7eb16240e3c16 Mon Sep 17 00:00:00 2001 From: Doug Zongker Date: Fri, 5 Sep 2014 08:22:12 -0700 Subject: create block map for all update packages on /data Always create the block map for packages on /data; don't only look at the encryptable/encrypted flags. Bug: 17395453 Change-Id: Iaa7643a32898328277841e324305b9419a9e071c --- uncrypt/uncrypt.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'uncrypt/uncrypt.c') diff --git a/uncrypt/uncrypt.c b/uncrypt/uncrypt.c index 07e5ae665..189fa57e1 100644 --- a/uncrypt/uncrypt.c +++ b/uncrypt/uncrypt.c @@ -160,8 +160,6 @@ char* parse_recovery_command_file() int count = 0; char temp[1024]; - - FILE* f = fopen(RECOVERY_COMMAND_FILE, "r"); if (f == NULL) { return NULL; @@ -405,16 +403,15 @@ int main(int argc, char** argv) ALOGI("encryptable: %s\n", encryptable ? "yes" : "no"); ALOGI(" encrypted: %s\n", encrypted ? "yes" : "no"); - if (!encryptable) { - // If the file is on a filesystem that doesn't support - // encryption (eg, /cache), then leave it alone. - // - // TODO: change this to be !encrypted -- if the file is on - // /data but /data isn't encrypted, we don't need to use the - // block map mechanism. We do for now so as to get more - // testing of it (since most dogfood devices aren't - // encrypted). - + // Recovery supports installing packages from 3 paths: /cache, + // /data, and /sdcard. (On a particular device, other locations + // may work, but those are three we actually expect.) + // + // On /data we want to convert the file to a block map so that we + // can read the package without mounting the partition. On /cache + // and /sdcard we leave the file alone. + if (strncmp(path, "/data/", 6) != 0) { + // path does not start with "/data/"; leave it alone. unlink(RECOVERY_COMMAND_FILE_TMP); } else { ALOGI("writing block map %s", map_file); -- cgit v1.2.3