Android-cuttlefish cvd tool
Macros | Functions | Variables
fastboot.cpp File Reference
#include "fastboot.h"
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <inttypes.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <chrono>
#include <functional>
#include <iostream>
#include <memory>
#include <regex>
#include <string>
#include <thread>
#include <utility>
#include <vector>
#include <android-base/endian.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/macros.h>
#include <android-base/parseint.h>
#include <android-base/parsenetaddress.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
#include <build/version.h>
#include <libavb/libavb.h>
#include <liblp/liblp.h>
#include <liblp/super_layout_builder.h>
#include <platform_tools_version.h>
#include <sparse/sparse.h>
#include <zip.h>
#include "bootimg_utils.h"
#include "constants.h"
#include "diagnose_usb.h"
#include "fastboot_driver.h"
#include "fastboot_driver_interface.h"
#include "fs.h"
#include "storage.h"
#include "task.h"
#include "tcp.h"
#include "transport.h"
#include "udp.h"
#include "usb.h"
#include "util.h"
#include "vendor_boot_img_utils.h"

Macros

#define FASTBOOT_INFO_VERSION   1
 

Functions

char * get_android_product_out ()
 
static std::string find_item_given_name (const std::string &img_name)
 
std::string find_item (const std::string &item)
 
static void Status (const std::string &message)
 
static void Epilog (int status)
 
static void InfoMessage (const std::string &info)
 
static void TextMessage (const std::string &text)
 
bool ReadFileToVector (const std::string &file, std::vector< char > *out)
 
static int match_fastboot_with_serial (usb_ifc_info *info, const char *local_serial)
 
static ifc_match_func match_fastboot (const char *local_serial=serial)
 
static void PrintDevice (const char *local_serial, const char *status=nullptr, const char *details=nullptr)
 
static int list_devices_callback (usb_ifc_info *info)
 
Result< NetworkSerial, FastbootErrorParseNetworkSerial (const std::string &serial)
 
static std::unique_ptr< Transportopen_device (const char *local_serial, bool wait_for_device=true, bool announce=true)
 
static std::unique_ptr< TransportNetworkDeviceConnected (bool print=false)
 
static std::unique_ptr< Transportopen_device ()
 
static int Connect (int argc, char *argv[])
 
static int Disconnect (const char *local_serial)
 
static int Disconnect ()
 
static int Disconnect (int argc, char *argv[])
 
static void list_devices ()
 
void syntax_error (const char *fmt,...)
 
static int show_help ()
 
static std::vector< char > LoadBootableImage (const std::string &kernel, const std::string &ramdisk, const std::string &second_stage)
 
static zip_int64_t ExtractToBuffer (zip_file_t *zip_file, std::vector< char > &out)
 
static bool UnzipToMemory (unique_zip_t &zip, const char *entry_name, std::vector< char > *out)
 
static std::string make_temporary_template ()
 
static int make_temporary_fd (const char *what)
 
static unique_fd UnzipToFile (unique_zip_t &zip, const char *entry_name)
 
static bool CheckRequirement (const std::string &cur_product, const std::string &var, const std::string &product, bool invert, const std::vector< std::string > &options)
 
bool ParseRequirementLine (const std::string &line, std::string *name, std::string *product, bool *invert, std::vector< std::string > *options)
 
static void HandlePartitionExists (const std::vector< std::string > &options)
 
static void CheckRequirements (const std::string &data, bool force_flash)
 
static void DisplayVarOrError (const std::string &label, const std::string &var)
 
static void DumpInfo ()
 
std::vector< SparsePtrresparse_file (sparse_file *s, int64_t max_size)
 
static std::vector< SparsePtrload_sparse_files (int fd, int64_t max_size)
 
static uint64_t get_uint_var (const char *var_name, fastboot::IFastBootDriver *fb)
 
int64_t get_sparse_limit (int64_t size, const FlashingPlan *fp)
 
static bool load_buf_fd (unique_fd fd, struct fastboot_buffer *buf, const FlashingPlan *fp)
 
static bool load_buf (const char *fname, struct fastboot_buffer *buf, const FlashingPlan *fp)
 
