blob: d4e0ae902d5c8459d7f7614f32d54546de1be347 [file] [log] [blame]
Tom Cherry9027af02018-09-24 15:48:09 -07001/*
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 */
Daniel Zheng1a01a1c2023-01-30 23:29:50 +000028#pragma once
Tom Cherry9027af02018-09-24 15:48:09 -070029
Daniel Zheng0d307182023-01-31 21:07:53 +000030#include <string>
Daniel Zheng7df2ab92023-02-21 18:22:08 +000031#include "fastboot_driver.h"
Daniel Zhengc9e869b2023-03-30 14:59:48 -070032#include "fastboot_driver_interface.h"
Daniel Zheng65eb2462023-03-30 14:54:13 -070033#include "filesystem.h"
Daniel Zheng47d70a52023-02-14 17:44:40 +000034#include "super_flash_helper.h"
Daniel Zhengefca3332023-03-30 15:39:14 -070035#include "task.h"
Daniel Zheng7df2ab92023-02-21 18:22:08 +000036#include "util.h"
Daniel Zheng0d307182023-01-31 21:07:53 +000037
Tom Cherry9027af02018-09-24 15:48:09 -070038#include <bootimg.h>
39
Dmitrii Merkurevcdbfa7a2023-03-01 23:50:05 +000040#include "result.h"
41#include "socket.h"
42#include "util.h"
Daniel Zheng7f8fff02023-06-21 14:23:56 -070043#include "ziparchive/zip_archive.h"
Dmitrii Merkurevcdbfa7a2023-03-01 23:50:05 +000044
Tom Cherry9027af02018-09-24 15:48:09 -070045class FastBootTool {
46 public:
47 int Main(int argc, char* argv[]);
48
49 void ParseOsPatchLevel(boot_img_hdr_v1*, const char*);
50 void ParseOsVersion(boot_img_hdr_v1*, const char*);
Jaegeuk Kim638d05e2020-11-09 08:54:13 -080051 unsigned ParseFsOption(const char*);
Tom Cherry9027af02018-09-24 15:48:09 -070052};
Daniel Zheng0d307182023-01-31 21:07:53 +000053
Daniel Zheng65eb2462023-03-30 14:54:13 -070054enum fb_buffer_type {
55 FB_BUFFER_FD,
56 FB_BUFFER_SPARSE,
57};
58
59struct fastboot_buffer {
60 enum fb_buffer_type type;
61 std::vector<SparsePtr> files;
62 int64_t sz;
63 unique_fd fd;
64 int64_t image_size;
65};
66
Daniel Zheng7df2ab92023-02-21 18:22:08 +000067enum class ImageType {
68 // Must be flashed for device to boot into the kernel.
69 BootCritical,
70 // Normal partition to be flashed during "flashall".
71 Normal,
72 // Partition that is never flashed during "flashall".
73 Extra
74};
75
76struct Image {
77 std::string nickname;
78 std::string img_name;
79 std::string sig_name;
80 std::string part_name;
81 bool optional_if_no_image;
82 ImageType type;
83 bool IsSecondary() const { return nickname.empty(); }
84};
85
86using ImageEntry = std::pair<const Image*, std::string>;
87
88struct FlashingPlan {
Daniel Zheng15e77832023-03-13 17:09:43 +000089 unsigned fs_options = 0;
Daniel Zheng7df2ab92023-02-21 18:22:08 +000090 // If the image uses the default slot, or the user specified "all", then
91 // the paired string will be empty. If the image requests a specific slot
92 // (for example, system_other) it is specified instead.
93 ImageSource* source;
94 bool wants_wipe = false;
95 bool skip_reboot = false;
96 bool wants_set_active = false;
97 bool skip_secondary = false;
98 bool force_flash = false;
Daniel Zhengd62002c2023-06-21 14:27:35 -070099 bool should_optimize_flash_super = true;
Daniel Zheng5769b262023-06-21 14:41:11 -0700100 uint64_t sparse_limit = 0;
Daniel Zheng7df2ab92023-02-21 18:22:08 +0000101
Daniel Zheng1e6456b2023-04-10 09:35:48 -0700102 std::string slot_override;
Kelvin Zhang2a4a45f2023-04-03 20:44:05 +0000103 std::string current_slot;
Daniel Zheng7df2ab92023-02-21 18:22:08 +0000104 std::string secondary_slot;
Daniel Zheng29a211c2023-03-10 07:23:49 +0000105
Daniel Zhengbf09c612023-04-11 09:33:47 -0700106 fastboot::IFastBootDriver* fb;
Daniel Zheng7df2ab92023-02-21 18:22:08 +0000107};
108
Daniel Zheng65eb2462023-03-30 14:54:13 -0700109class FlashAllTool {
110 public:
111 FlashAllTool(FlashingPlan* fp);
112
113 void Flash();
114
115 private:
116 void CheckRequirements();
117 void DetermineSlot();
118 void CollectImages();
119 void FlashImages(const std::vector<std::pair<const Image*, std::string>>& images);
120 void FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf);
121 void HardcodedFlash();
122
123 std::vector<ImageEntry> boot_images_;
124 std::vector<ImageEntry> os_images_;
125 FlashingPlan* fp_;
126};
127
Daniel Zheng7f8fff02023-06-21 14:23:56 -0700128class ZipImageSource final : public ImageSource {
129 public:
130 explicit ZipImageSource(ZipArchiveHandle zip) : zip_(zip) {}
131 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
132 unique_fd OpenFile(const std::string& name) const override;
133
134 private:
135 ZipArchiveHandle zip_;
136};
137
138class LocalImageSource final : public ImageSource {
139 public:
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
Daniel Zheng0d307182023-01-31 21:07:53 +0000144bool should_flash_in_userspace(const std::string& partition_name);
145bool is_userspace_fastboot();
Daniel Zheng791a83b2023-05-23 10:42:39 -0700146void do_flash(const char* pname, const char* fname, const bool apply_vbmeta,
147 const FlashingPlan* fp);
Daniel Zheng0d307182023-01-31 21:07:53 +0000148void do_for_partitions(const std::string& part, const std::string& slot,
149 const std::function<void(const std::string&)>& func, bool force_slot);
150std::string find_item(const std::string& item);
151void reboot_to_userspace_fastboot();
152void syntax_error(const char* fmt, ...);
Daniel Zheng29a211c2023-03-10 07:23:49 +0000153std::string get_current_slot();
Dmitrii Merkurevcdbfa7a2023-03-01 23:50:05 +0000154
Daniel Zhengefca3332023-03-30 15:39:14 -0700155// Code for Parsing fastboot-info.txt
Daniel Zheng630f29e2023-04-28 14:06:45 -0700156bool CheckFastbootInfoRequirements(const std::vector<std::string>& command,
157 uint32_t host_tool_version);
Daniel Zhengefca3332023-03-30 15:39:14 -0700158std::unique_ptr<FlashTask> ParseFlashCommand(const FlashingPlan* fp,
159 const std::vector<std::string>& parts);
160std::unique_ptr<RebootTask> ParseRebootCommand(const FlashingPlan* fp,
161 const std::vector<std::string>& parts);
162std::unique_ptr<WipeTask> ParseWipeCommand(const FlashingPlan* fp,
163 const std::vector<std::string>& parts);
164std::unique_ptr<Task> ParseFastbootInfoLine(const FlashingPlan* fp,
165 const std::vector<std::string>& command);
Daniel Zheng997ce152023-05-30 09:24:46 -0700166bool AddResizeTasks(const FlashingPlan* fp, std::vector<std::unique_ptr<Task>>& tasks);
Daniel Zhengefca3332023-03-30 15:39:14 -0700167std::vector<std::unique_ptr<Task>> ParseFastbootInfo(const FlashingPlan* fp,
168 const std::vector<std::string>& file);
169
Dmitrii Merkurevcdbfa7a2023-03-01 23:50:05 +0000170struct NetworkSerial {
171 Socket::Protocol protocol;
172 std::string address;
173 int port;
174};
175
Daniel Zheng47d70a52023-02-14 17:44:40 +0000176Result<NetworkSerial, FastbootError> ParseNetworkSerial(const std::string& serial);
177bool supports_AB();
Daniel Zheng29a211c2023-03-10 07:23:49 +0000178std::string GetPartitionName(const ImageEntry& entry, const std::string& current_slot_);
Daniel Zheng47d70a52023-02-14 17:44:40 +0000179void flash_partition_files(const std::string& partition, const std::vector<SparsePtr>& files);
Daniel Zheng5769b262023-06-21 14:41:11 -0700180int64_t get_sparse_limit(int64_t size, const FlashingPlan* fp);
Daniel Zheng47d70a52023-02-14 17:44:40 +0000181std::vector<SparsePtr> resparse_file(sparse_file* s, int64_t max_size);
182
183bool is_retrofit_device();
184bool is_logical(const std::string& partition);
Daniel Zheng15e77832023-03-13 17:09:43 +0000185void fb_perform_format(const std::string& partition, int skip_if_not_supported,
186 const std::string& type_override, const std::string& size_override,
Daniel Zheng5769b262023-06-21 14:41:11 -0700187 const unsigned fs_options, const FlashingPlan* fp);