blob: 28932636da09cfe6036f0728d1f674f7001ecfd9 [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"
Daniel Rosenbergd6eba892015-06-29 17:30:28 -070036#include "fs_mgr.h"
37
38const std::string kFstab_Prefix = "/fstab.";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
Elliott Hughes5677c232015-05-07 23:37:40 -070040// Returns the device used to mount a directory in /proc/mounts.
Daniel Rosenbergd6eba892015-06-29 17:30:28 -070041static std::string find_proc_mount(const char* dir) {
Elliott Hughes5677c232015-05-07 23:37:40 -070042 std::unique_ptr<FILE, int(*)(FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent);
43 if (!fp) {
44 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045 }
Elliott Hughes5677c232015-05-07 23:37:40 -070046
47 mntent* e;
48 while ((e = getmntent(fp.get())) != nullptr) {
49 if (strcmp(dir, e->mnt_dir) == 0) {
50 return e->mnt_fsname;
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080051 }
52 }
Elliott Hughes5677c232015-05-07 23:37:40 -070053 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054}
55
Daniel Rosenbergd6eba892015-06-29 17:30:28 -070056// Returns the device used to mount a directory in the fstab.
57static std::string find_fstab_mount(const char* dir) {
58 char propbuf[PROPERTY_VALUE_MAX];
59
60 property_get("ro.hardware", propbuf, "");
61 std::string fstab_filename = kFstab_Prefix + propbuf;
62 struct fstab* fstab = fs_mgr_read_fstab(fstab_filename.c_str());
63 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, dir);
64 std::string dev = rec ? std::string(rec->blk_device) : "";
65 fs_mgr_free_fstab(fstab);
66 return dev;
67}
68
69// The proc entry for / is full of lies, so check fstab instead.
70// /proc/mounts lists rootfs and /dev/root, neither of which is what we want.
71static std::string find_mount(const char* dir) {
72 if (strcmp(dir, "/") == 0) {
73 return find_fstab_mount(dir);
74 } else {
75 return find_proc_mount(dir);
76 }
77}
78
Elliott Hughes9aa4fda2015-05-11 11:55:25 -070079bool make_block_device_writable(const std::string& dev) {
Elliott Hughesec7a6672015-03-16 21:58:32 +000080 int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC);
81 if (fd == -1) {
Elliott Hughes9aa4fda2015-05-11 11:55:25 -070082 return false;
Elliott Hughesec7a6672015-03-16 21:58:32 +000083 }
84
Paul Lawrence982089d2014-12-03 15:31:57 -080085 int OFF = 0;
Elliott Hughes9aa4fda2015-05-11 11:55:25 -070086 bool result = (ioctl(fd, BLKROSET, &OFF) != -1);
Spencer Low6ac5d7d2015-05-22 20:09:06 -070087 unix_close(fd);
Elliott Hughesec7a6672015-03-16 21:58:32 +000088 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089}
90
Elliott Hughes9aa4fda2015-05-11 11:55:25 -070091static bool remount_partition(int fd, const char* dir) {
92 if (!directory_exists(dir)) {
Elliott Hughes5677c232015-05-07 23:37:40 -070093 return true;
94 }
Elliott Hughes9aa4fda2015-05-11 11:55:25 -070095 std::string dev = find_mount(dir);
96 if (dev.empty()) {
97 return true;
98 }
99 if (!make_block_device_writable(dev)) {
100 WriteFdFmt(fd, "remount of %s failed; couldn't make block device %s writable: %s\n",
101 dir, dev.c_str(), strerror(errno));
102 return false;
103 }
104 if (mount(dev.c_str(), dir, "none", MS_REMOUNT, nullptr) == -1) {
105 WriteFdFmt(fd, "remount of %s failed: %s\n", dir, strerror(errno));
Elliott Hughes5677c232015-05-07 23:37:40 -0700106 return false;
107 }
Elliott Hughesec7a6672015-03-16 21:58:32 +0000108 return true;
Elliott Hughesec7a6672015-03-16 21:58:32 +0000109}
110
111void remount_service(int fd, void* cookie) {
Nick Kralevich268eb4f2015-02-25 15:48:06 -0800112 if (getuid() != 0) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700113 WriteFdExactly(fd, "Not running as root. Try \"adb root\" first.\n");
Nick Kralevich268eb4f2015-02-25 15:48:06 -0800114 adb_close(fd);
115 return;
116 }
117
Elliott Hughes5677c232015-05-07 23:37:40 -0700118 char prop_buf[PROPERTY_VALUE_MAX];
Sami Tolvanen45474232015-03-30 11:38:38 +0100119 property_get("partition.system.verified", prop_buf, "");
Elliott Hughes5677c232015-05-07 23:37:40 -0700120 bool system_verified = (strlen(prop_buf) > 0);
Paul Lawrence34637552014-10-27 10:37:59 -0700121
Sami Tolvanen45474232015-03-30 11:38:38 +0100122 property_get("partition.vendor.verified", prop_buf, "");
Elliott Hughes5677c232015-05-07 23:37:40 -0700123 bool vendor_verified = (strlen(prop_buf) > 0);
Paul Lawrence34637552014-10-27 10:37:59 -0700124
125 if (system_verified || vendor_verified) {
126 // Allow remount but warn of likely bad effects
127 bool both = system_verified && vendor_verified;
Elliott Hughesab52c182015-05-01 17:04:38 -0700128 WriteFdFmt(fd,
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" : "");
Elliott Hughese67f1f82015-04-30 17:32:03 -0700134 WriteFdExactly(fd,
135 "Use \"adb disable-verity\" to disable verity.\n"
136 "If you do not, remount may succeed, however, you will still "
137 "not be able to write to these volumes.\n");
Paul Lawrence34637552014-10-27 10:37:59 -0700138 }
139
Elliott Hughesec7a6672015-03-16 21:58:32 +0000140 bool success = true;
Daniel Rosenbergd6eba892015-06-29 17:30:28 -0700141 property_get("ro.build.system_root_image", prop_buf, "");
142 bool system_root = !strcmp(prop_buf, "true");
143 if (system_root) {
144 success &= remount_partition(fd, "/");
145 } else {
146 success &= remount_partition(fd, "/system");
147 }
Elliott Hughes9aa4fda2015-05-11 11:55:25 -0700148 success &= remount_partition(fd, "/vendor");
149 success &= remount_partition(fd, "/oem");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150
Elliott Hughese67f1f82015-04-30 17:32:03 -0700151 WriteFdExactly(fd, success ? "remount succeeded\n" : "remount failed\n");
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700152
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153 adb_close(fd);
154}