2009-03-04 11:32:55 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-03-20 06:21:08 +08:00
|
|
|
#define TRACE_TAG TRACE_ADB
|
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
|
2014-05-01 00:10:31 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2015-01-03 06:02:14 +08:00
|
|
|
#include <mntent.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <stdio.h>
|
2014-05-01 00:10:31 +08:00
|
|
|
#include <stdlib.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mount.h>
|
2014-05-01 00:10:31 +08:00
|
|
|
#include <unistd.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-03-17 05:58:32 +08:00
|
|
|
#include <string>
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
#include "adb.h"
|
2015-02-25 13:26:58 +08:00
|
|
|
#include "adb_io.h"
|
2015-02-25 07:51:19 +08:00
|
|
|
#include "cutils/properties.h"
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
static int system_ro = 1;
|
2014-07-01 11:29:40 +08:00
|
|
|
static int vendor_ro = 1;
|
2015-03-17 05:58:32 +08:00
|
|
|
static int oem_ro = 1;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2010-05-07 08:06:18 +08:00
|
|
|
/* Returns the device used to mount a directory in /proc/mounts */
|
2015-03-17 05:58:32 +08:00
|
|
|
static std::string find_mount(const char *dir) {
|
2015-01-03 06:02:14 +08:00
|
|
|
FILE* fp;
|
|
|
|
struct mntent* mentry;
|
|
|
|
char* device = NULL;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-01-03 06:02:14 +08:00
|
|
|
if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
|
2010-05-07 08:06:18 +08:00
|
|
|
return NULL;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2015-01-03 06:02:14 +08:00
|
|
|
while ((mentry = getmntent(fp)) != NULL) {
|
|
|
|
if (strcmp(dir, mentry->mnt_dir) == 0) {
|
2015-03-17 05:58:32 +08:00
|
|
|
device = mentry->mnt_fsname;
|
2015-01-03 06:02:14 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endmntent(fp);
|
|
|
|
return device;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-03-17 05:58:32 +08:00
|
|
|
static bool has_partition(const char* path) {
|
|
|
|
struct stat sb;
|
|
|
|
return (lstat(path, &sb) == 0 && S_ISDIR(sb.st_mode));
|
2014-07-01 11:29:40 +08:00
|
|
|
}
|
|
|
|
|
2015-03-17 05:58:32 +08:00
|
|
|
int make_block_device_writable(const std::string& dev) {
|
|
|
|
int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC);
|
|
|
|
if (fd == -1) {
|
|
|
|
return -1;
|
2014-12-04 07:31:57 +08:00
|
|
|
}
|
|
|
|
|
2015-03-17 05:58:32 +08:00
|
|
|
int result = -1;
|
|
|
|
int OFF = 0;
|
|
|
|
if (!ioctl(fd, BLKROSET, &OFF)) {
|
|
|
|
result = 0;
|
2015-03-17 05:35:53 +08:00
|
|
|
}
|
2015-03-17 05:58:32 +08:00
|
|
|
adb_close(fd);
|
2015-03-17 05:35:53 +08:00
|
|
|
|
2015-03-17 05:58:32 +08:00
|
|
|
return result;
|
|
|
|
}
|
2015-03-17 05:35:53 +08:00
|
|
|
|
2015-03-17 05:58:32 +08:00
|
|
|
// Init mounts /system as read only, remount to enable writes.
|
|
|
|
static int remount(const char* dir, int* dir_ro) {
|
|
|
|
std::string dev(find_mount(dir));
|
|
|
|
if (dev.empty() || make_block_device_writable(dev)) {
|
|
|
|
return -1;
|
2015-01-02 21:30:50 +08:00
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-03-17 05:58:32 +08:00
|
|
|
int rc = mount(dev.c_str(), dir, "none", MS_REMOUNT, NULL);
|
2015-01-02 21:30:50 +08:00
|
|
|
*dir_ro = rc;
|
2015-03-17 05:35:53 +08:00
|
|
|
return rc;
|
2014-11-03 20:52:57 +08:00
|
|
|
}
|
|
|
|
|
2015-03-17 05:58:32 +08:00
|
|
|
static bool remount_partition(int fd, const char* partition, int* ro) {
|
|
|
|
if (!has_partition(partition)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (remount(partition, ro)) {
|
|
|
|
char buf[200];
|
|
|
|
snprintf(buf, sizeof(buf), "remount of %s failed: %s\n", partition, strerror(errno));
|
|
|
|
WriteStringFully(fd, buf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void remount_service(int fd, void* cookie) {
|
2014-10-28 01:37:59 +08:00
|
|
|
char prop_buf[PROPERTY_VALUE_MAX];
|
|
|
|
|
2015-02-26 07:48:06 +08:00
|
|
|
if (getuid() != 0) {
|
|
|
|
WriteStringFully(fd, "Not running as root. Try \"adb root\" first.\n");
|
|
|
|
adb_close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-28 01:37:59 +08:00
|
|
|
bool system_verified = false, vendor_verified = false;
|
2015-03-30 18:38:38 +08:00
|
|
|
property_get("partition.system.verified", prop_buf, "");
|
|
|
|
if (strlen(prop_buf) > 0) {
|
2014-10-28 01:37:59 +08:00
|
|
|
system_verified = true;
|
|
|
|
}
|
|
|
|
|
2015-03-30 18:38:38 +08:00
|
|
|
property_get("partition.vendor.verified", prop_buf, "");
|
|
|
|
if (strlen(prop_buf) > 0) {
|
2014-10-28 01:37:59 +08:00
|
|
|
vendor_verified = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (system_verified || vendor_verified) {
|
|
|
|
// Allow remount but warn of likely bad effects
|
|
|
|
bool both = system_verified && vendor_verified;
|
2015-03-17 05:58:32 +08:00
|
|
|
char buffer[200];
|
2014-10-28 01:37:59 +08:00
|
|
|
snprintf(buffer, sizeof(buffer),
|
|
|
|
"dm_verity is enabled on the %s%s%s partition%s.\n",
|
|
|
|
system_verified ? "system" : "",
|
|
|
|
both ? " and " : "",
|
|
|
|
vendor_verified ? "vendor" : "",
|
|
|
|
both ? "s" : "");
|
2015-02-25 13:26:58 +08:00
|
|
|
WriteStringFully(fd, buffer);
|
2014-10-28 01:37:59 +08:00
|
|
|
snprintf(buffer, sizeof(buffer),
|
|
|
|
"Use \"adb disable-verity\" to disable verity.\n"
|
|
|
|
"If you do not, remount may succeed, however, you will still "
|
|
|
|
"not be able to write to these volumes.\n");
|
2015-02-25 13:26:58 +08:00
|
|
|
WriteStringFully(fd, buffer);
|
2014-10-28 01:37:59 +08:00
|
|
|
}
|
|
|
|
|
2015-03-17 05:58:32 +08:00
|
|
|
bool success = true;
|
|
|
|
success &= remount_partition(fd, "/system", &system_ro);
|
|
|
|
success &= remount_partition(fd, "/vendor", &vendor_ro);
|
|
|
|
success &= remount_partition(fd, "/oem", &oem_ro);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-03-17 05:58:32 +08:00
|
|
|
WriteStringFully(fd, success ? "remount succeeded\n" : "remount failed\n");
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
adb_close(fd);
|
|
|
|
}
|