diff options
Diffstat (limited to 'otautil/include')
-rw-r--r-- | otautil/include/otautil/SysUtil.h | 53 | ||||
-rw-r--r-- | otautil/include/otautil/cache_location.h | 69 | ||||
-rw-r--r-- | otautil/include/otautil/dirutil.h (renamed from otautil/include/otautil/DirUtil.h) | 0 | ||||
-rw-r--r-- | otautil/include/otautil/error_code.h | 2 | ||||
-rw-r--r-- | otautil/include/otautil/logging.h | 65 | ||||
-rw-r--r-- | otautil/include/otautil/mounts.h | 25 | ||||
-rw-r--r-- | otautil/include/otautil/parse_install_logs.h | 33 | ||||
-rw-r--r-- | otautil/include/otautil/paths.h | 118 | ||||
-rw-r--r-- | otautil/include/otautil/rangeset.h | 7 | ||||
-rw-r--r-- | otautil/include/otautil/roots.h | 55 | ||||
-rw-r--r-- | otautil/include/otautil/sysutil.h | 114 | ||||
-rw-r--r-- | otautil/include/otautil/thermalutil.h (renamed from otautil/include/otautil/ThermalUtil.h) | 0 |
12 files changed, 419 insertions, 122 deletions
diff --git a/otautil/include/otautil/SysUtil.h b/otautil/include/otautil/SysUtil.h deleted file mode 100644 index 52f6d20a7..000000000 --- a/otautil/include/otautil/SysUtil.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2006 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_SYSUTIL -#define _OTAUTIL_SYSUTIL - -#include <sys/types.h> - -#include <string> -#include <vector> - -/* - * Use this to keep track of mapped segments. - */ -class MemMapping { - public: - ~MemMapping(); - // Map a file into a private, read-only memory segment. If 'filename' begins with an '@' - // character, it is a map of blocks to be mapped, otherwise it is treated as an ordinary file. - bool MapFile(const std::string& filename); - size_t ranges() const { - return ranges_.size(); - }; - - unsigned char* addr; // start of data - size_t length; // length of data - - private: - struct MappedRange { - void* addr; - size_t length; - }; - - bool MapBlockFile(const std::string& filename); - bool MapFD(int fd); - - std::vector<MappedRange> ranges_; -}; - -#endif // _OTAUTIL_SYSUTIL diff --git a/otautil/include/otautil/cache_location.h b/otautil/include/otautil/cache_location.h deleted file mode 100644 index f2f663816..000000000 --- a/otautil/include/otautil/cache_location.h +++ /dev/null @@ -1,69 +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. - */ - -#ifndef _OTAUTIL_OTAUTIL_CACHE_LOCATION_H_ -#define _OTAUTIL_OTAUTIL_CACHE_LOCATION_H_ - -#include <string> - -#include "android-base/macros.h" - -// A singleton class to maintain the update related locations. The locations should be only set -// once at the start of the program. -class CacheLocation { - public: - static CacheLocation& location(); - - // getter and setter functions. - std::string cache_temp_source() const { - return cache_temp_source_; - } - void set_cache_temp_source(const std::string& temp_source) { - cache_temp_source_ = temp_source; - } - - std::string last_command_file() const { - return last_command_file_; - } - void set_last_command_file(const std::string& last_command) { - last_command_file_ = last_command; - } - - std::string stash_directory_base() const { - return stash_directory_base_; - } - void set_stash_directory_base(const std::string& base) { - stash_directory_base_ = base; - } - - private: - CacheLocation(); - DISALLOW_COPY_AND_ASSIGN(CacheLocation); - - // When there isn't enough room on the target filesystem to hold the patched version of the file, - // we copy the original here and delete it to free up space. If the expected source file doesn't - // exist, or is corrupted, we look to see if the cached file contains the bits we want and use it - // as the source instead. The default location for the cached source is "/cache/saved.file". - std::string cache_temp_source_; - - // Location to save the last command that stashes blocks. - std::string last_command_file_; - - // The base directory to write stashes during update. - std::string stash_directory_base_; -}; - -#endif // _OTAUTIL_OTAUTIL_CACHE_LOCATION_H_ diff --git a/otautil/include/otautil/DirUtil.h b/otautil/include/otautil/dirutil.h index 85d6c16d1..85d6c16d1 100644 --- a/otautil/include/otautil/DirUtil.h +++ b/otautil/include/otautil/dirutil.h diff --git a/otautil/include/otautil/error_code.h b/otautil/include/otautil/error_code.h index b0ff42d8d..2b73c1353 100644 --- a/otautil/include/otautil/error_code.h +++ b/otautil/include/otautil/error_code.h @@ -48,6 +48,8 @@ enum CauseCode : int { kRebootFailure, kPackageExtractFileFailure, kPatchApplicationFailure, + kHashTreeComputationFailure, + kEioFailure, kVendorFailure = 200 }; diff --git a/otautil/include/otautil/logging.h b/otautil/include/otautil/logging.h new file mode 100644 index 000000000..608349785 --- /dev/null +++ b/otautil/include/otautil/logging.h @@ -0,0 +1,65 @@ +/* + * 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 <stddef.h> +#include <sys/stat.h> +#include <sys/types.h> + +#include <string> +#include <vector> + +#include <log/log_id.h> + +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, bool has_cache, const selabel_handle* sehandle); +void reset_tmplog_offset(); + +void save_kernel_log(const char* destination); + +std::vector<saved_log_file> ReadLogFilesToMemory(); + +bool RestoreLogFilesAfterFormat(const std::vector<saved_log_file>& log_files); + +#endif //_LOGGING_H diff --git a/otautil/include/otautil/mounts.h b/otautil/include/otautil/mounts.h new file mode 100644 index 000000000..6786c8d2e --- /dev/null +++ b/otautil/include/otautil/mounts.h @@ -0,0 +1,25 @@ +/* + * 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 + +struct MountedVolume; + +bool scan_mounted_volumes(); + +MountedVolume* find_mounted_volume_by_mount_point(const char* mount_point); + +int unmount_mounted_volume(MountedVolume* volume); diff --git a/otautil/include/otautil/parse_install_logs.h b/otautil/include/otautil/parse_install_logs.h new file mode 100644 index 000000000..135d29ccf --- /dev/null +++ b/otautil/include/otautil/parse_install_logs.h @@ -0,0 +1,33 @@ +/* + * 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 <stdint.h> + +#include <map> +#include <string> +#include <vector> + +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<std::string, int64_t> ParseRecoveryUpdateMetrics(const std::vector<std::string>& 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<std::string, int64_t> ParseLastInstall(const std::string& file_name); diff --git a/otautil/include/otautil/paths.h b/otautil/include/otautil/paths.h new file mode 100644 index 000000000..f95741a24 --- /dev/null +++ b/otautil/include/otautil/paths.h @@ -0,0 +1,118 @@ +/* + * 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. + */ + +#ifndef _OTAUTIL_PATHS_H_ +#define _OTAUTIL_PATHS_H_ + +#include <string> + +#include <android-base/macros.h> + +// A singleton class to maintain the update related paths. The paths should be only set once at the +// start of the program. +class Paths { + public: + static Paths& Get(); + + std::string cache_log_directory() const { + return cache_log_directory_; + } + void set_cache_log_directory(const std::string& log_dir) { + cache_log_directory_ = log_dir; + } + + std::string cache_temp_source() const { + return cache_temp_source_; + } + void set_cache_temp_source(const std::string& temp_source) { + cache_temp_source_ = temp_source; + } + + std::string last_command_file() const { + return last_command_file_; + } + void set_last_command_file(const std::string& last_command_file) { + last_command_file_ = last_command_file; + } + + std::string resource_dir() const { + return resource_dir_; + } + void set_resource_dir(const std::string& resource_dir) { + resource_dir_ = resource_dir; + } + + std::string stash_directory_base() const { + return stash_directory_base_; + } + void set_stash_directory_base(const std::string& base) { + stash_directory_base_ = base; + } + + std::string temporary_install_file() const { + return temporary_install_file_; + } + void set_temporary_install_file(const std::string& install_file) { + temporary_install_file_ = install_file; + } + + std::string temporary_log_file() const { + return temporary_log_file_; + } + void set_temporary_log_file(const std::string& log_file) { + temporary_log_file_ = log_file; + } + + std::string temporary_update_binary() const { + return temporary_update_binary_; + } + void set_temporary_update_binary(const std::string& update_binary) { + temporary_update_binary_ = update_binary; + } + + private: + Paths(); + DISALLOW_COPY_AND_ASSIGN(Paths); + + // Path to the directory that contains last_log and last_kmsg log files. + std::string cache_log_directory_; + + // Path to the temporary source file on /cache. When there isn't enough room on the target + // filesystem to hold the patched version of the file, we copy the original here and delete it to + // free up space. If the expected source file doesn't exist, or is corrupted, we look to see if + // the cached file contains the bits we want and use it as the source instead. + std::string cache_temp_source_; + + // Path to the last command file. + std::string last_command_file_; + + // Path to the resource dir; + std::string resource_dir_; + + // Path to the base directory to write stashes during update. + std::string stash_directory_base_; + + // Path to the temporary file that contains the install result. + std::string temporary_install_file_; + + // Path to the temporary log file while under recovery. + std::string temporary_log_file_; + + // Path to the temporary update binary while installing a non-A/B package. + std::string temporary_update_binary_; +}; + +#endif // _OTAUTIL_PATHS_H_ diff --git a/otautil/include/otautil/rangeset.h b/otautil/include/otautil/rangeset.h index e91d02ca6..a18c30e29 100644 --- a/otautil/include/otautil/rangeset.h +++ b/otautil/include/otautil/rangeset.h @@ -18,6 +18,7 @@ #include <stddef.h> +#include <optional> #include <string> #include <utility> #include <vector> @@ -49,6 +50,12 @@ class RangeSet { // bounds. For example, "3,5" contains blocks 3 and 4. So "3,5" and "5,7" are not overlapped. bool Overlaps(const RangeSet& other) const; + // Returns a subset of ranges starting from |start_index| with respect to the original range. The + // output range will have |num_of_blocks| blocks in size. Returns std::nullopt if the input is + // invalid. e.g. RangeSet({{0, 5}, {10, 15}}).GetSubRanges(1, 5) returns + // RangeSet({{1, 5}, {10, 11}}). + std::optional<RangeSet> GetSubRanges(size_t start_index, size_t num_of_blocks) const; + // Returns a vector of RangeSets that contain the same set of blocks represented by the current // RangeSet. The RangeSets in the vector contain similar number of blocks, with a maximum delta // of 1-block between any two of them. For example, 14 blocks would be split into 4 + 4 + 3 + 3, diff --git a/otautil/include/otautil/roots.h b/otautil/include/otautil/roots.h new file mode 100644 index 000000000..2ab3f4549 --- /dev/null +++ b/otautil/include/otautil/roots.h @@ -0,0 +1,55 @@ +/* + * 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 <string> + +#include <fstab/fstab.h> + +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(); diff --git a/otautil/include/otautil/sysutil.h b/otautil/include/otautil/sysutil.h new file mode 100644 index 000000000..48e9011e5 --- /dev/null +++ b/otautil/include/otautil/sysutil.h @@ -0,0 +1,114 @@ +/* + * Copyright 2006 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 <sys/types.h> + +#include <string> +#include <string_view> +#include <vector> + +#include "rangeset.h" + +// This class holds the content of a block map file. +class BlockMapData { + public: + // A "block map" which looks like this (from uncrypt/uncrypt.cpp): + // + // /dev/block/platform/msm_sdcc.1/by-name/userdata # block device + // 49652 4096 # file size in bytes, block size + // 3 # count of block ranges + // 1000 1008 # block range 0 + // 2100 2102 # ... block range 1 + // 30 33 # ... block range 2 + // + // Each block range represents a half-open interval; the line "30 33" reprents the blocks + // [30, 31, 32]. + static BlockMapData ParseBlockMapFile(const std::string& block_map_path); + + explicit operator bool() const { + return !path_.empty(); + } + + std::string path() const { + return path_; + } + uint64_t file_size() const { + return file_size_; + } + uint32_t block_size() const { + return block_size_; + } + RangeSet block_ranges() const { + return block_ranges_; + } + + private: + BlockMapData() = default; + + BlockMapData(const std::string& path, uint64_t file_size, uint32_t block_size, + RangeSet block_ranges) + : path_(path), + file_size_(file_size), + block_size_(block_size), + block_ranges_(std::move(block_ranges)) {} + + std::string path_; + uint64_t file_size_ = 0; + uint32_t block_size_ = 0; + RangeSet block_ranges_; +}; + +/* + * Use this to keep track of mapped segments. + */ +class MemMapping { + public: + ~MemMapping(); + // Map a file into a private, read-only memory segment. If 'filename' begins with an '@' + // character, it is a map of blocks to be mapped, otherwise it is treated as an ordinary file. + bool MapFile(const std::string& filename); + size_t ranges() const { + return ranges_.size(); + }; + + unsigned char* addr; // start of data + size_t length; // length of data + + private: + struct MappedRange { + void* addr; + size_t length; + }; + + bool MapBlockFile(const std::string& filename); + bool MapFD(int fd); + + std::vector<MappedRange> ranges_; +}; + +// Reboots the device into the specified target, by additionally handling quiescent reboot mode. +// 'target' can be an empty string, which indicates booting into Android. +bool Reboot(std::string_view target); + +// Triggers a shutdown. +bool Shutdown(); + +// Returns a null-terminated char* array, where the elements point to the C-strings in the given +// vector, plus an additional nullptr at the end. This is a helper function that facilitates +// calling C functions (such as getopt(3)) that expect an array of C-strings. +std::vector<char*> StringVectorToNullTerminatedArray(const std::vector<std::string>& args); diff --git a/otautil/include/otautil/ThermalUtil.h b/otautil/include/otautil/thermalutil.h index 43ab55940..43ab55940 100644 --- a/otautil/include/otautil/ThermalUtil.h +++ b/otautil/include/otautil/thermalutil.h |