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