blob: d7b0dd101a3364b133946bcfd225490368bb6d4d [file] [log] [blame]
The Android Open Source Project9ca14dc2009-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 Lawrencea64f8772014-10-27 10:37:59 -070017#include "sysdeps.h"
18
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080019#include <errno.h>
Mark Salyzyn63e39f22014-04-30 09:10:31 -070020#include <fcntl.h>
Yabin Cui685860d2015-01-02 14:02:14 -080021#include <mntent.h>
Mark Salyzyn63e39f22014-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 Project9ca14dc2009-03-03 19:32:55 -080027
Paul Lawrencea64f8772014-10-27 10:37:59 -070028#include "cutils/properties.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080029
30#define TRACE_TAG TRACE_ADB
31#include "adb.h"
32
33
34static int system_ro = 1;
Daniel Rosenberge9a1c9c2014-06-30 20:29:40 -070035static int vendor_ro = 1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080036
Colin Cross0b573352010-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 Project9ca14dc2009-03-03 19:32:55 -080039{
Yabin Cui685860d2015-01-02 14:02:14 -080040 FILE* fp;
41 struct mntent* mentry;
42 char* device = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080043
Yabin Cui685860d2015-01-02 14:02:14 -080044 if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
Colin Cross0b573352010-05-06 17:06:18 -070045 return NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080046 }
Yabin Cui685860d2015-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 Project9ca14dc2009-03-03 19:32:55 -080055}
56
Daniel Rosenberge9a1c9c2014-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
Sami Tolvanen5638a1a2015-01-02 13:30:50 +000066int make_block_device_writable(const char* dev)
Paul Lawrence8ba2b022014-12-03 15:31:57 -080067{
Paul Lawrence8ba2b022014-12-03 15:31:57 -080068 int fd = -1;
69 int OFF = 0;
70 int rc = -1;
71
Paul Lawrence8ba2b022014-12-03 15:31:57 -080072 if (!dev)
73 goto errout;
74
75 fd = unix_open(dev, O_RDONLY | O_CLOEXEC);
76 if (fd < 0)
77 goto errout;
78
79 if (ioctl(fd, BLKROSET, &OFF)) {
80 goto errout;
81 }
82
83 rc = 0;
84
85errout:
86 if (fd >= 0) {
87 adb_close(fd);
88 }
Paul Lawrence8ba2b022014-12-03 15:31:57 -080089 return rc;
90}
91
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080092/* Init mounts /system as read only, remount to enable writes. */
Daniel Rosenberge9a1c9c2014-06-30 20:29:40 -070093static int remount(const char* dir, int* dir_ro)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080094{
Sami Tolvanen5638a1a2015-01-02 13:30:50 +000095 char *dev = 0;
96 int rc = -1;
Paul Lawrence8ba2b022014-12-03 15:31:57 -080097
Daniel Rosenberge9a1c9c2014-06-30 20:29:40 -070098 dev = find_mount(dir);
Colin Cross0b573352010-05-06 17:06:18 -070099
Sami Tolvanen5638a1a2015-01-02 13:30:50 +0000100 if (!dev || make_block_device_writable(dev)) {
101 goto errout;
102 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800103
Sami Tolvanen5638a1a2015-01-02 13:30:50 +0000104 rc = mount(dev, dir, "none", MS_REMOUNT, NULL);
105 *dir_ro = rc;
Colin Cross0b573352010-05-06 17:06:18 -0700106
Sami Tolvanen5638a1a2015-01-02 13:30:50 +0000107errout:
Colin Cross0b573352010-05-06 17:06:18 -0700108 free(dev);
Sami Tolvanen5638a1a2015-01-02 13:30:50 +0000109 return rc;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800110}
111
112static void write_string(int fd, const char* str)
113{
114 writex(fd, str, strlen(str));
115}
116
117void remount_service(int fd, void *cookie)
118{
Daniel Rosenberge9a1c9c2014-06-30 20:29:40 -0700119 char buffer[200];
Paul Lawrencea64f8772014-10-27 10:37:59 -0700120 char prop_buf[PROPERTY_VALUE_MAX];
121
122 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" : "");
142 write_string(fd, buffer);
143 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");
147 write_string(fd, buffer);
148 }
149
Daniel Rosenberge9a1c9c2014-06-30 20:29:40 -0700150 if (remount("/system", &system_ro)) {
151 snprintf(buffer, sizeof(buffer), "remount of system failed: %s\n",strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800152 write_string(fd, buffer);
153 }
154
Daniel Rosenberge9a1c9c2014-06-30 20:29:40 -0700155 if (hasVendorPartition()) {
156 if (remount("/vendor", &vendor_ro)) {
157 snprintf(buffer, sizeof(buffer), "remount of vendor failed: %s\n",strerror(errno));
158 write_string(fd, buffer);
159 }
160 }
161
162 if (!system_ro && (!vendor_ro || !hasVendorPartition()))
163 write_string(fd, "remount succeeded\n");
164 else {
165 write_string(fd, "remount failed\n");
166 }
167
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800168 adb_close(fd);
169}