blob: a83d5b1343ac9f99816fd45ac4a35f4f3a1707ad [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
Elliott Hughesec7a6672015-03-16 21:58:32 +000026#include <string>
27
Dan Albert76649012015-02-24 15:51:19 -080028#include "sysdeps.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
30#define TRACE_TAG TRACE_ADB
31#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080032#include "adb_io.h"
Dan Albert76649012015-02-24 15:51:19 -080033#include "cutils/properties.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
35static int system_ro = 1;
Daniel Rosenberg686bce62014-06-30 20:29:40 -070036static int vendor_ro = 1;
Elliott Hughesec7a6672015-03-16 21:58:32 +000037static int oem_ro = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038
Colin Crossc880ee02010-05-06 17:06:18 -070039/* Returns the device used to mount a directory in /proc/mounts */
Elliott Hughesec7a6672015-03-16 21:58:32 +000040static std::string find_mount(const char *dir) {
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080041 FILE* fp;
42 struct mntent* mentry;
43 char* device = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080045 if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
Colin Crossc880ee02010-05-06 17:06:18 -070046 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047 }
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080048 while ((mentry = getmntent(fp)) != NULL) {
49 if (strcmp(dir, mentry->mnt_dir) == 0) {
Elliott Hughesec7a6672015-03-16 21:58:32 +000050 device = mentry->mnt_fsname;
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080051 break;
52 }
53 }
54 endmntent(fp);
55 return device;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056}
57
Elliott Hughesec7a6672015-03-16 21:58:32 +000058static bool has_partition(const char* path) {
59 struct stat sb;
60 return (lstat(path, &sb) == 0 && S_ISDIR(sb.st_mode));
Daniel Rosenberg686bce62014-06-30 20:29:40 -070061}
62
Elliott Hughesec7a6672015-03-16 21:58:32 +000063int make_block_device_writable(const std::string& dev) {
64 int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC);
65 if (fd == -1) {
66 return -1;
67 }
68
69 int result = -1;
Paul Lawrence982089d2014-12-03 15:31:57 -080070 int OFF = 0;
Elliott Hughesec7a6672015-03-16 21:58:32 +000071 if (!ioctl(fd, BLKROSET, &OFF)) {
72 result = 0;
Sami Tolvanen13449cd2015-01-02 13:30:50 +000073 }
Elliott Hughesec7a6672015-03-16 21:58:32 +000074 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075
Elliott Hughesec7a6672015-03-16 21:58:32 +000076 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077}
78
Elliott Hughesec7a6672015-03-16 21:58:32 +000079// Init mounts /system as read only, remount to enable writes.
80static int remount(const char* dir, int* dir_ro) {
81 std::string dev(find_mount(dir));
82 if (dev.empty() || make_block_device_writable(dev)) {
83 return -1;
Dan Albert6084a012015-03-16 21:35:53 +000084 }
85
Elliott Hughesec7a6672015-03-16 21:58:32 +000086 int rc = mount(dev.c_str(), dir, "none", MS_REMOUNT, NULL);
Dan Albert6084a012015-03-16 21:35:53 +000087 *dir_ro = rc;
Dan Albert6084a012015-03-16 21:35:53 +000088 return rc;
MÃ¥rten Kongstad81416fd2014-11-03 13:52:57 +010089}
90
Elliott Hughesec7a6672015-03-16 21:58:32 +000091static bool remount_partition(int fd, const char* partition, int* ro) {
92 if (!has_partition(partition)) {
93 return true;
94 }
95 if (remount(partition, ro)) {
96 char buf[200];
97 snprintf(buf, sizeof(buf), "remount of %s failed: %s\n", partition, strerror(errno));
98 WriteStringFully(fd, buf);
99 return false;
100 }
101 return true;
102}
103
104void remount_service(int fd, void* cookie) {
Paul Lawrence34637552014-10-27 10:37:59 -0700105 char prop_buf[PROPERTY_VALUE_MAX];
106
Nick Kralevich268eb4f2015-02-25 15:48:06 -0800107 if (getuid() != 0) {
108 WriteStringFully(fd, "Not running as root. Try \"adb root\" first.\n");
109 adb_close(fd);
110 return;
111 }
112
Paul Lawrence34637552014-10-27 10:37:59 -0700113 bool system_verified = false, vendor_verified = false;
114 property_get("partition.system.verified", prop_buf, "0");
115 if (!strcmp(prop_buf, "1")) {
116 system_verified = true;
117 }
118
119 property_get("partition.vendor.verified", prop_buf, "0");
120 if (!strcmp(prop_buf, "1")) {
121 vendor_verified = true;
122 }
123
124 if (system_verified || vendor_verified) {
125 // Allow remount but warn of likely bad effects
126 bool both = system_verified && vendor_verified;
Elliott Hughesec7a6672015-03-16 21:58:32 +0000127 char buffer[200];
Paul Lawrence34637552014-10-27 10:37:59 -0700128 snprintf(buffer, sizeof(buffer),
129 "dm_verity is enabled on the %s%s%s partition%s.\n",
130 system_verified ? "system" : "",
131 both ? " and " : "",
132 vendor_verified ? "vendor" : "",
133 both ? "s" : "");
Dan Albertcc731cc2015-02-24 21:26:58 -0800134 WriteStringFully(fd, buffer);
Paul Lawrence34637552014-10-27 10:37:59 -0700135 snprintf(buffer, sizeof(buffer),
136 "Use \"adb disable-verity\" to disable verity.\n"
137 "If you do not, remount may succeed, however, you will still "
138 "not be able to write to these volumes.\n");
Dan Albertcc731cc2015-02-24 21:26:58 -0800139 WriteStringFully(fd, buffer);
Paul Lawrence34637552014-10-27 10:37:59 -0700140 }
141
Elliott Hughesec7a6672015-03-16 21:58:32 +0000142 bool success = true;
143 success &= remount_partition(fd, "/system", &system_ro);
144 success &= remount_partition(fd, "/vendor", &vendor_ro);
145 success &= remount_partition(fd, "/oem", &oem_ro);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146
Elliott Hughesec7a6672015-03-16 21:58:32 +0000147 WriteStringFully(fd, success ? "remount succeeded\n" : "remount failed\n");
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700148
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 adb_close(fd);
150}