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
64 std::vector<SparsePtr> files;
65 int64_t sz;
67 int64_t image_size;
68};
69
70enum class ImageType {
71 // Must be flashed for device to boot into the kernel.
73 // Normal partition to be flashed during "flashall".
74 Normal,
75 // Partition that is never flashed during "flashall".
76 Extra
77};
78
79struct Image {
80 std::string nickname;
81 std::string img_name;
82 std::string sig_name;
83 std::string part_name;
86 bool IsSecondary() const { return nickname.empty(); }
87};
88
89using ImageEntry = std::pair<const Image*, std::string>;
90
92 unsigned fs_options = 0;
93 // If the image uses the default slot, or the user specified "all", then
94 // the paired string will be empty. If the image requests a specific slot
95 // (for example, system_other) it is specified instead.
96 std::unique_ptr<ImageSource> source;
97 bool wants_wipe = false;
98 bool skip_reboot = false;
99 bool wants_set_active = false;
100 bool skip_secondary = false;
101 bool force_flash = false;
105 uint64_t sparse_limit = 0;
106
107 std::string slot_override;
108 std::string current_slot;
109 std::string secondary_slot;
110
112};
113
115 public:
117
118 void Flash();
119 std::vector<std::unique_ptr<Task>> CollectTasks();
120
121 private:
122 void CheckRequirements();
123 void DetermineSlot();
124 void CollectImages();
125 void AddFlashTasks(const std::vector<std::pair<const Image*, std::string>>& images,
126 std::vector<std::unique_ptr<Task>>& tasks);
127
128 std::vector<std::unique_ptr<Task>> CollectTasksFromFastbootInfo();
129 std::vector<std::unique_ptr<Task>> CollectTasksFromImageList();
130
131 std::vector<ImageEntry> boot_images_;
132 std::vector<ImageEntry> os_images_;
133 std::vector<std::unique_ptr<Task>> tasks_;
134
136};
137
138class ZipImageSource final : public ImageSource {
139 public:
140 explicit ZipImageSource(unique_zip_t& zip) : zip_(zip) {}
141 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
142 unique_fd OpenFile(const std::string& name) const override;
143
144 private:
146};
147
148class LocalImageSource final : public ImageSource {
149 public:
150 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
151 unique_fd OpenFile(const std::string& name) const override;
152};
153
155bool should_flash_in_userspace(const ImageSource* source, const std::string& partition_name);
157void do_flash(const char* pname, const char* fname, const bool apply_vbmeta,
158 const FlashingPlan* fp);
159void do_for_partitions(const std::string& part, const std::string& slot,
160 const std::function<void(const std::string&)>& func, bool force_slot);
161std::string find_item(const std::string& item);
163void syntax_error(const char* fmt, ...);
164std::string get_current_slot();
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_);
189void flash_partition_files(const std::string& partition, const std::vector<SparsePtr>& files);
190int64_t get_sparse_limit(int64_t size, const FlashingPlan* fp);
191std::vector<SparsePtr> resparse_file(sparse_file* s, int64_t max_size);
192
194bool is_logical(const std::string& partition);
195void fb_perform_format(const std::string& partition, int skip_if_not_supported,
196 const std::string& type_override, const std::string& size_override,
197 const unsigned fs_options, const FlashingPlan* fp);
Definition: fastboot.h:47
void ParseOsVersion(boot_img_hdr_v1 *, const char *)
Definition: fastboot.cpp:2707
void ParseOsPatchLevel(boot_img_hdr_v1 *, const char *)
Definition: fastboot.cpp:2697
unsigned ParseFsOption(const char *)
Definition: fastboot.cpp:2720
int Main(int argc, char *argv[])
Definition: fastboot.cpp:2275
Definition: fastboot.h:114
void AddFlashTasks(const std::vector< std::pair< const Image *, std::string > > &images, std::vector< std::unique_ptr< Task > > &tasks)
Definition: fastboot.cpp:1980
std::vector< std::unique_ptr< Task > > tasks_
Definition: fastboot.h:133
FlashAllTool(FlashingPlan *fp)
Definition: fastboot.cpp:1845
std::vector< std::unique_ptr< Task > > CollectTasks()
Definition: fastboot.cpp:1871
std::vector< std::unique_ptr< Task > > CollectTasksFromFastbootInfo()
Definition: fastboot.cpp:1968
std::vector< ImageEntry > os_images_
Definition: fastboot.h:132
void DetermineSlot()
Definition: fastboot.cpp:1903
std::vector< std::unique_ptr< Task > > CollectTasksFromImageList()
Definition: fastboot.cpp:1943
void CheckRequirements()
Definition: fastboot.cpp:1895
void Flash()
Definition: fastboot.cpp:1847
std::vector< ImageEntry > boot_images_
Definition: fastboot.h:131
FlashingPlan * fp_
Definition: fastboot.h:135
void CollectImages()
Definition: fastboot.cpp:1926
Definition: util.h:51
Definition: fastboot.h:148
unique_fd OpenFile(const std::string &name) const override
Definition: fastboot.cpp:2025
bool ReadFile(const std::string &name, std::vector< char > *out) const override
Definition: fastboot.cpp:2017
Protocol
Definition: socket.h:48
Definition: fastboot.h:138
ZipImageSource(unique_zip_t &zip)
Definition: fastboot.h:140
unique_zip_t & zip_
Definition: fastboot.h:145
bool ReadFile(const std::string &name, std::vector< char > *out) const override
Definition: fastboot.cpp:1996
unique_fd OpenFile(const std::string &name) const override
Definition: fastboot.cpp:2000
Definition: unique_fd.h:61
Definition: fastboot_driver_interface.h:35
ImageType
Definition: fastboot.h:70
std::pair< const Image *, std::string > ImageEntry
Definition: fastboot.h:89
std::unique_ptr< zip_t, decltype(&zip_close)> unique_zip_t
Definition: fastboot.h:45
bool is_userspace_fastboot()
Definition: fastboot.cpp:1610
bool is_logical(const std::string &partition)
Definition: fastboot.cpp:1217
std::string find_item(const std::string &item)
Definition: fastboot.cpp:192
void reboot_to_userspace_fastboot()
Definition: fastboot.cpp:1615
std::unique_ptr< RebootTask > ParseRebootCommand(const FlashingPlan *fp, const std::vector< std::string > &parts)
Definition: fastboot.cpp:1692
void flash_partition_files(const std::string &partition, const std::vector< SparsePtr > &files)
Definition: fastboot.cpp:1300
std::unique_ptr< Task > ParseFastbootInfoLine(const FlashingPlan *fp, const std::vector< std::string > &command)
Definition: fastboot.cpp:1713
bool AddResizeTasks(const FlashingPlan *fp, std::vector< std::unique_ptr< Task > > &tasks)
void do_flash(const char *pname, const char *fname, const bool apply_vbmeta, const FlashingPlan *fp)
Definition: fastboot.cpp:1564
bool supports_AB(fastboot::IFastBootDriver *fb)
Definition: fastboot.cpp:1357
std::vector< std::unique_ptr< Task > > ParseFastbootInfo(const FlashingPlan *fp, const std::vector< std::string > &file)
Definition: fastboot.cpp:1807
bool CheckFastbootInfoRequirements(const std::vector< std::string > &command, uint32_t host_tool_version)
Definition: fastboot.cpp:1777
int64_t get_sparse_limit(int64_t size, const FlashingPlan *fp)
Definition: fastboot.cpp:1071
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:2073
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:2162
void do_for_partitions(const std::string &part, const std::string &slot, const std::function< void(const std::string &)> &func, bool force_slot)
Definition: fastboot.cpp:1457
std::string get_current_slot()
Definition: fastboot.cpp:1340
void syntax_error(const char *fmt,...)
Definition: fastboot.cpp:529
char * get_android_product_out()
Definition: fastboot.cpp:176
Result< NetworkSerial, FastbootError > ParseNetworkSerial(const std::string &serial)
Definition: fastboot.cpp:308
std::unique_ptr< WipeTask > ParseWipeCommand(const FlashingPlan *fp, const std::vector< std::string > &parts)
Definition: fastboot.cpp:1703
std::string GetPartitionName(const ImageEntry &entry, const std::string &current_slot_)
Definition: fastboot.cpp:1646
std::vector< SparsePtr > resparse_file(sparse_file *s, int64_t max_size)
Definition: fastboot.cpp:1026
std::unique_ptr< FlashTask > ParseFlashCommand(const FlashingPlan *fp, const std::vector< std::string > &parts)
Definition: fastboot.cpp:1660
static const char * serial
Definition: fastboot.cpp:96
static std::vector< Image > images
Definition: fastboot.cpp:115
fastboot::FastBootDriver * fb
Definition: fastboot.cpp:113
uint32_t size
Definition: io.h:2
Definition: result.h:31
Definition: fastboot.h:91
std::string current_slot
Definition: fastboot.h:108
bool exclude_dynamic_partitions
Definition: fastboot.h:104
bool skip_secondary
Definition: fastboot.h:100
unsigned fs_options
Definition: fastboot.h:92
bool skip_reboot
Definition: fastboot.h:98
std::string secondary_slot
Definition: fastboot.h:109
bool wants_set_active
Definition: fastboot.h:99
bool force_flash
Definition: fastboot.h:101
bool should_optimize_flash_super
Definition: fastboot.h:102
bool wants_wipe
Definition: fastboot.h:97
uint64_t sparse_limit
Definition: fastboot.h:105
bool should_use_fastboot_info
Definition: fastboot.h:103
fastboot::IFastBootDriver * fb
Definition: fastboot.h:111
std::string slot_override
Definition: fastboot.h:107
std::unique_ptr< ImageSource > source
Definition: fastboot.h:96
Definition: fastboot.h:79
bool IsSecondary() const
Definition: fastboot.h:86
std::string nickname
Definition: fastboot.h:80
std::string part_name
Definition: fastboot.h:83
ImageType type
Definition: fastboot.h:85
std::string sig_name
Definition: fastboot.h:82
bool optional_if_no_image
Definition: fastboot.h:84
std::string img_name
Definition: fastboot.h:81
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:66
std::vector< SparsePtr > files
Definition: fastboot.h:64
fb_buffer_type file_type
Definition: fastboot.h:63
int64_t sz
Definition: fastboot.h:65
int64_t image_size
Definition: fastboot.h:67
fb_buffer_type type
Definition: fastboot.h:62