2009-03-04 04:32:55 +01: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.
|
|
|
|
*/
|
|
|
|
|
2014-04-30 18:10:31 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2015-01-02 23:02:14 +01:00
|
|
|
#include <mntent.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
2014-04-30 18:10:31 +02:00
|
|
|
#include <stdlib.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mount.h>
|
2014-04-30 18:10:31 +02:00
|
|
|
#include <unistd.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-02-25 00:51:19 +01:00
|
|
|
#include "sysdeps.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
#define TRACE_TAG TRACE_ADB
|
|
|
|
#include "adb.h"
|
2015-02-25 06:26:58 +01:00
|
|
|
#include "adb_io.h"
|
2015-02-25 00:51:19 +01:00
|
|
|
#include "cutils/properties.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
static int system_ro = 1;
|
2014-07-01 05:29:40 +02:00
|
|
|
static int vendor_ro = 1;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2010-05-07 02:06:18 +02:00
|
|
|
/* Returns the device used to mount a directory in /proc/mounts */
|
|
|
|
static char *find_mount(const char *dir)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2015-01-02 23:02:14 +01:00
|
|
|
FILE* fp;
|
|
|
|
struct mntent* mentry;
|
|
|
|
char* device = NULL;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-01-02 23:02:14 +01:00
|
|
|
if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
|
2010-05-07 02:06:18 +02:00
|
|
|
return NULL;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-01-02 23:02:14 +01:00
|
|
|
while ((mentry = getmntent(fp)) != NULL) {
|
|
|
|
if (strcmp(dir, mentry->mnt_dir) == 0) {
|
|
|
|
device = strdup(mentry->mnt_fsname);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endmntent(fp);
|
|
|
|
return device;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2014-07-01 05:29:40 +02:00
|
|
|
static int hasVendorPartition()
|
|
|
|
{
|
|
|
|
struct stat info;
|
|
|
|
if (!lstat("/vendor", &info))
|
|
|
|
if ((info.st_mode & S_IFMT) == S_IFDIR)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-02 14:30:50 +01:00
|
|
|
int make_block_device_writable(const char* dev)
|
2014-12-04 00:31:57 +01:00
|
|
|
{
|
|
|
|
int fd = -1;
|
|
|
|
int OFF = 0;
|
|
|
|
int rc = -1;
|
|
|
|
|
|
|
|
if (!dev)
|
|
|
|
goto errout;
|
|
|
|
|
|
|
|
fd = unix_open(dev, O_RDONLY | O_CLOEXEC);
|
|
|
|
if (fd < 0)
|
|
|
|
goto errout;
|
|
|
|
|
|
|
|
if (ioctl(fd, BLKROSET, &OFF)) {
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
if (fd >= 0) {
|
|
|
|
adb_close(fd);
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
/* Init mounts /system as read only, remount to enable writes. */
|
2014-07-01 05:29:40 +02:00
|
|
|
static int remount(const char* dir, int* dir_ro)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2015-01-02 14:30:50 +01:00
|
|
|
char *dev = 0;
|
|
|
|
int rc = -1;
|
2014-12-04 00:31:57 +01:00
|
|
|
|
2014-07-01 05:29:40 +02:00
|
|
|
dev = find_mount(dir);
|
2010-05-07 02:06:18 +02:00
|
|
|
|
2015-01-02 14:30:50 +01:00
|
|
|
if (!dev || make_block_device_writable(dev)) {
|
|
|
|
goto errout;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-01-02 14:30:50 +01:00
|
|
|
rc = mount(dev, dir, "none", MS_REMOUNT, NULL);
|
|
|
|
*dir_ro = rc;
|
2010-05-07 02:06:18 +02:00
|
|
|
|
2015-01-02 14:30:50 +01:00
|
|
|
errout:
|
2010-05-07 02:06:18 +02:00
|
|
|
free(dev);
|
2015-01-02 14:30:50 +01:00
|
|
|
return rc;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void remount_service(int fd, void *cookie)
|
|
|
|
{
|
2014-07-01 05:29:40 +02:00
|
|
|
char buffer[200];
|
2014-10-27 18:37:59 +01:00
|
|
|
char prop_buf[PROPERTY_VALUE_MAX];
|
|
|
|
|
2015-02-26 00:48:06 +01:00
|
|
|
if (getuid() != 0) {
|
|
|
|
WriteStringFully(fd, "Not running as root. Try \"adb root\" first.\n");
|
|
|
|
adb_close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-27 18:37:59 +01:00
|
|
|
bool system_verified = false, vendor_verified = false;
|
|
|
|
property_get("partition.system.verified", prop_buf, "0");
|
|
|
|
if (!strcmp(prop_buf, "1")) {
|
|
|
|
system_verified = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
property_get("partition.vendor.verified", prop_buf, "0");
|
|
|
|
if (!strcmp(prop_buf, "1")) {
|
|
|
|
vendor_verified = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (system_verified || vendor_verified) {
|
|
|
|
// Allow remount but warn of likely bad effects
|
|
|
|
bool both = system_verified && vendor_verified;
|
|
|
|
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 06:26:58 +01:00
|
|
|
WriteStringFully(fd, buffer);
|
2014-10-27 18:37:59 +01: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 06:26:58 +01:00
|
|
|
WriteStringFully(fd, buffer);
|
2014-10-27 18:37:59 +01:00
|
|
|
}
|
|
|
|
|
2014-07-01 05:29:40 +02:00
|
|
|
if (remount("/system", &system_ro)) {
|
|
|
|
snprintf(buffer, sizeof(buffer), "remount of system failed: %s\n",strerror(errno));
|
2015-02-25 06:26:58 +01:00
|
|
|
WriteStringFully(fd, buffer);
|
2014-07-01 05:29:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hasVendorPartition()) {
|
|
|
|
if (remount("/vendor", &vendor_ro)) {
|
|
|
|
snprintf(buffer, sizeof(buffer), "remount of vendor failed: %s\n",strerror(errno));
|
2015-02-25 06:26:58 +01:00
|
|
|
WriteStringFully(fd, buffer);
|
2014-07-01 05:29:40 +02:00
|
|
|
}
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-07-01 05:29:40 +02:00
|
|
|
if (!system_ro && (!vendor_ro || !hasVendorPartition()))
|
2015-02-25 06:26:58 +01:00
|
|
|
WriteStringFully(fd, "remount succeeded\n");
|
2009-03-04 04:32:55 +01:00
|
|
|
else {
|
2015-02-25 06:26:58 +01:00
|
|
|
WriteStringFully(fd, "remount failed\n");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
adb_close(fd);
|
|
|
|
}
|