blob: de7e48182834db1b7ad6f0beebb09d2a314cbcb2 [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>
Tao Baob8df5fb2015-12-08 22:47:25 -080044#include <inttypes.h>
Tao Bao75238632015-05-27 14:46:17 -070045#include <linux/fs.h>
46#include <stdarg.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080047#include <stdio.h>
48#include <stdlib.h>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080049#include <string.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080050#include <sys/mman.h>
Tao Bao383b00d2015-05-21 16:44:44 -070051#include <sys/stat.h>
52#include <sys/types.h>
Tao Bao75238632015-05-27 14:46:17 -070053#include <unistd.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080054
Tao Baoc7547922015-08-06 18:35:05 -070055#include <memory>
56
Elliott Hughes4b166f02015-12-04 15:30:20 -080057#include <android-base/file.h>
58#include <android-base/strings.h>
Tao Bao75238632015-05-27 14:46:17 -070059#include <cutils/android_reboot.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080060#include <cutils/properties.h>
61#include <fs_mgr.h>
62
Tao Bao383b00d2015-05-21 16:44:44 -070063#define LOG_TAG "uncrypt"
64#include <log/log.h>
65
Tao Baoc7547922015-08-06 18:35:05 -070066#include "unique_fd.h"
67
Doug Zongker76adfc52014-01-13 10:04:25 -080068#define WINDOW_SIZE 5
Tao Bao383b00d2015-05-21 16:44:44 -070069
70static const std::string cache_block_map = "/cache/recovery/block.map";
71static const std::string status_file = "/cache/recovery/uncrypt_status";
72static const std::string uncrypt_file = "/cache/recovery/uncrypt_file";
Doug Zongker76adfc52014-01-13 10:04:25 -080073
Doug Zongker2efc9d92014-08-18 15:55:28 -070074static struct fstab* fstab = NULL;
75
Tao Bao381f4552015-05-05 18:36:45 -070076static int write_at_offset(unsigned char* buffer, size_t size, int wfd, off64_t offset) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -070077 if (TEMP_FAILURE_RETRY(lseek64(wfd, offset, SEEK_SET)) == -1) {
Tao Baob8df5fb2015-12-08 22:47:25 -080078 ALOGE("error seeking to offset %" PRId64 ": %s\n", offset, strerror(errno));
Elliott Hughes7bad7c42015-04-28 17:24:24 -070079 return -1;
80 }
Doug Zongker76adfc52014-01-13 10:04:25 -080081 size_t written = 0;
82 while (written < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -070083 ssize_t wrote = TEMP_FAILURE_RETRY(write(wfd, buffer + written, size - written));
84 if (wrote == -1) {
Tao Baob8df5fb2015-12-08 22:47:25 -080085 ALOGE("error writing offset %" PRId64 ": %s\n",
86 offset + static_cast<off64_t>(written), strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -080087 return -1;
88 }
89 written += wrote;
90 }
91 return 0;
92}
93
Tao Bao381f4552015-05-05 18:36:45 -070094static void add_block_to_ranges(int** ranges, int* range_alloc, int* range_used, int new_block) {
Doug Zongker76adfc52014-01-13 10:04:25 -080095 // If the current block start is < 0, set the start to the new
96 // block. (This only happens for the very first block of the very
97 // first range.)
98 if ((*ranges)[*range_used*2-2] < 0) {
99 (*ranges)[*range_used*2-2] = new_block;
100 (*ranges)[*range_used*2-1] = new_block;
101 }
102
103 if (new_block == (*ranges)[*range_used*2-1]) {
104 // If the new block comes immediately after the current range,
105 // all we have to do is extend the current range.
106 ++(*ranges)[*range_used*2-1];
107 } else {
108 // We need to start a new range.
109
110 // If there isn't enough room in the array, we need to expand it.
111 if (*range_used >= *range_alloc) {
112 *range_alloc *= 2;
Tao Bao381f4552015-05-05 18:36:45 -0700113 *ranges = reinterpret_cast<int*>(realloc(*ranges, *range_alloc * 2 * sizeof(int)));
Doug Zongker76adfc52014-01-13 10:04:25 -0800114 }
115
116 ++*range_used;
117 (*ranges)[*range_used*2-2] = new_block;
118 (*ranges)[*range_used*2-1] = new_block+1;
119 }
120}
121
Tao Bao381f4552015-05-05 18:36:45 -0700122static struct fstab* read_fstab() {
Doug Zongker2efc9d92014-08-18 15:55:28 -0700123 fstab = NULL;
124
Doug Zongker76adfc52014-01-13 10:04:25 -0800125 // The fstab path is always "/fstab.${ro.hardware}".
126 char fstab_path[PATH_MAX+1] = "/fstab.";
127 if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700128 ALOGE("failed to get ro.hardware\n");
Doug Zongker76adfc52014-01-13 10:04:25 -0800129 return NULL;
130 }
131
Doug Zongker2efc9d92014-08-18 15:55:28 -0700132 fstab = fs_mgr_read_fstab(fstab_path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800133 if (!fstab) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700134 ALOGE("failed to read %s\n", fstab_path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800135 return NULL;
136 }
137
Doug Zongker2efc9d92014-08-18 15:55:28 -0700138 return fstab;
139}
140
Tao Bao381f4552015-05-05 18:36:45 -0700141static const char* find_block_device(const char* path, bool* encryptable, bool* encrypted) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800142 // Look for a volume whose mount point is the prefix of path and
143 // return its block device. Set encrypted if it's currently
144 // encrypted.
Tao Bao381f4552015-05-05 18:36:45 -0700145 for (int i = 0; i < fstab->num_entries; ++i) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800146 struct fstab_rec* v = &fstab->recs[i];
Tao Bao381f4552015-05-05 18:36:45 -0700147 if (!v->mount_point) {
148 continue;
149 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800150 int len = strlen(v->mount_point);
151 if (strncmp(path, v->mount_point, len) == 0 &&
152 (path[len] == '/' || path[len] == 0)) {
Tao Bao381f4552015-05-05 18:36:45 -0700153 *encrypted = false;
154 *encryptable = false;
Tao Bao7cf50c62015-07-16 20:04:13 -0700155 if (fs_mgr_is_encryptable(v) || fs_mgr_is_file_encrypted(v)) {
Tao Bao381f4552015-05-05 18:36:45 -0700156 *encryptable = true;
Doug Zongker76adfc52014-01-13 10:04:25 -0800157 char buffer[PROPERTY_VALUE_MAX+1];
158 if (property_get("ro.crypto.state", buffer, "") &&
159 strcmp(buffer, "encrypted") == 0) {
Tao Bao381f4552015-05-05 18:36:45 -0700160 *encrypted = true;
Doug Zongker76adfc52014-01-13 10:04:25 -0800161 }
162 }
163 return v->blk_device;
164 }
165 }
166
167 return NULL;
168}
169
Tao Bao383b00d2015-05-21 16:44:44 -0700170// Parse uncrypt_file to find the update package name.
171static bool find_uncrypt_package(std::string& package_name)
Doug Zongker76adfc52014-01-13 10:04:25 -0800172{
Tao Bao383b00d2015-05-21 16:44:44 -0700173 if (!android::base::ReadFileToString(uncrypt_file, &package_name)) {
174 ALOGE("failed to open \"%s\": %s\n", uncrypt_file.c_str(), strerror(errno));
175 return false;
Maxim Siniavinee7b28882014-02-13 15:48:53 -0800176 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800177
Tao Bao383b00d2015-05-21 16:44:44 -0700178 // Remove the trailing '\n' if present.
179 package_name = android::base::Trim(package_name);
180
181 return true;
Doug Zongker76adfc52014-01-13 10:04:25 -0800182}
183
Tao Bao381f4552015-05-05 18:36:45 -0700184static int produce_block_map(const char* path, const char* map_file, const char* blk_dev,
Tao Bao383b00d2015-05-21 16:44:44 -0700185 bool encrypted, int status_fd) {
Sungmin Choia72512c2014-12-10 21:57:09 +0900186 int mapfd = open(map_file, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR);
Tao Bao383b00d2015-05-21 16:44:44 -0700187 if (mapfd == -1) {
Sungmin Choia72512c2014-12-10 21:57:09 +0900188 ALOGE("failed to open %s\n", map_file);
189 return -1;
190 }
Elliott Hughes63b089e2015-11-12 21:07:55 -0800191 std::unique_ptr<FILE, int(*)(FILE*)> mapf(fdopen(mapfd, "w"), fclose);
Doug Zongker76adfc52014-01-13 10:04:25 -0800192
Tao Bao383b00d2015-05-21 16:44:44 -0700193 // Make sure we can write to the status_file.
194 if (!android::base::WriteStringToFd("0\n", status_fd)) {
195 ALOGE("failed to update \"%s\"\n", status_file.c_str());
196 return -1;
197 }
198
Tao Bao381f4552015-05-05 18:36:45 -0700199 struct stat sb;
Tao Baoc7547922015-08-06 18:35:05 -0700200 if (stat(path, &sb) != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700201 ALOGE("failed to stat %s\n", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800202 return -1;
203 }
204
Tao Baob8df5fb2015-12-08 22:47:25 -0800205 ALOGI(" block size: %ld bytes\n", static_cast<long>(sb.st_blksize));
Doug Zongker76adfc52014-01-13 10:04:25 -0800206
207 int blocks = ((sb.st_size-1) / sb.st_blksize) + 1;
Tao Baob8df5fb2015-12-08 22:47:25 -0800208 ALOGI(" file size: %" PRId64 " bytes, %d blocks\n", sb.st_size, blocks);
Doug Zongker76adfc52014-01-13 10:04:25 -0800209
Doug Zongker76adfc52014-01-13 10:04:25 -0800210 int range_alloc = 1;
211 int range_used = 1;
Tao Bao381f4552015-05-05 18:36:45 -0700212 int* ranges = reinterpret_cast<int*>(malloc(range_alloc * 2 * sizeof(int)));
Doug Zongker76adfc52014-01-13 10:04:25 -0800213 ranges[0] = -1;
214 ranges[1] = -1;
215
Tao Baob8df5fb2015-12-08 22:47:25 -0800216 fprintf(mapf.get(), "%s\n%" PRId64 " %ld\n",
217 blk_dev, sb.st_size, static_cast<long>(sb.st_blksize));
Doug Zongker76adfc52014-01-13 10:04:25 -0800218
219 unsigned char* buffers[WINDOW_SIZE];
Doug Zongker76adfc52014-01-13 10:04:25 -0800220 if (encrypted) {
Tao Bao381f4552015-05-05 18:36:45 -0700221 for (size_t i = 0; i < WINDOW_SIZE; ++i) {
222 buffers[i] = reinterpret_cast<unsigned char*>(malloc(sb.st_blksize));
Doug Zongker76adfc52014-01-13 10:04:25 -0800223 }
224 }
225 int head_block = 0;
226 int head = 0, tail = 0;
Doug Zongker76adfc52014-01-13 10:04:25 -0800227
228 int fd = open(path, O_RDONLY);
Tao Baoc7547922015-08-06 18:35:05 -0700229 unique_fd fd_holder(fd);
230 if (fd == -1) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700231 ALOGE("failed to open fd for reading: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800232 return -1;
233 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800234
235 int wfd = -1;
Tao Baoc7547922015-08-06 18:35:05 -0700236 unique_fd wfd_holder(wfd);
Doug Zongker76adfc52014-01-13 10:04:25 -0800237 if (encrypted) {
Jaegeuk Kimcc4e3c62015-11-04 11:43:58 -0800238 wfd = open(blk_dev, O_WRONLY);
Tao Baoc7547922015-08-06 18:35:05 -0700239 wfd_holder = unique_fd(wfd);
240 if (wfd == -1) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700241 ALOGE("failed to open fd for writing: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800242 return -1;
243 }
244 }
245
Tao Baob8df5fb2015-12-08 22:47:25 -0800246 off64_t pos = 0;
Tao Bao383b00d2015-05-21 16:44:44 -0700247 int last_progress = 0;
Doug Zongker76adfc52014-01-13 10:04:25 -0800248 while (pos < sb.st_size) {
Tao Bao383b00d2015-05-21 16:44:44 -0700249 // Update the status file, progress must be between [0, 99].
250 int progress = static_cast<int>(100 * (double(pos) / double(sb.st_size)));
251 if (progress > last_progress) {
252 last_progress = progress;
253 android::base::WriteStringToFd(std::to_string(progress) + "\n", status_fd);
254 }
255
Doug Zongker76adfc52014-01-13 10:04:25 -0800256 if ((tail+1) % WINDOW_SIZE == head) {
257 // write out head buffer
258 int block = head_block;
Tao Baoc7547922015-08-06 18:35:05 -0700259 if (ioctl(fd, FIBMAP, &block) != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700260 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800261 return -1;
262 }
263 add_block_to_ranges(&ranges, &range_alloc, &range_used, block);
264 if (encrypted) {
Tao Bao381f4552015-05-05 18:36:45 -0700265 if (write_at_offset(buffers[head], sb.st_blksize, wfd,
Tao Baob8df5fb2015-12-08 22:47:25 -0800266 static_cast<off64_t>(sb.st_blksize) * block) != 0) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800267 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;
Tao Baob8df5fb2015-12-08 22:47:25 -0800277 while (so_far < static_cast<size_t>(sb.st_blksize) && pos < sb.st_size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700278 ssize_t this_read =
279 TEMP_FAILURE_RETRY(read(fd, buffers[tail] + so_far, sb.st_blksize - so_far));
280 if (this_read == -1) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700281 ALOGE("failed to read: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800282 return -1;
283 }
284 so_far += this_read;
285 pos += this_read;
286 }
287 } else {
288 // If we're not encrypting; we don't need to actually read
289 // anything, just skip pos forward as if we'd read a
290 // block.
291 pos += sb.st_blksize;
292 }
293 tail = (tail+1) % WINDOW_SIZE;
294 }
295
296 while (head != tail) {
297 // write out head buffer
298 int block = head_block;
Tao Baoc7547922015-08-06 18:35:05 -0700299 if (ioctl(fd, FIBMAP, &block) != 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) {
Tao Bao381f4552015-05-05 18:36:45 -0700305 if (write_at_offset(buffers[head], sb.st_blksize, wfd,
Tao Baob8df5fb2015-12-08 22:47:25 -0800306 static_cast<off64_t>(sb.st_blksize) * block) != 0) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800307 return -1;
308 }
309 }
310 head = (head + 1) % WINDOW_SIZE;
311 ++head_block;
312 }
313
Elliott Hughes63b089e2015-11-12 21:07:55 -0800314 fprintf(mapf.get(), "%d\n", range_used);
Tao Bao381f4552015-05-05 18:36:45 -0700315 for (int i = 0; i < range_used; ++i) {
Elliott Hughes63b089e2015-11-12 21:07:55 -0800316 fprintf(mapf.get(), "%d %d\n", ranges[i*2], ranges[i*2+1]);
Doug Zongker76adfc52014-01-13 10:04:25 -0800317 }
318
Tao Baofb4ccef2015-05-04 10:10:13 -0700319 if (fsync(mapfd) == -1) {
320 ALOGE("failed to fsync \"%s\": %s\n", map_file, strerror(errno));
321 return -1;
322 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800323 if (encrypted) {
Tao Baofb4ccef2015-05-04 10:10:13 -0700324 if (fsync(wfd) == -1) {
325 ALOGE("failed to fsync \"%s\": %s\n", blk_dev, strerror(errno));
326 return -1;
327 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800328 }
329
330 return 0;
331}
332
Tao Bao381f4552015-05-05 18:36:45 -0700333static void wipe_misc() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700334 ALOGI("removing old commands from misc");
Tao Bao381f4552015-05-05 18:36:45 -0700335 for (int i = 0; i < fstab->num_entries; ++i) {
Doug Zongker2efc9d92014-08-18 15:55:28 -0700336 struct fstab_rec* v = &fstab->recs[i];
337 if (!v->mount_point) continue;
338 if (strcmp(v->mount_point, "/misc") == 0) {
Michael Runge4b542392014-11-21 16:00:45 -0800339 int fd = open(v->blk_device, O_WRONLY | O_SYNC);
Tao Baoc7547922015-08-06 18:35:05 -0700340 unique_fd fd_holder(fd);
341
Doug Zongker2efc9d92014-08-18 15:55:28 -0700342 uint8_t zeroes[1088]; // sizeof(bootloader_message) from recovery
343 memset(zeroes, 0, sizeof(zeroes));
344
345 size_t written = 0;
346 size_t size = sizeof(zeroes);
347 while (written < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700348 ssize_t w = TEMP_FAILURE_RETRY(write(fd, zeroes, size-written));
349 if (w == -1) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700350 ALOGE("zero write failed: %s\n", strerror(errno));
Doug Zongker2efc9d92014-08-18 15:55:28 -0700351 return;
352 } else {
353 written += w;
354 }
355 }
Tao Baofb4ccef2015-05-04 10:10:13 -0700356 if (fsync(fd) == -1) {
357 ALOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno));
Tao Baofb4ccef2015-05-04 10:10:13 -0700358 return;
359 }
Doug Zongker2efc9d92014-08-18 15:55:28 -0700360 }
361 }
362}
363
Tao Bao381f4552015-05-05 18:36:45 -0700364static void reboot_to_recovery() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700365 ALOGI("rebooting to recovery");
Doug Zongker76adfc52014-01-13 10:04:25 -0800366 property_set("sys.powerctl", "reboot,recovery");
Tao Bao75238632015-05-27 14:46:17 -0700367 while (true) {
368 pause();
369 }
Doug Zongkerf449db22014-08-26 09:15:08 -0700370 ALOGE("reboot didn't succeed?");
Doug Zongker76adfc52014-01-13 10:04:25 -0800371}
372
Tao Bao383b00d2015-05-21 16:44:44 -0700373int uncrypt(const char* input_path, const char* map_file, int status_fd) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800374
Tao Bao383b00d2015-05-21 16:44:44 -0700375 ALOGI("update package is \"%s\"", input_path);
Doug Zongkerf449db22014-08-26 09:15:08 -0700376
Doug Zongker76adfc52014-01-13 10:04:25 -0800377 // Turn the name of the file we're supposed to convert into an
378 // absolute path, so we can find what filesystem it's on.
379 char path[PATH_MAX+1];
380 if (realpath(input_path, path) == NULL) {
Tao Bao383b00d2015-05-21 16:44:44 -0700381 ALOGE("failed to convert \"%s\" to absolute path: %s", input_path, strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800382 return 1;
383 }
384
Doug Zongker2efc9d92014-08-18 15:55:28 -0700385 if (read_fstab() == NULL) {
386 return 1;
387 }
Tao Bao381f4552015-05-05 18:36:45 -0700388
389 bool encryptable;
390 bool encrypted;
Doug Zongker76adfc52014-01-13 10:04:25 -0800391 const char* blk_dev = find_block_device(path, &encryptable, &encrypted);
392 if (blk_dev == NULL) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700393 ALOGE("failed to find block device for %s", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800394 return 1;
395 }
396
397 // If the filesystem it's on isn't encrypted, we only produce the
398 // block map, we don't rewrite the file contents (it would be
399 // pointless to do so).
Doug Zongkerf449db22014-08-26 09:15:08 -0700400 ALOGI("encryptable: %s\n", encryptable ? "yes" : "no");
401 ALOGI(" encrypted: %s\n", encrypted ? "yes" : "no");
Doug Zongker76adfc52014-01-13 10:04:25 -0800402
Doug Zongker574443d2014-09-05 08:22:12 -0700403 // Recovery supports installing packages from 3 paths: /cache,
404 // /data, and /sdcard. (On a particular device, other locations
405 // may work, but those are three we actually expect.)
406 //
407 // On /data we want to convert the file to a block map so that we
408 // can read the package without mounting the partition. On /cache
409 // and /sdcard we leave the file alone.
Tao Bao383b00d2015-05-21 16:44:44 -0700410 if (strncmp(path, "/data/", 6) == 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700411 ALOGI("writing block map %s", map_file);
Tao Bao383b00d2015-05-21 16:44:44 -0700412 if (produce_block_map(path, map_file, blk_dev, encrypted, status_fd) != 0) {
413 return 1;
414 }
415 }
416
417 return 0;
418}
419
420int main(int argc, char** argv) {
Tao Bao383b00d2015-05-21 16:44:44 -0700421
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
Daniel Micayc5631fc2016-01-12 16:54:44 -0500444 std::string package;
445 const char* input_path;
446 const char* map_file;
447
Tao Baoac6aa7e2015-05-29 14:24:02 -0700448 if (argc == 3) {
449 // when command-line args are given this binary is being used
450 // for debugging.
451 input_path = argv[1];
452 map_file = argv[2];
453 } else {
Tao Baoac6aa7e2015-05-29 14:24:02 -0700454 if (!find_uncrypt_package(package)) {
455 android::base::WriteStringToFd("-1\n", status_fd);
Tao Baoac6aa7e2015-05-29 14:24:02 -0700456 return 1;
457 }
458 input_path = package.c_str();
459 map_file = cache_block_map.c_str();
460 }
461
Tao Bao383b00d2015-05-21 16:44:44 -0700462 int status = uncrypt(input_path, map_file, status_fd);
463 if (status != 0) {
464 android::base::WriteStringToFd("-1\n", status_fd);
Tao Bao383b00d2015-05-21 16:44:44 -0700465 return 1;
466 }
467
468 android::base::WriteStringToFd("100\n", status_fd);
Doug Zongker76adfc52014-01-13 10:04:25 -0800469 }
470
Doug Zongker76adfc52014-01-13 10:04:25 -0800471 return 0;
472}