blob: 098a7a979a6aea285c881a5e27c61adf8f901a95 [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>
Yabin Cui25dd0382016-02-01 11:40:37 -080045#include <libgen.h>
Tao Bao75238632015-05-27 14:46:17 -070046#include <linux/fs.h>
47#include <stdarg.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080048#include <stdio.h>
49#include <stdlib.h>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080050#include <string.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080051#include <sys/mman.h>
Tao Bao383b00d2015-05-21 16:44:44 -070052#include <sys/stat.h>
53#include <sys/types.h>
Tao Bao75238632015-05-27 14:46:17 -070054#include <unistd.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080055
Yabin Cui25dd0382016-02-01 11:40:37 -080056#include <algorithm>
Tao Baoc7547922015-08-06 18:35:05 -070057#include <memory>
Yabin Cui25dd0382016-02-01 11:40:37 -080058#include <vector>
Tao Baoc7547922015-08-06 18:35:05 -070059
Elliott Hughes4b166f02015-12-04 15:30:20 -080060#include <android-base/file.h>
Yabin Cui25dd0382016-02-01 11:40:37 -080061#include <android-base/stringprintf.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080062#include <android-base/strings.h>
Tao Bao75238632015-05-27 14:46:17 -070063#include <cutils/android_reboot.h>
Doug Zongker76adfc52014-01-13 10:04:25 -080064#include <cutils/properties.h>
65#include <fs_mgr.h>
66
Tao Bao383b00d2015-05-21 16:44:44 -070067#define LOG_TAG "uncrypt"
68#include <log/log.h>
69
Tao Baoc7547922015-08-06 18:35:05 -070070#include "unique_fd.h"
71
Doug Zongker76adfc52014-01-13 10:04:25 -080072#define WINDOW_SIZE 5
Tao Bao383b00d2015-05-21 16:44:44 -070073
74static const std::string cache_block_map = "/cache/recovery/block.map";
75static const std::string status_file = "/cache/recovery/uncrypt_status";
76static const std::string uncrypt_file = "/cache/recovery/uncrypt_file";
Doug Zongker76adfc52014-01-13 10:04:25 -080077
Doug Zongker2efc9d92014-08-18 15:55:28 -070078static struct fstab* fstab = NULL;
79
Tao Bao381f4552015-05-05 18:36:45 -070080static int write_at_offset(unsigned char* buffer, size_t size, int wfd, off64_t offset) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -070081 if (TEMP_FAILURE_RETRY(lseek64(wfd, offset, SEEK_SET)) == -1) {
Tao Baob8df5fb2015-12-08 22:47:25 -080082 ALOGE("error seeking to offset %" PRId64 ": %s\n", offset, strerror(errno));
Elliott Hughes7bad7c42015-04-28 17:24:24 -070083 return -1;
84 }
Yabin Cui25dd0382016-02-01 11:40:37 -080085 if (!android::base::WriteFully(wfd, buffer, size)) {
86 ALOGE("error writing offset %" PRId64 ": %s\n", offset, strerror(errno));
87 return -1;
Doug Zongker76adfc52014-01-13 10:04:25 -080088 }
89 return 0;
90}
91
Yabin Cui25dd0382016-02-01 11:40:37 -080092static void add_block_to_ranges(std::vector<int>& ranges, int new_block) {
93 if (!ranges.empty() && new_block == ranges.back()) {
Doug Zongker76adfc52014-01-13 10:04:25 -080094 // If the new block comes immediately after the current range,
95 // all we have to do is extend the current range.
Yabin Cui25dd0382016-02-01 11:40:37 -080096 ++ranges.back();
Doug Zongker76adfc52014-01-13 10:04:25 -080097 } else {
98 // We need to start a new range.
Yabin Cui25dd0382016-02-01 11:40:37 -080099 ranges.push_back(new_block);
100 ranges.push_back(new_block + 1);
Doug Zongker76adfc52014-01-13 10:04:25 -0800101 }
102}
103
Tao Bao381f4552015-05-05 18:36:45 -0700104static struct fstab* read_fstab() {
Doug Zongker2efc9d92014-08-18 15:55:28 -0700105 fstab = NULL;
106
Doug Zongker76adfc52014-01-13 10:04:25 -0800107 // The fstab path is always "/fstab.${ro.hardware}".
108 char fstab_path[PATH_MAX+1] = "/fstab.";
109 if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700110 ALOGE("failed to get ro.hardware\n");
Doug Zongker76adfc52014-01-13 10:04:25 -0800111 return NULL;
112 }
113
Doug Zongker2efc9d92014-08-18 15:55:28 -0700114 fstab = fs_mgr_read_fstab(fstab_path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800115 if (!fstab) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700116 ALOGE("failed to read %s\n", fstab_path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800117 return NULL;
118 }
119
Doug Zongker2efc9d92014-08-18 15:55:28 -0700120 return fstab;
121}
122
Tao Bao381f4552015-05-05 18:36:45 -0700123static const char* find_block_device(const char* path, bool* encryptable, bool* encrypted) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800124 // Look for a volume whose mount point is the prefix of path and
125 // return its block device. Set encrypted if it's currently
126 // encrypted.
Tao Bao381f4552015-05-05 18:36:45 -0700127 for (int i = 0; i < fstab->num_entries; ++i) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800128 struct fstab_rec* v = &fstab->recs[i];
Tao Bao381f4552015-05-05 18:36:45 -0700129 if (!v->mount_point) {
130 continue;
131 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800132 int len = strlen(v->mount_point);
133 if (strncmp(path, v->mount_point, len) == 0 &&
134 (path[len] == '/' || path[len] == 0)) {
Tao Bao381f4552015-05-05 18:36:45 -0700135 *encrypted = false;
136 *encryptable = false;
Tao Bao7cf50c62015-07-16 20:04:13 -0700137 if (fs_mgr_is_encryptable(v) || fs_mgr_is_file_encrypted(v)) {
Tao Bao381f4552015-05-05 18:36:45 -0700138 *encryptable = true;
Doug Zongker76adfc52014-01-13 10:04:25 -0800139 char buffer[PROPERTY_VALUE_MAX+1];
140 if (property_get("ro.crypto.state", buffer, "") &&
141 strcmp(buffer, "encrypted") == 0) {
Tao Bao381f4552015-05-05 18:36:45 -0700142 *encrypted = true;
Doug Zongker76adfc52014-01-13 10:04:25 -0800143 }
144 }
145 return v->blk_device;
146 }
147 }
148
149 return NULL;
150}
151
Tao Bao383b00d2015-05-21 16:44:44 -0700152// Parse uncrypt_file to find the update package name.
153static bool find_uncrypt_package(std::string& package_name)
Doug Zongker76adfc52014-01-13 10:04:25 -0800154{
Tao Bao383b00d2015-05-21 16:44:44 -0700155 if (!android::base::ReadFileToString(uncrypt_file, &package_name)) {
156 ALOGE("failed to open \"%s\": %s\n", uncrypt_file.c_str(), strerror(errno));
157 return false;
Maxim Siniavinee7b28882014-02-13 15:48:53 -0800158 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800159
Tao Bao383b00d2015-05-21 16:44:44 -0700160 // Remove the trailing '\n' if present.
161 package_name = android::base::Trim(package_name);
162
163 return true;
Doug Zongker76adfc52014-01-13 10:04:25 -0800164}
165
Tao Bao381f4552015-05-05 18:36:45 -0700166static int produce_block_map(const char* path, const char* map_file, const char* blk_dev,
Tao Bao383b00d2015-05-21 16:44:44 -0700167 bool encrypted, int status_fd) {
Yabin Cui25dd0382016-02-01 11:40:37 -0800168 std::string err;
169 if (!android::base::RemoveFileIfExists(map_file, &err)) {
170 ALOGE("failed to remove the existing map file %s: %s\n", map_file, err.c_str());
Sungmin Choia72512c2014-12-10 21:57:09 +0900171 return -1;
172 }
Yabin Cui25dd0382016-02-01 11:40:37 -0800173 std::string tmp_map_file = std::string(map_file) + ".tmp";
174 unique_fd mapfd(open(tmp_map_file.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR));
175 if (!mapfd) {
176 ALOGE("failed to open %s: %s\n", tmp_map_file.c_str(), strerror(errno));
177 return -1;
178 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800179
Tao Bao383b00d2015-05-21 16:44:44 -0700180 // Make sure we can write to the status_file.
181 if (!android::base::WriteStringToFd("0\n", status_fd)) {
182 ALOGE("failed to update \"%s\"\n", status_file.c_str());
183 return -1;
184 }
185
Tao Bao381f4552015-05-05 18:36:45 -0700186 struct stat sb;
Tao Baoc7547922015-08-06 18:35:05 -0700187 if (stat(path, &sb) != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700188 ALOGE("failed to stat %s\n", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800189 return -1;
190 }
191
Tao Baob8df5fb2015-12-08 22:47:25 -0800192 ALOGI(" block size: %ld bytes\n", static_cast<long>(sb.st_blksize));
Doug Zongker76adfc52014-01-13 10:04:25 -0800193
194 int blocks = ((sb.st_size-1) / sb.st_blksize) + 1;
Tao Baob8df5fb2015-12-08 22:47:25 -0800195 ALOGI(" file size: %" PRId64 " bytes, %d blocks\n", sb.st_size, blocks);
Doug Zongker76adfc52014-01-13 10:04:25 -0800196
Yabin Cui25dd0382016-02-01 11:40:37 -0800197 std::vector<int> ranges;
Doug Zongker76adfc52014-01-13 10:04:25 -0800198
Yabin Cui25dd0382016-02-01 11:40:37 -0800199 std::string s = android::base::StringPrintf("%s\n%" PRId64 " %ld\n",
200 blk_dev, sb.st_size, static_cast<long>(sb.st_blksize));
201 if (!android::base::WriteStringToFd(s, mapfd.get())) {
202 ALOGE("failed to write %s: %s\n", tmp_map_file.c_str(), strerror(errno));
203 return -1;
204 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800205
Yabin Cui25dd0382016-02-01 11:40:37 -0800206 std::vector<std::vector<unsigned char>> buffers;
Doug Zongker76adfc52014-01-13 10:04:25 -0800207 if (encrypted) {
Yabin Cui25dd0382016-02-01 11:40:37 -0800208 buffers.resize(WINDOW_SIZE, std::vector<unsigned char>(sb.st_blksize));
Doug Zongker76adfc52014-01-13 10:04:25 -0800209 }
210 int head_block = 0;
211 int head = 0, tail = 0;
Doug Zongker76adfc52014-01-13 10:04:25 -0800212
Yabin Cui25dd0382016-02-01 11:40:37 -0800213 unique_fd fd(open(path, O_RDONLY));
214 if (!fd) {
215 ALOGE("failed to open %s for reading: %s\n", path, strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800216 return -1;
217 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800218
Yabin Cui25dd0382016-02-01 11:40:37 -0800219 unique_fd wfd(-1);
Doug Zongker76adfc52014-01-13 10:04:25 -0800220 if (encrypted) {
Jaegeuk Kimcc4e3c62015-11-04 11:43:58 -0800221 wfd = open(blk_dev, O_WRONLY);
Yabin Cui25dd0382016-02-01 11:40:37 -0800222 if (!wfd) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700223 ALOGE("failed to open fd for writing: %s\n", strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800224 return -1;
225 }
226 }
227
Tao Baob8df5fb2015-12-08 22:47:25 -0800228 off64_t pos = 0;
Tao Bao383b00d2015-05-21 16:44:44 -0700229 int last_progress = 0;
Doug Zongker76adfc52014-01-13 10:04:25 -0800230 while (pos < sb.st_size) {
Tao Bao383b00d2015-05-21 16:44:44 -0700231 // Update the status file, progress must be between [0, 99].
232 int progress = static_cast<int>(100 * (double(pos) / double(sb.st_size)));
233 if (progress > last_progress) {
234 last_progress = progress;
235 android::base::WriteStringToFd(std::to_string(progress) + "\n", status_fd);
236 }
237
Doug Zongker76adfc52014-01-13 10:04:25 -0800238 if ((tail+1) % WINDOW_SIZE == head) {
239 // write out head buffer
240 int block = head_block;
Yabin Cui25dd0382016-02-01 11:40:37 -0800241 if (ioctl(fd.get(), FIBMAP, &block) != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700242 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800243 return -1;
244 }
Yabin Cui25dd0382016-02-01 11:40:37 -0800245 add_block_to_ranges(ranges, block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800246 if (encrypted) {
Yabin Cui25dd0382016-02-01 11:40:37 -0800247 if (write_at_offset(buffers[head].data(), sb.st_blksize, wfd.get(),
Tao Baob8df5fb2015-12-08 22:47:25 -0800248 static_cast<off64_t>(sb.st_blksize) * block) != 0) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800249 return -1;
250 }
251 }
252 head = (head + 1) % WINDOW_SIZE;
253 ++head_block;
254 }
255
256 // read next block to tail
257 if (encrypted) {
Yabin Cui25dd0382016-02-01 11:40:37 -0800258 size_t to_read = static_cast<size_t>(
259 std::min(static_cast<off64_t>(sb.st_blksize), sb.st_size - pos));
260 if (!android::base::ReadFully(fd.get(), buffers[tail].data(), to_read)) {
261 ALOGE("failed to read: %s\n", strerror(errno));
262 return -1;
Doug Zongker76adfc52014-01-13 10:04:25 -0800263 }
Yabin Cui25dd0382016-02-01 11:40:37 -0800264 pos += to_read;
Doug Zongker76adfc52014-01-13 10:04:25 -0800265 } else {
266 // If we're not encrypting; we don't need to actually read
267 // anything, just skip pos forward as if we'd read a
268 // block.
269 pos += sb.st_blksize;
270 }
271 tail = (tail+1) % WINDOW_SIZE;
272 }
273
274 while (head != tail) {
275 // write out head buffer
276 int block = head_block;
Yabin Cui25dd0382016-02-01 11:40:37 -0800277 if (ioctl(fd.get(), FIBMAP, &block) != 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700278 ALOGE("failed to find block %d\n", head_block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800279 return -1;
280 }
Yabin Cui25dd0382016-02-01 11:40:37 -0800281 add_block_to_ranges(ranges, block);
Doug Zongker76adfc52014-01-13 10:04:25 -0800282 if (encrypted) {
Yabin Cui25dd0382016-02-01 11:40:37 -0800283 if (write_at_offset(buffers[head].data(), sb.st_blksize, wfd.get(),
Tao Baob8df5fb2015-12-08 22:47:25 -0800284 static_cast<off64_t>(sb.st_blksize) * block) != 0) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800285 return -1;
286 }
287 }
288 head = (head + 1) % WINDOW_SIZE;
289 ++head_block;
290 }
291
Yabin Cui25dd0382016-02-01 11:40:37 -0800292 if (!android::base::WriteStringToFd(
293 android::base::StringPrintf("%zu\n", ranges.size() / 2), mapfd.get())) {
294 ALOGE("failed to write %s: %s\n", tmp_map_file.c_str(), strerror(errno));
Tao Baofb4ccef2015-05-04 10:10:13 -0700295 return -1;
296 }
Yabin Cui25dd0382016-02-01 11:40:37 -0800297 for (size_t i = 0; i < ranges.size(); i += 2) {
298 if (!android::base::WriteStringToFd(
299 android::base::StringPrintf("%d %d\n", ranges[i], ranges[i+1]), mapfd.get())) {
300 ALOGE("failed to write %s: %s\n", tmp_map_file.c_str(), strerror(errno));
Tao Baofb4ccef2015-05-04 10:10:13 -0700301 return -1;
302 }
Doug Zongker76adfc52014-01-13 10:04:25 -0800303 }
304
Yabin Cui25dd0382016-02-01 11:40:37 -0800305 if (fsync(mapfd.get()) == -1) {
306 ALOGE("failed to fsync \"%s\": %s\n", tmp_map_file.c_str(), strerror(errno));
307 return -1;
308 }
309 if (close(mapfd.get() == -1)) {
310 ALOGE("failed to close %s: %s\n", tmp_map_file.c_str(), strerror(errno));
311 return -1;
312 }
313 mapfd = -1;
314
315 if (encrypted) {
316 if (fsync(wfd.get()) == -1) {
317 ALOGE("failed to fsync \"%s\": %s\n", blk_dev, strerror(errno));
318 return -1;
319 }
320 if (close(wfd.get()) == -1) {
321 ALOGE("failed to close %s: %s\n", blk_dev, strerror(errno));
322 return -1;
323 }
324 wfd = -1;
325 }
326
327 if (rename(tmp_map_file.c_str(), map_file) == -1) {
328 ALOGE("failed to rename %s to %s: %s\n", tmp_map_file.c_str(), map_file, strerror(errno));
329 return -1;
330 }
331 // Sync dir to make rename() result written to disk.
332 std::string file_name = map_file;
333 std::string dir_name = dirname(&file_name[0]);
334 unique_fd dfd(open(dir_name.c_str(), O_RDONLY | O_DIRECTORY));
335 if (!dfd) {
336 ALOGE("failed to open dir %s: %s\n", dir_name.c_str(), strerror(errno));
337 return -1;
338 }
339 if (fsync(dfd.get()) == -1) {
340 ALOGE("failed to fsync %s: %s\n", dir_name.c_str(), strerror(errno));
341 return -1;
342 }
343 if (close(dfd.get() == -1)) {
344 ALOGE("failed to close %s: %s\n", dir_name.c_str(), strerror(errno));
345 return -1;
346 }
347 dfd = -1;
Doug Zongker76adfc52014-01-13 10:04:25 -0800348 return 0;
349}
350
Tao Bao381f4552015-05-05 18:36:45 -0700351static void wipe_misc() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700352 ALOGI("removing old commands from misc");
Tao Bao381f4552015-05-05 18:36:45 -0700353 for (int i = 0; i < fstab->num_entries; ++i) {
Doug Zongker2efc9d92014-08-18 15:55:28 -0700354 struct fstab_rec* v = &fstab->recs[i];
355 if (!v->mount_point) continue;
356 if (strcmp(v->mount_point, "/misc") == 0) {
Michael Runge4b542392014-11-21 16:00:45 -0800357 int fd = open(v->blk_device, O_WRONLY | O_SYNC);
Tao Baoc7547922015-08-06 18:35:05 -0700358 unique_fd fd_holder(fd);
359
Doug Zongker2efc9d92014-08-18 15:55:28 -0700360 uint8_t zeroes[1088]; // sizeof(bootloader_message) from recovery
361 memset(zeroes, 0, sizeof(zeroes));
362
363 size_t written = 0;
364 size_t size = sizeof(zeroes);
365 while (written < size) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700366 ssize_t w = TEMP_FAILURE_RETRY(write(fd, zeroes, size-written));
367 if (w == -1) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700368 ALOGE("zero write failed: %s\n", strerror(errno));
Doug Zongker2efc9d92014-08-18 15:55:28 -0700369 return;
370 } else {
371 written += w;
372 }
373 }
Tao Baofb4ccef2015-05-04 10:10:13 -0700374 if (fsync(fd) == -1) {
375 ALOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno));
Tao Baofb4ccef2015-05-04 10:10:13 -0700376 return;
377 }
Doug Zongker2efc9d92014-08-18 15:55:28 -0700378 }
379 }
380}
381
Tao Bao381f4552015-05-05 18:36:45 -0700382static void reboot_to_recovery() {
Doug Zongkerf449db22014-08-26 09:15:08 -0700383 ALOGI("rebooting to recovery");
Doug Zongker76adfc52014-01-13 10:04:25 -0800384 property_set("sys.powerctl", "reboot,recovery");
Tao Bao75238632015-05-27 14:46:17 -0700385 while (true) {
386 pause();
387 }
Doug Zongkerf449db22014-08-26 09:15:08 -0700388 ALOGE("reboot didn't succeed?");
Doug Zongker76adfc52014-01-13 10:04:25 -0800389}
390
Tao Bao383b00d2015-05-21 16:44:44 -0700391int uncrypt(const char* input_path, const char* map_file, int status_fd) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800392
Tao Bao383b00d2015-05-21 16:44:44 -0700393 ALOGI("update package is \"%s\"", input_path);
Doug Zongkerf449db22014-08-26 09:15:08 -0700394
Doug Zongker76adfc52014-01-13 10:04:25 -0800395 // Turn the name of the file we're supposed to convert into an
396 // absolute path, so we can find what filesystem it's on.
397 char path[PATH_MAX+1];
398 if (realpath(input_path, path) == NULL) {
Tao Bao383b00d2015-05-21 16:44:44 -0700399 ALOGE("failed to convert \"%s\" to absolute path: %s", input_path, strerror(errno));
Doug Zongker76adfc52014-01-13 10:04:25 -0800400 return 1;
401 }
402
Doug Zongker2efc9d92014-08-18 15:55:28 -0700403 if (read_fstab() == NULL) {
404 return 1;
405 }
Tao Bao381f4552015-05-05 18:36:45 -0700406
407 bool encryptable;
408 bool encrypted;
Doug Zongker76adfc52014-01-13 10:04:25 -0800409 const char* blk_dev = find_block_device(path, &encryptable, &encrypted);
410 if (blk_dev == NULL) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700411 ALOGE("failed to find block device for %s", path);
Doug Zongker76adfc52014-01-13 10:04:25 -0800412 return 1;
413 }
414
415 // If the filesystem it's on isn't encrypted, we only produce the
416 // block map, we don't rewrite the file contents (it would be
417 // pointless to do so).
Doug Zongkerf449db22014-08-26 09:15:08 -0700418 ALOGI("encryptable: %s\n", encryptable ? "yes" : "no");
419 ALOGI(" encrypted: %s\n", encrypted ? "yes" : "no");
Doug Zongker76adfc52014-01-13 10:04:25 -0800420
Doug Zongker574443d2014-09-05 08:22:12 -0700421 // Recovery supports installing packages from 3 paths: /cache,
422 // /data, and /sdcard. (On a particular device, other locations
423 // may work, but those are three we actually expect.)
424 //
425 // On /data we want to convert the file to a block map so that we
426 // can read the package without mounting the partition. On /cache
427 // and /sdcard we leave the file alone.
Tao Bao383b00d2015-05-21 16:44:44 -0700428 if (strncmp(path, "/data/", 6) == 0) {
Doug Zongkerf449db22014-08-26 09:15:08 -0700429 ALOGI("writing block map %s", map_file);
Tao Bao383b00d2015-05-21 16:44:44 -0700430 if (produce_block_map(path, map_file, blk_dev, encrypted, status_fd) != 0) {
431 return 1;
432 }
433 }
434
435 return 0;
436}
437
438int main(int argc, char** argv) {
Tao Bao383b00d2015-05-21 16:44:44 -0700439
440 if (argc != 3 && argc != 1 && (argc == 2 && strcmp(argv[1], "--reboot") != 0)) {
441 fprintf(stderr, "usage: %s [--reboot] [<transform_path> <map_file>]\n", argv[0]);
442 return 2;
443 }
444
445 // When uncrypt is started with "--reboot", it wipes misc and reboots.
446 // Otherwise it uncrypts the package and writes the block map.
447 if (argc == 2) {
448 if (read_fstab() == NULL) {
Doug Zongker76adfc52014-01-13 10:04:25 -0800449 return 1;
450 }
Tao Baofb4ccef2015-05-04 10:10:13 -0700451 wipe_misc();
Tao Bao383b00d2015-05-21 16:44:44 -0700452 reboot_to_recovery();
453 } else {
Tao Bao383b00d2015-05-21 16:44:44 -0700454 // The pipe has been created by the system server.
455 int status_fd = open(status_file.c_str(), O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR);
456 if (status_fd == -1) {
457 ALOGE("failed to open pipe \"%s\": %s\n", status_file.c_str(), strerror(errno));
458 return 1;
459 }
Tao Baoc7547922015-08-06 18:35:05 -0700460 unique_fd status_fd_holder(status_fd);
Tao Baoac6aa7e2015-05-29 14:24:02 -0700461
Daniel Micayc5631fc2016-01-12 16:54:44 -0500462 std::string package;
463 const char* input_path;
464 const char* map_file;
465
Tao Baoac6aa7e2015-05-29 14:24:02 -0700466 if (argc == 3) {
467 // when command-line args are given this binary is being used
468 // for debugging.
469 input_path = argv[1];
470 map_file = argv[2];
471 } else {
Tao Baoac6aa7e2015-05-29 14:24:02 -0700472 if (!find_uncrypt_package(package)) {
473 android::base::WriteStringToFd("-1\n", status_fd);
Tao Baoac6aa7e2015-05-29 14:24:02 -0700474 return 1;
475 }
476 input_path = package.c_str();
477 map_file = cache_block_map.c_str();
478 }
479
Tao Bao383b00d2015-05-21 16:44:44 -0700480 int status = uncrypt(input_path, map_file, status_fd);
481 if (status != 0) {
482 android::base::WriteStringToFd("-1\n", status_fd);
Tao Bao383b00d2015-05-21 16:44:44 -0700483 return 1;
484 }
485
486 android::base::WriteStringToFd("100\n", status_fd);
Doug Zongker76adfc52014-01-13 10:04:25 -0800487 }
488
Doug Zongker76adfc52014-01-13 10:04:25 -0800489 return 0;
490}