blob: 3c811dfedf0bea5f4fc5431714ea5be771cf7d58 [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
Tao Bao9a319f02018-01-04 13:19:11 -080042#include "otautil/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 Bao3e18f2b2017-09-29 17:11:13 -070072Volume* volume_for_mount_point(const std::string& mount_point) {
73 return fs_mgr_get_entry_for_mount_point(fstab, mount_point);
74}
75
Tao Bao2dfc1a32017-09-27 13:12:11 -070076// Finds the volume specified by the given path. fs_mgr_get_entry_for_mount_point() does exact match
77// only, so it attempts the prefixes recursively (e.g. "/cache/recovery/last_log",
78// "/cache/recovery", "/cache", "/" for a given path of "/cache/recovery/last_log") and returns the
79// first match or nullptr.
Tao Bao3e18f2b2017-09-29 17:11:13 -070080static Volume* volume_for_path(const char* path) {
Tao Bao2dfc1a32017-09-27 13:12:11 -070081 if (path == nullptr || path[0] == '\0') return nullptr;
82 std::string str(path);
83 while (true) {
Tao Baoad774b22017-09-29 10:39:08 -070084 Volume* result = fs_mgr_get_entry_for_mount_point(fstab, str);
Tao Bao2dfc1a32017-09-27 13:12:11 -070085 if (result != nullptr || str == "/") {
86 return result;
87 }
88 size_t slash = str.find_last_of('/');
89 if (slash == std::string::npos) return nullptr;
90 if (slash == 0) {
91 str = "/";
92 } else {
93 str = str.substr(0, slash);
94 }
95 }
96 return nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080097}
98
Tao Baoabb8f772015-07-30 14:43:27 -070099// Mount the volume specified by path at the given mount_point.
100int ensure_path_mounted_at(const char* path, const char* mount_point) {
Tao Baobb10e582017-07-22 16:30:34 -0700101 Volume* v = volume_for_path(path);
102 if (v == nullptr) {
103 LOG(ERROR) << "unknown volume for path [" << path << "]";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800104 return -1;
Tao Baobb10e582017-07-22 16:30:34 -0700105 }
106 if (strcmp(v->fs_type, "ramdisk") == 0) {
107 // The ramdisk is always mounted.
108 return 0;
109 }
110
111 if (!scan_mounted_volumes()) {
112 LOG(ERROR) << "Failed to scan mounted volumes";
113 return -1;
114 }
115
116 if (!mount_point) {
117 mount_point = v->mount_point;
118 }
119
120 const MountedVolume* mv = find_mounted_volume_by_mount_point(mount_point);
121 if (mv != nullptr) {
122 // Volume is already mounted.
123 return 0;
124 }
125
126 mkdir(mount_point, 0755); // in case it doesn't already exist
127
128 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "squashfs") == 0 ||
129 strcmp(v->fs_type, "vfat") == 0) {
130 int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
131 if (result == -1 && fs_mgr_is_formattable(v)) {
132 PLOG(ERROR) << "Failed to mount " << mount_point << "; formatting";
133 bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer");
134 if (fs_mgr_do_format(v, crypt_footer) == 0) {
135 result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
136 } else {
137 PLOG(ERROR) << "Failed to format " << mount_point;
138 return -1;
139 }
140 }
141
142 if (result == -1) {
143 PLOG(ERROR) << "Failed to mount " << mount_point;
144 return -1;
145 }
146 return 0;
147 }
148
149 LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << mount_point;
150 return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800151}
152
Tao Baoabb8f772015-07-30 14:43:27 -0700153int ensure_path_mounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700154 // Mount at the default mount point.
155 return ensure_path_mounted_at(path, nullptr);
Tao Baoabb8f772015-07-30 14:43:27 -0700156}
157
Doug Zongkerd4208f92010-09-20 12:16:13 -0700158int ensure_path_unmounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700159 const Volume* v = volume_for_path(path);
160 if (v == nullptr) {
161 LOG(ERROR) << "unknown volume for path [" << path << "]";
162 return -1;
163 }
164 if (strcmp(v->fs_type, "ramdisk") == 0) {
165 // The ramdisk is always mounted; you can't unmount it.
166 return -1;
167 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800168
Tao Baobb10e582017-07-22 16:30:34 -0700169 if (!scan_mounted_volumes()) {
170 LOG(ERROR) << "Failed to scan mounted volumes";
171 return -1;
172 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700173
Tao Baobb10e582017-07-22 16:30:34 -0700174 MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point);
175 if (mv == nullptr) {
176 // Volume is already unmounted.
177 return 0;
178 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800179
Tao Baobb10e582017-07-22 16:30:34 -0700180 return unmount_mounted_volume(mv);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700181}
182
Tao Bao3c00fac2017-07-22 16:46:54 -0700183static int exec_cmd(const std::vector<std::string>& args) {
184 CHECK_NE(static_cast<size_t>(0), args.size());
185
186 std::vector<char*> argv(args.size());
187 std::transform(args.cbegin(), args.cend(), argv.begin(),
188 [](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
189 argv.push_back(nullptr);
190
191 pid_t child;
George Burgess IV1cfb3612018-02-17 17:48:45 -0800192 if ((child = fork()) == 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700193 execv(argv[0], argv.data());
194 _exit(EXIT_FAILURE);
195 }
196
197 int status;
198 waitpid(child, &status, 0);
199 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
200 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
201 }
202 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700203}
204
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530205static int64_t get_file_size(int fd, uint64_t reserve_len) {
Jin Qianf3ccad52017-07-24 10:34:35 -0700206 struct stat buf;
207 int ret = fstat(fd, &buf);
208 if (ret) return 0;
209
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530210 int64_t computed_size;
Jin Qianf3ccad52017-07-24 10:34:35 -0700211 if (S_ISREG(buf.st_mode)) {
212 computed_size = buf.st_size - reserve_len;
213 } else if (S_ISBLK(buf.st_mode)) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530214 uint64_t block_device_size = get_block_device_size(fd);
215 if (block_device_size < reserve_len ||
216 block_device_size > std::numeric_limits<int64_t>::max()) {
217 computed_size = 0;
218 } else {
219 computed_size = block_device_size - reserve_len;
220 }
Jin Qianf3ccad52017-07-24 10:34:35 -0700221 } else {
222 computed_size = 0;
223 }
224
225 return computed_size;
226}
227
Paul Lawrenced0db3372015-11-05 13:38:40 -0800228int format_volume(const char* volume, const char* directory) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700229 const Volume* v = volume_for_path(volume);
230 if (v == nullptr) {
231 LOG(ERROR) << "unknown volume \"" << volume << "\"";
232 return -1;
233 }
234 if (strcmp(v->fs_type, "ramdisk") == 0) {
235 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
236 return -1;
237 }
238 if (strcmp(v->mount_point, volume) != 0) {
239 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
240 return -1;
241 }
242 if (ensure_path_unmounted(volume) != 0) {
243 LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
244 return -1;
245 }
246 if (strcmp(v->fs_type, "ext4") != 0 && strcmp(v->fs_type, "f2fs") != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700247 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800248 return -1;
Tao Bao3c00fac2017-07-22 16:46:54 -0700249 }
250
251 // If there's a key_loc that looks like a path, it should be a block device for storing encryption
252 // metadata. Wipe it too.
253 if (v->key_loc != nullptr && v->key_loc[0] == '/') {
254 LOG(INFO) << "Wiping " << v->key_loc;
255 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
256 if (fd == -1) {
257 PLOG(ERROR) << "format_volume: Failed to open " << v->key_loc;
258 return -1;
259 }
260 wipe_block_device(fd, get_file_size(fd));
261 close(fd);
262 }
263
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530264 int64_t length = 0;
Jin Qiancc100082017-11-17 23:53:22 -0800265 if (v->length > 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700266 length = v->length;
Jin Qiancc100082017-11-17 23:53:22 -0800267 } else if (v->length < 0 ||
268 (v->key_loc != nullptr && strcmp(v->key_loc, "footer") == 0)) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700269 android::base::unique_fd fd(open(v->blk_device, O_RDONLY));
270 if (fd == -1) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530271 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700272 return -1;
273 }
Jin Qiancc100082017-11-17 23:53:22 -0800274 length =
275 get_file_size(fd.get(), v->length ? -v->length : CRYPT_FOOTER_OFFSET);
Tao Bao3c00fac2017-07-22 16:46:54 -0700276 if (length <= 0) {
Jin Qiancc100082017-11-17 23:53:22 -0800277 LOG(ERROR) << "get_file_size: invalid size " << length << " for "
278 << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700279 return -1;
280 }
281 }
282
283 if (strcmp(v->fs_type, "ext4") == 0) {
284 static constexpr int kBlockSize = 4096;
285 std::vector<std::string> mke2fs_args = {
286 "/sbin/mke2fs_static", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
287 };
288
289 int raid_stride = v->logical_blk_size / kBlockSize;
290 int raid_stripe_width = v->erase_blk_size / kBlockSize;
291 // stride should be the max of 8KB and logical block size
292 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
293 raid_stride = 8192 / kBlockSize;
294 }
295 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
296 mke2fs_args.push_back("-E");
297 mke2fs_args.push_back(
298 android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
299 }
300 mke2fs_args.push_back(v->blk_device);
301 if (length != 0) {
302 mke2fs_args.push_back(std::to_string(length / kBlockSize));
303 }
304
305 int result = exec_cmd(mke2fs_args);
306 if (result == 0 && directory != nullptr) {
307 std::vector<std::string> e2fsdroid_args = {
308 "/sbin/e2fsdroid_static",
309 "-e",
310 "-f",
311 directory,
312 "-a",
313 volume,
314 v->blk_device,
315 };
316 result = exec_cmd(e2fsdroid_args);
317 }
318
319 if (result != 0) {
320 PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
321 return -1;
322 }
323 return 0;
324 }
325
326 // Has to be f2fs because we checked earlier.
Jaegeuk Kim43582622018-04-02 13:37:35 -0700327 static constexpr int kSectorSize = 4096;
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800328 std::string cmd("/sbin/mkfs.f2fs");
Jaegeuk Kim43582622018-04-02 13:37:35 -0700329 // clang-format off
330 std::vector<std::string> make_f2fs_cmd = {
331 cmd,
332 "-d1",
333 "-f",
334 "-O", "encrypt",
335 "-O", "quota",
Jaegeuk Kim2e5dc842018-04-05 22:42:13 -0700336 "-O", "verity",
Jaegeuk Kim43582622018-04-02 13:37:35 -0700337 "-w", std::to_string(kSectorSize),
338 v->blk_device,
339 };
340 // clang-format on
341 if (length >= kSectorSize) {
342 make_f2fs_cmd.push_back(std::to_string(length / kSectorSize));
Tao Bao3c00fac2017-07-22 16:46:54 -0700343 }
344
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800345 int result = exec_cmd(make_f2fs_cmd);
346 if (result == 0 && directory != nullptr) {
347 cmd = "/sbin/sload.f2fs";
Jaegeuk Kim43582622018-04-02 13:37:35 -0700348 // clang-format off
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800349 std::vector<std::string> sload_f2fs_cmd = {
Jaegeuk Kim43582622018-04-02 13:37:35 -0700350 cmd,
351 "-f", directory,
352 "-t", volume,
353 v->blk_device,
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800354 };
Jaegeuk Kim43582622018-04-02 13:37:35 -0700355 // clang-format on
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800356 result = exec_cmd(sload_f2fs_cmd);
357 }
Tao Bao3c00fac2017-07-22 16:46:54 -0700358 if (result != 0) {
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800359 PLOG(ERROR) << "format_volume: Failed " << cmd << " on " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700360 return -1;
361 }
362 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800363}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700364
Paul Lawrenced0db3372015-11-05 13:38:40 -0800365int format_volume(const char* volume) {
Tao Baobb10e582017-07-22 16:30:34 -0700366 return format_volume(volume, nullptr);
Paul Lawrenced0db3372015-11-05 13:38:40 -0800367}
368
Doug Zongker239ac6a2013-08-20 16:03:25 -0700369int setup_install_mounts() {
Tao Bao57130c42017-05-10 12:11:21 -0700370 if (fstab == nullptr) {
371 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
372 return -1;
373 }
374 for (int i = 0; i < fstab->num_entries; ++i) {
375 const Volume* v = fstab->recs + i;
376
377 // We don't want to do anything with "/".
378 if (strcmp(v->mount_point, "/") == 0) {
379 continue;
380 }
381
382 if (strcmp(v->mount_point, "/tmp") == 0 || strcmp(v->mount_point, "/cache") == 0) {
383 if (ensure_path_mounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700384 LOG(ERROR) << "Failed to mount " << v->mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700385 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700386 }
387 } else {
388 if (ensure_path_unmounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700389 LOG(ERROR) << "Failed to unmount " << v->mount_point;
Tao Bao57130c42017-05-10 12:11:21 -0700390 return -1;
391 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700392 }
Tao Bao57130c42017-05-10 12:11:21 -0700393 }
394 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700395}