The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 15 | */ |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 16 | |
Tao Bao | 782dcc1 | 2019-04-29 11:23:16 -0700 | [diff] [blame] | 17 | #pragma once |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 18 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 21 | #include <string> |
Tao Bao | 782dcc1 | 2019-04-29 11:23:16 -0700 | [diff] [blame] | 22 | #include <string_view> |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 23 | #include <vector> |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 24 | |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 25 | #include "rangeset.h" |
| 26 | |
| 27 | // This class holds the content of a block map file. |
| 28 | class BlockMapData { |
| 29 | public: |
| 30 | // A "block map" which looks like this (from uncrypt/uncrypt.cpp): |
| 31 | // |
| 32 | // /dev/block/platform/msm_sdcc.1/by-name/userdata # block device |
| 33 | // 49652 4096 # file size in bytes, block size |
| 34 | // 3 # count of block ranges |
| 35 | // 1000 1008 # block range 0 |
| 36 | // 2100 2102 # ... block range 1 |
| 37 | // 30 33 # ... block range 2 |
| 38 | // |
| 39 | // Each block range represents a half-open interval; the line "30 33" reprents the blocks |
| 40 | // [30, 31, 32]. |
| 41 | static BlockMapData ParseBlockMapFile(const std::string& block_map_path); |
| 42 | |
| 43 | explicit operator bool() const { |
| 44 | return !path_.empty(); |
| 45 | } |
| 46 | |
| 47 | std::string path() const { |
| 48 | return path_; |
| 49 | } |
| 50 | uint64_t file_size() const { |
| 51 | return file_size_; |
| 52 | } |
| 53 | uint32_t block_size() const { |
| 54 | return block_size_; |
| 55 | } |
| 56 | RangeSet block_ranges() const { |
| 57 | return block_ranges_; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | BlockMapData() = default; |
| 62 | |
| 63 | BlockMapData(const std::string& path, uint64_t file_size, uint32_t block_size, |
| 64 | RangeSet block_ranges) |
| 65 | : path_(path), |
| 66 | file_size_(file_size), |
| 67 | block_size_(block_size), |
| 68 | block_ranges_(std::move(block_ranges)) {} |
| 69 | |
| 70 | std::string path_; |
| 71 | uint64_t file_size_ = 0; |
| 72 | uint32_t block_size_ = 0; |
| 73 | RangeSet block_ranges_; |
| 74 | }; |
| 75 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 76 | /* |
| 77 | * Use this to keep track of mapped segments. |
| 78 | */ |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 79 | class MemMapping { |
| 80 | public: |
| 81 | ~MemMapping(); |
| 82 | // Map a file into a private, read-only memory segment. If 'filename' begins with an '@' |
| 83 | // character, it is a map of blocks to be mapped, otherwise it is treated as an ordinary file. |
| 84 | bool MapFile(const std::string& filename); |
| 85 | size_t ranges() const { |
| 86 | return ranges_.size(); |
| 87 | }; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 88 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 89 | unsigned char* addr; // start of data |
| 90 | size_t length; // length of data |
| 91 | |
| 92 | private: |
| 93 | struct MappedRange { |
| 94 | void* addr; |
| 95 | size_t length; |
| 96 | }; |
| 97 | |
| 98 | bool MapBlockFile(const std::string& filename); |
| 99 | bool MapFD(int fd); |
| 100 | |
| 101 | std::vector<MappedRange> ranges_; |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 102 | }; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 103 | |
Tao Bao | 782dcc1 | 2019-04-29 11:23:16 -0700 | [diff] [blame] | 104 | // Reboots the device into the specified target, by additionally handling quiescent reboot mode. |
Mark Salyzyn | 488cc05 | 2019-05-20 10:36:16 -0700 | [diff] [blame] | 105 | // All unknown targets reboot into Android. |
Tao Bao | 782dcc1 | 2019-04-29 11:23:16 -0700 | [diff] [blame] | 106 | bool Reboot(std::string_view target); |
| 107 | |
| 108 | // Triggers a shutdown. |
Mark Salyzyn | 488cc05 | 2019-05-20 10:36:16 -0700 | [diff] [blame] | 109 | bool Shutdown(std::string_view target); |
Tao Bao | 2c52639 | 2018-05-03 23:01:13 -0700 | [diff] [blame] | 110 | |
Tao Bao | 1700cc4 | 2018-07-16 22:09:59 -0700 | [diff] [blame] | 111 | // Returns a null-terminated char* array, where the elements point to the C-strings in the given |
| 112 | // vector, plus an additional nullptr at the end. This is a helper function that facilitates |
| 113 | // calling C functions (such as getopt(3)) that expect an array of C-strings. |
| 114 | std::vector<char*> StringVectorToNullTerminatedArray(const std::vector<std::string>& args); |