blob: aa75210b0b2abce441ec5dd4c75b80312786a0ca [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{
68 lseek64(wfd, offset, SEEK_SET);
69 size_t written = 0;
70 while (written < size) {
71 ssize_t wrote = write(wfd, buffer + written, size - written);
72 if (wrote < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -070073 ALOGE("error writing offset %lld: %s\n", offset, strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -080074 return -1;
75 }
76 written += wrote;
77 }
78 return 0;
79}
80
81void add_block_to_ranges(int** ranges, int* range_alloc, int* range_used, int new_block)
82{
83 // If the current block start is < 0, set the start to the new
84 // block. (This only happens for the very first block of the very
85 // first range.)
86 if ((*ranges)[*range_used*2-2] < 0) {
87 (*ranges)[*range_used*2-2] = new_block;
88 (*ranges)[*range_used*2-1] = new_block;
89 }
90
91 if (new_block == (*ranges)[*range_used*2-1]) {
92 // If the new block comes immediately after the current range,
93 // all we have to do is extend the current range.
94 ++(*ranges)[*range_used*2-1];
95 } else {
96 // We need to start a new range.
97
98 // If there isn't enough room in the array, we need to expand it.
99 if (*range_used >= *range_alloc) {
100 *range_alloc *= 2;
101 *ranges = realloc(*ranges, *range_alloc * 2 * sizeof(int));
102 }
103
104 ++*range_used;
105 (*ranges)[*range_used*2-2] = new_block;
106 (*ranges)[*range_used*2-1] = new_block+1;
107 }
108}
109
Doug Zongker2efc9d92014-08-18 15:55:28 -0700110static struct fstab* read_fstab()
Doug Zongker76adfc52014-01-13 10:04:25 -0800111{
Doug Zongker2efc9d92014-08-18 15:55:28 -0700112 fstab = NULL;
113
Doug Zongker76adfc52014-01-13 10:04:25 -0800114 // The fstab path is always "/fstab.${ro.hardware}".
115 char fstab_path[PATH_MAX+1] = "/fstab.";
116 if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700117 ALOGE("failed to get ro.hardware\n");
Doug Zongker76adfc52014-01-13 10:04:25 -0800118 return NULL;
119 }
120
Doug Zongker2efc9d92014-08-18 15:55:28 -0700121 fstab = fs_mgr_read_fstab(fstab_path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800122 if (!fstab) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700123 ALOGE("failed to read %s\n", fstab_path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800124 return NULL;
125 }
126
Doug Zongker2efc9d92014-08-18 15:55:28 -0700127 return fstab;
128}
129
130const char* find_block_device(const char* path, int* encryptable, int* encrypted)
131{
Doug Zongker76adfc52014-01-13 10:04:25 -0800132 // Look for a volume whose mount point is the prefix of path and
133 // return its block device. Set encrypted if it's currently
134 // encrypted.
135 int i;
136 for (i = 0; i < fstab->num_entries; ++i) {
137 struct fstab_rec* v = &fstab->recs[i];
138 if (!v->mount_point) continue;
139 int len = strlen(v->mount_point);
140 if (strncmp(path, v->mount_point, len) == 0 &&
141 (path[len] == '/' || path[len] == 0)) {
142 *encrypted = 0;
143 *encryptable = 0;
144 if (fs_mgr_is_encryptable(v)) {
145 *encryptable = 1;
146 char buffer[PROPERTY_VALUE_MAX+1];
147 if (property_get("ro.crypto.state", buffer, "") &&
148 strcmp(buffer, "encrypted") == 0) {
149 *encrypted = 1;
150 }
151 }
152 return v->blk_device;
153 }
154 }
155
156 return NULL;
157}
158
159char* parse_recovery_command_file()
160{
161 char* fn = NULL;
162 int count = 0;
163 char temp[1024];
164
Doug Zongker76adfc52014-01-13 10:04:25 -0800165 FILE* f = fopen(RECOVERY_COMMAND_FILE, "r");
Maxim Siniavinee7b28882014-02-13 15:48:53 -0800166 if (f == NULL) {
167 return NULL;
168 }
Sungmin Choia72512c2014-12-10 21:57:09 +0900169 int fd = open(RECOVERY_COMMAND_FILE_TMP, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR);
170 if (fd < 0) {
171 ALOGE("failed to open %s\n", RECOVERY_COMMAND_FILE_TMP);
172 return NULL;
173 }
Michael Runge4b542392014-11-21 16:00:45 -0800174 FILE* fo = fdopen(fd, "w");
Maxim Siniavinee7b28882014-02-13 15:48:53 -0800175
Doug Zongker76adfc52014-01-13 10:04:25 -0800176 while (fgets(temp, sizeof(temp), f)) {
177 printf("read: %s", temp);
Doug Zongkereaf33652014-07-31 14:59:01 -0700178 if (strncmp(temp, "--update_package=/data/", strlen("--update_package=/data/")) == 0) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800179 fn = strdup(temp + strlen("--update_package="));
180 strcpy(temp, "--update_package=@" CACHE_BLOCK_MAP "\n");
181 }
182 fputs(temp, fo);
183 }
184 fclose(f);
Michael Runge4b542392014-11-21 16:00:45 -0800185 fsync(fd);
Doug Zongker76adfc52014-01-13 10:04:25 -0800186 fclose(fo);
187
188 if (fn) {
189 char* newline = strchr(fn, '\n');
190 if (newline) *newline = 0;
191 }
192 return fn;
193}
194
195int produce_block_map(const char* path, const char* map_file, const char* blk_dev,
196 int encrypted)
197{
198 struct stat sb;
199 int ret;
200
Sungmin Choia72512c2014-12-10 21:57:09 +0900201 int mapfd = open(map_file, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR);
202 if (mapfd < 0) {
203 ALOGE("failed to open %s\n", map_file);
204 return -1;
205 }
Michael Runge4b542392014-11-21 16:00:45 -0800206 FILE* mapf = fdopen(mapfd, "w");
Doug Zongker76adfc52014-01-13 10:04:25 -0800207
208 ret = stat(path, &sb);
209 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700210 ALOGE("failed to stat %s\n", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800211 return -1;
212 }
213
Doug Zongkerf449db22014-08-26 09:15:08 -0700214 ALOGI(" block size: %ld bytes\n", (long)sb.st_blksize);
Doug Zongker76adfc52014-01-13 10:04:25 -0800215
216 int blocks = ((sb.st_size-1) / sb.st_blksize) + 1;
Doug Zongkerf449db22014-08-26 09:15:08 -0700217 ALOGI(" file size: %lld bytes, %d blocks\n", (long long)sb.st_size, blocks);
Doug Zongker76adfc52014-01-13 10:04:25 -0800218
219 int* ranges;
220 int range_alloc = 1;
221 int range_used = 1;
222 ranges = malloc(range_alloc * 2 * sizeof(int));
223 ranges[0] = -1;
224 ranges[1] = -1;
225
Mark Salyzyn2605dec2014-03-19 15:30:25 -0700226 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 -0800227
228 unsigned char* buffers[WINDOW_SIZE];
229 int i;
230 if (encrypted) {
231 for (i = 0; i < WINDOW_SIZE; ++i) {
232 buffers[i] = malloc(sb.st_blksize);
233 }
234 }
235 int head_block = 0;
236 int head = 0, tail = 0;
237 size_t pos = 0;
238
239 int fd = open(path, O_RDONLY);
240 if (fd < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700241 ALOGE("failed to open fd for reading: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800242 return -1;
243 }
244 fsync(fd);
245
246 int wfd = -1;
247 if (encrypted) {
Michael Runge4b542392014-11-21 16:00:45 -0800248 wfd = open(blk_dev, O_WRONLY | O_SYNC);
Doug Zongker76adfc52014-01-13 10:04:25 -0800249 if (wfd < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700250 ALOGE("failed to open fd for writing: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800251 return -1;
252 }
253 }
254
255 while (pos < sb.st_size) {
256 if ((tail+1) % WINDOW_SIZE == head) {
257 // write out head buffer
258 int block = head_block;
259 ret = ioctl(fd, FIBMAP, &block);
260 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700261 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800262 return -1;
263 }
264 add_block_to_ranges(&ranges, &range_alloc, &range_used, block);
265 if (encrypted) {
266 if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) {
267 return -1;
268 }
269 }
270 head = (head + 1) % WINDOW_SIZE;
271 ++head_block;
272 }
273
274 // read next block to tail
275 if (encrypted) {
276 size_t so_far = 0;
277 while (so_far < sb.st_blksize && pos < sb.st_size) {
278 ssize_t this_read = read(fd, buffers[tail] + so_far, sb.st_blksize - so_far);
279 if (this_read < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700280 ALOGE("failed to read: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800281 return -1;
282 }
283 so_far += this_read;
284 pos += this_read;
285 }
286 } else {
287 // If we're not encrypting; we don't need to actually read
288 // anything, just skip pos forward as if we'd read a
289 // block.
290 pos += sb.st_blksize;
291 }
292 tail = (tail+1) % WINDOW_SIZE;
293 }
294
295 while (head != tail) {
296 // write out head buffer
297 int block = head_block;
298 ret = ioctl(fd, FIBMAP, &block);
299 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700300 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800301 return -1;
302 }
303 add_block_to_ranges(&ranges, &range_alloc, &range_used, block);
304 if (encrypted) {
305 if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) {
306 return -1;
307 }
308 }
309 head = (head + 1) % WINDOW_SIZE;
310 ++head_block;
311 }
312
313 fprintf(mapf, "%d\n", range_used);
314 for (i = 0; i < range_used; ++i) {
315 fprintf(mapf, "%d %d\n", ranges[i*2], ranges[i*2+1]);
316 }
317
Michael Runge4b542392014-11-21 16:00:45 -0800318 fsync(mapfd);
Doug Zongker76adfc52014-01-13 10:04:25 -0800319 fclose(mapf);
320 close(fd);
321 if (encrypted) {
Michael Runge4b542392014-11-21 16:00:45 -0800322 fsync(wfd);
Doug Zongker76adfc52014-01-13 10:04:25 -0800323 close(wfd);
324 }
325
326 return 0;
327}
328
Doug Zongker2efc9d92014-08-18 15:55:28 -0700329void wipe_misc() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700330 ALOGI("removing old commands from misc");
Doug Zongker2efc9d92014-08-18 15:55:28 -0700331 int i;
332 for (i = 0; i < fstab->num_entries; ++i) {
333 struct fstab_rec* v = &fstab->recs[i];
334 if (!v->mount_point) continue;
335 if (strcmp(v->mount_point, "/misc") == 0) {
Michael Runge4b542392014-11-21 16:00:45 -0800336 int fd = open(v->blk_device, O_WRONLY | O_SYNC);
Doug Zongker2efc9d92014-08-18 15:55:28 -0700337 uint8_t zeroes[1088]; // sizeof(bootloader_message) from recovery
338 memset(zeroes, 0, sizeof(zeroes));
339
340 size_t written = 0;
341 size_t size = sizeof(zeroes);
342 while (written < size) {
343 ssize_t w = write(fd, zeroes, size-written);
344 if (w < 0 && errno != EINTR) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700345 ALOGE("zero write failed: %s\n", strerror(errno));
Doug Zongker2efc9d92014-08-18 15:55:28 -0700346 return;
347 } else {
348 written += w;
349 }
350 }
Michael Runge4b542392014-11-21 16:00:45 -0800351 fsync(fd);
Doug Zongker2efc9d92014-08-18 15:55:28 -0700352 close(fd);
353 }
354 }
355}
356
Doug Zongker76adfc52014-01-13 10:04:25 -0800357void reboot_to_recovery() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700358 ALOGI("rebooting to recovery");
Doug Zongker76adfc52014-01-13 10:04:25 -0800359 property_set("sys.powerctl", "reboot,recovery");
360 sleep(10);
Doug Zongkerf449db22014-08-26 09:15:08 -0700361 ALOGE("reboot didn't succeed?");
Doug Zongker76adfc52014-01-13 10:04:25 -0800362}
363
364int main(int argc, char** argv)
365{
366 const char* input_path;
367 const char* map_file;
368 int do_reboot = 1;
369
370 if (argc != 1 && argc != 3) {
371 fprintf(stderr, "usage: %s [<transform_path> <map_file>]\n", argv[0]);
372 return 2;
373 }
374
375 if (argc == 3) {
376 // when command-line args are given this binary is being used
377 // for debugging; don't reboot to recovery at the end.
378 input_path = argv[1];
379 map_file = argv[2];
380 do_reboot = 0;
381 } else {
382 input_path = parse_recovery_command_file();
383 if (input_path == NULL) {
384 // if we're rebooting to recovery without a package (say,
385 // to wipe data), then we don't need to do anything before
386 // going to recovery.
Doug Zongkerf449db22014-08-26 09:15:08 -0700387 ALOGI("no recovery command file or no update package arg");
Doug Zongker76adfc52014-01-13 10:04:25 -0800388 reboot_to_recovery();
389 return 1;
390 }
391 map_file = CACHE_BLOCK_MAP;
392 }
393
Doug Zongkerf449db22014-08-26 09:15:08 -0700394 ALOGI("update package is %s", input_path);
395
Doug Zongker76adfc52014-01-13 10:04:25 -0800396 // Turn the name of the file we're supposed to convert into an
397 // absolute path, so we can find what filesystem it's on.
398 char path[PATH_MAX+1];
399 if (realpath(input_path, path) == NULL) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700400 ALOGE("failed to convert %s to absolute path: %s", input_path, strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800401 return 1;
402 }
403
404 int encryptable;
405 int encrypted;
Doug Zongker2efc9d92014-08-18 15:55:28 -0700406 if (read_fstab() == NULL) {
407 return 1;
408 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800409 const char* blk_dev = find_block_device(path, &encryptable, &encrypted);
410 if (blk_dev == NULL) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700411 ALOGE("failed to find block device for %s", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800412 return 1;
413 }
414
415 // If the filesystem it's on isn't encrypted, we only produce the
416 // block map, we don't rewrite the file contents (it would be
417 // pointless to do so).
Doug Zongkerf449db22014-08-26 09:15:08 -0700418 ALOGI("encryptable: %s\n", encryptable ? "yes" : "no");
419 ALOGI(" encrypted: %s\n", encrypted ? "yes" : "no");
Doug Zongker76adfc52014-01-13 10:04:25 -0800420
Doug Zongker574443d2014-09-05 08:22:12 -0700421 // Recovery supports installing packages from 3 paths: /cache,
422 // /data, and /sdcard. (On a particular device, other locations
423 // may work, but those are three we actually expect.)
424 //
425 // On /data we want to convert the file to a block map so that we
426 // can read the package without mounting the partition. On /cache
427 // and /sdcard we leave the file alone.
428 if (strncmp(path, "/data/", 6) != 0) {
429 // path does not start with "/data/"; leave it alone.
Doug Zongker76adfc52014-01-13 10:04:25 -0800430 unlink(RECOVERY_COMMAND_FILE_TMP);
431 } else {
Doug Zongkerf449db22014-08-26 09:15:08 -0700432 ALOGI("writing block map %s", map_file);
Doug Zongker76adfc52014-01-13 10:04:25 -0800433 if (produce_block_map(path, map_file, blk_dev, encrypted) != 0) {
434 return 1;
435 }
436 }
437
Doug Zongker2efc9d92014-08-18 15:55:28 -0700438 wipe_misc();
Doug Zongker76adfc52014-01-13 10:04:25 -0800439 rename(RECOVERY_COMMAND_FILE_TMP, RECOVERY_COMMAND_FILE);
Doug Zongker2efc9d92014-08-18 15:55:28 -0700440 if (do_reboot) reboot_to_recovery();
Doug Zongker76adfc52014-01-13 10:04:25 -0800441 return 0;
442}