The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 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 Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 17 | #include "roots.h" |
| 18 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 19 | #include <ctype.h> |
| 20 | #include <fcntl.h> |
Tom Cherry | 45e505a | 2018-11-29 13:33:07 -0800 | [diff] [blame] | 21 | #include <inttypes.h> |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 22 | #include <stdint.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 23 | #include <stdlib.h> |
Tao Bao | ad774b2 | 2017-09-29 10:39:08 -0700 | [diff] [blame] | 24 | #include <string.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 25 | #include <sys/mount.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 28 | #include <sys/wait.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 31 | #include <algorithm> |
| 32 | #include <string> |
| 33 | #include <vector> |
| 34 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 35 | #include <android-base/logging.h> |
Jin Qian | ded2dac | 2017-04-21 14:36:12 -0700 | [diff] [blame] | 36 | #include <android-base/properties.h> |
| 37 | #include <android-base/stringprintf.h> |
Jin Qian | f3ccad5 | 2017-07-24 10:34:35 -0700 | [diff] [blame] | 38 | #include <android-base/unique_fd.h> |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 39 | #include <cryptfs.h> |
Tao Bao | de40ba5 | 2016-10-05 23:17:01 -0700 | [diff] [blame] | 40 | #include <ext4_utils/wipe.h> |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 41 | #include <fs_mgr.h> |
David Anderson | 2b2f423 | 2018-10-29 18:48:56 -0700 | [diff] [blame] | 42 | #include <fs_mgr_dm_linear.h> |
Tao Bao | de40ba5 | 2016-10-05 23:17:01 -0700 | [diff] [blame] | 43 | |
Tao Bao | 9a319f0 | 2018-01-04 13:19:11 -0800 | [diff] [blame] | 44 | #include "otautil/mounts.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 45 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 46 | static struct fstab* fstab = nullptr; |
David Anderson | 2b2f423 | 2018-10-29 18:48:56 -0700 | [diff] [blame] | 47 | static bool did_map_logical_partitions = false; |
Yifan Hong | 4932780 | 2018-11-26 14:59:09 -0800 | [diff] [blame] | 48 | static constexpr const char* SYSTEM_ROOT = "/system"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 49 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 50 | extern struct selabel_handle* sehandle; |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 51 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 52 | void load_volume_table() { |
| 53 | fstab = fs_mgr_read_fstab_default(); |
| 54 | if (!fstab) { |
| 55 | LOG(ERROR) << "Failed to read default fstab"; |
| 56 | return; |
| 57 | } |
Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 58 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 59 | int ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk"); |
| 60 | if (ret == -1) { |
| 61 | LOG(ERROR) << "Failed to add /tmp entry to fstab"; |
| 62 | fs_mgr_free_fstab(fstab); |
| 63 | fstab = nullptr; |
| 64 | return; |
| 65 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 66 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 67 | printf("recovery filesystem table\n"); |
| 68 | printf("=========================\n"); |
| 69 | for (int i = 0; i < fstab->num_entries; ++i) { |
| 70 | const Volume* v = &fstab->recs[i]; |
Tom Cherry | 45e505a | 2018-11-29 13:33:07 -0800 | [diff] [blame] | 71 | printf(" %d %s %s %s %" PRId64 "\n", i, v->mount_point, v->fs_type, v->blk_device, v->length); |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 72 | } |
| 73 | printf("\n"); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Tao Bao | 3e18f2b | 2017-09-29 17:11:13 -0700 | [diff] [blame] | 76 | Volume* volume_for_mount_point(const std::string& mount_point) { |
| 77 | return fs_mgr_get_entry_for_mount_point(fstab, mount_point); |
| 78 | } |
| 79 | |
Tao Bao | 2dfc1a3 | 2017-09-27 13:12:11 -0700 | [diff] [blame] | 80 | // Finds the volume specified by the given path. fs_mgr_get_entry_for_mount_point() does exact match |
| 81 | // only, so it attempts the prefixes recursively (e.g. "/cache/recovery/last_log", |
| 82 | // "/cache/recovery", "/cache", "/" for a given path of "/cache/recovery/last_log") and returns the |
| 83 | // first match or nullptr. |
Tao Bao | 3e18f2b | 2017-09-29 17:11:13 -0700 | [diff] [blame] | 84 | static Volume* volume_for_path(const char* path) { |
Tao Bao | 2dfc1a3 | 2017-09-27 13:12:11 -0700 | [diff] [blame] | 85 | if (path == nullptr || path[0] == '\0') return nullptr; |
| 86 | std::string str(path); |
| 87 | while (true) { |
Tao Bao | ad774b2 | 2017-09-29 10:39:08 -0700 | [diff] [blame] | 88 | Volume* result = fs_mgr_get_entry_for_mount_point(fstab, str); |
Tao Bao | 2dfc1a3 | 2017-09-27 13:12:11 -0700 | [diff] [blame] | 89 | if (result != nullptr || str == "/") { |
| 90 | return result; |
| 91 | } |
| 92 | size_t slash = str.find_last_of('/'); |
| 93 | if (slash == std::string::npos) return nullptr; |
| 94 | if (slash == 0) { |
| 95 | str = "/"; |
| 96 | } else { |
| 97 | str = str.substr(0, slash); |
| 98 | } |
| 99 | } |
| 100 | return nullptr; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Tao Bao | abb8f77 | 2015-07-30 14:43:27 -0700 | [diff] [blame] | 103 | // Mount the volume specified by path at the given mount_point. |
| 104 | int ensure_path_mounted_at(const char* path, const char* mount_point) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 105 | Volume* v = volume_for_path(path); |
| 106 | if (v == nullptr) { |
| 107 | LOG(ERROR) << "unknown volume for path [" << path << "]"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 108 | return -1; |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 109 | } |
| 110 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 111 | // The ramdisk is always mounted. |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | if (!scan_mounted_volumes()) { |
| 116 | LOG(ERROR) << "Failed to scan mounted volumes"; |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | if (!mount_point) { |
| 121 | mount_point = v->mount_point; |
| 122 | } |
| 123 | |
David Anderson | 2b2f423 | 2018-10-29 18:48:56 -0700 | [diff] [blame] | 124 | // If we can't acquire the block device for a logical partition, it likely |
| 125 | // was never created. In that case we try to create it. |
| 126 | if (fs_mgr_is_logical(v) && !fs_mgr_update_logical_partition(v)) { |
| 127 | if (did_map_logical_partitions) { |
| 128 | LOG(ERROR) << "Failed to find block device for partition"; |
| 129 | return -1; |
| 130 | } |
| 131 | std::string super_name = fs_mgr_get_super_partition_name(); |
| 132 | if (!android::fs_mgr::CreateLogicalPartitions(super_name)) { |
| 133 | LOG(ERROR) << "Failed to create logical partitions"; |
| 134 | return -1; |
| 135 | } |
| 136 | did_map_logical_partitions = true; |
| 137 | if (!fs_mgr_update_logical_partition(v)) { |
| 138 | LOG(ERROR) << "Failed to find block device for partition"; |
| 139 | return -1; |
| 140 | } |
| 141 | } |
| 142 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 143 | const MountedVolume* mv = find_mounted_volume_by_mount_point(mount_point); |
| 144 | if (mv != nullptr) { |
| 145 | // Volume is already mounted. |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | mkdir(mount_point, 0755); // in case it doesn't already exist |
| 150 | |
| 151 | if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "squashfs") == 0 || |
Lianjun Huang | 5d7be6b | 2018-09-03 18:07:35 +0800 | [diff] [blame] | 152 | strcmp(v->fs_type, "vfat") == 0 || strcmp(v->fs_type, "f2fs") == 0) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 153 | int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options); |
| 154 | if (result == -1 && fs_mgr_is_formattable(v)) { |
| 155 | PLOG(ERROR) << "Failed to mount " << mount_point << "; formatting"; |
| 156 | bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer"); |
| 157 | if (fs_mgr_do_format(v, crypt_footer) == 0) { |
| 158 | result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options); |
| 159 | } else { |
| 160 | PLOG(ERROR) << "Failed to format " << mount_point; |
| 161 | return -1; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (result == -1) { |
| 166 | PLOG(ERROR) << "Failed to mount " << mount_point; |
| 167 | return -1; |
| 168 | } |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << mount_point; |
| 173 | return -1; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Tao Bao | abb8f77 | 2015-07-30 14:43:27 -0700 | [diff] [blame] | 176 | int ensure_path_mounted(const char* path) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 177 | // Mount at the default mount point. |
| 178 | return ensure_path_mounted_at(path, nullptr); |
Tao Bao | abb8f77 | 2015-07-30 14:43:27 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 181 | int ensure_path_unmounted(const char* path) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 182 | const Volume* v = volume_for_path(path); |
| 183 | if (v == nullptr) { |
| 184 | LOG(ERROR) << "unknown volume for path [" << path << "]"; |
| 185 | return -1; |
| 186 | } |
| 187 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 188 | // The ramdisk is always mounted; you can't unmount it. |
| 189 | return -1; |
| 190 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 191 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 192 | if (!scan_mounted_volumes()) { |
| 193 | LOG(ERROR) << "Failed to scan mounted volumes"; |
| 194 | return -1; |
| 195 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 196 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 197 | MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point); |
| 198 | if (mv == nullptr) { |
| 199 | // Volume is already unmounted. |
| 200 | return 0; |
| 201 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 202 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 203 | return unmount_mounted_volume(mv); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 206 | static int exec_cmd(const std::vector<std::string>& args) { |
| 207 | CHECK_NE(static_cast<size_t>(0), args.size()); |
| 208 | |
| 209 | std::vector<char*> argv(args.size()); |
| 210 | std::transform(args.cbegin(), args.cend(), argv.begin(), |
| 211 | [](const std::string& arg) { return const_cast<char*>(arg.c_str()); }); |
| 212 | argv.push_back(nullptr); |
| 213 | |
| 214 | pid_t child; |
George Burgess IV | 1cfb361 | 2018-02-17 17:48:45 -0800 | [diff] [blame] | 215 | if ((child = fork()) == 0) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 216 | execv(argv[0], argv.data()); |
| 217 | _exit(EXIT_FAILURE); |
| 218 | } |
| 219 | |
| 220 | int status; |
| 221 | waitpid(child, &status, 0); |
| 222 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 223 | LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status); |
| 224 | } |
| 225 | return WEXITSTATUS(status); |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 228 | static int64_t get_file_size(int fd, uint64_t reserve_len) { |
Jin Qian | f3ccad5 | 2017-07-24 10:34:35 -0700 | [diff] [blame] | 229 | struct stat buf; |
| 230 | int ret = fstat(fd, &buf); |
| 231 | if (ret) return 0; |
| 232 | |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 233 | int64_t computed_size; |
Jin Qian | f3ccad5 | 2017-07-24 10:34:35 -0700 | [diff] [blame] | 234 | if (S_ISREG(buf.st_mode)) { |
| 235 | computed_size = buf.st_size - reserve_len; |
| 236 | } else if (S_ISBLK(buf.st_mode)) { |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 237 | uint64_t block_device_size = get_block_device_size(fd); |
| 238 | if (block_device_size < reserve_len || |
| 239 | block_device_size > std::numeric_limits<int64_t>::max()) { |
| 240 | computed_size = 0; |
| 241 | } else { |
| 242 | computed_size = block_device_size - reserve_len; |
| 243 | } |
Jin Qian | f3ccad5 | 2017-07-24 10:34:35 -0700 | [diff] [blame] | 244 | } else { |
| 245 | computed_size = 0; |
| 246 | } |
| 247 | |
| 248 | return computed_size; |
| 249 | } |
| 250 | |
Paul Lawrence | d0db337 | 2015-11-05 13:38:40 -0800 | [diff] [blame] | 251 | int format_volume(const char* volume, const char* directory) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 252 | const Volume* v = volume_for_path(volume); |
| 253 | if (v == nullptr) { |
| 254 | LOG(ERROR) << "unknown volume \"" << volume << "\""; |
| 255 | return -1; |
| 256 | } |
| 257 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 258 | LOG(ERROR) << "can't format_volume \"" << volume << "\""; |
| 259 | return -1; |
| 260 | } |
| 261 | if (strcmp(v->mount_point, volume) != 0) { |
| 262 | LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume"; |
| 263 | return -1; |
| 264 | } |
| 265 | if (ensure_path_unmounted(volume) != 0) { |
| 266 | LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\""; |
| 267 | return -1; |
| 268 | } |
| 269 | if (strcmp(v->fs_type, "ext4") != 0 && strcmp(v->fs_type, "f2fs") != 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 270 | LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 271 | return -1; |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // If there's a key_loc that looks like a path, it should be a block device for storing encryption |
| 275 | // metadata. Wipe it too. |
| 276 | if (v->key_loc != nullptr && v->key_loc[0] == '/') { |
| 277 | LOG(INFO) << "Wiping " << v->key_loc; |
| 278 | int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644); |
| 279 | if (fd == -1) { |
| 280 | PLOG(ERROR) << "format_volume: Failed to open " << v->key_loc; |
| 281 | return -1; |
| 282 | } |
| 283 | wipe_block_device(fd, get_file_size(fd)); |
| 284 | close(fd); |
| 285 | } |
| 286 | |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 287 | int64_t length = 0; |
Jin Qian | cc10008 | 2017-11-17 23:53:22 -0800 | [diff] [blame] | 288 | if (v->length > 0) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 289 | length = v->length; |
Jin Qian | cc10008 | 2017-11-17 23:53:22 -0800 | [diff] [blame] | 290 | } else if (v->length < 0 || |
| 291 | (v->key_loc != nullptr && strcmp(v->key_loc, "footer") == 0)) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 292 | android::base::unique_fd fd(open(v->blk_device, O_RDONLY)); |
| 293 | if (fd == -1) { |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 294 | PLOG(ERROR) << "format_volume: failed to open " << v->blk_device; |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 295 | return -1; |
| 296 | } |
Jin Qian | cc10008 | 2017-11-17 23:53:22 -0800 | [diff] [blame] | 297 | length = |
| 298 | get_file_size(fd.get(), v->length ? -v->length : CRYPT_FOOTER_OFFSET); |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 299 | if (length <= 0) { |
Jin Qian | cc10008 | 2017-11-17 23:53:22 -0800 | [diff] [blame] | 300 | LOG(ERROR) << "get_file_size: invalid size " << length << " for " |
| 301 | << v->blk_device; |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 302 | return -1; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (strcmp(v->fs_type, "ext4") == 0) { |
| 307 | static constexpr int kBlockSize = 4096; |
| 308 | std::vector<std::string> mke2fs_args = { |
Jiyong Park | 69364fe | 2018-06-20 14:18:18 +0900 | [diff] [blame] | 309 | "/system/bin/mke2fs", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize), |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 310 | }; |
| 311 | |
| 312 | int raid_stride = v->logical_blk_size / kBlockSize; |
| 313 | int raid_stripe_width = v->erase_blk_size / kBlockSize; |
| 314 | // stride should be the max of 8KB and logical block size |
| 315 | if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) { |
| 316 | raid_stride = 8192 / kBlockSize; |
| 317 | } |
| 318 | if (v->erase_blk_size != 0 && v->logical_blk_size != 0) { |
| 319 | mke2fs_args.push_back("-E"); |
| 320 | mke2fs_args.push_back( |
| 321 | android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width)); |
| 322 | } |
| 323 | mke2fs_args.push_back(v->blk_device); |
| 324 | if (length != 0) { |
| 325 | mke2fs_args.push_back(std::to_string(length / kBlockSize)); |
| 326 | } |
| 327 | |
| 328 | int result = exec_cmd(mke2fs_args); |
| 329 | if (result == 0 && directory != nullptr) { |
| 330 | std::vector<std::string> e2fsdroid_args = { |
Jiyong Park | 69364fe | 2018-06-20 14:18:18 +0900 | [diff] [blame] | 331 | "/system/bin/e2fsdroid", "-e", "-f", directory, "-a", volume, v->blk_device, |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 332 | }; |
| 333 | result = exec_cmd(e2fsdroid_args); |
| 334 | } |
| 335 | |
| 336 | if (result != 0) { |
| 337 | PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device; |
| 338 | return -1; |
| 339 | } |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | // Has to be f2fs because we checked earlier. |
Jaegeuk Kim | 4358262 | 2018-04-02 13:37:35 -0700 | [diff] [blame] | 344 | static constexpr int kSectorSize = 4096; |
Jaegeuk Kim | c1c7311 | 2017-11-28 19:48:05 -0800 | [diff] [blame] | 345 | std::string cmd("/sbin/mkfs.f2fs"); |
Jaegeuk Kim | 4358262 | 2018-04-02 13:37:35 -0700 | [diff] [blame] | 346 | // clang-format off |
| 347 | std::vector<std::string> make_f2fs_cmd = { |
| 348 | cmd, |
Jaegeuk Kim | 91e631d | 2018-11-21 11:12:54 -0800 | [diff] [blame] | 349 | "-g", "android", |
Jaegeuk Kim | 4358262 | 2018-04-02 13:37:35 -0700 | [diff] [blame] | 350 | v->blk_device, |
| 351 | }; |
| 352 | // clang-format on |
| 353 | if (length >= kSectorSize) { |
| 354 | make_f2fs_cmd.push_back(std::to_string(length / kSectorSize)); |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Jaegeuk Kim | c1c7311 | 2017-11-28 19:48:05 -0800 | [diff] [blame] | 357 | int result = exec_cmd(make_f2fs_cmd); |
| 358 | if (result == 0 && directory != nullptr) { |
| 359 | cmd = "/sbin/sload.f2fs"; |
Jaegeuk Kim | 4358262 | 2018-04-02 13:37:35 -0700 | [diff] [blame] | 360 | // clang-format off |
Jaegeuk Kim | c1c7311 | 2017-11-28 19:48:05 -0800 | [diff] [blame] | 361 | std::vector<std::string> sload_f2fs_cmd = { |
Jaegeuk Kim | 4358262 | 2018-04-02 13:37:35 -0700 | [diff] [blame] | 362 | cmd, |
| 363 | "-f", directory, |
| 364 | "-t", volume, |
| 365 | v->blk_device, |
Jaegeuk Kim | c1c7311 | 2017-11-28 19:48:05 -0800 | [diff] [blame] | 366 | }; |
Jaegeuk Kim | 4358262 | 2018-04-02 13:37:35 -0700 | [diff] [blame] | 367 | // clang-format on |
Jaegeuk Kim | c1c7311 | 2017-11-28 19:48:05 -0800 | [diff] [blame] | 368 | result = exec_cmd(sload_f2fs_cmd); |
| 369 | } |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 370 | if (result != 0) { |
Jaegeuk Kim | c1c7311 | 2017-11-28 19:48:05 -0800 | [diff] [blame] | 371 | PLOG(ERROR) << "format_volume: Failed " << cmd << " on " << v->blk_device; |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 372 | return -1; |
| 373 | } |
| 374 | return 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 375 | } |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 376 | |
Paul Lawrence | d0db337 | 2015-11-05 13:38:40 -0800 | [diff] [blame] | 377 | int format_volume(const char* volume) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 378 | return format_volume(volume, nullptr); |
Paul Lawrence | d0db337 | 2015-11-05 13:38:40 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 381 | int setup_install_mounts() { |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 382 | if (fstab == nullptr) { |
| 383 | LOG(ERROR) << "can't set up install mounts: no fstab loaded"; |
| 384 | return -1; |
| 385 | } |
| 386 | for (int i = 0; i < fstab->num_entries; ++i) { |
| 387 | const Volume* v = fstab->recs + i; |
| 388 | |
| 389 | // We don't want to do anything with "/". |
| 390 | if (strcmp(v->mount_point, "/") == 0) { |
| 391 | continue; |
| 392 | } |
| 393 | |
| 394 | if (strcmp(v->mount_point, "/tmp") == 0 || strcmp(v->mount_point, "/cache") == 0) { |
| 395 | if (ensure_path_mounted(v->mount_point) != 0) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 396 | LOG(ERROR) << "Failed to mount " << v->mount_point; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 397 | return -1; |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 398 | } |
| 399 | } else { |
| 400 | if (ensure_path_unmounted(v->mount_point) != 0) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 401 | LOG(ERROR) << "Failed to unmount " << v->mount_point; |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 402 | return -1; |
| 403 | } |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 404 | } |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 405 | } |
| 406 | return 0; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 407 | } |
David Anderson | 2b2f423 | 2018-10-29 18:48:56 -0700 | [diff] [blame] | 408 | |
| 409 | bool logical_partitions_mapped() { |
| 410 | return did_map_logical_partitions; |
| 411 | } |
Yifan Hong | 4932780 | 2018-11-26 14:59:09 -0800 | [diff] [blame] | 412 | |
| 413 | std::string get_system_root() { |
| 414 | if (volume_for_mount_point(SYSTEM_ROOT) == nullptr) { |
| 415 | return "/"; |
| 416 | } else { |
| 417 | return SYSTEM_ROOT; |
| 418 | } |
| 419 | } |