From e3f09a72f51df5abcf46a63de4a39ca5ed53698b Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Tue, 1 Oct 2019 11:55:36 -0700 Subject: otautil: Factor out the utils that're private to recovery. A number of utility functions are intended for serving recovery's own use. Exposing them via libotautil (which is a static lib) would pass the dependencies onto libotautil's users (e.g. recovery image, updater, host simulator, device-specific recovery UI/updater extensions etc). This CL finds a new home for the utils that are private to recovery. Test: mmma bootable/recovery Change-Id: I575e97ad099b85fe1c1c8c7c9458a5a43d4e11e1 --- otautil/Android.bp | 28 --- otautil/include/otautil/logging.h | 65 ------ otautil/include/otautil/parse_install_logs.h | 33 --- otautil/include/otautil/roots.h | 58 ----- otautil/include/otautil/thermalutil.h | 24 -- otautil/logging.cpp | 324 --------------------------- otautil/parse_install_logs.cpp | 114 ---------- otautil/roots.cpp | 279 ----------------------- otautil/thermalutil.cpp | 80 ------- 9 files changed, 1005 deletions(-) delete mode 100644 otautil/include/otautil/logging.h delete mode 100644 otautil/include/otautil/parse_install_logs.h delete mode 100644 otautil/include/otautil/roots.h delete mode 100644 otautil/include/otautil/thermalutil.h delete mode 100644 otautil/logging.cpp delete mode 100644 otautil/parse_install_logs.cpp delete mode 100644 otautil/roots.cpp delete mode 100644 otautil/thermalutil.cpp (limited to 'otautil') diff --git a/otautil/Android.bp b/otautil/Android.bp index 6f816a17d..c8f974688 100644 --- a/otautil/Android.bp +++ b/otautil/Android.bp @@ -39,32 +39,4 @@ cc_library_static { export_include_dirs: [ "include", ], - - target: { - android: { - srcs: [ - "logging.cpp", - "parse_install_logs.cpp", - "roots.cpp", - "thermalutil.cpp", - ], - - include_dirs: [ - "system/vold", - ], - - static_libs: [ - "libfstab", - ], - - shared_libs: [ - "libext4_utils", - "libfs_mgr", - ], - - export_static_lib_headers: [ - "libfstab", - ], - }, - }, } diff --git a/otautil/include/otautil/logging.h b/otautil/include/otautil/logging.h deleted file mode 100644 index 4462eca6e..000000000 --- a/otautil/include/otautil/logging.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (C) 2016 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. - */ - -#ifndef _LOGGING_H -#define _LOGGING_H - -#include -#include -#include - -#include -#include - -#include - -static constexpr int KEEP_LOG_COUNT = 10; - -struct selabel_handle; - -struct saved_log_file { - std::string name; - struct stat sb; - std::string data; -}; - -void SetLoggingSehandle(selabel_handle* handle); - -ssize_t logbasename(log_id_t id, char prio, const char* filename, const char* buf, size_t len, - void* arg); - -ssize_t logrotate(log_id_t id, char prio, const char* filename, const char* buf, size_t len, - void* arg); - -// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max. -// Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max. -// Overwrite any existing last_log.$max and last_kmsg.$max. -void rotate_logs(const char* last_log_file, const char* last_kmsg_file); - -// In turn fflush(3)'s, fsync(3)'s and fclose(3)'s the given stream. -void check_and_fclose(FILE* fp, const std::string& name); - -void copy_log_file_to_pmsg(const std::string& source, const std::string& destination); -void copy_logs(bool save_current_log); -void reset_tmplog_offset(); - -void save_kernel_log(const char* destination); - -std::vector ReadLogFilesToMemory(); - -bool RestoreLogFilesAfterFormat(const std::vector& log_files); - -#endif //_LOGGING_H diff --git a/otautil/include/otautil/parse_install_logs.h b/otautil/include/otautil/parse_install_logs.h deleted file mode 100644 index 135d29ccf..000000000 --- a/otautil/include/otautil/parse_install_logs.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (C) 2018 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. - */ - -#pragma once - -#include - -#include -#include -#include - -constexpr const char* LAST_INSTALL_FILE = "/data/misc/recovery/last_install"; -constexpr const char* LAST_INSTALL_FILE_IN_CACHE = "/cache/recovery/last_install"; - -// Parses the metrics of update applied under recovery mode in |lines|, and returns a map with -// "name: value". -std::map ParseRecoveryUpdateMetrics(const std::vector& lines); -// Parses the sideload history and update metrics in the last_install file. Returns a map with -// entries as "metrics_name: value". If no such file exists, returns an empty map. -std::map ParseLastInstall(const std::string& file_name); diff --git a/otautil/include/otautil/roots.h b/otautil/include/otautil/roots.h deleted file mode 100644 index 92ee756f0..000000000 --- a/otautil/include/otautil/roots.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (C) 2007 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. - */ - -#pragma once - -#include - -#include - -using Volume = android::fs_mgr::FstabEntry; - -// Load and parse volume data from /etc/recovery.fstab. -void load_volume_table(); - -// Return the Volume* record for this mount point (or nullptr). -Volume* volume_for_mount_point(const std::string& mount_point); - -// Make sure that the volume 'path' is on is mounted. Returns 0 on -// success (volume is mounted). -int ensure_path_mounted(const std::string& path); - -// Similar to ensure_path_mounted, but allows one to specify the mount_point. -int ensure_path_mounted_at(const std::string& path, const std::string& mount_point); - -// Make sure that the volume 'path' is on is unmounted. Returns 0 on -// success (volume is unmounted); -int ensure_path_unmounted(const std::string& path); - -// Reformat the given volume (must be the mount point only, eg -// "/cache"), no paths permitted. Attempts to unmount the volume if -// it is mounted. -int format_volume(const std::string& volume); - -// Reformat the given volume (must be the mount point only, eg -// "/cache"), no paths permitted. Attempts to unmount the volume if -// it is mounted. -// Copies 'directory' to root of the newly formatted volume -int format_volume(const std::string& volume, const std::string& directory); - -// Ensure that all and only the volumes that packages expect to find -// mounted (/tmp and /cache) are mounted. Returns 0 on success. -int setup_install_mounts(); - -// Returns true if there is /cache in the volumes. -bool HasCache(); diff --git a/otautil/include/otautil/thermalutil.h b/otautil/include/otautil/thermalutil.h deleted file mode 100644 index 43ab55940..000000000 --- a/otautil/include/otautil/thermalutil.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (C) 2017 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. - */ - -#ifndef OTAUTIL_THERMALUTIL_H -#define OTAUTIL_THERMALUTIL_H - -// We can find the temperature reported by all sensors in /sys/class/thermal/thermal_zone*/temp. -// Their values are in millidegree Celsius; and we will log the maximum one. -int GetMaxValueFromThermalZone(); - -#endif // OTAUTIL_THERMALUTIL_H diff --git a/otautil/logging.cpp b/otautil/logging.cpp deleted file mode 100644 index 3db0e8ac2..000000000 --- a/otautil/logging.cpp +++ /dev/null @@ -1,324 +0,0 @@ -/* - * Copyright (C) 2016 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. - */ - -#include "otautil/logging.h" - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include /* for AID_SYSTEM */ -#include /* private pmsg functions */ -#include - -#include "otautil/dirutil.h" -#include "otautil/paths.h" -#include "otautil/roots.h" - -constexpr const char* LOG_FILE = "/cache/recovery/log"; -constexpr const char* LAST_INSTALL_FILE = "/cache/recovery/last_install"; -constexpr const char* LAST_KMSG_FILE = "/cache/recovery/last_kmsg"; -constexpr const char* LAST_LOG_FILE = "/cache/recovery/last_log"; - -constexpr const char* LAST_KMSG_FILTER = "recovery/last_kmsg"; -constexpr const char* LAST_LOG_FILTER = "recovery/last_log"; - -constexpr const char* CACHE_LOG_DIR = "/cache/recovery"; - -static struct selabel_handle* logging_sehandle; - -void SetLoggingSehandle(selabel_handle* handle) { - logging_sehandle = handle; -} - -// fopen(3)'s the given file, by mounting volumes and making parent dirs as necessary. Returns the -// file pointer, or nullptr on error. -static FILE* fopen_path(const std::string& path, const char* mode, const selabel_handle* sehandle) { - if (ensure_path_mounted(path) != 0) { - LOG(ERROR) << "Can't mount " << path; - return nullptr; - } - - // When writing, try to create the containing directory, if necessary. Use generous permissions, - // the system (init.rc) will reset them. - if (strchr("wa", mode[0])) { - mkdir_recursively(path, 0777, true, sehandle); - } - return fopen(path.c_str(), mode); -} - -void check_and_fclose(FILE* fp, const std::string& name) { - fflush(fp); - if (fsync(fileno(fp)) == -1) { - PLOG(ERROR) << "Failed to fsync " << name; - } - if (ferror(fp)) { - PLOG(ERROR) << "Error in " << name; - } - fclose(fp); -} - -// close a file, log an error if the error indicator is set -ssize_t logbasename(log_id_t /* id */, char /* prio */, const char* filename, const char* /* buf */, - size_t len, void* arg) { - bool* do_rotate = static_cast(arg); - if (std::string(LAST_KMSG_FILTER).find(filename) != std::string::npos || - std::string(LAST_LOG_FILTER).find(filename) != std::string::npos) { - *do_rotate = true; - } - return len; -} - -ssize_t logrotate(log_id_t id, char prio, const char* filename, const char* buf, size_t len, - void* arg) { - bool* do_rotate = static_cast(arg); - if (!*do_rotate) { - return __android_log_pmsg_file_write(id, prio, filename, buf, len); - } - - std::string name(filename); - size_t dot = name.find_last_of('.'); - std::string sub = name.substr(0, dot); - - if (std::string(LAST_KMSG_FILTER).find(sub) == std::string::npos && - std::string(LAST_LOG_FILTER).find(sub) == std::string::npos) { - return __android_log_pmsg_file_write(id, prio, filename, buf, len); - } - - // filename rotation - if (dot == std::string::npos) { - name += ".1"; - } else { - std::string number = name.substr(dot + 1); - if (!isdigit(number[0])) { - name += ".1"; - } else { - size_t i; - if (!android::base::ParseUint(number, &i)) { - LOG(ERROR) << "failed to parse uint in " << number; - return -1; - } - name = sub + "." + std::to_string(i + 1); - } - } - - return __android_log_pmsg_file_write(id, prio, name.c_str(), buf, len); -} - -// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max. -// Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max. -// Overwrite any existing last_log.$max and last_kmsg.$max. -void rotate_logs(const char* last_log_file, const char* last_kmsg_file) { - // Logs should only be rotated once. - static bool rotated = false; - if (rotated) { - return; - } - rotated = true; - - for (int i = KEEP_LOG_COUNT - 1; i >= 0; --i) { - std::string old_log = android::base::StringPrintf("%s", last_log_file); - if (i > 0) { - old_log += "." + std::to_string(i); - } - std::string new_log = android::base::StringPrintf("%s.%d", last_log_file, i + 1); - // Ignore errors if old_log doesn't exist. - rename(old_log.c_str(), new_log.c_str()); - - std::string old_kmsg = android::base::StringPrintf("%s", last_kmsg_file); - if (i > 0) { - old_kmsg += "." + std::to_string(i); - } - std::string new_kmsg = android::base::StringPrintf("%s.%d", last_kmsg_file, i + 1); - rename(old_kmsg.c_str(), new_kmsg.c_str()); - } -} - -// Writes content to the current pmsg session. -static ssize_t __pmsg_write(const std::string& filename, const std::string& buf) { - return __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filename.c_str(), - buf.data(), buf.size()); -} - -void copy_log_file_to_pmsg(const std::string& source, const std::string& destination) { - std::string content; - android::base::ReadFileToString(source, &content); - __pmsg_write(destination, content); -} - -// How much of the temp log we have copied to the copy in cache. -static off_t tmplog_offset = 0; - -void reset_tmplog_offset() { - tmplog_offset = 0; -} - -static void copy_log_file(const std::string& source, const std::string& destination, bool append) { - FILE* dest_fp = fopen_path(destination, append ? "ae" : "we", logging_sehandle); - if (dest_fp == nullptr) { - PLOG(ERROR) << "Can't open " << destination; - } else { - FILE* source_fp = fopen(source.c_str(), "re"); - if (source_fp != nullptr) { - if (append) { - fseeko(source_fp, tmplog_offset, SEEK_SET); // Since last write - } - char buf[4096]; - size_t bytes; - while ((bytes = fread(buf, 1, sizeof(buf), source_fp)) != 0) { - fwrite(buf, 1, bytes, dest_fp); - } - if (append) { - tmplog_offset = ftello(source_fp); - } - check_and_fclose(source_fp, source); - } - check_and_fclose(dest_fp, destination); - } -} - -void copy_logs(bool save_current_log) { - // We only rotate and record the log of the current session if explicitly requested. This usually - // happens after wipes, installation from BCB or menu selections. This is to avoid unnecessary - // rotation (and possible deletion) of log files, if it does not do anything loggable. - if (!save_current_log) { - return; - } - - // Always write to pmsg, this allows the OTA logs to be caught in `logcat -L`. - copy_log_file_to_pmsg(Paths::Get().temporary_log_file(), LAST_LOG_FILE); - copy_log_file_to_pmsg(Paths::Get().temporary_install_file(), LAST_INSTALL_FILE); - - // We can do nothing for now if there's no /cache partition. - if (!HasCache()) { - return; - } - - ensure_path_mounted(LAST_LOG_FILE); - ensure_path_mounted(LAST_KMSG_FILE); - rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE); - - // Copy logs to cache so the system can find out what happened. - copy_log_file(Paths::Get().temporary_log_file(), LOG_FILE, true); - copy_log_file(Paths::Get().temporary_log_file(), LAST_LOG_FILE, false); - copy_log_file(Paths::Get().temporary_install_file(), LAST_INSTALL_FILE, false); - save_kernel_log(LAST_KMSG_FILE); - chmod(LOG_FILE, 0600); - chown(LOG_FILE, AID_SYSTEM, AID_SYSTEM); - chmod(LAST_KMSG_FILE, 0600); - chown(LAST_KMSG_FILE, AID_SYSTEM, AID_SYSTEM); - chmod(LAST_LOG_FILE, 0640); - chmod(LAST_INSTALL_FILE, 0644); - chown(LAST_INSTALL_FILE, AID_SYSTEM, AID_SYSTEM); - sync(); -} - -// Read from kernel log into buffer and write out to file. -void save_kernel_log(const char* destination) { - int klog_buf_len = klogctl(KLOG_SIZE_BUFFER, 0, 0); - if (klog_buf_len <= 0) { - PLOG(ERROR) << "Error getting klog size"; - return; - } - - std::string buffer(klog_buf_len, 0); - int n = klogctl(KLOG_READ_ALL, &buffer[0], klog_buf_len); - if (n == -1) { - PLOG(ERROR) << "Error in reading klog"; - return; - } - buffer.resize(n); - android::base::WriteStringToFile(buffer, destination); -} - -std::vector ReadLogFilesToMemory() { - ensure_path_mounted("/cache"); - - struct dirent* de; - std::unique_ptr d(opendir(CACHE_LOG_DIR), closedir); - if (!d) { - if (errno != ENOENT) { - PLOG(ERROR) << "Failed to opendir " << CACHE_LOG_DIR; - } - return {}; - } - - std::vector log_files; - while ((de = readdir(d.get())) != nullptr) { - if (strncmp(de->d_name, "last_", 5) == 0 || strcmp(de->d_name, "log") == 0) { - std::string path = android::base::StringPrintf("%s/%s", CACHE_LOG_DIR, de->d_name); - - struct stat sb; - if (stat(path.c_str(), &sb) != 0) { - PLOG(ERROR) << "Failed to stat " << path; - continue; - } - // Truncate files to 512kb - size_t read_size = std::min(sb.st_size, 1 << 19); - std::string data(read_size, '\0'); - - android::base::unique_fd log_fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY))); - if (log_fd == -1 || !android::base::ReadFully(log_fd, data.data(), read_size)) { - PLOG(ERROR) << "Failed to read log file " << path; - continue; - } - - log_files.emplace_back(saved_log_file{ path, sb, data }); - } - } - - return log_files; -} - -bool RestoreLogFilesAfterFormat(const std::vector& log_files) { - // Re-create the log dir and write back the log entries. - if (ensure_path_mounted(CACHE_LOG_DIR) != 0) { - PLOG(ERROR) << "Failed to mount " << CACHE_LOG_DIR; - return false; - } - - if (mkdir_recursively(CACHE_LOG_DIR, 0777, false, logging_sehandle) != 0) { - PLOG(ERROR) << "Failed to create " << CACHE_LOG_DIR; - return false; - } - - for (const auto& log : log_files) { - if (!android::base::WriteStringToFile(log.data, log.name, log.sb.st_mode, log.sb.st_uid, - log.sb.st_gid)) { - PLOG(ERROR) << "Failed to write to " << log.name; - } - } - - // Any part of the log we'd copied to cache is now gone. - // Reset the pointer so we copy from the beginning of the temp - // log. - reset_tmplog_offset(); - copy_logs(true /* save_current_log */); - - return true; -} diff --git a/otautil/parse_install_logs.cpp b/otautil/parse_install_logs.cpp deleted file mode 100644 index 13a729921..000000000 --- a/otautil/parse_install_logs.cpp +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (C) 2018 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. - */ - -#include "otautil/parse_install_logs.h" - -#include - -#include - -#include -#include -#include -#include -#include - -constexpr const char* OTA_SIDELOAD_METRICS = "ota_sideload"; - -// Here is an example of lines in last_install: -// ... -// time_total: 101 -// bytes_written_vendor: 51074 -// bytes_stashed_vendor: 200 -std::map ParseRecoveryUpdateMetrics(const std::vector& lines) { - constexpr unsigned int kMiB = 1024 * 1024; - std::optional bytes_written_in_mib; - std::optional bytes_stashed_in_mib; - std::map metrics; - for (const auto& line : lines) { - size_t num_index = line.find(':'); - if (num_index == std::string::npos) { - LOG(WARNING) << "Skip parsing " << line; - continue; - } - - std::string num_string = android::base::Trim(line.substr(num_index + 1)); - int64_t parsed_num; - if (!android::base::ParseInt(num_string, &parsed_num)) { - LOG(ERROR) << "Failed to parse numbers in " << line; - continue; - } - - if (android::base::StartsWith(line, "bytes_written")) { - bytes_written_in_mib = bytes_written_in_mib.value_or(0) + parsed_num / kMiB; - } else if (android::base::StartsWith(line, "bytes_stashed")) { - bytes_stashed_in_mib = bytes_stashed_in_mib.value_or(0) + parsed_num / kMiB; - } else if (android::base::StartsWith(line, "time")) { - metrics.emplace("ota_time_total", parsed_num); - } else if (android::base::StartsWith(line, "uncrypt_time")) { - metrics.emplace("ota_uncrypt_time", parsed_num); - } else if (android::base::StartsWith(line, "source_build")) { - metrics.emplace("ota_source_version", parsed_num); - } else if (android::base::StartsWith(line, "temperature_start")) { - metrics.emplace("ota_temperature_start", parsed_num); - } else if (android::base::StartsWith(line, "temperature_end")) { - metrics.emplace("ota_temperature_end", parsed_num); - } else if (android::base::StartsWith(line, "temperature_max")) { - metrics.emplace("ota_temperature_max", parsed_num); - } else if (android::base::StartsWith(line, "error")) { - metrics.emplace("ota_non_ab_error_code", parsed_num); - } else if (android::base::StartsWith(line, "cause")) { - metrics.emplace("ota_non_ab_cause_code", parsed_num); - } - } - - if (bytes_written_in_mib) { - metrics.emplace("ota_written_in_MiBs", bytes_written_in_mib.value()); - } - if (bytes_stashed_in_mib) { - metrics.emplace("ota_stashed_in_MiBs", bytes_stashed_in_mib.value()); - } - - return metrics; -} - -std::map ParseLastInstall(const std::string& file_name) { - if (access(file_name.c_str(), F_OK) != 0) { - return {}; - } - - std::string content; - if (!android::base::ReadFileToString(file_name, &content)) { - PLOG(ERROR) << "Failed to read " << file_name; - return {}; - } - - if (content.empty()) { - LOG(INFO) << "Empty last_install file"; - return {}; - } - - std::vector lines = android::base::Split(content, "\n"); - auto metrics = ParseRecoveryUpdateMetrics(lines); - - // LAST_INSTALL starts with "/sideload/package.zip" after a sideload. - if (android::base::Trim(lines[0]) == "/sideload/package.zip") { - int type = (android::base::GetProperty("ro.build.type", "") == "user") ? 1 : 0; - metrics.emplace(OTA_SIDELOAD_METRICS, type); - } - - return metrics; -} diff --git a/otautil/roots.cpp b/otautil/roots.cpp deleted file mode 100644 index e098b4b77..000000000 --- a/otautil/roots.cpp +++ /dev/null @@ -1,279 +0,0 @@ -/* - * Copyright (C) 2007 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. - */ - -#include "otautil/roots.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include "otautil/sysutil.h" - -using android::fs_mgr::Fstab; -using android::fs_mgr::FstabEntry; -using android::fs_mgr::ReadDefaultFstab; - -static Fstab fstab; - -constexpr const char* CACHE_ROOT = "/cache"; - -void load_volume_table() { - if (!ReadDefaultFstab(&fstab)) { - LOG(ERROR) << "Failed to read default fstab"; - return; - } - - fstab.emplace_back(FstabEntry{ - .mount_point = "/tmp", .fs_type = "ramdisk", .blk_device = "ramdisk", .length = 0 }); - - std::cout << "recovery filesystem table" << std::endl << "=========================" << std::endl; - for (size_t i = 0; i < fstab.size(); ++i) { - const auto& entry = fstab[i]; - std::cout << " " << i << " " << entry.mount_point << " " - << " " << entry.fs_type << " " << entry.blk_device << " " << entry.length - << std::endl; - } - std::cout << std::endl; -} - -Volume* volume_for_mount_point(const std::string& mount_point) { - return android::fs_mgr::GetEntryForMountPoint(&fstab, mount_point); -} - -// Mount the volume specified by path at the given mount_point. -int ensure_path_mounted_at(const std::string& path, const std::string& mount_point) { - return android::fs_mgr::EnsurePathMounted(&fstab, path, mount_point) ? 0 : -1; -} - -int ensure_path_mounted(const std::string& path) { - // Mount at the default mount point. - return android::fs_mgr::EnsurePathMounted(&fstab, path) ? 0 : -1; -} - -int ensure_path_unmounted(const std::string& path) { - return android::fs_mgr::EnsurePathUnmounted(&fstab, path) ? 0 : -1; -} - -static int exec_cmd(const std::vector& args) { - CHECK(!args.empty()); - auto argv = StringVectorToNullTerminatedArray(args); - - pid_t child; - if ((child = fork()) == 0) { - execv(argv[0], argv.data()); - _exit(EXIT_FAILURE); - } - - int status; - waitpid(child, &status, 0); - if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { - LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status); - } - return WEXITSTATUS(status); -} - -static int64_t get_file_size(int fd, uint64_t reserve_len) { - struct stat buf; - int ret = fstat(fd, &buf); - if (ret) return 0; - - int64_t computed_size; - if (S_ISREG(buf.st_mode)) { - computed_size = buf.st_size - reserve_len; - } else if (S_ISBLK(buf.st_mode)) { - uint64_t block_device_size = get_block_device_size(fd); - if (block_device_size < reserve_len || - block_device_size > std::numeric_limits::max()) { - computed_size = 0; - } else { - computed_size = block_device_size - reserve_len; - } - } else { - computed_size = 0; - } - - return computed_size; -} - -int format_volume(const std::string& volume, const std::string& directory) { - const FstabEntry* v = android::fs_mgr::GetEntryForPath(&fstab, volume); - if (v == nullptr) { - LOG(ERROR) << "unknown volume \"" << volume << "\""; - return -1; - } - if (v->fs_type == "ramdisk") { - LOG(ERROR) << "can't format_volume \"" << volume << "\""; - return -1; - } - if (v->mount_point != volume) { - LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume"; - return -1; - } - if (ensure_path_unmounted(volume) != 0) { - LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\""; - return -1; - } - if (v->fs_type != "ext4" && v->fs_type != "f2fs") { - LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported"; - return -1; - } - - // If there's a key_loc that looks like a path, it should be a block device for storing encryption - // metadata. Wipe it too. - if (!v->key_loc.empty() && v->key_loc[0] == '/') { - LOG(INFO) << "Wiping " << v->key_loc; - int fd = open(v->key_loc.c_str(), O_WRONLY | O_CREAT, 0644); - if (fd == -1) { - PLOG(ERROR) << "format_volume: Failed to open " << v->key_loc; - return -1; - } - wipe_block_device(fd, get_file_size(fd)); - close(fd); - } - - int64_t length = 0; - if (v->length > 0) { - length = v->length; - } else if (v->length < 0 || v->key_loc == "footer") { - android::base::unique_fd fd(open(v->blk_device.c_str(), O_RDONLY)); - if (fd == -1) { - PLOG(ERROR) << "format_volume: failed to open " << v->blk_device; - return -1; - } - length = get_file_size(fd.get(), v->length ? -v->length : CRYPT_FOOTER_OFFSET); - if (length <= 0) { - LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device; - return -1; - } - } - - if (v->fs_type == "ext4") { - static constexpr int kBlockSize = 4096; - std::vector mke2fs_args = { - "/system/bin/mke2fs", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize), - }; - - int raid_stride = v->logical_blk_size / kBlockSize; - int raid_stripe_width = v->erase_blk_size / kBlockSize; - // stride should be the max of 8KB and logical block size - if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) { - raid_stride = 8192 / kBlockSize; - } - if (v->erase_blk_size != 0 && v->logical_blk_size != 0) { - mke2fs_args.push_back("-E"); - mke2fs_args.push_back( - android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width)); - } - mke2fs_args.push_back(v->blk_device); - if (length != 0) { - mke2fs_args.push_back(std::to_string(length / kBlockSize)); - } - - int result = exec_cmd(mke2fs_args); - if (result == 0 && !directory.empty()) { - std::vector e2fsdroid_args = { - "/system/bin/e2fsdroid", "-e", "-f", directory, "-a", volume, v->blk_device, - }; - result = exec_cmd(e2fsdroid_args); - } - - if (result != 0) { - PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device; - return -1; - } - return 0; - } - - // Has to be f2fs because we checked earlier. - static constexpr int kSectorSize = 4096; - std::vector make_f2fs_cmd = { - "/system/bin/make_f2fs", - "-g", - "android", - v->blk_device, - }; - if (length >= kSectorSize) { - make_f2fs_cmd.push_back(std::to_string(length / kSectorSize)); - } - - if (exec_cmd(make_f2fs_cmd) != 0) { - PLOG(ERROR) << "format_volume: Failed to make_f2fs on " << v->blk_device; - return -1; - } - if (!directory.empty()) { - std::vector sload_f2fs_cmd = { - "/system/bin/sload_f2fs", "-f", directory, "-t", volume, v->blk_device, - }; - if (exec_cmd(sload_f2fs_cmd) != 0) { - PLOG(ERROR) << "format_volume: Failed to sload_f2fs on " << v->blk_device; - return -1; - } - } - return 0; -} - -int format_volume(const std::string& volume) { - return format_volume(volume, ""); -} - -int setup_install_mounts() { - if (fstab.empty()) { - LOG(ERROR) << "can't set up install mounts: no fstab loaded"; - return -1; - } - for (const FstabEntry& entry : fstab) { - // We don't want to do anything with "/". - if (entry.mount_point == "/") { - continue; - } - - if (entry.mount_point == "/tmp" || entry.mount_point == "/cache") { - if (ensure_path_mounted(entry.mount_point) != 0) { - LOG(ERROR) << "Failed to mount " << entry.mount_point; - return -1; - } - } else { - if (ensure_path_unmounted(entry.mount_point) != 0) { - LOG(ERROR) << "Failed to unmount " << entry.mount_point; - return -1; - } - } - } - return 0; -} - -bool HasCache() { - CHECK(!fstab.empty()); - static bool has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr; - return has_cache; -} diff --git a/otautil/thermalutil.cpp b/otautil/thermalutil.cpp deleted file mode 100644 index 4660e057e..000000000 --- a/otautil/thermalutil.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (C) 2017 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. - */ - -#include "otautil/thermalutil.h" - -#include -#include - -#include -#include -#include - -#include -#include -#include -#include - -static constexpr auto THERMAL_PREFIX = "/sys/class/thermal/"; - -static int thermal_filter(const dirent* de) { - if (android::base::StartsWith(de->d_name, "thermal_zone")) { - return 1; - } - return 0; -} - -static std::vector InitThermalPaths() { - dirent** namelist; - int n = scandir(THERMAL_PREFIX, &namelist, thermal_filter, alphasort); - if (n == -1) { - PLOG(ERROR) << "Failed to scandir " << THERMAL_PREFIX; - return {}; - } - if (n == 0) { - LOG(ERROR) << "Failed to find CPU thermal info in " << THERMAL_PREFIX; - return {}; - } - - std::vector thermal_paths; - while (n--) { - thermal_paths.push_back(THERMAL_PREFIX + std::string(namelist[n]->d_name) + "/temp"); - free(namelist[n]); - } - free(namelist); - return thermal_paths; -} - -int GetMaxValueFromThermalZone() { - static std::vector thermal_paths = InitThermalPaths(); - int max_temperature = -1; - for (const auto& path : thermal_paths) { - std::string content; - if (!android::base::ReadFileToString(path, &content)) { - PLOG(WARNING) << "Failed to read " << path; - continue; - } - - int temperature; - if (!android::base::ParseInt(android::base::Trim(content), &temperature)) { - LOG(WARNING) << "Failed to parse integer in " << content; - continue; - } - max_temperature = std::max(temperature, max_temperature); - } - LOG(INFO) << "current maximum temperature: " << max_temperature; - return max_temperature; -} -- cgit v1.2.3