blob: 482504192589d36cf2fecc557400334031250a92 [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/mman.h>
Tao Bao383b00d2015-05-21 16:44:44 -070050#include <sys/stat.h>
51#include <sys/types.h>
Tao Bao75238632015-05-27 14:46:17 -070052#include <unistd.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080053
Tao Baoc7547922015-08-06 18:35:05 -070054#include <memory>
55
Elliott Hughes4b166f02015-12-04 15:30:20 -080056#include <android-base/file.h>
57#include <android-base/strings.h>
Tao Bao75238632015-05-27 14:46:17 -070058#include <cutils/android_reboot.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080059#include <cutils/properties.h>
60#include <fs_mgr.h>
61
Tao Bao383b00d2015-05-21 16:44:44 -070062#define LOG_TAG "uncrypt"
63#include <log/log.h>
64
Tao Baoc7547922015-08-06 18:35:05 -070065#include "unique_fd.h"
66
Doug Zongker76adfc52014-01-13 10:04:25 -080067#define WINDOW_SIZE 5
Tao Bao383b00d2015-05-21 16:44:44 -070068
69static const std::string cache_block_map = "/cache/recovery/block.map";
70static const std::string status_file = "/cache/recovery/uncrypt_status";
71static const std::string uncrypt_file = "/cache/recovery/uncrypt_file";
Doug Zongker76adfc52014-01-13 10:04:25 -080072
Doug Zongker2efc9d92014-08-18 15:55:28 -070073static struct fstab* fstab = NULL;
74
Tao Bao381f4552015-05-05 18:36:45 -070075static int write_at_offset(unsigned char* buffer, size_t size, int wfd, off64_t offset) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -070076 if (TEMP_FAILURE_RETRY(lseek64(wfd, offset, SEEK_SET)) == -1) {
77 ALOGE("error seeking to offset %lld: %s\n", offset, strerror(errno));
78 return -1;
79 }
Doug Zongker76adfc52014-01-13 10:04:25 -080080 size_t written = 0;
81 while (written < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -070082 ssize_t wrote = TEMP_FAILURE_RETRY(write(wfd, buffer + written, size - written));
83 if (wrote == -1) {
84 ALOGE("error writing offset %lld: %s\n", (offset + written), strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -080085 return -1;
86 }
87 written += wrote;
88 }
89 return 0;
90}
91
Tao Bao381f4552015-05-05 18:36:45 -070092static void add_block_to_ranges(int** ranges, int* range_alloc, int* range_used, int new_block) {
Doug Zongker76adfc52014-01-13 10:04:25 -080093 // If the current block start is < 0, set the start to the new
94 // block. (This only happens for the very first block of the very
95 // first range.)
96 if ((*ranges)[*range_used*2-2] < 0) {
97 (*ranges)[*range_used*2-2] = new_block;
98 (*ranges)[*range_used*2-1] = new_block;
99 }
100
101 if (new_block == (*ranges)[*range_used*2-1]) {
102 // If the new block comes immediately after the current range,
103 // all we have to do is extend the current range.
104 ++(*ranges)[*range_used*2-1];
105 } else {
106 // We need to start a new range.
107
108 // If there isn't enough room in the array, we need to expand it.
109 if (*range_used >= *range_alloc) {
110 *range_alloc *= 2;
Tao Bao381f4552015-05-05 18:36:45 -0700111 *ranges = reinterpret_cast<int*>(realloc(*ranges, *range_alloc * 2 * sizeof(int)));
Doug Zongker76adfc52014-01-13 10:04:25 -0800112 }
113
114 ++*range_used;
115 (*ranges)[*range_used*2-2] = new_block;
116 (*ranges)[*range_used*2-1] = new_block+1;
117 }
118}
119
Tao Bao381f4552015-05-05 18:36:45 -0700120static struct fstab* read_fstab() {
Doug Zongker2efc9d92014-08-18 15:55:28 -0700121 fstab = NULL;
122
Doug Zongker76adfc52014-01-13 10:04:25 -0800123 // The fstab path is always "/fstab.${ro.hardware}".
124 char fstab_path[PATH_MAX+1] = "/fstab.";
125 if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700126 ALOGE("failed to get ro.hardware\n");
Doug Zongker76adfc52014-01-13 10:04:25 -0800127 return NULL;
128 }
129
Doug Zongker2efc9d92014-08-18 15:55:28 -0700130 fstab = fs_mgr_read_fstab(fstab_path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800131 if (!fstab) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700132 ALOGE("failed to read %s\n", fstab_path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800133 return NULL;
134 }
135
Doug Zongker2efc9d92014-08-18 15:55:28 -0700136 return fstab;
137}
138
Tao Bao381f4552015-05-05 18:36:45 -0700139static const char* find_block_device(const char* path, bool* encryptable, bool* encrypted) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800140 // Look for a volume whose mount point is the prefix of path and
141 // return its block device. Set encrypted if it's currently
142 // encrypted.
Tao Bao381f4552015-05-05 18:36:45 -0700143 for (int i = 0; i < fstab->num_entries; ++i) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800144 struct fstab_rec* v = &fstab->recs[i];
Tao Bao381f4552015-05-05 18:36:45 -0700145 if (!v->mount_point) {
146 continue;
147 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800148 int len = strlen(v->mount_point);
149 if (strncmp(path, v->mount_point, len) == 0 &&
150 (path[len] == '/' || path[len] == 0)) {
Tao Bao381f4552015-05-05 18:36:45 -0700151 *encrypted = false;
152 *encryptable = false;
Tao Bao7cf50c62015-07-16 20:04:13 -0700153 if (fs_mgr_is_encryptable(v) || fs_mgr_is_file_encrypted(v)) {
Tao Bao381f4552015-05-05 18:36:45 -0700154 *encryptable = true;
Doug Zongker76adfc52014-01-13 10:04:25 -0800155 char buffer[PROPERTY_VALUE_MAX+1];
156 if (property_get("ro.crypto.state", buffer, "") &&
157 strcmp(buffer, "encrypted") == 0) {
Tao Bao381f4552015-05-05 18:36:45 -0700158 *encrypted = true;
Doug Zongker76adfc52014-01-13 10:04:25 -0800159 }
160 }
161 return v->blk_device;
162 }
163 }
164
165 return NULL;
166}
167
Tao Bao383b00d2015-05-21 16:44:44 -0700168// Parse uncrypt_file to find the update package name.
169static bool find_uncrypt_package(std::string& package_name)
Doug Zongker76adfc52014-01-13 10:04:25 -0800170{
Tao Bao383b00d2015-05-21 16:44:44 -0700171 if (!android::base::ReadFileToString(uncrypt_file, &package_name)) {
172 ALOGE("failed to open \"%s\": %s\n", uncrypt_file.c_str(), strerror(errno));
173 return false;
Maxim Siniavinee7b28882014-02-13 15:48:53 -0800174 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800175
Tao Bao383b00d2015-05-21 16:44:44 -0700176 // Remove the trailing '\n' if present.
177 package_name = android::base::Trim(package_name);
178
179 return true;
Doug Zongker76adfc52014-01-13 10:04:25 -0800180}
181
Tao Bao381f4552015-05-05 18:36:45 -0700182static int produce_block_map(const char* path, const char* map_file, const char* blk_dev,
Tao Bao383b00d2015-05-21 16:44:44 -0700183 bool encrypted, int status_fd) {
Sungmin Choia72512c2014-12-10 21:57:09 +0900184 int mapfd = open(map_file, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR);
Tao Bao383b00d2015-05-21 16:44:44 -0700185 if (mapfd == -1) {
Sungmin Choia72512c2014-12-10 21:57:09 +0900186 ALOGE("failed to open %s\n", map_file);
187 return -1;
188 }
Elliott Hughes63b089e2015-11-12 21:07:55 -0800189 std::unique_ptr<FILE, int(*)(FILE*)> mapf(fdopen(mapfd, "w"), fclose);
Doug Zongker76adfc52014-01-13 10:04:25 -0800190
Tao Bao383b00d2015-05-21 16:44:44 -0700191 // Make sure we can write to the status_file.
192 if (!android::base::WriteStringToFd("0\n", status_fd)) {
193 ALOGE("failed to update \"%s\"\n", status_file.c_str());
194 return -1;
195 }
196
Tao Bao381f4552015-05-05 18:36:45 -0700197 struct stat sb;
Tao Baoc7547922015-08-06 18:35:05 -0700198 if (stat(path, &sb) != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700199 ALOGE("failed to stat %s\n", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800200 return -1;
201 }
202
Doug Zongkerf449db22014-08-26 09:15:08 -0700203 ALOGI(" block size: %ld bytes\n", (long)sb.st_blksize);
Doug Zongker76adfc52014-01-13 10:04:25 -0800204
205 int blocks = ((sb.st_size-1) / sb.st_blksize) + 1;
Doug Zongkerf449db22014-08-26 09:15:08 -0700206 ALOGI(" file size: %lld bytes, %d blocks\n", (long long)sb.st_size, blocks);
Doug Zongker76adfc52014-01-13 10:04:25 -0800207
Doug Zongker76adfc52014-01-13 10:04:25 -0800208 int range_alloc = 1;
209 int range_used = 1;
Tao Bao381f4552015-05-05 18:36:45 -0700210 int* ranges = reinterpret_cast<int*>(malloc(range_alloc * 2 * sizeof(int)));
Doug Zongker76adfc52014-01-13 10:04:25 -0800211 ranges[0] = -1;
212 ranges[1] = -1;
213
Elliott Hughes63b089e2015-11-12 21:07:55 -0800214 fprintf(mapf.get(), "%s\n%lld %lu\n",
215 blk_dev, (long long)sb.st_size, (unsigned long)sb.st_blksize);
Doug Zongker76adfc52014-01-13 10:04:25 -0800216
217 unsigned char* buffers[WINDOW_SIZE];
Doug Zongker76adfc52014-01-13 10:04:25 -0800218 if (encrypted) {
Tao Bao381f4552015-05-05 18:36:45 -0700219 for (size_t i = 0; i < WINDOW_SIZE; ++i) {
220 buffers[i] = reinterpret_cast<unsigned char*>(malloc(sb.st_blksize));
Doug Zongker76adfc52014-01-13 10:04:25 -0800221 }
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);
Tao Baoc7547922015-08-06 18:35:05 -0700228 unique_fd fd_holder(fd);
229 if (fd == -1) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700230 ALOGE("failed to open fd for reading: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800231 return -1;
232 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800233
234 int wfd = -1;
Tao Baoc7547922015-08-06 18:35:05 -0700235 unique_fd wfd_holder(wfd);
Doug Zongker76adfc52014-01-13 10:04:25 -0800236 if (encrypted) {
Jaegeuk Kimcc4e3c62015-11-04 11:43:58 -0800237 wfd = open(blk_dev, O_WRONLY);
Tao Baoc7547922015-08-06 18:35:05 -0700238 wfd_holder = unique_fd(wfd);
239 if (wfd == -1) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700240 ALOGE("failed to open fd for writing: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800241 return -1;
242 }
243 }
244
Tao Bao383b00d2015-05-21 16:44:44 -0700245 int last_progress = 0;
Doug Zongker76adfc52014-01-13 10:04:25 -0800246 while (pos < sb.st_size) {
Tao Bao383b00d2015-05-21 16:44:44 -0700247 // Update the status file, progress must be between [0, 99].
248 int progress = static_cast<int>(100 * (double(pos) / double(sb.st_size)));
249 if (progress > last_progress) {
250 last_progress = progress;
251 android::base::WriteStringToFd(std::to_string(progress) + "\n", status_fd);
252 }
253
Doug Zongker76adfc52014-01-13 10:04:25 -0800254 if ((tail+1) % WINDOW_SIZE == head) {
255 // write out head buffer
256 int block = head_block;
Tao Baoc7547922015-08-06 18:35:05 -0700257 if (ioctl(fd, FIBMAP, &block) != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700258 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800259 return -1;
260 }
261 add_block_to_ranges(&ranges, &range_alloc, &range_used, block);
262 if (encrypted) {
Tao Bao381f4552015-05-05 18:36:45 -0700263 if (write_at_offset(buffers[head], sb.st_blksize, wfd,
264 (off64_t)sb.st_blksize * block) != 0) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800265 return -1;
266 }
267 }
268 head = (head + 1) % WINDOW_SIZE;
269 ++head_block;
270 }
271
272 // read next block to tail
273 if (encrypted) {
274 size_t so_far = 0;
275 while (so_far < sb.st_blksize && pos < sb.st_size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700276 ssize_t this_read =
277 TEMP_FAILURE_RETRY(read(fd, buffers[tail] + so_far, sb.st_blksize - so_far));
278 if (this_read == -1) {
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;
Tao Baoc7547922015-08-06 18:35:05 -0700297 if (ioctl(fd, FIBMAP, &block) != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700298 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800299 return -1;
300 }
301 add_block_to_ranges(&ranges, &range_alloc, &range_used, block);
302 if (encrypted) {
Tao Bao381f4552015-05-05 18:36:45 -0700303 if (write_at_offset(buffers[head], sb.st_blksize, wfd,
304 (off64_t)sb.st_blksize * block) != 0) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800305 return -1;
306 }
307 }
308 head = (head + 1) % WINDOW_SIZE;
309 ++head_block;
310 }
311
Elliott Hughes63b089e2015-11-12 21:07:55 -0800312 fprintf(mapf.get(), "%d\n", range_used);
Tao Bao381f4552015-05-05 18:36:45 -0700313 for (int i = 0; i < range_used; ++i) {
Elliott Hughes63b089e2015-11-12 21:07:55 -0800314 fprintf(mapf.get(), "%d %d\n", ranges[i*2], ranges[i*2+1]);
Doug Zongker76adfc52014-01-13 10:04:25 -0800315 }
316
Tao Baofb4ccef2015-05-04 10:10:13 -0700317 if (fsync(mapfd) == -1) {
318 ALOGE("failed to fsync \"%s\": %s\n", map_file, strerror(errno));
319 return -1;
320 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800321 if (encrypted) {
Tao Baofb4ccef2015-05-04 10:10:13 -0700322 if (fsync(wfd) == -1) {
323 ALOGE("failed to fsync \"%s\": %s\n", blk_dev, strerror(errno));
324 return -1;
325 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800326 }
327
328 return 0;
329}
330
Tao Bao381f4552015-05-05 18:36:45 -0700331static void wipe_misc() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700332 ALOGI("removing old commands from misc");
Tao Bao381f4552015-05-05 18:36:45 -0700333 for (int i = 0; i < fstab->num_entries; ++i) {
Doug Zongker2efc9d92014-08-18 15:55:28 -0700334 struct fstab_rec* v = &fstab->recs[i];
335 if (!v->mount_point) continue;
336 if (strcmp(v->mount_point, "/misc") == 0) {
Michael Runge4b542392014-11-21 16:00:45 -0800337 int fd = open(v->blk_device, O_WRONLY | O_SYNC);
Tao Baoc7547922015-08-06 18:35:05 -0700338 unique_fd fd_holder(fd);
339
Doug Zongker2efc9d92014-08-18 15:55:28 -0700340 uint8_t zeroes[1088]; // sizeof(bootloader_message) from recovery
341 memset(zeroes, 0, sizeof(zeroes));
342
343 size_t written = 0;
344 size_t size = sizeof(zeroes);
345 while (written < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700346 ssize_t w = TEMP_FAILURE_RETRY(write(fd, zeroes, size-written));
347 if (w == -1) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700348 ALOGE("zero write failed: %s\n", strerror(errno));
Doug Zongker2efc9d92014-08-18 15:55:28 -0700349 return;
350 } else {
351 written += w;
352 }
353 }
Tao Baofb4ccef2015-05-04 10:10:13 -0700354 if (fsync(fd) == -1) {
355 ALOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno));
Tao Baofb4ccef2015-05-04 10:10:13 -0700356 return;
357 }
Doug Zongker2efc9d92014-08-18 15:55:28 -0700358 }
359 }
360}
361
Tao Bao381f4552015-05-05 18:36:45 -0700362static void reboot_to_recovery() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700363 ALOGI("rebooting to recovery");
Doug Zongker76adfc52014-01-13 10:04:25 -0800364 property_set("sys.powerctl", "reboot,recovery");
Tao Bao75238632015-05-27 14:46:17 -0700365 while (true) {
366 pause();
367 }
Doug Zongkerf449db22014-08-26 09:15:08 -0700368 ALOGE("reboot didn't succeed?");
Doug Zongker76adfc52014-01-13 10:04:25 -0800369}
370
Tao Bao383b00d2015-05-21 16:44:44 -0700371int uncrypt(const char* input_path, const char* map_file, int status_fd) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800372
Tao Bao383b00d2015-05-21 16:44:44 -0700373 ALOGI("update package is \"%s\"", input_path);
Doug Zongkerf449db22014-08-26 09:15:08 -0700374
Doug Zongker76adfc52014-01-13 10:04:25 -0800375 // Turn the name of the file we're supposed to convert into an
376 // absolute path, so we can find what filesystem it's on.
377 char path[PATH_MAX+1];
378 if (realpath(input_path, path) == NULL) {
Tao Bao383b00d2015-05-21 16:44:44 -0700379 ALOGE("failed to convert \"%s\" to absolute path: %s", input_path, strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800380 return 1;
381 }
382
Doug Zongker2efc9d92014-08-18 15:55:28 -0700383 if (read_fstab() == NULL) {
384 return 1;
385 }
Tao Bao381f4552015-05-05 18:36:45 -0700386
387 bool encryptable;
388 bool encrypted;
Doug Zongker76adfc52014-01-13 10:04:25 -0800389 const char* blk_dev = find_block_device(path, &encryptable, &encrypted);
390 if (blk_dev == NULL) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700391 ALOGE("failed to find block device for %s", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800392 return 1;
393 }
394
395 // If the filesystem it's on isn't encrypted, we only produce the
396 // block map, we don't rewrite the file contents (it would be
397 // pointless to do so).
Doug Zongkerf449db22014-08-26 09:15:08 -0700398 ALOGI("encryptable: %s\n", encryptable ? "yes" : "no");
399 ALOGI(" encrypted: %s\n", encrypted ? "yes" : "no");
Doug Zongker76adfc52014-01-13 10:04:25 -0800400
Doug Zongker574443d2014-09-05 08:22:12 -0700401 // Recovery supports installing packages from 3 paths: /cache,
402 // /data, and /sdcard. (On a particular device, other locations
403 // may work, but those are three we actually expect.)
404 //
405 // On /data we want to convert the file to a block map so that we
406 // can read the package without mounting the partition. On /cache
407 // and /sdcard we leave the file alone.
Tao Bao383b00d2015-05-21 16:44:44 -0700408 if (strncmp(path, "/data/", 6) == 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700409 ALOGI("writing block map %s", map_file);
Tao Bao383b00d2015-05-21 16:44:44 -0700410 if (produce_block_map(path, map_file, blk_dev, encrypted, status_fd) != 0) {
411 return 1;
412 }
413 }
414
415 return 0;
416}
417
418int main(int argc, char** argv) {
419 const char* input_path;
420 const char* map_file;
421
422 if (argc != 3 && argc != 1 && (argc == 2 && strcmp(argv[1], "--reboot") != 0)) {
423 fprintf(stderr, "usage: %s [--reboot] [<transform_path> <map_file>]\n", argv[0]);
424 return 2;
425 }
426
427 // When uncrypt is started with "--reboot", it wipes misc and reboots.
428 // Otherwise it uncrypts the package and writes the block map.
429 if (argc == 2) {
430 if (read_fstab() == NULL) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800431 return 1;
432 }
Tao Baofb4ccef2015-05-04 10:10:13 -0700433 wipe_misc();
Tao Bao383b00d2015-05-21 16:44:44 -0700434 reboot_to_recovery();
435 } else {
Tao Bao383b00d2015-05-21 16:44:44 -0700436 // The pipe has been created by the system server.
437 int status_fd = open(status_file.c_str(), O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR);
438 if (status_fd == -1) {
439 ALOGE("failed to open pipe \"%s\": %s\n", status_file.c_str(), strerror(errno));
440 return 1;
441 }
Tao Baoc7547922015-08-06 18:35:05 -0700442 unique_fd status_fd_holder(status_fd);
Tao Baoac6aa7e2015-05-29 14:24:02 -0700443
444 if (argc == 3) {
445 // when command-line args are given this binary is being used
446 // for debugging.
447 input_path = argv[1];
448 map_file = argv[2];
449 } else {
450 std::string package;
451 if (!find_uncrypt_package(package)) {
452 android::base::WriteStringToFd("-1\n", status_fd);
Tao Baoac6aa7e2015-05-29 14:24:02 -0700453 return 1;
454 }
455 input_path = package.c_str();
456 map_file = cache_block_map.c_str();
457 }
458
Tao Bao383b00d2015-05-21 16:44:44 -0700459 int status = uncrypt(input_path, map_file, status_fd);
460 if (status != 0) {
461 android::base::WriteStringToFd("-1\n", status_fd);
Tao Bao383b00d2015-05-21 16:44:44 -0700462 return 1;
463 }
464
465 android::base::WriteStringToFd("100\n", status_fd);
Doug Zongker76adfc52014-01-13 10:04:25 -0800466 }
467
Doug Zongker76adfc52014-01-13 10:04:25 -0800468 return 0;
469}