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