• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
31 #include "fastboot_driver.h"
32 #include "fastboot_driver_interface.h"
33 #include "filesystem.h"
34 #include "super_flash_helper.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 
44 class FastBootTool {
45   public:
46     int Main(int argc, char* argv[]);
47 
48     void ParseOsPatchLevel(boot_img_hdr_v1*, const char*);
49     void ParseOsVersion(boot_img_hdr_v1*, const char*);
50     unsigned ParseFsOption(const char*);
51 };
52 
53 enum fb_buffer_type {
54     FB_BUFFER_FD,
55     FB_BUFFER_SPARSE,
56 };
57 
58 struct fastboot_buffer {
59     enum fb_buffer_type type;
60     std::vector<SparsePtr> files;
61     int64_t sz;
62     unique_fd fd;
63     int64_t image_size;
64 };
65 
66 enum class ImageType {
67     // Must be flashed for device to boot into the kernel.
68     BootCritical,
69     // Normal partition to be flashed during "flashall".
70     Normal,
71     // Partition that is never flashed during "flashall".
72     Extra
73 };
74 
75 struct Image {
76     std::string nickname;
77     std::string img_name;
78     std::string sig_name;
79     std::string part_name;
80     bool optional_if_no_image;
81     ImageType type;
IsSecondaryImage82     bool IsSecondary() const { return nickname.empty(); }
83 };
84 
85 using ImageEntry = std::pair<const Image*, std::string>;
86 
87 struct FlashingPlan {
88     unsigned fs_options = 0;
89     // If the image uses the default slot, or the user specified "all", then
90     // the paired string will be empty. If the image requests a specific slot
91     // (for example, system_other) it is specified instead.
92     ImageSource* source;
93     bool wants_wipe = false;
94     bool skip_reboot = false;
95     bool wants_set_active = false;
96     bool skip_secondary = false;
97     bool force_flash = false;
98 
99     std::string slot_override;
100     std::string current_slot;
101     std::string secondary_slot;
102 
103     fastboot::IFastBootDriver* fb;
104 };
105 
106 class FlashAllTool {
107   public:
108     FlashAllTool(FlashingPlan* fp);
109 
110     void Flash();
111 
112   private:
113     void CheckRequirements();
114     void DetermineSlot();
115     void CollectImages();
116     void FlashImages(const std::vector<std::pair<const Image*, std::string>>& images);
117     void FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf);
118     void HardcodedFlash();
119 
120     std::vector<ImageEntry> boot_images_;
121     std::vector<ImageEntry> os_images_;
122     FlashingPlan* fp_;
123 };
124 
125 bool should_flash_in_userspace(const std::string& partition_name);
126 bool is_userspace_fastboot();
127 void do_flash(const char* pname, const char* fname, const bool apply_vbmeta);
128 void do_for_partitions(const std::string& part, const std::string& slot,
129                        const std::function<void(const std::string&)>& func, bool force_slot);
130 std::string find_item(const std::string& item);
131 void reboot_to_userspace_fastboot();
132 void syntax_error(const char* fmt, ...);
133 std::string get_current_slot();
134 
135 // Code for Parsing fastboot-info.txt
136 bool CheckFastbootInfoRequirements(const std::vector<std::string>& command);
137 std::unique_ptr<FlashTask> ParseFlashCommand(const FlashingPlan* fp,
138                                              const std::vector<std::string>& parts);
139 std::unique_ptr<RebootTask> ParseRebootCommand(const FlashingPlan* fp,
140                                                const std::vector<std::string>& parts);
141 std::unique_ptr<WipeTask> ParseWipeCommand(const FlashingPlan* fp,
142                                            const std::vector<std::string>& parts);
143 std::unique_ptr<Task> ParseFastbootInfoLine(const FlashingPlan* fp,
144                                             const std::vector<std::string>& command);
145 void AddResizeTasks(const FlashingPlan* fp, std::vector<std::unique_ptr<Task>>& tasks);
146 std::vector<std::unique_ptr<Task>> ParseFastbootInfo(const FlashingPlan* fp,
147                                                      const std::vector<std::string>& file);
148 
149 struct NetworkSerial {
150     Socket::Protocol protocol;
151     std::string address;
152     int port;
153 };
154 
155 Result<NetworkSerial, FastbootError> ParseNetworkSerial(const std::string& serial);
156 bool supports_AB();
157 std::string GetPartitionName(const ImageEntry& entry, const std::string& current_slot_);
158 void flash_partition_files(const std::string& partition, const std::vector<SparsePtr>& files);
159 int64_t get_sparse_limit(int64_t size);
160 std::vector<SparsePtr> resparse_file(sparse_file* s, int64_t max_size);
161 
162 bool is_retrofit_device();
163 bool is_logical(const std::string& partition);
164 void fb_perform_format(const std::string& partition, int skip_if_not_supported,
165                        const std::string& type_override, const std::string& size_override,
166                        const unsigned fs_options);
167