blob: 7e5613ab66a769ce52e7adbe0bb12a0f1b912793 [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 Albert76649012015-02-24 15:51:19 -080030#include "cutils/properties.h"
31#include "transport.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 */
37static char *find_mount(const char *dir)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038{
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) {
48 device = strdup(mentry->mnt_fsname);
49 break;
50 }
51 }
52 endmntent(fp);
53 return device;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054}
55
Daniel Rosenberg686bce62014-06-30 20:29:40 -070056static 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;
63}
64
Sami Tolvanen13449cd2015-01-02 13:30:50 +000065int make_block_device_writable(const char* dev)
Paul Lawrence982089d2014-12-03 15:31:57 -080066{
Paul Lawrence982089d2014-12-03 15:31:57 -080067 int fd = -1;
68 int OFF = 0;
69 int rc = -1;
70
Paul Lawrence982089d2014-12-03 15:31:57 -080071 if (!dev)
72 goto errout;
73
74 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;
80 }
81
82 rc = 0;
83
84errout:
85 if (fd >= 0) {
86 adb_close(fd);
87 }
Paul Lawrence982089d2014-12-03 15:31:57 -080088 return rc;
89}
90
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091/* Init mounts /system as read only, remount to enable writes. */
Daniel Rosenberg686bce62014-06-30 20:29:40 -070092static int remount(const char* dir, int* dir_ro)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093{
Sami Tolvanen13449cd2015-01-02 13:30:50 +000094 char *dev = 0;
95 int rc = -1;
Paul Lawrence982089d2014-12-03 15:31:57 -080096
Daniel Rosenberg686bce62014-06-30 20:29:40 -070097 dev = find_mount(dir);
Colin Crossc880ee02010-05-06 17:06:18 -070098
Sami Tolvanen13449cd2015-01-02 13:30:50 +000099 if (!dev || make_block_device_writable(dev)) {
100 goto errout;
101 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102
Sami Tolvanen13449cd2015-01-02 13:30:50 +0000103 rc = mount(dev, dir, "none", MS_REMOUNT, NULL);
104 *dir_ro = rc;
Colin Crossc880ee02010-05-06 17:06:18 -0700105
Sami Tolvanen13449cd2015-01-02 13:30:50 +0000106errout:
Colin Crossc880ee02010-05-06 17:06:18 -0700107 free(dev);
Sami Tolvanen13449cd2015-01-02 13:30:50 +0000108 return rc;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109}
110
111static void write_string(int fd, const char* str)
112{
113 writex(fd, str, strlen(str));
114}
115
116void remount_service(int fd, void *cookie)
117{
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700118 char buffer[200];
Paul Lawrence34637552014-10-27 10:37:59 -0700119 char prop_buf[PROPERTY_VALUE_MAX];
120
121 bool system_verified = false, vendor_verified = false;
122 property_get("partition.system.verified", prop_buf, "0");
123 if (!strcmp(prop_buf, "1")) {
124 system_verified = true;
125 }
126
127 property_get("partition.vendor.verified", prop_buf, "0");
128 if (!strcmp(prop_buf, "1")) {
129 vendor_verified = true;
130 }
131
132 if (system_verified || vendor_verified) {
133 // Allow remount but warn of likely bad effects
134 bool both = system_verified && vendor_verified;
135 snprintf(buffer, sizeof(buffer),
136 "dm_verity is enabled on the %s%s%s partition%s.\n",
137 system_verified ? "system" : "",
138 both ? " and " : "",
139 vendor_verified ? "vendor" : "",
140 both ? "s" : "");
141 write_string(fd, buffer);
142 snprintf(buffer, sizeof(buffer),
143 "Use \"adb disable-verity\" to disable verity.\n"
144 "If you do not, remount may succeed, however, you will still "
145 "not be able to write to these volumes.\n");
146 write_string(fd, buffer);
147 }
148
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700149 if (remount("/system", &system_ro)) {
150 snprintf(buffer, sizeof(buffer), "remount of system failed: %s\n",strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151 write_string(fd, buffer);
152 }
153
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700154 if (hasVendorPartition()) {
155 if (remount("/vendor", &vendor_ro)) {
156 snprintf(buffer, sizeof(buffer), "remount of vendor failed: %s\n",strerror(errno));
157 write_string(fd, buffer);
158 }
159 }
160
161 if (!system_ro && (!vendor_ro || !hasVendorPartition()))
162 write_string(fd, "remount succeeded\n");
163 else {
164 write_string(fd, "remount failed\n");
165 }
166
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 adb_close(fd);
168}