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 | |
| 17 | #include <errno.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <sys/mount.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 23 | #include <ctype.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 24 | |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 25 | #include <fs_mgr.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 26 | #include "mtdutils/mtdutils.h" |
| 27 | #include "mtdutils/mounts.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 28 | #include "roots.h" |
| 29 | #include "common.h" |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 30 | #include "make_ext4fs.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 31 | |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 32 | static struct fstab *fstab = NULL; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 33 | |
Kenny Root | 41dda82 | 2012-03-30 20:48:34 -0700 | [diff] [blame] | 34 | extern struct selabel_handle *sehandle; |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 35 | |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 36 | void load_volume_table() |
| 37 | { |
| 38 | int i; |
| 39 | int ret; |
Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 40 | |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 41 | fstab = fs_mgr_read_fstab("/etc/recovery.fstab"); |
| 42 | if (!fstab) { |
| 43 | LOGE("failed to read /etc/recovery.fstab\n"); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 44 | return; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 45 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 46 | |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 47 | ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk", 0); |
| 48 | if (ret < 0 ) { |
| 49 | LOGE("failed to add /tmp entry to fstab\n"); |
| 50 | fs_mgr_free_fstab(fstab); |
| 51 | fstab = NULL; |
| 52 | return; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 53 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 54 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 55 | printf("recovery filesystem table\n"); |
| 56 | printf("=========================\n"); |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 57 | for (i = 0; i < fstab->num_entries; ++i) { |
| 58 | Volume* v = &fstab->recs[i]; |
| 59 | printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type, |
| 60 | v->blk_device, v->length); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 61 | } |
| 62 | printf("\n"); |
| 63 | } |
| 64 | |
| 65 | Volume* volume_for_path(const char* path) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 66 | return fs_mgr_get_entry_for_mount_point(fstab, path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 69 | int ensure_path_mounted(const char* path) { |
| 70 | Volume* v = volume_for_path(path); |
| 71 | if (v == NULL) { |
| 72 | LOGE("unknown volume for path [%s]\n", path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 73 | return -1; |
| 74 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 75 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 76 | // the ramdisk is always mounted. |
| 77 | return 0; |
| 78 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 79 | |
| 80 | int result; |
| 81 | result = scan_mounted_volumes(); |
| 82 | if (result < 0) { |
| 83 | LOGE("failed to scan mounted volumes\n"); |
| 84 | return -1; |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 85 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 86 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 87 | const MountedVolume* mv = |
| 88 | find_mounted_volume_by_mount_point(v->mount_point); |
| 89 | if (mv) { |
| 90 | // volume is already mounted |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 91 | return 0; |
| 92 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 93 | |
| 94 | mkdir(v->mount_point, 0755); // in case it doesn't already exist |
| 95 | |
| 96 | if (strcmp(v->fs_type, "yaffs2") == 0) { |
| 97 | // mount an MTD partition as a YAFFS2 filesystem. |
| 98 | mtd_scan_partitions(); |
| 99 | const MtdPartition* partition; |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 100 | partition = mtd_find_partition_by_name(v->blk_device); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 101 | if (partition == NULL) { |
| 102 | LOGE("failed to find \"%s\" partition to mount at \"%s\"\n", |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 103 | v->blk_device, v->mount_point); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 104 | return -1; |
| 105 | } |
| 106 | return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0); |
| 107 | } else if (strcmp(v->fs_type, "ext4") == 0 || |
| 108 | strcmp(v->fs_type, "vfat") == 0) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 109 | result = mount(v->blk_device, v->mount_point, v->fs_type, |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 110 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, ""); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 111 | if (result == 0) return 0; |
| 112 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 113 | LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno)); |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 118 | return -1; |
| 119 | } |
| 120 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 121 | int ensure_path_unmounted(const char* path) { |
| 122 | Volume* v = volume_for_path(path); |
| 123 | if (v == NULL) { |
| 124 | LOGE("unknown volume for path [%s]\n", path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 125 | return -1; |
| 126 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 127 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 128 | // the ramdisk is always mounted; you can't unmount it. |
| 129 | return -1; |
| 130 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 131 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 132 | int result; |
| 133 | result = scan_mounted_volumes(); |
| 134 | if (result < 0) { |
| 135 | LOGE("failed to scan mounted volumes\n"); |
| 136 | return -1; |
| 137 | } |
| 138 | |
| 139 | const MountedVolume* mv = |
| 140 | find_mounted_volume_by_mount_point(v->mount_point); |
| 141 | if (mv == NULL) { |
| 142 | // volume is already unmounted |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 143 | return 0; |
| 144 | } |
| 145 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 146 | return unmount_mounted_volume(mv); |
| 147 | } |
| 148 | |
| 149 | int format_volume(const char* volume) { |
| 150 | Volume* v = volume_for_path(volume); |
| 151 | if (v == NULL) { |
| 152 | LOGE("unknown volume \"%s\"\n", volume); |
| 153 | return -1; |
| 154 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 155 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 156 | // you can't format the ramdisk. |
| 157 | LOGE("can't format_volume \"%s\"", volume); |
| 158 | return -1; |
| 159 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 160 | if (strcmp(v->mount_point, volume) != 0) { |
| 161 | LOGE("can't give path \"%s\" to format_volume\n", volume); |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | if (ensure_path_unmounted(volume) != 0) { |
| 166 | LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point); |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 171 | mtd_scan_partitions(); |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 172 | const MtdPartition* partition = mtd_find_partition_by_name(v->blk_device); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 173 | if (partition == NULL) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 174 | LOGE("format_volume: no MTD partition \"%s\"\n", v->blk_device); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 175 | return -1; |
| 176 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 177 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 178 | MtdWriteContext *write = mtd_write_partition(partition); |
| 179 | if (write == NULL) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 180 | LOGW("format_volume: can't open MTD \"%s\"\n", v->blk_device); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 181 | return -1; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 182 | } else if (mtd_erase_blocks(write, -1) == (off_t) -1) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 183 | LOGW("format_volume: can't erase MTD \"%s\"\n", v->blk_device); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 184 | mtd_write_close(write); |
| 185 | return -1; |
| 186 | } else if (mtd_write_close(write)) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 187 | LOGW("format_volume: can't close MTD \"%s\"\n", v->blk_device); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 188 | return -1; |
| 189 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 193 | if (strcmp(v->fs_type, "ext4") == 0) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 194 | int result = make_ext4fs(v->blk_device, v->length, volume, sehandle); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 195 | if (result != 0) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 196 | LOGE("format_volume: make_extf4fs failed on %s\n", v->blk_device); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 197 | return -1; |
| 198 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 199 | return 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 200 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 201 | |
| 202 | LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 203 | return -1; |
| 204 | } |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 205 | |
| 206 | int setup_install_mounts() { |
| 207 | if (fstab == NULL) { |
| 208 | LOGE("can't set up install mounts: no fstab loaded\n"); |
| 209 | return -1; |
| 210 | } |
| 211 | for (int i = 0; i < fstab->num_entries; ++i) { |
| 212 | Volume* v = fstab->recs + i; |
| 213 | |
| 214 | if (strcmp(v->mount_point, "/tmp") == 0 || |
| 215 | strcmp(v->mount_point, "/cache") == 0) { |
| 216 | if (ensure_path_mounted(v->mount_point) != 0) return -1; |
| 217 | |
| 218 | } else { |
| 219 | if (ensure_path_unmounted(v->mount_point) != 0) return -1; |
| 220 | } |
| 221 | } |
| 222 | return 0; |
| 223 | } |