Android-cuttlefish cvd tool
fastboot.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28#pragma once
29
30#include <functional>
31#include <memory>
32#include <string>
34#include "filesystem.h"
35#include "task.h"
36#include "util.h"
37
38#include <bootimg.h>
39
40#include "result.h"
41#include "socket.h"
42#include "util.h"
43#include <zip.h>
44
45using unique_zip_t = std::unique_ptr<zip_t, decltype(&zip_close)>;
46
48 public:
49 int Main(int argc, char* argv[]);
50
51 void ParseOsPatchLevel(boot_img_hdr_v1*, const char*);
52 void ParseOsVersion(boot_img_hdr_v1*, const char*);
53 unsigned ParseFsOption(const char*);
54};
55
59};
60
63 int64_t sz;
65 int64_t image_size;
66};
67
68enum class ImageType {
69 // Must be flashed for device to boot into the kernel.
71 // Normal partition to be flashed during "flashall".
72 Normal,
73 // Partition that is never flashed during "flashall".
74 Extra
75};
76
77struct Image {
78 std::string nickname;
79 std::string img_name;
80 std::string sig_name;
81 std::string part_name;
84 bool IsSecondary() const { return nickname.empty(); }
85};
86
87using ImageEntry = std::pair<const Image*, std::string>;
88
90 unsigned fs_options = 0;
91 // If the image uses the default slot, or the user specified "all", then
92 // the paired string will be empty. If the image requests a specific slot
93 // (for example, system_other) it is specified instead.
94 std::unique_ptr<ImageSource> source;
95 bool wants_wipe = false;
96 bool skip_reboot = false;
97 bool wants_set_active = false;
98 bool skip_secondary = false;
99 bool force_flash = false;
103 uint64_t sparse_limit = 0;
104
105 std::string slot_override;
106 std::string current_slot;
107 std::string secondary_slot;
108
110};
111
113 public:
115
116 void Flash();
117 std::vector<std::unique_ptr<Task>> CollectTasks();
118
119 private:
120 void DumpInfo();
121 void CheckRequirements();
122 void DetermineSlot();
123 void CollectImages();
124 void AddFlashTasks(const std::vector<std::pair<const Image*, std::string>>& images,
125 std::vector<std::unique_ptr<Task>>& tasks);
126
127 std::vector<std::unique_ptr<Task>> CollectTasksFromFastbootInfo();
128 std::vector<std::unique_ptr<Task>> CollectTasksFromImageList();
129
130 std::vector<ImageEntry> boot_images_;
131 std::vector<ImageEntry> os_images_;
132 std::vector<std::unique_ptr<Task>> tasks_;
133
135};
136
137class ZipImageSource final : public ImageSource {
138 public:
139 explicit ZipImageSource(unique_zip_t& zip) : zip_(zip) {}
140 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
141 unique_fd OpenFile(const std::string& name) const override;
142
143 private:
145};
146
147class LocalImageSource final : public ImageSource {
148 public:
149 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
150 unique_fd OpenFile(const std::string& name) const override;
151};
152
154bool should_flash_in_userspace(const ImageSource* source, const std::string& partition_name);
156void do_flash(const char* pname, const char* fname, const bool apply_vbmeta,
157 const FlashingPlan* fp);
158void do_for_partitions(fastboot::IFastBootDriver* fb, const std::string& part,
159 const std::string& slot, const std::function<void(const std::string&)>& func,
160 bool force_slot);
161std::string find_item(const std::string& item);
163void syntax_error(const char* fmt, ...);
165
166// Code for Parsing fastboot-info.txt
167bool CheckFastbootInfoRequirements(const std::vector<std::string>& command,
168 uint32_t host_tool_version);
169std::unique_ptr<FlashTask> ParseFlashCommand(const FlashingPlan* fp,
170 const std::vector<std::string>& parts);
171std::unique_ptr<RebootTask> ParseRebootCommand(const FlashingPlan* fp,
172 const std::vector<std::string>& parts);
173std::unique_ptr<WipeTask> ParseWipeCommand(const FlashingPlan* fp,
174 const std::vector<std::string>& parts);
175std::unique_ptr<Task> ParseFastbootInfoLine(const FlashingPlan* fp,
176 const std::vector<std::string>& command);
177bool AddResizeTasks(const FlashingPlan* fp, std::vector<std::unique_ptr<Task>>& tasks);
178std::vector<std::unique_ptr<Task>> ParseFastbootInfo(const FlashingPlan* fp,
179 const std::vector<std::string>& file);
180
183 std::string address;
184 int port;
185};
186
187Result<NetworkSerial, FastbootError> ParseNetworkSerial(const std::string& serial);
188std::string GetPartitionName(const ImageEntry& entry, const std::string& current_slot_);
189// Flash a single large sparse file to specified partition
190void flash_partition(const FlashingPlan* fp, const std::string& partition, SparsePtr sparse_file);
191// Flash a set of sparse files to specified partition, each sparse file should
192// not exceed sparse limit of the device
193void flash_partition_files(fastboot::IFastBootDriver* fb, const std::string& partition,
194 const std::vector<SparsePtr>& files);
195int64_t get_sparse_limit(int64_t size, const FlashingPlan* fp);
196
198bool is_logical(fastboot::IFastBootDriver* fb, const std::string& partition);
199void fb_perform_format(const std::string& partition, int skip_if_not_supported,
200 const std::string& type_override, const std::string& size_override,
201 const unsigned fs_options, const FlashingPlan* fp);
android::base::unique_fd unique_fd
Definition: adb_unique_fd.h:32
Definition: fastboot.h:47
void ParseOsVersion(boot_img_hdr_v1 *, const char *)
Definition: fastboot.cpp:2695
void ParseOsPatchLevel(boot_img_hdr_v1 *, const char *)
Definition: fastboot.cpp:2685
unsigned ParseFsOption(const char *)
Definition: fastboot.cpp:2708
int Main(int argc, char *argv[])
Definition: fastboot.cpp:2262
Definition: fastboot.h:112
void DumpInfo()
Definition: fastboot.cpp:1027
void AddFlashTasks(const std::vector< std::pair< const Image *, std::string > > &images, std::vector< std::unique_ptr< Task > > &tasks)
Definition: fastboot.cpp:1966
std::vector< std::unique_ptr< Task > > tasks_
Definition: fastboot.h:132
FlashAllTool(FlashingPlan *fp)
Definition: fastboot.cpp:1831
std::vector< std::unique_ptr< Task > > CollectTasks()
Definition: fastboot.cpp:1857
std::vector< std::unique_ptr< Task > > CollectTasksFromFastbootInfo()
Definition: fastboot.cpp:1954
std::vector< ImageEntry > os_images_
Definition: fastboot.h:131
void DetermineSlot()
Definition: fastboot.cpp:1889
std::vector< std::unique_ptr< Task > > CollectTasksFromImageList()
Definition: fastboot.cpp:1929
void CheckRequirements()
Definition: fastboot.cpp:1881
void Flash()
Definition: fastboot.cpp:1833
std::vector< ImageEntry > boot_images_
Definition: fastboot.h:130
FlashingPlan * fp_
Definition: fastboot.h:134
void CollectImages()
Definition: fastboot.cpp:1912
Definition: util.h:52
Definition: fastboot.h:147
unique_fd OpenFile(const std::string &name) const override
Definition: fastboot.cpp:2011
bool ReadFile(const std::string &name, std::vector< char > *out) const override
Definition: fastboot.cpp:2003
Protocol
Definition: socket.h:48
Definition: fastboot.h:137
ZipImageSource(unique_zip_t &zip)
Definition: fastboot.h:139
unique_zip_t & zip_
Definition: fastboot.h:144
bool ReadFile(const std::string &name, std::vector< char > *out) const override
Definition: fastboot.cpp:1982
unique_fd OpenFile(const std::string &name) const override
Definition: fastboot.cpp:1986
Definition: fastboot_driver_interface.h:38
ImageType
Definition: fastboot.h:68
std::pair< const Image *, std::string > ImageEntry
Definition: fastboot.h:87
std::unique_ptr< zip_t, decltype(&zip_close)> unique_zip_t
Definition: fastboot.h:45
void do_for_partitions(fastboot::IFastBootDriver *fb, const std::string &part, const std::string &slot, const std::function< void(const std::string &)> &func, bool force_slot)
Definition: fastboot.cpp:1443
std::string find_item(const std::string &item)
Definition: fastboot.cpp:194
std::unique_ptr< RebootTask > ParseRebootCommand(const FlashingPlan *fp, const std::vector< std::string > &parts)
Definition: fastboot.cpp:1678
std::unique_ptr< Task > ParseFastbootInfoLine(const FlashingPlan *fp, const std::vector< std::string > &command)
Definition: fastboot.cpp:1699
bool AddResizeTasks(const FlashingPlan *fp, std::vector< std::unique_ptr< Task > > &tasks)
void flash_partition_files(fastboot::IFastBootDriver *fb, const std::string &partition, const std::vector< SparsePtr > &files)
Definition: fastboot.cpp:1281
void do_flash(const char *pname, const char *fname, const bool apply_vbmeta, const FlashingPlan *fp)
Definition: fastboot.cpp:1550
bool supports_AB(fastboot::IFastBootDriver *fb)
Definition: fastboot.cpp:1343
std::vector< std::unique_ptr< Task > > ParseFastbootInfo(const FlashingPlan *fp, const std::vector< std::string > &file)
Definition: fastboot.cpp:1793
bool CheckFastbootInfoRequirements(const std::vector< std::string > &command, uint32_t host_tool_version)
Definition: fastboot.cpp:1763
bool is_logical(fastboot::IFastBootDriver *fb, const std::string &partition)
Definition: fastboot.cpp:1187
int64_t get_sparse_limit(int64_t size, const FlashingPlan *fp)
Definition: fastboot.cpp:1054
void fb_perform_format(const std::string &partition, int skip_if_not_supported, const std::string &type_override, const std::string &size_override, const unsigned fs_options, const FlashingPlan *fp)
Definition: fastboot.cpp:2060
fb_buffer_type
Definition: fastboot.h:56
@ FB_BUFFER_SPARSE
Definition: fastboot.h:58
@ FB_BUFFER_FD
Definition: fastboot.h:57
bool should_flash_in_userspace(const ImageSource *source, const std::string &partition_name)
Definition: fastboot.cpp:2149
void syntax_error(const char *fmt,...)
Definition: fastboot.cpp:537
void reboot_to_userspace_fastboot(fastboot::IFastBootDriver *fb)
Definition: fastboot.cpp:1601
char * get_android_product_out()
Definition: fastboot.cpp:178
Result< NetworkSerial, FastbootError > ParseNetworkSerial(const std::string &serial)
Definition: fastboot.cpp:317
std::unique_ptr< WipeTask > ParseWipeCommand(const FlashingPlan *fp, const std::vector< std::string > &parts)
Definition: fastboot.cpp:1689
std::string GetPartitionName(const ImageEntry &entry, const std::string &current_slot_)
Definition: fastboot.cpp:1632
bool is_userspace_fastboot(fastboot::IFastBootDriver *fb)
Definition: fastboot.cpp:1596
void flash_partition(const FlashingPlan *fp, const std::string &partition, SparsePtr sparse_file)
Definition: fastboot.cpp:1268
std::string get_current_slot(fastboot::IFastBootDriver *fb)
Definition: fastboot.cpp:1326
std::unique_ptr< FlashTask > ParseFlashCommand(const FlashingPlan *fp, const std::vector< std::string > &parts)
Definition: fastboot.cpp:1646
std::unique_ptr< sparse_file, decltype(&sparse_file_destroy)> SparsePtr
Definition: util.h:29
static const char * serial
Definition: fastboot.cpp:100
static std::vector< Image > images
Definition: fastboot.cpp:117
uint32_t size
Definition: io.h:2
Definition: fastboot.h:89
std::string current_slot
Definition: fastboot.h:106
bool exclude_dynamic_partitions
Definition: fastboot.h:102
bool skip_secondary
Definition: fastboot.h:98
unsigned fs_options
Definition: fastboot.h:90
bool skip_reboot
Definition: fastboot.h:96
std::string secondary_slot
Definition: fastboot.h:107
bool wants_set_active
Definition: fastboot.h:97
bool force_flash
Definition: fastboot.h:99
bool should_optimize_flash_super
Definition: fastboot.h:100
bool wants_wipe
Definition: fastboot.h:95
uint64_t sparse_limit
Definition: fastboot.h:103
bool should_use_fastboot_info
Definition: fastboot.h:101
fastboot::IFastBootDriver * fb
Definition: fastboot.h:109
std::string slot_override
Definition: fastboot.h:105
std::unique_ptr< ImageSource > source
Definition: fastboot.h:94
Definition: fastboot.h:77
bool IsSecondary() const
Definition: fastboot.h:84
std::string nickname
Definition: fastboot.h:78
std::string part_name
Definition: fastboot.h:81
ImageType type
Definition: fastboot.h:83
std::string sig_name
Definition: fastboot.h:80
bool optional_if_no_image
Definition: fastboot.h:82
std::string img_name
Definition: fastboot.h:79
Definition: fastboot.h:181
Socket::Protocol protocol
Definition: fastboot.h:182
int port
Definition: fastboot.h:184
std::string address
Definition: fastboot.h:183
Definition: fastboot.h:61
unique_fd fd
Definition: fastboot.h:64
fb_buffer_type file_type
Definition: fastboot.h:62
int64_t sz
Definition: fastboot.h:63
int64_t image_size
Definition: fastboot.h:65