2009-03-04 04:32:55 +01:00
|
|
|
/*
|
2018-09-25 00:48:09 +02:00
|
|
|
* Copyright (C) 2018 The Android Open Source Project
|
2009-03-04 04:32:55 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
2012-02-28 16:21:08 +01:00
|
|
|
* the documentation and/or other materials provided with the
|
2009-03-04 04:32:55 +01:00
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
2012-02-28 16:21:08 +01:00
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
2009-03-04 04:32:55 +01:00
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
2023-01-31 00:29:50 +01:00
|
|
|
#pragma once
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2023-07-28 19:58:56 +02:00
|
|
|
#include <functional>
|
2023-10-03 02:02:05 +02:00
|
|
|
#include <memory>
|
2023-01-31 22:07:53 +01:00
|
|
|
#include <string>
|
2023-03-30 23:59:48 +02:00
|
|
|
#include "fastboot_driver_interface.h"
|
2023-03-30 23:54:13 +02:00
|
|
|
#include "filesystem.h"
|
2023-03-31 00:39:14 +02:00
|
|
|
#include "task.h"
|
2023-02-21 19:22:08 +01:00
|
|
|
#include "util.h"
|
2023-01-31 22:07:53 +01:00
|
|
|
|
2018-04-10 23:22:13 +02:00
|
|
|
#include <bootimg.h>
|
2011-04-05 02:54:59 +02:00
|
|
|
|
2023-03-02 00:50:05 +01:00
|
|
|
#include "result.h"
|
|
|
|
#include "socket.h"
|
|
|
|
#include "util.h"
|
2023-06-21 23:23:56 +02:00
|
|
|
#include "ziparchive/zip_archive.h"
|
2023-03-02 00:50:05 +01:00
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
class FastBootTool {
|
2018-04-09 22:58:41 +02:00
|
|
|
public:
|
|
|
|
int Main(int argc, char* argv[]);
|
|
|
|
|
2018-04-10 23:22:13 +02:00
|
|
|
void ParseOsPatchLevel(boot_img_hdr_v1*, const char*);
|
|
|
|
void ParseOsVersion(boot_img_hdr_v1*, const char*);
|
2020-11-09 17:54:13 +01:00
|
|
|
unsigned ParseFsOption(const char*);
|
2018-04-10 23:22:13 +02:00
|
|
|
};
|
2023-01-31 22:07:53 +01:00
|
|
|
|
2023-03-30 23:54:13 +02:00
|
|
|
enum fb_buffer_type {
|
|
|
|
FB_BUFFER_FD,
|
|
|
|
FB_BUFFER_SPARSE,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct fastboot_buffer {
|
2023-10-20 20:17:44 +02:00
|
|
|
fb_buffer_type type;
|
|
|
|
fb_buffer_type file_type;
|
2023-03-30 23:54:13 +02:00
|
|
|
std::vector<SparsePtr> files;
|
|
|
|
int64_t sz;
|
|
|
|
unique_fd fd;
|
|
|
|
int64_t image_size;
|
|
|
|
};
|
|
|
|
|
2023-02-21 19:22:08 +01:00
|
|
|
enum class ImageType {
|
|
|
|
// Must be flashed for device to boot into the kernel.
|
|
|
|
BootCritical,
|
|
|
|
// Normal partition to be flashed during "flashall".
|
|
|
|
Normal,
|
|
|
|
// Partition that is never flashed during "flashall".
|
|
|
|
Extra
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Image {
|
|
|
|
std::string nickname;
|
|
|
|
std::string img_name;
|
|
|
|
std::string sig_name;
|
|
|
|
std::string part_name;
|
|
|
|
bool optional_if_no_image;
|
|
|
|
ImageType type;
|
|
|
|
bool IsSecondary() const { return nickname.empty(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
using ImageEntry = std::pair<const Image*, std::string>;
|
|
|
|
|
|
|
|
struct FlashingPlan {
|
2023-03-13 18:09:43 +01:00
|
|
|
unsigned fs_options = 0;
|
2023-02-21 19:22:08 +01:00
|
|
|
// If the image uses the default slot, or the user specified "all", then
|
|
|
|
// the paired string will be empty. If the image requests a specific slot
|
|
|
|
// (for example, system_other) it is specified instead.
|
2023-10-03 02:02:05 +02:00
|
|
|
std::unique_ptr<ImageSource> source;
|
2023-02-21 19:22:08 +01:00
|
|
|
bool wants_wipe = false;
|
|
|
|
bool skip_reboot = false;
|
|
|
|
bool wants_set_active = false;
|
|
|
|
bool skip_secondary = false;
|
|
|
|
bool force_flash = false;
|
2023-06-21 23:27:35 +02:00
|
|
|
bool should_optimize_flash_super = true;
|
2023-07-05 19:23:37 +02:00
|
|
|
bool should_use_fastboot_info = true;
|
2023-07-19 00:04:43 +02:00
|
|
|
bool exclude_dynamic_partitions = false;
|
2023-06-21 23:41:11 +02:00
|
|
|
uint64_t sparse_limit = 0;
|
2023-02-21 19:22:08 +01:00
|
|
|
|
2023-04-10 18:35:48 +02:00
|
|
|
std::string slot_override;
|
2023-04-03 22:44:05 +02:00
|
|
|
std::string current_slot;
|
2023-02-21 19:22:08 +01:00
|
|
|
std::string secondary_slot;
|
2023-03-10 08:23:49 +01:00
|
|
|
|
2023-04-11 18:33:47 +02:00
|
|
|
fastboot::IFastBootDriver* fb;
|
2023-02-21 19:22:08 +01:00
|
|
|
};
|
|
|
|
|
2023-03-30 23:54:13 +02:00
|
|
|
class FlashAllTool {
|
|
|
|
public:
|
|
|
|
FlashAllTool(FlashingPlan* fp);
|
|
|
|
|
|
|
|
void Flash();
|
2023-06-21 23:49:52 +02:00
|
|
|
std::vector<std::unique_ptr<Task>> CollectTasks();
|
2023-03-30 23:54:13 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
void CheckRequirements();
|
|
|
|
void DetermineSlot();
|
|
|
|
void CollectImages();
|
2023-06-21 23:14:19 +02:00
|
|
|
void AddFlashTasks(const std::vector<std::pair<const Image*, std::string>>& images,
|
|
|
|
std::vector<std::unique_ptr<Task>>& tasks);
|
2023-06-21 23:49:52 +02:00
|
|
|
|
|
|
|
std::vector<std::unique_ptr<Task>> CollectTasksFromFastbootInfo();
|
2023-06-21 23:14:19 +02:00
|
|
|
std::vector<std::unique_ptr<Task>> CollectTasksFromImageList();
|
2023-03-30 23:54:13 +02:00
|
|
|
|
|
|
|
std::vector<ImageEntry> boot_images_;
|
|
|
|
std::vector<ImageEntry> os_images_;
|
2023-06-21 23:14:19 +02:00
|
|
|
std::vector<std::unique_ptr<Task>> tasks_;
|
|
|
|
|
2023-03-30 23:54:13 +02:00
|
|
|
FlashingPlan* fp_;
|
|
|
|
};
|
|
|
|
|
2023-06-21 23:23:56 +02:00
|
|
|
class ZipImageSource final : public ImageSource {
|
|
|
|
public:
|
|
|
|
explicit ZipImageSource(ZipArchiveHandle zip) : zip_(zip) {}
|
|
|
|
bool ReadFile(const std::string& name, std::vector<char>* out) const override;
|
|
|
|
unique_fd OpenFile(const std::string& name) const override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
ZipArchiveHandle zip_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class LocalImageSource final : public ImageSource {
|
|
|
|
public:
|
|
|
|
bool ReadFile(const std::string& name, std::vector<char>* out) const override;
|
|
|
|
unique_fd OpenFile(const std::string& name) const override;
|
|
|
|
};
|
|
|
|
|
2023-06-21 23:49:52 +02:00
|
|
|
char* get_android_product_out();
|
2023-09-22 05:37:47 +02:00
|
|
|
bool should_flash_in_userspace(const ImageSource* source, const std::string& partition_name);
|
2023-01-31 22:07:53 +01:00
|
|
|
bool is_userspace_fastboot();
|
2023-05-23 19:42:39 +02:00
|
|
|
void do_flash(const char* pname, const char* fname, const bool apply_vbmeta,
|
|
|
|
const FlashingPlan* fp);
|
2023-01-31 22:07:53 +01:00
|
|
|
void do_for_partitions(const std::string& part, const std::string& slot,
|
|
|
|
const std::function<void(const std::string&)>& func, bool force_slot);
|
|
|
|
std::string find_item(const std::string& item);
|
|
|
|
void reboot_to_userspace_fastboot();
|
|
|
|
void syntax_error(const char* fmt, ...);
|
2023-03-10 08:23:49 +01:00
|
|
|
std::string get_current_slot();
|
2023-03-02 00:50:05 +01:00
|
|
|
|
2023-03-31 00:39:14 +02:00
|
|
|
// Code for Parsing fastboot-info.txt
|
2023-04-28 23:06:45 +02:00
|
|
|
bool CheckFastbootInfoRequirements(const std::vector<std::string>& command,
|
|
|
|
uint32_t host_tool_version);
|
2023-03-31 00:39:14 +02:00
|
|
|
std::unique_ptr<FlashTask> ParseFlashCommand(const FlashingPlan* fp,
|
|
|
|
const std::vector<std::string>& parts);
|
|
|
|
std::unique_ptr<RebootTask> ParseRebootCommand(const FlashingPlan* fp,
|
|
|
|
const std::vector<std::string>& parts);
|
|
|
|
std::unique_ptr<WipeTask> ParseWipeCommand(const FlashingPlan* fp,
|
|
|
|
const std::vector<std::string>& parts);
|
|
|
|
std::unique_ptr<Task> ParseFastbootInfoLine(const FlashingPlan* fp,
|
|
|
|
const std::vector<std::string>& command);
|
2023-05-30 18:24:46 +02:00
|
|
|
bool AddResizeTasks(const FlashingPlan* fp, std::vector<std::unique_ptr<Task>>& tasks);
|
2023-03-31 00:39:14 +02:00
|
|
|
std::vector<std::unique_ptr<Task>> ParseFastbootInfo(const FlashingPlan* fp,
|
|
|
|
const std::vector<std::string>& file);
|
|
|
|
|
2023-03-02 00:50:05 +01:00
|
|
|
struct NetworkSerial {
|
|
|
|
Socket::Protocol protocol;
|
|
|
|
std::string address;
|
|
|
|
int port;
|
|
|
|
};
|
|
|
|
|
2023-02-14 18:44:40 +01:00
|
|
|
Result<NetworkSerial, FastbootError> ParseNetworkSerial(const std::string& serial);
|
2023-03-10 08:23:49 +01:00
|
|
|
std::string GetPartitionName(const ImageEntry& entry, const std::string& current_slot_);
|
2023-02-14 18:44:40 +01:00
|
|
|
void flash_partition_files(const std::string& partition, const std::vector<SparsePtr>& files);
|
2023-06-21 23:41:11 +02:00
|
|
|
int64_t get_sparse_limit(int64_t size, const FlashingPlan* fp);
|
2023-02-14 18:44:40 +01:00
|
|
|
std::vector<SparsePtr> resparse_file(sparse_file* s, int64_t max_size);
|
|
|
|
|
2023-08-24 18:47:43 +02:00
|
|
|
bool supports_AB(fastboot::IFastBootDriver* fb);
|
2023-02-14 18:44:40 +01:00
|
|
|
bool is_logical(const std::string& partition);
|
2023-03-13 18:09:43 +01:00
|
|
|
void fb_perform_format(const std::string& partition, int skip_if_not_supported,
|
|
|
|
const std::string& type_override, const std::string& size_override,
|
2023-06-21 23:41:11 +02:00
|
|
|
const unsigned fs_options, const FlashingPlan* fp);
|