blob: e619237be0c018c266f173e0bd81f187a194d661 [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 }
168 FILE* fo = fopen(RECOVERY_COMMAND_FILE_TMP, "w");
169
Doug Zongker76adfc52014-01-13 10:04:25 -0800170 while (fgets(temp, sizeof(temp), f)) {
171 printf("read: %s", temp);
Doug Zongkereaf33652014-07-31 14:59:01 -0700172 if (strncmp(temp, "--update_package=/data/", strlen("--update_package=/data/")) == 0) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800173 fn = strdup(temp + strlen("--update_package="));
174 strcpy(temp, "--update_package=@" CACHE_BLOCK_MAP "\n");
175 }
176 fputs(temp, fo);
177 }
178 fclose(f);
179 fclose(fo);
180
181 if (fn) {
182 char* newline = strchr(fn, '\n');
183 if (newline) *newline = 0;
184 }
185 return fn;
186}
187
188int produce_block_map(const char* path, const char* map_file, const char* blk_dev,
189 int encrypted)
190{
191 struct stat sb;
192 int ret;
193
194 FILE* mapf = fopen(map_file, "w");
195
196 ret = stat(path, &sb);
197 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700198 ALOGE("failed to stat %s\n", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800199 return -1;
200 }
201
Doug Zongkerf449db22014-08-26 09:15:08 -0700202 ALOGI(" block size: %ld bytes\n", (long)sb.st_blksize);
Doug Zongker76adfc52014-01-13 10:04:25 -0800203
204 int blocks = ((sb.st_size-1) / sb.st_blksize) + 1;
Doug Zongkerf449db22014-08-26 09:15:08 -0700205 ALOGI(" file size: %lld bytes, %d blocks\n", (long long)sb.st_size, blocks);
Doug Zongker76adfc52014-01-13 10:04:25 -0800206
207 int* ranges;
208 int range_alloc = 1;
209 int range_used = 1;
210 ranges = malloc(range_alloc * 2 * sizeof(int));
211 ranges[0] = -1;
212 ranges[1] = -1;
213
Mark Salyzyn2605dec2014-03-19 15:30:25 -0700214 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 -0800215
216 unsigned char* buffers[WINDOW_SIZE];
217 int i;
218 if (encrypted) {
219 for (i = 0; i < WINDOW_SIZE; ++i) {
220 buffers[i] = malloc(sb.st_blksize);
221 }
222 }
223 int head_block = 0;
224 int head = 0, tail = 0;
225 size_t pos = 0;
226
227 int fd = open(path, O_RDONLY);
228 if (fd < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700229 ALOGE("failed to open fd for reading: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800230 return -1;
231 }
232 fsync(fd);
233
234 int wfd = -1;
235 if (encrypted) {
236 wfd = open(blk_dev, O_WRONLY);
237 if (wfd < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700238 ALOGE("failed to open fd for writing: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800239 return -1;
240 }
241 }
242
243 while (pos < sb.st_size) {
244 if ((tail+1) % WINDOW_SIZE == head) {
245 // write out head buffer
246 int block = head_block;
247 ret = ioctl(fd, FIBMAP, &block);
248 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700249 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800250 return -1;
251 }
252 add_block_to_ranges(&ranges, &range_alloc, &range_used, block);
253 if (encrypted) {
254 if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) {
255 return -1;
256 }
257 }
258 head = (head + 1) % WINDOW_SIZE;
259 ++head_block;
260 }
261
262 // read next block to tail
263 if (encrypted) {
264 size_t so_far = 0;
265 while (so_far < sb.st_blksize && pos < sb.st_size) {
266 ssize_t this_read = read(fd, buffers[tail] + so_far, sb.st_blksize - so_far);
267 if (this_read < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700268 ALOGE("failed to read: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800269 return -1;
270 }
271 so_far += this_read;
272 pos += this_read;
273 }
274 } else {
275 // If we're not encrypting; we don't need to actually read
276 // anything, just skip pos forward as if we'd read a
277 // block.
278 pos += sb.st_blksize;
279 }
280 tail = (tail+1) % WINDOW_SIZE;
281 }
282
283 while (head != tail) {
284 // write out head buffer
285 int block = head_block;
286 ret = ioctl(fd, FIBMAP, &block);
287 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700288 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800289 return -1;
290 }
291 add_block_to_ranges(&ranges, &range_alloc, &range_used, block);
292 if (encrypted) {
293 if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) {
294 return -1;
295 }
296 }
297 head = (head + 1) % WINDOW_SIZE;
298 ++head_block;
299 }
300
301 fprintf(mapf, "%d\n", range_used);
302 for (i = 0; i < range_used; ++i) {
303 fprintf(mapf, "%d %d\n", ranges[i*2], ranges[i*2+1]);
304 }
305
306 fclose(mapf);
307 close(fd);
308 if (encrypted) {
309 close(wfd);
310 }
311
312 return 0;
313}
314
Doug Zongker2efc9d92014-08-18 15:55:28 -0700315void wipe_misc() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700316 ALOGI("removing old commands from misc");
Doug Zongker2efc9d92014-08-18 15:55:28 -0700317 int i;
318 for (i = 0; i < fstab->num_entries; ++i) {
319 struct fstab_rec* v = &fstab->recs[i];
320 if (!v->mount_point) continue;
321 if (strcmp(v->mount_point, "/misc") == 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700322 int fd = open(v->blk_device, O_WRONLY);
Doug Zongker2efc9d92014-08-18 15:55:28 -0700323 uint8_t zeroes[1088]; // sizeof(bootloader_message) from recovery
324 memset(zeroes, 0, sizeof(zeroes));
325
326 size_t written = 0;
327 size_t size = sizeof(zeroes);
328 while (written < size) {
329 ssize_t w = write(fd, zeroes, size-written);
330 if (w < 0 && errno != EINTR) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700331 ALOGE("zero write failed: %s\n", strerror(errno));
Doug Zongker2efc9d92014-08-18 15:55:28 -0700332 return;
333 } else {
334 written += w;
335 }
336 }
337
338 close(fd);
339 }
340 }
341}
342
Doug Zongker76adfc52014-01-13 10:04:25 -0800343void reboot_to_recovery() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700344 ALOGI("rebooting to recovery");
Doug Zongker76adfc52014-01-13 10:04:25 -0800345 property_set("sys.powerctl", "reboot,recovery");
346 sleep(10);
Doug Zongkerf449db22014-08-26 09:15:08 -0700347 ALOGE("reboot didn't succeed?");
Doug Zongker76adfc52014-01-13 10:04:25 -0800348}
349
350int main(int argc, char** argv)
351{
352 const char* input_path;
353 const char* map_file;
354 int do_reboot = 1;
355
356 if (argc != 1 && argc != 3) {
357 fprintf(stderr, "usage: %s [<transform_path> <map_file>]\n", argv[0]);
358 return 2;
359 }
360
361 if (argc == 3) {
362 // when command-line args are given this binary is being used
363 // for debugging; don't reboot to recovery at the end.
364 input_path = argv[1];
365 map_file = argv[2];
366 do_reboot = 0;
367 } else {
368 input_path = parse_recovery_command_file();
369 if (input_path == NULL) {
370 // if we're rebooting to recovery without a package (say,
371 // to wipe data), then we don't need to do anything before
372 // going to recovery.
Doug Zongkerf449db22014-08-26 09:15:08 -0700373 ALOGI("no recovery command file or no update package arg");
Doug Zongker76adfc52014-01-13 10:04:25 -0800374 reboot_to_recovery();
375 return 1;
376 }
377 map_file = CACHE_BLOCK_MAP;
378 }
379
Doug Zongkerf449db22014-08-26 09:15:08 -0700380 ALOGI("update package is %s", input_path);
381
Doug Zongker76adfc52014-01-13 10:04:25 -0800382 // Turn the name of the file we're supposed to convert into an
383 // absolute path, so we can find what filesystem it's on.
384 char path[PATH_MAX+1];
385 if (realpath(input_path, path) == NULL) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700386 ALOGE("failed to convert %s to absolute path: %s", input_path, strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800387 return 1;
388 }
389
390 int encryptable;
391 int encrypted;
Doug Zongker2efc9d92014-08-18 15:55:28 -0700392 if (read_fstab() == NULL) {
393 return 1;
394 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800395 const char* blk_dev = find_block_device(path, &encryptable, &encrypted);
396 if (blk_dev == NULL) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700397 ALOGE("failed to find block device for %s", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800398 return 1;
399 }
400
401 // If the filesystem it's on isn't encrypted, we only produce the
402 // block map, we don't rewrite the file contents (it would be
403 // pointless to do so).
Doug Zongkerf449db22014-08-26 09:15:08 -0700404 ALOGI("encryptable: %s\n", encryptable ? "yes" : "no");
405 ALOGI(" encrypted: %s\n", encrypted ? "yes" : "no");
Doug Zongker76adfc52014-01-13 10:04:25 -0800406
Doug Zongker574443d2014-09-05 08:22:12 -0700407 // Recovery supports installing packages from 3 paths: /cache,
408 // /data, and /sdcard. (On a particular device, other locations
409 // may work, but those are three we actually expect.)
410 //
411 // On /data we want to convert the file to a block map so that we
412 // can read the package without mounting the partition. On /cache
413 // and /sdcard we leave the file alone.
414 if (strncmp(path, "/data/", 6) != 0) {
415 // path does not start with "/data/"; leave it alone.
Doug Zongker76adfc52014-01-13 10:04:25 -0800416 unlink(RECOVERY_COMMAND_FILE_TMP);
417 } else {
Doug Zongkerf449db22014-08-26 09:15:08 -0700418 ALOGI("writing block map %s", map_file);
Doug Zongker76adfc52014-01-13 10:04:25 -0800419 if (produce_block_map(path, map_file, blk_dev, encrypted) != 0) {
420 return 1;
421 }
422 }
423
Doug Zongker2efc9d92014-08-18 15:55:28 -0700424 wipe_misc();
Doug Zongker76adfc52014-01-13 10:04:25 -0800425 rename(RECOVERY_COMMAND_FILE_TMP, RECOVERY_COMMAND_FILE);
Doug Zongker2efc9d92014-08-18 15:55:28 -0700426 if (do_reboot) reboot_to_recovery();
Doug Zongker76adfc52014-01-13 10:04:25 -0800427 return 0;
428}