Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 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 | */ |
Daniel Zheng | 1a01a1c | 2023-01-30 23:29:50 +0000 | [diff] [blame] | 28 | #pragma once |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 29 | |
Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 30 | #include <string> |
Daniel Zheng | 7df2ab9 | 2023-02-21 18:22:08 +0000 | [diff] [blame] | 31 | #include "fastboot_driver.h" |
Daniel Zheng | c9e869b | 2023-03-30 14:59:48 -0700 | [diff] [blame] | 32 | #include "fastboot_driver_interface.h" |
Daniel Zheng | 65eb246 | 2023-03-30 14:54:13 -0700 | [diff] [blame] | 33 | #include "filesystem.h" |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 34 | #include "super_flash_helper.h" |
Daniel Zheng | 7df2ab9 | 2023-02-21 18:22:08 +0000 | [diff] [blame] | 35 | #include "util.h" |
Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 36 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 37 | #include <bootimg.h> |
| 38 | |
Dmitrii Merkurev | cdbfa7a | 2023-03-01 23:50:05 +0000 | [diff] [blame] | 39 | #include "result.h" |
| 40 | #include "socket.h" |
| 41 | #include "util.h" |
| 42 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 43 | class FastBootTool { |
| 44 | public: |
| 45 | int Main(int argc, char* argv[]); |
| 46 | |
| 47 | void ParseOsPatchLevel(boot_img_hdr_v1*, const char*); |
| 48 | void ParseOsVersion(boot_img_hdr_v1*, const char*); |
Jaegeuk Kim | 638d05e | 2020-11-09 08:54:13 -0800 | [diff] [blame] | 49 | unsigned ParseFsOption(const char*); |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 50 | }; |
Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 51 | |
Daniel Zheng | 65eb246 | 2023-03-30 14:54:13 -0700 | [diff] [blame] | 52 | enum fb_buffer_type { |
| 53 | FB_BUFFER_FD, |
| 54 | FB_BUFFER_SPARSE, |
| 55 | }; |
| 56 | |
| 57 | struct fastboot_buffer { |
| 58 | enum fb_buffer_type type; |
| 59 | std::vector<SparsePtr> files; |
| 60 | int64_t sz; |
| 61 | unique_fd fd; |
| 62 | int64_t image_size; |
| 63 | }; |
| 64 | |
Daniel Zheng | 7df2ab9 | 2023-02-21 18:22:08 +0000 | [diff] [blame] | 65 | enum class ImageType { |
| 66 | // Must be flashed for device to boot into the kernel. |
| 67 | BootCritical, |
| 68 | // Normal partition to be flashed during "flashall". |
| 69 | Normal, |
| 70 | // Partition that is never flashed during "flashall". |
| 71 | Extra |
| 72 | }; |
| 73 | |
| 74 | struct Image { |
| 75 | std::string nickname; |
| 76 | std::string img_name; |
| 77 | std::string sig_name; |
| 78 | std::string part_name; |
| 79 | bool optional_if_no_image; |
| 80 | ImageType type; |
| 81 | bool IsSecondary() const { return nickname.empty(); } |
| 82 | }; |
| 83 | |
| 84 | using ImageEntry = std::pair<const Image*, std::string>; |
| 85 | |
| 86 | struct FlashingPlan { |
Daniel Zheng | 15e7783 | 2023-03-13 17:09:43 +0000 | [diff] [blame] | 87 | unsigned fs_options = 0; |
Daniel Zheng | 7df2ab9 | 2023-02-21 18:22:08 +0000 | [diff] [blame] | 88 | // If the image uses the default slot, or the user specified "all", then |
| 89 | // the paired string will be empty. If the image requests a specific slot |
| 90 | // (for example, system_other) it is specified instead. |
| 91 | ImageSource* source; |
| 92 | bool wants_wipe = false; |
| 93 | bool skip_reboot = false; |
| 94 | bool wants_set_active = false; |
| 95 | bool skip_secondary = false; |
| 96 | bool force_flash = false; |
| 97 | |
Daniel Zheng | 1e6456b | 2023-04-10 09:35:48 -0700 | [diff] [blame] | 98 | std::string slot_override; |
Kelvin Zhang | 2a4a45f | 2023-04-03 20:44:05 +0000 | [diff] [blame] | 99 | std::string current_slot; |
Daniel Zheng | 7df2ab9 | 2023-02-21 18:22:08 +0000 | [diff] [blame] | 100 | std::string secondary_slot; |
Daniel Zheng | 29a211c | 2023-03-10 07:23:49 +0000 | [diff] [blame] | 101 | |
Daniel Zheng | bf09c61 | 2023-04-11 09:33:47 -0700 | [diff] [blame^] | 102 | fastboot::IFastBootDriver* fb; |
Daniel Zheng | 7df2ab9 | 2023-02-21 18:22:08 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
Daniel Zheng | 65eb246 | 2023-03-30 14:54:13 -0700 | [diff] [blame] | 105 | class FlashAllTool { |
| 106 | public: |
| 107 | FlashAllTool(FlashingPlan* fp); |
| 108 | |
| 109 | void Flash(); |
| 110 | |
| 111 | private: |
| 112 | void CheckRequirements(); |
| 113 | void DetermineSlot(); |
| 114 | void CollectImages(); |
| 115 | void FlashImages(const std::vector<std::pair<const Image*, std::string>>& images); |
| 116 | void FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf); |
| 117 | void HardcodedFlash(); |
| 118 | |
| 119 | std::vector<ImageEntry> boot_images_; |
| 120 | std::vector<ImageEntry> os_images_; |
| 121 | FlashingPlan* fp_; |
| 122 | }; |
| 123 | |
Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 124 | bool should_flash_in_userspace(const std::string& partition_name); |
| 125 | bool is_userspace_fastboot(); |
Daniel Zheng | 403657d | 2023-03-10 07:37:25 +0000 | [diff] [blame] | 126 | void do_flash(const char* pname, const char* fname, const bool apply_vbmeta); |
Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 127 | void do_for_partitions(const std::string& part, const std::string& slot, |
| 128 | const std::function<void(const std::string&)>& func, bool force_slot); |
| 129 | std::string find_item(const std::string& item); |
| 130 | void reboot_to_userspace_fastboot(); |
| 131 | void syntax_error(const char* fmt, ...); |
Daniel Zheng | 29a211c | 2023-03-10 07:23:49 +0000 | [diff] [blame] | 132 | std::string get_current_slot(); |
Dmitrii Merkurev | cdbfa7a | 2023-03-01 23:50:05 +0000 | [diff] [blame] | 133 | |
| 134 | struct NetworkSerial { |
| 135 | Socket::Protocol protocol; |
| 136 | std::string address; |
| 137 | int port; |
| 138 | }; |
| 139 | |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 140 | Result<NetworkSerial, FastbootError> ParseNetworkSerial(const std::string& serial); |
| 141 | bool supports_AB(); |
Daniel Zheng | 29a211c | 2023-03-10 07:23:49 +0000 | [diff] [blame] | 142 | std::string GetPartitionName(const ImageEntry& entry, const std::string& current_slot_); |
Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 143 | void flash_partition_files(const std::string& partition, const std::vector<SparsePtr>& files); |
| 144 | int64_t get_sparse_limit(int64_t size); |
| 145 | std::vector<SparsePtr> resparse_file(sparse_file* s, int64_t max_size); |
| 146 | |
| 147 | bool is_retrofit_device(); |
| 148 | bool is_logical(const std::string& partition); |
Daniel Zheng | 15e7783 | 2023-03-13 17:09:43 +0000 | [diff] [blame] | 149 | void fb_perform_format(const std::string& partition, int skip_if_not_supported, |
| 150 | const std::string& type_override, const std::string& size_override, |
| 151 | const unsigned fs_options); |