blob: 414b316f8ee721bdbce27ad64d2770becb3805f0 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080017#include <errno.h>
Mark Salyzyn60299df2014-04-30 09:10:31 -070018#include <fcntl.h>
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080019#include <mntent.h>
Mark Salyzyn60299df2014-04-30 09:10:31 -070020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/mount.h>
24#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025
Dan Albert76649012015-02-24 15:51:19 -080026#include "sysdeps.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
28#define TRACE_TAG TRACE_ADB
29#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080030#include "adb_io.h"
Dan Albert76649012015-02-24 15:51:19 -080031#include "cutils/properties.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
33static int system_ro = 1;
Daniel Rosenberg686bce62014-06-30 20:29:40 -070034static int vendor_ro = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
Colin Crossc880ee02010-05-06 17:06:18 -070036/* Returns the device used to mount a directory in /proc/mounts */
Dan Albert6084a012015-03-16 21:35:53 +000037static char *find_mount(const char *dir)
38{
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080039 FILE* fp;
40 struct mntent* mentry;
41 char* device = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080043 if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
Colin Crossc880ee02010-05-06 17:06:18 -070044 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045 }
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080046 while ((mentry = getmntent(fp)) != NULL) {
47 if (strcmp(dir, mentry->mnt_dir) == 0) {
Dan Albert6084a012015-03-16 21:35:53 +000048 device = strdup(mentry->mnt_fsname);
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080049 break;
50 }
51 }
52 endmntent(fp);
53 return device;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054}
55
Dan Albert6084a012015-03-16 21:35:53 +000056static int hasVendorPartition()
57{
58 struct stat info;
59 if (!lstat("/vendor", &info))
60 if ((info.st_mode & S_IFMT) == S_IFDIR)
61 return true;
62 return false;
Daniel Rosenberg686bce62014-06-30 20:29:40 -070063}
64
Dan Albert6084a012015-03-16 21:35:53 +000065int make_block_device_writable(const char* dev)
66{
67 int fd = -1;
Paul Lawrence982089d2014-12-03 15:31:57 -080068 int OFF = 0;
Dan Albert6084a012015-03-16 21:35:53 +000069 int rc = -1;
Paul Lawrence982089d2014-12-03 15:31:57 -080070
Dan Albert6084a012015-03-16 21:35:53 +000071 if (!dev)
72 goto errout;
Paul Lawrence982089d2014-12-03 15:31:57 -080073
Dan Albert6084a012015-03-16 21:35:53 +000074 fd = unix_open(dev, O_RDONLY | O_CLOEXEC);
75 if (fd < 0)
76 goto errout;
77
78 if (ioctl(fd, BLKROSET, &OFF)) {
79 goto errout;
Sami Tolvanen13449cd2015-01-02 13:30:50 +000080 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081
Dan Albert6084a012015-03-16 21:35:53 +000082 rc = 0;
83
84errout:
85 if (fd >= 0) {
86 adb_close(fd);
87 }
Sami Tolvanen13449cd2015-01-02 13:30:50 +000088 return rc;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089}
90
Dan Albert6084a012015-03-16 21:35:53 +000091/* Init mounts /system as read only, remount to enable writes. */
92static int remount(const char* dir, int* dir_ro)
93{
94 char *dev = 0;
95 int rc = -1;
96
97 dev = find_mount(dir);
98
99 if (!dev || make_block_device_writable(dev)) {
100 goto errout;
101 }
102
103 rc = mount(dev, dir, "none", MS_REMOUNT, NULL);
104 *dir_ro = rc;
105
106errout:
107 free(dev);
108 return rc;
MÃ¥rten Kongstad81416fd2014-11-03 13:52:57 +0100109}
110
Dan Albert6084a012015-03-16 21:35:53 +0000111void remount_service(int fd, void *cookie)
112{
113 char buffer[200];
Paul Lawrence34637552014-10-27 10:37:59 -0700114 char prop_buf[PROPERTY_VALUE_MAX];
115
Nick Kralevich268eb4f2015-02-25 15:48:06 -0800116 if (getuid() != 0) {
117 WriteStringFully(fd, "Not running as root. Try \"adb root\" first.\n");
118 adb_close(fd);
119 return;
120 }
121
Paul Lawrence34637552014-10-27 10:37:59 -0700122 bool system_verified = false, vendor_verified = false;
123 property_get("partition.system.verified", prop_buf, "0");
124 if (!strcmp(prop_buf, "1")) {
125 system_verified = true;
126 }
127
128 property_get("partition.vendor.verified", prop_buf, "0");
129 if (!strcmp(prop_buf, "1")) {
130 vendor_verified = true;
131 }
132
133 if (system_verified || vendor_verified) {
134 // Allow remount but warn of likely bad effects
135 bool both = system_verified && vendor_verified;
136 snprintf(buffer, sizeof(buffer),
137 "dm_verity is enabled on the %s%s%s partition%s.\n",
138 system_verified ? "system" : "",
139 both ? " and " : "",
140 vendor_verified ? "vendor" : "",
141 both ? "s" : "");
Dan Albertcc731cc2015-02-24 21:26:58 -0800142 WriteStringFully(fd, buffer);
Paul Lawrence34637552014-10-27 10:37:59 -0700143 snprintf(buffer, sizeof(buffer),
144 "Use \"adb disable-verity\" to disable verity.\n"
145 "If you do not, remount may succeed, however, you will still "
146 "not be able to write to these volumes.\n");
Dan Albertcc731cc2015-02-24 21:26:58 -0800147 WriteStringFully(fd, buffer);
Paul Lawrence34637552014-10-27 10:37:59 -0700148 }
149
Dan Albert6084a012015-03-16 21:35:53 +0000150 if (remount("/system", &system_ro)) {
151 snprintf(buffer, sizeof(buffer), "remount of system failed: %s\n",strerror(errno));
152 WriteStringFully(fd, buffer);
153 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154
Dan Albert6084a012015-03-16 21:35:53 +0000155 if (hasVendorPartition()) {
156 if (remount("/vendor", &vendor_ro)) {
157 snprintf(buffer, sizeof(buffer), "remount of vendor failed: %s\n",strerror(errno));
158 WriteStringFully(fd, buffer);
159 }
160 }
161
162 if (!system_ro && (!vendor_ro || !hasVendorPartition()))
163 WriteStringFully(fd, "remount succeeded\n");
164 else {
165 WriteStringFully(fd, "remount failed\n");
166 }
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700167
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 adb_close(fd);
169}