blob: 5b3fa9f7ea4165dd81a8fd1f94d87c3585bb0b74 [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
Paul Lawrence34637552014-10-27 10:37:59 -070017#include "sysdeps.h"
18
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019#include <errno.h>
Mark Salyzyn60299df2014-04-30 09:10:31 -070020#include <fcntl.h>
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080021#include <mntent.h>
Mark Salyzyn60299df2014-04-30 09:10:31 -070022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/mount.h>
26#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
Paul Lawrence34637552014-10-27 10:37:59 -070028#include "cutils/properties.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
30#define TRACE_TAG TRACE_ADB
31#include "adb.h"
32
33
34static int system_ro = 1;
Daniel Rosenberg686bce62014-06-30 20:29:40 -070035static int vendor_ro = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036
Colin Crossc880ee02010-05-06 17:06:18 -070037/* Returns the device used to mount a directory in /proc/mounts */
38static char *find_mount(const char *dir)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039{
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080040 FILE* fp;
41 struct mntent* mentry;
42 char* device = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080044 if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
Colin Crossc880ee02010-05-06 17:06:18 -070045 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046 }
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080047 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 Projectdd7bc332009-03-03 19:32:55 -080055}
56
Daniel Rosenberg686bce62014-06-30 20:29:40 -070057static 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 Lawrence982089d2014-12-03 15:31:57 -080066static 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
87errout:
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 Projectdd7bc332009-03-03 19:32:55 -080098/* Init mounts /system as read only, remount to enable writes. */
Daniel Rosenberg686bce62014-06-30 20:29:40 -070099static int remount(const char* dir, int* dir_ro)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100{
Colin Crossc880ee02010-05-06 17:06:18 -0700101 char *dev;
102
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700103 if (dir_ro == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 return 0;
105 }
Colin Crossc880ee02010-05-06 17:06:18 -0700106
Paul Lawrence982089d2014-12-03 15:31:57 -0800107 if (make_block_device_writable(dir)) {
108 return -1;
109 }
110
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700111 dev = find_mount(dir);
Colin Crossc880ee02010-05-06 17:06:18 -0700112
113 if (!dev)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114 return -1;
115
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700116 *dir_ro = mount(dev, dir, "none", MS_REMOUNT, NULL);
Colin Crossc880ee02010-05-06 17:06:18 -0700117
118 free(dev);
119
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700120 return *dir_ro;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121}
122
123static void write_string(int fd, const char* str)
124{
125 writex(fd, str, strlen(str));
126}
127
Paul Lawrence982089d2014-12-03 15:31:57 -0800128int 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 Projectdd7bc332009-03-03 19:32:55 -0800150void remount_service(int fd, void *cookie)
151{
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700152 char buffer[200];
Paul Lawrence34637552014-10-27 10:37:59 -0700153 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 Rosenberg686bce62014-06-30 20:29:40 -0700183 if (remount("/system", &system_ro)) {
184 snprintf(buffer, sizeof(buffer), "remount of system failed: %s\n",strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185 write_string(fd, buffer);
186 }
187
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700188 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 Projectdd7bc332009-03-03 19:32:55 -0800201 adb_close(fd);
202}