blob: 02c1ecb7cfac1750a539adc4a867a524f5c63772 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2007 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
Elliott Hughes63a31922016-06-09 17:41:22 -070017#include "roots.h"
18
Tao Baobb10e582017-07-22 16:30:34 -070019#include <ctype.h>
20#include <fcntl.h>
Abhishek Arpure4fec8e92017-08-24 15:27:16 +053021#include <stdint.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080022#include <stdlib.h>
Tao Baoad774b22017-09-29 10:39:08 -070023#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080024#include <sys/mount.h>
25#include <sys/stat.h>
26#include <sys/types.h>
JP Abgrall37aedb32014-06-16 19:07:39 -070027#include <sys/wait.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028#include <unistd.h>
29
Tao Bao3c00fac2017-07-22 16:46:54 -070030#include <algorithm>
31#include <string>
32#include <vector>
33
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070034#include <android-base/logging.h>
Jin Qianded2dac2017-04-21 14:36:12 -070035#include <android-base/properties.h>
36#include <android-base/stringprintf.h>
Jin Qianf3ccad52017-07-24 10:34:35 -070037#include <android-base/unique_fd.h>
Tao Baobb10e582017-07-22 16:30:34 -070038#include <cryptfs.h>
Tao Baode40ba52016-10-05 23:17:01 -070039#include <ext4_utils/wipe.h>
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080040#include <fs_mgr.h>
Tao Baode40ba52016-10-05 23:17:01 -070041
Elliott Hughes63a31922016-06-09 17:41:22 -070042#include "mounts.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080043
Tao Baobb10e582017-07-22 16:30:34 -070044static struct fstab* fstab = nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045
Tao Baobb10e582017-07-22 16:30:34 -070046extern struct selabel_handle* sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050047
Tao Baobb10e582017-07-22 16:30:34 -070048void load_volume_table() {
49 fstab = fs_mgr_read_fstab_default();
50 if (!fstab) {
51 LOG(ERROR) << "Failed to read default fstab";
52 return;
53 }
Doug Zongker2810ced2011-02-17 15:55:21 -080054
Tao Baobb10e582017-07-22 16:30:34 -070055 int ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
56 if (ret == -1) {
57 LOG(ERROR) << "Failed to add /tmp entry to fstab";
58 fs_mgr_free_fstab(fstab);
59 fstab = nullptr;
60 return;
61 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070062
Tao Baobb10e582017-07-22 16:30:34 -070063 printf("recovery filesystem table\n");
64 printf("=========================\n");
65 for (int i = 0; i < fstab->num_entries; ++i) {
66 const Volume* v = &fstab->recs[i];
67 printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type, v->blk_device, v->length);
68 }
69 printf("\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -070070}
71
Tao Bao2dfc1a32017-09-27 13:12:11 -070072// Finds the volume specified by the given path. fs_mgr_get_entry_for_mount_point() does exact match
73// only, so it attempts the prefixes recursively (e.g. "/cache/recovery/last_log",
74// "/cache/recovery", "/cache", "/" for a given path of "/cache/recovery/last_log") and returns the
75// first match or nullptr.
Doug Zongkerd4208f92010-09-20 12:16:13 -070076Volume* volume_for_path(const char* path) {
Tao Bao2dfc1a32017-09-27 13:12:11 -070077 if (path == nullptr || path[0] == '\0') return nullptr;
78 std::string str(path);
79 while (true) {
Tao Baoad774b22017-09-29 10:39:08 -070080 Volume* result = fs_mgr_get_entry_for_mount_point(fstab, str);
Tao Bao2dfc1a32017-09-27 13:12:11 -070081 if (result != nullptr || str == "/") {
82 return result;
83 }
84 size_t slash = str.find_last_of('/');
85 if (slash == std::string::npos) return nullptr;
86 if (slash == 0) {
87 str = "/";
88 } else {
89 str = str.substr(0, slash);
90 }
91 }
92 return nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080093}
94
Tao Baoabb8f772015-07-30 14:43:27 -070095// Mount the volume specified by path at the given mount_point.
96int ensure_path_mounted_at(const char* path, const char* mount_point) {
Tao Baobb10e582017-07-22 16:30:34 -070097 Volume* v = volume_for_path(path);
98 if (v == nullptr) {
99 LOG(ERROR) << "unknown volume for path [" << path << "]";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800100 return -1;
Tao Baobb10e582017-07-22 16:30:34 -0700101 }
102 if (strcmp(v->fs_type, "ramdisk") == 0) {
103 // The ramdisk is always mounted.
104 return 0;
105 }
106
107 if (!scan_mounted_volumes()) {
108 LOG(ERROR) << "Failed to scan mounted volumes";
109 return -1;
110 }
111
112 if (!mount_point) {
113 mount_point = v->mount_point;
114 }
115
116 const MountedVolume* mv = find_mounted_volume_by_mount_point(mount_point);
117 if (mv != nullptr) {
118 // Volume is already mounted.
119 return 0;
120 }
121
122 mkdir(mount_point, 0755); // in case it doesn't already exist
123
124 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "squashfs") == 0 ||
125 strcmp(v->fs_type, "vfat") == 0) {
126 int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
127 if (result == -1 && fs_mgr_is_formattable(v)) {
128 PLOG(ERROR) << "Failed to mount " << mount_point << "; formatting";
129 bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer");
130 if (fs_mgr_do_format(v, crypt_footer) == 0) {
131 result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
132 } else {
133 PLOG(ERROR) << "Failed to format " << mount_point;
134 return -1;
135 }
136 }
137
138 if (result == -1) {
139 PLOG(ERROR) << "Failed to mount " << mount_point;
140 return -1;
141 }
142 return 0;
143 }
144
145 LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << mount_point;
146 return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800147}
148
Tao Baoabb8f772015-07-30 14:43:27 -0700149int ensure_path_mounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700150 // Mount at the default mount point.
151 return ensure_path_mounted_at(path, nullptr);
Tao Baoabb8f772015-07-30 14:43:27 -0700152}
153
Doug Zongkerd4208f92010-09-20 12:16:13 -0700154int ensure_path_unmounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700155 const Volume* v = volume_for_path(path);
156 if (v == nullptr) {
157 LOG(ERROR) << "unknown volume for path [" << path << "]";
158 return -1;
159 }
160 if (strcmp(v->fs_type, "ramdisk") == 0) {
161 // The ramdisk is always mounted; you can't unmount it.
162 return -1;
163 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800164
Tao Baobb10e582017-07-22 16:30:34 -0700165 if (!scan_mounted_volumes()) {
166 LOG(ERROR) << "Failed to scan mounted volumes";
167 return -1;
168 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700169
Tao Baobb10e582017-07-22 16:30:34 -0700170 MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point);
171 if (mv == nullptr) {
172 // Volume is already unmounted.
173 return 0;
174 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800175
Tao Baobb10e582017-07-22 16:30:34 -0700176 return unmount_mounted_volume(mv);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700177}
178
Tao Bao3c00fac2017-07-22 16:46:54 -0700179static int exec_cmd(const std::vector<std::string>& args) {
180 CHECK_NE(static_cast<size_t>(0), args.size());
181
182 std::vector<char*> argv(args.size());
183 std::transform(args.cbegin(), args.cend(), argv.begin(),
184 [](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
185 argv.push_back(nullptr);
186
187 pid_t child;
188 if ((child = vfork()) == 0) {
189 execv(argv[0], argv.data());
190 _exit(EXIT_FAILURE);
191 }
192
193 int status;
194 waitpid(child, &status, 0);
195 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
196 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
197 }
198 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700199}
200
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530201static int64_t get_file_size(int fd, uint64_t reserve_len) {
Jin Qianf3ccad52017-07-24 10:34:35 -0700202 struct stat buf;
203 int ret = fstat(fd, &buf);
204 if (ret) return 0;
205
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530206 int64_t computed_size;
Jin Qianf3ccad52017-07-24 10:34:35 -0700207 if (S_ISREG(buf.st_mode)) {
208 computed_size = buf.st_size - reserve_len;
209 } else if (S_ISBLK(buf.st_mode)) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530210 uint64_t block_device_size = get_block_device_size(fd);
211 if (block_device_size < reserve_len ||
212 block_device_size > std::numeric_limits<int64_t>::max()) {
213 computed_size = 0;
214 } else {
215 computed_size = block_device_size - reserve_len;
216 }
Jin Qianf3ccad52017-07-24 10:34:35 -0700217 } else {
218 computed_size = 0;
219 }
220
221 return computed_size;
222}
223
Paul Lawrenced0db3372015-11-05 13:38:40 -0800224int format_volume(const char* volume, const char* directory) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700225 const Volume* v = volume_for_path(volume);
226 if (v == nullptr) {
227 LOG(ERROR) << "unknown volume \"" << volume << "\"";
228 return -1;
229 }
230 if (strcmp(v->fs_type, "ramdisk") == 0) {
231 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
232 return -1;
233 }
234 if (strcmp(v->mount_point, volume) != 0) {
235 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
236 return -1;
237 }
238 if (ensure_path_unmounted(volume) != 0) {
239 LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
240 return -1;
241 }
242 if (strcmp(v->fs_type, "ext4") != 0 && strcmp(v->fs_type, "f2fs") != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700243 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800244 return -1;
Tao Bao3c00fac2017-07-22 16:46:54 -0700245 }
246
247 // If there's a key_loc that looks like a path, it should be a block device for storing encryption
248 // metadata. Wipe it too.
249 if (v->key_loc != nullptr && v->key_loc[0] == '/') {
250 LOG(INFO) << "Wiping " << v->key_loc;
251 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
252 if (fd == -1) {
253 PLOG(ERROR) << "format_volume: Failed to open " << v->key_loc;
254 return -1;
255 }
256 wipe_block_device(fd, get_file_size(fd));
257 close(fd);
258 }
259
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530260 int64_t length = 0;
Tao Bao3c00fac2017-07-22 16:46:54 -0700261 if (v->length != 0) {
262 length = v->length;
263 } else if (v->key_loc != nullptr && strcmp(v->key_loc, "footer") == 0) {
264 android::base::unique_fd fd(open(v->blk_device, O_RDONLY));
265 if (fd == -1) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530266 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700267 return -1;
268 }
269 length = get_file_size(fd.get(), CRYPT_FOOTER_OFFSET);
270 if (length <= 0) {
271 LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device;
272 return -1;
273 }
274 }
275
276 if (strcmp(v->fs_type, "ext4") == 0) {
277 static constexpr int kBlockSize = 4096;
278 std::vector<std::string> mke2fs_args = {
279 "/sbin/mke2fs_static", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
280 };
281
282 int raid_stride = v->logical_blk_size / kBlockSize;
283 int raid_stripe_width = v->erase_blk_size / kBlockSize;
284 // stride should be the max of 8KB and logical block size
285 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
286 raid_stride = 8192 / kBlockSize;
287 }
288 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
289 mke2fs_args.push_back("-E");
290 mke2fs_args.push_back(
291 android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
292 }
293 mke2fs_args.push_back(v->blk_device);
294 if (length != 0) {
295 mke2fs_args.push_back(std::to_string(length / kBlockSize));
296 }
297
298 int result = exec_cmd(mke2fs_args);
299 if (result == 0 && directory != nullptr) {
300 std::vector<std::string> e2fsdroid_args = {
301 "/sbin/e2fsdroid_static",
302 "-e",
303 "-f",
304 directory,
305 "-a",
306 volume,
307 v->blk_device,
308 };
309 result = exec_cmd(e2fsdroid_args);
310 }
311
312 if (result != 0) {
313 PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
314 return -1;
315 }
316 return 0;
317 }
318
319 // Has to be f2fs because we checked earlier.
320 std::vector<std::string> f2fs_args = { "/sbin/mkfs.f2fs", "-t", "-d1", v->blk_device };
321 if (length >= 512) {
322 f2fs_args.push_back(std::to_string(length / 512));
323 }
324
325 int result = exec_cmd(f2fs_args);
326 if (result != 0) {
327 PLOG(ERROR) << "format_volume: Failed to make f2fs on " << v->blk_device;
328 return -1;
329 }
330 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800331}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700332
Paul Lawrenced0db3372015-11-05 13:38:40 -0800333int format_volume(const char* volume) {
Tao Baobb10e582017-07-22 16:30:34 -0700334 return format_volume(volume, nullptr);
Paul Lawrenced0db3372015-11-05 13:38:40 -0800335}
336
Doug Zongker239ac6a2013-08-20 16:03:25 -0700337int setup_install_mounts() {
Tao Bao57130c42017-05-10 12:11:21 -0700338 if (fstab == nullptr) {
339 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
340 return -1;
341 }
342 for (int i = 0; i < fstab->num_entries; ++i) {
343 const Volume* v = fstab->recs + i;
344
345 // We don't want to do anything with "/".
346 if (strcmp(v->mount_point, "/") == 0) {
347 continue;
348 }
349
350 if (strcmp(v->mount_point, "/tmp") == 0 || strcmp(v->mount_point, "/cache") == 0) {
351 if (ensure_path_mounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700352 LOG(ERROR) << "Failed to mount " << v->mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700353 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700354 }
355 } else {
356 if (ensure_path_unmounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700357 LOG(ERROR) << "Failed to unmount " << v->mount_point;
Tao Bao57130c42017-05-10 12:11:21 -0700358 return -1;
359 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700360 }
Tao Bao57130c42017-05-10 12:11:21 -0700361 }
362 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700363}