blob: 8785b29afaa2184f6799a009cc565b0b59196c6e [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 Bao158e11d2015-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 Bao158e11d2015-05-21 16:44:44 -070054#include <base/file.h>
55#include <base/strings.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
Tao Bao158e11d2015-05-21 16:44:44 -070060#define LOG_TAG "uncrypt"
61#include <log/log.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080062
63#define WINDOW_SIZE 5
Tao Bao158e11d2015-05-21 16:44:44 -070064
65static const std::string cache_block_map = "/cache/recovery/block.map";
66static const std::string status_file = "/cache/recovery/uncrypt_status";
67static const std::string uncrypt_file = "/cache/recovery/uncrypt_file";
Doug Zongker76adfc52014-01-13 10:04:25 -080068
Doug Zongker2efc9d92014-08-18 15:55:28 -070069static struct fstab* fstab = NULL;
70
Tao Bao381f4552015-05-05 18:36:45 -070071static int write_at_offset(unsigned char* buffer, size_t size, int wfd, off64_t offset) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -070072 if (TEMP_FAILURE_RETRY(lseek64(wfd, offset, SEEK_SET)) == -1) {
73 ALOGE("error seeking to offset %lld: %s\n", offset, strerror(errno));
74 return -1;
75 }
Doug Zongker76adfc52014-01-13 10:04:25 -080076 size_t written = 0;
77 while (written < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -070078 ssize_t wrote = TEMP_FAILURE_RETRY(write(wfd, buffer + written, size - written));
79 if (wrote == -1) {
80 ALOGE("error writing offset %lld: %s\n", (offset + written), strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -080081 return -1;
82 }
83 written += wrote;
84 }
85 return 0;
86}
87
Tao Bao381f4552015-05-05 18:36:45 -070088static void add_block_to_ranges(int** ranges, int* range_alloc, int* range_used, int new_block) {
Doug Zongker76adfc52014-01-13 10:04:25 -080089 // If the current block start is < 0, set the start to the new
90 // block. (This only happens for the very first block of the very
91 // first range.)
92 if ((*ranges)[*range_used*2-2] < 0) {
93 (*ranges)[*range_used*2-2] = new_block;
94 (*ranges)[*range_used*2-1] = new_block;
95 }
96
97 if (new_block == (*ranges)[*range_used*2-1]) {
98 // If the new block comes immediately after the current range,
99 // all we have to do is extend the current range.
100 ++(*ranges)[*range_used*2-1];
101 } else {
102 // We need to start a new range.
103
104 // If there isn't enough room in the array, we need to expand it.
105 if (*range_used >= *range_alloc) {
106 *range_alloc *= 2;
Tao Bao381f4552015-05-05 18:36:45 -0700107 *ranges = reinterpret_cast<int*>(realloc(*ranges, *range_alloc * 2 * sizeof(int)));
Doug Zongker76adfc52014-01-13 10:04:25 -0800108 }
109
110 ++*range_used;
111 (*ranges)[*range_used*2-2] = new_block;
112 (*ranges)[*range_used*2-1] = new_block+1;
113 }
114}
115
Tao Bao381f4552015-05-05 18:36:45 -0700116static struct fstab* read_fstab() {
Doug Zongker2efc9d92014-08-18 15:55:28 -0700117 fstab = NULL;
118
Doug Zongker76adfc52014-01-13 10:04:25 -0800119 // The fstab path is always "/fstab.${ro.hardware}".
120 char fstab_path[PATH_MAX+1] = "/fstab.";
121 if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700122 ALOGE("failed to get ro.hardware\n");
Doug Zongker76adfc52014-01-13 10:04:25 -0800123 return NULL;
124 }
125
Doug Zongker2efc9d92014-08-18 15:55:28 -0700126 fstab = fs_mgr_read_fstab(fstab_path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800127 if (!fstab) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700128 ALOGE("failed to read %s\n", fstab_path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800129 return NULL;
130 }
131
Doug Zongker2efc9d92014-08-18 15:55:28 -0700132 return fstab;
133}
134
Tao Bao381f4552015-05-05 18:36:45 -0700135static const char* find_block_device(const char* path, bool* encryptable, bool* encrypted) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800136 // Look for a volume whose mount point is the prefix of path and
137 // return its block device. Set encrypted if it's currently
138 // encrypted.
Tao Bao381f4552015-05-05 18:36:45 -0700139 for (int i = 0; i < fstab->num_entries; ++i) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800140 struct fstab_rec* v = &fstab->recs[i];
Tao Bao381f4552015-05-05 18:36:45 -0700141 if (!v->mount_point) {
142 continue;
143 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800144 int len = strlen(v->mount_point);
145 if (strncmp(path, v->mount_point, len) == 0 &&
146 (path[len] == '/' || path[len] == 0)) {
Tao Bao381f4552015-05-05 18:36:45 -0700147 *encrypted = false;
148 *encryptable = false;
Tao Bao6e9dda72015-07-16 20:04:13 -0700149 if (fs_mgr_is_encryptable(v) || fs_mgr_is_file_encrypted(v)) {
Tao Bao381f4552015-05-05 18:36:45 -0700150 *encryptable = true;
Doug Zongker76adfc52014-01-13 10:04:25 -0800151 char buffer[PROPERTY_VALUE_MAX+1];
152 if (property_get("ro.crypto.state", buffer, "") &&
153 strcmp(buffer, "encrypted") == 0) {
Tao Bao381f4552015-05-05 18:36:45 -0700154 *encrypted = true;
Doug Zongker76adfc52014-01-13 10:04:25 -0800155 }
156 }
157 return v->blk_device;
158 }
159 }
160
161 return NULL;
162}
163
Tao Bao158e11d2015-05-21 16:44:44 -0700164// Parse uncrypt_file to find the update package name.
165static bool find_uncrypt_package(std::string& package_name)
Doug Zongker76adfc52014-01-13 10:04:25 -0800166{
Tao Bao158e11d2015-05-21 16:44:44 -0700167 if (!android::base::ReadFileToString(uncrypt_file, &package_name)) {
168 ALOGE("failed to open \"%s\": %s\n", uncrypt_file.c_str(), strerror(errno));
169 return false;
Maxim Siniavinee7b28882014-02-13 15:48:53 -0800170 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800171
Tao Bao158e11d2015-05-21 16:44:44 -0700172 // Remove the trailing '\n' if present.
173 package_name = android::base::Trim(package_name);
174
175 return true;
Doug Zongker76adfc52014-01-13 10:04:25 -0800176}
177
Tao Bao381f4552015-05-05 18:36:45 -0700178static int produce_block_map(const char* path, const char* map_file, const char* blk_dev,
Tao Bao158e11d2015-05-21 16:44:44 -0700179 bool encrypted, int status_fd) {
Sungmin Choia72512c2014-12-10 21:57:09 +0900180 int mapfd = open(map_file, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR);
Tao Bao158e11d2015-05-21 16:44:44 -0700181 if (mapfd == -1) {
Sungmin Choia72512c2014-12-10 21:57:09 +0900182 ALOGE("failed to open %s\n", map_file);
183 return -1;
184 }
Michael Runge4b542392014-11-21 16:00:45 -0800185 FILE* mapf = fdopen(mapfd, "w");
Doug Zongker76adfc52014-01-13 10:04:25 -0800186
Tao Bao158e11d2015-05-21 16:44:44 -0700187 // Make sure we can write to the status_file.
188 if (!android::base::WriteStringToFd("0\n", status_fd)) {
189 ALOGE("failed to update \"%s\"\n", status_file.c_str());
190 return -1;
191 }
192
Tao Bao381f4552015-05-05 18:36:45 -0700193 struct stat sb;
194 int ret = stat(path, &sb);
Doug Zongker76adfc52014-01-13 10:04:25 -0800195 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700196 ALOGE("failed to stat %s\n", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800197 return -1;
198 }
199
Doug Zongkerf449db22014-08-26 09:15:08 -0700200 ALOGI(" block size: %ld bytes\n", (long)sb.st_blksize);
Doug Zongker76adfc52014-01-13 10:04:25 -0800201
202 int blocks = ((sb.st_size-1) / sb.st_blksize) + 1;
Doug Zongkerf449db22014-08-26 09:15:08 -0700203 ALOGI(" file size: %lld bytes, %d blocks\n", (long long)sb.st_size, blocks);
Doug Zongker76adfc52014-01-13 10:04:25 -0800204
Doug Zongker76adfc52014-01-13 10:04:25 -0800205 int range_alloc = 1;
206 int range_used = 1;
Tao Bao381f4552015-05-05 18:36:45 -0700207 int* ranges = reinterpret_cast<int*>(malloc(range_alloc * 2 * sizeof(int)));
Doug Zongker76adfc52014-01-13 10:04:25 -0800208 ranges[0] = -1;
209 ranges[1] = -1;
210
Mark Salyzyn2605dec2014-03-19 15:30:25 -0700211 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 -0800212
213 unsigned char* buffers[WINDOW_SIZE];
Doug Zongker76adfc52014-01-13 10:04:25 -0800214 if (encrypted) {
Tao Bao381f4552015-05-05 18:36:45 -0700215 for (size_t i = 0; i < WINDOW_SIZE; ++i) {
216 buffers[i] = reinterpret_cast<unsigned char*>(malloc(sb.st_blksize));
Doug Zongker76adfc52014-01-13 10:04:25 -0800217 }
218 }
219 int head_block = 0;
220 int head = 0, tail = 0;
221 size_t pos = 0;
222
223 int fd = open(path, O_RDONLY);
224 if (fd < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700225 ALOGE("failed to open fd for reading: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800226 return -1;
227 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800228
229 int wfd = -1;
230 if (encrypted) {
Michael Runge4b542392014-11-21 16:00:45 -0800231 wfd = open(blk_dev, O_WRONLY | O_SYNC);
Doug Zongker76adfc52014-01-13 10:04:25 -0800232 if (wfd < 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700233 ALOGE("failed to open fd for writing: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800234 return -1;
235 }
236 }
237
Tao Bao158e11d2015-05-21 16:44:44 -0700238 int last_progress = 0;
Doug Zongker76adfc52014-01-13 10:04:25 -0800239 while (pos < sb.st_size) {
Tao Bao158e11d2015-05-21 16:44:44 -0700240 // Update the status file, progress must be between [0, 99].
241 int progress = static_cast<int>(100 * (double(pos) / double(sb.st_size)));
242 if (progress > last_progress) {
243 last_progress = progress;
244 android::base::WriteStringToFd(std::to_string(progress) + "\n", status_fd);
245 }
246
Doug Zongker76adfc52014-01-13 10:04:25 -0800247 if ((tail+1) % WINDOW_SIZE == head) {
248 // write out head buffer
249 int block = head_block;
250 ret = ioctl(fd, FIBMAP, &block);
251 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700252 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800253 return -1;
254 }
255 add_block_to_ranges(&ranges, &range_alloc, &range_used, block);
256 if (encrypted) {
Tao Bao381f4552015-05-05 18:36:45 -0700257 if (write_at_offset(buffers[head], sb.st_blksize, wfd,
258 (off64_t)sb.st_blksize * block) != 0) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800259 return -1;
260 }
261 }
262 head = (head + 1) % WINDOW_SIZE;
263 ++head_block;
264 }
265
266 // read next block to tail
267 if (encrypted) {
268 size_t so_far = 0;
269 while (so_far < sb.st_blksize && pos < sb.st_size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700270 ssize_t this_read =
271 TEMP_FAILURE_RETRY(read(fd, buffers[tail] + so_far, sb.st_blksize - so_far));
272 if (this_read == -1) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700273 ALOGE("failed to read: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800274 return -1;
275 }
276 so_far += this_read;
277 pos += this_read;
278 }
279 } else {
280 // If we're not encrypting; we don't need to actually read
281 // anything, just skip pos forward as if we'd read a
282 // block.
283 pos += sb.st_blksize;
284 }
285 tail = (tail+1) % WINDOW_SIZE;
286 }
287
288 while (head != tail) {
289 // write out head buffer
290 int block = head_block;
291 ret = ioctl(fd, FIBMAP, &block);
292 if (ret != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700293 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800294 return -1;
295 }
296 add_block_to_ranges(&ranges, &range_alloc, &range_used, block);
297 if (encrypted) {
Tao Bao381f4552015-05-05 18:36:45 -0700298 if (write_at_offset(buffers[head], sb.st_blksize, wfd,
299 (off64_t)sb.st_blksize * block) != 0) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800300 return -1;
301 }
302 }
303 head = (head + 1) % WINDOW_SIZE;
304 ++head_block;
305 }
306
307 fprintf(mapf, "%d\n", range_used);
Tao Bao381f4552015-05-05 18:36:45 -0700308 for (int i = 0; i < range_used; ++i) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800309 fprintf(mapf, "%d %d\n", ranges[i*2], ranges[i*2+1]);
310 }
311
Tao Baofb4ccef2015-05-04 10:10:13 -0700312 if (fsync(mapfd) == -1) {
313 ALOGE("failed to fsync \"%s\": %s\n", map_file, strerror(errno));
314 return -1;
315 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800316 fclose(mapf);
317 close(fd);
318 if (encrypted) {
Tao Baofb4ccef2015-05-04 10:10:13 -0700319 if (fsync(wfd) == -1) {
320 ALOGE("failed to fsync \"%s\": %s\n", blk_dev, strerror(errno));
321 return -1;
322 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800323 close(wfd);
324 }
325
326 return 0;
327}
328
Tao Bao381f4552015-05-05 18:36:45 -0700329static void wipe_misc() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700330 ALOGI("removing old commands from misc");
Tao Bao381f4552015-05-05 18:36:45 -0700331 for (int i = 0; i < fstab->num_entries; ++i) {
Doug Zongker2efc9d92014-08-18 15:55:28 -0700332 struct fstab_rec* v = &fstab->recs[i];
333 if (!v->mount_point) continue;
334 if (strcmp(v->mount_point, "/misc") == 0) {
Michael Runge4b542392014-11-21 16:00:45 -0800335 int fd = open(v->blk_device, O_WRONLY | O_SYNC);
Doug Zongker2efc9d92014-08-18 15:55:28 -0700336 uint8_t zeroes[1088]; // sizeof(bootloader_message) from recovery
337 memset(zeroes, 0, sizeof(zeroes));
338
339 size_t written = 0;
340 size_t size = sizeof(zeroes);
341 while (written < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700342 ssize_t w = TEMP_FAILURE_RETRY(write(fd, zeroes, size-written));
343 if (w == -1) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700344 ALOGE("zero write failed: %s\n", strerror(errno));
Doug Zongker2efc9d92014-08-18 15:55:28 -0700345 return;
346 } else {
347 written += w;
348 }
349 }
Tao Baofb4ccef2015-05-04 10:10:13 -0700350 if (fsync(fd) == -1) {
351 ALOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno));
352 close(fd);
353 return;
354 }
Doug Zongker2efc9d92014-08-18 15:55:28 -0700355 close(fd);
356 }
357 }
358}
359
Tao Bao381f4552015-05-05 18:36:45 -0700360static void reboot_to_recovery() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700361 ALOGI("rebooting to recovery");
Doug Zongker76adfc52014-01-13 10:04:25 -0800362 property_set("sys.powerctl", "reboot,recovery");
Tao Bao75238632015-05-27 14:46:17 -0700363 while (true) {
364 pause();
365 }
Doug Zongkerf449db22014-08-26 09:15:08 -0700366 ALOGE("reboot didn't succeed?");
Doug Zongker76adfc52014-01-13 10:04:25 -0800367}
368
Tao Bao158e11d2015-05-21 16:44:44 -0700369int uncrypt(const char* input_path, const char* map_file, int status_fd) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800370
Tao Bao158e11d2015-05-21 16:44:44 -0700371 ALOGI("update package is \"%s\"", input_path);
Doug Zongkerf449db22014-08-26 09:15:08 -0700372
Doug Zongker76adfc52014-01-13 10:04:25 -0800373 // Turn the name of the file we're supposed to convert into an
374 // absolute path, so we can find what filesystem it's on.
375 char path[PATH_MAX+1];
376 if (realpath(input_path, path) == NULL) {
Tao Bao158e11d2015-05-21 16:44:44 -0700377 ALOGE("failed to convert \"%s\" to absolute path: %s", input_path, strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800378 return 1;
379 }
380
Doug Zongker2efc9d92014-08-18 15:55:28 -0700381 if (read_fstab() == NULL) {
382 return 1;
383 }
Tao Bao381f4552015-05-05 18:36:45 -0700384
385 bool encryptable;
386 bool encrypted;
Doug Zongker76adfc52014-01-13 10:04:25 -0800387 const char* blk_dev = find_block_device(path, &encryptable, &encrypted);
388 if (blk_dev == NULL) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700389 ALOGE("failed to find block device for %s", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800390 return 1;
391 }
392
393 // If the filesystem it's on isn't encrypted, we only produce the
394 // block map, we don't rewrite the file contents (it would be
395 // pointless to do so).
Doug Zongkerf449db22014-08-26 09:15:08 -0700396 ALOGI("encryptable: %s\n", encryptable ? "yes" : "no");
397 ALOGI(" encrypted: %s\n", encrypted ? "yes" : "no");
Doug Zongker76adfc52014-01-13 10:04:25 -0800398
Doug Zongker574443d2014-09-05 08:22:12 -0700399 // Recovery supports installing packages from 3 paths: /cache,
400 // /data, and /sdcard. (On a particular device, other locations
401 // may work, but those are three we actually expect.)
402 //
403 // On /data we want to convert the file to a block map so that we
404 // can read the package without mounting the partition. On /cache
405 // and /sdcard we leave the file alone.
Tao Bao158e11d2015-05-21 16:44:44 -0700406 if (strncmp(path, "/data/", 6) == 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700407 ALOGI("writing block map %s", map_file);
Tao Bao158e11d2015-05-21 16:44:44 -0700408 if (produce_block_map(path, map_file, blk_dev, encrypted, status_fd) != 0) {
409 return 1;
410 }
411 }
412
413 return 0;
414}
415
416int main(int argc, char** argv) {
417 const char* input_path;
418 const char* map_file;
419
420 if (argc != 3 && argc != 1 && (argc == 2 && strcmp(argv[1], "--reboot") != 0)) {
421 fprintf(stderr, "usage: %s [--reboot] [<transform_path> <map_file>]\n", argv[0]);
422 return 2;
423 }
424
425 // When uncrypt is started with "--reboot", it wipes misc and reboots.
426 // Otherwise it uncrypts the package and writes the block map.
427 if (argc == 2) {
428 if (read_fstab() == NULL) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800429 return 1;
430 }
Tao Baofb4ccef2015-05-04 10:10:13 -0700431 wipe_misc();
Tao Bao158e11d2015-05-21 16:44:44 -0700432 reboot_to_recovery();
433 } else {
Tao Bao158e11d2015-05-21 16:44:44 -0700434 // The pipe has been created by the system server.
435 int status_fd = open(status_file.c_str(), O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR);
436 if (status_fd == -1) {
437 ALOGE("failed to open pipe \"%s\": %s\n", status_file.c_str(), strerror(errno));
438 return 1;
439 }
Tao Bao2c2cae82015-05-29 14:24:02 -0700440
441 if (argc == 3) {
442 // when command-line args are given this binary is being used
443 // for debugging.
444 input_path = argv[1];
445 map_file = argv[2];
446 } else {
447 std::string package;
448 if (!find_uncrypt_package(package)) {
449 android::base::WriteStringToFd("-1\n", status_fd);
450 close(status_fd);
451 return 1;
452 }
453 input_path = package.c_str();
454 map_file = cache_block_map.c_str();
455 }
456
Tao Bao158e11d2015-05-21 16:44:44 -0700457 int status = uncrypt(input_path, map_file, status_fd);
458 if (status != 0) {
459 android::base::WriteStringToFd("-1\n", status_fd);
460 close(status_fd);
461 return 1;
462 }
463
464 android::base::WriteStringToFd("100\n", status_fd);
465 close(status_fd);
Doug Zongker76adfc52014-01-13 10:04:25 -0800466 }
467
Doug Zongker76adfc52014-01-13 10:04:25 -0800468 return 0;
469}