static void rewrite_vbmeta_buffer (struct fastboot_buffer *buf, bool vbmeta_in_boot)
 
static bool has_vbmeta_partition ()
 
static bool is_vbmeta_partition (const std::string &partition)
 
bool is_logical (const std::string &partition)
 
static uint64_t get_partition_size (const std::string &partition)
 
static void copy_avb_footer (const ImageSource *source, const std::string &partition, struct fastboot_buffer *buf)
 
void flash_partition_files (const std::string &partition, const std::vector< SparsePtr > &files)
 
static void flash_buf (const ImageSource *source, const std::string &partition, struct fastboot_buffer *buf, const bool apply_vbmeta)
 
std::string get_current_slot ()
 
static int get_slot_count (fastboot::IFastBootDriver *fb)
 
bool supports_AB (fastboot::IFastBootDriver *fb)
 
static std::string get_other_slot (const std::string &current_slot, int count)
 
static std::string get_other_slot (const std::string &current_slot)
 
static std::string get_other_slot (int count)
 
static std::string get_other_slot ()
 
static std::string verify_slot (const std::string &slot_name, bool allow_all)
 
static std::string verify_slot (const std::string &slot)
 
static void do_for_partition (const std::string &part, const std::string &slot, const std::function< void(const std::string &)> &func, bool force_slot)
 
void do_for_partitions (const std::string &part, const std::string &slot, const std::function< void(const std::string &)> &func, bool force_slot)
 
static uint64_t fetch_partition (const std::string &partition, borrowed_fd fd, fastboot::IFastBootDriver *fb)
 
static void do_fetch (const std::string &partition, const std::string &slot_override, const std::string &outfile, fastboot::IFastBootDriver *fb)
 
static std::string repack_ramdisk (const char *pname, struct fastboot_buffer *buf, fastboot::IFastBootDriver *fb)
 
void do_flash (const char *pname, const char *fname, const bool apply_vbmeta, const FlashingPlan *fp)
 
static void set_active (const std::string &slot_override)
 
bool is_userspace_fastboot ()
 
void reboot_to_userspace_fastboot ()
 
static void CancelSnapshotIfNeeded ()
 
std::string GetPartitionName (const ImageEntry &entry, const std::string &current_slot)
 
std::unique_ptr< FlashTaskParseFlashCommand (const FlashingPlan *fp, const std::vector< std::string > &parts)
 
std::unique_ptr< RebootTaskParseRebootCommand (const FlashingPlan *fp, const std::vector< std::string > &parts)
 
std::unique_ptr< WipeTaskParseWipeCommand (const FlashingPlan *fp, const std::vector< std::string > &parts)
 
std::unique_ptr< TaskParseFastbootInfoLine (const FlashingPlan *fp, const std::vector< std::string > &command)
 
bool AddResizeTasks (const FlashingPlan *fp, std::vector< std::unique_ptr< Task > > *tasks)
 
static bool IsIgnore (const std::vector< std::string > &command)
 
bool CheckFastbootInfoRequirements (const std::vector< std::string > &command, uint32_t host_tool_version)
 
std::vector< std::unique_ptr< Task > > ParseFastbootInfo (const FlashingPlan *fp, const std::vector< std::string > &file)
 
static void do_update (const char *filename, FlashingPlan *fp)
 
static void do_flashall (FlashingPlan *fp)
 
static std::string next_arg (std::vector< std::string > *args)
 
static void do_oem_command (const std::string &cmd, std::vector< std::string > *args)
 
static unsigned fb_get_flash_block_size (std::string name)
 
void fb_perform_format (const std::string &partition, int skip_if_not_supported, const std::string &type_override, const std::string &size_override, const unsigned fs_options, const FlashingPlan *fp)
 
bool should_flash_in_userspace (const ImageSource *source, const std::string &partition_name)
 
static bool wipe_super (const android::fs_mgr::LpMetadata &metadata, const std::string &slot, std::string *message, const FlashingPlan *fp)
 
static void do_wipe_super (const std::string &image, const std::string &slot_override, const FlashingPlan *fp)
 
static void FastbootLogger (android::base::LogId, android::base::LogSeverity severity, const char *, const char *, unsigned int, const char *message)
 
