blob: 2479f88d3bd7fbe8128446ae8f6a23727c14a966 [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>
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 Projectdd7bc332009-03-03 19:32:55 -080026
Paul Lawrence34637552014-10-27 10:37:59 -070027#include "cutils/properties.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
29#define TRACE_TAG TRACE_ADB
30#include "adb.h"
31
32
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{
39 int fd;
40 int res;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041 char *token = NULL;
42 const char delims[] = "\n";
Colin Crossc880ee02010-05-06 17:06:18 -070043 char buf[4096];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044
Nick Kralevichfe8d7f42014-07-18 20:57:35 -070045 fd = unix_open("/proc/mounts", O_RDONLY | O_CLOEXEC);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046 if (fd < 0)
Colin Crossc880ee02010-05-06 17:06:18 -070047 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
49 buf[sizeof(buf) - 1] = '\0';
Mark Salyzyn60299df2014-04-30 09:10:31 -070050 adb_read(fd, buf, sizeof(buf) - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051 adb_close(fd);
52
53 token = strtok(buf, delims);
54
55 while (token) {
Colin Crossc880ee02010-05-06 17:06:18 -070056 char mount_dev[256];
57 char mount_dir[256];
58 int mount_freq;
59 int mount_passno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Colin Crossc880ee02010-05-06 17:06:18 -070061 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 Projectdd7bc332009-03-03 19:32:55 -080067
68 token = strtok(NULL, delims);
69 }
Colin Crossc880ee02010-05-06 17:06:18 -070070 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071}
72
Daniel Rosenberg686bce62014-06-30 20:29:40 -070073static 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
Sami Tolvanen13449cd2015-01-02 13:30:50 +000082int make_block_device_writable(const char* dev)
Paul Lawrence982089d2014-12-03 15:31:57 -080083{
Paul Lawrence982089d2014-12-03 15:31:57 -080084 int fd = -1;
85 int OFF = 0;
86 int rc = -1;
87
Paul Lawrence982089d2014-12-03 15:31:57 -080088 if (!dev)
89 goto errout;
90
91 fd = unix_open(dev, O_RDONLY | O_CLOEXEC);
92 if (fd < 0)
93 goto errout;
94
95 if (ioctl(fd, BLKROSET, &OFF)) {
96 goto errout;
97 }
98
99 rc = 0;
100
101errout:
102 if (fd >= 0) {
103 adb_close(fd);
104 }
Paul Lawrence982089d2014-12-03 15:31:57 -0800105 return rc;
106}
107
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108/* Init mounts /system as read only, remount to enable writes. */
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700109static int remount(const char* dir, int* dir_ro)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110{
Sami Tolvanen13449cd2015-01-02 13:30:50 +0000111 char *dev = 0;
112 int rc = -1;
Paul Lawrence982089d2014-12-03 15:31:57 -0800113
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700114 dev = find_mount(dir);
Colin Crossc880ee02010-05-06 17:06:18 -0700115
Sami Tolvanen13449cd2015-01-02 13:30:50 +0000116 if (!dev || make_block_device_writable(dev)) {
117 goto errout;
118 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119
Sami Tolvanen13449cd2015-01-02 13:30:50 +0000120 rc = mount(dev, dir, "none", MS_REMOUNT, NULL);
121 *dir_ro = rc;
Colin Crossc880ee02010-05-06 17:06:18 -0700122
Sami Tolvanen13449cd2015-01-02 13:30:50 +0000123errout:
Colin Crossc880ee02010-05-06 17:06:18 -0700124 free(dev);
Sami Tolvanen13449cd2015-01-02 13:30:50 +0000125 return rc;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126}
127
128static void write_string(int fd, const char* str)
129{
130 writex(fd, str, strlen(str));
131}
132
133void remount_service(int fd, void *cookie)
134{
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700135 char buffer[200];
Paul Lawrence34637552014-10-27 10:37:59 -0700136 char prop_buf[PROPERTY_VALUE_MAX];
137
138 bool system_verified = false, vendor_verified = false;
139 property_get("partition.system.verified", prop_buf, "0");
140 if (!strcmp(prop_buf, "1")) {
141 system_verified = true;
142 }
143
144 property_get("partition.vendor.verified", prop_buf, "0");
145 if (!strcmp(prop_buf, "1")) {
146 vendor_verified = true;
147 }
148
149 if (system_verified || vendor_verified) {
150 // Allow remount but warn of likely bad effects
151 bool both = system_verified && vendor_verified;
152 snprintf(buffer, sizeof(buffer),
153 "dm_verity is enabled on the %s%s%s partition%s.\n",
154 system_verified ? "system" : "",
155 both ? " and " : "",
156 vendor_verified ? "vendor" : "",
157 both ? "s" : "");
158 write_string(fd, buffer);
159 snprintf(buffer, sizeof(buffer),
160 "Use \"adb disable-verity\" to disable verity.\n"
161 "If you do not, remount may succeed, however, you will still "
162 "not be able to write to these volumes.\n");
163 write_string(fd, buffer);
164 }
165
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700166 if (remount("/system", &system_ro)) {
167 snprintf(buffer, sizeof(buffer), "remount of system failed: %s\n",strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 write_string(fd, buffer);
169 }
170
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700171 if (hasVendorPartition()) {
172 if (remount("/vendor", &vendor_ro)) {
173 snprintf(buffer, sizeof(buffer), "remount of vendor failed: %s\n",strerror(errno));
174 write_string(fd, buffer);
175 }
176 }
177
178 if (!system_ro && (!vendor_ro || !hasVendorPartition()))
179 write_string(fd, "remount succeeded\n");
180 else {
181 write_string(fd, "remount failed\n");
182 }
183
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184 adb_close(fd);
185}