blob: d71271d8eb2ee08b82326b10a3a78ab1a3d59a7d [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>
Tao Bao75238632015-05-27 14:46:17 -070043#include <fcntl.h>
44#include <linux/fs.h>
45#include <stdarg.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080046#include <stdio.h>
47#include <stdlib.h>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080048#include <string.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080049#include <sys/types.h>
50#include <sys/stat.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080051#include <sys/mman.h>
Tao Bao75238632015-05-27 14:46:17 -070052#include <unistd.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080053
Doug Zongkerf449db22014-08-26 09:15:08 -070054#define LOG_TAG "uncrypt"
55#include <log/log.h>
Tao Bao75238632015-05-27 14:46:17 -070056#include <cutils/android_reboot.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080057#include <cutils/properties.h>
58#include <fs_mgr.h>
59
60#define WINDOW_SIZE 5
61#define RECOVERY_COMMAND_FILE "/cache/recovery/command"
62#define RECOVERY_COMMAND_FILE_TMP "/cache/recovery/command.tmp"
63#define CACHE_BLOCK_MAP "/cache/recovery/block.map"
64
Doug Zongker2efc9d92014-08-18 15:55:28 -070065static struct fstab* fstab = NULL;
66
Tao Bao381f4552015-05-05 18:36:45 -070067static int write_at_offset(unsigned char* buffer, size_t size, int wfd, off64_t offset) {
Elliott Hughes7bad7c42015-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 Hughes7bad7c42015-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
Tao Bao381f4552015-05-05 18:36:45 -070084static void add_block_to_ranges(int** ranges, int* range_alloc, int* range_used, int new_block) {
Doug Zongker76adfc52014-01-13 10:04:25 -080085 // If the current block start is < 0, set the start to the new
86 // block. (This only happens for the very first block of the very
87 // first range.)
88 if ((*ranges)[*range_used*2-2] < 0) {
89 (*ranges)[*range_used*2-2] = new_block;
90 (*ranges)[*range_used*2-1] = new_block;
91 }
92
93 if (new_block == (*ranges)[*range_used*2-1]) {
94 // If the new block comes immediately after the current range,
95 // all we have to do is extend the current range.
96 ++(*ranges)[*range_used*2-1];
97 } else {
98 // We need to start a new range.
99
100 // If there isn't enough room in the array, we need to expand it.
101 if (*range_used >= *range_alloc) {
102 *range_alloc *= 2;
Tao Bao381f4552015-05-05 18:36:45 -0700103 *ranges = reinterpret_cast<int*>(realloc(*ranges, *range_alloc * 2 * sizeof(int)));
Doug Zongker76adfc52014-01-13 10:04:25 -0800104 }
105
106 ++*range_used;
107 (*ranges)[*range_used*2-2] = new_block;
108 (*ranges)[*range_used*2-1] = new_block+1;
109 }
110}
111
Tao Bao381f4552015-05-05 18:36:45 -0700112static struct fstab* read_fstab() {
Doug Zongker2efc9d92014-08-18 15:55:28 -0700113 fstab = NULL;
114
Doug Zongker76adfc52014-01-13 10:04:25 -0800115 // The fstab path is always "/fstab.${ro.hardware}".
116 char fstab_path[PATH_MAX+1] = "/fstab.";
117 if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700118 ALOGE("failed to get ro.hardware\n");
Doug Zongker76adfc52014-01-13 10:04:25 -0800119 return NULL;
120 }
121
Doug Zongker2efc9d92014-08-18 15:55:28 -0700122 fstab = fs_mgr_read_fstab(fstab_path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800123 if (!fstab) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700124 ALOGE("failed to read %s\n", fstab_path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800125 return NULL;
126 }
127
Doug Zongker2efc9d92014-08-18 15:55:28 -0700128 return fstab;
129}
130
Tao Bao381f4552015-05-05 18:36:45 -0700131static const char* find_block_device(const char* path, bool* encryptable, bool* encrypted) {
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.
Tao Bao381f4552015-05-05 18:36:45 -0700135 for (int i = 0; i < fstab->num_entries; ++i) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800136 struct fstab_rec* v = &fstab->recs[i];
Tao Bao381f4552015-05-05 18:36:45 -0700137 if (!v->mount_point) {
138 continue;
139 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800140 int len = strlen(v->mount_point);
141 if (strncmp(path, v->mount_point, len) == 0 &&
142 (path[len] == '/' || path[len] == 0)) {
Tao Bao381f4552015-05-05 18:36:45 -0700143 *encrypted = false;
144 *encryptable = false;
Doug Zongker76adfc52014-01-13 10:04:25 -0800145 if (fs_mgr_is_encryptable(v)) {
Tao Bao381f4552015-05-05 18:36:45 -0700146 *encryptable = true;
Doug Zongker76adfc52014-01-13 10:04:25 -0800147 char buffer[PROPERTY_VALUE_MAX+1];
148 if (property_get("ro.crypto.state", buffer, "") &&
149 strcmp(buffer, "encrypted") == 0) {
Tao Bao381f4552015-05-05 18:36:45 -0700150 *encrypted = true;
Doug Zongker76adfc52014-01-13 10:04:25 -0800151 }
152 }
153 return v->blk_device;
154 }
155 }
156
157 return NULL;
158}
159
Tao Baofb4ccef2015-05-04 10:10:13 -0700160// Parse the command file RECOVERY_COMMAND_FILE to find the update package
161// name. If it's on the /data partition, replace the package name with the
162// block map file name and store it temporarily in RECOVERY_COMMAND_FILE_TMP.
163// It will be renamed to RECOVERY_COMMAND_FILE if uncrypt finishes
164// successfully.
165static char* find_update_package()
Doug Zongker76adfc52014-01-13 10:04:25 -0800166{
Doug Zongker76adfc52014-01-13 10:04:25 -0800167 FILE* f = fopen(RECOVERY_COMMAND_FILE, "r");
Maxim Siniavinee7b28882014-02-13 15:48:53 -0800168 if (f == NULL) {
169 return NULL;
170 }
Sungmin Choia72512c2014-12-10 21:57:09 +0900171 int fd = open(RECOVERY_COMMAND_FILE_TMP, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR);
172 if (fd < 0) {
173 ALOGE("failed to open %s\n", RECOVERY_COMMAND_FILE_TMP);
174 return NULL;
175 }
Michael Runge4b542392014-11-21 16:00:45 -0800176 FILE* fo = fdopen(fd, "w");
Tao Baofb4ccef2015-05-04 10:10:13 -0700177 char* fn = NULL;
178 char* line = NULL;
179 size_t len = 0;
180 while (getline(&line, &len, f) != -1) {
181 if (strncmp(line, "--update_package=", strlen("--update_package=")) == 0) {
182 fn = strdup(line + strlen("--update_package="));
183 // Replace the package name with block map file if it's on /data partition.
184 if (strncmp(fn, "/data/", strlen("/data/")) == 0) {
185 fputs("--update_package=@" CACHE_BLOCK_MAP "\n", fo);
186 continue;
187 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800188 }
Tao Baofb4ccef2015-05-04 10:10:13 -0700189 fputs(line, fo);
Doug Zongker76adfc52014-01-13 10:04:25 -0800190 }
Tao Baofb4ccef2015-05-04 10:10:13 -0700191 free(line);
Doug Zongker76adfc52014-01-13 10:04:25 -0800192 fclose(f);
Tao Baofb4ccef2015-05-04 10:10:13 -0700193 if (fsync(fd) == -1) {
194 ALOGE("failed to fsync \"%s\": %s\n", RECOVERY_COMMAND_FILE_TMP, strerror(errno));
195 fclose(fo);
196 return NULL;
197 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800198 fclose(fo);
199
200 if (fn) {
201 char* newline = strchr(fn, '\n');
Tao Bao381f4552015-05-05 18:36:45 -0700202 if (newline) {
203 *newline = 0;
204 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800205 }
206 return fn;
207}
208
Tao Bao381f4552015-05-05 18:36:45 -0700209static int produce_block_map(const char* path, const char* map_file, const char* blk_dev,
210 bool encrypted) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800211
Sungmin Choia72512c2014-12-10 21:57:09 +0900212 int mapfd = open(map_file, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR);
213 if (mapfd < 0) {
214 ALOGE("failed to open %s\n", map_file);
215 return -1;
216 }
Michael Runge4b542392014-11-21 16:00:45 -0800217 FILE* mapf = fdopen(mapfd, "w");
Doug Zongker76adfc52014-01-13 10:04:25 -0800218
Tao Bao381f4552015-05-05 18:36:45 -0700219 struct stat sb;
220 int ret = stat(path, &sb);
Doug Zongker76adfc52014-01-13 10:04:25 -0800221 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700222 ALOGE("failed to stat %s\n", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800223 return -1;
224 }
225
Doug Zongkerf449db22014-08-26 09:15:08 -0700226 ALOGI(" block size: %ld bytes\n", (long)sb.st_blksize);
Doug Zongker76adfc52014-01-13 10:04:25 -0800227
228 int blocks = ((sb.st_size-1) / sb.st_blksize) + 1;
Doug Zongkerf449db22014-08-26 09:15:08 -0700229 ALOGI(" file size: %lld bytes, %d blocks\n", (long long)sb.st_size, blocks);
Doug Zongker76adfc52014-01-13 10:04:25 -0800230
Doug Zongker76adfc52014-01-13 10:04:25 -0800231 int range_alloc = 1;
232 int range_used = 1;
Tao Bao381f4552015-05-05 18:36:45 -0700233 int* ranges = reinterpret_cast<int*>(malloc(range_alloc * 2 * sizeof(int)));
Doug Zongker76adfc52014-01-13 10:04:25 -0800234 ranges[0] = -1;
235 ranges[1] = -1;
236
Mark Salyzyn2605dec2014-03-19 15:30:25 -0700237 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 -0800238
239 unsigned char* buffers[WINDOW_SIZE];
Doug Zongker76adfc52014-01-13 10:04:25 -0800240 if (encrypted) {
Tao Bao381f4552015-05-05 18:36:45 -0700241 for (size_t i = 0; i < WINDOW_SIZE; ++i) {
242 buffers[i] = reinterpret_cast<unsigned char*>(malloc(sb.st_blksize));
Doug Zongker76adfc52014-01-13 10:04:25 -0800243 }
244 }
245 int head_block = 0;
246 int head = 0, tail = 0;
247 size_t pos = 0;
248
249 int fd = open(path, O_RDONLY);
250 if (fd < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700251 ALOGE("failed to open fd for reading: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800252 return -1;
253 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800254
255 int wfd = -1;
256 if (encrypted) {
Michael Runge4b542392014-11-21 16:00:45 -0800257 wfd = open(blk_dev, O_WRONLY | O_SYNC);
Doug Zongker76adfc52014-01-13 10:04:25 -0800258 if (wfd < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700259 ALOGE("failed to open fd for writing: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800260 return -1;
261 }
262 }
263
264 while (pos < sb.st_size) {
265 if ((tail+1) % WINDOW_SIZE == head) {
266 // write out head buffer
267 int block = head_block;
268 ret = ioctl(fd, FIBMAP, &block);
269 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700270 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800271 return -1;
272 }
273 add_block_to_ranges(&ranges, &range_alloc, &range_used, block);
274 if (encrypted) {
Tao Bao381f4552015-05-05 18:36:45 -0700275 if (write_at_offset(buffers[head], sb.st_blksize, wfd,
276 (off64_t)sb.st_blksize * block) != 0) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800277 return -1;
278 }
279 }
280 head = (head + 1) % WINDOW_SIZE;
281 ++head_block;
282 }
283
284 // read next block to tail
285 if (encrypted) {
286 size_t so_far = 0;
287 while (so_far < sb.st_blksize && pos < sb.st_size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700288 ssize_t this_read =
289 TEMP_FAILURE_RETRY(read(fd, buffers[tail] + so_far, sb.st_blksize - so_far));
290 if (this_read == -1) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700291 ALOGE("failed to read: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800292 return -1;
293 }
294 so_far += this_read;
295 pos += this_read;
296 }
297 } else {
298 // If we're not encrypting; we don't need to actually read
299 // anything, just skip pos forward as if we'd read a
300 // block.
301 pos += sb.st_blksize;
302 }
303 tail = (tail+1) % WINDOW_SIZE;
304 }
305
306 while (head != tail) {
307 // write out head buffer
308 int block = head_block;
309 ret = ioctl(fd, FIBMAP, &block);
310 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700311 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800312 return -1;
313 }
314 add_block_to_ranges(&ranges, &range_alloc, &range_used, block);
315 if (encrypted) {
Tao Bao381f4552015-05-05 18:36:45 -0700316 if (write_at_offset(buffers[head], sb.st_blksize, wfd,
317 (off64_t)sb.st_blksize * block) != 0) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800318 return -1;
319 }
320 }
321 head = (head + 1) % WINDOW_SIZE;
322 ++head_block;
323 }
324
325 fprintf(mapf, "%d\n", range_used);
Tao Bao381f4552015-05-05 18:36:45 -0700326 for (int i = 0; i < range_used; ++i) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800327 fprintf(mapf, "%d %d\n", ranges[i*2], ranges[i*2+1]);
328 }
329
Tao Baofb4ccef2015-05-04 10:10:13 -0700330 if (fsync(mapfd) == -1) {
331 ALOGE("failed to fsync \"%s\": %s\n", map_file, strerror(errno));
332 return -1;
333 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800334 fclose(mapf);
335 close(fd);
336 if (encrypted) {
Tao Baofb4ccef2015-05-04 10:10:13 -0700337 if (fsync(wfd) == -1) {
338 ALOGE("failed to fsync \"%s\": %s\n", blk_dev, strerror(errno));
339 return -1;
340 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800341 close(wfd);
342 }
343
344 return 0;
345}
346
Tao Bao381f4552015-05-05 18:36:45 -0700347static void wipe_misc() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700348 ALOGI("removing old commands from misc");
Tao Bao381f4552015-05-05 18:36:45 -0700349 for (int i = 0; i < fstab->num_entries; ++i) {
Doug Zongker2efc9d92014-08-18 15:55:28 -0700350 struct fstab_rec* v = &fstab->recs[i];
351 if (!v->mount_point) continue;
352 if (strcmp(v->mount_point, "/misc") == 0) {
Michael Runge4b542392014-11-21 16:00:45 -0800353 int fd = open(v->blk_device, O_WRONLY | O_SYNC);
Doug Zongker2efc9d92014-08-18 15:55:28 -0700354 uint8_t zeroes[1088]; // sizeof(bootloader_message) from recovery
355 memset(zeroes, 0, sizeof(zeroes));
356
357 size_t written = 0;
358 size_t size = sizeof(zeroes);
359 while (written < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700360 ssize_t w = TEMP_FAILURE_RETRY(write(fd, zeroes, size-written));
361 if (w == -1) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700362 ALOGE("zero write failed: %s\n", strerror(errno));
Doug Zongker2efc9d92014-08-18 15:55:28 -0700363 return;
364 } else {
365 written += w;
366 }
367 }
Tao Baofb4ccef2015-05-04 10:10:13 -0700368 if (fsync(fd) == -1) {
369 ALOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno));
370 close(fd);
371 return;
372 }
Doug Zongker2efc9d92014-08-18 15:55:28 -0700373 close(fd);
374 }
375 }
376}
377
Tao Bao381f4552015-05-05 18:36:45 -0700378static void reboot_to_recovery() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700379 ALOGI("rebooting to recovery");
Doug Zongker76adfc52014-01-13 10:04:25 -0800380 property_set("sys.powerctl", "reboot,recovery");
Tao Bao75238632015-05-27 14:46:17 -0700381 while (true) {
382 pause();
383 }
Doug Zongkerf449db22014-08-26 09:15:08 -0700384 ALOGE("reboot didn't succeed?");
Doug Zongker76adfc52014-01-13 10:04:25 -0800385}
386
387int main(int argc, char** argv)
388{
389 const char* input_path;
390 const char* map_file;
Tao Bao381f4552015-05-05 18:36:45 -0700391 bool do_reboot = true;
Doug Zongker76adfc52014-01-13 10:04:25 -0800392
393 if (argc != 1 && argc != 3) {
394 fprintf(stderr, "usage: %s [<transform_path> <map_file>]\n", argv[0]);
395 return 2;
396 }
397
398 if (argc == 3) {
399 // when command-line args are given this binary is being used
400 // for debugging; don't reboot to recovery at the end.
401 input_path = argv[1];
402 map_file = argv[2];
Tao Bao381f4552015-05-05 18:36:45 -0700403 do_reboot = false;
Doug Zongker76adfc52014-01-13 10:04:25 -0800404 } else {
Tao Baofb4ccef2015-05-04 10:10:13 -0700405 input_path = find_update_package();
Doug Zongker76adfc52014-01-13 10:04:25 -0800406 if (input_path == NULL) {
407 // if we're rebooting to recovery without a package (say,
408 // to wipe data), then we don't need to do anything before
409 // going to recovery.
Doug Zongkerf449db22014-08-26 09:15:08 -0700410 ALOGI("no recovery command file or no update package arg");
Doug Zongker76adfc52014-01-13 10:04:25 -0800411 reboot_to_recovery();
412 return 1;
413 }
414 map_file = CACHE_BLOCK_MAP;
415 }
416
Doug Zongkerf449db22014-08-26 09:15:08 -0700417 ALOGI("update package is %s", input_path);
418
Doug Zongker76adfc52014-01-13 10:04:25 -0800419 // Turn the name of the file we're supposed to convert into an
420 // absolute path, so we can find what filesystem it's on.
421 char path[PATH_MAX+1];
422 if (realpath(input_path, path) == NULL) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700423 ALOGE("failed to convert %s to absolute path: %s", input_path, strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800424 return 1;
425 }
426
Doug Zongker2efc9d92014-08-18 15:55:28 -0700427 if (read_fstab() == NULL) {
428 return 1;
429 }
Tao Bao381f4552015-05-05 18:36:45 -0700430
431 bool encryptable;
432 bool encrypted;
Doug Zongker76adfc52014-01-13 10:04:25 -0800433 const char* blk_dev = find_block_device(path, &encryptable, &encrypted);
434 if (blk_dev == NULL) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700435 ALOGE("failed to find block device for %s", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800436 return 1;
437 }
438
439 // If the filesystem it's on isn't encrypted, we only produce the
440 // block map, we don't rewrite the file contents (it would be
441 // pointless to do so).
Doug Zongkerf449db22014-08-26 09:15:08 -0700442 ALOGI("encryptable: %s\n", encryptable ? "yes" : "no");
443 ALOGI(" encrypted: %s\n", encrypted ? "yes" : "no");
Doug Zongker76adfc52014-01-13 10:04:25 -0800444
Doug Zongker574443d2014-09-05 08:22:12 -0700445 // Recovery supports installing packages from 3 paths: /cache,
446 // /data, and /sdcard. (On a particular device, other locations
447 // may work, but those are three we actually expect.)
448 //
449 // On /data we want to convert the file to a block map so that we
450 // can read the package without mounting the partition. On /cache
451 // and /sdcard we leave the file alone.
452 if (strncmp(path, "/data/", 6) != 0) {
453 // path does not start with "/data/"; leave it alone.
Doug Zongker76adfc52014-01-13 10:04:25 -0800454 unlink(RECOVERY_COMMAND_FILE_TMP);
Tao Baofb4ccef2015-05-04 10:10:13 -0700455 wipe_misc();
Doug Zongker76adfc52014-01-13 10:04:25 -0800456 } else {
Doug Zongkerf449db22014-08-26 09:15:08 -0700457 ALOGI("writing block map %s", map_file);
Doug Zongker76adfc52014-01-13 10:04:25 -0800458 if (produce_block_map(path, map_file, blk_dev, encrypted) != 0) {
459 return 1;
460 }
Tao Baofb4ccef2015-05-04 10:10:13 -0700461 wipe_misc();
462 rename(RECOVERY_COMMAND_FILE_TMP, RECOVERY_COMMAND_FILE);
Doug Zongker76adfc52014-01-13 10:04:25 -0800463 }
464
Tao Bao381f4552015-05-05 18:36:45 -0700465 if (do_reboot) {
466 reboot_to_recovery();
467 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800468 return 0;
469}