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>
|
2023-01-31 22:07:53 +01:00
|
|
|
#include <fstream>
|
2015-09-15 06:05:41 +02:00
|
|
|
#include <functional>
|
2023-01-31 22:07:53 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
2018-09-06 00:11:44 +02:00
|
|
|
#include <regex>
|
2023-01-31 22:07:53 +01:00
|
|
|
#include <sstream>
|
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
|
|
|
|
2019-09-16 05:23:55 +02:00
|
|
|
#include <android-base/endian.h>
|
2016-09-01 00:07:18 +02:00
|
|
|
#include <android-base/file.h>
|
2023-01-28 01:15:52 +01:00
|
|
|
#include <android-base/logging.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>
|
2019-09-16 05:23:55 +02:00
|
|
|
#include <libavb/libavb.h>
|
2018-11-17 00:53:35 +01:00
|
|
|
#include <liblp/liblp.h>
|
fastboot: Avoid reboots to userspace when using flashall/update.
Reboots to fastbootd (userspace fastboot) take a long time, particularly
due to the orange AVB screen and the likelihood of devices having uart
enabled. For "flashall", there is rarely a need to actually go into
userspace, because all of super is getting thrown away. We can just
flash super in the bootloader.
In the past we didn't do this because computing super.img is expensive -
both in terms of time (due to reading dependent images) and in terms of
space (it's easily over 5GB).
But we don't actually need to fully compute super.img. We can build a
sparse_file containing the metadata/headers, with additional references
to each image file containing partition data. Liblp provides the API to
do that, and here, we simply need to translate the layout to libsparse.
On Pixel, this reduces flashall time by around 35-50 seconds, or around
20% of total time, depending on whether uart is in use.
There are some caveats, in which case we'll fall back to normal
fastbootd. This does not work on non-A/B devices, on retrofit dynamic
partition devices (Pixel 3), and in some other edge-casey scenarios. If
it fails, -v will add logging information about why.
Bue: 266982466
Test: fastboot flashall on Pixel 5+
Change-Id: Ie040da597d739faa7f834202184cec8f8e412076
2023-01-28 06:11:11 +01:00
|
|
|
#include <liblp/super_layout_builder.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"
|
2023-02-11 01:02:22 +01:00
|
|
|
#include "storage.h"
|
fastboot: Avoid reboots to userspace when using flashall/update.
Reboots to fastbootd (userspace fastboot) take a long time, particularly
due to the orange AVB screen and the likelihood of devices having uart
enabled. For "flashall", there is rarely a need to actually go into
userspace, because all of super is getting thrown away. We can just
flash super in the bootloader.
In the past we didn't do this because computing super.img is expensive -
both in terms of time (due to reading dependent images) and in terms of
space (it's easily over 5GB).
But we don't actually need to fully compute super.img. We can build a
sparse_file containing the metadata/headers, with additional references
to each image file containing partition data. Liblp provides the API to
do that, and here, we simply need to translate the layout to libsparse.
On Pixel, this reduces flashall time by around 35-50 seconds, or around
20% of total time, depending on whether uart is in use.
There are some caveats, in which case we'll fall back to normal
fastbootd. This does not work on non-A/B devices, on retrofit dynamic
partition devices (Pixel 3), and in some other edge-casey scenarios. If
it fails, -v will add logging information about why.
Bue: 266982466
Test: fastboot flashall on Pixel 5+
Change-Id: Ie040da597d739faa7f834202184cec8f8e412076
2023-01-28 06:11:11 +01:00
|
|
|
#include "super_flash_helper.h"
|
2023-01-31 00:29:50 +01:00
|
|
|
#include "task.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"
|
2021-02-23 00:00:15 +01:00
|
|
|
#include "vendor_boot_img_utils.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2021-02-17 04:37:32 +01:00
|
|
|
using android::base::borrowed_fd;
|
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;
|
2021-02-17 04:37:32 +01:00
|
|
|
using namespace std::placeholders;
|
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;
|
|
|
|
|
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;
|
2023-01-27 20:57:27 +01:00
|
|
|
std::vector<SparsePtr> files;
|
2015-04-08 05:12:50 +02:00
|
|
|
int64_t sz;
|
2021-02-23 00:06:27 +01:00
|
|
|
unique_fd 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
|
|
|
};
|
|
|
|
|
2022-11-30 03:25:31 +01:00
|
|
|
static std::vector<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 },
|
2022-01-05 01:20:15 +01:00
|
|
|
{ "init_boot",
|
|
|
|
"init_boot.img", "init_boot.sig",
|
|
|
|
"init_boot",
|
2022-01-11 03:01:26 +01:00
|
|
|
true, ImageType::BootCritical },
|
2022-11-30 03:25:31 +01:00
|
|
|
{ "", "boot_other.img", "boot.sig", "boot", true, ImageType::Normal },
|
2018-08-28 22:54:03 +02:00
|
|
|
{ "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 },
|
2020-07-16 02:07:11 +02:00
|
|
|
{ "odm_dlkm", "odm_dlkm.img", "odm_dlkm.sig", "odm_dlkm", true, ImageType::Normal },
|
2018-08-28 22:54:03 +02:00
|
|
|
{ "product", "product.img", "product.sig", "product", true, ImageType::Normal },
|
2021-01-15 15:37:15 +01:00
|
|
|
{ "pvmfw", "pvmfw.img", "pvmfw.sig", "pvmfw", true, ImageType::BootCritical },
|
2018-08-28 22:54:03 +02:00
|
|
|
{ "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 },
|
2021-12-06 03:28:28 +01:00
|
|
|
{ "system_dlkm",
|
|
|
|
"system_dlkm.img", "system_dlkm.sig",
|
|
|
|
"system_dlkm",
|
|
|
|
true, ImageType::Normal },
|
2019-06-26 02:18:03 +02:00
|
|
|
{ "system_ext",
|
|
|
|
"system_ext.img", "system_ext.sig",
|
|
|
|
"system_ext",
|
|
|
|
true, ImageType::Normal },
|
2022-11-30 03:25:31 +01:00
|
|
|
{ "", "system_other.img", "system.sig", "system", true, ImageType::Normal },
|
2018-08-28 22:54:03 +02:00
|
|
|
{ "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 },
|
2021-06-03 08:39:34 +02:00
|
|
|
{ "vbmeta_vendor",
|
|
|
|
"vbmeta_vendor.img",
|
|
|
|
"vbmeta_vendor.sig",
|
|
|
|
"vbmeta_vendor",
|
|
|
|
true, ImageType::BootCritical },
|
2018-08-28 22:54:03 +02:00
|
|
|
{ "vendor", "vendor.img", "vendor.sig", "vendor", true, ImageType::Normal },
|
2019-08-21 20:16:14 +02:00
|
|
|
{ "vendor_boot",
|
|
|
|
"vendor_boot.img", "vendor_boot.sig",
|
|
|
|
"vendor_boot",
|
|
|
|
true, ImageType::BootCritical },
|
2020-05-12 19:38:53 +02:00
|
|
|
{ "vendor_dlkm",
|
|
|
|
"vendor_dlkm.img", "vendor_dlkm.sig",
|
|
|
|
"vendor_dlkm",
|
|
|
|
true, ImageType::Normal },
|
2022-03-28 15:57:58 +02:00
|
|
|
{ "vendor_kernel_boot",
|
|
|
|
"vendor_kernel_boot.img",
|
|
|
|
"vendor_kernel_boot.sig",
|
|
|
|
"vendor_kernel_boot",
|
|
|
|
true, ImageType::BootCritical },
|
2022-11-30 03:25:31 +01:00
|
|
|
{ "", "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
|
|
|
}
|
|
|
|
|
2023-01-31 22:07:53 +01:00
|
|
|
std::string find_item(const std::string& item) {
|
2022-11-30 03:25:31 +01:00
|
|
|
for (size_t i = 0; i < images.size(); ++i) {
|
|
|
|
if (!images[i].nickname.empty() && 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) {
|
2020-06-10 18:29:25 +02:00
|
|
|
if (!message.empty()) {
|
|
|
|
static constexpr char kStatusFormat[] = "%-50s ";
|
|
|
|
fprintf(stderr, kStatusFormat, message.c_str());
|
|
|
|
}
|
2018-09-25 00:48:09 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2022-12-30 12:51:54 +01:00
|
|
|
static void TextMessage(const std::string& text) {
|
|
|
|
fprintf(stderr, "%s", text.c_str());
|
|
|
|
}
|
|
|
|
|
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 &&
|
2022-11-30 03:25:31 +01:00
|
|
|
strcmp(local_serial, info->device_path) != 0))
|
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-11 01:02:22 +01:00
|
|
|
static ifc_match_func match_fastboot(const char* local_serial = serial) {
|
|
|
|
return [local_serial](usb_ifc_info* info) -> int {
|
|
|
|
return match_fastboot_with_serial(info, local_serial);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// output compatible with "adb devices"
|
|
|
|
static void PrintDevice(const char* local_serial, const char* status = nullptr,
|
|
|
|
const char* details = nullptr) {
|
|
|
|
if (local_serial == nullptr || strlen(local_serial) == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_long_listing) {
|
|
|
|
printf("%-22s", local_serial);
|
|
|
|
} else {
|
|
|
|
printf("%s\t", local_serial);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status != nullptr && strlen(status) > 0) {
|
|
|
|
printf(" %s", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_long_listing) {
|
|
|
|
if (details != nullptr && strlen(details) > 0) {
|
|
|
|
printf(" %s", details);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
putchar('\n');
|
2013-03-08 02:06:41 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2020-05-28 06:39:37 +02:00
|
|
|
std::string interface = info->interface;
|
|
|
|
if (interface.empty()) {
|
|
|
|
interface = "fastboot";
|
|
|
|
}
|
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 = "????????????";
|
|
|
|
}
|
2023-02-11 01:02:22 +01:00
|
|
|
|
|
|
|
PrintDevice(serial.c_str(), interface.c_str(), info->device_path);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-02-11 01:02:22 +01:00
|
|
|
struct NetworkSerial {
|
|
|
|
Socket::Protocol protocol;
|
|
|
|
std::string address;
|
|
|
|
int port;
|
|
|
|
};
|
|
|
|
|
|
|
|
static Result<NetworkSerial> ParseNetworkSerial(const std::string& serial) {
|
|
|
|
const auto serial_parsed = android::base::Tokenize(serial, ":");
|
|
|
|
const auto parsed_segments_count = serial_parsed.size();
|
|
|
|
if (parsed_segments_count != 2 && parsed_segments_count != 3) {
|
|
|
|
return Error() << "invalid network address: " << serial << ". Expected format:\n"
|
|
|
|
<< "<protocol>:<address>:<port> (tcp:localhost:5554)";
|
|
|
|
}
|
|
|
|
|
|
|
|
Socket::Protocol protocol;
|
|
|
|
if (serial_parsed[0] == "tcp") {
|
|
|
|
protocol = Socket::Protocol::kTcp;
|
|
|
|
} else if (serial_parsed[0] == "udp") {
|
|
|
|
protocol = Socket::Protocol::kUdp;
|
|
|
|
} else {
|
|
|
|
return Error() << "invalid network address: " << serial << ". Expected format:\n"
|
|
|
|
<< "<protocol>:<address>:<port> (tcp:localhost:5554)";
|
|
|
|
}
|
|
|
|
|
|
|
|
int port = 5554;
|
|
|
|
if (parsed_segments_count == 3) {
|
|
|
|
android::base::ParseInt(serial_parsed[2], &port, 5554);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NetworkSerial{protocol, serial_parsed[1], port};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Opens a new Transport connected to the particular device.
|
|
|
|
// arguments:
|
2016-01-20 17:32:08 +01:00
|
|
|
//
|
2023-02-11 01:02:22 +01:00
|
|
|
// local_serial - device to connect (can be a network or usb serial name)
|
|
|
|
// wait_for_device - flag indicates whether we need to wait for device
|
|
|
|
// announce - flag indicates whether we need to print error to stdout in case
|
|
|
|
// we cannot connect to the device
|
2016-01-20 17:32:08 +01:00
|
|
|
//
|
|
|
|
// 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.
|
2023-02-11 01:02:22 +01:00
|
|
|
static Transport* open_device(const char* local_serial, bool wait_for_device = true,
|
|
|
|
bool announce = true) {
|
|
|
|
const Result<NetworkSerial> network_serial = ParseNetworkSerial(local_serial);
|
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) {
|
2023-02-11 01:02:22 +01:00
|
|
|
if (network_serial.ok()) {
|
2016-01-20 17:32:08 +01:00
|
|
|
std::string error;
|
2023-02-11 01:02:22 +01:00
|
|
|
if (network_serial->protocol == Socket::Protocol::kTcp) {
|
|
|
|
transport = tcp::Connect(network_serial->address, network_serial->port, &error)
|
|
|
|
.release();
|
|
|
|
} else if (network_serial->protocol == Socket::Protocol::kUdp) {
|
|
|
|
transport = udp::Connect(network_serial->address, network_serial->port, &error)
|
|
|
|
.release();
|
2016-02-06 00:35:09 +01:00
|
|
|
}
|
|
|
|
|
2016-01-20 17:32:08 +01:00
|
|
|
if (transport == nullptr && announce) {
|
2023-02-11 01:02:22 +01:00
|
|
|
LOG(ERROR) << "error: " << error;
|
2016-01-20 17:32:08 +01:00
|
|
|
}
|
|
|
|
} else {
|
2023-02-11 01:02:22 +01:00
|
|
|
transport = usb_open(match_fastboot(local_serial));
|
2016-01-20 17:32:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (transport != nullptr) {
|
|
|
|
return transport;
|
|
|
|
}
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2023-02-11 01:02:22 +01:00
|
|
|
if (!wait_for_device) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (announce) {
|
2016-01-20 17:32:08 +01:00
|
|
|
announce = false;
|
2023-02-11 01:02:22 +01:00
|
|
|
LOG(ERROR) << "< waiting for " << local_serial << ">";
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-11 01:02:22 +01:00
|
|
|
static Transport* NetworkDeviceConnected(bool print = false) {
|
|
|
|
Transport* transport = nullptr;
|
|
|
|
Transport* result = nullptr;
|
|
|
|
|
|
|
|
ConnectedDevicesStorage storage;
|
|
|
|
std::set<std::string> devices;
|
|
|
|
{
|
|
|
|
FileLock lock = storage.Lock();
|
|
|
|
devices = storage.ReadDevices(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const std::string& device : devices) {
|
|
|
|
transport = open_device(device.c_str(), false, false);
|
|
|
|
|
|
|
|
if (print) {
|
2023-02-15 16:34:58 +01:00
|
|
|
PrintDevice(device.c_str(), transport == nullptr ? "offline" : "fastboot");
|
2023-02-11 01:02:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (transport != nullptr) {
|
|
|
|
result = transport;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detects the fastboot connected device to open a new Transport.
|
|
|
|
// Detecting logic:
|
|
|
|
//
|
|
|
|
// if serial is provided - try to connect to this particular usb/network device
|
|
|
|
// othervise:
|
|
|
|
// 1. Check connected usb devices and return the last connected one
|
|
|
|
// 2. Check connected network devices and return the last connected one
|
|
|
|
// 2. If nothing is connected - wait for any device by repeating p. 1 and 2
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
static Transport* open_device() {
|
|
|
|
if (serial != nullptr) {
|
|
|
|
return open_device(serial);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool announce = true;
|
|
|
|
Transport* transport = nullptr;
|
|
|
|
while (true) {
|
|
|
|
transport = usb_open(match_fastboot(nullptr));
|
|
|
|
if (transport != nullptr) {
|
|
|
|
return transport;
|
|
|
|
}
|
|
|
|
|
|
|
|
transport = NetworkDeviceConnected();
|
|
|
|
if (transport != nullptr) {
|
|
|
|
return transport;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (announce) {
|
|
|
|
announce = false;
|
|
|
|
LOG(ERROR) << "< waiting for any device >";
|
|
|
|
}
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int Connect(int argc, char* argv[]) {
|
|
|
|
if (argc != 1) {
|
|
|
|
LOG(FATAL) << "connect command requires to receive only 1 argument. Usage:" << std::endl
|
|
|
|
<< "fastboot connect [tcp:|udp:host:port]";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* local_serial = *argv;
|
|
|
|
EXPECT(ParseNetworkSerial(local_serial));
|
|
|
|
|
|
|
|
const Transport* transport = open_device(local_serial, false);
|
|
|
|
if (transport == nullptr) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectedDevicesStorage storage;
|
|
|
|
{
|
|
|
|
FileLock lock = storage.Lock();
|
|
|
|
std::set<std::string> devices = storage.ReadDevices(lock);
|
|
|
|
devices.insert(local_serial);
|
|
|
|
storage.WriteDevices(lock, devices);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int Disconnect(const char* local_serial) {
|
|
|
|
EXPECT(ParseNetworkSerial(local_serial));
|
|
|
|
|
|
|
|
ConnectedDevicesStorage storage;
|
|
|
|
{
|
|
|
|
FileLock lock = storage.Lock();
|
|
|
|
std::set<std::string> devices = storage.ReadDevices(lock);
|
|
|
|
devices.erase(local_serial);
|
|
|
|
storage.WriteDevices(lock, devices);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int Disconnect() {
|
|
|
|
ConnectedDevicesStorage storage;
|
|
|
|
{
|
|
|
|
FileLock lock = storage.Lock();
|
|
|
|
storage.Clear(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int Disconnect(int argc, char* argv[]) {
|
|
|
|
switch (argc) {
|
|
|
|
case 0: {
|
|
|
|
return Disconnect();
|
|
|
|
}
|
|
|
|
case 1: {
|
|
|
|
return Disconnect(*argv);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
LOG(FATAL) << "disconnect command can receive only 0 or 1 arguments. Usage:"
|
|
|
|
<< std::endl
|
|
|
|
<< "fastboot disconnect # disconnect all devices" << std::endl
|
|
|
|
<< "fastboot disconnect [tcp:|udp:host:port] # disconnect device";
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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);
|
2023-02-11 01:02:22 +01:00
|
|
|
NetworkDeviceConnected(/* print */ true);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2023-01-31 22:07:53 +01:00
|
|
|
void syntax_error(const char* fmt, ...) {
|
2017-05-09 03:04:49 +02:00
|
|
|
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"
|
2019-06-28 07:15:29 +02:00
|
|
|
" wipe-super [SUPER_EMPTY] Wipe the super partition. This will reset it to\n"
|
|
|
|
" contain an empty set of default dynamic partitions.\n"
|
2021-04-12 19:16:43 +02:00
|
|
|
" create-logical-partition NAME SIZE\n"
|
|
|
|
" Create a logical partition with the given name and\n"
|
|
|
|
" size, in the super partition.\n"
|
|
|
|
" delete-logical-partition NAME\n"
|
|
|
|
" Delete a logical partition with the given name.\n"
|
|
|
|
" resize-logical-partition NAME SIZE\n"
|
|
|
|
" Change the size of the named logical partition.\n"
|
2019-10-22 01:45:59 +02:00
|
|
|
" snapshot-update cancel On devices that support snapshot-based updates, cancel\n"
|
|
|
|
" an in-progress update. This may make the device\n"
|
|
|
|
" unbootable until it is reflashed.\n"
|
2019-11-01 02:02:41 +01:00
|
|
|
" snapshot-update merge On devices that support snapshot-based updates, finish\n"
|
|
|
|
" an in-progress update if it is in the \"merging\"\n"
|
|
|
|
" phase.\n"
|
2021-06-14 14:16:06 +02:00
|
|
|
" fetch PARTITION OUT_FILE Fetch a partition image from the device."
|
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"
|
2020-11-09 17:54:13 +01:00
|
|
|
" --fs-options=OPTION[,OPTION]\n"
|
|
|
|
" Enable filesystem features. OPTION supports casefold, projid, compress\n"
|
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
|
|
|
);
|
2021-02-17 04:37:32 +01:00
|
|
|
// clang-format on
|
2017-05-09 03:04:49 +02:00
|
|
|
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?
|
2020-03-18 22:55:06 +01:00
|
|
|
if (kernel_data.size() < sizeof(boot_img_hdr_v3)) {
|
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()) {
|
2020-03-18 22:55:06 +01:00
|
|
|
if (g_boot_img_hdr.header_version != 2) {
|
2022-11-30 03:25:31 +01:00
|
|
|
die("Argument dtb not supported for boot image header version %d\n",
|
|
|
|
g_boot_img_hdr.header_version);
|
2019-01-24 21:47:08 +01:00
|
|
|
}
|
|
|
|
if (!ReadFileToVector(g_dtb_path, &dtb_data)) {
|
|
|
|
die("cannot load '%s': %s", g_dtb_path.c_str(), strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 03:25:31 +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;
|
2021-03-24 18:14:52 +01:00
|
|
|
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
|
|
|
|
2021-03-24 18:14:52 +01:00
|
|
|
if (!g_cmdline.empty()) {
|
|
|
|
bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v2*>(out.data()), g_cmdline);
|
|
|
|
}
|
2018-09-20 23:45:05 +02:00
|
|
|
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) {
|
2020-09-15 23:26:17 +02:00
|
|
|
ZipEntry64 zip_entry;
|
2019-05-04 07:38:44 +02:00
|
|
|
if (FindEntry(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
|
|
|
}
|
|
|
|
|
2020-09-15 23:26:17 +02:00
|
|
|
if (zip_entry.uncompressed_length > std::numeric_limits<size_t>::max()) {
|
2022-11-30 03:25:31 +01:00
|
|
|
die("entry '%s' is too large: %" PRIu64, entry_name.c_str(), zip_entry.uncompressed_length);
|
2020-09-15 23:26:17 +02: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
|
|
|
|
2022-11-30 03:25:31 +01:00
|
|
|
int error =
|
|
|
|
ExtractToMemory(zip, &zip_entry, reinterpret_cast<uint8_t*>(out->data()), out->size());
|
2018-09-20 23:45:05 +02:00
|
|
|
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
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-11-30 03:25:31 +01: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
|
|
|
|
|
2021-02-23 00:06:27 +01:00
|
|
|
static unique_fd 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
|
|
|
|
2020-09-15 23:26:17 +02:00
|
|
|
ZipEntry64 zip_entry;
|
2019-05-04 07:38:44 +02:00
|
|
|
if (FindEntry(zip, entry_name, &zip_entry) != 0) {
|
2015-03-19 19:44:32 +01:00
|
|
|
fprintf(stderr, "archive does not contain '%s'\n", entry_name);
|
2018-08-29 23:15:49 +02:00
|
|
|
errno = ENOENT;
|
2021-02-23 00:06:27 +01:00
|
|
|
return unique_fd();
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
|
|
|
|
2020-09-15 23:26:17 +02:00
|
|
|
fprintf(stderr, "extracting %s (%" PRIu64 " 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();
|
2021-03-23 00:39:13 +01:00
|
|
|
int error = ExtractEntryToFile(zip, &zip_entry, fd.get());
|
2015-03-19 19:44:32 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-23 00:39:13 +01:00
|
|
|
if (lseek(fd.get(), 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);
|
|
|
|
|
2021-02-23 00:06:27 +01:00
|
|
|
return fd;
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
|
|
|
|
2021-04-06 21:48:27 +02:00
|
|
|
static bool CheckRequirement(const std::string& cur_product, const std::string& var,
|
2018-09-06 00:11:44 +02:00
|
|
|
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));
|
2021-04-06 21:48:27 +02:00
|
|
|
return true;
|
2018-09-06 00:11:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2022-11-30 03:25:31 +01:00
|
|
|
fprintf(stderr, "Could not getvar for '%s' (%s)\n\n", var.c_str(), fb->Error().c_str());
|
2021-04-06 21:48:27 +02:00
|
|
|
return false;
|
2018-09-06 00:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool match = false;
|
|
|
|
for (const auto& option : options) {
|
2022-11-30 03:25:31 +01:00
|
|
|
if (option == var_value ||
|
|
|
|
(option.back() == '*' &&
|
|
|
|
!var_value.compare(0, option.length() - 1, option, 0, option.length() - 1))) {
|
2018-09-06 00:11:44 +02:00
|
|
|
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));
|
2021-04-06 21:48:27 +02:00
|
|
|
return true;
|
2018-09-06 00:11:44 +02:00
|
|
|
}
|
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");
|
2021-04-06 21:48:27 +02:00
|
|
|
return false;
|
2018-09-06 00:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2022-11-30 03:25:31 +01:00
|
|
|
for (size_t i = 0; i < images.size(); ++i) {
|
|
|
|
if (!images[i].nickname.empty() && images[i].nickname == partition_name) {
|
2018-09-06 00:11:44 +02:00
|
|
|
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
|
|
|
|
2021-04-06 21:48:27 +02:00
|
|
|
static void CheckRequirements(const std::string& data, bool force_flash) {
|
2018-09-06 00:11:44 +02:00
|
|
|
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 {
|
2021-04-06 21:48:27 +02:00
|
|
|
bool met = CheckRequirement(cur_product, name, product, invert, options);
|
|
|
|
if (!met) {
|
|
|
|
if (!force_flash) {
|
2022-11-30 03:25:31 +01:00
|
|
|
die("requirements not met!");
|
2021-04-06 21:48:27 +02:00
|
|
|
} else {
|
2022-11-30 03:25:31 +01:00
|
|
|
fprintf(stderr, "requirements not met! but proceeding due to --force\n");
|
2021-04-06 21:48:27 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-01-28 05:39:06 +01:00
|
|
|
static std::vector<SparsePtr> resparse_file(sparse_file* s, int64_t max_size) {
|
2018-04-24 19:54:21 +02:00
|
|
|
if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
|
2022-11-30 03:25:31 +01:00
|
|
|
die("invalid max size %" PRId64, max_size);
|
2018-04-24 19:54:21 +02:00
|
|
|
}
|
|
|
|
|
2023-01-28 05:39:06 +01:00
|
|
|
const 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
|
|
|
|
2023-01-27 20:57:27 +01:00
|
|
|
auto temp = std::make_unique<sparse_file*[]>(files);
|
2023-01-28 05:39:06 +01:00
|
|
|
const int rv = sparse_file_resparse(s, max_size, temp.get(), files);
|
2023-01-27 20:57:27 +01:00
|
|
|
if (rv < 0) die("Failed to resparse");
|
2012-05-25 02:18:41 +02:00
|
|
|
|
2023-01-27 20:57:27 +01:00
|
|
|
std::vector<SparsePtr> out_s;
|
|
|
|
for (int i = 0; i < files; i++) {
|
|
|
|
out_s.emplace_back(temp[i], sparse_file_destroy);
|
|
|
|
}
|
2012-05-25 02:18:41 +02:00
|
|
|
return out_s;
|
|
|
|
}
|
|
|
|
|
2023-01-28 05:39:06 +01:00
|
|
|
static std::vector<SparsePtr> load_sparse_files(int fd, int64_t max_size) {
|
|
|
|
SparsePtr s(sparse_file_import_auto(fd, false, true), sparse_file_destroy);
|
|
|
|
if (!s) die("cannot sparse read file");
|
|
|
|
|
|
|
|
return resparse_file(s.get(), max_size);
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:37:32 +01:00
|
|
|
static uint64_t get_uint_var(const char* var_name) {
|
|
|
|
std::string value_str;
|
|
|
|
if (fb->GetVar(var_name, &value_str) != fastboot::SUCCESS || value_str.empty()) {
|
|
|
|
verbose("target didn't report %s", var_name);
|
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.
|
2021-02-17 04:37:32 +01:00
|
|
|
value_str = android::base::Trim(value_str);
|
2015-11-03 00:51:12 +01:00
|
|
|
|
2021-02-17 04:37:32 +01:00
|
|
|
uint64_t value;
|
|
|
|
if (!android::base::ParseUint(value_str, &value)) {
|
|
|
|
fprintf(stderr, "couldn't parse %s '%s'\n", var_name, value_str.c_str());
|
2015-10-30 19:49:47 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2021-02-17 04:37:32 +01:00
|
|
|
if (value > 0) verbose("target reported %s of %" PRId64 " bytes", var_name, value);
|
|
|
|
return value;
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2021-02-17 04:37:32 +01:00
|
|
|
target_sparse_limit = static_cast<int64_t>(get_uint_var("max-download-size"));
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-23 00:06:27 +01:00
|
|
|
static bool load_buf_fd(unique_fd 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
|
|
|
|
2021-03-23 00:39:13 +01:00
|
|
|
if (sparse_file* s = sparse_file_import(fd.get(), false, false)) {
|
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
|
|
|
buf->image_size = sparse_file_len(s, false, false);
|
|
|
|
sparse_file_destroy(s);
|
|
|
|
} else {
|
|
|
|
buf->image_size = sz;
|
|
|
|
}
|
|
|
|
|
2021-03-23 00:39:13 +01:00
|
|
|
lseek(fd.get(), 0, SEEK_SET);
|
2018-06-26 22:38:35 +02:00
|
|
|
int64_t limit = get_sparse_limit(sz);
|
2021-03-23 00:55:39 +01:00
|
|
|
buf->fd = std::move(fd);
|
2012-05-25 02:18:41 +02:00
|
|
|
if (limit) {
|
2023-01-27 20:57:27 +01:00
|
|
|
buf->files = load_sparse_files(buf->fd.get(), limit);
|
|
|
|
if (buf->files.empty()) {
|
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;
|
2012-05-25 02:18:41 +02:00
|
|
|
} else {
|
2017-04-12 17:25:57 +02:00
|
|
|
buf->type = FB_BUFFER_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) {
|
2023-01-30 18:43:00 +01:00
|
|
|
auto path = find_item_given_name(fname);
|
|
|
|
fd = unique_fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_BINARY)));
|
|
|
|
if (fd == -1) return false;
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
2017-04-12 17:25:57 +02:00
|
|
|
|
|
|
|
struct stat s;
|
2021-03-23 00:39:13 +01:00
|
|
|
if (fstat(fd.get(), &s)) {
|
2017-04-12 17:25:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!S_ISREG(s.st_mode)) {
|
|
|
|
errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-23 00:06:27 +01:00
|
|
|
return load_buf_fd(std::move(fd), buf);
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
|
|
|
|
2019-09-16 05:23:55 +02:00
|
|
|
static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf, bool vbmeta_in_boot) {
|
2017-08-07 20:29:26 +02:00
|
|
|
// Buffer needs to be at least the size of the VBMeta struct which
|
|
|
|
// is 256 bytes.
|
|
|
|
if (buf->sz < 256) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string data;
|
|
|
|
if (!android::base::ReadFdToString(buf->fd, &data)) {
|
|
|
|
die("Failed reading from vbmeta");
|
|
|
|
}
|
|
|
|
|
2019-09-16 05:23:55 +02:00
|
|
|
uint64_t vbmeta_offset = 0;
|
|
|
|
if (vbmeta_in_boot) {
|
|
|
|
// Tries to locate top-level vbmeta from boot.img footer.
|
|
|
|
uint64_t footer_offset = buf->sz - AVB_FOOTER_SIZE;
|
|
|
|
if (0 != data.compare(footer_offset, AVB_FOOTER_MAGIC_LEN, AVB_FOOTER_MAGIC)) {
|
2021-08-10 10:32:37 +02:00
|
|
|
die("Failed to find AVB_FOOTER at offset: %" PRId64 ", is BOARD_AVB_ENABLE true?",
|
|
|
|
footer_offset);
|
2019-09-16 05:23:55 +02:00
|
|
|
}
|
|
|
|
const AvbFooter* footer = reinterpret_cast<const AvbFooter*>(data.c_str() + footer_offset);
|
|
|
|
vbmeta_offset = be64toh(footer->vbmeta_offset);
|
|
|
|
}
|
|
|
|
// Ensures there is AVB_MAGIC at vbmeta_offset.
|
|
|
|
if (0 != data.compare(vbmeta_offset, AVB_MAGIC_LEN, AVB_MAGIC)) {
|
|
|
|
die("Failed to find AVB_MAGIC at offset: %" PRId64, vbmeta_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Rewriting vbmeta struct at offset: %" PRId64 "\n", vbmeta_offset);
|
|
|
|
|
2017-08-07 20:29:26 +02:00
|
|
|
// 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.
|
2019-09-16 05:23:55 +02:00
|
|
|
uint64_t flags_offset = 123 + vbmeta_offset;
|
2017-08-07 20:29:26 +02:00
|
|
|
if (g_disable_verity) {
|
2019-09-16 05:23:55 +02:00
|
|
|
data[flags_offset] |= 0x01;
|
2017-08-07 20:29:26 +02:00
|
|
|
}
|
|
|
|
if (g_disable_verification) {
|
2019-09-16 05:23:55 +02:00
|
|
|
data[flags_offset] |= 0x02;
|
2017-08-07 20:29:26 +02:00
|
|
|
}
|
|
|
|
|
2021-02-23 00:06:27 +01:00
|
|
|
unique_fd fd(make_temporary_fd("vbmeta rewriting"));
|
2017-08-07 20:29:26 +02:00
|
|
|
if (!android::base::WriteStringToFd(data, fd)) {
|
|
|
|
die("Failed writing to modified vbmeta");
|
|
|
|
}
|
2021-02-23 00:06:27 +01:00
|
|
|
buf->fd = std::move(fd);
|
2021-03-23 00:39:13 +01:00
|
|
|
lseek(buf->fd.get(), 0, SEEK_SET);
|
2017-08-07 20:29:26 +02:00
|
|
|
}
|
|
|
|
|
2019-09-16 05:23:55 +02:00
|
|
|
static bool has_vbmeta_partition() {
|
|
|
|
std::string partition_type;
|
|
|
|
return fb->GetVar("partition-type:vbmeta", &partition_type) == fastboot::SUCCESS ||
|
|
|
|
fb->GetVar("partition-type:vbmeta_a", &partition_type) == fastboot::SUCCESS ||
|
|
|
|
fb->GetVar("partition-type:vbmeta_b", &partition_type) == fastboot::SUCCESS;
|
|
|
|
}
|
|
|
|
|
2023-01-28 05:39:06 +01:00
|
|
|
// Note: this only works in userspace fastboot. In the bootloader, use
|
|
|
|
// should_flash_in_userspace().
|
2020-11-06 16:58:28 +01:00
|
|
|
static bool is_logical(const std::string& partition) {
|
|
|
|
std::string value;
|
|
|
|
return fb->GetVar("is-logical:" + partition, &value) == fastboot::SUCCESS && value == "yes";
|
|
|
|
}
|
|
|
|
|
2020-06-19 02:02:15 +02: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;
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:37:32 +01:00
|
|
|
static uint64_t get_partition_size(const std::string& partition) {
|
2020-06-19 02:02:15 +02:00
|
|
|
std::string partition_size_str;
|
|
|
|
if (fb->GetVar("partition-size:" + partition, &partition_size_str) != fastboot::SUCCESS) {
|
2020-11-06 16:58:28 +01:00
|
|
|
if (!is_logical(partition)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-02-17 22:51:54 +01:00
|
|
|
die("cannot get partition size for %s", partition.c_str());
|
2020-06-19 02:02:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
partition_size_str = fb_fix_numeric_var(partition_size_str);
|
2021-02-17 04:37:32 +01:00
|
|
|
uint64_t partition_size;
|
|
|
|
if (!android::base::ParseUint(partition_size_str, &partition_size)) {
|
2020-11-06 16:58:28 +01:00
|
|
|
if (!is_logical(partition)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-06-19 02:02:15 +02:00
|
|
|
die("Couldn't parse partition size '%s'.", partition_size_str.c_str());
|
|
|
|
}
|
2021-02-17 04:37:32 +01:00
|
|
|
return partition_size;
|
|
|
|
}
|
|
|
|
|
2022-01-07 17:33:46 +01:00
|
|
|
static void copy_avb_footer(const std::string& partition, struct fastboot_buffer* buf) {
|
2021-02-17 04:37:32 +01:00
|
|
|
if (buf->sz < AVB_FOOTER_SIZE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:32:37 +02:00
|
|
|
// If overflows and negative, it should be < buf->sz.
|
|
|
|
int64_t partition_size = static_cast<int64_t>(get_partition_size(partition));
|
|
|
|
|
|
|
|
if (partition_size == buf->sz) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Some device bootloaders might not implement `fastboot getvar partition-size:boot[_a|_b]`.
|
|
|
|
// In this case, partition_size will be zero.
|
|
|
|
if (partition_size < buf->sz) {
|
|
|
|
fprintf(stderr,
|
2022-01-07 17:33:46 +01:00
|
|
|
"Warning: skip copying %s image avb footer"
|
|
|
|
" (%s partition size: %" PRId64 ", %s image size: %" PRId64 ").\n",
|
|
|
|
partition.c_str(), partition.c_str(), partition_size, partition.c_str(), buf->sz);
|
2021-08-10 10:32:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IMPORTANT: after the following read, we need to reset buf->fd before return (if not die).
|
|
|
|
// Because buf->fd will still be used afterwards.
|
2020-06-19 02:02:15 +02:00
|
|
|
std::string data;
|
|
|
|
if (!android::base::ReadFdToString(buf->fd, &data)) {
|
2022-01-07 17:33:46 +01:00
|
|
|
die("Failed reading from %s", partition.c_str());
|
2020-06-19 02:02:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t footer_offset = buf->sz - AVB_FOOTER_SIZE;
|
|
|
|
if (0 != data.compare(footer_offset, AVB_FOOTER_MAGIC_LEN, AVB_FOOTER_MAGIC)) {
|
2021-08-10 10:32:37 +02:00
|
|
|
lseek(buf->fd.get(), 0, SEEK_SET); // IMPORTANT: resets buf->fd before return.
|
2020-06-19 02:02:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-07 17:33:46 +01:00
|
|
|
const std::string tmp_fd_template = partition + " rewriting";
|
|
|
|
unique_fd fd(make_temporary_fd(tmp_fd_template.c_str()));
|
2020-06-19 02:02:15 +02:00
|
|
|
if (!android::base::WriteStringToFd(data, fd)) {
|
2022-01-07 17:33:46 +01:00
|
|
|
die("Failed writing to modified %s", partition.c_str());
|
2020-06-19 02:02:15 +02:00
|
|
|
}
|
2021-03-23 00:39:13 +01:00
|
|
|
lseek(fd.get(), partition_size - AVB_FOOTER_SIZE, SEEK_SET);
|
2020-06-19 02:02:15 +02:00
|
|
|
if (!android::base::WriteStringToFd(data.substr(footer_offset), fd)) {
|
2022-01-07 17:33:46 +01:00
|
|
|
die("Failed copying AVB footer in %s", partition.c_str());
|
2020-06-19 02:02:15 +02:00
|
|
|
}
|
2021-02-23 00:06:27 +01:00
|
|
|
buf->fd = std::move(fd);
|
2020-06-19 02:02:15 +02:00
|
|
|
buf->sz = partition_size;
|
2021-03-23 00:39:13 +01:00
|
|
|
lseek(buf->fd.get(), 0, SEEK_SET);
|
2020-06-19 02:02:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-28 05:39:06 +01:00
|
|
|
static void flash_partition_files(const std::string& partition,
|
|
|
|
const std::vector<SparsePtr>& files) {
|
|
|
|
for (size_t i = 0; i < files.size(); i++) {
|
|
|
|
sparse_file* s = files[i].get();
|
|
|
|
int64_t sz = sparse_file_len(s, true, false);
|
|
|
|
fb->FlashPartition(partition, s, sz, i + 1, files.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 03:25:31 +01:00
|
|
|
static void flash_buf(const std::string& partition, struct fastboot_buffer* buf) {
|
2022-01-06 02:11:23 +01:00
|
|
|
if (partition == "boot" || partition == "boot_a" || partition == "boot_b" ||
|
2023-01-12 23:28:29 +01:00
|
|
|
partition == "init_boot" || partition == "init_boot_a" || partition == "init_boot_b" ||
|
|
|
|
partition == "recovery" || partition == "recovery_a" || partition == "recovery_b") {
|
2022-01-07 17:33:46 +01:00
|
|
|
copy_avb_footer(partition, buf);
|
2020-06-19 02:02:15 +02:00
|
|
|
}
|
|
|
|
|
2017-08-07 20:29:26 +02:00
|
|
|
// Rewrite vbmeta if that's what we're flashing and modification has been requested.
|
2019-09-16 05:23:55 +02:00
|
|
|
if (g_disable_verity || g_disable_verification) {
|
2021-03-04 13:11:25 +01:00
|
|
|
// The vbmeta partition might have additional prefix if running in virtual machine
|
|
|
|
// e.g., guest_vbmeta_a.
|
|
|
|
if (android::base::EndsWith(partition, "vbmeta") ||
|
|
|
|
android::base::EndsWith(partition, "vbmeta_a") ||
|
|
|
|
android::base::EndsWith(partition, "vbmeta_b")) {
|
2019-09-16 05:23:55 +02:00
|
|
|
rewrite_vbmeta_buffer(buf, false /* vbmeta_in_boot */);
|
|
|
|
} else if (!has_vbmeta_partition() &&
|
|
|
|
(partition == "boot" || partition == "boot_a" || partition == "boot_b")) {
|
2022-11-30 03:25:31 +01:00
|
|
|
rewrite_vbmeta_buffer(buf, true /* vbmeta_in_boot */);
|
2019-09-16 05:23:55 +02:00
|
|
|
}
|
2017-08-07 20:29:26 +02:00
|
|
|
}
|
|
|
|
|
2013-06-28 18:54:59 +02:00
|
|
|
switch (buf->type) {
|
2016-01-19 23:50:18 +01:00
|
|
|
case FB_BUFFER_SPARSE: {
|
2023-01-28 05:39:06 +01:00
|
|
|
flash_partition_files(partition, buf->files);
|
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 "";
|
2020-04-06 14:38:58 +02:00
|
|
|
if (current_slot[0] == '_') current_slot.erase(0, 1);
|
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() {
|
2022-11-30 03:25:31 +01:00
|
|
|
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 "";
|
|
|
|
|
2022-11-30 03:25:31 +01:00
|
|
|
char next = (current_slot[0] - 'a' + 1) % count + 'a';
|
2016-07-01 04:25:31 +02:00
|
|
|
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") {
|
2022-11-30 03:25:31 +01:00
|
|
|
std::string other = get_other_slot(count);
|
2016-06-28 08:03:48 +02:00
|
|
|
if (other == "") {
|
2022-11-30 03:25:31 +01: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
|
|
|
}
|
|
|
|
|
2022-11-30 03:25:31 +01:00
|
|
|
if (slot.size() == 1 && (slot[0] - 'a' >= 0 && slot[0] - 'a' < count)) return slot;
|
2016-07-01 04:25:31 +02:00
|
|
|
|
|
|
|
fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
|
2022-11-30 03:25:31 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2016-07-01 04:25:31 +02:00
|
|
|
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) {
|
2022-11-30 03:25:31 +01:00
|
|
|
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;
|
2021-02-23 21:15:51 +01:00
|
|
|
// |part| can be vendor_boot:default. Append slot to the first token.
|
|
|
|
auto part_tokens = android::base::Split(part, ":");
|
2015-09-15 06:05:41 +02:00
|
|
|
|
2021-02-23 21:15:51 +01:00
|
|
|
if (fb->GetVar("has-slot:" + part_tokens[0], &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
|
|
|
}
|
2021-02-23 21:15:51 +01:00
|
|
|
part_tokens[0] += "_" + current_slot;
|
2015-09-15 06:05:41 +02:00
|
|
|
} else {
|
2021-02-23 21:15:51 +01:00
|
|
|
part_tokens[0] += "_" + slot;
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
2021-02-23 21:15:51 +01:00
|
|
|
func(android::base::Join(part_tokens, ":"));
|
2015-09-15 06:05:41 +02:00
|
|
|
} else {
|
2016-07-01 04:25:31 +02:00
|
|
|
if (force_slot && slot != "") {
|
2021-02-23 21:15:51 +01:00
|
|
|
fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
|
|
|
|
part_tokens[0].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
|
|
|
*/
|
2023-01-31 22:07:53 +01:00
|
|
|
void do_for_partitions(const std::string& part, const std::string& slot,
|
|
|
|
const std::function<void(const std::string&)>& func, bool force_slot) {
|
2015-11-12 01:13:13 +01:00
|
|
|
std::string has_slot;
|
2021-02-23 21:15:51 +01:00
|
|
|
// |part| can be vendor_boot:default. Query has-slot on the first token only.
|
|
|
|
auto part_tokens = android::base::Split(part, ":");
|
2015-09-15 06:05:41 +02:00
|
|
|
|
2016-07-01 04:25:31 +02:00
|
|
|
if (slot == "all") {
|
2021-02-23 21:15:51 +01:00
|
|
|
if (fb->GetVar("has-slot:" + part_tokens[0], &has_slot) != fastboot::SUCCESS) {
|
|
|
|
die("Could not check if partition %s has slot %s", part_tokens[0].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") {
|
2022-11-30 03:25:31 +01:00
|
|
|
for (int i = 0; i < get_slot_count(); i++) {
|
2018-06-26 22:38:35 +02:00
|
|
|
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-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_");
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:37:32 +01:00
|
|
|
// Fetch a partition from the device to a given fd. This is a wrapper over FetchToFd to fetch
|
|
|
|
// the full image.
|
|
|
|
static uint64_t fetch_partition(const std::string& partition, borrowed_fd fd) {
|
|
|
|
uint64_t fetch_size = get_uint_var(FB_VAR_MAX_FETCH_SIZE);
|
|
|
|
if (fetch_size == 0) {
|
|
|
|
die("Unable to get %s. Device does not support fetch command.", FB_VAR_MAX_FETCH_SIZE);
|
|
|
|
}
|
|
|
|
uint64_t partition_size = get_partition_size(partition);
|
|
|
|
if (partition_size <= 0) {
|
|
|
|
die("Invalid partition size for partition %s: %" PRId64, partition.c_str(), partition_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t offset = 0;
|
|
|
|
while (offset < partition_size) {
|
|
|
|
uint64_t chunk_size = std::min(fetch_size, partition_size - offset);
|
|
|
|
if (fb->FetchToFd(partition, fd, offset, chunk_size) != fastboot::RetCode::SUCCESS) {
|
|
|
|
die("Unable to fetch %s (offset=%" PRIx64 ", size=%" PRIx64 ")", partition.c_str(),
|
|
|
|
offset, chunk_size);
|
|
|
|
}
|
|
|
|
offset += chunk_size;
|
|
|
|
}
|
|
|
|
return partition_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_fetch(const std::string& partition, const std::string& slot_override,
|
|
|
|
const std::string& outfile) {
|
|
|
|
unique_fd fd(TEMP_FAILURE_RETRY(
|
|
|
|
open(outfile.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY, 0644)));
|
|
|
|
auto fetch = std::bind(fetch_partition, _1, borrowed_fd(fd));
|
|
|
|
do_for_partitions(partition, slot_override, fetch, false /* force slot */);
|
|
|
|
}
|
|
|
|
|
2021-02-23 00:00:15 +01:00
|
|
|
// Return immediately if not flashing a vendor boot image. If flashing a vendor boot image,
|
|
|
|
// repack vendor_boot image with an updated ramdisk. After execution, buf is set
|
|
|
|
// to the new image to flash, and return value is the real partition name to flash.
|
|
|
|
static std::string repack_ramdisk(const char* pname, struct fastboot_buffer* buf) {
|
|
|
|
std::string_view pname_sv{pname};
|
|
|
|
|
|
|
|
if (!android::base::StartsWith(pname_sv, "vendor_boot:") &&
|
|
|
|
!android::base::StartsWith(pname_sv, "vendor_boot_a:") &&
|
|
|
|
!android::base::StartsWith(pname_sv, "vendor_boot_b:")) {
|
|
|
|
return std::string(pname_sv);
|
|
|
|
}
|
|
|
|
if (buf->type != FB_BUFFER_FD) {
|
|
|
|
die("Flashing sparse vendor ramdisk image is not supported.");
|
|
|
|
}
|
|
|
|
if (buf->sz <= 0) {
|
|
|
|
die("repack_ramdisk() sees negative size: %" PRId64, buf->sz);
|
|
|
|
}
|
|
|
|
std::string partition(pname_sv.substr(0, pname_sv.find(':')));
|
|
|
|
std::string ramdisk(pname_sv.substr(pname_sv.find(':') + 1));
|
|
|
|
|
|
|
|
unique_fd vendor_boot(make_temporary_fd("vendor boot repack"));
|
|
|
|
uint64_t vendor_boot_size = fetch_partition(partition, vendor_boot);
|
|
|
|
auto repack_res = replace_vendor_ramdisk(vendor_boot, vendor_boot_size, ramdisk, buf->fd,
|
|
|
|
static_cast<uint64_t>(buf->sz));
|
|
|
|
if (!repack_res.ok()) {
|
|
|
|
die("%s", repack_res.error().message().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
buf->fd = std::move(vendor_boot);
|
|
|
|
buf->sz = vendor_boot_size;
|
|
|
|
buf->image_size = vendor_boot_size;
|
|
|
|
return partition;
|
|
|
|
}
|
|
|
|
|
2023-01-31 22:07:53 +01:00
|
|
|
void do_flash(const char* pname, const char* fname) {
|
2021-02-23 21:15:51 +01:00
|
|
|
verbose("Do flash %s %s", pname, 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));
|
|
|
|
}
|
2021-02-23 00:00:15 +01:00
|
|
|
std::string flash_pname = repack_ramdisk(pname, &buf);
|
|
|
|
flash_buf(flash_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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 22:07:53 +01:00
|
|
|
bool is_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
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-31 22:07:53 +01:00
|
|
|
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.");
|
|
|
|
}
|
2019-05-17 23:24:19 +02:00
|
|
|
|
|
|
|
// Reset target_sparse_limit after reboot to userspace fastboot. Max
|
|
|
|
// download sizes may differ in bootloader and fastbootd.
|
|
|
|
target_sparse_limit = -1;
|
2018-08-29 23:15:49 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-10-22 01:45:59 +02:00
|
|
|
static void CancelSnapshotIfNeeded() {
|
|
|
|
std::string merge_status = "none";
|
|
|
|
if (fb->GetVar(FB_VAR_SNAPSHOT_UPDATE_STATUS, &merge_status) == fastboot::SUCCESS &&
|
2020-06-05 23:06:15 +02:00
|
|
|
!merge_status.empty() && merge_status != "none") {
|
2019-12-11 03:11:13 +01:00
|
|
|
fb->SnapshotUpdateCommand("cancel");
|
2019-10-22 01:45:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
class FlashAllTool {
|
|
|
|
public:
|
2023-02-21 19:22:08 +01:00
|
|
|
FlashAllTool(FlashingPlan* fp);
|
2018-08-29 23:15:49 +02:00
|
|
|
|
|
|
|
void Flash();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void CheckRequirements();
|
fastboot: Avoid reboots to userspace when using flashall/update.
Reboots to fastbootd (userspace fastboot) take a long time, particularly
due to the orange AVB screen and the likelihood of devices having uart
enabled. For "flashall", there is rarely a need to actually go into
userspace, because all of super is getting thrown away. We can just
flash super in the bootloader.
In the past we didn't do this because computing super.img is expensive -
both in terms of time (due to reading dependent images) and in terms of
space (it's easily over 5GB).
But we don't actually need to fully compute super.img. We can build a
sparse_file containing the metadata/headers, with additional references
to each image file containing partition data. Liblp provides the API to
do that, and here, we simply need to translate the layout to libsparse.
On Pixel, this reduces flashall time by around 35-50 seconds, or around
20% of total time, depending on whether uart is in use.
There are some caveats, in which case we'll fall back to normal
fastbootd. This does not work on non-A/B devices, on retrofit dynamic
partition devices (Pixel 3), and in some other edge-casey scenarios. If
it fails, -v will add logging information about why.
Bue: 266982466
Test: fastboot flashall on Pixel 5+
Change-Id: Ie040da597d739faa7f834202184cec8f8e412076
2023-01-28 06:11:11 +01:00
|
|
|
void DetermineSlot();
|
2018-08-29 23:15:49 +02:00
|
|
|
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();
|
fastboot: Avoid reboots to userspace when using flashall/update.
Reboots to fastbootd (userspace fastboot) take a long time, particularly
due to the orange AVB screen and the likelihood of devices having uart
enabled. For "flashall", there is rarely a need to actually go into
userspace, because all of super is getting thrown away. We can just
flash super in the bootloader.
In the past we didn't do this because computing super.img is expensive -
both in terms of time (due to reading dependent images) and in terms of
space (it's easily over 5GB).
But we don't actually need to fully compute super.img. We can build a
sparse_file containing the metadata/headers, with additional references
to each image file containing partition data. Liblp provides the API to
do that, and here, we simply need to translate the layout to libsparse.
On Pixel, this reduces flashall time by around 35-50 seconds, or around
20% of total time, depending on whether uart is in use.
There are some caveats, in which case we'll fall back to normal
fastbootd. This does not work on non-A/B devices, on retrofit dynamic
partition devices (Pixel 3), and in some other edge-casey scenarios. If
it fails, -v will add logging information about why.
Bue: 266982466
Test: fastboot flashall on Pixel 5+
Change-Id: Ie040da597d739faa7f834202184cec8f8e412076
2023-01-28 06:11:11 +01:00
|
|
|
bool OptimizedFlashSuper();
|
|
|
|
|
|
|
|
// If the image uses the default slot, or the user specified "all", then
|
|
|
|
// the paired string will be empty. If the image requests a specific slot
|
|
|
|
// (for example, system_other) it is specified instead.
|
|
|
|
using ImageEntry = std::pair<const Image*, std::string>;
|
|
|
|
|
|
|
|
std::string GetPartitionName(const ImageEntry& entry);
|
2018-08-29 23:15:49 +02:00
|
|
|
|
fastboot: Avoid reboots to userspace when using flashall/update.
Reboots to fastbootd (userspace fastboot) take a long time, particularly
due to the orange AVB screen and the likelihood of devices having uart
enabled. For "flashall", there is rarely a need to actually go into
userspace, because all of super is getting thrown away. We can just
flash super in the bootloader.
In the past we didn't do this because computing super.img is expensive -
both in terms of time (due to reading dependent images) and in terms of
space (it's easily over 5GB).
But we don't actually need to fully compute super.img. We can build a
sparse_file containing the metadata/headers, with additional references
to each image file containing partition data. Liblp provides the API to
do that, and here, we simply need to translate the layout to libsparse.
On Pixel, this reduces flashall time by around 35-50 seconds, or around
20% of total time, depending on whether uart is in use.
There are some caveats, in which case we'll fall back to normal
fastbootd. This does not work on non-A/B devices, on retrofit dynamic
partition devices (Pixel 3), and in some other edge-casey scenarios. If
it fails, -v will add logging information about why.
Bue: 266982466
Test: fastboot flashall on Pixel 5+
Change-Id: Ie040da597d739faa7f834202184cec8f8e412076
2023-01-28 06:11:11 +01:00
|
|
|
std::vector<ImageEntry> boot_images_;
|
|
|
|
std::vector<ImageEntry> os_images_;
|
2023-02-21 19:22:08 +01:00
|
|
|
FlashingPlan* fp_;
|
2018-08-29 23:15:49 +02:00
|
|
|
};
|
2017-10-27 23:21:12 +02:00
|
|
|
|
2023-02-21 19:22:08 +01:00
|
|
|
FlashAllTool::FlashAllTool(FlashingPlan* fp) : fp_(fp) {}
|
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.
|
2023-02-21 19:22:08 +01:00
|
|
|
if (fp_->slot == "all") {
|
2018-11-06 00:21:44 +01:00
|
|
|
set_active("a");
|
|
|
|
} else {
|
2023-02-21 19:22:08 +01:00
|
|
|
set_active(fp_->slot);
|
2018-11-06 00:21:44 +01:00
|
|
|
}
|
|
|
|
|
fastboot: Avoid reboots to userspace when using flashall/update.
Reboots to fastbootd (userspace fastboot) take a long time, particularly
due to the orange AVB screen and the likelihood of devices having uart
enabled. For "flashall", there is rarely a need to actually go into
userspace, because all of super is getting thrown away. We can just
flash super in the bootloader.
In the past we didn't do this because computing super.img is expensive -
both in terms of time (due to reading dependent images) and in terms of
space (it's easily over 5GB).
But we don't actually need to fully compute super.img. We can build a
sparse_file containing the metadata/headers, with additional references
to each image file containing partition data. Liblp provides the API to
do that, and here, we simply need to translate the layout to libsparse.
On Pixel, this reduces flashall time by around 35-50 seconds, or around
20% of total time, depending on whether uart is in use.
There are some caveats, in which case we'll fall back to normal
fastbootd. This does not work on non-A/B devices, on retrofit dynamic
partition devices (Pixel 3), and in some other edge-casey scenarios. If
it fails, -v will add logging information about why.
Bue: 266982466
Test: fastboot flashall on Pixel 5+
Change-Id: Ie040da597d739faa7f834202184cec8f8e412076
2023-01-28 06:11:11 +01:00
|
|
|
DetermineSlot();
|
2018-08-29 23:15:49 +02:00
|
|
|
CollectImages();
|
|
|
|
|
2019-10-22 01:45:59 +02:00
|
|
|
CancelSnapshotIfNeeded();
|
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
// First flash boot partitions. We allow this to happen either in userspace
|
|
|
|
// or in bootloader fastboot.
|
|
|
|
FlashImages(boot_images_);
|
|
|
|
|
fastboot: Avoid reboots to userspace when using flashall/update.
Reboots to fastbootd (userspace fastboot) take a long time, particularly
due to the orange AVB screen and the likelihood of devices having uart
enabled. For "flashall", there is rarely a need to actually go into
userspace, because all of super is getting thrown away. We can just
flash super in the bootloader.
In the past we didn't do this because computing super.img is expensive -
both in terms of time (due to reading dependent images) and in terms of
space (it's easily over 5GB).
But we don't actually need to fully compute super.img. We can build a
sparse_file containing the metadata/headers, with additional references
to each image file containing partition data. Liblp provides the API to
do that, and here, we simply need to translate the layout to libsparse.
On Pixel, this reduces flashall time by around 35-50 seconds, or around
20% of total time, depending on whether uart is in use.
There are some caveats, in which case we'll fall back to normal
fastbootd. This does not work on non-A/B devices, on retrofit dynamic
partition devices (Pixel 3), and in some other edge-casey scenarios. If
it fails, -v will add logging information about why.
Bue: 266982466
Test: fastboot flashall on Pixel 5+
Change-Id: Ie040da597d739faa7f834202184cec8f8e412076
2023-01-28 06:11:11 +01:00
|
|
|
if (!OptimizedFlashSuper()) {
|
|
|
|
// Sync the super partition. This will reboot to userspace fastboot if needed.
|
|
|
|
UpdateSuperPartition();
|
2018-08-29 23:15:49 +02:00
|
|
|
|
fastboot: Avoid reboots to userspace when using flashall/update.
Reboots to fastbootd (userspace fastboot) take a long time, particularly
due to the orange AVB screen and the likelihood of devices having uart
enabled. For "flashall", there is rarely a need to actually go into
userspace, because all of super is getting thrown away. We can just
flash super in the bootloader.
In the past we didn't do this because computing super.img is expensive -
both in terms of time (due to reading dependent images) and in terms of
space (it's easily over 5GB).
But we don't actually need to fully compute super.img. We can build a
sparse_file containing the metadata/headers, with additional references
to each image file containing partition data. Liblp provides the API to
do that, and here, we simply need to translate the layout to libsparse.
On Pixel, this reduces flashall time by around 35-50 seconds, or around
20% of total time, depending on whether uart is in use.
There are some caveats, in which case we'll fall back to normal
fastbootd. This does not work on non-A/B devices, on retrofit dynamic
partition devices (Pixel 3), and in some other edge-casey scenarios. If
it fails, -v will add logging information about why.
Bue: 266982466
Test: fastboot flashall on Pixel 5+
Change-Id: Ie040da597d739faa7f834202184cec8f8e412076
2023-01-28 06:11:11 +01:00
|
|
|
// 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)) {
|
|
|
|
fb->ResizePartition(partition, "0");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
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
|
|
|
}
|
|
|
|
|
fastboot: Avoid reboots to userspace when using flashall/update.
Reboots to fastbootd (userspace fastboot) take a long time, particularly
due to the orange AVB screen and the likelihood of devices having uart
enabled. For "flashall", there is rarely a need to actually go into
userspace, because all of super is getting thrown away. We can just
flash super in the bootloader.
In the past we didn't do this because computing super.img is expensive -
both in terms of time (due to reading dependent images) and in terms of
space (it's easily over 5GB).
But we don't actually need to fully compute super.img. We can build a
sparse_file containing the metadata/headers, with additional references
to each image file containing partition data. Liblp provides the API to
do that, and here, we simply need to translate the layout to libsparse.
On Pixel, this reduces flashall time by around 35-50 seconds, or around
20% of total time, depending on whether uart is in use.
There are some caveats, in which case we'll fall back to normal
fastbootd. This does not work on non-A/B devices, on retrofit dynamic
partition devices (Pixel 3), and in some other edge-casey scenarios. If
it fails, -v will add logging information about why.
Bue: 266982466
Test: fastboot flashall on Pixel 5+
Change-Id: Ie040da597d739faa7f834202184cec8f8e412076
2023-01-28 06:11:11 +01:00
|
|
|
bool FlashAllTool::OptimizedFlashSuper() {
|
|
|
|
if (!supports_AB()) {
|
|
|
|
LOG(VERBOSE) << "Cannot optimize flashing super on non-AB device";
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-21 19:22:08 +01:00
|
|
|
if (fp_->slot == "all") {
|
fastboot: Avoid reboots to userspace when using flashall/update.
Reboots to fastbootd (userspace fastboot) take a long time, particularly
due to the orange AVB screen and the likelihood of devices having uart
enabled. For "flashall", there is rarely a need to actually go into
userspace, because all of super is getting thrown away. We can just
flash super in the bootloader.
In the past we didn't do this because computing super.img is expensive -
both in terms of time (due to reading dependent images) and in terms of
space (it's easily over 5GB).
But we don't actually need to fully compute super.img. We can build a
sparse_file containing the metadata/headers, with additional references
to each image file containing partition data. Liblp provides the API to
do that, and here, we simply need to translate the layout to libsparse.
On Pixel, this reduces flashall time by around 35-50 seconds, or around
20% of total time, depending on whether uart is in use.
There are some caveats, in which case we'll fall back to normal
fastbootd. This does not work on non-A/B devices, on retrofit dynamic
partition devices (Pixel 3), and in some other edge-casey scenarios. If
it fails, -v will add logging information about why.
Bue: 266982466
Test: fastboot flashall on Pixel 5+
Change-Id: Ie040da597d739faa7f834202184cec8f8e412076
2023-01-28 06:11:11 +01:00
|
|
|
LOG(VERBOSE) << "Cannot optimize flashing super for all slots";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does this device use dynamic partitions at all?
|
2023-02-21 19:22:08 +01:00
|
|
|
unique_fd fd = fp_->source->OpenFile("super_empty.img");
|
fastboot: Avoid reboots to userspace when using flashall/update.
Reboots to fastbootd (userspace fastboot) take a long time, particularly
due to the orange AVB screen and the likelihood of devices having uart
enabled. For "flashall", there is rarely a need to actually go into
userspace, because all of super is getting thrown away. We can just
flash super in the bootloader.
In the past we didn't do this because computing super.img is expensive -
both in terms of time (due to reading dependent images) and in terms of
space (it's easily over 5GB).
But we don't actually need to fully compute super.img. We can build a
sparse_file containing the metadata/headers, with additional references
to each image file containing partition data. Liblp provides the API to
do that, and here, we simply need to translate the layout to libsparse.
On Pixel, this reduces flashall time by around 35-50 seconds, or around
20% of total time, depending on whether uart is in use.
There are some caveats, in which case we'll fall back to normal
fastbootd. This does not work on non-A/B devices, on retrofit dynamic
partition devices (Pixel 3), and in some other edge-casey scenarios. If
it fails, -v will add logging information about why.
Bue: 266982466
Test: fastboot flashall on Pixel 5+
Change-Id: Ie040da597d739faa7f834202184cec8f8e412076
2023-01-28 06:11:11 +01:00
|
|
|
if (fd < 0) {
|
|
|
|
LOG(VERBOSE) << "could not open super_empty.img";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find whether there is a super partition.
|
|
|
|
std::string super_name;
|
|
|
|
if (fb->GetVar("super-partition-name", &super_name) != fastboot::SUCCESS) {
|
|
|
|
super_name = "super";
|
|
|
|
}
|
|
|
|
std::string partition_size_str;
|
|
|
|
if (fb->GetVar("partition-size:" + super_name, &partition_size_str) != fastboot::SUCCESS) {
|
|
|
|
LOG(VERBOSE) << "Cannot optimize super flashing: could not determine super partition";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-21 19:22:08 +01:00
|
|
|
SuperFlashHelper helper(*fp_->source);
|
fastboot: Avoid reboots to userspace when using flashall/update.
Reboots to fastbootd (userspace fastboot) take a long time, particularly
due to the orange AVB screen and the likelihood of devices having uart
enabled. For "flashall", there is rarely a need to actually go into
userspace, because all of super is getting thrown away. We can just
flash super in the bootloader.
In the past we didn't do this because computing super.img is expensive -
both in terms of time (due to reading dependent images) and in terms of
space (it's easily over 5GB).
But we don't actually need to fully compute super.img. We can build a
sparse_file containing the metadata/headers, with additional references
to each image file containing partition data. Liblp provides the API to
do that, and here, we simply need to translate the layout to libsparse.
On Pixel, this reduces flashall time by around 35-50 seconds, or around
20% of total time, depending on whether uart is in use.
There are some caveats, in which case we'll fall back to normal
fastbootd. This does not work on non-A/B devices, on retrofit dynamic
partition devices (Pixel 3), and in some other edge-casey scenarios. If
it fails, -v will add logging information about why.
Bue: 266982466
Test: fastboot flashall on Pixel 5+
Change-Id: Ie040da597d739faa7f834202184cec8f8e412076
2023-01-28 06:11:11 +01:00
|
|
|
if (!helper.Open(fd)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& entry : os_images_) {
|
|
|
|
auto partition = GetPartitionName(entry);
|
|
|
|
auto image = entry.first;
|
|
|
|
|
|
|
|
if (!helper.AddPartition(partition, image->img_name, image->optional_if_no_image)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto s = helper.GetSparseLayout();
|
|
|
|
if (!s) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<SparsePtr> files;
|
|
|
|
if (int limit = get_sparse_limit(sparse_file_len(s.get(), false, false))) {
|
|
|
|
files = resparse_file(s.get(), limit);
|
|
|
|
} else {
|
|
|
|
files.emplace_back(std::move(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the data to the device.
|
|
|
|
flash_partition_files(super_name, files);
|
|
|
|
|
|
|
|
// Remove images that we already flashed, just in case we have non-dynamic OS images.
|
|
|
|
auto remove_if_callback = [&, this](const ImageEntry& entry) -> bool {
|
|
|
|
return helper.WillFlash(GetPartitionName(entry));
|
|
|
|
};
|
|
|
|
os_images_.erase(std::remove_if(os_images_.begin(), os_images_.end(), remove_if_callback),
|
|
|
|
os_images_.end());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
void FlashAllTool::CheckRequirements() {
|
2018-09-20 23:45:05 +02:00
|
|
|
std::vector<char> contents;
|
2023-02-21 19:22:08 +01:00
|
|
|
if (!fp_->source->ReadFile("android-info.txt", &contents)) {
|
2018-08-29 23:15:49 +02:00
|
|
|
die("could not read android-info.txt");
|
|
|
|
}
|
2023-02-21 19:22:08 +01:00
|
|
|
::CheckRequirements({contents.data(), contents.size()}, fp_->force_flash);
|
2018-08-29 23:15:49 +02:00
|
|
|
}
|
2016-04-25 23:31:18 +02:00
|
|
|
|
fastboot: Avoid reboots to userspace when using flashall/update.
Reboots to fastbootd (userspace fastboot) take a long time, particularly
due to the orange AVB screen and the likelihood of devices having uart
enabled. For "flashall", there is rarely a need to actually go into
userspace, because all of super is getting thrown away. We can just
flash super in the bootloader.
In the past we didn't do this because computing super.img is expensive -
both in terms of time (due to reading dependent images) and in terms of
space (it's easily over 5GB).
But we don't actually need to fully compute super.img. We can build a
sparse_file containing the metadata/headers, with additional references
to each image file containing partition data. Liblp provides the API to
do that, and here, we simply need to translate the layout to libsparse.
On Pixel, this reduces flashall time by around 35-50 seconds, or around
20% of total time, depending on whether uart is in use.
There are some caveats, in which case we'll fall back to normal
fastbootd. This does not work on non-A/B devices, on retrofit dynamic
partition devices (Pixel 3), and in some other edge-casey scenarios. If
it fails, -v will add logging information about why.
Bue: 266982466
Test: fastboot flashall on Pixel 5+
Change-Id: Ie040da597d739faa7f834202184cec8f8e412076
2023-01-28 06:11:11 +01:00
|
|
|
void FlashAllTool::DetermineSlot() {
|
2023-02-21 19:22:08 +01:00
|
|
|
if (fp_->slot.empty()) {
|
|
|
|
fp_->current_slot = get_current_slot();
|
fastboot: Avoid reboots to userspace when using flashall/update.
Reboots to fastbootd (userspace fastboot) take a long time, particularly
due to the orange AVB screen and the likelihood of devices having uart
enabled. For "flashall", there is rarely a need to actually go into
userspace, because all of super is getting thrown away. We can just
flash super in the bootloader.
In the past we didn't do this because computing super.img is expensive -
both in terms of time (due to reading dependent images) and in terms of
space (it's easily over 5GB).
But we don't actually need to fully compute super.img. We can build a
sparse_file containing the metadata/headers, with additional references
to each image file containing partition data. Liblp provides the API to
do that, and here, we simply need to translate the layout to libsparse.
On Pixel, this reduces flashall time by around 35-50 seconds, or around
20% of total time, depending on whether uart is in use.
There are some caveats, in which case we'll fall back to normal
fastbootd. This does not work on non-A/B devices, on retrofit dynamic
partition devices (Pixel 3), and in some other edge-casey scenarios. If
it fails, -v will add logging information about why.
Bue: 266982466
Test: fastboot flashall on Pixel 5+
Change-Id: Ie040da597d739faa7f834202184cec8f8e412076
2023-01-28 06:11:11 +01:00
|
|
|
} else {
|
2023-02-21 19:22:08 +01:00
|
|
|
fp_->current_slot = fp_->slot;
|
fastboot: Avoid reboots to userspace when using flashall/update.
Reboots to fastbootd (userspace fastboot) take a long time, particularly
due to the orange AVB screen and the likelihood of devices having uart
enabled. For "flashall", there is rarely a need to actually go into
userspace, because all of super is getting thrown away. We can just
flash super in the bootloader.
In the past we didn't do this because computing super.img is expensive -
both in terms of time (due to reading dependent images) and in terms of
space (it's easily over 5GB).
But we don't actually need to fully compute super.img. We can build a
sparse_file containing the metadata/headers, with additional references
to each image file containing partition data. Liblp provides the API to
do that, and here, we simply need to translate the layout to libsparse.
On Pixel, this reduces flashall time by around 35-50 seconds, or around
20% of total time, depending on whether uart is in use.
There are some caveats, in which case we'll fall back to normal
fastbootd. This does not work on non-A/B devices, on retrofit dynamic
partition devices (Pixel 3), and in some other edge-casey scenarios. If
it fails, -v will add logging information about why.
Bue: 266982466
Test: fastboot flashall on Pixel 5+
Change-Id: Ie040da597d739faa7f834202184cec8f8e412076
2023-01-28 06:11:11 +01:00
|
|
|
}
|
|
|
|
|
2023-02-21 19:22:08 +01:00
|
|
|
if (fp_->skip_secondary) {
|
2018-08-29 23:15:49 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-02-21 19:22:08 +01:00
|
|
|
if (fp_->slot != "" && fp_->slot != "all") {
|
|
|
|
fp_->secondary_slot = get_other_slot(fp_->slot);
|
2018-08-29 23:15:49 +02:00
|
|
|
} else {
|
2023-02-21 19:22:08 +01:00
|
|
|
fp_->secondary_slot = get_other_slot();
|
2018-08-29 23:15:49 +02:00
|
|
|
}
|
2023-02-21 19:22:08 +01:00
|
|
|
if (fp_->secondary_slot == "") {
|
2018-08-29 23:15:49 +02:00
|
|
|
if (supports_AB()) {
|
|
|
|
fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
|
|
|
|
}
|
2023-02-21 19:22:08 +01:00
|
|
|
fp_->skip_secondary = true;
|
2018-08-29 23:15:49 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-08-29 23:15:49 +02:00
|
|
|
void FlashAllTool::CollectImages() {
|
2022-11-30 03:25:31 +01:00
|
|
|
for (size_t i = 0; i < images.size(); ++i) {
|
2023-02-21 19:22:08 +01:00
|
|
|
std::string slot = fp_->slot;
|
2018-08-29 23:15:49 +02:00
|
|
|
if (images[i].IsSecondary()) {
|
2023-02-21 19:22:08 +01:00
|
|
|
if (fp_->skip_secondary) {
|
2018-08-29 23:15:49 +02:00
|
|
|
continue;
|
|
|
|
}
|
2023-02-21 19:22:08 +01:00
|
|
|
slot = fp_->secondary_slot;
|
2018-08-29 23:15:49 +02:00
|
|
|
}
|
|
|
|
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;
|
2023-02-21 19:22:08 +01:00
|
|
|
unique_fd fd = fp_->source->OpenFile(image->img_name);
|
2021-02-23 00:06:27 +01:00
|
|
|
if (fd < 0 || !load_buf_fd(std::move(fd), &buf)) {
|
2018-08-29 23:15:49 +02:00
|
|
|
if (image->optional_if_no_image) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-11-30 03:25:31 +01:00
|
|
|
die("could not load '%s': %s", image->img_name.c_str(), strerror(errno));
|
2018-08-29 23:15:49 +02:00
|
|
|
}
|
|
|
|
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;
|
2023-02-21 19:22:08 +01:00
|
|
|
if (fp_->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() {
|
2023-02-21 19:22:08 +01:00
|
|
|
unique_fd fd = fp_->source->OpenFile("super_empty.img");
|
2018-08-29 23:15:49 +02:00
|
|
|
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;
|
2023-02-21 19:22:08 +01:00
|
|
|
if (fp_->wants_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
|
|
|
}
|
|
|
|
|
fastboot: Avoid reboots to userspace when using flashall/update.
Reboots to fastbootd (userspace fastboot) take a long time, particularly
due to the orange AVB screen and the likelihood of devices having uart
enabled. For "flashall", there is rarely a need to actually go into
userspace, because all of super is getting thrown away. We can just
flash super in the bootloader.
In the past we didn't do this because computing super.img is expensive -
both in terms of time (due to reading dependent images) and in terms of
space (it's easily over 5GB).
But we don't actually need to fully compute super.img. We can build a
sparse_file containing the metadata/headers, with additional references
to each image file containing partition data. Liblp provides the API to
do that, and here, we simply need to translate the layout to libsparse.
On Pixel, this reduces flashall time by around 35-50 seconds, or around
20% of total time, depending on whether uart is in use.
There are some caveats, in which case we'll fall back to normal
fastbootd. This does not work on non-A/B devices, on retrofit dynamic
partition devices (Pixel 3), and in some other edge-casey scenarios. If
it fails, -v will add logging information about why.
Bue: 266982466
Test: fastboot flashall on Pixel 5+
Change-Id: Ie040da597d739faa7f834202184cec8f8e412076
2023-01-28 06:11:11 +01:00
|
|
|
std::string FlashAllTool::GetPartitionName(const ImageEntry& entry) {
|
|
|
|
auto slot = entry.second;
|
|
|
|
if (slot.empty()) {
|
2023-02-21 19:22:08 +01:00
|
|
|
slot = fp_->current_slot;
|
fastboot: Avoid reboots to userspace when using flashall/update.
Reboots to fastbootd (userspace fastboot) take a long time, particularly
due to the orange AVB screen and the likelihood of devices having uart
enabled. For "flashall", there is rarely a need to actually go into
userspace, because all of super is getting thrown away. We can just
flash super in the bootloader.
In the past we didn't do this because computing super.img is expensive -
both in terms of time (due to reading dependent images) and in terms of
space (it's easily over 5GB).
But we don't actually need to fully compute super.img. We can build a
sparse_file containing the metadata/headers, with additional references
to each image file containing partition data. Liblp provides the API to
do that, and here, we simply need to translate the layout to libsparse.
On Pixel, this reduces flashall time by around 35-50 seconds, or around
20% of total time, depending on whether uart is in use.
There are some caveats, in which case we'll fall back to normal
fastbootd. This does not work on non-A/B devices, on retrofit dynamic
partition devices (Pixel 3), and in some other edge-casey scenarios. If
it fails, -v will add logging information about why.
Bue: 266982466
Test: fastboot flashall on Pixel 5+
Change-Id: Ie040da597d739faa7f834202184cec8f8e412076
2023-01-28 06:11:11 +01:00
|
|
|
}
|
|
|
|
if (slot.empty()) {
|
|
|
|
return entry.first->part_name;
|
|
|
|
}
|
|
|
|
if (slot == "all") {
|
|
|
|
LOG(FATAL) << "Cannot retrieve a singular name when using all slots";
|
|
|
|
}
|
|
|
|
return entry.first->part_name + "_" + slot;
|
|
|
|
}
|
|
|
|
|
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;
|
2021-02-23 00:06:27 +01:00
|
|
|
unique_fd OpenFile(const std::string& name) const override;
|
2018-08-29 23:15:49 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-23 00:06:27 +01:00
|
|
|
unique_fd ZipImageSource::OpenFile(const std::string& name) const {
|
2018-08-29 23:15:49 +02:00
|
|
|
return unzip_to_file(zip_, name.c_str());
|
|
|
|
}
|
|
|
|
|
2023-02-21 19:22:08 +01:00
|
|
|
static void do_update(const char* filename, FlashingPlan* fp) {
|
2018-08-29 23:15:49 +02:00
|
|
|
ZipArchiveHandle zip;
|
|
|
|
int error = OpenArchive(filename, &zip);
|
|
|
|
if (error != 0) {
|
|
|
|
die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
|
|
|
|
}
|
2023-02-21 19:22:08 +01:00
|
|
|
ZipImageSource zp = ZipImageSource(zip);
|
|
|
|
fp->source = &zp;
|
|
|
|
fp->wants_wipe = false;
|
|
|
|
FlashAllTool tool(fp);
|
2018-08-29 23:15:49 +02:00
|
|
|
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;
|
2021-02-23 00:06:27 +01:00
|
|
|
unique_fd OpenFile(const std::string& name) const override;
|
2018-08-29 23:15:49 +02:00
|
|
|
};
|
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
|
|
|
|
2021-02-23 00:06:27 +01:00
|
|
|
unique_fd LocalImageSource::OpenFile(const std::string& name) const {
|
2018-08-29 23:15:49 +02:00
|
|
|
auto path = find_item_given_name(name);
|
2021-02-23 00:06:27 +01:00
|
|
|
return unique_fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_BINARY)));
|
2018-08-29 23:15:49 +02:00
|
|
|
}
|
2018-08-28 21:14:33 +02:00
|
|
|
|
2023-02-21 19:22:08 +01:00
|
|
|
static void do_flashall(FlashingPlan* fp) {
|
|
|
|
LocalImageSource s = LocalImageSource();
|
|
|
|
fp->source = &s;
|
|
|
|
FlashAllTool tool(fp);
|
2018-08-29 23:15:49 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-07-21 06:43:20 +02:00
|
|
|
static void fb_perform_format(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,
|
2022-07-21 06:43:20 +02:00
|
|
|
const unsigned fs_options) {
|
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;
|
|
|
|
}
|
2022-11-30 03:25:31 +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
|
|
|
|
2022-07-21 06:43:20 +02:00
|
|
|
if (fs_generator_generate(gen, output.path, size, eraseBlkSize, logicalBlkSize, fs_options)) {
|
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
|
|
|
}
|
2021-02-23 00:06:27 +01:00
|
|
|
if (!load_buf_fd(std::move(fd), &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
|
|
|
}
|
|
|
|
|
2023-01-31 22:07:53 +01:00
|
|
|
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;
|
|
|
|
}
|
2023-01-28 05:39:06 +01:00
|
|
|
return should_flash_in_userspace(*metadata.get(), partition_name);
|
2018-11-17 00:53:35 +01:00
|
|
|
}
|
|
|
|
|
2019-06-28 07:15:29 +02:00
|
|
|
static bool wipe_super(const android::fs_mgr::LpMetadata& metadata, const std::string& slot,
|
|
|
|
std::string* message) {
|
|
|
|
auto super_device = GetMetadataSuperBlockDevice(metadata);
|
|
|
|
auto block_size = metadata.geometry.logical_block_size;
|
|
|
|
auto super_bdev_name = android::fs_mgr::GetBlockDevicePartitionName(*super_device);
|
|
|
|
|
|
|
|
if (super_bdev_name != "super") {
|
|
|
|
// retrofit devices do not allow flashing to the retrofit partitions,
|
|
|
|
// so enable it if we can.
|
|
|
|
fb->RawCommand("oem allow-flash-super");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: do not use die() in here, since we want TemporaryDir's destructor
|
|
|
|
// to be called.
|
|
|
|
TemporaryDir temp_dir;
|
|
|
|
|
|
|
|
bool ok;
|
|
|
|
if (metadata.block_devices.size() > 1) {
|
|
|
|
ok = WriteSplitImageFiles(temp_dir.path, metadata, block_size, {}, true);
|
|
|
|
} else {
|
|
|
|
auto image_path = temp_dir.path + "/"s + super_bdev_name + ".img";
|
|
|
|
ok = WriteToImageFile(image_path, metadata, block_size, {}, true);
|
|
|
|
}
|
|
|
|
if (!ok) {
|
|
|
|
*message = "Could not generate a flashable super image file";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& block_device : metadata.block_devices) {
|
|
|
|
auto partition = android::fs_mgr::GetBlockDevicePartitionName(block_device);
|
|
|
|
bool force_slot = !!(block_device.flags & LP_BLOCK_DEVICE_SLOT_SUFFIXED);
|
|
|
|
|
|
|
|
std::string image_name;
|
|
|
|
if (metadata.block_devices.size() > 1) {
|
|
|
|
image_name = "super_" + partition + ".img";
|
|
|
|
} else {
|
|
|
|
image_name = partition + ".img";
|
|
|
|
}
|
|
|
|
|
|
|
|
auto image_path = temp_dir.path + "/"s + image_name;
|
|
|
|
auto flash = [&](const std::string& partition_name) {
|
|
|
|
do_flash(partition_name.c_str(), image_path.c_str());
|
|
|
|
};
|
|
|
|
do_for_partitions(partition, slot, flash, force_slot);
|
|
|
|
|
|
|
|
unlink(image_path.c_str());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_wipe_super(const std::string& image, const std::string& slot_override) {
|
|
|
|
if (access(image.c_str(), R_OK) != 0) {
|
|
|
|
die("Could not read image: %s", image.c_str());
|
|
|
|
}
|
|
|
|
auto metadata = android::fs_mgr::ReadFromImageFile(image);
|
|
|
|
if (!metadata) {
|
|
|
|
die("Could not parse image: %s", image.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto slot = slot_override;
|
|
|
|
if (slot.empty()) {
|
|
|
|
slot = get_current_slot();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string message;
|
|
|
|
if (!wipe_super(*metadata.get(), slot, &message)) {
|
|
|
|
die(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-11 01:02:22 +01:00
|
|
|
static void FastbootLogger(android::base::LogId /* id */, android::base::LogSeverity severity,
|
2023-01-28 01:15:52 +01:00
|
|
|
const char* /* tag */, const char* /* file */, unsigned int /* line */,
|
|
|
|
const char* message) {
|
2023-02-11 01:02:22 +01:00
|
|
|
switch (severity) {
|
|
|
|
case android::base::INFO:
|
|
|
|
fprintf(stdout, "%s\n", message);
|
|
|
|
break;
|
|
|
|
case android::base::ERROR:
|
|
|
|
fprintf(stderr, "%s\n", message);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
verbose("%s\n", message);
|
|
|
|
}
|
2023-01-28 01:15:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void FastbootAborter(const char* message) {
|
|
|
|
die("%s", message);
|
|
|
|
}
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
int FastBootTool::Main(int argc, char* argv[]) {
|
2023-01-28 01:15:52 +01:00
|
|
|
android::base::InitLogging(argv, FastbootLogger, FastbootAborter);
|
2023-02-21 19:22:08 +01:00
|
|
|
std::unique_ptr<FlashingPlan> fp = std::make_unique<FlashingPlan>();
|
2023-01-28 01:15:52 +01:00
|
|
|
|
2020-11-09 17:54:13 +01:00
|
|
|
unsigned fs_options = 0;
|
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
|
|
|
|
2022-11-30 03:25:31 +01:00
|
|
|
const struct option longopts[] = {{"base", required_argument, 0, 0},
|
|
|
|
{"cmdline", required_argument, 0, 0},
|
|
|
|
{"disable-verification", no_argument, 0, 0},
|
|
|
|
{"disable-verity", no_argument, 0, 0},
|
|
|
|
{"force", no_argument, 0, 0},
|
|
|
|
{"fs-options", required_argument, 0, 0},
|
|
|
|
{"header-version", required_argument, 0, 0},
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{"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},
|
|
|
|
{"set-active", optional_argument, 0, 'a'},
|
|
|
|
{"skip-reboot", no_argument, 0, 0},
|
|
|
|
{"skip-secondary", no_argument, 0, 0},
|
|
|
|
{"slot", required_argument, 0, 0},
|
|
|
|
{"tags-offset", required_argument, 0, 0},
|
|
|
|
{"dtb", required_argument, 0, 0},
|
|
|
|
{"dtb-offset", required_argument, 0, 0},
|
|
|
|
{"unbuffered", no_argument, 0, 0},
|
|
|
|
{"verbose", no_argument, 0, 'v'},
|
|
|
|
{"version", no_argument, 0, 0},
|
|
|
|
{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") {
|
2023-02-21 19:22:08 +01:00
|
|
|
fp->force_flash = true;
|
2020-11-09 17:54:13 +01:00
|
|
|
} else if (name == "fs-options") {
|
|
|
|
fs_options = ParseFsOption(optarg);
|
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") {
|
2023-02-21 19:22:08 +01:00
|
|
|
fp->skip_reboot = true;
|
2018-04-06 01:12:47 +02:00
|
|
|
} else if (name == "skip-secondary") {
|
2023-02-21 19:22:08 +01:00
|
|
|
fp->skip_secondary = true;
|
2018-04-06 01:12:47 +02:00
|
|
|
} 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") {
|
2022-11-30 03:25:31 +01: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
|
|
|
} 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':
|
2023-02-21 19:22:08 +01:00
|
|
|
fp->wants_set_active = true;
|
2018-04-06 01:12:47 +02:00
|
|
|
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':
|
2023-02-21 19:22:08 +01:00
|
|
|
fp->wants_wipe = true;
|
2018-04-06 01:12:47 +02:00
|
|
|
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;
|
|
|
|
|
2023-02-21 19:22:08 +01:00
|
|
|
if (argc == 0 && !fp->wants_wipe && !fp->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;
|
|
|
|
}
|
|
|
|
|
2023-02-11 01:02:22 +01:00
|
|
|
if (argc > 0 && !strcmp(*argv, "connect")) {
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
return Connect(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 0 && !strcmp(*argv, "disconnect")) {
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
return Disconnect(argc, argv);
|
|
|
|
}
|
|
|
|
|
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 = {
|
2022-11-30 03:25:31 +01:00
|
|
|
.prolog = Status,
|
|
|
|
.epilog = Epilog,
|
|
|
|
.info = InfoMessage,
|
2022-12-30 12:51:54 +01:00
|
|
|
.text = TextMessage,
|
2018-09-25 00:48:09 +02:00
|
|
|
};
|
2022-12-30 12:51:54 +01:00
|
|
|
|
2018-09-25 00:48:09 +02:00
|
|
|
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
|
|
|
|
2023-02-21 19:22:08 +01:00
|
|
|
if (fp->wants_set_active) {
|
2015-11-12 01:15:30 +01:00
|
|
|
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) {
|
2020-04-06 14:38:58 +02:00
|
|
|
if (current_slot[0] == '_') current_slot.erase(0, 1);
|
2018-06-26 22:38:35 +02:00
|
|
|
next_active = verify_slot(current_slot, false);
|
2016-06-28 04:43:11 +02:00
|
|
|
} else {
|
2023-02-21 19:22:08 +01:00
|
|
|
fp->wants_set_active = false;
|
2016-06-28 04:43:11 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-30 18:43:00 +01:00
|
|
|
std::unique_ptr<Task> reboot_task = nullptr;
|
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;
|
2022-11-30 03:25:31 +01: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) {
|
2022-07-21 06:43:20 +02:00
|
|
|
fb_perform_format(partition, 0, type_override, size_override, fs_options);
|
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) {
|
2017-05-09 03:04:49 +02:00
|
|
|
if (args.size() == 1) {
|
2023-01-30 18:43:00 +01:00
|
|
|
std::string reboot_target = next_arg(&args);
|
|
|
|
reboot_task = std::make_unique<RebootTask>(fb, reboot_target);
|
|
|
|
} else {
|
|
|
|
reboot_task = std::make_unique<RebootTask>(fb);
|
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) {
|
2023-01-30 18:43:00 +01:00
|
|
|
reboot_task = std::make_unique<RebootTask>(fb, "bootloader");
|
2018-08-29 19:44:33 +02:00
|
|
|
} else if (command == FB_CMD_REBOOT_RECOVERY) {
|
2023-01-30 18:43:00 +01:00
|
|
|
reboot_task = std::make_unique<RebootTask>(fb, "recovery");
|
2018-08-29 19:44:33 +02:00
|
|
|
} else if (command == FB_CMD_REBOOT_FASTBOOT) {
|
2023-01-30 18:43:00 +01:00
|
|
|
reboot_task = std::make_unique<RebootTask>(fb, "fastboot");
|
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());
|
2023-02-21 19:22:08 +01:00
|
|
|
FlashTask task(slot_override, fp->force_flash, pname, fname);
|
2023-01-31 22:07:53 +01:00
|
|
|
task.Run();
|
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") {
|
2022-11-30 03:25:31 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
"Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
|
2023-02-21 19:22:08 +01:00
|
|
|
fp->skip_secondary = true;
|
|
|
|
do_flashall(fp.get());
|
2016-06-29 18:26:44 +02:00
|
|
|
} else {
|
2023-02-21 19:22:08 +01:00
|
|
|
do_flashall(fp.get());
|
2016-06-29 18:26:44 +02:00
|
|
|
}
|
2023-01-30 18:43:00 +01:00
|
|
|
reboot_task = std::make_unique<RebootTask>(fb);
|
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) {
|
2022-11-30 03:25:31 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
"Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
|
2016-07-14 05:03:25 +02:00
|
|
|
}
|
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
|
|
|
}
|
2023-02-21 19:22:08 +01:00
|
|
|
do_update(filename.c_str(), fp.get());
|
2023-01-30 18:43:00 +01:00
|
|
|
reboot_task = std::make_unique<RebootTask>(fb);
|
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
|
|
|
}
|
2021-03-23 00:39:13 +01:00
|
|
|
fb->Download(filename, buf.fd.get(), 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");
|
2022-11-30 03:25:31 +01:00
|
|
|
} else if (args.size() == 1 &&
|
|
|
|
(args[0] == "unlock" || args[0] == "lock" || args[0] == "unlock_critical" ||
|
|
|
|
args[0] == "lock_critical" || 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'");
|
|
|
|
}
|
2019-06-28 07:15:29 +02:00
|
|
|
} else if (command == "wipe-super") {
|
|
|
|
std::string image;
|
|
|
|
if (args.empty()) {
|
|
|
|
image = find_item_given_name("super_empty.img");
|
|
|
|
} else {
|
|
|
|
image = next_arg(&args);
|
|
|
|
}
|
|
|
|
do_wipe_super(image, slot_override);
|
2019-10-22 01:45:59 +02:00
|
|
|
} else if (command == "snapshot-update") {
|
|
|
|
std::string arg;
|
|
|
|
if (!args.empty()) {
|
|
|
|
arg = next_arg(&args);
|
|
|
|
}
|
2019-11-01 02:02:41 +01:00
|
|
|
if (!arg.empty() && (arg != "cancel" && arg != "merge")) {
|
|
|
|
syntax_error("expected: snapshot-update [cancel|merge]");
|
2019-10-22 01:45:59 +02:00
|
|
|
}
|
|
|
|
fb->SnapshotUpdateCommand(arg);
|
2021-02-17 04:37:32 +01:00
|
|
|
} else if (command == FB_CMD_FETCH) {
|
|
|
|
std::string partition = next_arg(&args);
|
|
|
|
std::string outfile = next_arg(&args);
|
|
|
|
do_fetch(partition, slot_override, outfile);
|
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
|
|
|
}
|
|
|
|
}
|
2023-02-21 19:22:08 +01:00
|
|
|
if (fp->wants_wipe) {
|
|
|
|
if (fp->force_flash) {
|
2019-10-22 01:45:59 +02:00
|
|
|
CancelSnapshotIfNeeded();
|
|
|
|
}
|
2022-11-30 03:25:31 +01:00
|
|
|
std::vector<std::string> partitions = {"userdata", "cache", "metadata"};
|
2018-04-25 02:06:30 +02:00
|
|
|
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);
|
2022-07-21 06:43:20 +02:00
|
|
|
fb_perform_format(partition, 1, partition_type, "", fs_options);
|
2015-10-30 19:49:47 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2023-02-21 19:22:08 +01:00
|
|
|
if (fp->wants_set_active) {
|
2018-09-25 00:48:09 +02:00
|
|
|
fb->SetActive(next_active);
|
2015-11-12 01:15:30 +01:00
|
|
|
}
|
2023-02-21 19:22:08 +01:00
|
|
|
if (reboot_task && !fp->skip_reboot) {
|
2023-01-30 18:43:00 +01:00
|
|
|
reboot_task->Run();
|
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);
|
|
|
|
}
|
2020-11-09 17:54:13 +01:00
|
|
|
|
|
|
|
unsigned FastBootTool::ParseFsOption(const char* arg) {
|
|
|
|
unsigned fsOptions = 0;
|
|
|
|
|
|
|
|
std::vector<std::string> options = android::base::Split(arg, ",");
|
2022-11-30 03:25:31 +01:00
|
|
|
if (options.size() < 1) syntax_error("bad options: %s", arg);
|
2020-11-09 17:54:13 +01:00
|
|
|
|
|
|
|
for (size_t i = 0; i < options.size(); ++i) {
|
|
|
|
if (options[i] == "casefold")
|
|
|
|
fsOptions |= (1 << FS_OPT_CASEFOLD);
|
|
|
|
else if (options[i] == "projid")
|
|
|
|
fsOptions |= (1 << FS_OPT_PROJID);
|
|
|
|
else if (options[i] == "compress")
|
|
|
|
fsOptions |= (1 << FS_OPT_COMPRESS);
|
|
|
|
else
|
|
|
|
syntax_error("unsupported options: %s", options[i].c_str());
|
|
|
|
}
|
|
|
|
return fsOptions;
|
|
|
|
}
|