blob: c9544875b61a61863ca569cf7763f7082b88d3a9 [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 Zheng47d70a52023-02-14 17:44:40 +000032#include "super_flash_helper.h"
Daniel Zheng7df2ab92023-02-21 18:22:08 +000033#include "util.h"
Daniel Zheng0d307182023-01-31 21:07:53 +000034
Tom Cherry9027af02018-09-24 15:48:09 -070035#include <bootimg.h>
36
Dmitrii Merkurevcdbfa7a2023-03-01 23:50:05 +000037#include "result.h"
38#include "socket.h"
39#include "util.h"
40
Tom Cherry9027af02018-09-24 15:48:09 -070041class FastBootTool {
42 public:
43 int Main(int argc, char* argv[]);
44
45 void ParseOsPatchLevel(boot_img_hdr_v1*, const char*);
46 void ParseOsVersion(boot_img_hdr_v1*, const char*);
Jaegeuk Kim638d05e2020-11-09 08:54:13 -080047 unsigned ParseFsOption(const char*);
Tom Cherry9027af02018-09-24 15:48:09 -070048};
Daniel Zheng0d307182023-01-31 21:07:53 +000049
Daniel Zheng7df2ab92023-02-21 18:22:08 +000050enum class ImageType {
51 // Must be flashed for device to boot into the kernel.
52 BootCritical,
53 // Normal partition to be flashed during "flashall".
54 Normal,
55 // Partition that is never flashed during "flashall".
56 Extra
57};
58
59struct Image {
60 std::string nickname;
61 std::string img_name;
62 std::string sig_name;
63 std::string part_name;
64 bool optional_if_no_image;
65 ImageType type;
66 bool IsSecondary() const { return nickname.empty(); }
67};
68
69using ImageEntry = std::pair<const Image*, std::string>;
70
71struct FlashingPlan {
Daniel Zheng15e77832023-03-13 17:09:43 +000072 unsigned fs_options = 0;
Daniel Zheng7df2ab92023-02-21 18:22:08 +000073 // If the image uses the default slot, or the user specified "all", then
74 // the paired string will be empty. If the image requests a specific slot
75 // (for example, system_other) it is specified instead.
76 ImageSource* source;
77 bool wants_wipe = false;
78 bool skip_reboot = false;
79 bool wants_set_active = false;
80 bool skip_secondary = false;
81 bool force_flash = false;
82
Kelvin Zhang2a4a45f2023-04-03 20:44:05 +000083 std::string slot;
84 std::string current_slot;
Daniel Zheng7df2ab92023-02-21 18:22:08 +000085 std::string secondary_slot;
86 fastboot::FastBootDriver* fb;
Daniel Zheng7df2ab92023-02-21 18:22:08 +000087};
88
Daniel Zheng0d307182023-01-31 21:07:53 +000089bool should_flash_in_userspace(const std::string& partition_name);
90bool is_userspace_fastboot();
Daniel Zheng403657d2023-03-10 07:37:25 +000091void do_flash(const char* pname, const char* fname, const bool apply_vbmeta);
Daniel Zheng0d307182023-01-31 21:07:53 +000092void do_for_partitions(const std::string& part, const std::string& slot,
93 const std::function<void(const std::string&)>& func, bool force_slot);
94std::string find_item(const std::string& item);
95void reboot_to_userspace_fastboot();
96void syntax_error(const char* fmt, ...);
Dmitrii Merkurevcdbfa7a2023-03-01 23:50:05 +000097
98struct NetworkSerial {
99 Socket::Protocol protocol;
100 std::string address;
101 int port;
102};
103
Daniel Zheng47d70a52023-02-14 17:44:40 +0000104Result<NetworkSerial, FastbootError> ParseNetworkSerial(const std::string& serial);
105bool supports_AB();
Kelvin Zhang2a4a45f2023-04-03 20:44:05 +0000106std::string GetPartitionName(const ImageEntry& entry, std::string& current_slot_);
Daniel Zheng47d70a52023-02-14 17:44:40 +0000107void flash_partition_files(const std::string& partition, const std::vector<SparsePtr>& files);
108int64_t get_sparse_limit(int64_t size);
109std::vector<SparsePtr> resparse_file(sparse_file* s, int64_t max_size);
110
111bool is_retrofit_device();
112bool is_logical(const std::string& partition);
Daniel Zheng15e77832023-03-13 17:09:43 +0000113void fb_perform_format(const std::string& partition, int skip_if_not_supported,
114 const std::string& type_override, const std::string& size_override,
115 const unsigned fs_options);