The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Paul Lawrence | 3463755 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 17 | #include "sysdeps.h" |
| 18 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | #include <errno.h> |
Mark Salyzyn | 60299df | 2014-04-30 09:10:31 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame^] | 21 | #include <mntent.h> |
Mark Salyzyn | 60299df | 2014-04-30 09:10:31 -0700 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/mount.h> |
| 26 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | |
Paul Lawrence | 3463755 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 28 | #include "cutils/properties.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 | |
| 30 | #define TRACE_TAG TRACE_ADB |
| 31 | #include "adb.h" |
| 32 | |
| 33 | |
| 34 | static int system_ro = 1; |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 35 | static int vendor_ro = 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | |
Colin Cross | c880ee0 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 37 | /* Returns the device used to mount a directory in /proc/mounts */ |
| 38 | static char *find_mount(const char *dir) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 39 | { |
Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame^] | 40 | FILE* fp; |
| 41 | struct mntent* mentry; |
| 42 | char* device = NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | |
Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame^] | 44 | if ((fp = setmntent("/proc/mounts", "r")) == NULL) { |
Colin Cross | c880ee0 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 45 | return NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | } |
Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame^] | 47 | while ((mentry = getmntent(fp)) != NULL) { |
| 48 | if (strcmp(dir, mentry->mnt_dir) == 0) { |
| 49 | device = strdup(mentry->mnt_fsname); |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | endmntent(fp); |
| 54 | return device; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 57 | static int hasVendorPartition() |
| 58 | { |
| 59 | struct stat info; |
| 60 | if (!lstat("/vendor", &info)) |
| 61 | if ((info.st_mode & S_IFMT) == S_IFDIR) |
| 62 | return true; |
| 63 | return false; |
| 64 | } |
| 65 | |
Paul Lawrence | 982089d | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 66 | static int make_block_device_writable(const char* dir) |
| 67 | { |
| 68 | char *dev = 0; |
| 69 | int fd = -1; |
| 70 | int OFF = 0; |
| 71 | int rc = -1; |
| 72 | |
| 73 | dev = find_mount(dir); |
| 74 | if (!dev) |
| 75 | goto errout; |
| 76 | |
| 77 | fd = unix_open(dev, O_RDONLY | O_CLOEXEC); |
| 78 | if (fd < 0) |
| 79 | goto errout; |
| 80 | |
| 81 | if (ioctl(fd, BLKROSET, &OFF)) { |
| 82 | goto errout; |
| 83 | } |
| 84 | |
| 85 | rc = 0; |
| 86 | |
| 87 | errout: |
| 88 | if (fd >= 0) { |
| 89 | adb_close(fd); |
| 90 | } |
| 91 | |
| 92 | if (dev) { |
| 93 | free(dev); |
| 94 | } |
| 95 | return rc; |
| 96 | } |
| 97 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 98 | /* Init mounts /system as read only, remount to enable writes. */ |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 99 | static int remount(const char* dir, int* dir_ro) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 100 | { |
Colin Cross | c880ee0 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 101 | char *dev; |
| 102 | |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 103 | if (dir_ro == 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 104 | return 0; |
| 105 | } |
Colin Cross | c880ee0 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 106 | |
Paul Lawrence | 982089d | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 107 | if (make_block_device_writable(dir)) { |
| 108 | return -1; |
| 109 | } |
| 110 | |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 111 | dev = find_mount(dir); |
Colin Cross | c880ee0 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 112 | |
| 113 | if (!dev) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 114 | return -1; |
| 115 | |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 116 | *dir_ro = mount(dev, dir, "none", MS_REMOUNT, NULL); |
Colin Cross | c880ee0 | 2010-05-06 17:06:18 -0700 | [diff] [blame] | 117 | |
| 118 | free(dev); |
| 119 | |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 120 | return *dir_ro; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static void write_string(int fd, const char* str) |
| 124 | { |
| 125 | writex(fd, str, strlen(str)); |
| 126 | } |
| 127 | |
Paul Lawrence | 982089d | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 128 | int make_system_and_vendor_block_devices_writable(int fd) |
| 129 | { |
| 130 | char buffer[200]; |
| 131 | if (make_block_device_writable("/system")) { |
| 132 | snprintf(buffer, sizeof(buffer), |
| 133 | "Failed to make system block device writable %s\n", |
| 134 | strerror(errno)); |
| 135 | write_string(fd, buffer); |
| 136 | return -1; |
| 137 | } |
| 138 | |
| 139 | if (hasVendorPartition() && make_block_device_writable("/vendor")) { |
| 140 | snprintf(buffer, sizeof(buffer), |
| 141 | "Failed to make vendor block device writable: %s\n", |
| 142 | strerror(errno)); |
| 143 | write_string(fd, buffer); |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 150 | void remount_service(int fd, void *cookie) |
| 151 | { |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 152 | char buffer[200]; |
Paul Lawrence | 3463755 | 2014-10-27 10:37:59 -0700 | [diff] [blame] | 153 | char prop_buf[PROPERTY_VALUE_MAX]; |
| 154 | |
| 155 | bool system_verified = false, vendor_verified = false; |
| 156 | property_get("partition.system.verified", prop_buf, "0"); |
| 157 | if (!strcmp(prop_buf, "1")) { |
| 158 | system_verified = true; |
| 159 | } |
| 160 | |
| 161 | property_get("partition.vendor.verified", prop_buf, "0"); |
| 162 | if (!strcmp(prop_buf, "1")) { |
| 163 | vendor_verified = true; |
| 164 | } |
| 165 | |
| 166 | if (system_verified || vendor_verified) { |
| 167 | // Allow remount but warn of likely bad effects |
| 168 | bool both = system_verified && vendor_verified; |
| 169 | snprintf(buffer, sizeof(buffer), |
| 170 | "dm_verity is enabled on the %s%s%s partition%s.\n", |
| 171 | system_verified ? "system" : "", |
| 172 | both ? " and " : "", |
| 173 | vendor_verified ? "vendor" : "", |
| 174 | both ? "s" : ""); |
| 175 | write_string(fd, buffer); |
| 176 | snprintf(buffer, sizeof(buffer), |
| 177 | "Use \"adb disable-verity\" to disable verity.\n" |
| 178 | "If you do not, remount may succeed, however, you will still " |
| 179 | "not be able to write to these volumes.\n"); |
| 180 | write_string(fd, buffer); |
| 181 | } |
| 182 | |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 183 | if (remount("/system", &system_ro)) { |
| 184 | snprintf(buffer, sizeof(buffer), "remount of system failed: %s\n",strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 185 | write_string(fd, buffer); |
| 186 | } |
| 187 | |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 188 | if (hasVendorPartition()) { |
| 189 | if (remount("/vendor", &vendor_ro)) { |
| 190 | snprintf(buffer, sizeof(buffer), "remount of vendor failed: %s\n",strerror(errno)); |
| 191 | write_string(fd, buffer); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if (!system_ro && (!vendor_ro || !hasVendorPartition())) |
| 196 | write_string(fd, "remount succeeded\n"); |
| 197 | else { |
| 198 | write_string(fd, "remount failed\n"); |
| 199 | } |
| 200 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 201 | adb_close(fd); |
| 202 | } |