blob: 3fb2fcb6508145aab089aed406009b03777f14d5 [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"
Elliott Hughes58305772015-04-17 13:57:15 -070034#include "adb_utils.h"
Dan Albert76649012015-02-24 15:51:19 -080035#include "cutils/properties.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036
37static int system_ro = 1;
Daniel Rosenberg686bce62014-06-30 20:29:40 -070038static int vendor_ro = 1;
Elliott Hughesec7a6672015-03-16 21:58:32 +000039static int oem_ro = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
Colin Crossc880ee02010-05-06 17:06:18 -070041/* Returns the device used to mount a directory in /proc/mounts */
Elliott Hughesec7a6672015-03-16 21:58:32 +000042static std::string find_mount(const char *dir) {
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080043 FILE* fp;
44 struct mntent* mentry;
45 char* device = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080047 if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
Colin Crossc880ee02010-05-06 17:06:18 -070048 return NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049 }
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080050 while ((mentry = getmntent(fp)) != NULL) {
51 if (strcmp(dir, mentry->mnt_dir) == 0) {
Elliott Hughesec7a6672015-03-16 21:58:32 +000052 device = mentry->mnt_fsname;
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080053 break;
54 }
55 }
56 endmntent(fp);
57 return device;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058}
59
Elliott Hughesec7a6672015-03-16 21:58:32 +000060int make_block_device_writable(const std::string& dev) {
61 int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC);
62 if (fd == -1) {
63 return -1;
64 }
65
66 int result = -1;
Paul Lawrence982089d2014-12-03 15:31:57 -080067 int OFF = 0;
Elliott Hughesec7a6672015-03-16 21:58:32 +000068 if (!ioctl(fd, BLKROSET, &OFF)) {
69 result = 0;
Sami Tolvanen13449cd2015-01-02 13:30:50 +000070 }
Elliott Hughesec7a6672015-03-16 21:58:32 +000071 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072
Elliott Hughesec7a6672015-03-16 21:58:32 +000073 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074}
75
Elliott Hughesec7a6672015-03-16 21:58:32 +000076// Init mounts /system as read only, remount to enable writes.
77static int remount(const char* dir, int* dir_ro) {
78 std::string dev(find_mount(dir));
79 if (dev.empty() || make_block_device_writable(dev)) {
80 return -1;
Dan Albert6084a012015-03-16 21:35:53 +000081 }
82
Elliott Hughesec7a6672015-03-16 21:58:32 +000083 int rc = mount(dev.c_str(), dir, "none", MS_REMOUNT, NULL);
Dan Albert6084a012015-03-16 21:35:53 +000084 *dir_ro = rc;
Dan Albert6084a012015-03-16 21:35:53 +000085 return rc;
MÃ¥rten Kongstad81416fd2014-11-03 13:52:57 +010086}
87
Elliott Hughesec7a6672015-03-16 21:58:32 +000088static bool remount_partition(int fd, const char* partition, int* ro) {
Elliott Hughes58305772015-04-17 13:57:15 -070089 if (!directory_exists(partition)) {
Elliott Hughesec7a6672015-03-16 21:58:32 +000090 return true;
91 }
92 if (remount(partition, ro)) {
93 char buf[200];
94 snprintf(buf, sizeof(buf), "remount of %s failed: %s\n", partition, strerror(errno));
Elliott Hughese67f1f82015-04-30 17:32:03 -070095 WriteFdExactly(fd, buf);
Elliott Hughesec7a6672015-03-16 21:58:32 +000096 return false;
97 }
98 return true;
99}
100
101void remount_service(int fd, void* cookie) {
Paul Lawrence34637552014-10-27 10:37:59 -0700102 char prop_buf[PROPERTY_VALUE_MAX];
103
Nick Kralevich268eb4f2015-02-25 15:48:06 -0800104 if (getuid() != 0) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700105 WriteFdExactly(fd, "Not running as root. Try \"adb root\" first.\n");
Nick Kralevich268eb4f2015-02-25 15:48:06 -0800106 adb_close(fd);
107 return;
108 }
109
Paul Lawrence34637552014-10-27 10:37:59 -0700110 bool system_verified = false, vendor_verified = false;
Sami Tolvanen45474232015-03-30 11:38:38 +0100111 property_get("partition.system.verified", prop_buf, "");
112 if (strlen(prop_buf) > 0) {
Paul Lawrence34637552014-10-27 10:37:59 -0700113 system_verified = true;
114 }
115
Sami Tolvanen45474232015-03-30 11:38:38 +0100116 property_get("partition.vendor.verified", prop_buf, "");
117 if (strlen(prop_buf) > 0) {
Paul Lawrence34637552014-10-27 10:37:59 -0700118 vendor_verified = true;
119 }
120
121 if (system_verified || vendor_verified) {
122 // Allow remount but warn of likely bad effects
123 bool both = system_verified && vendor_verified;
Elliott Hughesec7a6672015-03-16 21:58:32 +0000124 char buffer[200];
Paul Lawrence34637552014-10-27 10:37:59 -0700125 snprintf(buffer, sizeof(buffer),
126 "dm_verity is enabled on the %s%s%s partition%s.\n",
127 system_verified ? "system" : "",
128 both ? " and " : "",
129 vendor_verified ? "vendor" : "",
130 both ? "s" : "");
Elliott Hughese67f1f82015-04-30 17:32:03 -0700131 WriteFdExactly(fd, buffer);
132 WriteFdExactly(fd,
133 "Use \"adb disable-verity\" to disable verity.\n"
134 "If you do not, remount may succeed, however, you will still "
135 "not be able to write to these volumes.\n");
Paul Lawrence34637552014-10-27 10:37:59 -0700136 }
137
Elliott Hughesec7a6672015-03-16 21:58:32 +0000138 bool success = true;
139 success &= remount_partition(fd, "/system", &system_ro);
140 success &= remount_partition(fd, "/vendor", &vendor_ro);
141 success &= remount_partition(fd, "/oem", &oem_ro);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142
Elliott Hughese67f1f82015-04-30 17:32:03 -0700143 WriteFdExactly(fd, success ? "remount succeeded\n" : "remount failed\n");
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700144
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145 adb_close(fd);
146}