blob: 326db86446275cc6ac5511500149472159e0c4b1 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright 2006 The Android Open Source Project
3 *
Tao Baoc3292f32016-11-04 10:52:13 -07004 * 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 Projectc24a8e62009-03-03 19:28:42 -080015 */
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080016
Tao Bao782dcc12019-04-29 11:23:16 -070017#pragma once
Tao Baoc3292f32016-11-04 10:52:13 -070018
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080019#include <sys/types.h>
20
Tao Baob656a152017-04-18 23:54:29 -070021#include <string>
Tao Bao782dcc12019-04-29 11:23:16 -070022#include <string_view>
Tao Baoc3292f32016-11-04 10:52:13 -070023#include <vector>
Doug Zongker99916f02014-01-13 14:16:58 -080024
xunchang625c5882019-03-22 09:33:49 -070025#include "rangeset.h"
26
27// This class holds the content of a block map file.
28class 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 Projectc24a8e62009-03-03 19:28:42 -080076/*
77 * Use this to keep track of mapped segments.
78 */
Tao Baob656a152017-04-18 23:54:29 -070079class 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 Projectc24a8e62009-03-03 19:28:42 -080088
Tao Baob656a152017-04-18 23:54:29 -070089 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 Baoc3292f32016-11-04 10:52:13 -0700102};
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800103
Tao Bao782dcc12019-04-29 11:23:16 -0700104// Reboots the device into the specified target, by additionally handling quiescent reboot mode.
Mark Salyzyn488cc052019-05-20 10:36:16 -0700105// All unknown targets reboot into Android.
Tao Bao782dcc12019-04-29 11:23:16 -0700106bool Reboot(std::string_view target);
107
108// Triggers a shutdown.
Mark Salyzyn488cc052019-05-20 10:36:16 -0700109bool Shutdown(std::string_view target);
Tao Bao2c526392018-05-03 23:01:13 -0700110
Tao Bao1700cc42018-07-16 22:09:59 -0700111// 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.
114std::vector<char*> StringVectorToNullTerminatedArray(const std::vector<std::string>& args);