static void FastbootAborter (const char *message)
 

Variables

static const char * serial = nullptr
 
static bool g_long_listing = false
 
static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024
 
static int64_t target_sparse_limit = -1
 
static unsigned g_base_addr = 0x10000000
 
static boot_img_hdr_v2 g_boot_img_hdr = {}
 
static std::string g_cmdline
 
static std::string g_dtb_path
 
static bool g_disable_verity = false
 
static bool g_disable_verification = false
 
fastboot::FastBootDriverfb = nullptr
 
static std::vector< Imageimages
 
double last_start_time
 

Macro Definition Documentation

◆ FASTBOOT_INFO_VERSION

#define FASTBOOT_INFO_VERSION   1

Function Documentation

◆ AddResizeTasks()

bool AddResizeTasks ( const FlashingPlan fp,
std::vector< std::unique_ptr< Task > > *  tasks 
)

◆ CancelSnapshotIfNeeded()

static void CancelSnapshotIfNeeded ( )
static

◆ CheckFastbootInfoRequirements()

bool CheckFastbootInfoRequirements ( const std::vector< std::string > &  command,
uint32_t  host_tool_version 
)

◆ CheckRequirement()

static bool CheckRequirement ( const std::string &  cur_product,
const std::string &  var,
const std::string &  product,
bool  invert,
const std::vector< std::string > &  options 
)
static

◆ CheckRequirements()

static void CheckRequirements ( const std::string &  data,
bool  force_flash 
)
static

◆ Connect()

static int Connect ( int  argc,
char *  argv[] 
)
static

◆ copy_avb_footer()

static void copy_avb_footer ( const ImageSource source,
const std::string &  partition,
struct fastboot_buffer buf 
)
static

◆ Disconnect() [1/3]

static int Disconnect ( )
static

◆ Disconnect() [2/3]

static int Disconnect ( const char *  local_serial)
static

◆ Disconnect() [3/3]

static int Disconnect ( int  argc,
char *  argv[] 
)
static

◆ DisplayVarOrError()

static void DisplayVarOrError ( const std::string &  label,
const std::string &  var 
)
static

◆ do_fetch()

static void do_fetch ( const std::string &  partition,
const std::string &  slot_override,
const std::string &  outfile,
fastboot::IFastBootDriver fb 
)
static

◆ do_flash()

void do_flash ( const char *  pname,
const char *  fname,
const bool  apply_vbmeta,
const FlashingPlan fp 
)

◆ do_flashall()

static void do_flashall ( FlashingPlan fp)
static

◆ do_for_partition()

static void do_for_partition ( const std::string &  part,
const std::string &  slot,
const std::function< void(const std::string &)> &  func,
bool  force_slot 
)
static

◆ do_for_partitions()

void do_for_partitions ( const std::string &  part,
const std::string &  slot,
const std::function< void(const std::string &)> &  func,
bool  force_slot 
)

◆ do_oem_command()

static void do_oem_command ( const std::string &  cmd,
std::vector< std::string > *  args 
)
static

◆ do_update()

static void do_update ( const char *  filename,
FlashingPlan fp 
)
static

◆ do_wipe_super()

static void do_wipe_super ( const std::string &  image,
const std::string &  slot_override,
const FlashingPlan fp 
)
static

◆ DumpInfo()

static void DumpInfo ( )
static

◆ Epilog()

static void Epilog ( int  status)
static

◆ ExtractToBuffer()

static zip_int64_t ExtractToBuffer ( zip_file_t *  zip_file,
std::vector< char > &  out 
)
static

◆ FastbootAborter()

static void FastbootAborter ( const char *  message)
static

◆ FastbootLogger()

static void FastbootLogger ( android::base::LogId  ,
android::base::LogSeverity  severity,
const char *  ,
const char *  ,
unsigned int  ,
const char *  message 
)
static

◆ fb_get_flash_block_size()

static unsigned fb_get_flash_block_size ( std::string  name)
static

◆ fb_perform_format()

void fb_perform_format ( const std::string &  partition,
int  skip_if_not_supported,
const std::string &  type_override,
const std::string &  size_override,
const unsigned  fs_options,
const FlashingPlan fp 
)

