blob: 483ca3d3dd82851bdcab8508d93207974f5e9fe3 [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
Dan Albert33134262015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_ADB
18
19#include "sysdeps.h"
20
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <errno.h>
Mark Salyzyn60299df2014-04-30 09:10:31 -070022#include <fcntl.h>
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080023#include <mntent.h>
Mark Salyzyn60299df2014-04-30 09:10:31 -070024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/mount.h>
28#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
Elliott Hughesec7a6672015-03-16 21:58:32 +000030#include <string>
31
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080033#include "adb_io.h"
Dan Albert76649012015-02-24 15:51:19 -080034#include "cutils/properties.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
36static int system_ro = 1;
Daniel Rosenberg686bce62014-06-30 20:29:40 -070037static int vendor_ro = 1;
Elliott Hughesec7a6672015-03-16 21:58:32 +000038static int oem_ro = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
Colin Crossc880ee02010-05-06 17:06:18 -070040/* Returns the device used to mount a directory in /proc/mounts */
Elliott Hughesec7a6672015-03-16 21:58:32 +000041static std::string find_mount(const char *dir) {
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080042 FILE* fp;
43 struct mntent* mentry;
44 char* device = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080046 if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
Colin Crossc880ee02010-05-06 17:06:18 -070047 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048 }
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080049 while ((mentry = getmntent(fp)) != NULL) {
50 if (strcmp(dir, mentry->mnt_dir) == 0) {
Elliott Hughesec7a6672015-03-16 21:58:32 +000051 device = mentry->mnt_fsname;
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080052 break;
53 }
54 }
55 endmntent(fp);
56 return device;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057}
58
Elliott Hughesec7a6672015-03-16 21:58:32 +000059static bool has_partition(const char* path) {
60 struct stat sb;
61 return (lstat(path, &sb) == 0 && S_ISDIR(sb.st_mode));
Daniel Rosenberg686bce62014-06-30 20:29:40 -070062}
63
Elliott Hughesec7a6672015-03-16 21:58:32 +000064int make_block_device_writable(const std::string& dev) {
65 int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC);
66 if (fd == -1) {
67 return -1;
68 }
69
70 int result = -1;
Paul Lawrence982089d2014-12-03 15:31:57 -080071 int OFF = 0;
Elliott Hughesec7a6672015-03-16 21:58:32 +000072 if (!ioctl(fd, BLKROSET, &OFF)) {
73 result = 0;
Sami Tolvanen13449cd2015-01-02 13:30:50 +000074 }
Elliott Hughesec7a6672015-03-16 21:58:32 +000075 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076
Elliott Hughesec7a6672015-03-16 21:58:32 +000077 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078}
79
Elliott Hughesec7a6672015-03-16 21:58:32 +000080// Init mounts /system as read only, remount to enable writes.
81static int remount(const char* dir, int* dir_ro) {
82 std::string dev(find_mount(dir));
83 if (dev.empty() || make_block_device_writable(dev)) {
84 return -1;
Dan Albert6084a012015-03-16 21:35:53 +000085 }
86
Elliott Hughesec7a6672015-03-16 21:58:32 +000087 int rc = mount(dev.c_str(), dir, "none", MS_REMOUNT, NULL);
Dan Albert6084a012015-03-16 21:35:53 +000088 *dir_ro = rc;
Dan Albert6084a012015-03-16 21:35:53 +000089 return rc;
MÃ¥rten Kongstad81416fd2014-11-03 13:52:57 +010090}
91
Elliott Hughesec7a6672015-03-16 21:58:32 +000092static bool remount_partition(int fd, const char* partition, int* ro) {
93 if (!has_partition(partition)) {
94 return true;
95 }
96 if (remount(partition, ro)) {
97 char buf[200];
98 snprintf(buf, sizeof(buf), "remount of %s failed: %s\n", partition, strerror(errno));
99 WriteStringFully(fd, buf);
100 return false;
101 }
102 return true;
103}
104
105void remount_service(int fd, void* cookie) {
Paul Lawrence34637552014-10-27 10:37:59 -0700106 char prop_buf[PROPERTY_VALUE_MAX];
107
Nick Kralevich268eb4f2015-02-25 15:48:06 -0800108 if (getuid() != 0) {
109 WriteStringFully(fd, "Not running as root. Try \"adb root\" first.\n");
110 adb_close(fd);
111 return;
112 }
113
Paul Lawrence34637552014-10-27 10:37:59 -0700114 bool system_verified = false, vendor_verified = false;
115 property_get("partition.system.verified", prop_buf, "0");
116 if (!strcmp(prop_buf, "1")) {
117 system_verified = true;
118 }
119
120 property_get("partition.vendor.verified", prop_buf, "0");
121 if (!strcmp(prop_buf, "1")) {
122 vendor_verified = true;
123 }
124
125 if (system_verified || vendor_verified) {
126 // Allow remount but warn of likely bad effects
127 bool both = system_verified && vendor_verified;
Elliott Hughesec7a6672015-03-16 21:58:32 +0000128 char buffer[200];
Paul Lawrence34637552014-10-27 10:37:59 -0700129 snprintf(buffer, sizeof(buffer),
130 "dm_verity is enabled on the %s%s%s partition%s.\n",
131 system_verified ? "system" : "",
132 both ? " and " : "",
133 vendor_verified ? "vendor" : "",
134 both ? "s" : "");
Dan Albertcc731cc2015-02-24 21:26:58 -0800135 WriteStringFully(fd, buffer);
Paul Lawrence34637552014-10-27 10:37:59 -0700136 snprintf(buffer, sizeof(buffer),
137 "Use \"adb disable-verity\" to disable verity.\n"
138 "If you do not, remount may succeed, however, you will still "
139 "not be able to write to these volumes.\n");
Dan Albertcc731cc2015-02-24 21:26:58 -0800140 WriteStringFully(fd, buffer);
Paul Lawrence34637552014-10-27 10:37:59 -0700141 }
142
Elliott Hughesec7a6672015-03-16 21:58:32 +0000143 bool success = true;
144 success &= remount_partition(fd, "/system", &system_ro);
145 success &= remount_partition(fd, "/vendor", &vendor_ro);
146 success &= remount_partition(fd, "/oem", &oem_ro);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147
Elliott Hughesec7a6672015-03-16 21:58:32 +0000148 WriteStringFully(fd, success ? "remount succeeded\n" : "remount failed\n");
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700149
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150 adb_close(fd);
151}