2009-03-04 04:28:42 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2006 The Android Open Source Project
|
|
|
|
*
|
2016-11-04 18:52:13 +01:00
|
|
|
* 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.
|
2009-03-04 04:28:42 +01:00
|
|
|
*/
|
2016-11-04 18:52:13 +01:00
|
|
|
|
2018-05-04 07:41:23 +02:00
|
|
|
#include "otautil/sysutil.h"
|
2016-11-04 18:52:13 +01:00
|
|
|
|
2017-10-11 21:14:38 +02:00
|
|
|
#include <errno.h> // TEMP_FAILURE_RETRY
|
2015-06-23 21:31:02 +02:00
|
|
|
#include <fcntl.h>
|
2019-03-22 17:33:49 +01:00
|
|
|
#include <inttypes.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
#include <sys/mman.h>
|
2014-01-13 23:16:58 +01:00
|
|
|
#include <sys/stat.h>
|
2015-06-23 21:31:02 +02:00
|
|
|
#include <sys/types.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2018-07-17 07:09:59 +02:00
|
|
|
#include <algorithm>
|
2019-03-22 17:33:49 +01:00
|
|
|
#include <limits>
|
2016-11-04 18:52:13 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2016-08-06 03:00:04 +02:00
|
|
|
|
2016-11-04 18:52:13 +01:00
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/logging.h>
|
2018-05-04 08:01:13 +02:00
|
|
|
#include <android-base/properties.h>
|
2016-11-04 18:52:13 +01:00
|
|
|
#include <android-base/strings.h>
|
|
|
|
#include <android-base/unique_fd.h>
|
2018-05-04 08:01:13 +02:00
|
|
|
#include <cutils/android_reboot.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2019-03-22 17:33:49 +01:00
|
|
|
BlockMapData BlockMapData::ParseBlockMapFile(const std::string& block_map_path) {
|
|
|
|
std::string content;
|
|
|
|
if (!android::base::ReadFileToString(block_map_path, &content)) {
|
2019-06-25 22:59:39 +02:00
|
|
|
PLOG(ERROR) << "Failed to read " << block_map_path;
|
2019-03-22 17:33:49 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> lines = android::base::Split(android::base::Trim(content), "\n");
|
|
|
|
if (lines.size() < 4) {
|
|
|
|
LOG(ERROR) << "Block map file is too short: " << lines.size();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& block_dev = lines[0];
|
|
|
|
|
|
|
|
uint64_t file_size;
|
|
|
|
uint32_t blksize;
|
|
|
|
if (sscanf(lines[1].c_str(), "%" SCNu64 "%" SCNu32, &file_size, &blksize) != 2) {
|
|
|
|
LOG(ERROR) << "Failed to parse file size and block size: " << lines[1];
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_size == 0 || blksize == 0) {
|
|
|
|
LOG(ERROR) << "Invalid size in block map file: size " << file_size << ", blksize " << blksize;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t range_count;
|
|
|
|
if (sscanf(lines[2].c_str(), "%zu", &range_count) != 1) {
|
|
|
|
LOG(ERROR) << "Failed to parse block map header: " << lines[2];
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t blocks = ((file_size - 1) / blksize) + 1;
|
|
|
|
if (blocks > std::numeric_limits<uint32_t>::max() || range_count == 0 ||
|
|
|
|
lines.size() != 3 + range_count) {
|
|
|
|
LOG(ERROR) << "Invalid data in block map file: size " << file_size << ", blksize " << blksize
|
|
|
|
<< ", range_count " << range_count << ", lines " << lines.size();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
RangeSet ranges;
|
|
|
|
uint64_t remaining_blocks = blocks;
|
|
|
|
for (size_t i = 0; i < range_count; ++i) {
|
|
|
|
const std::string& line = lines[i + 3];
|
|
|
|
uint64_t start, end;
|
|
|
|
if (sscanf(line.c_str(), "%" SCNu64 "%" SCNu64, &start, &end) != 2) {
|
|
|
|
LOG(ERROR) << "failed to parse range " << i << ": " << line;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
uint64_t range_blocks = end - start;
|
|
|
|
if (end <= start || range_blocks > remaining_blocks) {
|
|
|
|
LOG(ERROR) << "Invalid range: " << start << " " << end;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
ranges.PushBack({ start, end });
|
|
|
|
remaining_blocks -= range_blocks;
|
|
|
|
}
|
|
|
|
|
2019-03-22 16:54:35 +01:00
|
|
|
if (remaining_blocks != 0) {
|
|
|
|
LOG(ERROR) << "Invalid ranges: remaining blocks " << remaining_blocks;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-03-22 17:33:49 +01:00
|
|
|
return BlockMapData(block_dev, file_size, blksize, std::move(ranges));
|
|
|
|
}
|
|
|
|
|
2017-04-19 08:54:29 +02:00
|
|
|
bool MemMapping::MapFD(int fd) {
|
2016-11-04 18:52:13 +01:00
|
|
|
struct stat sb;
|
|
|
|
if (fstat(fd, &sb) == -1) {
|
|
|
|
PLOG(ERROR) << "fstat(" << fd << ") failed";
|
|
|
|
return false;
|
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2016-11-04 18:52:13 +01:00
|
|
|
void* memPtr = mmap(nullptr, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
if (memPtr == MAP_FAILED) {
|
|
|
|
PLOG(ERROR) << "mmap(" << sb.st_size << ", R, PRIVATE, " << fd << ", 0) failed";
|
|
|
|
return false;
|
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2017-04-19 08:54:29 +02:00
|
|
|
addr = static_cast<unsigned char*>(memPtr);
|
|
|
|
length = sb.st_size;
|
|
|
|
ranges_.clear();
|
|
|
|
ranges_.emplace_back(MappedRange{ memPtr, static_cast<size_t>(sb.st_size) });
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2016-11-04 18:52:13 +01:00
|
|
|
return true;
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
2017-04-19 08:54:29 +02:00
|
|
|
bool MemMapping::MapBlockFile(const std::string& filename) {
|
2019-03-22 17:33:49 +01:00
|
|
|
auto block_map_data = BlockMapData::ParseBlockMapFile(filename);
|
|
|
|
if (!block_map_data) {
|
2017-04-19 08:54:29 +02:00
|
|
|
return false;
|
2016-11-04 18:52:13 +01:00
|
|
|
}
|
|
|
|
|
2019-03-22 17:33:49 +01:00
|
|
|
if (block_map_data.file_size() > std::numeric_limits<size_t>::max()) {
|
|
|
|
LOG(ERROR) << "File size is too large for mmap " << block_map_data.file_size();
|
2017-04-19 08:54:29 +02:00
|
|
|
return false;
|
2016-11-04 18:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reserve enough contiguous address space for the whole file.
|
2019-03-22 17:33:49 +01:00
|
|
|
uint32_t blksize = block_map_data.block_size();
|
|
|
|
uint64_t blocks = ((block_map_data.file_size() - 1) / blksize) + 1;
|
2017-10-10 19:56:09 +02:00
|
|
|
void* reserve = mmap(nullptr, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
|
2016-11-04 18:52:13 +01:00
|
|
|
if (reserve == MAP_FAILED) {
|
|
|
|
PLOG(ERROR) << "failed to reserve address space";
|
2017-04-19 08:54:29 +02:00
|
|
|
return false;
|
2016-11-04 18:52:13 +01:00
|
|
|
}
|
|
|
|
|
2019-03-22 17:33:49 +01:00
|
|
|
android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(block_map_data.path().c_str(), O_RDONLY)));
|
2016-11-04 18:52:13 +01:00
|
|
|
if (fd == -1) {
|
2019-03-22 17:33:49 +01:00
|
|
|
PLOG(ERROR) << "failed to open block device " << block_map_data.path();
|
2016-11-04 18:52:13 +01:00
|
|
|
munmap(reserve, blocks * blksize);
|
2017-04-19 08:54:29 +02:00
|
|
|
return false;
|
2016-11-04 18:52:13 +01:00
|
|
|
}
|
|
|
|
|
2017-04-19 08:54:29 +02:00
|
|
|
ranges_.clear();
|
2016-11-04 18:52:13 +01:00
|
|
|
|
2019-03-22 17:33:49 +01:00
|
|
|
auto next = static_cast<unsigned char*>(reserve);
|
2016-11-04 18:52:13 +01:00
|
|
|
size_t remaining_size = blocks * blksize;
|
2019-03-22 17:33:49 +01:00
|
|
|
for (const auto& [start, end] : block_map_data.block_ranges()) {
|
2017-04-19 08:54:29 +02:00
|
|
|
size_t range_size = (end - start) * blksize;
|
2017-10-10 19:56:09 +02:00
|
|
|
void* range_start = mmap(next, range_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd,
|
|
|
|
static_cast<off_t>(start) * blksize);
|
2017-04-19 08:54:29 +02:00
|
|
|
if (range_start == MAP_FAILED) {
|
2019-03-22 17:33:49 +01:00
|
|
|
PLOG(ERROR) << "failed to map range " << start << ": " << end;
|
|
|
|
munmap(reserve, blocks * blksize);
|
|
|
|
return false;
|
2016-02-18 20:32:10 +01:00
|
|
|
}
|
2017-04-19 08:54:29 +02:00
|
|
|
ranges_.emplace_back(MappedRange{ range_start, range_size });
|
2014-01-13 23:16:58 +01:00
|
|
|
|
2017-04-19 08:54:29 +02:00
|
|
|
next += range_size;
|
|
|
|
remaining_size -= range_size;
|
2016-11-04 18:52:13 +01:00
|
|
|
}
|
2019-03-22 17:33:49 +01:00
|
|
|
if (remaining_size != 0) {
|
2017-04-19 08:54:29 +02:00
|
|
|
LOG(ERROR) << "Invalid ranges: remaining_size " << remaining_size;
|
2016-11-04 18:52:13 +01:00
|
|
|
munmap(reserve, blocks * blksize);
|
2017-04-19 08:54:29 +02:00
|
|
|
return false;
|
2016-11-04 18:52:13 +01:00
|
|
|
}
|
2014-01-13 23:16:58 +01:00
|
|
|
|
2017-04-19 08:54:29 +02:00
|
|
|
addr = static_cast<unsigned char*>(reserve);
|
2019-03-22 17:33:49 +01:00
|
|
|
length = block_map_data.file_size();
|
2014-01-13 23:16:58 +01:00
|
|
|
|
2019-03-22 17:33:49 +01:00
|
|
|
LOG(INFO) << "mmapped " << block_map_data.block_ranges().size() << " ranges";
|
2014-01-13 23:16:58 +01:00
|
|
|
|
2017-04-19 08:54:29 +02:00
|
|
|
return true;
|
2016-11-04 18:52:13 +01:00
|
|
|
}
|
2014-01-13 23:16:58 +01:00
|
|
|
|
2017-04-19 08:54:29 +02:00
|
|
|
bool MemMapping::MapFile(const std::string& fn) {
|
|
|
|
if (fn.empty()) {
|
|
|
|
LOG(ERROR) << "Empty filename";
|
|
|
|
return false;
|
2016-11-04 18:52:13 +01:00
|
|
|
}
|
2014-01-13 23:16:58 +01:00
|
|
|
|
2016-11-04 18:52:13 +01:00
|
|
|
if (fn[0] == '@') {
|
2017-04-19 08:54:29 +02:00
|
|
|
// Block map file "@/cache/recovery/block.map".
|
|
|
|
if (!MapBlockFile(fn.substr(1))) {
|
2016-11-04 18:52:13 +01:00
|
|
|
LOG(ERROR) << "Map of '" << fn << "' failed";
|
2017-04-19 08:54:29 +02:00
|
|
|
return false;
|
2016-11-04 18:52:13 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This is a regular file.
|
2017-04-19 08:54:29 +02:00
|
|
|
android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY)));
|
2016-11-04 18:52:13 +01:00
|
|
|
if (fd == -1) {
|
|
|
|
PLOG(ERROR) << "Unable to open '" << fn << "'";
|
2017-04-19 08:54:29 +02:00
|
|
|
return false;
|
2016-11-04 18:52:13 +01:00
|
|
|
}
|
2014-01-13 23:16:58 +01:00
|
|
|
|
2017-04-19 08:54:29 +02:00
|
|
|
if (!MapFD(fd)) {
|
2016-11-04 18:52:13 +01:00
|
|
|
LOG(ERROR) << "Map of '" << fn << "' failed";
|
2017-04-19 08:54:29 +02:00
|
|
|
return false;
|
2014-01-13 23:16:58 +01:00
|
|
|
}
|
2016-11-04 18:52:13 +01:00
|
|
|
}
|
2017-04-19 08:54:29 +02:00
|
|
|
return true;
|
2014-01-13 23:16:58 +01:00
|
|
|
}
|
|
|
|
|
2017-04-19 08:54:29 +02:00
|
|
|
MemMapping::~MemMapping() {
|
|
|
|
for (const auto& range : ranges_) {
|
2016-11-04 18:52:13 +01:00
|
|
|
if (munmap(range.addr, range.length) == -1) {
|
2017-04-19 08:54:29 +02:00
|
|
|
PLOG(ERROR) << "Failed to munmap(" << range.addr << ", " << range.length << ")";
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
2017-04-19 08:54:29 +02:00
|
|
|
};
|
|
|
|
ranges_.clear();
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
2018-05-04 08:01:13 +02:00
|
|
|
|
2020-03-13 22:25:02 +01:00
|
|
|
void Reboot(std::string_view target) {
|
2019-04-29 20:23:16 +02:00
|
|
|
std::string cmd = "reboot," + std::string(target);
|
|
|
|
// Honor the quiescent mode if applicable.
|
|
|
|
if (target != "bootloader" && target != "fastboot" &&
|
|
|
|
android::base::GetBoolProperty("ro.boot.quiescent", false)) {
|
2018-05-04 08:01:13 +02:00
|
|
|
cmd += ",quiescent";
|
|
|
|
}
|
2020-03-13 22:25:02 +01:00
|
|
|
if (!android::base::SetProperty(ANDROID_RB_PROPERTY, cmd)) {
|
|
|
|
LOG(FATAL) << "Reboot failed";
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) pause();
|
2018-05-04 08:01:13 +02:00
|
|
|
}
|
2018-07-17 07:09:59 +02:00
|
|
|
|
2024-04-07 09:10:45 +02:00
|
|
|
void Shutdown(std::string_view target) {
|
recovery: report compliant reboot reason (Part Deux)
shutdown and reboot should have a corresponding sub-reason.
Adding:
"reboot,userrequested,fastboot"
"reboot,userrequested,recovery"
"reboot,userrequested,recovery,ui"
"shutdown,userrequested,fastboot"
"shutdown,userrequested,recovery"
"reboot,unknown#" (Can't happen, debug)
Test: manual, multiple targets, enter recovery, be able to exit recovery
Bug: 133326470
Change-Id: Ibfcb2a23158e8e99922e8053edd815fb592150f2
2019-05-20 19:36:16 +02:00
|
|
|
std::string cmd = "shutdown," + std::string(target);
|
2024-04-07 09:10:45 +02:00
|
|
|
if (!android::base::SetProperty(ANDROID_RB_PROPERTY, cmd)) {
|
|
|
|
LOG(FATAL) << "Shutdown failed";
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) pause();
|
2019-04-29 20:23:16 +02:00
|
|
|
}
|
|
|
|
|
2018-07-17 07:09:59 +02:00
|
|
|
std::vector<char*> StringVectorToNullTerminatedArray(const std::vector<std::string>& args) {
|
|
|
|
std::vector<char*> result(args.size());
|
|
|
|
std::transform(args.cbegin(), args.cend(), result.begin(),
|
|
|
|
[](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
|
|
|
|
result.push_back(nullptr);
|
|
|
|
return result;
|
|
|
|
}
|