◆ fetch_partition()

static uint64_t fetch_partition ( const std::string &  partition,
borrowed_fd  fd,
fastboot::IFastBootDriver fb 
)
static

◆ find_item()

std::string find_item ( const std::string &  item)

◆ find_item_given_name()

static std::string find_item_given_name ( const std::string &  img_name)
static

◆ flash_buf()

static void flash_buf ( const ImageSource source,
const std::string &  partition,
struct fastboot_buffer buf,
const bool  apply_vbmeta 
)
static

◆ flash_partition_files()

void flash_partition_files ( const std::string &  partition,
const std::vector< SparsePtr > &  files 
)

◆ get_android_product_out()

char * get_android_product_out ( )

◆ get_current_slot()

std::string get_current_slot ( )

◆ get_other_slot() [1/4]

static std::string get_other_slot ( )
static

◆ get_other_slot() [2/4]

static std::string get_other_slot ( const std::string &  current_slot)
static

◆ get_other_slot() [3/4]

static std::string get_other_slot ( const std::string &  current_slot,
int  count 
)
static

◆ get_other_slot() [4/4]

static std::string get_other_slot ( int  count)
static

◆ get_partition_size()

static uint64_t get_partition_size ( const std::string &  partition)
static

◆ get_slot_count()

static int get_slot_count ( fastboot::IFastBootDriver fb)
static

◆ get_sparse_limit()

int64_t get_sparse_limit ( int64_t  size,
const FlashingPlan fp 
)

◆ get_uint_var()

static uint64_t get_uint_var ( const char *  var_name,
fastboot::IFastBootDriver fb 
)
static

◆ GetPartitionName()

std::string GetPartitionName ( const ImageEntry entry,
const std::string &  current_slot 
)

◆ HandlePartitionExists()

static void HandlePartitionExists ( const std::vector< std::string > &  options)
static

◆ has_vbmeta_partition()

static bool has_vbmeta_partition ( )
static

◆ InfoMessage()

static void InfoMessage ( const std::string &  info)
static

◆ is_logical()

bool is_logical ( const std::string &  partition)

◆ is_userspace_fastboot()

bool is_userspace_fastboot ( )

◆ is_vbmeta_partition()

static bool is_vbmeta_partition ( const std::string &  partition)
static

◆ IsIgnore()

static bool IsIgnore ( const std::vector< std::string > &  command)
static

◆ list_devices()

static void list_devices ( )
static

◆ list_devices_callback()

static int list_devices_callback ( usb_ifc_info info)
static

◆ load_buf()

static bool load_buf ( const char *  fname,
struct fastboot_buffer buf,
const FlashingPlan fp 
)
static

◆ load_buf_fd()

static bool load_buf_fd ( unique_fd  fd,
struct fastboot_buffer buf,
const FlashingPlan fp 
)
static

◆ load_sparse_files()

static std::vector< SparsePtr > load_sparse_files ( int  fd,
int64_t  max_size 
)
static

◆ LoadBootableImage()

static std::vector< char > LoadBootableImage ( const std::string &  kernel,
const std::string &  ramdisk,
const std::string &  second_stage 
)
static

◆ make_temporary_fd()

static int make_temporary_fd ( const char *  what)
static

◆ make_temporary_template()

static std::string make_temporary_template ( )
static

◆ match_fastboot()

static ifc_match_func match_fastboot ( const char *  local_serial = serial)
static

◆ match_fastboot_with_serial()

static int match_fastboot_with_serial ( usb_ifc_info info,
const char *  local_serial 
)
static

◆ NetworkDeviceConnected()

static std::unique_ptr< Transport > NetworkDeviceConnected ( bool  print = false)
static

◆ next_arg()

static std::string next_arg ( std::vector< std::string > *  args)
static

◆ open_device() [1/2]

static std::unique_ptr< Transport > open_device ( )
static

◆ open_device() [2/2]

static std::unique_ptr< Transport > open_device ( const char *  local_serial,
bool  wait_for_device = true,
bool  announce = true 
)
static

◆ ParseFastbootInfo()

