blob: 7f1a01a803fd0e774e8ee3bde724fb6f3f4dbea5 [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>
45#include <stdarg.h>
46#include <sys/types.h>
47#include <sys/stat.h>
48#include <fcntl.h>
49#include <linux/fs.h>
50#include <sys/mman.h>
51
Doug Zongkerf449db22014-08-26 09:15:08 -070052#define LOG_TAG "uncrypt"
53#include <log/log.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080054#include <cutils/properties.h>
55#include <fs_mgr.h>
56
57#define WINDOW_SIZE 5
58#define RECOVERY_COMMAND_FILE "/cache/recovery/command"
59#define RECOVERY_COMMAND_FILE_TMP "/cache/recovery/command.tmp"
60#define CACHE_BLOCK_MAP "/cache/recovery/block.map"
61
Doug Zongker2efc9d92014-08-18 15:55:28 -070062static struct fstab* fstab = NULL;
63
Doug Zongker76adfc52014-01-13 10:04:25 -080064static int write_at_offset(unsigned char* buffer, size_t size,
65 int wfd, off64_t offset)
66{
67 lseek64(wfd, offset, SEEK_SET);
68 size_t written = 0;
69 while (written < size) {
70 ssize_t wrote = write(wfd, buffer + written, size - written);
71 if (wrote < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -070072 ALOGE("error writing offset %lld: %s\n", offset, strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -080073 return -1;
74 }
75 written += wrote;
76 }
77 return 0;
78}
79
80void add_block_to_ranges(int** ranges, int* range_alloc, int* range_used, int new_block)
81{
82 // If the current block start is < 0, set the start to the new
83 // block. (This only happens for the very first block of the very
84 // first range.)
85 if ((*ranges)[*range_used*2-2] < 0) {
86 (*ranges)[*range_used*2-2] = new_block;
87 (*ranges)[*range_used*2-1] = new_block;
88 }
89
90 if (new_block == (*ranges)[*range_used*2-1]) {
91 // If the new block comes immediately after the current range,
92 // all we have to do is extend the current range.
93 ++(*ranges)[*range_used*2-1];
94 } else {
95 // We need to start a new range.
96
97 // If there isn't enough room in the array, we need to expand it.
98 if (*range_used >= *range_alloc) {
99 *range_alloc *= 2;
100 *ranges = realloc(*ranges, *range_alloc * 2 * sizeof(int));
101 }
102
103 ++*range_used;
104 (*ranges)[*range_used*2-2] = new_block;
105 (*ranges)[*range_used*2-1] = new_block+1;
106 }
107}
108
Doug Zongker2efc9d92014-08-18 15:55:28 -0700109static struct fstab* read_fstab()
Doug Zongker76adfc52014-01-13 10:04:25 -0800110{
Doug Zongker2efc9d92014-08-18 15:55:28 -0700111 fstab = NULL;
112
Doug Zongker76adfc52014-01-13 10:04:25 -0800113 // The fstab path is always "/fstab.${ro.hardware}".
114 char fstab_path[PATH_MAX+1] = "/fstab.";
115 if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700116 ALOGE("failed to get ro.hardware\n");
Doug Zongker76adfc52014-01-13 10:04:25 -0800117 return NULL;
118 }
119
Doug Zongker2efc9d92014-08-18 15:55:28 -0700120 fstab = fs_mgr_read_fstab(fstab_path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800121 if (!fstab) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700122 ALOGE("failed to read %s\n", fstab_path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800123 return NULL;
124 }
125
Doug Zongker2efc9d92014-08-18 15:55:28 -0700126 return fstab;
127}
128
129const char* find_block_device(const char* path, int* encryptable, int* encrypted)
130{
Doug Zongker76adfc52014-01-13 10:04:25 -0800131 // Look for a volume whose mount point is the prefix of path and
132 // return its block device. Set encrypted if it's currently
133 // encrypted.
134 int i;
135 for (i = 0; i < fstab->num_entries; ++i) {
136 struct fstab_rec* v = &fstab->recs[i];
137 if (!v->mount_point) continue;
138 int len = strlen(v->mount_point);
139 if (strncmp(path, v->mount_point, len) == 0 &&
140 (path[len] == '/' || path[len] == 0)) {
141 *encrypted = 0;
142 *encryptable = 0;
143 if (fs_mgr_is_encryptable(v)) {
144 *encryptable = 1;
145 char buffer[PROPERTY_VALUE_MAX+1];
146 if (property_get("ro.crypto.state", buffer, "") &&
147 strcmp(buffer, "encrypted") == 0) {
148 *encrypted = 1;
149 }
150 }
151 return v->blk_device;
152 }
153 }
154
155 return NULL;
156}
157
158char* parse_recovery_command_file()
159{
160 char* fn = NULL;
161 int count = 0;
162 char temp[1024];
163
Doug Zongker76adfc52014-01-13 10:04:25 -0800164 FILE* f = fopen(RECOVERY_COMMAND_FILE, "r");
Maxim Siniavinee7b28882014-02-13 15:48:53 -0800165 if (f == NULL) {
166 return NULL;
167 }
Sungmin Choia72512c2014-12-10 21:57:09 +0900168 int fd = open(RECOVERY_COMMAND_FILE_TMP, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR);
169 if (fd < 0) {
170 ALOGE("failed to open %s\n", RECOVERY_COMMAND_FILE_TMP);
171 return NULL;
172 }
Michael Runge4b542392014-11-21 16:00:45 -0800173 FILE* fo = fdopen(fd, "w");
Maxim Siniavinee7b28882014-02-13 15:48:53 -0800174
Doug Zongker76adfc52014-01-13 10:04:25 -0800175 while (fgets(temp, sizeof(temp), f)) {
176 printf("read: %s", temp);
Doug Zongkereaf33652014-07-31 14:59:01 -0700177 if (strncmp(temp, "--update_package=/data/", strlen("--update_package=/data/")) == 0) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800178 fn = strdup(temp + strlen("--update_package="));
179 strcpy(temp, "--update_package=@" CACHE_BLOCK_MAP "\n");
180 }
181 fputs(temp, fo);
182 }
183 fclose(f);
Michael Runge4b542392014-11-21 16:00:45 -0800184 fsync(fd);
Doug Zongker76adfc52014-01-13 10:04:25 -0800185 fclose(fo);
186
187 if (fn) {
188 char* newline = strchr(fn, '\n');
189 if (newline) *newline = 0;
190 }
191 return fn;
192}
193
194int produce_block_map(const char* path, const char* map_file, const char* blk_dev,
195 int encrypted)
196{
197 struct stat sb;
198 int ret;
199
Sungmin Choia72512c2014-12-10 21:57:09 +0900200 int mapfd = open(map_file, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR);
201 if (mapfd < 0) {
202 ALOGE("failed to open %s\n", map_file);
203 return -1;
204 }
Michael Runge4b542392014-11-21 16:00:45 -0800205 FILE* mapf = fdopen(mapfd, "w");
Doug Zongker76adfc52014-01-13 10:04:25 -0800206
207 ret = stat(path, &sb);
208 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700209 ALOGE("failed to stat %s\n", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800210 return -1;
211 }
212
Doug Zongkerf449db22014-08-26 09:15:08 -0700213 ALOGI(" block size: %ld bytes\n", (long)sb.st_blksize);
Doug Zongker76adfc52014-01-13 10:04:25 -0800214
215 int blocks = ((sb.st_size-1) / sb.st_blksize) + 1;
Doug Zongkerf449db22014-08-26 09:15:08 -0700216 ALOGI(" file size: %lld bytes, %d blocks\n", (long long)sb.st_size, blocks);
Doug Zongker76adfc52014-01-13 10:04:25 -0800217
218 int* ranges;
219 int range_alloc = 1;
220 int range_used = 1;
221 ranges = malloc(range_alloc * 2 * sizeof(int));
222 ranges[0] = -1;
223 ranges[1] = -1;
224
Mark Salyzyn2605dec2014-03-19 15:30:25 -0700225 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 -0800226
227 unsigned char* buffers[WINDOW_SIZE];
228 int i;
229 if (encrypted) {
230 for (i = 0; i < WINDOW_SIZE; ++i) {
231 buffers[i] = malloc(sb.st_blksize);
232 }
233 }
234 int head_block = 0;
235 int head = 0, tail = 0;
236 size_t pos = 0;
237
238 int fd = open(path, O_RDONLY);
239 if (fd < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700240 ALOGE("failed to open fd for reading: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800241 return -1;
242 }
243 fsync(fd);
244
245 int wfd = -1;
246 if (encrypted) {
Michael Runge4b542392014-11-21 16:00:45 -0800247 wfd = open(blk_dev, O_WRONLY | O_SYNC);
Doug Zongker76adfc52014-01-13 10:04:25 -0800248 if (wfd < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700249 ALOGE("failed to open fd for writing: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800250 return -1;
251 }
252 }
253
254 while (pos < sb.st_size) {
255 if ((tail+1) % WINDOW_SIZE == head) {
256 // write out head buffer
257 int block = head_block;
258 ret = ioctl(fd, FIBMAP, &block);
259 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700260 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800261 return -1;
262 }
263 add_block_to_ranges(&ranges, &range_alloc, &range_used, block);
264 if (encrypted) {
265 if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) {
266 return -1;
267 }
268 }
269 head = (head + 1) % WINDOW_SIZE;
270 ++head_block;
271 }
272
273 // read next block to tail
274 if (encrypted) {
275 size_t so_far = 0;
276 while (so_far < sb.st_blksize && pos < sb.st_size) {
277 ssize_t this_read = read(fd, buffers[tail] + so_far, sb.st_blksize - so_far);
278 if (this_read < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700279 ALOGE("failed to read: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800280 return -1;
281 }
282 so_far += this_read;
283 pos += this_read;
284 }
285 } else {
286 // If we're not encrypting; we don't need to actually read
287 // anything, just skip pos forward as if we'd read a
288 // block.
289 pos += sb.st_blksize;
290 }
291 tail = (tail+1) % WINDOW_SIZE;
292 }
293
294 while (head != tail) {
295 // write out head buffer
296 int block = head_block;
297 ret = ioctl(fd, FIBMAP, &block);
298 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700299 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800300 return -1;
301 }
302 add_block_to_ranges(&ranges, &range_alloc, &range_used, block);
303 if (encrypted) {
304 if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) {
305 return -1;
306 }
307 }
308 head = (head + 1) % WINDOW_SIZE;
309 ++head_block;
310 }
311
312 fprintf(mapf, "%d\n", range_used);
313 for (i = 0; i < range_used; ++i) {
314 fprintf(mapf, "%d %d\n", ranges[i*2], ranges[i*2+1]);
315 }
316
Michael Runge4b542392014-11-21 16:00:45 -0800317 fsync(mapfd);
Doug Zongker76adfc52014-01-13 10:04:25 -0800318 fclose(mapf);
319 close(fd);
320 if (encrypted) {
Michael Runge4b542392014-11-21 16:00:45 -0800321 fsync(wfd);
Doug Zongker76adfc52014-01-13 10:04:25 -0800322 close(wfd);
323 }
324
325 return 0;
326}
327
Doug Zongker2efc9d92014-08-18 15:55:28 -0700328void wipe_misc() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700329 ALOGI("removing old commands from misc");
Doug Zongker2efc9d92014-08-18 15:55:28 -0700330 int i;
331 for (i = 0; i < fstab->num_entries; ++i) {
332 struct fstab_rec* v = &fstab->recs[i];
333 if (!v->mount_point) continue;
334 if (strcmp(v->mount_point, "/misc") == 0) {
Michael Runge4b542392014-11-21 16:00:45 -0800335 int fd = open(v->blk_device, O_WRONLY | O_SYNC);
Doug Zongker2efc9d92014-08-18 15:55:28 -0700336 uint8_t zeroes[1088]; // sizeof(bootloader_message) from recovery
337 memset(zeroes, 0, sizeof(zeroes));
338
339 size_t written = 0;
340 size_t size = sizeof(zeroes);
341 while (written < size) {
342 ssize_t w = write(fd, zeroes, size-written);
343 if (w < 0 && errno != EINTR) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700344 ALOGE("zero write failed: %s\n", strerror(errno));
Doug Zongker2efc9d92014-08-18 15:55:28 -0700345 return;
346 } else {
347 written += w;
348 }
349 }
Michael Runge4b542392014-11-21 16:00:45 -0800350 fsync(fd);
Doug Zongker2efc9d92014-08-18 15:55:28 -0700351 close(fd);
352 }
353 }
354}
355
Doug Zongker76adfc52014-01-13 10:04:25 -0800356void reboot_to_recovery() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700357 ALOGI("rebooting to recovery");
Doug Zongker76adfc52014-01-13 10:04:25 -0800358 property_set("sys.powerctl", "reboot,recovery");
359 sleep(10);
Doug Zongkerf449db22014-08-26 09:15:08 -0700360 ALOGE("reboot didn't succeed?");
Doug Zongker76adfc52014-01-13 10:04:25 -0800361}
362
363int main(int argc, char** argv)
364{
365 const char* input_path;
366 const char* map_file;
367 int do_reboot = 1;
368
369 if (argc != 1 && argc != 3) {
370 fprintf(stderr, "usage: %s [<transform_path> <map_file>]\n", argv[0]);
371 return 2;
372 }
373
374 if (argc == 3) {
375 // when command-line args are given this binary is being used
376 // for debugging; don't reboot to recovery at the end.
377 input_path = argv[1];
378 map_file = argv[2];
379 do_reboot = 0;
380 } else {
381 input_path = parse_recovery_command_file();
382 if (input_path == NULL) {
383 // if we're rebooting to recovery without a package (say,
384 // to wipe data), then we don't need to do anything before
385 // going to recovery.
Doug Zongkerf449db22014-08-26 09:15:08 -0700386 ALOGI("no recovery command file or no update package arg");
Doug Zongker76adfc52014-01-13 10:04:25 -0800387 reboot_to_recovery();
388 return 1;
389 }
390 map_file = CACHE_BLOCK_MAP;
391 }
392
Doug Zongkerf449db22014-08-26 09:15:08 -0700393 ALOGI("update package is %s", input_path);
394
Doug Zongker76adfc52014-01-13 10:04:25 -0800395 // Turn the name of the file we're supposed to convert into an
396 // absolute path, so we can find what filesystem it's on.
397 char path[PATH_MAX+1];
398 if (realpath(input_path, path) == NULL) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700399 ALOGE("failed to convert %s to absolute path: %s", input_path, strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800400 return 1;
401 }
402
403 int encryptable;
404 int encrypted;
Doug Zongker2efc9d92014-08-18 15:55:28 -0700405 if (read_fstab() == NULL) {
406 return 1;
407 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800408 const char* blk_dev = find_block_device(path, &encryptable, &encrypted);
409 if (blk_dev == NULL) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700410 ALOGE("failed to find block device for %s", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800411 return 1;
412 }
413
414 // If the filesystem it's on isn't encrypted, we only produce the
415 // block map, we don't rewrite the file contents (it would be
416 // pointless to do so).
Doug Zongkerf449db22014-08-26 09:15:08 -0700417 ALOGI("encryptable: %s\n", encryptable ? "yes" : "no");
418 ALOGI(" encrypted: %s\n", encrypted ? "yes" : "no");
Doug Zongker76adfc52014-01-13 10:04:25 -0800419
Doug Zongker574443d2014-09-05 08:22:12 -0700420 // Recovery supports installing packages from 3 paths: /cache,
421 // /data, and /sdcard. (On a particular device, other locations
422 // may work, but those are three we actually expect.)
423 //
424 // On /data we want to convert the file to a block map so that we
425 // can read the package without mounting the partition. On /cache
426 // and /sdcard we leave the file alone.
427 if (strncmp(path, "/data/", 6) != 0) {
428 // path does not start with "/data/"; leave it alone.
Doug Zongker76adfc52014-01-13 10:04:25 -0800429 unlink(RECOVERY_COMMAND_FILE_TMP);
430 } else {
Doug Zongkerf449db22014-08-26 09:15:08 -0700431 ALOGI("writing block map %s", map_file);
Doug Zongker76adfc52014-01-13 10:04:25 -0800432 if (produce_block_map(path, map_file, blk_dev, encrypted) != 0) {
433 return 1;
434 }
435 }
436
Doug Zongker2efc9d92014-08-18 15:55:28 -0700437 wipe_misc();
Doug Zongker76adfc52014-01-13 10:04:25 -0800438 rename(RECOVERY_COMMAND_FILE_TMP, RECOVERY_COMMAND_FILE);
Doug Zongker2efc9d92014-08-18 15:55:28 -0700439 if (do_reboot) reboot_to_recovery();
Doug Zongker76adfc52014-01-13 10:04:25 -0800440 return 0;
441}