blob: da035dfbaa6139d16b07d378ced0268d5dcaceb6 [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 Hughes2f5feed2015-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 Hughes2f5feed2015-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
162char* parse_recovery_command_file()
163{
164 char* fn = NULL;
165 int count = 0;
166 char temp[1024];
167
Doug Zongker76adfc52014-01-13 10:04:25 -0800168 FILE* f = fopen(RECOVERY_COMMAND_FILE, "r");
Maxim Siniavinee7b28882014-02-13 15:48:53 -0800169 if (f == NULL) {
170 return NULL;
171 }
Sungmin Choia72512c2014-12-10 21:57:09 +0900172 int fd = open(RECOVERY_COMMAND_FILE_TMP, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR);
173 if (fd < 0) {
174 ALOGE("failed to open %s\n", RECOVERY_COMMAND_FILE_TMP);
175 return NULL;
176 }
Michael Runge4b542392014-11-21 16:00:45 -0800177 FILE* fo = fdopen(fd, "w");
Maxim Siniavinee7b28882014-02-13 15:48:53 -0800178
Doug Zongker76adfc52014-01-13 10:04:25 -0800179 while (fgets(temp, sizeof(temp), f)) {
180 printf("read: %s", temp);
Doug Zongkereaf33652014-07-31 14:59:01 -0700181 if (strncmp(temp, "--update_package=/data/", strlen("--update_package=/data/")) == 0) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800182 fn = strdup(temp + strlen("--update_package="));
183 strcpy(temp, "--update_package=@" CACHE_BLOCK_MAP "\n");
184 }
185 fputs(temp, fo);
186 }
187 fclose(f);
Michael Runge4b542392014-11-21 16:00:45 -0800188 fsync(fd);
Doug Zongker76adfc52014-01-13 10:04:25 -0800189 fclose(fo);
190
191 if (fn) {
192 char* newline = strchr(fn, '\n');
193 if (newline) *newline = 0;
194 }
195 return fn;
196}
197
198int produce_block_map(const char* path, const char* map_file, const char* blk_dev,
199 int encrypted)
200{
201 struct stat sb;
202 int ret;
203
Sungmin Choia72512c2014-12-10 21:57:09 +0900204 int mapfd = open(map_file, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR);
205 if (mapfd < 0) {
206 ALOGE("failed to open %s\n", map_file);
207 return -1;
208 }
Michael Runge4b542392014-11-21 16:00:45 -0800209 FILE* mapf = fdopen(mapfd, "w");
Doug Zongker76adfc52014-01-13 10:04:25 -0800210
211 ret = stat(path, &sb);
212 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700213 ALOGE("failed to stat %s\n", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800214 return -1;
215 }
216
Doug Zongkerf449db22014-08-26 09:15:08 -0700217 ALOGI(" block size: %ld bytes\n", (long)sb.st_blksize);
Doug Zongker76adfc52014-01-13 10:04:25 -0800218
219 int blocks = ((sb.st_size-1) / sb.st_blksize) + 1;
Doug Zongkerf449db22014-08-26 09:15:08 -0700220 ALOGI(" file size: %lld bytes, %d blocks\n", (long long)sb.st_size, blocks);
Doug Zongker76adfc52014-01-13 10:04:25 -0800221
222 int* ranges;
223 int range_alloc = 1;
224 int range_used = 1;
225 ranges = malloc(range_alloc * 2 * sizeof(int));
226 ranges[0] = -1;
227 ranges[1] = -1;
228
Mark Salyzyn2605dec2014-03-19 15:30:25 -0700229 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 -0800230
231 unsigned char* buffers[WINDOW_SIZE];
232 int i;
233 if (encrypted) {
234 for (i = 0; i < WINDOW_SIZE; ++i) {
235 buffers[i] = malloc(sb.st_blksize);
236 }
237 }
238 int head_block = 0;
239 int head = 0, tail = 0;
240 size_t pos = 0;
241
242 int fd = open(path, O_RDONLY);
243 if (fd < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700244 ALOGE("failed to open fd for reading: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800245 return -1;
246 }
247 fsync(fd);
248
249 int wfd = -1;
250 if (encrypted) {
Michael Runge4b542392014-11-21 16:00:45 -0800251 wfd = open(blk_dev, O_WRONLY | O_SYNC);
Doug Zongker76adfc52014-01-13 10:04:25 -0800252 if (wfd < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700253 ALOGE("failed to open fd for writing: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800254 return -1;
255 }
256 }
257
258 while (pos < sb.st_size) {
259 if ((tail+1) % WINDOW_SIZE == head) {
260 // write out head buffer
261 int block = head_block;
262 ret = ioctl(fd, FIBMAP, &block);
263 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700264 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800265 return -1;
266 }
267 add_block_to_ranges(&ranges, &range_alloc, &range_used, block);
268 if (encrypted) {
269 if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) {
270 return -1;
271 }
272 }
273 head = (head + 1) % WINDOW_SIZE;
274 ++head_block;
275 }
276
277 // read next block to tail
278 if (encrypted) {
279 size_t so_far = 0;
280 while (so_far < sb.st_blksize && pos < sb.st_size) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700281 ssize_t this_read =
282 TEMP_FAILURE_RETRY(read(fd, buffers[tail] + so_far, sb.st_blksize - so_far));
283 if (this_read == -1) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700284 ALOGE("failed to read: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800285 return -1;
286 }
287 so_far += this_read;
288 pos += this_read;
289 }
290 } else {
291 // If we're not encrypting; we don't need to actually read
292 // anything, just skip pos forward as if we'd read a
293 // block.
294 pos += sb.st_blksize;
295 }
296 tail = (tail+1) % WINDOW_SIZE;
297 }
298
299 while (head != tail) {
300 // write out head buffer
301 int block = head_block;
302 ret = ioctl(fd, FIBMAP, &block);
303 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700304 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800305 return -1;
306 }
307 add_block_to_ranges(&ranges, &range_alloc, &range_used, block);
308 if (encrypted) {
309 if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) {
310 return -1;
311 }
312 }
313 head = (head + 1) % WINDOW_SIZE;
314 ++head_block;
315 }
316
317 fprintf(mapf, "%d\n", range_used);
318 for (i = 0; i < range_used; ++i) {
319 fprintf(mapf, "%d %d\n", ranges[i*2], ranges[i*2+1]);
320 }
321
Michael Runge4b542392014-11-21 16:00:45 -0800322 fsync(mapfd);
Doug Zongker76adfc52014-01-13 10:04:25 -0800323 fclose(mapf);
324 close(fd);
325 if (encrypted) {
Michael Runge4b542392014-11-21 16:00:45 -0800326 fsync(wfd);
Doug Zongker76adfc52014-01-13 10:04:25 -0800327 close(wfd);
328 }
329
330 return 0;
331}
332
Doug Zongker2efc9d92014-08-18 15:55:28 -0700333void wipe_misc() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700334 ALOGI("removing old commands from misc");
Doug Zongker2efc9d92014-08-18 15:55:28 -0700335 int i;
336 for (i = 0; i < fstab->num_entries; ++i) {
337 struct fstab_rec* v = &fstab->recs[i];
338 if (!v->mount_point) continue;
339 if (strcmp(v->mount_point, "/misc") == 0) {
Michael Runge4b542392014-11-21 16:00:45 -0800340 int fd = open(v->blk_device, O_WRONLY | O_SYNC);
Doug Zongker2efc9d92014-08-18 15:55:28 -0700341 uint8_t zeroes[1088]; // sizeof(bootloader_message) from recovery
342 memset(zeroes, 0, sizeof(zeroes));
343
344 size_t written = 0;
345 size_t size = sizeof(zeroes);
346 while (written < size) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700347 ssize_t w = TEMP_FAILURE_RETRY(write(fd, zeroes, size-written));
348 if (w == -1) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700349 ALOGE("zero write failed: %s\n", strerror(errno));
Doug Zongker2efc9d92014-08-18 15:55:28 -0700350 return;
351 } else {
352 written += w;
353 }
354 }
Michael Runge4b542392014-11-21 16:00:45 -0800355 fsync(fd);
Doug Zongker2efc9d92014-08-18 15:55:28 -0700356 close(fd);
357 }
358 }
359}
360
Doug Zongker76adfc52014-01-13 10:04:25 -0800361void reboot_to_recovery() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700362 ALOGI("rebooting to recovery");
Doug Zongker76adfc52014-01-13 10:04:25 -0800363 property_set("sys.powerctl", "reboot,recovery");
364 sleep(10);
Doug Zongkerf449db22014-08-26 09:15:08 -0700365 ALOGE("reboot didn't succeed?");
Doug Zongker76adfc52014-01-13 10:04:25 -0800366}
367
368int main(int argc, char** argv)
369{
370 const char* input_path;
371 const char* map_file;
372 int do_reboot = 1;
373
374 if (argc != 1 && argc != 3) {
375 fprintf(stderr, "usage: %s [<transform_path> <map_file>]\n", argv[0]);
376 return 2;
377 }
378
379 if (argc == 3) {
380 // when command-line args are given this binary is being used
381 // for debugging; don't reboot to recovery at the end.
382 input_path = argv[1];
383 map_file = argv[2];
384 do_reboot = 0;
385 } else {
386 input_path = parse_recovery_command_file();
387 if (input_path == NULL) {
388 // if we're rebooting to recovery without a package (say,
389 // to wipe data), then we don't need to do anything before
390 // going to recovery.
Doug Zongkerf449db22014-08-26 09:15:08 -0700391 ALOGI("no recovery command file or no update package arg");
Doug Zongker76adfc52014-01-13 10:04:25 -0800392 reboot_to_recovery();
393 return 1;
394 }
395 map_file = CACHE_BLOCK_MAP;
396 }
397
Doug Zongkerf449db22014-08-26 09:15:08 -0700398 ALOGI("update package is %s", input_path);
399
Doug Zongker76adfc52014-01-13 10:04:25 -0800400 // Turn the name of the file we're supposed to convert into an
401 // absolute path, so we can find what filesystem it's on.
402 char path[PATH_MAX+1];
403 if (realpath(input_path, path) == NULL) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700404 ALOGE("failed to convert %s to absolute path: %s", input_path, strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800405 return 1;
406 }
407
408 int encryptable;
409 int encrypted;
Doug Zongker2efc9d92014-08-18 15:55:28 -0700410 if (read_fstab() == NULL) {
411 return 1;
412 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800413 const char* blk_dev = find_block_device(path, &encryptable, &encrypted);
414 if (blk_dev == NULL) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700415 ALOGE("failed to find block device for %s", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800416 return 1;
417 }
418
419 // If the filesystem it's on isn't encrypted, we only produce the
420 // block map, we don't rewrite the file contents (it would be
421 // pointless to do so).
Doug Zongkerf449db22014-08-26 09:15:08 -0700422 ALOGI("encryptable: %s\n", encryptable ? "yes" : "no");
423 ALOGI(" encrypted: %s\n", encrypted ? "yes" : "no");
Doug Zongker76adfc52014-01-13 10:04:25 -0800424
Doug Zongker574443d2014-09-05 08:22:12 -0700425 // Recovery supports installing packages from 3 paths: /cache,
426 // /data, and /sdcard. (On a particular device, other locations
427 // may work, but those are three we actually expect.)
428 //
429 // On /data we want to convert the file to a block map so that we
430 // can read the package without mounting the partition. On /cache
431 // and /sdcard we leave the file alone.
432 if (strncmp(path, "/data/", 6) != 0) {
433 // path does not start with "/data/"; leave it alone.
Doug Zongker76adfc52014-01-13 10:04:25 -0800434 unlink(RECOVERY_COMMAND_FILE_TMP);
435 } else {
Doug Zongkerf449db22014-08-26 09:15:08 -0700436 ALOGI("writing block map %s", map_file);
Doug Zongker76adfc52014-01-13 10:04:25 -0800437 if (produce_block_map(path, map_file, blk_dev, encrypted) != 0) {
438 return 1;
439 }
440 }
441
Doug Zongker2efc9d92014-08-18 15:55:28 -0700442 wipe_misc();
Doug Zongker76adfc52014-01-13 10:04:25 -0800443 rename(RECOVERY_COMMAND_FILE_TMP, RECOVERY_COMMAND_FILE);
Doug Zongker2efc9d92014-08-18 15:55:28 -0700444 if (do_reboot) reboot_to_recovery();
Doug Zongker76adfc52014-01-13 10:04:25 -0800445 return 0;
446}