std::vector< std::unique_ptr< Task > > ParseFastbootInfo ( const FlashingPlan fp,
const std::vector< std::string > &  file 
)

◆ ParseFastbootInfoLine()

std::unique_ptr< Task > ParseFastbootInfoLine ( const FlashingPlan fp,
const std::vector< std::string > &  command 
)

◆ ParseFlashCommand()

std::unique_ptr< FlashTask > ParseFlashCommand ( const FlashingPlan fp,
const std::vector< std::string > &  parts 
)

◆ ParseNetworkSerial()

Result< NetworkSerial, FastbootError > ParseNetworkSerial ( const std::string &  serial)

◆ ParseRebootCommand()

std::unique_ptr< RebootTask > ParseRebootCommand ( const FlashingPlan fp,
const std::vector< std::string > &  parts 
)

◆ ParseRequirementLine()

bool ParseRequirementLine ( const std::string &  line,
std::string *  name,
std::string *  product,
bool *  invert,
std::vector< std::string > *  options 
)

◆ ParseWipeCommand()

std::unique_ptr< WipeTask > ParseWipeCommand ( const FlashingPlan fp,
const std::vector< std::string > &  parts 
)

◆ PrintDevice()

static void PrintDevice ( const char *  local_serial,
const char *  status = nullptr,
const char *  details = nullptr 
)
static

◆ ReadFileToVector()

bool ReadFileToVector ( const std::string &  file,
std::vector< char > *  out 
)

◆ reboot_to_userspace_fastboot()

void reboot_to_userspace_fastboot ( )

◆ repack_ramdisk()

static std::string repack_ramdisk ( const char *  pname,
struct fastboot_buffer buf,
fastboot::IFastBootDriver fb 
)
static

◆ resparse_file()

std::vector< SparsePtr > resparse_file ( sparse_file *  s,
int64_t  max_size 
)

◆ rewrite_vbmeta_buffer()

static void rewrite_vbmeta_buffer ( struct fastboot_buffer buf,
bool  vbmeta_in_boot 
)
static

◆ set_active()

static void set_active ( const std::string &  slot_override)
static

◆ should_flash_in_userspace()

bool should_flash_in_userspace ( const ImageSource source,
const std::string &  partition_name 
)

◆ show_help()

static int show_help ( )
static

◆ Status()

static void Status ( const std::string &  message)
static

◆ supports_AB()

bool supports_AB ( fastboot::IFastBootDriver fb)

◆ syntax_error()

void syntax_error ( const char *  fmt,
  ... 
)

◆ TextMessage()

static void TextMessage ( const std::string &  text)
static

◆ UnzipToFile()

static unique_fd UnzipToFile ( unique_zip_t zip,
const char *  entry_name 
)
static

◆ UnzipToMemory()

static bool UnzipToMemory ( unique_zip_t zip,
const char *  entry_name,
std::vector< char > *  out 
)
static

◆ verify_slot() [1/2]

static std::string verify_slot ( const std::string &  slot)
static

◆ verify_slot() [2/2]

static std::string verify_slot ( const std::string &  slot_name,
bool  allow_all 
)
static

◆ wipe_super()

static bool wipe_super ( const android::fs_mgr::LpMetadata &  metadata,
const std::string &  slot,
std::string *  message,
const FlashingPlan fp 
)
static

Variable Documentation

◆ fb

fastboot::FastBootDriver* fb = nullptr

◆ g_base_addr

unsigned g_base_addr = 0x10000000
static

◆ g_boot_img_hdr

boot_img_hdr_v2 g_boot_img_hdr = {}
static

◆ g_cmdline

std::string g_cmdline
static

◆ g_disable_verification

bool g_disable_verification = false
static

◆ g_disable_verity

bool g_disable_verity = false
static

◆ g_dtb_path

std::string g_dtb_path
static

◆ g_long_listing

bool g_long_listing = false
static

◆ images

std::vector<Image> images
static

◆ last_start_time

double last_start_time

◆ RESPARSE_LIMIT

constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024
staticconstexpr

◆ serial

const char* serial = nullptr
static

◆ target_sparse_limit

int64_t target_sparse_limit = -1
static