2009-03-04 04:32:55 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
* 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
|
2011-02-26 03:38:53 +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
|
2011-02-26 03:38:53 +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.
|
|
|
|
*/
|
|
|
|
|
2018-09-25 00:48:09 +02:00
|
|
|
#include "fastboot.h"
|
|
|
|
|
2014-04-30 23:05:28 +02:00
|
|
|
#include <ctype.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2012-05-23 02:53:34 +02:00
|
|
|
#include <getopt.h>
|
2014-05-16 05:06:40 +02:00
|
|
|
#include <inttypes.h>
|
2014-04-30 23:05:28 +02:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <sys/time.h>
|
2012-05-25 02:18:41 +02:00
|
|
|
#include <sys/types.h>
|
2014-04-30 23:05:28 +02:00
|
|
|
#include <unistd.h>
|
2016-01-20 17:32:08 +01:00
|
|
|
|
2016-11-15 02:08:47 +01:00
|
|
|
#include <chrono>
|
2015-09-15 06:05:41 +02:00
|
|
|
#include <functional>
|
2018-09-06 00:11:44 +02:00
|
|
|
#include <regex>
|
2018-11-29 02:10:42 +01:00
|
|
|
#include <string>
|
2016-11-15 02:08:47 +01:00
|
|
|
#include <thread>
|
2016-01-19 23:50:18 +01:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2012-05-25 02:18:41 +02:00
|
|
|
|
2016-09-01 00:07:18 +02:00
|
|
|
#include <android-base/file.h>
|
2016-06-28 23:48:45 +02:00
|
|
|
#include <android-base/macros.h>
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/parseint.h>
|
2016-01-20 17:32:08 +01:00
|
|
|
#include <android-base/parsenetaddress.h>
|
2016-04-25 23:31:18 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/strings.h>
|
2017-04-12 17:25:57 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
2018-08-29 23:58:02 +02:00
|
|
|
#include <build/version.h>
|
2018-11-17 00:53:35 +01:00
|
|
|
#include <liblp/liblp.h>
|
2018-08-29 23:58:02 +02:00
|
|
|
#include <platform_tools_version.h>
|
2012-05-25 02:18:41 +02:00
|
|
|
#include <sparse/sparse.h>
|
2015-03-19 07:12:44 +01:00
|
|
|
#include <ziparchive/zip_archive.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-03-19 06:47:09 +01:00
|
|
|
#include "bootimg_utils.h"
|
2018-08-29 19:44:33 +02:00
|
|
|
#include "constants.h"
|
2015-12-12 04:07:01 +01:00
|
|
|
#include "diagnose_usb.h"
|
2018-09-25 00:48:09 +02:00
|
|
|
#include "fastboot_driver.h"
|
2014-03-12 02:28:15 +01:00
|
|
|
#include "fs.h"
|
2016-01-20 17:32:08 +01:00
|
|
|
#include "tcp.h"
|
2015-10-30 19:22:01 +01:00
|
|
|
#include "transport.h"
|
2016-02-06 00:35:09 +01:00
|
|
|
#include "udp.h"
|
2015-10-30 19:22:01 +01:00
|
|
|
#include "usb.h"
|
2018-09-25 00:48:09 +02:00
|
|
|
#include "util.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-09-20 23:45:05 +02:00
|
|
|
using android::base::ReadFully;
|
2018-09-06 00:11:44 +02:00
|
|
|
using android::base::Split;
|
|
|
|
using android::base::Trim;
|
2017-04-12 17:25:57 +02:00
|
|
|
using android::base::unique_fd;
|
2018-11-29 02:10:42 +01:00
|
|
|
using namespace std::string_literals;
|
2017-04-12 17:25:57 +02:00
|
|
|
|
2016-01-20 17:32:08 +01:00
|
|
|
static const char* serial = nullptr;
|
2018-04-06 01:12:47 +02:00
|
|
|
|
|
|
|
static bool g_long_listing = false;
|
2017-04-04 16:52:47 +02:00
|
|
|
// Don't resparse files in too-big chunks.
|
|
|
|
// libsparse will support INT_MAX, but this results in large allocations, so
|
|
|
|
// let's keep it at 1GB to avoid memory pressure on the host.
|
|
|
|
static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
|
2018-04-20 04:49:44 +02:00
|
|
|
static uint64_t sparse_limit = 0;
|
2012-05-25 02:18:41 +02:00
|
|
|
static int64_t target_sparse_limit = -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-04-06 01:12:47 +02:00
|
|
|
static unsigned g_base_addr = 0x10000000;
|
2019-01-24 21:47:08 +01:00
|
|
|
static boot_img_hdr_v2 g_boot_img_hdr = {};
|
2018-04-06 01:12:47 +02:00
|
|
|
static std::string g_cmdline;
|
2019-01-24 21:47:08 +01:00
|
|
|
static std::string g_dtb_path;
|
2013-03-08 02:06:41 +01:00
|
|
|
|
2017-08-07 20:29:26 +02:00
|
|
|
static bool g_disable_verity = false;
|
|
|
|
static bool g_disable_verification = false;
|
|
|
|
|
2015-11-27 10:29:37 +01:00
|
|
|
static const std::string convert_fbe_marker_filename("convert_fbe");
|
|
|
|
|
2018-09-25 00:48:09 +02:00
|
|
|
fastboot::FastBootDriver* fb = nullptr;
|
|
|
|
|
2013-06-28 18:54:59 +02:00
|
|
|
enum fb_buffer_type {
|
2017-04-12 17:25:57 +02:00
|
|
|
FB_BUFFER_FD,
|
2013-06-28 18:54:59 +02:00
|
|
|
FB_BUFFER_SPARSE,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct fastboot_buffer {
|
|
|
|
enum fb_buffer_type type;
|
2015-04-08 05:12:50 +02:00
|
|
|
void* data;
|
|
|
|
int64_t sz;
|
2017-04-12 17:25:57 +02:00
|
|
|
int fd;
|
fastbootd: Support flashall with logical partitions.
Currently, fastboot flashall will try to flash every partition in the
image list. For devices with logical partitions, this will cause those
partitions to be flashed twice (once via super.img, and again for each
individual image). Additionally, super.img is needed to synchronize the
logical partition list, but it is only generated with "make dist",
making it inconvenient for most workflows.
This patch removes automatic flashing of super.img. Instead, devices
with a super partition must now generate a "super_empty.img"
describing the super geometry and logical partition layout. Before
flashing any other partitions, this image is used to ensure the device
has created all necessary logical partitions.
Additionally, before flashing (but after updating super), all logical
partitions that will be flashed are resized to 0. This ensures that
logical partitions will receive more optimal space allocation.
Finally, during flashing of individual images, logical partitions are
resized to the image size, since they do not have hardcoded sizes in the
build.
Bug: 78793464
Test: fastboot flashall can flash vendor, product, product_services
without a make dist/super.img.
Change-Id: I18c2291e0d0cb10de729c5646b75ac84dde8c871
2018-08-16 22:43:11 +02:00
|
|
|
int64_t image_size;
|
2013-06-28 18:54:59 +02:00
|
|
|
};
|
|
|
|
|
2018-08-28 22:54:03 +02: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
|
|
|
|
};
|
|
|
|
|
fastbootd: Support flashall with logical partitions.
Currently, fastboot flashall will try to flash every partition in the
image list. For devices with logical partitions, this will cause those
partitions to be flashed twice (once via super.img, and again for each
individual image). Additionally, super.img is needed to synchronize the
logical partition list, but it is only generated with "make dist",
making it inconvenient for most workflows.
This patch removes automatic flashing of super.img. Instead, devices
with a super partition must now generate a "super_empty.img"
describing the super geometry and logical partition layout. Before
flashing any other partitions, this image is used to ensure the device
has created all necessary logical partitions.
Additionally, before flashing (but after updating super), all logical
partitions that will be flashed are resized to 0. This ensures that
logical partitions will receive more optimal space allocation.
Finally, during flashing of individual images, logical partitions are
resized to the image size, since they do not have hardcoded sizes in the
build.
Bug: 78793464
Test: fastboot flashall can flash vendor, product, product_services
without a make dist/super.img.
Change-Id: I18c2291e0d0cb10de729c5646b75ac84dde8c871
2018-08-16 22:43:11 +02:00
|
|
|
struct Image {
|
2017-05-04 07:43:23 +02:00
|
|
|
const char* nickname;
|
|
|
|
const char* img_name;
|
|
|
|
const char* sig_name;
|
|
|
|
const char* part_name;
|
2018-08-02 19:46:21 +02:00
|
|
|
bool optional_if_no_image;
|
2018-08-28 22:54:03 +02:00
|
|
|
ImageType type;
|
2018-08-02 19:46:21 +02:00
|
|
|
bool IsSecondary() const { return nickname == nullptr; }
|
fastbootd: Support flashall with logical partitions.
Currently, fastboot flashall will try to flash every partition in the
image list. For devices with logical partitions, this will cause those
partitions to be flashed twice (once via super.img, and again for each
individual image). Additionally, super.img is needed to synchronize the
logical partition list, but it is only generated with "make dist",
making it inconvenient for most workflows.
This patch removes automatic flashing of super.img. Instead, devices
with a super partition must now generate a "super_empty.img"
describing the super geometry and logical partition layout. Before
flashing any other partitions, this image is used to ensure the device
has created all necessary logical partitions.
Additionally, before flashing (but after updating super), all logical
partitions that will be flashed are resized to 0. This ensures that
logical partitions will receive more optimal space allocation.
Finally, during flashing of individual images, logical partitions are
resized to the image size, since they do not have hardcoded sizes in the
build.
Bug: 78793464
Test: fastboot flashall can flash vendor, product, product_services
without a make dist/super.img.
Change-Id: I18c2291e0d0cb10de729c5646b75ac84dde8c871
2018-08-16 22:43:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static Image images[] = {
|
2018-05-25 17:07:19 +02:00
|
|
|
// clang-format off
|
2018-08-28 22:54:03 +02:00
|
|
|
{ "boot", "boot.img", "boot.sig", "boot", false, ImageType::BootCritical },
|
|
|
|
{ nullptr, "boot_other.img", "boot.sig", "boot", true, ImageType::Normal },
|
|
|
|
{ "cache", "cache.img", "cache.sig", "cache", true, ImageType::Extra },
|
|
|
|
{ "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, ImageType::BootCritical },
|
|
|
|
{ "dts", "dt.img", "dt.sig", "dts", true, ImageType::BootCritical },
|
|
|
|
{ "odm", "odm.img", "odm.sig", "odm", true, ImageType::Normal },
|
|
|
|
{ "product", "product.img", "product.sig", "product", true, ImageType::Normal },
|
2018-08-17 02:01:25 +02:00
|
|
|
{ "product_services",
|
|
|
|
"product_services.img",
|
|
|
|
"product_services.sig",
|
2018-08-07 00:36:00 +02:00
|
|
|
"product_services",
|
2018-08-28 22:54:03 +02:00
|
|
|
true, ImageType::Normal },
|
|
|
|
{ "recovery", "recovery.img", "recovery.sig", "recovery", true, ImageType::BootCritical },
|
|
|
|
{ "super", "super.img", "super.sig", "super", true, ImageType::Extra },
|
|
|
|
{ "system", "system.img", "system.sig", "system", false, ImageType::Normal },
|
|
|
|
{ nullptr, "system_other.img", "system.sig", "system", true, ImageType::Normal },
|
|
|
|
{ "userdata", "userdata.img", "userdata.sig", "userdata", true, ImageType::Extra },
|
|
|
|
{ "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, ImageType::BootCritical },
|
2018-10-15 23:46:59 +02:00
|
|
|
{ "vbmeta_system",
|
|
|
|
"vbmeta_system.img",
|
|
|
|
"vbmeta_system.sig",
|
|
|
|
"vbmeta_system",
|
2018-09-18 02:32:54 +02:00
|
|
|
true, ImageType::BootCritical },
|
2018-08-28 22:54:03 +02:00
|
|
|
{ "vendor", "vendor.img", "vendor.sig", "vendor", true, ImageType::Normal },
|
|
|
|
{ nullptr, "vendor_other.img", "vendor.sig", "vendor", true, ImageType::Normal },
|
2018-05-25 17:07:19 +02:00
|
|
|
// clang-format on
|
2013-06-28 18:54:59 +02:00
|
|
|
};
|
2009-04-29 01:05:07 +02:00
|
|
|
|
2018-11-28 05:19:26 +01:00
|
|
|
static char* get_android_product_out() {
|
2017-05-04 07:43:23 +02:00
|
|
|
char* dir = getenv("ANDROID_PRODUCT_OUT");
|
2016-06-29 18:26:44 +02:00
|
|
|
if (dir == nullptr || dir[0] == '\0') {
|
2018-11-28 05:19:26 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string find_item_given_name(const std::string& img_name) {
|
|
|
|
char* dir = get_android_product_out();
|
|
|
|
if (!dir) {
|
2017-05-04 07:43:23 +02:00
|
|
|
die("ANDROID_PRODUCT_OUT not set");
|
2016-06-29 18:26:44 +02:00
|
|
|
}
|
2018-08-28 22:54:03 +02:00
|
|
|
return std::string(dir) + "/" + img_name;
|
2016-06-29 18:26:44 +02:00
|
|
|
}
|
|
|
|
|
2017-05-12 00:05:13 +02:00
|
|
|
static std::string find_item(const std::string& item) {
|
2017-05-04 07:43:23 +02:00
|
|
|
for (size_t i = 0; i < arraysize(images); ++i) {
|
2017-05-09 03:04:49 +02:00
|
|
|
if (images[i].nickname && item == images[i].nickname) {
|
2017-05-04 07:43:23 +02:00
|
|
|
return find_item_given_name(images[i].img_name);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-05-09 03:04:49 +02:00
|
|
|
fprintf(stderr, "unknown partition '%s'\n", item.c_str());
|
2017-05-04 07:43:23 +02:00
|
|
|
return "";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-09-25 00:48:09 +02:00
|
|
|
double last_start_time;
|
|
|
|
|
|
|
|
static void Status(const std::string& message) {
|
|
|
|
static constexpr char kStatusFormat[] = "%-50s ";
|
|
|
|
fprintf(stderr, kStatusFormat, message.c_str());
|
|
|
|
last_start_time = now();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Epilog(int status) {
|
|
|
|
if (status) {
|
|
|
|
fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
|
|
|
|
die("Command failed");
|
|
|
|
} else {
|
|
|
|
double split = now();
|
|
|
|
fprintf(stderr, "OKAY [%7.3fs]\n", (split - last_start_time));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void InfoMessage(const std::string& info) {
|
|
|
|
fprintf(stderr, "(bootloader) %s\n", info.c_str());
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
static int64_t get_file_size(int fd) {
|
|
|
|
struct stat sb;
|
2018-09-20 23:45:05 +02:00
|
|
|
if (fstat(fd, &sb) == -1) {
|
|
|
|
die("could not get file size");
|
|
|
|
}
|
|
|
|
return sb.st_size;
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 23:45:05 +02:00
|
|
|
bool ReadFileToVector(const std::string& file, std::vector<char>* out) {
|
|
|
|
out->clear();
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-09-20 23:45:05 +02:00
|
|
|
unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC | O_BINARY)));
|
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-09-20 23:45:05 +02:00
|
|
|
out->resize(get_file_size(fd));
|
|
|
|
return ReadFully(fd, out->data(), out->size());
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
|
2015-08-11 00:30:54 +02:00
|
|
|
if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-04-06 21:39:30 +02:00
|
|
|
// require matching serial number or device path if requested
|
2009-03-04 04:32:55 +01:00
|
|
|
// at the command line with the -s option.
|
2012-06-06 20:53:33 +02:00
|
|
|
if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
|
|
|
|
strcmp(local_serial, info->device_path) != 0)) return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
static int match_fastboot(usb_ifc_info* info) {
|
2013-03-08 02:06:41 +01:00
|
|
|
return match_fastboot_with_serial(info, serial);
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
static int list_devices_callback(usb_ifc_info* info) {
|
|
|
|
if (match_fastboot_with_serial(info, nullptr) == 0) {
|
2015-12-12 04:07:01 +01:00
|
|
|
std::string serial = info->serial_number;
|
2009-10-07 03:07:49 +02:00
|
|
|
if (!info->writable) {
|
2015-12-12 04:07:01 +01:00
|
|
|
serial = UsbNoPermissionsShortHelpText();
|
2009-10-07 03:07:49 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
if (!serial[0]) {
|
|
|
|
serial = "????????????";
|
|
|
|
}
|
2012-06-05 05:29:11 +02:00
|
|
|
// output compatible with "adb devices"
|
2018-04-06 01:12:47 +02:00
|
|
|
if (!g_long_listing) {
|
2015-12-12 04:07:01 +01:00
|
|
|
printf("%s\tfastboot", serial.c_str());
|
2012-04-06 21:39:30 +02:00
|
|
|
} else {
|
2015-12-12 04:07:01 +01:00
|
|
|
printf("%-22s fastboot", serial.c_str());
|
|
|
|
if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
|
2012-04-06 21:39:30 +02:00
|
|
|
}
|
2015-12-12 04:07:01 +01:00
|
|
|
putchar('\n');
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-01-20 17:32:08 +01:00
|
|
|
// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
|
|
|
|
// a specific device, otherwise the first USB device found will be used.
|
|
|
|
//
|
2018-04-02 23:24:03 +02:00
|
|
|
// If |serial| is non-null but invalid, this exits.
|
2016-01-20 17:32:08 +01:00
|
|
|
// Otherwise it blocks until the target is available.
|
|
|
|
//
|
|
|
|
// The returned Transport is a singleton, so multiple calls to this function will return the same
|
|
|
|
// object, and the caller should not attempt to delete the returned Transport.
|
2015-10-30 19:22:01 +01:00
|
|
|
static Transport* open_device() {
|
2016-01-20 17:32:08 +01:00
|
|
|
bool announce = true;
|
|
|
|
|
2016-02-06 00:35:09 +01:00
|
|
|
Socket::Protocol protocol = Socket::Protocol::kTcp;
|
2016-01-20 17:32:08 +01:00
|
|
|
std::string host;
|
2016-02-06 00:35:09 +01:00
|
|
|
int port = 0;
|
|
|
|
if (serial != nullptr) {
|
|
|
|
const char* net_address = nullptr;
|
|
|
|
|
|
|
|
if (android::base::StartsWith(serial, "tcp:")) {
|
|
|
|
protocol = Socket::Protocol::kTcp;
|
|
|
|
port = tcp::kDefaultPort;
|
|
|
|
net_address = serial + strlen("tcp:");
|
|
|
|
} else if (android::base::StartsWith(serial, "udp:")) {
|
|
|
|
protocol = Socket::Protocol::kUdp;
|
|
|
|
port = udp::kDefaultPort;
|
|
|
|
net_address = serial + strlen("udp:");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (net_address != nullptr) {
|
|
|
|
std::string error;
|
|
|
|
if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
|
2018-04-02 23:24:03 +02:00
|
|
|
die("invalid network address '%s': %s\n", net_address, error.c_str());
|
2016-02-06 00:35:09 +01:00
|
|
|
}
|
2016-01-20 17:32:08 +01:00
|
|
|
}
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-08-28 01:47:32 +02:00
|
|
|
Transport* transport = nullptr;
|
2016-01-20 17:32:08 +01:00
|
|
|
while (true) {
|
|
|
|
if (!host.empty()) {
|
|
|
|
std::string error;
|
2016-02-06 00:35:09 +01:00
|
|
|
if (protocol == Socket::Protocol::kTcp) {
|
|
|
|
transport = tcp::Connect(host, port, &error).release();
|
|
|
|
} else if (protocol == Socket::Protocol::kUdp) {
|
|
|
|
transport = udp::Connect(host, port, &error).release();
|
|
|
|
}
|
|
|
|
|
2016-01-20 17:32:08 +01:00
|
|
|
if (transport == nullptr && announce) {
|
|
|
|
fprintf(stderr, "error: %s\n", error.c_str());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
transport = usb_open(match_fastboot);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transport != nullptr) {
|
|
|
|
return transport;
|
|
|
|
}
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (announce) {
|
2016-01-20 17:32:08 +01:00
|
|
|
announce = false;
|
2015-08-20 02:49:45 +02:00
|
|
|
fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2016-11-15 02:08:47 +01:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
static void list_devices() {
|
2009-03-04 04:32:55 +01:00
|
|
|
// We don't actually open a USB device here,
|
|
|
|
// just getting our callback called so we can
|
|
|
|
// list all the connected devices.
|
|
|
|
usb_open(list_devices_callback);
|
|
|
|
}
|
|
|
|
|
2017-05-09 03:04:49 +02:00
|
|
|
static void syntax_error(const char* fmt, ...) {
|
|
|
|
fprintf(stderr, "fastboot: usage: ");
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int show_help() {
|
|
|
|
// clang-format off
|
|
|
|
fprintf(stdout,
|
2018-05-25 01:36:05 +02:00
|
|
|
// 1 2 3 4 5 6 7 8
|
|
|
|
// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
|
2018-04-03 22:30:18 +02:00
|
|
|
"usage: fastboot [OPTION...] COMMAND...\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
"\n"
|
2018-04-03 22:30:18 +02:00
|
|
|
"flashing:\n"
|
|
|
|
" update ZIP Flash all partitions from an update.zip package.\n"
|
|
|
|
" flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
|
|
|
|
" On A/B devices, flashed slot is set as active.\n"
|
|
|
|
" Secondary images may be flashed to inactive slot.\n"
|
2018-05-25 01:36:05 +02:00
|
|
|
" flash PARTITION [FILENAME] Flash given partition, using the image from\n"
|
|
|
|
" $ANDROID_PRODUCT_OUT if no filename is given.\n"
|
2018-04-03 22:30:18 +02:00
|
|
|
"\n"
|
|
|
|
"basics:\n"
|
|
|
|
" devices [-l] List devices in bootloader (-l: with device paths).\n"
|
|
|
|
" getvar NAME Display given bootloader variable.\n"
|
|
|
|
" reboot [bootloader] Reboot device.\n"
|
|
|
|
"\n"
|
|
|
|
"locking/unlocking:\n"
|
|
|
|
" flashing lock|unlock Lock/unlock partitions for flashing\n"
|
|
|
|
" flashing lock_critical|unlock_critical\n"
|
|
|
|
" Lock/unlock 'critical' bootloader partitions.\n"
|
|
|
|
" flashing get_unlock_ability\n"
|
|
|
|
" Check whether unlocking is allowed (1) or not(0).\n"
|
|
|
|
"\n"
|
|
|
|
"advanced:\n"
|
|
|
|
" erase PARTITION Erase a flash partition.\n"
|
|
|
|
" format[:FS_TYPE[:SIZE]] PARTITION\n"
|
|
|
|
" Format a flash partition.\n"
|
|
|
|
" set_active SLOT Set the active slot.\n"
|
|
|
|
" oem [COMMAND...] Execute OEM-specific command.\n"
|
2019-01-15 23:38:20 +01:00
|
|
|
" gsi wipe|disable Wipe or disable a GSI installation (fastbootd only).\n"
|
2018-04-03 22:30:18 +02:00
|
|
|
"\n"
|
|
|
|
"boot image:\n"
|
|
|
|
" boot KERNEL [RAMDISK [SECOND]]\n"
|
|
|
|
" Download and boot kernel from RAM.\n"
|
|
|
|
" flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
|
|
|
|
" Create boot image and flash it.\n"
|
2019-01-24 21:47:08 +01:00
|
|
|
" --dtb DTB Specify path to DTB for boot image header version 2.\n"
|
2018-04-06 01:12:47 +02:00
|
|
|
" --cmdline CMDLINE Override kernel command line.\n"
|
2018-04-03 22:30:18 +02:00
|
|
|
" --base ADDRESS Set kernel base address (default: 0x10000000).\n"
|
|
|
|
" --kernel-offset Set kernel offset (default: 0x00008000).\n"
|
|
|
|
" --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
|
|
|
|
" --tags-offset Set tags offset (default: 0x00000100).\n"
|
2019-01-24 21:47:08 +01:00
|
|
|
" --dtb-offset Set dtb offset (default: 0x01100000).\n"
|
2018-04-03 22:30:18 +02:00
|
|
|
" --page-size BYTES Set flash page size (default: 2048).\n"
|
|
|
|
" --header-version VERSION Set boot image header version.\n"
|
2018-04-06 01:12:47 +02:00
|
|
|
" --os-version MAJOR[.MINOR[.PATCH]]\n"
|
|
|
|
" Set boot image OS version (default: 0.0.0).\n"
|
|
|
|
" --os-patch-level YYYY-MM-DD\n"
|
|
|
|
" Set boot image OS security patch level.\n"
|
|
|
|
// TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
|
2018-04-03 22:30:18 +02:00
|
|
|
"\n"
|
2018-04-04 22:36:44 +02:00
|
|
|
// TODO: what device(s) used this? is there any documentation?
|
2018-04-03 22:30:18 +02:00
|
|
|
//" continue Continue with autoboot.\n"
|
|
|
|
//"\n"
|
|
|
|
"Android Things:\n"
|
|
|
|
" stage IN_FILE Sends given file to stage for the next command.\n"
|
|
|
|
" get_staged OUT_FILE Writes data staged by the last command to a file.\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
"\n"
|
|
|
|
"options:\n"
|
2018-04-03 22:30:18 +02:00
|
|
|
" -w Wipe userdata.\n"
|
|
|
|
" -s SERIAL Specify a USB device.\n"
|
|
|
|
" -s tcp|udp:HOST[:PORT] Specify a network device.\n"
|
2018-04-20 04:49:44 +02:00
|
|
|
" -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
|
2018-11-17 00:53:35 +01:00
|
|
|
" --force Force a flash operation that may be unsafe.\n"
|
2018-04-03 22:30:18 +02:00
|
|
|
" --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
|
|
|
|
" non-current slot (default: current active slot).\n"
|
|
|
|
" --set-active[=SLOT] Sets the active slot before rebooting.\n"
|
|
|
|
" --skip-secondary Don't flash secondary slots in flashall/update.\n"
|
|
|
|
" --skip-reboot Don't reboot device after flashing.\n"
|
|
|
|
" --disable-verity Sets disable-verity when flashing vbmeta.\n"
|
|
|
|
" --disable-verification Sets disable-verification when flashing vbmeta.\n"
|
2015-11-27 10:29:37 +01:00
|
|
|
#if !defined(_WIN32)
|
2018-04-03 22:30:18 +02:00
|
|
|
" --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
|
2015-11-27 10:29:37 +01:00
|
|
|
#endif
|
2018-04-03 22:30:18 +02:00
|
|
|
// TODO: remove --unbuffered?
|
|
|
|
" --unbuffered Don't buffer input or output.\n"
|
|
|
|
" --verbose, -v Verbose output.\n"
|
|
|
|
" --version Display version.\n"
|
|
|
|
" --help, -h Show this message.\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
);
|
2017-05-09 03:04:49 +02:00
|
|
|
// clang-format off
|
|
|
|
return 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-11-27 10:29:37 +01:00
|
|
|
|
2018-09-20 23:45:05 +02:00
|
|
|
static std::vector<char> LoadBootableImage(const std::string& kernel, const std::string& ramdisk,
|
|
|
|
const std::string& second_stage) {
|
|
|
|
std::vector<char> kernel_data;
|
|
|
|
if (!ReadFileToVector(kernel, &kernel_data)) {
|
|
|
|
die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
|
|
|
|
}
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
// Is this actually a boot image?
|
2019-01-24 21:47:08 +01:00
|
|
|
if (kernel_data.size() < sizeof(boot_img_hdr_v2)) {
|
2018-01-19 01:08:24 +01:00
|
|
|
die("cannot load '%s': too short", kernel.c_str());
|
|
|
|
}
|
2018-09-20 23:45:05 +02:00
|
|
|
if (!memcmp(kernel_data.data(), BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
|
2018-04-06 01:12:47 +02:00
|
|
|
if (!g_cmdline.empty()) {
|
2019-01-24 21:47:08 +01:00
|
|
|
bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v2*>(kernel_data.data()), g_cmdline);
|
2018-04-06 01:12:47 +02:00
|
|
|
}
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2017-10-27 23:21:12 +02:00
|
|
|
if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2018-09-20 23:45:05 +02:00
|
|
|
return kernel_data;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-09-20 23:45:05 +02:00
|
|
|
std::vector<char> ramdisk_data;
|
2017-05-09 03:04:49 +02:00
|
|
|
if (!ramdisk.empty()) {
|
2018-09-20 23:45:05 +02:00
|
|
|
if (!ReadFileToVector(ramdisk, &ramdisk_data)) {
|
|
|
|
die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-09-20 23:45:05 +02:00
|
|
|
std::vector<char> second_stage_data;
|
2017-05-09 03:04:49 +02:00
|
|
|
if (!second_stage.empty()) {
|
2018-09-20 23:45:05 +02:00
|
|
|
if (!ReadFileToVector(second_stage, &second_stage_data)) {
|
|
|
|
die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
|
|
|
|
}
|
2014-07-17 17:17:54 +02:00
|
|
|
}
|
2019-01-24 21:47:08 +01:00
|
|
|
|
|
|
|
std::vector<char> dtb_data;
|
|
|
|
if (!g_dtb_path.empty()) {
|
|
|
|
if (g_boot_img_hdr.header_version < 2) {
|
|
|
|
die("Argument dtb not supported for boot image header version %d\n",
|
|
|
|
g_boot_img_hdr.header_version);
|
|
|
|
}
|
|
|
|
if (!ReadFileToVector(g_dtb_path, &dtb_data)) {
|
|
|
|
die("cannot load '%s': %s", g_dtb_path.c_str(), strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
fprintf(stderr,"creating boot image...\n");
|
2017-05-09 03:04:49 +02:00
|
|
|
|
2018-09-20 23:45:05 +02:00
|
|
|
std::vector<char> out;
|
2019-01-24 21:47:08 +01:00
|
|
|
boot_img_hdr_v2* boot_image_data = mkbootimg(kernel_data, ramdisk_data, second_stage_data,
|
|
|
|
dtb_data, g_base_addr, g_boot_img_hdr, &out);
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2018-09-20 23:45:05 +02:00
|
|
|
if (!g_cmdline.empty()) bootimg_set_cmdline(boot_image_data, g_cmdline);
|
|
|
|
fprintf(stderr, "creating boot image - %zu bytes\n", out.size());
|
|
|
|
return out;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-09-20 23:45:05 +02:00
|
|
|
static bool UnzipToMemory(ZipArchiveHandle zip, const std::string& entry_name,
|
|
|
|
std::vector<char>* out) {
|
|
|
|
ZipString zip_entry_name(entry_name.c_str());
|
2015-03-19 07:12:44 +01:00
|
|
|
ZipEntry zip_entry;
|
|
|
|
if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
|
2018-09-20 23:45:05 +02:00
|
|
|
fprintf(stderr, "archive does not contain '%s'\n", entry_name.c_str());
|
|
|
|
return false;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-09-20 23:45:05 +02:00
|
|
|
out->resize(zip_entry.uncompressed_length);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-09-20 23:45:05 +02:00
|
|
|
fprintf(stderr, "extracting %s (%zu MB) to RAM...\n", entry_name.c_str(),
|
|
|
|
out->size() / 1024 / 1024);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-09-20 23:45:05 +02:00
|
|
|
int error = ExtractToMemory(zip, &zip_entry, reinterpret_cast<uint8_t*>(out->data()),
|
|
|
|
out->size());
|
|
|
|
if (error != 0) die("failed to extract '%s': %s", entry_name.c_str(), ErrorCodeString(error));
|
2014-05-08 01:31:59 +02:00
|
|
|
|
2018-09-20 23:45:05 +02:00
|
|
|
return true;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-06-04 00:22:11 +02:00
|
|
|
#if defined(_WIN32)
|
|
|
|
|
|
|
|
// TODO: move this to somewhere it can be shared.
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
// Windows' tmpfile(3) requires administrator rights because
|
|
|
|
// it creates temporary files in the root directory.
|
|
|
|
static FILE* win32_tmpfile() {
|
|
|
|
char temp_path[PATH_MAX];
|
|
|
|
DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
|
|
|
|
if (nchars == 0 || nchars >= sizeof(temp_path)) {
|
2017-10-27 23:21:12 +02:00
|
|
|
die("GetTempPath failed, error %ld", GetLastError());
|
2015-06-04 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char filename[PATH_MAX];
|
|
|
|
if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
|
2017-10-27 23:21:12 +02:00
|
|
|
die("GetTempFileName failed, error %ld", GetLastError());
|
2015-06-04 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return fopen(filename, "w+bTD");
|
|
|
|
}
|
|
|
|
|
|
|
|
#define tmpfile win32_tmpfile
|
|
|
|
|
2015-11-27 10:29:37 +01:00
|
|
|
static std::string make_temporary_directory() {
|
2017-10-27 23:21:12 +02:00
|
|
|
die("make_temporary_directory not supported under Windows, sorry!");
|
2015-11-27 10:29:37 +01:00
|
|
|
}
|
|
|
|
|
2018-04-02 23:24:03 +02:00
|
|
|
static int make_temporary_fd(const char* /*what*/) {
|
2017-04-26 03:33:09 +02:00
|
|
|
// TODO: reimplement to avoid leaking a FILE*.
|
|
|
|
return fileno(tmpfile());
|
|
|
|
}
|
|
|
|
|
2015-11-27 10:29:37 +01:00
|
|
|
#else
|
|
|
|
|
2017-04-26 03:33:09 +02:00
|
|
|
static std::string make_temporary_template() {
|
|
|
|
const char* tmpdir = getenv("TMPDIR");
|
|
|
|
if (tmpdir == nullptr) tmpdir = P_tmpdir;
|
|
|
|
return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
|
|
|
|
}
|
|
|
|
|
2015-11-27 10:29:37 +01:00
|
|
|
static std::string make_temporary_directory() {
|
2017-04-26 03:33:09 +02:00
|
|
|
std::string result(make_temporary_template());
|
|
|
|
if (mkdtemp(&result[0]) == nullptr) {
|
2018-05-22 08:02:26 +02:00
|
|
|
die("unable to create temporary directory with template %s: %s",
|
|
|
|
result.c_str(), strerror(errno));
|
2015-11-27 10:29:37 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-02 23:24:03 +02:00
|
|
|
static int make_temporary_fd(const char* what) {
|
2017-04-26 03:33:09 +02:00
|
|
|
std::string path_template(make_temporary_template());
|
|
|
|
int fd = mkstemp(&path_template[0]);
|
|
|
|
if (fd == -1) {
|
2018-05-22 08:02:26 +02:00
|
|
|
die("failed to create temporary file for %s with template %s: %s\n",
|
|
|
|
path_template.c_str(), what, strerror(errno));
|
2017-04-26 03:33:09 +02:00
|
|
|
}
|
|
|
|
unlink(path_template.c_str());
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2015-06-04 00:22:11 +02:00
|
|
|
#endif
|
|
|
|
|
2015-11-27 10:29:37 +01:00
|
|
|
static std::string create_fbemarker_tmpdir() {
|
|
|
|
std::string dir = make_temporary_directory();
|
|
|
|
std::string marker_file = dir + "/" + convert_fbe_marker_filename;
|
|
|
|
int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
|
|
|
|
if (fd == -1) {
|
2018-04-02 23:24:03 +02:00
|
|
|
die("unable to create FBE marker file %s locally: %s",
|
|
|
|
marker_file.c_str(), strerror(errno));
|
2015-11-27 10:29:37 +01:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void delete_fbemarker_tmpdir(const std::string& dir) {
|
|
|
|
std::string marker_file = dir + "/" + convert_fbe_marker_filename;
|
|
|
|
if (unlink(marker_file.c_str()) == -1) {
|
|
|
|
fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
|
|
|
|
marker_file.c_str(), errno, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rmdir(dir.c_str()) == -1) {
|
|
|
|
fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
|
|
|
|
dir.c_str(), errno, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 07:43:23 +02:00
|
|
|
static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
|
2018-04-02 23:24:03 +02:00
|
|
|
unique_fd fd(make_temporary_fd(entry_name));
|
2013-06-28 18:54:59 +02:00
|
|
|
|
2015-06-25 23:39:19 +02:00
|
|
|
ZipString zip_entry_name(entry_name);
|
2015-03-19 19:44:32 +01:00
|
|
|
ZipEntry zip_entry;
|
|
|
|
if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
|
|
|
|
fprintf(stderr, "archive does not contain '%s'\n", entry_name);
|
2018-08-29 23:15:49 +02:00
|
|
|
errno = ENOENT;
|
2013-06-28 18:54:59 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-11-10 17:42:17 +01:00
|
|
|
fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
|
2017-04-26 03:33:09 +02:00
|
|
|
zip_entry.uncompressed_length / 1024 / 1024);
|
2017-11-10 17:42:17 +01:00
|
|
|
double start = now();
|
2015-03-19 19:44:32 +01:00
|
|
|
int error = ExtractEntryToFile(zip, &zip_entry, fd);
|
|
|
|
if (error != 0) {
|
2017-11-10 17:42:17 +01:00
|
|
|
die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
|
2017-10-27 23:21:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lseek(fd, 0, SEEK_SET) != 0) {
|
2017-11-10 17:42:17 +01:00
|
|
|
die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
|
|
|
|
2017-11-10 17:42:17 +01:00
|
|
|
fprintf(stderr, " took %.3fs\n", now() - start);
|
|
|
|
|
2017-04-26 03:33:09 +02:00
|
|
|
return fd.release();
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
|
|
|
|
2018-09-06 00:11:44 +02:00
|
|
|
static void CheckRequirement(const std::string& cur_product, const std::string& var,
|
|
|
|
const std::string& product, bool invert,
|
|
|
|
const std::vector<std::string>& options) {
|
|
|
|
Status("Checking '" + var + "'");
|
|
|
|
|
|
|
|
double start = now();
|
2018-03-28 17:20:00 +02:00
|
|
|
|
2018-09-06 00:11:44 +02:00
|
|
|
if (!product.empty()) {
|
|
|
|
if (product != cur_product) {
|
|
|
|
double split = now();
|
|
|
|
fprintf(stderr, "IGNORE, product is %s required only for %s [%7.3fs]\n",
|
|
|
|
cur_product.c_str(), product.c_str(), (split - start));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string var_value;
|
2018-09-25 00:48:09 +02:00
|
|
|
if (fb->GetVar(var, &var_value) != fastboot::SUCCESS) {
|
2018-09-06 00:11:44 +02:00
|
|
|
fprintf(stderr, "FAILED\n\n");
|
2018-09-25 00:48:09 +02:00
|
|
|
fprintf(stderr, "Could not getvar for '%s' (%s)\n\n", var.c_str(),
|
|
|
|
fb->Error().c_str());
|
2018-09-06 00:11:44 +02:00
|
|
|
die("requirements not met!");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool match = false;
|
|
|
|
for (const auto& option : options) {
|
|
|
|
if (option == var_value || (option.back() == '*' &&
|
|
|
|
!var_value.compare(0, option.length() - 1, option, 0,
|
|
|
|
option.length() - 1))) {
|
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-09-06 00:11:44 +02:00
|
|
|
if (invert) {
|
|
|
|
match = !match;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
double split = now();
|
|
|
|
fprintf(stderr, "OKAY [%7.3fs]\n", (split - start));
|
|
|
|
return;
|
|
|
|
}
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2018-09-06 00:11:44 +02:00
|
|
|
fprintf(stderr, "FAILED\n\n");
|
|
|
|
fprintf(stderr, "Device %s is '%s'.\n", var.c_str(), var_value.c_str());
|
|
|
|
fprintf(stderr, "Update %s '%s'", invert ? "rejects" : "requires", options[0].c_str());
|
|
|
|
for (auto it = std::next(options.begin()); it != options.end(); ++it) {
|
|
|
|
fprintf(stderr, " or '%s'", it->c_str());
|
|
|
|
}
|
|
|
|
fprintf(stderr, ".\n\n");
|
|
|
|
die("requirements not met!");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParseRequirementLine(const std::string& line, std::string* name, std::string* product,
|
|
|
|
bool* invert, std::vector<std::string>* options) {
|
2018-03-28 17:20:00 +02:00
|
|
|
// "require product=alpha|beta|gamma"
|
|
|
|
// "require version-bootloader=1234"
|
|
|
|
// "require-for-product:gamma version-bootloader=istanbul|constantinople"
|
|
|
|
// "require partition-exists=vendor"
|
2018-09-06 00:11:44 +02:00
|
|
|
*product = "";
|
|
|
|
*invert = false;
|
|
|
|
|
|
|
|
auto require_reject_regex = std::regex{"(require\\s+|reject\\s+)?\\s*(\\S+)\\s*=\\s*(.*)"};
|
|
|
|
auto require_product_regex =
|
|
|
|
std::regex{"require-for-product:\\s*(\\S+)\\s+(\\S+)\\s*=\\s*(.*)"};
|
|
|
|
std::smatch match_results;
|
|
|
|
|
|
|
|
if (std::regex_match(line, match_results, require_reject_regex)) {
|
|
|
|
*invert = Trim(match_results[1]) == "reject";
|
|
|
|
} else if (std::regex_match(line, match_results, require_product_regex)) {
|
|
|
|
*product = match_results[1];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-28 17:20:00 +02:00
|
|
|
|
2018-09-06 00:11:44 +02:00
|
|
|
*name = match_results[2];
|
|
|
|
// Work around an unfortunate name mismatch.
|
|
|
|
if (*name == "board") {
|
|
|
|
*name = "product";
|
2018-03-28 17:20:00 +02:00
|
|
|
}
|
|
|
|
|
2018-09-06 00:11:44 +02:00
|
|
|
auto raw_options = Split(match_results[3], "|");
|
|
|
|
for (const auto& option : raw_options) {
|
|
|
|
auto trimmed_option = Trim(option);
|
|
|
|
options->emplace_back(trimmed_option);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2018-09-06 00:11:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-09-06 00:11:44 +02:00
|
|
|
// "require partition-exists=x" is a special case, added because of the trouble we had when
|
|
|
|
// Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
|
|
|
|
// missing out new partitions. A device with new partitions can use "partition-exists" to
|
|
|
|
// override the fields `optional_if_no_image` in the `images` array.
|
|
|
|
static void HandlePartitionExists(const std::vector<std::string>& options) {
|
|
|
|
const std::string& partition_name = options[0];
|
|
|
|
std::string has_slot;
|
2018-09-25 00:48:09 +02:00
|
|
|
if (fb->GetVar("has-slot:" + partition_name, &has_slot) != fastboot::SUCCESS ||
|
2018-09-06 00:11:44 +02:00
|
|
|
(has_slot != "yes" && has_slot != "no")) {
|
|
|
|
die("device doesn't have required partition %s!", partition_name.c_str());
|
|
|
|
}
|
|
|
|
bool known_partition = false;
|
|
|
|
for (size_t i = 0; i < arraysize(images); ++i) {
|
|
|
|
if (images[i].nickname && images[i].nickname == partition_name) {
|
|
|
|
images[i].optional_if_no_image = false;
|
|
|
|
known_partition = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!known_partition) {
|
|
|
|
die("device requires partition %s which is not known to this version of fastboot",
|
|
|
|
partition_name.c_str());
|
|
|
|
}
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-09-06 00:11:44 +02:00
|
|
|
static void CheckRequirements(const std::string& data) {
|
|
|
|
std::string cur_product;
|
2018-09-25 00:48:09 +02:00
|
|
|
if (fb->GetVar("product", &cur_product) != fastboot::SUCCESS) {
|
|
|
|
fprintf(stderr, "getvar:product FAILED (%s)\n", fb->Error().c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-09-06 00:11:44 +02:00
|
|
|
auto lines = Split(data, "\n");
|
|
|
|
for (const auto& line : lines) {
|
|
|
|
if (line.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
std::string product;
|
|
|
|
bool invert;
|
|
|
|
std::vector<std::string> options;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-09-06 00:11:44 +02:00
|
|
|
if (!ParseRequirementLine(line, &name, &product, &invert, &options)) {
|
|
|
|
fprintf(stderr, "android-info.txt syntax error: %s\n", line.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (name == "partition-exists") {
|
|
|
|
HandlePartitionExists(options);
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2018-09-06 00:11:44 +02:00
|
|
|
CheckRequirement(cur_product, name, product, invert, options);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 00:48:09 +02:00
|
|
|
static void DisplayVarOrError(const std::string& label, const std::string& var) {
|
|
|
|
std::string value;
|
|
|
|
|
|
|
|
if (fb->GetVar(var, &value) != fastboot::SUCCESS) {
|
|
|
|
Status("getvar:" + var);
|
|
|
|
fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "%s: %s\n", label.c_str(), value.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DumpInfo() {
|
|
|
|
fprintf(stderr, "--------------------------------------------\n");
|
|
|
|
DisplayVarOrError("Bootloader Version...", "version-bootloader");
|
|
|
|
DisplayVarOrError("Baseband Version.....", "version-baseband");
|
|
|
|
DisplayVarOrError("Serial Number........", "serialno");
|
|
|
|
fprintf(stderr, "--------------------------------------------\n");
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-04-24 19:54:21 +02:00
|
|
|
static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
|
2015-03-31 21:09:29 +02:00
|
|
|
struct sparse_file* s = sparse_file_import_auto(fd, false, true);
|
2017-10-27 23:21:12 +02:00
|
|
|
if (!s) die("cannot sparse read file");
|
2012-05-25 02:18:41 +02:00
|
|
|
|
2018-04-24 19:54:21 +02:00
|
|
|
if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
|
|
|
|
die("invalid max size %" PRId64, max_size);
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
int files = sparse_file_resparse(s, max_size, nullptr, 0);
|
2017-10-27 23:21:12 +02:00
|
|
|
if (files < 0) die("Failed to resparse");
|
2012-05-25 02:18:41 +02:00
|
|
|
|
2015-03-19 06:47:09 +01:00
|
|
|
sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
|
2017-10-27 23:21:12 +02:00
|
|
|
if (!out_s) die("Failed to allocate sparse file array");
|
2012-05-25 02:18:41 +02:00
|
|
|
|
|
|
|
files = sparse_file_resparse(s, max_size, out_s, files);
|
2017-10-27 23:21:12 +02:00
|
|
|
if (files < 0) die("Failed to resparse");
|
2012-05-25 02:18:41 +02:00
|
|
|
|
|
|
|
return out_s;
|
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
static int64_t get_target_sparse_limit() {
|
2015-10-30 19:49:47 +01:00
|
|
|
std::string max_download_size;
|
2018-09-25 00:48:09 +02:00
|
|
|
if (fb->GetVar("max-download-size", &max_download_size) != fastboot::SUCCESS ||
|
2018-04-02 23:24:03 +02:00
|
|
|
max_download_size.empty()) {
|
|
|
|
verbose("target didn't report max-download-size");
|
2015-10-30 19:49:47 +01:00
|
|
|
return 0;
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
|
2015-11-03 00:51:12 +01:00
|
|
|
// Some bootloaders (angler, for example) send spurious whitespace too.
|
|
|
|
max_download_size = android::base::Trim(max_download_size);
|
|
|
|
|
2015-10-30 19:49:47 +01:00
|
|
|
uint64_t limit;
|
2016-10-12 02:09:00 +02:00
|
|
|
if (!android::base::ParseUint(max_download_size, &limit)) {
|
2015-11-02 18:01:53 +01:00
|
|
|
fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
|
2015-10-30 19:49:47 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2018-04-02 23:24:03 +02:00
|
|
|
if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
|
2012-05-25 02:18:41 +02:00
|
|
|
return limit;
|
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
static int64_t get_sparse_limit(int64_t size) {
|
2018-04-20 04:49:44 +02:00
|
|
|
int64_t limit = sparse_limit;
|
|
|
|
if (limit == 0) {
|
|
|
|
// Unlimited, so see what the target device's limit is.
|
|
|
|
// TODO: shouldn't we apply this limit even if you've used -S?
|
2012-05-25 02:18:41 +02:00
|
|
|
if (target_sparse_limit == -1) {
|
2018-06-26 22:38:35 +02:00
|
|
|
target_sparse_limit = get_target_sparse_limit();
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
if (target_sparse_limit > 0) {
|
|
|
|
limit = target_sparse_limit;
|
|
|
|
} else {
|
2012-07-25 03:05:21 +02:00
|
|
|
return 0;
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > limit) {
|
2017-04-04 16:52:47 +02:00
|
|
|
return std::min(limit, RESPARSE_LIMIT);
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
|
2015-04-08 05:12:50 +02:00
|
|
|
int64_t sz = get_file_size(fd);
|
|
|
|
if (sz == -1) {
|
2016-05-11 21:39:27 +02:00
|
|
|
return false;
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
2014-03-12 02:28:15 +01:00
|
|
|
|
fastbootd: Support flashall with logical partitions.
Currently, fastboot flashall will try to flash every partition in the
image list. For devices with logical partitions, this will cause those
partitions to be flashed twice (once via super.img, and again for each
individual image). Additionally, super.img is needed to synchronize the
logical partition list, but it is only generated with "make dist",
making it inconvenient for most workflows.
This patch removes automatic flashing of super.img. Instead, devices
with a super partition must now generate a "super_empty.img"
describing the super geometry and logical partition layout. Before
flashing any other partitions, this image is used to ensure the device
has created all necessary logical partitions.
Additionally, before flashing (but after updating super), all logical
partitions that will be flashed are resized to 0. This ensures that
logical partitions will receive more optimal space allocation.
Finally, during flashing of individual images, logical partitions are
resized to the image size, since they do not have hardcoded sizes in the
build.
Bug: 78793464
Test: fastboot flashall can flash vendor, product, product_services
without a make dist/super.img.
Change-Id: I18c2291e0d0cb10de729c5646b75ac84dde8c871
2018-08-16 22:43:11 +02:00
|
|
|
if (sparse_file* s = sparse_file_import_auto(fd, false, false)) {
|
|
|
|
buf->image_size = sparse_file_len(s, false, false);
|
|
|
|
sparse_file_destroy(s);
|
|
|
|
} else {
|
|
|
|
buf->image_size = sz;
|
|
|
|
}
|
|
|
|
|
2018-10-26 19:34:53 +02:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
2018-06-26 22:38:35 +02:00
|
|
|
int64_t limit = get_sparse_limit(sz);
|
2012-05-25 02:18:41 +02:00
|
|
|
if (limit) {
|
2015-04-08 05:12:50 +02:00
|
|
|
sparse_file** s = load_sparse_files(fd, limit);
|
|
|
|
if (s == nullptr) {
|
2016-05-11 21:39:27 +02:00
|
|
|
return false;
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
2013-06-28 18:54:59 +02:00
|
|
|
buf->type = FB_BUFFER_SPARSE;
|
|
|
|
buf->data = s;
|
2012-05-25 02:18:41 +02:00
|
|
|
} else {
|
2017-04-12 17:25:57 +02:00
|
|
|
buf->type = FB_BUFFER_FD;
|
|
|
|
buf->data = nullptr;
|
|
|
|
buf->fd = fd;
|
2014-05-08 01:31:59 +02:00
|
|
|
buf->sz = sz;
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
|
|
|
|
2016-05-11 21:39:27 +02:00
|
|
|
return true;
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
|
2017-04-12 17:25:57 +02:00
|
|
|
unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
|
|
|
|
|
2016-05-11 21:39:27 +02:00
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
2017-04-12 17:25:57 +02:00
|
|
|
|
|
|
|
struct stat s;
|
|
|
|
if (fstat(fd, &s)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!S_ISREG(s.st_mode)) {
|
|
|
|
errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
return load_buf_fd(fd.release(), buf);
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
|
|
|
|
2017-08-07 20:29:26 +02:00
|
|
|
static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
|
|
|
|
// Buffer needs to be at least the size of the VBMeta struct which
|
|
|
|
// is 256 bytes.
|
|
|
|
if (buf->sz < 256) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-02 23:24:03 +02:00
|
|
|
int fd = make_temporary_fd("vbmeta rewriting");
|
2017-08-07 20:29:26 +02:00
|
|
|
|
|
|
|
std::string data;
|
|
|
|
if (!android::base::ReadFdToString(buf->fd, &data)) {
|
|
|
|
die("Failed reading from vbmeta");
|
|
|
|
}
|
|
|
|
|
|
|
|
// There's a 32-bit big endian |flags| field at offset 120 where
|
|
|
|
// bit 0 corresponds to disable-verity and bit 1 corresponds to
|
|
|
|
// disable-verification.
|
|
|
|
//
|
|
|
|
// See external/avb/libavb/avb_vbmeta_image.h for the layout of
|
|
|
|
// the VBMeta struct.
|
|
|
|
if (g_disable_verity) {
|
|
|
|
data[123] |= 0x01;
|
|
|
|
}
|
|
|
|
if (g_disable_verification) {
|
|
|
|
data[123] |= 0x02;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!android::base::WriteStringToFd(data, fd)) {
|
|
|
|
die("Failed writing to modified vbmeta");
|
|
|
|
}
|
|
|
|
close(buf->fd);
|
|
|
|
buf->fd = fd;
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
2018-03-28 17:20:00 +02:00
|
|
|
static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
|
2013-06-28 18:54:59 +02:00
|
|
|
{
|
2015-03-19 06:47:09 +01:00
|
|
|
sparse_file** s;
|
2013-06-28 18:54:59 +02:00
|
|
|
|
2017-08-07 20:29:26 +02:00
|
|
|
// Rewrite vbmeta if that's what we're flashing and modification has been requested.
|
|
|
|
if ((g_disable_verity || g_disable_verification) &&
|
2018-03-28 17:20:00 +02:00
|
|
|
(partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
|
2017-08-07 20:29:26 +02:00
|
|
|
rewrite_vbmeta_buffer(buf);
|
|
|
|
}
|
|
|
|
|
2013-06-28 18:54:59 +02:00
|
|
|
switch (buf->type) {
|
2016-01-19 23:50:18 +01:00
|
|
|
case FB_BUFFER_SPARSE: {
|
|
|
|
std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
|
2015-03-19 06:47:09 +01:00
|
|
|
s = reinterpret_cast<sparse_file**>(buf->data);
|
2013-06-28 18:54:59 +02:00
|
|
|
while (*s) {
|
2015-04-08 05:12:50 +02:00
|
|
|
int64_t sz = sparse_file_len(*s, true, false);
|
2016-01-19 23:50:18 +01:00
|
|
|
sparse_files.emplace_back(*s, sz);
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sparse_files.size(); ++i) {
|
|
|
|
const auto& pair = sparse_files[i];
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->FlashPartition(partition, pair.first, pair.second, i + 1, sparse_files.size());
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
|
|
|
break;
|
2016-01-19 23:50:18 +01:00
|
|
|
}
|
2017-04-12 17:25:57 +02:00
|
|
|
case FB_BUFFER_FD:
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->FlashPartition(partition, buf->fd, buf->sz);
|
2013-06-28 18:54:59 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
die("unknown buffer type: %d", buf->type);
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
static std::string get_current_slot() {
|
2016-07-01 04:25:31 +02:00
|
|
|
std::string current_slot;
|
2018-09-25 00:48:09 +02:00
|
|
|
if (fb->GetVar("current-slot", ¤t_slot) != fastboot::SUCCESS) return "";
|
2018-04-11 00:32:21 +02:00
|
|
|
return current_slot;
|
2016-07-01 04:25:31 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
static int get_slot_count() {
|
2016-07-01 04:25:31 +02:00
|
|
|
std::string var;
|
2018-04-11 00:32:21 +02:00
|
|
|
int count = 0;
|
2018-09-25 00:48:09 +02:00
|
|
|
if (fb->GetVar("slot-count", &var) != fastboot::SUCCESS ||
|
|
|
|
!android::base::ParseInt(var, &count)) {
|
2018-04-11 00:32:21 +02:00
|
|
|
return 0;
|
2016-07-01 04:25:31 +02:00
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
static bool supports_AB() {
|
|
|
|
return get_slot_count() >= 2;
|
2016-06-29 18:26:44 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 08:03:48 +02:00
|
|
|
// Given a current slot, this returns what the 'other' slot is.
|
2016-07-01 04:25:31 +02:00
|
|
|
static std::string get_other_slot(const std::string& current_slot, int count) {
|
|
|
|
if (count == 0) return "";
|
|
|
|
|
|
|
|
char next = (current_slot[0] - 'a' + 1)%count + 'a';
|
|
|
|
return std::string(1, next);
|
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
static std::string get_other_slot(const std::string& current_slot) {
|
|
|
|
return get_other_slot(current_slot, get_slot_count());
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
static std::string get_other_slot(int count) {
|
|
|
|
return get_other_slot(get_current_slot(), count);
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
static std::string get_other_slot() {
|
|
|
|
return get_other_slot(get_current_slot(), get_slot_count());
|
2016-06-29 18:26:44 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
static std::string verify_slot(const std::string& slot_name, bool allow_all) {
|
2016-07-01 04:25:31 +02:00
|
|
|
std::string slot = slot_name;
|
|
|
|
if (slot == "all") {
|
2015-11-26 00:17:10 +01:00
|
|
|
if (allow_all) {
|
|
|
|
return "all";
|
|
|
|
} else {
|
2018-06-26 22:38:35 +02:00
|
|
|
int count = get_slot_count();
|
2016-07-01 04:25:31 +02:00
|
|
|
if (count > 0) {
|
|
|
|
return "a";
|
2015-11-26 00:17:10 +01:00
|
|
|
} else {
|
2017-10-27 23:21:12 +02:00
|
|
|
die("No known slots");
|
2015-11-26 00:17:10 +01:00
|
|
|
}
|
|
|
|
}
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
2016-01-06 01:54:40 +01:00
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
int count = get_slot_count();
|
2017-10-27 23:21:12 +02:00
|
|
|
if (count == 0) die("Device does not support slots");
|
2016-01-06 01:54:40 +01:00
|
|
|
|
2016-07-01 04:25:31 +02:00
|
|
|
if (slot == "other") {
|
2018-06-26 22:38:35 +02:00
|
|
|
std::string other = get_other_slot( count);
|
2016-06-28 08:03:48 +02:00
|
|
|
if (other == "") {
|
2017-10-27 23:21:12 +02:00
|
|
|
die("No known slots");
|
2016-01-06 01:54:40 +01:00
|
|
|
}
|
2016-06-28 08:03:48 +02:00
|
|
|
return other;
|
2016-01-06 01:54:40 +01:00
|
|
|
}
|
|
|
|
|
2016-07-01 04:25:31 +02:00
|
|
|
if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
|
|
|
|
|
|
|
|
fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
|
|
|
|
for (int i=0; i<count; i++) {
|
|
|
|
fprintf(stderr, "%c\n", (char)(i + 'a'));
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
2016-07-01 04:25:31 +02:00
|
|
|
|
2015-09-15 06:05:41 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
static std::string verify_slot(const std::string& slot) {
|
|
|
|
return verify_slot(slot, true);
|
2015-11-26 00:17:10 +01:00
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
static void do_for_partition(const std::string& part, const std::string& slot,
|
2016-07-28 01:25:51 +02:00
|
|
|
const std::function<void(const std::string&)>& func, bool force_slot) {
|
2015-11-12 01:13:13 +01:00
|
|
|
std::string has_slot;
|
2015-09-15 06:05:41 +02:00
|
|
|
std::string current_slot;
|
|
|
|
|
2018-09-25 00:48:09 +02:00
|
|
|
if (fb->GetVar("has-slot:" + part, &has_slot) != fastboot::SUCCESS) {
|
2015-11-12 01:13:13 +01:00
|
|
|
/* If has-slot is not supported, the answer is no. */
|
|
|
|
has_slot = "no";
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
2015-11-12 01:13:13 +01:00
|
|
|
if (has_slot == "yes") {
|
2016-07-01 04:25:31 +02:00
|
|
|
if (slot == "") {
|
2018-06-26 22:38:35 +02:00
|
|
|
current_slot = get_current_slot();
|
2016-07-01 04:25:31 +02:00
|
|
|
if (current_slot == "") {
|
2017-10-27 23:21:12 +02:00
|
|
|
die("Failed to identify current slot");
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
2016-07-01 04:25:31 +02:00
|
|
|
func(part + "_" + current_slot);
|
2015-09-15 06:05:41 +02:00
|
|
|
} else {
|
2016-07-01 04:25:31 +02:00
|
|
|
func(part + '_' + slot);
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-07-01 04:25:31 +02:00
|
|
|
if (force_slot && slot != "") {
|
2015-10-30 19:22:01 +01:00
|
|
|
fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
|
2016-07-01 04:25:31 +02:00
|
|
|
part.c_str(), slot.c_str());
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
|
|
|
func(part);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
|
|
|
|
* empty, it will use the current slot. If slot is "all", it will return a list of all possible
|
|
|
|
* partition names. If force_slot is true, it will fail if a slot is specified, and the given
|
|
|
|
* partition does not support slots.
|
2015-09-15 06:05:41 +02:00
|
|
|
*/
|
2018-06-26 22:38:35 +02:00
|
|
|
static void do_for_partitions(const std::string& part, const std::string& slot,
|
2016-07-28 01:25:51 +02:00
|
|
|
const std::function<void(const std::string&)>& func, bool force_slot) {
|
2015-11-12 01:13:13 +01:00
|
|
|
std::string has_slot;
|
2015-09-15 06:05:41 +02:00
|
|
|
|
2016-07-01 04:25:31 +02:00
|
|
|
if (slot == "all") {
|
2018-09-25 00:48:09 +02:00
|
|
|
if (fb->GetVar("has-slot:" + part, &has_slot) != fastboot::SUCCESS) {
|
2017-10-27 23:21:12 +02:00
|
|
|
die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
2015-11-12 01:13:13 +01:00
|
|
|
if (has_slot == "yes") {
|
2018-06-26 22:38:35 +02:00
|
|
|
for (int i=0; i < get_slot_count(); i++) {
|
|
|
|
do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-06-26 22:38:35 +02:00
|
|
|
do_for_partition(part, "", func, force_slot);
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-06-26 22:38:35 +02:00
|
|
|
do_for_partition(part, slot, func, force_slot);
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 21:51:25 +02:00
|
|
|
static bool is_logical(const std::string& partition) {
|
|
|
|
std::string value;
|
|
|
|
return fb->GetVar("is-logical:" + partition, &value) == fastboot::SUCCESS && value == "yes";
|
|
|
|
}
|
|
|
|
|
2018-11-29 02:10:42 +01:00
|
|
|
static bool is_retrofit_device() {
|
|
|
|
std::string value;
|
|
|
|
if (fb->GetVar("super-partition-name", &value) != fastboot::SUCCESS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return android::base::StartsWith(value, "system_");
|
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
static void do_flash(const char* pname, const char* fname) {
|
2013-06-28 18:54:59 +02:00
|
|
|
struct fastboot_buffer buf;
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
if (!load_buf(fname, &buf)) {
|
2016-05-11 21:39:27 +02:00
|
|
|
die("cannot load '%s': %s", fname, strerror(errno));
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
2018-10-25 21:51:25 +02:00
|
|
|
if (is_logical(pname)) {
|
|
|
|
fb->ResizePartition(pname, std::to_string(buf.image_size));
|
|
|
|
}
|
2013-06-28 18:54:59 +02:00
|
|
|
flash_buf(pname, &buf);
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 04:43:11 +02:00
|
|
|
// Sets slot_override as the active slot. If slot_override is blank,
|
|
|
|
// set current slot as active instead. This clears slot-unbootable.
|
2018-06-26 22:38:35 +02:00
|
|
|
static void set_active(const std::string& slot_override) {
|
|
|
|
if (!supports_AB()) return;
|
2018-04-11 00:32:21 +02:00
|
|
|
|
2016-07-01 04:25:31 +02:00
|
|
|
if (slot_override != "") {
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->SetActive(slot_override);
|
2016-06-28 04:43:11 +02:00
|
|
|
} else {
|
2018-06-26 22:38:35 +02:00
|
|
|
std::string current_slot = get_current_slot();
|
2016-07-01 04:25:31 +02:00
|
|
|
if (current_slot != "") {
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->SetActive(current_slot);
|
2016-06-28 04:43:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
fastbootd: Support flashall with logical partitions.
Currently, fastboot flashall will try to flash every partition in the
image list. For devices with logical partitions, this will cause those
partitions to be flashed twice (once via super.img, and again for each
individual image). Additionally, super.img is needed to synchronize the
logical partition list, but it is only generated with "make dist",
making it inconvenient for most workflows.
This patch removes automatic flashing of super.img. Instead, devices
with a super partition must now generate a "super_empty.img"
describing the super geometry and logical partition layout. Before
flashing any other partitions, this image is used to ensure the device
has created all necessary logical partitions.
Additionally, before flashing (but after updating super), all logical
partitions that will be flashed are resized to 0. This ensures that
logical partitions will receive more optimal space allocation.
Finally, during flashing of individual images, logical partitions are
resized to the image size, since they do not have hardcoded sizes in the
build.
Bug: 78793464
Test: fastboot flashall can flash vendor, product, product_services
without a make dist/super.img.
Change-Id: I18c2291e0d0cb10de729c5646b75ac84dde8c871
2018-08-16 22:43:11 +02:00
|
|
|
static bool is_userspace_fastboot() {
|
|
|
|
std::string value;
|
2018-09-25 00:48:09 +02:00
|
|
|
return fb->GetVar("is-userspace", &value) == fastboot::SUCCESS && value == "yes";
|
fastbootd: Support flashall with logical partitions.
Currently, fastboot flashall will try to flash every partition in the
image list. For devices with logical partitions, this will cause those
partitions to be flashed twice (once via super.img, and again for each
individual image). Additionally, super.img is needed to synchronize the
logical partition list, but it is only generated with "make dist",
making it inconvenient for most workflows.
This patch removes automatic flashing of super.img. Instead, devices
with a super partition must now generate a "super_empty.img"
describing the super geometry and logical partition layout. Before
flashing any other partitions, this image is used to ensure the device
has created all necessary logical partitions.
Additionally, before flashing (but after updating super), all logical
partitions that will be flashed are resized to 0. This ensures that
logical partitions will receive more optimal space allocation.
Finally, during flashing of individual images, logical partitions are
resized to the image size, since they do not have hardcoded sizes in the
build.
Bug: 78793464
Test: fastboot flashall can flash vendor, product, product_services
without a make dist/super.img.
Change-Id: I18c2291e0d0cb10de729c5646b75ac84dde8c871
2018-08-16 22:43:11 +02:00
|
|
|
}
|
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
static void reboot_to_userspace_fastboot() {
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->RebootTo("fastboot");
|
|
|
|
|
|
|
|
auto* old_transport = fb->set_transport(nullptr);
|
|
|
|
delete old_transport;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
// Give the current connection time to close.
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->set_transport(open_device());
|
2018-12-08 01:39:07 +01:00
|
|
|
|
|
|
|
if (!is_userspace_fastboot()) {
|
|
|
|
die("Failed to boot into userspace fastboot; one or more components might be unbootable.");
|
|
|
|
}
|
2018-08-29 23:15:49 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
class ImageSource {
|
|
|
|
public:
|
2018-09-20 23:45:05 +02:00
|
|
|
virtual bool ReadFile(const std::string& name, std::vector<char>* out) const = 0;
|
2018-08-29 23:15:49 +02:00
|
|
|
virtual int OpenFile(const std::string& name) const = 0;
|
|
|
|
};
|
2016-06-29 18:26:44 +02:00
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
class FlashAllTool {
|
|
|
|
public:
|
|
|
|
FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe);
|
|
|
|
|
|
|
|
void Flash();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void CheckRequirements();
|
|
|
|
void DetermineSecondarySlot();
|
|
|
|
void CollectImages();
|
|
|
|
void FlashImages(const std::vector<std::pair<const Image*, std::string>>& images);
|
|
|
|
void FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf);
|
|
|
|
void UpdateSuperPartition();
|
|
|
|
|
|
|
|
const ImageSource& source_;
|
|
|
|
std::string slot_override_;
|
|
|
|
bool skip_secondary_;
|
|
|
|
bool wipe_;
|
|
|
|
std::string secondary_slot_;
|
|
|
|
std::vector<std::pair<const Image*, std::string>> boot_images_;
|
|
|
|
std::vector<std::pair<const Image*, std::string>> os_images_;
|
|
|
|
};
|
2017-10-27 23:21:12 +02:00
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
FlashAllTool::FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe)
|
|
|
|
: source_(source),
|
|
|
|
slot_override_(slot_override),
|
|
|
|
skip_secondary_(skip_secondary),
|
|
|
|
wipe_(wipe)
|
|
|
|
{
|
|
|
|
}
|
2015-09-15 06:05:41 +02:00
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
void FlashAllTool::Flash() {
|
2018-09-25 00:48:09 +02:00
|
|
|
DumpInfo();
|
2018-08-29 23:15:49 +02:00
|
|
|
CheckRequirements();
|
2018-11-06 00:21:44 +01:00
|
|
|
|
|
|
|
// Change the slot first, so we boot into the correct recovery image when
|
|
|
|
// using fastbootd.
|
|
|
|
if (slot_override_ == "all") {
|
|
|
|
set_active("a");
|
|
|
|
} else {
|
|
|
|
set_active(slot_override_);
|
|
|
|
}
|
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
DetermineSecondarySlot();
|
|
|
|
CollectImages();
|
|
|
|
|
|
|
|
// First flash boot partitions. We allow this to happen either in userspace
|
|
|
|
// or in bootloader fastboot.
|
|
|
|
FlashImages(boot_images_);
|
|
|
|
|
|
|
|
// Sync the super partition. This will reboot to userspace fastboot if needed.
|
|
|
|
UpdateSuperPartition();
|
|
|
|
|
|
|
|
// Resize any logical partition to 0, so each partition is reset to 0
|
|
|
|
// extents, and will achieve more optimal allocation.
|
|
|
|
for (const auto& [image, slot] : os_images_) {
|
|
|
|
auto resize_partition = [](const std::string& partition) -> void {
|
|
|
|
if (is_logical(partition)) {
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->ResizePartition(partition, "0");
|
2018-08-29 23:15:49 +02:00
|
|
|
}
|
2015-09-15 06:05:41 +02:00
|
|
|
};
|
2018-08-29 23:15:49 +02:00
|
|
|
do_for_partitions(image->part_name, slot, resize_partition, false);
|
2012-09-29 23:46:25 +02:00
|
|
|
}
|
2015-03-19 07:12:44 +01:00
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
// Flash OS images, resizing logical partitions as needed.
|
|
|
|
FlashImages(os_images_);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
void FlashAllTool::CheckRequirements() {
|
2018-09-20 23:45:05 +02:00
|
|
|
std::vector<char> contents;
|
|
|
|
if (!source_.ReadFile("android-info.txt", &contents)) {
|
2018-08-29 23:15:49 +02:00
|
|
|
die("could not read android-info.txt");
|
|
|
|
}
|
2018-09-06 00:11:44 +02:00
|
|
|
::CheckRequirements({contents.data(), contents.size()});
|
2018-08-29 23:15:49 +02:00
|
|
|
}
|
2016-04-25 23:31:18 +02:00
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
void FlashAllTool::DetermineSecondarySlot() {
|
|
|
|
if (skip_secondary_) {
|
|
|
|
return;
|
|
|
|
}
|
2018-11-06 00:21:44 +01:00
|
|
|
if (slot_override_ != "" && slot_override_ != "all") {
|
2018-08-29 23:15:49 +02:00
|
|
|
secondary_slot_ = get_other_slot(slot_override_);
|
|
|
|
} else {
|
|
|
|
secondary_slot_ = get_other_slot();
|
|
|
|
}
|
|
|
|
if (secondary_slot_ == "") {
|
|
|
|
if (supports_AB()) {
|
|
|
|
fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
|
|
|
|
}
|
|
|
|
skip_secondary_ = true;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
void FlashAllTool::CollectImages() {
|
|
|
|
for (size_t i = 0; i < arraysize(images); ++i) {
|
|
|
|
std::string slot = slot_override_;
|
|
|
|
if (images[i].IsSecondary()) {
|
|
|
|
if (skip_secondary_) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
slot = secondary_slot_;
|
|
|
|
}
|
|
|
|
if (images[i].type == ImageType::BootCritical) {
|
|
|
|
boot_images_.emplace_back(&images[i], slot);
|
|
|
|
} else if (images[i].type == ImageType::Normal) {
|
|
|
|
os_images_.emplace_back(&images[i], slot);
|
|
|
|
}
|
|
|
|
}
|
fastbootd: Support flashall with logical partitions.
Currently, fastboot flashall will try to flash every partition in the
image list. For devices with logical partitions, this will cause those
partitions to be flashed twice (once via super.img, and again for each
individual image). Additionally, super.img is needed to synchronize the
logical partition list, but it is only generated with "make dist",
making it inconvenient for most workflows.
This patch removes automatic flashing of super.img. Instead, devices
with a super partition must now generate a "super_empty.img"
describing the super geometry and logical partition layout. Before
flashing any other partitions, this image is used to ensure the device
has created all necessary logical partitions.
Additionally, before flashing (but after updating super), all logical
partitions that will be flashed are resized to 0. This ensures that
logical partitions will receive more optimal space allocation.
Finally, during flashing of individual images, logical partitions are
resized to the image size, since they do not have hardcoded sizes in the
build.
Bug: 78793464
Test: fastboot flashall can flash vendor, product, product_services
without a make dist/super.img.
Change-Id: I18c2291e0d0cb10de729c5646b75ac84dde8c871
2018-08-16 22:43:11 +02:00
|
|
|
}
|
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
void FlashAllTool::FlashImages(const std::vector<std::pair<const Image*, std::string>>& images) {
|
|
|
|
for (const auto& [image, slot] : images) {
|
|
|
|
fastboot_buffer buf;
|
|
|
|
int fd = source_.OpenFile(image->img_name);
|
|
|
|
if (fd < 0 || !load_buf_fd(fd, &buf)) {
|
|
|
|
if (image->optional_if_no_image) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
die("could not load '%s': %s", image->img_name, strerror(errno));
|
|
|
|
}
|
|
|
|
FlashImage(*image, slot, &buf);
|
2018-08-28 01:47:32 +02:00
|
|
|
}
|
2018-08-29 23:15:49 +02:00
|
|
|
}
|
2018-08-28 01:47:32 +02:00
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
void FlashAllTool::FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf) {
|
|
|
|
auto flash = [&, this](const std::string& partition_name) {
|
2018-09-20 23:45:05 +02:00
|
|
|
std::vector<char> signature_data;
|
|
|
|
if (source_.ReadFile(image.sig_name, &signature_data)) {
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->Download("signature", signature_data);
|
|
|
|
fb->RawCommand("signature", "installing signature");
|
2018-08-29 23:15:49 +02:00
|
|
|
}
|
2018-08-28 01:47:32 +02:00
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
if (is_logical(partition_name)) {
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->ResizePartition(partition_name, std::to_string(buf->image_size));
|
2018-08-29 23:15:49 +02:00
|
|
|
}
|
|
|
|
flash_buf(partition_name.c_str(), buf);
|
|
|
|
};
|
|
|
|
do_for_partitions(image.part_name, slot, flash, false);
|
2018-08-28 01:47:32 +02:00
|
|
|
}
|
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
void FlashAllTool::UpdateSuperPartition() {
|
|
|
|
int fd = source_.OpenFile("super_empty.img");
|
|
|
|
if (fd < 0) {
|
fastbootd: Support flashall with logical partitions.
Currently, fastboot flashall will try to flash every partition in the
image list. For devices with logical partitions, this will cause those
partitions to be flashed twice (once via super.img, and again for each
individual image). Additionally, super.img is needed to synchronize the
logical partition list, but it is only generated with "make dist",
making it inconvenient for most workflows.
This patch removes automatic flashing of super.img. Instead, devices
with a super partition must now generate a "super_empty.img"
describing the super geometry and logical partition layout. Before
flashing any other partitions, this image is used to ensure the device
has created all necessary logical partitions.
Additionally, before flashing (but after updating super), all logical
partitions that will be flashed are resized to 0. This ensures that
logical partitions will receive more optimal space allocation.
Finally, during flashing of individual images, logical partitions are
resized to the image size, since they do not have hardcoded sizes in the
build.
Bug: 78793464
Test: fastboot flashall can flash vendor, product, product_services
without a make dist/super.img.
Change-Id: I18c2291e0d0cb10de729c5646b75ac84dde8c871
2018-08-16 22:43:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!is_userspace_fastboot()) {
|
2018-08-28 01:47:32 +02:00
|
|
|
reboot_to_userspace_fastboot();
|
fastbootd: Support flashall with logical partitions.
Currently, fastboot flashall will try to flash every partition in the
image list. For devices with logical partitions, this will cause those
partitions to be flashed twice (once via super.img, and again for each
individual image). Additionally, super.img is needed to synchronize the
logical partition list, but it is only generated with "make dist",
making it inconvenient for most workflows.
This patch removes automatic flashing of super.img. Instead, devices
with a super partition must now generate a "super_empty.img"
describing the super geometry and logical partition layout. Before
flashing any other partitions, this image is used to ensure the device
has created all necessary logical partitions.
Additionally, before flashing (but after updating super), all logical
partitions that will be flashed are resized to 0. This ensures that
logical partitions will receive more optimal space allocation.
Finally, during flashing of individual images, logical partitions are
resized to the image size, since they do not have hardcoded sizes in the
build.
Bug: 78793464
Test: fastboot flashall can flash vendor, product, product_services
without a make dist/super.img.
Change-Id: I18c2291e0d0cb10de729c5646b75ac84dde8c871
2018-08-16 22:43:11 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 03:01:32 +01:00
|
|
|
std::string super_name;
|
|
|
|
if (fb->GetVar("super-partition-name", &super_name) != fastboot::RetCode::SUCCESS) {
|
|
|
|
super_name = "super";
|
|
|
|
}
|
|
|
|
fb->Download(super_name, fd, get_file_size(fd));
|
|
|
|
|
|
|
|
std::string command = "update-super:" + super_name;
|
2018-08-29 23:15:49 +02:00
|
|
|
if (wipe_) {
|
fastbootd: Support flashall with logical partitions.
Currently, fastboot flashall will try to flash every partition in the
image list. For devices with logical partitions, this will cause those
partitions to be flashed twice (once via super.img, and again for each
individual image). Additionally, super.img is needed to synchronize the
logical partition list, but it is only generated with "make dist",
making it inconvenient for most workflows.
This patch removes automatic flashing of super.img. Instead, devices
with a super partition must now generate a "super_empty.img"
describing the super geometry and logical partition layout. Before
flashing any other partitions, this image is used to ensure the device
has created all necessary logical partitions.
Additionally, before flashing (but after updating super), all logical
partitions that will be flashed are resized to 0. This ensures that
logical partitions will receive more optimal space allocation.
Finally, during flashing of individual images, logical partitions are
resized to the image size, since they do not have hardcoded sizes in the
build.
Bug: 78793464
Test: fastboot flashall can flash vendor, product, product_services
without a make dist/super.img.
Change-Id: I18c2291e0d0cb10de729c5646b75ac84dde8c871
2018-08-16 22:43:11 +02:00
|
|
|
command += ":wipe";
|
|
|
|
}
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->RawCommand(command, "Updating super partition");
|
2018-11-29 02:10:42 +01:00
|
|
|
|
|
|
|
// Retrofit devices have two super partitions, named super_a and super_b.
|
|
|
|
// On these devices, secondary slots must be flashed as physical
|
|
|
|
// partitions (otherwise they would not mount on first boot). To enforce
|
|
|
|
// this, we delete any logical partitions for the "other" slot.
|
|
|
|
if (is_retrofit_device()) {
|
|
|
|
for (const auto& [image, slot] : os_images_) {
|
|
|
|
std::string partition_name = image->part_name + "_"s + slot;
|
|
|
|
if (image->IsSecondary() && is_logical(partition_name)) {
|
|
|
|
fb->DeletePartition(partition_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
fastbootd: Support flashall with logical partitions.
Currently, fastboot flashall will try to flash every partition in the
image list. For devices with logical partitions, this will cause those
partitions to be flashed twice (once via super.img, and again for each
individual image). Additionally, super.img is needed to synchronize the
logical partition list, but it is only generated with "make dist",
making it inconvenient for most workflows.
This patch removes automatic flashing of super.img. Instead, devices
with a super partition must now generate a "super_empty.img"
describing the super geometry and logical partition layout. Before
flashing any other partitions, this image is used to ensure the device
has created all necessary logical partitions.
Additionally, before flashing (but after updating super), all logical
partitions that will be flashed are resized to 0. This ensures that
logical partitions will receive more optimal space allocation.
Finally, during flashing of individual images, logical partitions are
resized to the image size, since they do not have hardcoded sizes in the
build.
Bug: 78793464
Test: fastboot flashall can flash vendor, product, product_services
without a make dist/super.img.
Change-Id: I18c2291e0d0cb10de729c5646b75ac84dde8c871
2018-08-16 22:43:11 +02:00
|
|
|
}
|
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
class ZipImageSource final : public ImageSource {
|
|
|
|
public:
|
|
|
|
explicit ZipImageSource(ZipArchiveHandle zip) : zip_(zip) {}
|
2018-09-20 23:45:05 +02:00
|
|
|
bool ReadFile(const std::string& name, std::vector<char>* out) const override;
|
2018-08-29 23:15:49 +02:00
|
|
|
int OpenFile(const std::string& name) const override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
ZipArchiveHandle zip_;
|
|
|
|
};
|
|
|
|
|
2018-09-20 23:45:05 +02:00
|
|
|
bool ZipImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
|
|
|
|
return UnzipToMemory(zip_, name, out);
|
2018-08-28 21:14:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
int ZipImageSource::OpenFile(const std::string& name) const {
|
|
|
|
return unzip_to_file(zip_, name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
|
|
|
|
ZipArchiveHandle zip;
|
|
|
|
int error = OpenArchive(filename, &zip);
|
|
|
|
if (error != 0) {
|
|
|
|
die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
|
|
|
|
}
|
2015-03-19 06:47:09 +01:00
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
FlashAllTool tool(ZipImageSource(zip), slot_override, skip_secondary, false);
|
|
|
|
tool.Flash();
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
CloseArchive(zip);
|
|
|
|
}
|
2015-03-19 06:47:09 +01:00
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
class LocalImageSource final : public ImageSource {
|
|
|
|
public:
|
2018-09-20 23:45:05 +02:00
|
|
|
bool ReadFile(const std::string& name, std::vector<char>* out) const override;
|
2018-08-29 23:15:49 +02:00
|
|
|
int OpenFile(const std::string& name) const override;
|
|
|
|
};
|
2015-03-19 06:47:09 +01:00
|
|
|
|
2018-09-20 23:45:05 +02:00
|
|
|
bool LocalImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
|
2018-08-29 23:15:49 +02:00
|
|
|
auto path = find_item_given_name(name);
|
|
|
|
if (path.empty()) {
|
2018-09-20 23:45:05 +02:00
|
|
|
return false;
|
2018-08-28 21:14:33 +02:00
|
|
|
}
|
2018-09-20 23:45:05 +02:00
|
|
|
return ReadFileToVector(path, out);
|
2018-08-29 23:15:49 +02:00
|
|
|
}
|
2018-08-28 21:14:33 +02:00
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
int LocalImageSource::OpenFile(const std::string& name) const {
|
|
|
|
auto path = find_item_given_name(name);
|
2019-04-04 02:57:00 +02:00
|
|
|
return open(path.c_str(), O_RDONLY | O_BINARY);
|
2018-08-29 23:15:49 +02:00
|
|
|
}
|
2018-08-28 21:14:33 +02:00
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
static void do_flashall(const std::string& slot_override, bool skip_secondary, bool wipe) {
|
|
|
|
FlashAllTool tool(LocalImageSource(), slot_override, skip_secondary, wipe);
|
|
|
|
tool.Flash();
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-05-09 03:04:49 +02:00
|
|
|
static std::string next_arg(std::vector<std::string>* args) {
|
|
|
|
if (args->empty()) syntax_error("expected argument");
|
|
|
|
std::string result = args->front();
|
|
|
|
args->erase(args->begin());
|
|
|
|
return result;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-05-16 01:53:53 +02:00
|
|
|
static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
|
2017-05-09 03:04:49 +02:00
|
|
|
if (args->empty()) syntax_error("empty oem command");
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2017-05-16 01:53:53 +02:00
|
|
|
std::string command(cmd);
|
2017-05-09 03:04:49 +02:00
|
|
|
while (!args->empty()) {
|
2017-05-12 00:05:13 +02:00
|
|
|
command += " " + next_arg(args);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->RawCommand(command, "");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-02-06 23:39:31 +01:00
|
|
|
static std::string fb_fix_numeric_var(std::string var) {
|
|
|
|
// Some bootloaders (angler, for example), send spurious leading whitespace.
|
|
|
|
var = android::base::Trim(var);
|
|
|
|
// Some bootloaders (hammerhead, for example) use implicit hex.
|
|
|
|
// This code used to use strtol with base 16.
|
|
|
|
if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
static unsigned fb_get_flash_block_size(std::string name) {
|
2017-02-06 23:39:31 +01:00
|
|
|
std::string sizeString;
|
2018-09-25 00:48:09 +02:00
|
|
|
if (fb->GetVar(name, &sizeString) != fastboot::SUCCESS || sizeString.empty()) {
|
2018-03-28 17:20:00 +02:00
|
|
|
// This device does not report flash block sizes, so return 0.
|
2017-02-06 23:39:31 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sizeString = fb_fix_numeric_var(sizeString);
|
|
|
|
|
|
|
|
unsigned size;
|
|
|
|
if (!android::base::ParseUint(sizeString, &size)) {
|
|
|
|
fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
|
|
|
|
return 0;
|
|
|
|
}
|
2017-11-02 01:37:32 +01:00
|
|
|
if ((size & (size - 1)) != 0) {
|
|
|
|
fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
|
2017-02-06 23:39:31 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
static void fb_perform_format(
|
2018-03-28 17:20:00 +02:00
|
|
|
const std::string& partition, int skip_if_not_supported,
|
2017-05-09 03:04:49 +02:00
|
|
|
const std::string& type_override, const std::string& size_override,
|
2015-11-27 10:29:37 +01:00
|
|
|
const std::string& initial_dir) {
|
2015-10-30 19:49:47 +01:00
|
|
|
std::string partition_type, partition_size;
|
|
|
|
|
2014-03-12 02:28:15 +01:00
|
|
|
struct fastboot_buffer buf;
|
2015-10-30 19:49:47 +01:00
|
|
|
const char* errMsg = nullptr;
|
|
|
|
const struct fs_generator* gen = nullptr;
|
2017-04-19 01:23:18 +02:00
|
|
|
TemporaryFile output;
|
|
|
|
unique_fd fd;
|
2014-03-12 02:28:15 +01:00
|
|
|
|
2015-10-30 19:49:47 +01:00
|
|
|
unsigned int limit = INT_MAX;
|
|
|
|
if (target_sparse_limit > 0 && target_sparse_limit < limit) {
|
2014-03-12 02:28:15 +01:00
|
|
|
limit = target_sparse_limit;
|
2015-10-30 19:49:47 +01:00
|
|
|
}
|
|
|
|
if (sparse_limit > 0 && sparse_limit < limit) {
|
2014-03-12 02:28:15 +01:00
|
|
|
limit = sparse_limit;
|
2015-10-30 19:49:47 +01:00
|
|
|
}
|
2014-03-12 02:28:15 +01:00
|
|
|
|
2018-09-25 00:48:09 +02:00
|
|
|
if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) {
|
2014-03-12 02:28:15 +01:00
|
|
|
errMsg = "Can't determine partition type.\n";
|
|
|
|
goto failed;
|
|
|
|
}
|
2017-05-09 03:04:49 +02:00
|
|
|
if (!type_override.empty()) {
|
2015-10-30 19:49:47 +01:00
|
|
|
if (partition_type != type_override) {
|
|
|
|
fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
|
2018-03-28 17:20:00 +02:00
|
|
|
partition.c_str(), partition_type.c_str(), type_override.c_str());
|
2014-05-07 00:14:15 +02:00
|
|
|
}
|
2015-10-30 19:49:47 +01:00
|
|
|
partition_type = type_override;
|
2014-05-07 00:14:15 +02:00
|
|
|
}
|
2014-03-12 02:28:15 +01:00
|
|
|
|
2018-09-25 00:48:09 +02:00
|
|
|
if (fb->GetVar("partition-size:" + partition, &partition_size) != fastboot::SUCCESS) {
|
2014-03-12 02:28:15 +01:00
|
|
|
errMsg = "Unable to get partition size\n";
|
|
|
|
goto failed;
|
|
|
|
}
|
2017-05-09 03:04:49 +02:00
|
|
|
if (!size_override.empty()) {
|
2015-10-30 19:49:47 +01:00
|
|
|
if (partition_size != size_override) {
|
|
|
|
fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
|
2018-03-28 17:20:00 +02:00
|
|
|
partition.c_str(), partition_size.c_str(), size_override.c_str());
|
2014-05-07 00:14:15 +02:00
|
|
|
}
|
2015-10-30 19:49:47 +01:00
|
|
|
partition_size = size_override;
|
2014-05-07 00:14:15 +02:00
|
|
|
}
|
2017-02-06 23:39:31 +01:00
|
|
|
partition_size = fb_fix_numeric_var(partition_size);
|
2014-03-12 02:28:15 +01:00
|
|
|
|
2015-11-02 23:05:57 +01:00
|
|
|
gen = fs_get_generator(partition_type);
|
2014-03-12 02:28:15 +01:00
|
|
|
if (!gen) {
|
|
|
|
if (skip_if_not_supported) {
|
|
|
|
fprintf(stderr, "Erase successful, but not automatically formatting.\n");
|
2015-10-30 19:49:47 +01:00
|
|
|
fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
|
2014-03-12 02:28:15 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-11-20 20:04:15 +01:00
|
|
|
die("Formatting is not supported for file system with type '%s'.",
|
|
|
|
partition_type.c_str());
|
2014-03-12 02:28:15 +01:00
|
|
|
}
|
|
|
|
|
2015-10-30 19:49:47 +01:00
|
|
|
int64_t size;
|
2016-10-12 02:09:00 +02:00
|
|
|
if (!android::base::ParseInt(partition_size, &size)) {
|
2018-11-20 20:04:15 +01:00
|
|
|
die("Couldn't parse partition size '%s'.", partition_size.c_str());
|
2015-10-30 19:49:47 +01:00
|
|
|
}
|
2014-03-12 02:28:15 +01:00
|
|
|
|
2017-02-06 23:39:31 +01:00
|
|
|
unsigned eraseBlkSize, logicalBlkSize;
|
2018-06-26 22:38:35 +02:00
|
|
|
eraseBlkSize = fb_get_flash_block_size("erase-block-size");
|
|
|
|
logicalBlkSize = fb_get_flash_block_size("logical-block-size");
|
2017-02-06 23:39:31 +01:00
|
|
|
|
2017-04-19 01:23:18 +02:00
|
|
|
if (fs_generator_generate(gen, output.path, size, initial_dir,
|
|
|
|
eraseBlkSize, logicalBlkSize)) {
|
2018-03-28 17:20:00 +02:00
|
|
|
die("Cannot generate image for %s", partition.c_str());
|
2014-03-12 02:28:15 +01:00
|
|
|
}
|
|
|
|
|
2017-04-19 01:23:18 +02:00
|
|
|
fd.reset(open(output.path, O_RDONLY));
|
|
|
|
if (fd == -1) {
|
2018-11-20 20:04:15 +01:00
|
|
|
die("Cannot open generated image: %s", strerror(errno));
|
2017-04-19 01:23:18 +02:00
|
|
|
}
|
2018-06-26 22:38:35 +02:00
|
|
|
if (!load_buf_fd(fd.release(), &buf)) {
|
2018-11-20 20:04:15 +01:00
|
|
|
die("Cannot read image: %s", strerror(errno));
|
2014-03-12 02:28:15 +01:00
|
|
|
}
|
|
|
|
flash_buf(partition, &buf);
|
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
if (skip_if_not_supported) {
|
|
|
|
fprintf(stderr, "Erase successful, but not automatically formatting.\n");
|
2015-10-30 19:49:47 +01:00
|
|
|
if (errMsg) fprintf(stderr, "%s", errMsg);
|
2014-03-12 02:28:15 +01:00
|
|
|
}
|
2018-09-25 00:48:09 +02:00
|
|
|
fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
|
2018-11-20 20:04:15 +01:00
|
|
|
if (!skip_if_not_supported) {
|
|
|
|
die("Command failed");
|
|
|
|
}
|
2014-03-12 02:28:15 +01:00
|
|
|
}
|
|
|
|
|
2018-11-17 00:53:35 +01:00
|
|
|
static bool should_flash_in_userspace(const std::string& partition_name) {
|
2018-11-28 05:19:26 +01:00
|
|
|
if (!get_android_product_out()) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-17 00:53:35 +01:00
|
|
|
auto path = find_item_given_name("super_empty.img");
|
2018-12-05 02:05:47 +01:00
|
|
|
if (path.empty() || access(path.c_str(), R_OK)) {
|
2018-11-17 00:53:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto metadata = android::fs_mgr::ReadFromImageFile(path);
|
|
|
|
if (!metadata) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (const auto& partition : metadata->partitions) {
|
|
|
|
auto candidate = android::fs_mgr::GetPartitionName(partition);
|
|
|
|
if (partition.attributes & LP_PARTITION_ATTR_SLOT_SUFFIXED) {
|
|
|
|
// On retrofit devices, we don't know if, or whether, the A or B
|
|
|
|
// slot has been flashed for dynamic partitions. Instead we add
|
|
|
|
// both names to the list as a conservative guess.
|
|
|
|
if (candidate + "_a" == partition_name || candidate + "_b" == partition_name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (candidate == partition_name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
int FastBootTool::Main(int argc, char* argv[]) {
|
2015-11-12 01:15:30 +01:00
|
|
|
bool wants_wipe = false;
|
|
|
|
bool wants_reboot = false;
|
|
|
|
bool wants_reboot_bootloader = false;
|
2018-07-17 20:14:01 +02:00
|
|
|
bool wants_reboot_recovery = false;
|
|
|
|
bool wants_reboot_fastboot = false;
|
2016-09-26 19:26:21 +02:00
|
|
|
bool skip_reboot = false;
|
2015-11-12 01:15:30 +01:00
|
|
|
bool wants_set_active = false;
|
2016-07-14 05:03:25 +02:00
|
|
|
bool skip_secondary = false;
|
2015-11-27 10:29:37 +01:00
|
|
|
bool set_fbe_marker = false;
|
2018-11-17 00:53:35 +01:00
|
|
|
bool force_flash = false;
|
2014-11-24 11:29:34 +01:00
|
|
|
int longindex;
|
2015-09-15 06:05:41 +02:00
|
|
|
std::string slot_override;
|
2015-11-12 01:15:30 +01:00
|
|
|
std::string next_active;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-04-06 01:12:47 +02:00
|
|
|
g_boot_img_hdr.kernel_addr = 0x00008000;
|
|
|
|
g_boot_img_hdr.ramdisk_addr = 0x01000000;
|
|
|
|
g_boot_img_hdr.second_addr = 0x00f00000;
|
|
|
|
g_boot_img_hdr.tags_addr = 0x00000100;
|
|
|
|
g_boot_img_hdr.page_size = 2048;
|
2019-01-24 21:47:08 +01:00
|
|
|
g_boot_img_hdr.dtb_addr = 0x01100000;
|
2018-04-06 01:12:47 +02:00
|
|
|
|
2013-03-08 02:06:41 +01:00
|
|
|
const struct option longopts[] = {
|
2018-04-06 01:12:47 +02:00
|
|
|
{"base", required_argument, 0, 0},
|
|
|
|
{"cmdline", required_argument, 0, 0},
|
|
|
|
{"disable-verification", no_argument, 0, 0},
|
|
|
|
{"disable-verity", no_argument, 0, 0},
|
2018-11-17 00:53:35 +01:00
|
|
|
{"force", no_argument, 0, 0},
|
2018-04-06 01:12:47 +02:00
|
|
|
{"header-version", required_argument, 0, 0},
|
2015-06-02 22:50:00 +02:00
|
|
|
{"help", no_argument, 0, 'h'},
|
2018-04-06 01:12:47 +02:00
|
|
|
{"kernel-offset", required_argument, 0, 0},
|
|
|
|
{"os-patch-level", required_argument, 0, 0},
|
|
|
|
{"os-version", required_argument, 0, 0},
|
|
|
|
{"page-size", required_argument, 0, 0},
|
|
|
|
{"ramdisk-offset", required_argument, 0, 0},
|
2015-11-12 02:29:37 +01:00
|
|
|
{"set-active", optional_argument, 0, 'a'},
|
2016-09-26 19:26:21 +02:00
|
|
|
{"skip-reboot", no_argument, 0, 0},
|
2018-04-06 01:12:47 +02:00
|
|
|
{"skip-secondary", no_argument, 0, 0},
|
|
|
|
{"slot", required_argument, 0, 0},
|
|
|
|
{"tags-offset", required_argument, 0, 0},
|
2019-01-24 21:47:08 +01:00
|
|
|
{"dtb", required_argument, 0, 0},
|
|
|
|
{"dtb-offset", required_argument, 0, 0},
|
2018-04-06 01:12:47 +02:00
|
|
|
{"unbuffered", no_argument, 0, 0},
|
2018-03-29 23:46:29 +02:00
|
|
|
{"verbose", no_argument, 0, 'v'},
|
|
|
|
{"version", no_argument, 0, 0},
|
2015-11-27 10:29:37 +01:00
|
|
|
#if !defined(_WIN32)
|
|
|
|
{"wipe-and-use-fbe", no_argument, 0, 0},
|
|
|
|
#endif
|
2013-03-08 02:06:41 +01:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
2012-05-23 02:53:34 +02:00
|
|
|
|
|
|
|
serial = getenv("ANDROID_SERIAL");
|
|
|
|
|
2018-04-06 01:12:47 +02:00
|
|
|
int c;
|
2018-04-11 00:38:08 +02:00
|
|
|
while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
|
2018-04-06 01:12:47 +02:00
|
|
|
if (c == 0) {
|
|
|
|
std::string name{longopts[longindex].name};
|
|
|
|
if (name == "base") {
|
|
|
|
g_base_addr = strtoul(optarg, 0, 16);
|
|
|
|
} else if (name == "cmdline") {
|
|
|
|
g_cmdline = optarg;
|
|
|
|
} else if (name == "disable-verification") {
|
|
|
|
g_disable_verification = true;
|
|
|
|
} else if (name == "disable-verity") {
|
|
|
|
g_disable_verity = true;
|
2018-11-17 00:53:35 +01:00
|
|
|
} else if (name == "force") {
|
|
|
|
force_flash = true;
|
2018-04-06 01:12:47 +02:00
|
|
|
} else if (name == "header-version") {
|
|
|
|
g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
|
2019-01-24 21:47:08 +01:00
|
|
|
} else if (name == "dtb") {
|
|
|
|
g_dtb_path = optarg;
|
2018-04-06 01:12:47 +02:00
|
|
|
} else if (name == "kernel-offset") {
|
|
|
|
g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
|
|
|
|
} else if (name == "os-patch-level") {
|
2018-04-10 23:22:13 +02:00
|
|
|
ParseOsPatchLevel(&g_boot_img_hdr, optarg);
|
2018-04-06 01:12:47 +02:00
|
|
|
} else if (name == "os-version") {
|
2018-04-10 23:22:13 +02:00
|
|
|
ParseOsVersion(&g_boot_img_hdr, optarg);
|
2018-04-06 01:12:47 +02:00
|
|
|
} else if (name == "page-size") {
|
|
|
|
g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
|
|
|
|
if (g_boot_img_hdr.page_size == 0) die("invalid page size");
|
|
|
|
} else if (name == "ramdisk-offset") {
|
|
|
|
g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
|
|
|
|
} else if (name == "skip-reboot") {
|
|
|
|
skip_reboot = true;
|
|
|
|
} else if (name == "skip-secondary") {
|
|
|
|
skip_secondary = true;
|
|
|
|
} else if (name == "slot") {
|
|
|
|
slot_override = optarg;
|
2019-01-24 21:47:08 +01:00
|
|
|
} else if (name == "dtb-offset") {
|
|
|
|
g_boot_img_hdr.dtb_addr = strtoul(optarg, 0, 16);
|
2018-04-06 01:12:47 +02:00
|
|
|
} else if (name == "tags-offset") {
|
|
|
|
g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
|
|
|
|
} else if (name == "unbuffered") {
|
2015-04-08 05:12:50 +02:00
|
|
|
setvbuf(stdout, nullptr, _IONBF, 0);
|
|
|
|
setvbuf(stderr, nullptr, _IONBF, 0);
|
2018-04-06 01:12:47 +02:00
|
|
|
} else if (name == "version") {
|
2018-08-29 23:58:02 +02:00
|
|
|
fprintf(stdout, "fastboot version %s-%s\n", PLATFORM_TOOLS_VERSION, android::build::GetBuildNumber().c_str());
|
2017-03-31 00:08:28 +02:00
|
|
|
fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
|
2015-06-02 22:50:00 +02:00
|
|
|
return 0;
|
2015-11-27 10:29:37 +01:00
|
|
|
#if !defined(_WIN32)
|
2018-04-06 01:12:47 +02:00
|
|
|
} else if (name == "wipe-and-use-fbe") {
|
2015-11-27 10:29:37 +01:00
|
|
|
wants_wipe = true;
|
|
|
|
set_fbe_marker = true;
|
|
|
|
#endif
|
|
|
|
} else {
|
2018-04-02 23:24:03 +02:00
|
|
|
die("unknown option %s", longopts[longindex].name);
|
2014-11-24 11:29:34 +01:00
|
|
|
}
|
2018-04-06 01:12:47 +02:00
|
|
|
} else {
|
|
|
|
switch (c) {
|
|
|
|
case 'a':
|
|
|
|
wants_set_active = true;
|
|
|
|
if (optarg) next_active = optarg;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
return show_help();
|
|
|
|
case 'l':
|
|
|
|
g_long_listing = true;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
serial = optarg;
|
|
|
|
break;
|
|
|
|
case 'S':
|
2018-04-20 04:49:44 +02:00
|
|
|
if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
|
|
|
|
die("invalid sparse limit %s", optarg);
|
|
|
|
}
|
2018-04-06 01:12:47 +02:00
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
set_verbose();
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
wants_wipe = true;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
2012-05-23 02:53:34 +02:00
|
|
|
}
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2012-05-23 02:53:34 +02:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2017-05-09 03:04:49 +02:00
|
|
|
if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2012-07-25 01:36:41 +02:00
|
|
|
if (argc > 0 && !strcmp(*argv, "devices")) {
|
2009-03-04 04:32:55 +01:00
|
|
|
list_devices();
|
2011-02-26 03:38:53 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-30 03:17:06 +02:00
|
|
|
if (argc > 0 && !strcmp(*argv, "help")) {
|
2017-05-09 03:04:49 +02:00
|
|
|
return show_help();
|
2012-08-30 03:17:06 +02:00
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
Transport* transport = open_device();
|
2016-01-20 17:32:08 +01:00
|
|
|
if (transport == nullptr) {
|
|
|
|
return 1;
|
|
|
|
}
|
2018-09-25 00:48:09 +02:00
|
|
|
fastboot::DriverCallbacks driver_callbacks = {
|
|
|
|
.prolog = Status,
|
|
|
|
.epilog = Epilog,
|
|
|
|
.info = InfoMessage,
|
|
|
|
};
|
|
|
|
fastboot::FastBootDriver fastboot_driver(transport, driver_callbacks, false);
|
|
|
|
fb = &fastboot_driver;
|
2016-01-20 17:32:08 +01:00
|
|
|
|
2018-03-28 17:20:00 +02:00
|
|
|
const double start = now();
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
if (slot_override != "") slot_override = verify_slot(slot_override);
|
|
|
|
if (next_active != "") next_active = verify_slot(next_active, false);
|
2015-11-12 01:15:30 +01:00
|
|
|
|
|
|
|
if (wants_set_active) {
|
|
|
|
if (next_active == "") {
|
|
|
|
if (slot_override == "") {
|
2016-06-28 04:43:11 +02:00
|
|
|
std::string current_slot;
|
2018-09-25 00:48:09 +02:00
|
|
|
if (fb->GetVar("current-slot", ¤t_slot) == fastboot::SUCCESS) {
|
2018-06-26 22:38:35 +02:00
|
|
|
next_active = verify_slot(current_slot, false);
|
2016-06-28 04:43:11 +02:00
|
|
|
} else {
|
|
|
|
wants_set_active = false;
|
|
|
|
}
|
2015-11-12 01:15:30 +01:00
|
|
|
} else {
|
2018-06-26 22:38:35 +02:00
|
|
|
next_active = verify_slot(slot_override, false);
|
2015-11-12 01:15:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-08 00:38:53 +02:00
|
|
|
|
2017-05-09 03:04:49 +02:00
|
|
|
std::vector<std::string> args(argv, argv + argc);
|
|
|
|
while (!args.empty()) {
|
|
|
|
std::string command = next_arg(&args);
|
2012-09-29 23:46:25 +02:00
|
|
|
|
2018-08-29 19:44:33 +02:00
|
|
|
if (command == FB_CMD_GETVAR) {
|
2017-05-09 03:04:49 +02:00
|
|
|
std::string variable = next_arg(&args);
|
2018-09-25 00:48:09 +02:00
|
|
|
DisplayVarOrError(variable, variable);
|
2018-08-29 19:44:33 +02:00
|
|
|
} else if (command == FB_CMD_ERASE) {
|
2017-05-09 03:04:49 +02:00
|
|
|
std::string partition = next_arg(&args);
|
|
|
|
auto erase = [&](const std::string& partition) {
|
2015-10-30 19:22:01 +01:00
|
|
|
std::string partition_type;
|
2018-09-25 00:48:09 +02:00
|
|
|
if (fb->GetVar("partition-type:" + partition, &partition_type) == fastboot::SUCCESS &&
|
2015-09-15 06:05:41 +02:00
|
|
|
fs_get_generator(partition_type) != nullptr) {
|
|
|
|
fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
|
|
|
|
partition_type.c_str());
|
|
|
|
}
|
2012-09-29 23:46:25 +02:00
|
|
|
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->Erase(partition);
|
2015-09-15 06:05:41 +02:00
|
|
|
};
|
2018-06-26 22:38:35 +02:00
|
|
|
do_for_partitions(partition, slot_override, erase, true);
|
2017-05-09 03:04:49 +02:00
|
|
|
} else if (android::base::StartsWith(command, "format")) {
|
|
|
|
// Parsing for: "format[:[type][:[size]]]"
|
|
|
|
// Some valid things:
|
|
|
|
// - select only the size, and leave default fs type:
|
|
|
|
// format::0x4000000 userdata
|
|
|
|
// - default fs type and size:
|
|
|
|
// format userdata
|
|
|
|
// format:: userdata
|
|
|
|
std::vector<std::string> pieces = android::base::Split(command, ":");
|
|
|
|
std::string type_override;
|
|
|
|
if (pieces.size() > 1) type_override = pieces[1].c_str();
|
|
|
|
std::string size_override;
|
|
|
|
if (pieces.size() > 2) size_override = pieces[2].c_str();
|
|
|
|
|
|
|
|
std::string partition = next_arg(&args);
|
|
|
|
|
|
|
|
auto format = [&](const std::string& partition) {
|
2018-06-26 22:38:35 +02:00
|
|
|
fb_perform_format(partition, 0, type_override, size_override, "");
|
2015-09-15 06:05:41 +02:00
|
|
|
};
|
2019-03-26 20:04:05 +01:00
|
|
|
do_for_partitions(partition, slot_override, format, true);
|
2017-05-09 03:04:49 +02:00
|
|
|
} else if (command == "signature") {
|
|
|
|
std::string filename = next_arg(&args);
|
2018-09-20 23:45:05 +02:00
|
|
|
std::vector<char> data;
|
|
|
|
if (!ReadFileToVector(filename, &data)) {
|
|
|
|
die("could not load '%s': %s", filename.c_str(), strerror(errno));
|
|
|
|
}
|
|
|
|
if (data.size() != 256) die("signature must be 256 bytes (got %zu)", data.size());
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->Download("signature", data);
|
|
|
|
fb->RawCommand("signature", "installing signature");
|
2018-08-29 19:44:33 +02:00
|
|
|
} else if (command == FB_CMD_REBOOT) {
|
2015-11-12 01:15:30 +01:00
|
|
|
wants_reboot = true;
|
2017-05-09 03:04:49 +02:00
|
|
|
|
|
|
|
if (args.size() == 1) {
|
|
|
|
std::string what = next_arg(&args);
|
|
|
|
if (what == "bootloader") {
|
2015-11-12 01:15:30 +01:00
|
|
|
wants_reboot = false;
|
|
|
|
wants_reboot_bootloader = true;
|
2018-07-17 20:14:01 +02:00
|
|
|
} else if (what == "recovery") {
|
|
|
|
wants_reboot = false;
|
|
|
|
wants_reboot_recovery = true;
|
|
|
|
} else if (what == "fastboot") {
|
|
|
|
wants_reboot = false;
|
|
|
|
wants_reboot_fastboot = true;
|
2017-05-09 03:04:49 +02:00
|
|
|
} else {
|
|
|
|
syntax_error("unknown reboot target %s", what.c_str());
|
2017-01-04 04:39:13 +01:00
|
|
|
}
|
2017-05-09 03:04:49 +02:00
|
|
|
|
2015-02-25 19:02:00 +01:00
|
|
|
}
|
2017-05-09 03:04:49 +02:00
|
|
|
if (!args.empty()) syntax_error("junk after reboot command");
|
2018-08-29 19:44:33 +02:00
|
|
|
} else if (command == FB_CMD_REBOOT_BOOTLOADER) {
|
2015-11-12 01:15:30 +01:00
|
|
|
wants_reboot_bootloader = true;
|
2018-08-29 19:44:33 +02:00
|
|
|
} else if (command == FB_CMD_REBOOT_RECOVERY) {
|
2018-07-17 20:14:01 +02:00
|
|
|
wants_reboot_recovery = true;
|
2018-08-29 19:44:33 +02:00
|
|
|
} else if (command == FB_CMD_REBOOT_FASTBOOT) {
|
2018-07-17 20:14:01 +02:00
|
|
|
wants_reboot_fastboot = true;
|
2018-08-29 19:44:33 +02:00
|
|
|
} else if (command == FB_CMD_CONTINUE) {
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->Continue();
|
2018-08-29 19:44:33 +02:00
|
|
|
} else if (command == FB_CMD_BOOT) {
|
2017-05-09 03:04:49 +02:00
|
|
|
std::string kernel = next_arg(&args);
|
|
|
|
std::string ramdisk;
|
|
|
|
if (!args.empty()) ramdisk = next_arg(&args);
|
|
|
|
std::string second_stage;
|
|
|
|
if (!args.empty()) second_stage = next_arg(&args);
|
2018-09-20 23:45:05 +02:00
|
|
|
auto data = LoadBootableImage(kernel, ramdisk, second_stage);
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->Download("boot.img", data);
|
|
|
|
fb->Boot();
|
2018-08-29 19:44:33 +02:00
|
|
|
} else if (command == FB_CMD_FLASH) {
|
2017-05-09 03:04:49 +02:00
|
|
|
std::string pname = next_arg(&args);
|
|
|
|
|
2016-04-25 23:31:18 +02:00
|
|
|
std::string fname;
|
2017-05-09 03:04:49 +02:00
|
|
|
if (!args.empty()) {
|
|
|
|
fname = next_arg(&args);
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2017-05-04 07:43:23 +02:00
|
|
|
fname = find_item(pname);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2017-05-09 03:04:49 +02:00
|
|
|
if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
|
2015-09-15 06:05:41 +02:00
|
|
|
|
|
|
|
auto flash = [&](const std::string &partition) {
|
2018-11-17 00:53:35 +01:00
|
|
|
if (should_flash_in_userspace(partition) && !is_userspace_fastboot() &&
|
|
|
|
!force_flash) {
|
|
|
|
die("The partition you are trying to flash is dynamic, and "
|
|
|
|
"should be flashed via fastbootd. Please run:\n"
|
|
|
|
"\n"
|
|
|
|
" fastboot reboot fastboot\n"
|
|
|
|
"\n"
|
|
|
|
"And try again. If you are intentionally trying to "
|
|
|
|
"overwrite a fixed partition, use --force.");
|
|
|
|
}
|
2018-06-26 22:38:35 +02:00
|
|
|
do_flash(partition.c_str(), fname.c_str());
|
2015-09-15 06:05:41 +02:00
|
|
|
};
|
2019-03-26 20:04:05 +01:00
|
|
|
do_for_partitions(pname, slot_override, flash, true);
|
2017-05-09 03:04:49 +02:00
|
|
|
} else if (command == "flash:raw") {
|
|
|
|
std::string partition = next_arg(&args);
|
|
|
|
std::string kernel = next_arg(&args);
|
|
|
|
std::string ramdisk;
|
|
|
|
if (!args.empty()) ramdisk = next_arg(&args);
|
|
|
|
std::string second_stage;
|
|
|
|
if (!args.empty()) second_stage = next_arg(&args);
|
|
|
|
|
2018-09-20 23:45:05 +02:00
|
|
|
auto data = LoadBootableImage(kernel, ramdisk, second_stage);
|
|
|
|
auto flashraw = [&data](const std::string& partition) {
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->FlashPartition(partition, data);
|
2015-09-15 06:05:41 +02:00
|
|
|
};
|
2018-06-26 22:38:35 +02:00
|
|
|
do_for_partitions(partition, slot_override, flashraw, true);
|
2017-05-09 03:04:49 +02:00
|
|
|
} else if (command == "flashall") {
|
2016-06-29 18:26:44 +02:00
|
|
|
if (slot_override == "all") {
|
2016-07-14 05:03:25 +02:00
|
|
|
fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
|
fastbootd: Support flashall with logical partitions.
Currently, fastboot flashall will try to flash every partition in the
image list. For devices with logical partitions, this will cause those
partitions to be flashed twice (once via super.img, and again for each
individual image). Additionally, super.img is needed to synchronize the
logical partition list, but it is only generated with "make dist",
making it inconvenient for most workflows.
This patch removes automatic flashing of super.img. Instead, devices
with a super partition must now generate a "super_empty.img"
describing the super geometry and logical partition layout. Before
flashing any other partitions, this image is used to ensure the device
has created all necessary logical partitions.
Additionally, before flashing (but after updating super), all logical
partitions that will be flashed are resized to 0. This ensures that
logical partitions will receive more optimal space allocation.
Finally, during flashing of individual images, logical partitions are
resized to the image size, since they do not have hardcoded sizes in the
build.
Bug: 78793464
Test: fastboot flashall can flash vendor, product, product_services
without a make dist/super.img.
Change-Id: I18c2291e0d0cb10de729c5646b75ac84dde8c871
2018-08-16 22:43:11 +02:00
|
|
|
do_flashall(slot_override, true, wants_wipe);
|
2016-06-29 18:26:44 +02:00
|
|
|
} else {
|
fastbootd: Support flashall with logical partitions.
Currently, fastboot flashall will try to flash every partition in the
image list. For devices with logical partitions, this will cause those
partitions to be flashed twice (once via super.img, and again for each
individual image). Additionally, super.img is needed to synchronize the
logical partition list, but it is only generated with "make dist",
making it inconvenient for most workflows.
This patch removes automatic flashing of super.img. Instead, devices
with a super partition must now generate a "super_empty.img"
describing the super geometry and logical partition layout. Before
flashing any other partitions, this image is used to ensure the device
has created all necessary logical partitions.
Additionally, before flashing (but after updating super), all logical
partitions that will be flashed are resized to 0. This ensures that
logical partitions will receive more optimal space allocation.
Finally, during flashing of individual images, logical partitions are
resized to the image size, since they do not have hardcoded sizes in the
build.
Bug: 78793464
Test: fastboot flashall can flash vendor, product, product_services
without a make dist/super.img.
Change-Id: I18c2291e0d0cb10de729c5646b75ac84dde8c871
2018-08-16 22:43:11 +02:00
|
|
|
do_flashall(slot_override, skip_secondary, wants_wipe);
|
2016-06-29 18:26:44 +02:00
|
|
|
}
|
2015-11-12 01:15:30 +01:00
|
|
|
wants_reboot = true;
|
2017-05-09 03:04:49 +02:00
|
|
|
} else if (command == "update") {
|
2016-07-14 05:03:25 +02:00
|
|
|
bool slot_all = (slot_override == "all");
|
|
|
|
if (slot_all) {
|
|
|
|
fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
|
|
|
|
}
|
2017-05-09 03:04:49 +02:00
|
|
|
std::string filename = "update.zip";
|
|
|
|
if (!args.empty()) {
|
|
|
|
filename = next_arg(&args);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2018-06-26 22:38:35 +02:00
|
|
|
do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
|
2016-09-26 19:26:21 +02:00
|
|
|
wants_reboot = true;
|
2018-08-29 19:44:33 +02:00
|
|
|
} else if (command == FB_CMD_SET_ACTIVE) {
|
2018-06-26 22:38:35 +02:00
|
|
|
std::string slot = verify_slot(next_arg(&args), false);
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->SetActive(slot);
|
2017-05-09 03:04:49 +02:00
|
|
|
} else if (command == "stage") {
|
|
|
|
std::string filename = next_arg(&args);
|
|
|
|
|
2017-01-27 04:20:53 +01:00
|
|
|
struct fastboot_buffer buf;
|
2018-06-26 22:38:35 +02:00
|
|
|
if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
|
2017-05-09 03:04:49 +02:00
|
|
|
die("cannot load '%s'", filename.c_str());
|
2017-01-27 04:20:53 +01:00
|
|
|
}
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->Download(filename, buf.fd, buf.sz);
|
2017-05-09 03:04:49 +02:00
|
|
|
} else if (command == "get_staged") {
|
|
|
|
std::string filename = next_arg(&args);
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->Upload(filename);
|
2018-08-29 19:44:33 +02:00
|
|
|
} else if (command == FB_CMD_OEM) {
|
|
|
|
do_oem_command(FB_CMD_OEM, &args);
|
2017-05-09 03:04:49 +02:00
|
|
|
} else if (command == "flashing") {
|
|
|
|
if (args.empty()) {
|
|
|
|
syntax_error("missing 'flashing' command");
|
|
|
|
} else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
|
|
|
|
args[0] == "unlock_critical" ||
|
|
|
|
args[0] == "lock_critical" ||
|
2018-04-04 22:36:44 +02:00
|
|
|
args[0] == "get_unlock_ability")) {
|
2017-05-16 01:53:53 +02:00
|
|
|
do_oem_command("flashing", &args);
|
2015-05-16 01:43:47 +02:00
|
|
|
} else {
|
2017-05-09 03:04:49 +02:00
|
|
|
syntax_error("unknown 'flashing' command %s", args[0].c_str());
|
2015-05-16 01:43:47 +02:00
|
|
|
}
|
2018-08-29 19:44:33 +02:00
|
|
|
} else if (command == FB_CMD_CREATE_PARTITION) {
|
2018-07-31 22:27:37 +02:00
|
|
|
std::string partition = next_arg(&args);
|
|
|
|
std::string size = next_arg(&args);
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->CreatePartition(partition, size);
|
2018-08-29 19:44:33 +02:00
|
|
|
} else if (command == FB_CMD_DELETE_PARTITION) {
|
2018-07-31 22:27:37 +02:00
|
|
|
std::string partition = next_arg(&args);
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->DeletePartition(partition);
|
2018-08-29 19:44:33 +02:00
|
|
|
} else if (command == FB_CMD_RESIZE_PARTITION) {
|
2018-07-31 22:27:37 +02:00
|
|
|
std::string partition = next_arg(&args);
|
|
|
|
std::string size = next_arg(&args);
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->ResizePartition(partition, size);
|
2019-01-15 23:38:20 +01:00
|
|
|
} else if (command == "gsi") {
|
2019-01-29 22:09:49 +01:00
|
|
|
std::string arg = next_arg(&args);
|
|
|
|
if (arg == "wipe") {
|
2019-01-15 23:38:20 +01:00
|
|
|
fb->RawCommand("gsi:wipe", "wiping GSI");
|
2019-01-29 22:09:49 +01:00
|
|
|
} else if (arg == "disable") {
|
2019-01-15 23:38:20 +01:00
|
|
|
fb->RawCommand("gsi:disable", "disabling GSI");
|
|
|
|
} else {
|
|
|
|
syntax_error("expected 'wipe' or 'disable'");
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2017-05-09 03:04:49 +02:00
|
|
|
syntax_error("unknown command %s", command.c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wants_wipe) {
|
2018-04-25 02:06:30 +02:00
|
|
|
std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
|
|
|
|
for (const auto& partition : partitions) {
|
|
|
|
std::string partition_type;
|
2018-09-25 00:48:09 +02:00
|
|
|
if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-25 02:06:30 +02:00
|
|
|
if (partition_type.empty()) continue;
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->Erase(partition);
|
2018-04-25 02:06:30 +02:00
|
|
|
if (partition == "userdata" && set_fbe_marker) {
|
|
|
|
fprintf(stderr, "setting FBE marker on initial userdata...\n");
|
|
|
|
std::string initial_userdata_dir = create_fbemarker_tmpdir();
|
2018-06-26 22:38:35 +02:00
|
|
|
fb_perform_format(partition, 1, "", "", initial_userdata_dir);
|
2018-04-25 02:06:30 +02:00
|
|
|
delete_fbemarker_tmpdir(initial_userdata_dir);
|
|
|
|
} else {
|
2018-06-26 22:38:35 +02:00
|
|
|
fb_perform_format(partition, 1, "", "", "");
|
2018-04-25 02:06:30 +02:00
|
|
|
}
|
2015-10-30 19:49:47 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-11-12 01:15:30 +01:00
|
|
|
if (wants_set_active) {
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->SetActive(next_active);
|
2015-11-12 01:15:30 +01:00
|
|
|
}
|
2016-09-26 19:26:21 +02:00
|
|
|
if (wants_reboot && !skip_reboot) {
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->Reboot();
|
|
|
|
fb->WaitForDisconnect();
|
2009-03-04 04:32:55 +01:00
|
|
|
} else if (wants_reboot_bootloader) {
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->RebootTo("bootloader");
|
|
|
|
fb->WaitForDisconnect();
|
2018-07-17 20:14:01 +02:00
|
|
|
} else if (wants_reboot_recovery) {
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->RebootTo("recovery");
|
|
|
|
fb->WaitForDisconnect();
|
2018-07-17 20:14:01 +02:00
|
|
|
} else if (wants_reboot_fastboot) {
|
2018-12-08 01:39:07 +01:00
|
|
|
reboot_to_userspace_fastboot();
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-03-28 17:20:00 +02:00
|
|
|
fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
|
2018-09-04 23:32:54 +02:00
|
|
|
|
2018-09-25 00:48:09 +02:00
|
|
|
auto* old_transport = fb->set_transport(nullptr);
|
|
|
|
delete old_transport;
|
|
|
|
|
2018-08-30 06:36:28 +02:00
|
|
|
return 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2018-04-10 23:22:13 +02:00
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
|
2018-04-10 23:22:13 +02:00
|
|
|
unsigned year, month, day;
|
|
|
|
if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
|
|
|
|
syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
|
|
|
|
}
|
|
|
|
if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
|
|
|
|
if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
|
|
|
|
hdr->SetOsPatchLevel(year, month);
|
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
|
2018-04-10 23:22:13 +02:00
|
|
|
unsigned major = 0, minor = 0, patch = 0;
|
|
|
|
std::vector<std::string> versions = android::base::Split(arg, ".");
|
|
|
|
if (versions.size() < 1 || versions.size() > 3 ||
|
|
|
|
(versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
|
|
|
|
(versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
|
|
|
|
(versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
|
|
|
|
(major > 0x7f || minor > 0x7f || patch > 0x7f)) {
|
|
|
|
syntax_error("bad OS version: %s", arg);
|
|
|
|
}
|
|
|
|
hdr->SetOsVersion(major, minor, patch);
|
|
|
|
}
|