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.
|
|
|
|
*/
|
|
|
|
|
2012-05-25 02:18:41 +02:00
|
|
|
#define _LARGEFILE64_SOURCE
|
|
|
|
|
2014-04-30 23:05:28 +02:00
|
|
|
#include <ctype.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2012-05-23 02:53:34 +02:00
|
|
|
#include <getopt.h>
|
2014-05-16 05:06:40 +02:00
|
|
|
#include <inttypes.h>
|
2014-04-30 23:05:28 +02:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <sys/time.h>
|
2012-05-25 02:18:41 +02:00
|
|
|
#include <sys/types.h>
|
2014-04-30 23:05:28 +02:00
|
|
|
#include <unistd.h>
|
2016-01-20 17:32:08 +01:00
|
|
|
|
2016-11-15 02:08:47 +01:00
|
|
|
#include <chrono>
|
2015-09-15 06:05:41 +02:00
|
|
|
#include <functional>
|
2016-11-15 02:08:47 +01:00
|
|
|
#include <thread>
|
2016-01-19 23:50:18 +01:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2012-05-25 02:18:41 +02:00
|
|
|
|
2016-09-01 00:07:18 +02:00
|
|
|
#include <android-base/file.h>
|
2016-06-28 23:48:45 +02:00
|
|
|
#include <android-base/macros.h>
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/parseint.h>
|
2016-01-20 17:32:08 +01:00
|
|
|
#include <android-base/parsenetaddress.h>
|
2016-04-25 23:31:18 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/strings.h>
|
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"
|
2015-12-12 04:07:01 +01:00
|
|
|
#include "diagnose_usb.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
#include "fastboot.h"
|
2014-03-12 02:28:15 +01:00
|
|
|
#include "fs.h"
|
2016-01-20 17:32:08 +01:00
|
|
|
#include "tcp.h"
|
2015-10-30 19:22:01 +01:00
|
|
|
#include "transport.h"
|
2016-02-06 00:35:09 +01:00
|
|
|
#include "udp.h"
|
2015-10-30 19:22:01 +01:00
|
|
|
#include "usb.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2012-05-25 02:18:41 +02:00
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2011-04-05 02:54:59 +02:00
|
|
|
char cur_product[FB_RESPONSE_SZ + 1];
|
|
|
|
|
2016-01-20 17:32:08 +01:00
|
|
|
static const char* serial = nullptr;
|
|
|
|
static const char* product = nullptr;
|
|
|
|
static const char* cmdline = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
static unsigned short vendor_id = 0;
|
2012-04-06 21:39:30 +02:00
|
|
|
static int long_listing = 0;
|
2012-05-25 02:18:41 +02:00
|
|
|
static int64_t sparse_limit = -1;
|
|
|
|
static int64_t target_sparse_limit = -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
static unsigned page_size = 2048;
|
|
|
|
static unsigned base_addr = 0x10000000;
|
|
|
|
static unsigned kernel_offset = 0x00008000;
|
|
|
|
static unsigned ramdisk_offset = 0x01000000;
|
|
|
|
static unsigned second_offset = 0x00f00000;
|
|
|
|
static unsigned tags_offset = 0x00000100;
|
2013-03-08 02:06:41 +01:00
|
|
|
|
2015-11-27 10:29:37 +01:00
|
|
|
static const std::string convert_fbe_marker_filename("convert_fbe");
|
|
|
|
|
2013-06-28 18:54:59 +02:00
|
|
|
enum fb_buffer_type {
|
|
|
|
FB_BUFFER,
|
|
|
|
FB_BUFFER_SPARSE,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct fastboot_buffer {
|
|
|
|
enum fb_buffer_type type;
|
2015-04-08 05:12:50 +02:00
|
|
|
void* data;
|
|
|
|
int64_t sz;
|
2013-06-28 18:54:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct {
|
2016-06-29 18:26:44 +02:00
|
|
|
char img_name[17];
|
|
|
|
char sig_name[17];
|
2013-06-28 18:54:59 +02:00
|
|
|
char part_name[9];
|
|
|
|
bool is_optional;
|
2016-06-29 18:26:44 +02:00
|
|
|
bool is_secondary;
|
2014-05-28 23:10:01 +02:00
|
|
|
} images[] = {
|
2016-06-29 18:26:44 +02:00
|
|
|
{"boot.img", "boot.sig", "boot", false, false},
|
|
|
|
{"boot_other.img", "boot.sig", "boot", true, true},
|
|
|
|
{"recovery.img", "recovery.sig", "recovery", true, false},
|
|
|
|
{"system.img", "system.sig", "system", false, false},
|
|
|
|
{"system_other.img", "system.sig", "system", true, true},
|
|
|
|
{"vendor.img", "vendor.sig", "vendor", true, false},
|
|
|
|
{"vendor_other.img", "vendor.sig", "vendor", true, true},
|
2013-06-28 18:54:59 +02:00
|
|
|
};
|
2009-04-29 01:05:07 +02:00
|
|
|
|
2016-06-29 18:26:44 +02:00
|
|
|
static std::string find_item_given_name(const char* img_name, const char* product) {
|
|
|
|
if(product) {
|
2016-09-01 00:07:18 +02:00
|
|
|
std::string path = android::base::GetExecutablePath();
|
2016-06-29 18:26:44 +02:00
|
|
|
path.erase(path.find_last_of('/'));
|
|
|
|
return android::base::StringPrintf("%s/../../../target/product/%s/%s",
|
|
|
|
path.c_str(), product, img_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *dir = getenv("ANDROID_PRODUCT_OUT");
|
|
|
|
if (dir == nullptr || dir[0] == '\0') {
|
|
|
|
die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
|
|
|
|
}
|
|
|
|
|
|
|
|
return android::base::StringPrintf("%s/%s", dir, img_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string find_item(const char* item, const char* product) {
|
2015-03-19 06:47:09 +01:00
|
|
|
const char *fn;
|
2016-06-29 18:26:44 +02:00
|
|
|
|
2016-04-25 23:31:18 +02:00
|
|
|
if (!strcmp(item,"boot")) {
|
2009-03-04 04:32:55 +01:00
|
|
|
fn = "boot.img";
|
|
|
|
} else if(!strcmp(item,"recovery")) {
|
|
|
|
fn = "recovery.img";
|
|
|
|
} else if(!strcmp(item,"system")) {
|
|
|
|
fn = "system.img";
|
2014-05-28 23:10:01 +02:00
|
|
|
} else if(!strcmp(item,"vendor")) {
|
|
|
|
fn = "vendor.img";
|
2009-03-04 04:32:55 +01:00
|
|
|
} else if(!strcmp(item,"userdata")) {
|
|
|
|
fn = "userdata.img";
|
2011-09-30 23:39:25 +02:00
|
|
|
} else if(!strcmp(item,"cache")) {
|
|
|
|
fn = "cache.img";
|
2009-03-04 04:32:55 +01:00
|
|
|
} else if(!strcmp(item,"info")) {
|
|
|
|
fn = "android-info.txt";
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,"unknown partition '%s'\n", item);
|
2016-04-25 23:31:18 +02:00
|
|
|
return "";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-06-29 18:26:44 +02:00
|
|
|
return find_item_given_name(fn, product);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
static int64_t get_file_size(int fd) {
|
|
|
|
struct stat sb;
|
|
|
|
return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
static void* load_fd(int fd, int64_t* sz) {
|
2011-12-08 20:59:38 +01:00
|
|
|
int errno_tmp;
|
2015-04-08 05:12:50 +02:00
|
|
|
char* data = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
*sz = get_file_size(fd);
|
|
|
|
if (*sz < 0) {
|
2013-06-28 18:54:59 +02:00
|
|
|
goto oops;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
data = (char*) malloc(*sz);
|
|
|
|
if (data == nullptr) goto oops;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
if(read(fd, data, *sz) != *sz) goto oops;
|
2009-03-04 04:32:55 +01:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
|
|
|
oops:
|
2011-12-08 20:59:38 +01:00
|
|
|
errno_tmp = errno;
|
2009-03-04 04:32:55 +01:00
|
|
|
close(fd);
|
|
|
|
if(data != 0) free(data);
|
2011-12-08 20:59:38 +01:00
|
|
|
errno = errno_tmp;
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-25 23:31:18 +02:00
|
|
|
static void* load_file(const std::string& path, int64_t* sz) {
|
|
|
|
int fd = open(path.c_str(), O_RDONLY | O_BINARY);
|
2015-04-08 05:12:50 +02:00
|
|
|
if (fd == -1) return nullptr;
|
|
|
|
return load_fd(fd, sz);
|
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
|
|
|
// Require a matching vendor id if the user specified one with -i.
|
2015-08-20 02:49:45 +02:00
|
|
|
if (vendor_id != 0 && info->dev_vendor != vendor_id) {
|
2015-08-11 00:30:54 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-04-06 21:39:30 +02:00
|
|
|
// require matching serial number or device path if requested
|
2009-03-04 04:32:55 +01:00
|
|
|
// at the command line with the -s option.
|
2012-06-06 20:53:33 +02:00
|
|
|
if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
|
|
|
|
strcmp(local_serial, info->device_path) != 0)) return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
static int match_fastboot(usb_ifc_info* info) {
|
2013-03-08 02:06:41 +01:00
|
|
|
return match_fastboot_with_serial(info, serial);
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
static int list_devices_callback(usb_ifc_info* info) {
|
|
|
|
if (match_fastboot_with_serial(info, nullptr) == 0) {
|
2015-12-12 04:07:01 +01:00
|
|
|
std::string serial = info->serial_number;
|
2009-10-07 03:07:49 +02:00
|
|
|
if (!info->writable) {
|
2015-12-12 04:07:01 +01:00
|
|
|
serial = UsbNoPermissionsShortHelpText();
|
2009-10-07 03:07:49 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
if (!serial[0]) {
|
|
|
|
serial = "????????????";
|
|
|
|
}
|
2012-06-05 05:29:11 +02:00
|
|
|
// output compatible with "adb devices"
|
2012-04-06 21:39:30 +02:00
|
|
|
if (!long_listing) {
|
2015-12-12 04:07:01 +01:00
|
|
|
printf("%s\tfastboot", serial.c_str());
|
2012-04-06 21:39:30 +02:00
|
|
|
} else {
|
2015-12-12 04:07:01 +01:00
|
|
|
printf("%-22s fastboot", serial.c_str());
|
|
|
|
if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
|
2012-04-06 21:39:30 +02:00
|
|
|
}
|
2015-12-12 04:07:01 +01:00
|
|
|
putchar('\n');
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-01-20 17:32:08 +01:00
|
|
|
// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
|
|
|
|
// a specific device, otherwise the first USB device found will be used.
|
|
|
|
//
|
|
|
|
// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
|
|
|
|
// Otherwise it blocks until the target is available.
|
|
|
|
//
|
|
|
|
// The returned Transport is a singleton, so multiple calls to this function will return the same
|
|
|
|
// object, and the caller should not attempt to delete the returned Transport.
|
2015-10-30 19:22:01 +01:00
|
|
|
static Transport* open_device() {
|
|
|
|
static Transport* transport = nullptr;
|
2016-01-20 17:32:08 +01:00
|
|
|
bool announce = true;
|
|
|
|
|
|
|
|
if (transport != nullptr) {
|
|
|
|
return transport;
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:35:09 +01:00
|
|
|
Socket::Protocol protocol = Socket::Protocol::kTcp;
|
2016-01-20 17:32:08 +01:00
|
|
|
std::string host;
|
2016-02-06 00:35:09 +01:00
|
|
|
int port = 0;
|
|
|
|
if (serial != nullptr) {
|
|
|
|
const char* net_address = nullptr;
|
|
|
|
|
|
|
|
if (android::base::StartsWith(serial, "tcp:")) {
|
|
|
|
protocol = Socket::Protocol::kTcp;
|
|
|
|
port = tcp::kDefaultPort;
|
|
|
|
net_address = serial + strlen("tcp:");
|
|
|
|
} else if (android::base::StartsWith(serial, "udp:")) {
|
|
|
|
protocol = Socket::Protocol::kUdp;
|
|
|
|
port = udp::kDefaultPort;
|
|
|
|
net_address = serial + strlen("udp:");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (net_address != nullptr) {
|
|
|
|
std::string error;
|
|
|
|
if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
|
|
|
|
fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
|
|
|
|
error.c_str());
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-01-20 17:32:08 +01:00
|
|
|
}
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-01-20 17:32:08 +01:00
|
|
|
while (true) {
|
|
|
|
if (!host.empty()) {
|
|
|
|
std::string error;
|
2016-02-06 00:35:09 +01:00
|
|
|
if (protocol == Socket::Protocol::kTcp) {
|
|
|
|
transport = tcp::Connect(host, port, &error).release();
|
|
|
|
} else if (protocol == Socket::Protocol::kUdp) {
|
|
|
|
transport = udp::Connect(host, port, &error).release();
|
|
|
|
}
|
|
|
|
|
2016-01-20 17:32:08 +01:00
|
|
|
if (transport == nullptr && announce) {
|
|
|
|
fprintf(stderr, "error: %s\n", error.c_str());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
transport = usb_open(match_fastboot);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transport != nullptr) {
|
|
|
|
return transport;
|
|
|
|
}
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (announce) {
|
2016-01-20 17:32:08 +01:00
|
|
|
announce = false;
|
2015-08-20 02:49:45 +02:00
|
|
|
fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2016-11-15 02:08:47 +01:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
static void list_devices() {
|
2009-03-04 04:32:55 +01:00
|
|
|
// We don't actually open a USB device here,
|
|
|
|
// just getting our callback called so we can
|
|
|
|
// list all the connected devices.
|
|
|
|
usb_open(list_devices_callback);
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
static void usage() {
|
2009-03-04 04:32:55 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
|
|
|
|
"usage: fastboot [ <option> ] <command>\n"
|
|
|
|
"\n"
|
|
|
|
"commands:\n"
|
2015-06-10 20:58:14 +02:00
|
|
|
" update <filename> Reflash device from update.zip.\n"
|
2016-06-28 04:43:11 +02:00
|
|
|
" Sets the flashed slot as active.\n"
|
2015-06-10 20:58:14 +02:00
|
|
|
" flashall Flash boot, system, vendor, and --\n"
|
2016-06-28 04:43:11 +02:00
|
|
|
" if found -- recovery. If the device\n"
|
|
|
|
" supports slots, the slot that has\n"
|
|
|
|
" been flashed to is set as active.\n"
|
2016-06-29 18:26:44 +02:00
|
|
|
" Secondary images may be flashed to\n"
|
|
|
|
" an inactive slot.\n"
|
2015-06-10 20:58:14 +02:00
|
|
|
" flash <partition> [ <filename> ] Write a file to a flash partition.\n"
|
|
|
|
" flashing lock Locks the device. Prevents flashing.\n"
|
|
|
|
" flashing unlock Unlocks the device. Allows flashing\n"
|
|
|
|
" any partition except\n"
|
|
|
|
" bootloader-related partitions.\n"
|
|
|
|
" flashing lock_critical Prevents flashing bootloader-related\n"
|
|
|
|
" partitions.\n"
|
|
|
|
" flashing unlock_critical Enables flashing bootloader-related\n"
|
|
|
|
" partitions.\n"
|
|
|
|
" flashing get_unlock_ability Queries bootloader to see if the\n"
|
|
|
|
" device is unlocked.\n"
|
2015-09-01 17:15:23 +02:00
|
|
|
" flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
|
2015-09-03 05:15:48 +02:00
|
|
|
" unlock nonce.\n"
|
|
|
|
" flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
|
2015-09-01 17:15:23 +02:00
|
|
|
" flashing lock_bootloader Locks the bootloader to prevent\n"
|
2015-09-03 05:15:48 +02:00
|
|
|
" bootloader version rollback.\n"
|
2015-06-10 20:58:14 +02:00
|
|
|
" erase <partition> Erase a flash partition.\n"
|
|
|
|
" format[:[<fs type>][:[<size>]] <partition>\n"
|
|
|
|
" Format a flash partition. Can\n"
|
|
|
|
" override the fs type and/or size\n"
|
|
|
|
" the bootloader reports.\n"
|
|
|
|
" getvar <variable> Display a bootloader variable.\n"
|
2016-07-01 04:25:31 +02:00
|
|
|
" set_active <slot> Sets the active slot. If slots are\n"
|
2015-11-26 00:17:10 +01:00
|
|
|
" not supported, this does nothing.\n"
|
2015-06-10 20:58:14 +02:00
|
|
|
" boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
|
|
|
|
" flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
|
|
|
|
" Create bootimage and flash it.\n"
|
|
|
|
" devices [-l] List all connected devices [with\n"
|
|
|
|
" device paths].\n"
|
|
|
|
" continue Continue with autoboot.\n"
|
2017-01-04 04:39:13 +01:00
|
|
|
" reboot [bootloader|emergency] Reboot device [into bootloader or emergency mode].\n"
|
2015-06-10 20:58:14 +02:00
|
|
|
" reboot-bootloader Reboot device into bootloader.\n"
|
|
|
|
" help Show this help message.\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
"\n"
|
|
|
|
"options:\n"
|
2015-06-10 20:58:14 +02:00
|
|
|
" -w Erase userdata and cache (and format\n"
|
|
|
|
" if supported by partition type).\n"
|
|
|
|
" -u Do not erase partition before\n"
|
|
|
|
" formatting.\n"
|
2016-01-20 17:32:08 +01:00
|
|
|
" -s <specific device> Specify a device. For USB, provide either\n"
|
|
|
|
" a serial number or path to device port.\n"
|
2016-03-25 03:51:05 +01:00
|
|
|
" For ethernet, provide an address in the\n"
|
|
|
|
" form <protocol>:<hostname>[:port] where\n"
|
2016-02-06 00:35:09 +01:00
|
|
|
" <protocol> is either tcp or udp.\n"
|
2015-06-10 20:58:14 +02:00
|
|
|
" -p <product> Specify product name.\n"
|
|
|
|
" -c <cmdline> Override kernel commandline.\n"
|
|
|
|
" -i <vendor id> Specify a custom USB vendor id.\n"
|
2015-11-12 02:29:37 +01:00
|
|
|
" -b, --base <base_addr> Specify a custom kernel base\n"
|
2015-06-10 20:58:14 +02:00
|
|
|
" address (default: 0x10000000).\n"
|
2015-11-12 02:29:37 +01:00
|
|
|
" --kernel-offset Specify a custom kernel offset.\n"
|
|
|
|
" (default: 0x00008000)\n"
|
|
|
|
" --ramdisk-offset Specify a custom ramdisk offset.\n"
|
|
|
|
" (default: 0x01000000)\n"
|
|
|
|
" --tags-offset Specify a custom tags offset.\n"
|
|
|
|
" (default: 0x00000100)\n"
|
|
|
|
" -n, --page-size <page size> Specify the nand page size\n"
|
2015-06-10 20:58:14 +02:00
|
|
|
" (default: 2048).\n"
|
|
|
|
" -S <size>[K|M|G] Automatically sparse files greater\n"
|
|
|
|
" than 'size'. 0 to disable.\n"
|
2016-07-01 04:25:31 +02:00
|
|
|
" --slot <slot> Specify slot name to be used if the\n"
|
|
|
|
" device supports slots. All operations\n"
|
|
|
|
" on partitions that support slots will\n"
|
|
|
|
" be done on the slot specified.\n"
|
|
|
|
" 'all' can be given to refer to all slots.\n"
|
|
|
|
" 'other' can be given to refer to a\n"
|
|
|
|
" non-current slot. If this flag is not\n"
|
|
|
|
" used, slotted partitions will default\n"
|
|
|
|
" to the current active slot.\n"
|
|
|
|
" -a, --set-active[=<slot>] Sets the active slot. If no slot is\n"
|
2015-11-12 01:15:30 +01:00
|
|
|
" provided, this will default to the value\n"
|
|
|
|
" given by --slot. If slots are not\n"
|
2015-11-26 00:17:10 +01:00
|
|
|
" supported, this does nothing. This will\n"
|
|
|
|
" run after all non-reboot commands.\n"
|
2016-07-14 05:03:25 +02:00
|
|
|
" --skip-secondary Will not flash secondary slots when\n"
|
|
|
|
" performing a flashall or update. This\n"
|
|
|
|
" will preserve data on other slots.\n"
|
2016-09-26 19:26:21 +02:00
|
|
|
" --skip-reboot Will not reboot the device when\n"
|
|
|
|
" performing commands that normally\n"
|
|
|
|
" trigger a reboot.\n"
|
2015-11-27 10:29:37 +01:00
|
|
|
#if !defined(_WIN32)
|
|
|
|
" --wipe-and-use-fbe On devices which support it,\n"
|
|
|
|
" erase userdata and cache, and\n"
|
|
|
|
" enable file-based encryption\n"
|
|
|
|
#endif
|
2015-11-12 02:29:37 +01:00
|
|
|
" --unbuffered Do not buffer input or output.\n"
|
|
|
|
" --version Display version.\n"
|
|
|
|
" -h, --help show this message.\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
);
|
|
|
|
}
|
2015-11-27 10:29:37 +01:00
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
static void* load_bootable_image(const char* kernel, const char* ramdisk,
|
|
|
|
const char* secondstage, int64_t* sz,
|
|
|
|
const char* cmdline) {
|
|
|
|
if (kernel == nullptr) {
|
2009-03-04 04:32:55 +01:00
|
|
|
fprintf(stderr, "no image specified\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
int64_t ksize;
|
|
|
|
void* kdata = load_file(kernel, &ksize);
|
|
|
|
if (kdata == nullptr) {
|
2011-12-08 20:59:38 +01:00
|
|
|
fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
// Is this actually a boot image?
|
2009-03-04 04:32:55 +01:00
|
|
|
if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
|
2015-04-08 05:12:50 +02:00
|
|
|
if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
if (ramdisk) {
|
2009-03-04 04:32:55 +01:00
|
|
|
fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
*sz = ksize;
|
|
|
|
return kdata;
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
void* rdata = nullptr;
|
|
|
|
int64_t rsize = 0;
|
|
|
|
if (ramdisk) {
|
2009-03-04 04:32:55 +01:00
|
|
|
rdata = load_file(ramdisk, &rsize);
|
2015-04-08 05:12:50 +02:00
|
|
|
if (rdata == nullptr) {
|
2011-12-08 20:59:38 +01:00
|
|
|
fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
void* sdata = nullptr;
|
|
|
|
int64_t ssize = 0;
|
2014-07-17 17:17:54 +02:00
|
|
|
if (secondstage) {
|
|
|
|
sdata = load_file(secondstage, &ssize);
|
2015-04-08 05:12:50 +02:00
|
|
|
if (sdata == nullptr) {
|
2014-07-17 17:17:54 +02:00
|
|
|
fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
fprintf(stderr,"creating boot image...\n");
|
2015-04-08 05:12:50 +02:00
|
|
|
int64_t bsize = 0;
|
|
|
|
void* bdata = mkbootimg(kdata, ksize, kernel_offset,
|
2013-03-08 02:06:41 +01:00
|
|
|
rdata, rsize, ramdisk_offset,
|
2014-07-17 17:17:54 +02:00
|
|
|
sdata, ssize, second_offset,
|
2013-03-08 02:06:41 +01:00
|
|
|
page_size, base_addr, tags_offset, &bsize);
|
2015-04-08 05:12:50 +02:00
|
|
|
if (bdata == nullptr) {
|
2009-03-04 04:32:55 +01:00
|
|
|
fprintf(stderr,"failed to create boot.img\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2015-04-08 05:12:50 +02:00
|
|
|
if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
|
|
|
|
fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
|
2009-03-04 04:32:55 +01:00
|
|
|
*sz = bsize;
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
return bdata;
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2015-06-25 23:39:19 +02:00
|
|
|
ZipString zip_entry_name(entry_name);
|
2015-03-19 07:12:44 +01:00
|
|
|
ZipEntry zip_entry;
|
|
|
|
if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
|
2015-03-19 19:44:32 +01:00
|
|
|
fprintf(stderr, "archive does not contain '%s'\n", entry_name);
|
2014-05-08 01:31:59 +02:00
|
|
|
return 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-03-19 07:12:44 +01:00
|
|
|
*sz = zip_entry.uncompressed_length;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-03-19 07:12:44 +01:00
|
|
|
uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
|
2015-04-08 05:12:50 +02:00
|
|
|
if (data == nullptr) {
|
|
|
|
fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
|
2014-05-08 01:31:59 +02:00
|
|
|
return 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-03-19 19:44:32 +01:00
|
|
|
int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
|
|
|
|
if (error != 0) {
|
|
|
|
fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
|
2009-03-04 04:32:55 +01:00
|
|
|
free(data);
|
2014-05-08 01:31:59 +02:00
|
|
|
return 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2014-05-08 01:31:59 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
char filename[PATH_MAX];
|
|
|
|
if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
|
|
|
|
fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fopen(filename, "w+bTD");
|
|
|
|
}
|
|
|
|
|
|
|
|
#define tmpfile win32_tmpfile
|
|
|
|
|
2015-11-27 10:29:37 +01:00
|
|
|
static std::string make_temporary_directory() {
|
|
|
|
fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static std::string make_temporary_directory() {
|
|
|
|
const char *tmpdir = getenv("TMPDIR");
|
|
|
|
if (tmpdir == nullptr) {
|
|
|
|
tmpdir = P_tmpdir;
|
|
|
|
}
|
|
|
|
std::string result = std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
|
|
|
|
if (mkdtemp(&result[0]) == NULL) {
|
|
|
|
fprintf(stderr, "Unable to create temporary directory: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-06-04 00:22:11 +02:00
|
|
|
#endif
|
|
|
|
|
2015-11-27 10:29:37 +01:00
|
|
|
static std::string create_fbemarker_tmpdir() {
|
|
|
|
std::string dir = make_temporary_directory();
|
|
|
|
if (dir.empty()) {
|
|
|
|
fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
std::string marker_file = dir + "/" + convert_fbe_marker_filename;
|
|
|
|
int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
|
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
|
|
|
|
marker_file.c_str(), errno, strerror(errno));
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void delete_fbemarker_tmpdir(const std::string& dir) {
|
|
|
|
std::string marker_file = dir + "/" + convert_fbe_marker_filename;
|
|
|
|
if (unlink(marker_file.c_str()) == -1) {
|
|
|
|
fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
|
|
|
|
marker_file.c_str(), errno, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rmdir(dir.c_str()) == -1) {
|
|
|
|
fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
|
|
|
|
dir.c_str(), errno, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 19:44:32 +01:00
|
|
|
static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
|
|
|
|
FILE* fp = tmpfile();
|
2015-04-08 05:12:50 +02:00
|
|
|
if (fp == nullptr) {
|
2015-03-19 19:44:32 +01:00
|
|
|
fprintf(stderr, "failed to create temporary file for '%s': %s\n",
|
|
|
|
entry_name, strerror(errno));
|
2014-05-08 01:31:59 +02:00
|
|
|
return -1;
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
|
|
|
|
2015-06-25 23:39:19 +02:00
|
|
|
ZipString zip_entry_name(entry_name);
|
2015-03-19 19:44:32 +01:00
|
|
|
ZipEntry zip_entry;
|
|
|
|
if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
|
|
|
|
fprintf(stderr, "archive does not contain '%s'\n", entry_name);
|
2016-08-11 20:34:16 +02:00
|
|
|
fclose(fp);
|
2013-06-28 18:54:59 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-03-19 19:44:32 +01:00
|
|
|
int fd = fileno(fp);
|
|
|
|
int error = ExtractEntryToFile(zip, &zip_entry, fd);
|
|
|
|
if (error != 0) {
|
|
|
|
fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
|
2016-08-11 20:34:16 +02:00
|
|
|
fclose(fp);
|
2015-03-19 19:44:32 +01:00
|
|
|
return -1;
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
|
|
|
|
2014-05-08 01:31:59 +02:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
2016-08-11 20:34:16 +02:00
|
|
|
// TODO: We're leaking 'fp' here.
|
2013-06-28 18:54:59 +02:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
static char *strip(char *s)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
while(*s && isspace(*s)) s++;
|
|
|
|
n = strlen(s);
|
|
|
|
while(n-- > 0) {
|
|
|
|
if(!isspace(s[n])) break;
|
|
|
|
s[n] = 0;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_OPTIONS 32
|
|
|
|
static int setup_requirement_line(char *name)
|
|
|
|
{
|
|
|
|
char *val[MAX_OPTIONS];
|
2015-04-08 05:12:50 +02:00
|
|
|
char *prod = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
unsigned n, count;
|
|
|
|
char *x;
|
|
|
|
int invert = 0;
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
if (!strncmp(name, "reject ", 7)) {
|
|
|
|
name += 7;
|
|
|
|
invert = 1;
|
|
|
|
} else if (!strncmp(name, "require ", 8)) {
|
|
|
|
name += 8;
|
|
|
|
invert = 0;
|
2011-04-05 02:54:59 +02:00
|
|
|
} else if (!strncmp(name, "require-for-product:", 20)) {
|
|
|
|
// Get the product and point name past it
|
|
|
|
prod = name + 20;
|
|
|
|
name = strchr(name, ' ');
|
|
|
|
if (!name) return -1;
|
|
|
|
*name = 0;
|
|
|
|
name += 1;
|
|
|
|
invert = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
x = strchr(name, '=');
|
|
|
|
if (x == 0) return 0;
|
|
|
|
*x = 0;
|
|
|
|
val[0] = x + 1;
|
|
|
|
|
|
|
|
for(count = 1; count < MAX_OPTIONS; count++) {
|
|
|
|
x = strchr(val[count - 1],'|');
|
|
|
|
if (x == 0) break;
|
|
|
|
*x = 0;
|
|
|
|
val[count] = x + 1;
|
|
|
|
}
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
name = strip(name);
|
|
|
|
for(n = 0; n < count; n++) val[n] = strip(val[n]);
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
name = strip(name);
|
|
|
|
if (name == 0) return -1;
|
|
|
|
|
2015-03-19 06:47:09 +01:00
|
|
|
const char* var = name;
|
|
|
|
// Work around an unfortunate name mismatch.
|
|
|
|
if (!strcmp(name,"board")) var = "product";
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-03-19 06:47:09 +01:00
|
|
|
const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
|
2009-03-04 04:32:55 +01:00
|
|
|
if (out == 0) return -1;
|
|
|
|
|
|
|
|
for(n = 0; n < count; n++) {
|
|
|
|
out[n] = strdup(strip(val[n]));
|
2013-10-29 22:12:46 +01:00
|
|
|
if (out[n] == 0) {
|
|
|
|
for(size_t i = 0; i < n; ++i) {
|
|
|
|
free((char*) out[i]);
|
|
|
|
}
|
|
|
|
free(out);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-03-19 06:47:09 +01:00
|
|
|
fb_queue_require(prod, var, invert, n, out);
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
static void setup_requirements(char* data, int64_t sz) {
|
|
|
|
char* s = data;
|
2009-03-04 04:32:55 +01:00
|
|
|
while (sz-- > 0) {
|
2015-04-08 05:12:50 +02:00
|
|
|
if (*s == '\n') {
|
2009-03-04 04:32:55 +01:00
|
|
|
*s++ = 0;
|
|
|
|
if (setup_requirement_line(data)) {
|
|
|
|
die("out of memory");
|
|
|
|
}
|
|
|
|
data = s;
|
|
|
|
} else {
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
static void queue_info_dump() {
|
2009-03-04 04:32:55 +01:00
|
|
|
fb_queue_notice("--------------------------------------------");
|
|
|
|
fb_queue_display("version-bootloader", "Bootloader Version...");
|
|
|
|
fb_queue_display("version-baseband", "Baseband Version.....");
|
|
|
|
fb_queue_display("serialno", "Serial Number........");
|
|
|
|
fb_queue_notice("--------------------------------------------");
|
|
|
|
}
|
|
|
|
|
2013-06-28 18:54:59 +02:00
|
|
|
static struct sparse_file **load_sparse_files(int fd, int max_size)
|
2012-05-25 02:18:41 +02:00
|
|
|
{
|
2015-03-31 21:09:29 +02:00
|
|
|
struct sparse_file* s = sparse_file_import_auto(fd, false, true);
|
2012-05-25 02:18:41 +02:00
|
|
|
if (!s) {
|
2013-06-28 18:54:59 +02:00
|
|
|
die("cannot sparse read file\n");
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
int files = sparse_file_resparse(s, max_size, nullptr, 0);
|
2012-05-25 02:18:41 +02:00
|
|
|
if (files < 0) {
|
2013-06-28 18:54:59 +02:00
|
|
|
die("Failed to resparse\n");
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
|
2015-03-19 06:47:09 +01:00
|
|
|
sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
|
2012-05-25 02:18:41 +02:00
|
|
|
if (!out_s) {
|
|
|
|
die("Failed to allocate sparse file array\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
files = sparse_file_resparse(s, max_size, out_s, files);
|
|
|
|
if (files < 0) {
|
2013-06-28 18:54:59 +02:00
|
|
|
die("Failed to resparse\n");
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return out_s;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
static int64_t get_target_sparse_limit(Transport* transport) {
|
2015-10-30 19:49:47 +01:00
|
|
|
std::string max_download_size;
|
2015-10-30 19:22:01 +01:00
|
|
|
if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
|
|
|
|
max_download_size.empty()) {
|
2015-11-02 18:01:53 +01:00
|
|
|
fprintf(stderr, "target didn't report max-download-size\n");
|
2015-10-30 19:49:47 +01:00
|
|
|
return 0;
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
|
2015-11-03 00:51:12 +01:00
|
|
|
// Some bootloaders (angler, for example) send spurious whitespace too.
|
|
|
|
max_download_size = android::base::Trim(max_download_size);
|
|
|
|
|
2015-10-30 19:49:47 +01:00
|
|
|
uint64_t limit;
|
2016-10-12 02:09:00 +02:00
|
|
|
if (!android::base::ParseUint(max_download_size, &limit)) {
|
2015-11-02 18:01:53 +01:00
|
|
|
fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
|
2015-10-30 19:49:47 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (limit > 0) {
|
|
|
|
fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
|
|
|
|
}
|
2012-05-25 02:18:41 +02:00
|
|
|
return limit;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
static int64_t get_sparse_limit(Transport* transport, int64_t size) {
|
2012-05-25 02:18:41 +02:00
|
|
|
int64_t limit;
|
|
|
|
|
|
|
|
if (sparse_limit == 0) {
|
|
|
|
return 0;
|
|
|
|
} else if (sparse_limit > 0) {
|
|
|
|
limit = sparse_limit;
|
|
|
|
} else {
|
|
|
|
if (target_sparse_limit == -1) {
|
2015-10-30 19:22:01 +01:00
|
|
|
target_sparse_limit = get_target_sparse_limit(transport);
|
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) {
|
|
|
|
return limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:49:47 +01:00
|
|
|
// Until we get lazy inode table init working in make_ext4fs, we need to
|
|
|
|
// erase partitions of type ext4 before flashing a filesystem so no stale
|
|
|
|
// inodes are left lying around. Otherwise, e2fsck gets very upset.
|
2015-10-30 19:22:01 +01:00
|
|
|
static bool needs_erase(Transport* transport, const char* partition) {
|
2015-11-02 23:05:57 +01:00
|
|
|
std::string partition_type;
|
2015-10-30 19:22:01 +01:00
|
|
|
if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
|
2015-11-02 23:05:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return partition_type == "ext4";
|
2012-09-29 23:46:25 +02:00
|
|
|
}
|
|
|
|
|
2016-05-11 21:39:27 +02:00
|
|
|
static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
|
2015-04-08 05:12:50 +02:00
|
|
|
int64_t sz = get_file_size(fd);
|
|
|
|
if (sz == -1) {
|
2016-05-11 21:39:27 +02:00
|
|
|
return false;
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
2014-03-12 02:28:15 +01:00
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
lseek64(fd, 0, SEEK_SET);
|
2015-10-30 19:22:01 +01:00
|
|
|
int64_t limit = get_sparse_limit(transport, sz);
|
2012-05-25 02:18:41 +02:00
|
|
|
if (limit) {
|
2015-04-08 05:12:50 +02:00
|
|
|
sparse_file** s = load_sparse_files(fd, limit);
|
|
|
|
if (s == nullptr) {
|
2016-05-11 21:39:27 +02:00
|
|
|
return false;
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
2013-06-28 18:54:59 +02:00
|
|
|
buf->type = FB_BUFFER_SPARSE;
|
|
|
|
buf->data = s;
|
2012-05-25 02:18:41 +02:00
|
|
|
} else {
|
2015-04-08 05:12:50 +02:00
|
|
|
void* data = load_fd(fd, &sz);
|
|
|
|
if (data == nullptr) return -1;
|
2013-06-28 18:54:59 +02:00
|
|
|
buf->type = FB_BUFFER;
|
2014-05-06 04:43:15 +02:00
|
|
|
buf->data = data;
|
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
|
|
|
}
|
|
|
|
|
2016-05-11 21:39:27 +02:00
|
|
|
static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
|
|
|
|
int fd = open(fname, O_RDONLY | O_BINARY);
|
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
2015-10-30 19:22:01 +01:00
|
|
|
return load_buf_fd(transport, fd, buf);
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void flash_buf(const char *pname, struct fastboot_buffer *buf)
|
|
|
|
{
|
2015-03-19 06:47:09 +01:00
|
|
|
sparse_file** s;
|
2013-06-28 18:54:59 +02:00
|
|
|
|
|
|
|
switch (buf->type) {
|
2016-01-19 23:50:18 +01:00
|
|
|
case FB_BUFFER_SPARSE: {
|
|
|
|
std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
|
2015-03-19 06:47:09 +01:00
|
|
|
s = reinterpret_cast<sparse_file**>(buf->data);
|
2013-06-28 18:54:59 +02:00
|
|
|
while (*s) {
|
2015-04-08 05:12:50 +02:00
|
|
|
int64_t sz = sparse_file_len(*s, true, false);
|
2016-01-19 23:50:18 +01:00
|
|
|
sparse_files.emplace_back(*s, sz);
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sparse_files.size(); ++i) {
|
|
|
|
const auto& pair = sparse_files[i];
|
|
|
|
fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
|
|
|
break;
|
2016-01-19 23:50:18 +01:00
|
|
|
}
|
|
|
|
|
2013-06-28 18:54:59 +02:00
|
|
|
case FB_BUFFER:
|
|
|
|
fb_queue_flash(pname, buf->data, buf->sz);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
die("unknown buffer type: %d", buf->type);
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 04:25:31 +02:00
|
|
|
static std::string get_current_slot(Transport* transport)
|
|
|
|
{
|
|
|
|
std::string current_slot;
|
|
|
|
if (fb_getvar(transport, "current-slot", ¤t_slot)) {
|
|
|
|
if (current_slot == "_a") return "a"; // Legacy support
|
|
|
|
if (current_slot == "_b") return "b"; // Legacy support
|
|
|
|
return current_slot;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Legacy support
|
|
|
|
static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
|
2015-09-15 06:05:41 +02:00
|
|
|
std::vector<std::string> suffixes;
|
|
|
|
std::string suffix_list;
|
2015-10-30 19:22:01 +01:00
|
|
|
if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
|
2016-06-28 08:03:48 +02:00
|
|
|
return suffixes;
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
2016-06-28 08:03:48 +02:00
|
|
|
suffixes = android::base::Split(suffix_list, ",");
|
|
|
|
// Unfortunately some devices will return an error message in the
|
|
|
|
// guise of a valid value. If we only see only one suffix, it's probably
|
|
|
|
// not real.
|
|
|
|
if (suffixes.size() == 1) {
|
|
|
|
suffixes.clear();
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
2016-06-28 08:03:48 +02:00
|
|
|
return suffixes;
|
|
|
|
}
|
|
|
|
|
2016-07-01 04:25:31 +02:00
|
|
|
// Legacy support
|
|
|
|
static bool supports_AB_obsolete(Transport* transport) {
|
|
|
|
return !get_suffixes_obsolete(transport).empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_slot_count(Transport* transport) {
|
|
|
|
std::string var;
|
|
|
|
int count;
|
|
|
|
if (!fb_getvar(transport, "slot-count", &var)) {
|
|
|
|
if (supports_AB_obsolete(transport)) return 2; // Legacy support
|
|
|
|
}
|
2016-10-12 02:09:00 +02:00
|
|
|
if (!android::base::ParseInt(var, &count)) return 0;
|
2016-07-01 04:25:31 +02:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2016-06-29 18:26:44 +02:00
|
|
|
static bool supports_AB(Transport* transport) {
|
2016-07-01 04:25:31 +02:00
|
|
|
return get_slot_count(transport) >= 2;
|
2016-06-29 18:26:44 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 08:03:48 +02:00
|
|
|
// Given a current slot, this returns what the 'other' slot is.
|
2016-07-01 04:25:31 +02:00
|
|
|
static std::string get_other_slot(const std::string& current_slot, int count) {
|
|
|
|
if (count == 0) return "";
|
|
|
|
|
|
|
|
char next = (current_slot[0] - 'a' + 1)%count + 'a';
|
|
|
|
return std::string(1, next);
|
|
|
|
}
|
|
|
|
|
2016-06-29 18:26:44 +02:00
|
|
|
static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
|
2016-07-01 04:25:31 +02:00
|
|
|
return get_other_slot(current_slot, get_slot_count(transport));
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
|
|
|
|
2016-07-01 04:25:31 +02:00
|
|
|
static std::string get_other_slot(Transport* transport, int count) {
|
|
|
|
return get_other_slot(get_current_slot(transport), count);
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
|
|
|
|
2016-06-29 18:26:44 +02:00
|
|
|
static std::string get_other_slot(Transport* transport) {
|
2016-07-01 04:25:31 +02:00
|
|
|
return get_other_slot(get_current_slot(transport), get_slot_count(transport));
|
2016-06-29 18:26:44 +02:00
|
|
|
}
|
|
|
|
|
2016-07-01 04:25:31 +02:00
|
|
|
static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
|
|
|
|
std::string slot = slot_name;
|
|
|
|
if (slot == "_a") slot = "a"; // Legacy support
|
|
|
|
if (slot == "_b") slot = "b"; // Legacy support
|
|
|
|
if (slot == "all") {
|
2015-11-26 00:17:10 +01:00
|
|
|
if (allow_all) {
|
|
|
|
return "all";
|
|
|
|
} else {
|
2016-07-01 04:25:31 +02:00
|
|
|
int count = get_slot_count(transport);
|
|
|
|
if (count > 0) {
|
|
|
|
return "a";
|
2015-11-26 00:17:10 +01:00
|
|
|
} else {
|
2016-01-06 01:54:40 +01: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
|
|
|
|
2016-07-01 04:25:31 +02:00
|
|
|
int count = get_slot_count(transport);
|
|
|
|
if (count == 0) die("Device does not support slots.\n");
|
2016-01-06 01:54:40 +01:00
|
|
|
|
2016-07-01 04:25:31 +02:00
|
|
|
if (slot == "other") {
|
|
|
|
std::string other = get_other_slot(transport, count);
|
2016-06-28 08:03:48 +02:00
|
|
|
if (other == "") {
|
|
|
|
die("No known slots.");
|
2016-01-06 01:54:40 +01:00
|
|
|
}
|
2016-06-28 08:03:48 +02:00
|
|
|
return other;
|
2016-01-06 01:54:40 +01:00
|
|
|
}
|
|
|
|
|
2016-07-01 04:25:31 +02:00
|
|
|
if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
|
|
|
|
|
|
|
|
fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
|
|
|
|
for (int i=0; i<count; i++) {
|
|
|
|
fprintf(stderr, "%c\n", (char)(i + 'a'));
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
2016-07-01 04:25:31 +02:00
|
|
|
|
2015-09-15 06:05:41 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-07-01 04:25:31 +02:00
|
|
|
static std::string verify_slot(Transport* transport, const std::string& slot) {
|
2015-11-26 00:17:10 +01:00
|
|
|
return verify_slot(transport, slot, true);
|
|
|
|
}
|
|
|
|
|
2016-07-01 04:25:31 +02:00
|
|
|
static void do_for_partition(Transport* transport, 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;
|
|
|
|
|
2016-07-01 04:25:31 +02:00
|
|
|
if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
|
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 == "") {
|
|
|
|
current_slot = get_current_slot(transport);
|
|
|
|
if (current_slot == "") {
|
2015-09-15 06:05:41 +02:00
|
|
|
die("Failed to identify current slot.\n");
|
|
|
|
}
|
2016-07-01 04:25:31 +02:00
|
|
|
func(part + "_" + current_slot);
|
2015-09-15 06:05:41 +02:00
|
|
|
} else {
|
2016-07-01 04:25:31 +02:00
|
|
|
func(part + '_' + slot);
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-07-01 04:25:31 +02:00
|
|
|
if (force_slot && slot != "") {
|
2015-10-30 19:22:01 +01:00
|
|
|
fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
|
2016-07-01 04:25:31 +02:00
|
|
|
part.c_str(), slot.c_str());
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
|
|
|
func(part);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
|
|
|
|
* empty, it will use the current slot. If slot is "all", it will return a list of all possible
|
|
|
|
* partition names. If force_slot is true, it will fail if a slot is specified, and the given
|
|
|
|
* partition does not support slots.
|
2015-09-15 06:05:41 +02:00
|
|
|
*/
|
2016-07-01 04:25:31 +02:00
|
|
|
static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
|
2016-07-28 01:25:51 +02:00
|
|
|
const std::function<void(const std::string&)>& func, bool force_slot) {
|
2015-11-12 01:13:13 +01:00
|
|
|
std::string has_slot;
|
2015-09-15 06:05:41 +02:00
|
|
|
|
2016-07-01 04:25:31 +02:00
|
|
|
if (slot == "all") {
|
|
|
|
if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
|
|
|
|
die("Could not check if partition %s has slot.", part.c_str());
|
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
|
|
|
for (int i=0; i < get_slot_count(transport); i++) {
|
|
|
|
do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
|
|
|
} else {
|
2015-10-30 19:22:01 +01:00
|
|
|
do_for_partition(transport, part, "", func, force_slot);
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
|
|
|
} else {
|
2015-10-30 19:22:01 +01:00
|
|
|
do_for_partition(transport, part, slot, func, force_slot);
|
2015-09-15 06:05:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
static void do_flash(Transport* transport, const char* pname, const char* fname) {
|
2013-06-28 18:54:59 +02:00
|
|
|
struct fastboot_buffer buf;
|
|
|
|
|
2016-05-11 21:39:27 +02:00
|
|
|
if (!load_buf(transport, fname, &buf)) {
|
|
|
|
die("cannot load '%s': %s", fname, strerror(errno));
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
|
|
|
flash_buf(pname, &buf);
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
static void do_update_signature(ZipArchiveHandle zip, char* fn) {
|
|
|
|
int64_t sz;
|
2015-03-19 07:12:44 +01:00
|
|
|
void* data = unzip_file(zip, fn, &sz);
|
2015-04-08 05:12:50 +02:00
|
|
|
if (data == nullptr) return;
|
2009-03-04 04:32:55 +01:00
|
|
|
fb_queue_download("signature", data, sz);
|
|
|
|
fb_queue_command("signature", "installing signature");
|
|
|
|
}
|
|
|
|
|
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.
|
2016-06-29 18:26:44 +02:00
|
|
|
static void set_active(Transport* transport, const std::string& slot_override) {
|
2016-07-01 04:25:31 +02:00
|
|
|
std::string separator = "";
|
2016-06-29 18:26:44 +02:00
|
|
|
if (!supports_AB(transport)) {
|
2016-07-01 04:25:31 +02:00
|
|
|
if (supports_AB_obsolete(transport)) {
|
|
|
|
separator = "_"; // Legacy support
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (slot_override != "") {
|
|
|
|
fb_set_active((separator + slot_override).c_str());
|
2016-06-28 04:43:11 +02:00
|
|
|
} else {
|
2016-07-01 04:25:31 +02:00
|
|
|
std::string current_slot = get_current_slot(transport);
|
|
|
|
if (current_slot != "") {
|
|
|
|
fb_set_active((separator + current_slot).c_str());
|
2016-06-28 04:43:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 05:03:25 +02:00
|
|
|
static void do_update(Transport* transport, const char* filename, const std::string& slot_override, bool erase_first, bool skip_secondary) {
|
2009-03-04 04:32:55 +01:00
|
|
|
queue_info_dump();
|
|
|
|
|
2011-04-05 02:54:59 +02:00
|
|
|
fb_queue_query_save("product", cur_product, sizeof(cur_product));
|
|
|
|
|
2015-03-19 07:12:44 +01:00
|
|
|
ZipArchiveHandle zip;
|
2015-03-19 19:44:32 +01:00
|
|
|
int error = OpenArchive(filename, &zip);
|
|
|
|
if (error != 0) {
|
2015-05-11 17:59:46 +02:00
|
|
|
CloseArchive(zip);
|
2015-03-19 19:44:32 +01:00
|
|
|
die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
|
2015-03-19 07:12:44 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
int64_t sz;
|
2015-03-19 06:47:09 +01:00
|
|
|
void* data = unzip_file(zip, "android-info.txt", &sz);
|
2015-04-08 05:12:50 +02:00
|
|
|
if (data == nullptr) {
|
2015-05-11 17:59:46 +02:00
|
|
|
CloseArchive(zip);
|
2015-03-19 18:30:53 +01:00
|
|
|
die("update package '%s' has no android-info.txt", filename);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-03-19 06:47:09 +01:00
|
|
|
setup_requirements(reinterpret_cast<char*>(data), sz);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-06-29 18:26:44 +02:00
|
|
|
std::string secondary;
|
2016-07-14 05:03:25 +02:00
|
|
|
if (!skip_secondary) {
|
2016-06-29 18:26:44 +02:00
|
|
|
if (slot_override != "") {
|
|
|
|
secondary = get_other_slot(transport, slot_override);
|
|
|
|
} else {
|
|
|
|
secondary = get_other_slot(transport);
|
|
|
|
}
|
|
|
|
if (secondary == "") {
|
|
|
|
if (supports_AB(transport)) {
|
|
|
|
fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
|
|
|
|
}
|
2016-07-14 05:03:25 +02:00
|
|
|
skip_secondary = true;
|
2016-06-29 18:26:44 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-28 23:48:45 +02:00
|
|
|
for (size_t i = 0; i < arraysize(images); ++i) {
|
2016-06-29 18:26:44 +02:00
|
|
|
const char* slot = slot_override.c_str();
|
|
|
|
if (images[i].is_secondary) {
|
2016-07-14 05:03:25 +02:00
|
|
|
if (!skip_secondary) {
|
2016-06-29 18:26:44 +02:00
|
|
|
slot = secondary.c_str();
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 06:47:09 +01:00
|
|
|
int fd = unzip_to_file(zip, images[i].img_name);
|
2015-06-03 06:37:05 +02:00
|
|
|
if (fd == -1) {
|
|
|
|
if (images[i].is_optional) {
|
2013-06-28 18:54:59 +02:00
|
|
|
continue;
|
2015-06-03 06:37:05 +02:00
|
|
|
}
|
2015-05-11 17:59:46 +02:00
|
|
|
CloseArchive(zip);
|
2015-06-03 06:37:05 +02:00
|
|
|
exit(1); // unzip_to_file already explained why.
|
2012-09-29 23:46:25 +02:00
|
|
|
}
|
2015-03-19 06:47:09 +01:00
|
|
|
fastboot_buffer buf;
|
2016-05-11 21:39:27 +02:00
|
|
|
if (!load_buf_fd(transport, fd, &buf)) {
|
|
|
|
die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
|
|
|
|
}
|
2015-09-15 06:05:41 +02:00
|
|
|
|
|
|
|
auto update = [&](const std::string &partition) {
|
|
|
|
do_update_signature(zip, images[i].sig_name);
|
2015-10-30 19:22:01 +01:00
|
|
|
if (erase_first && needs_erase(transport, partition.c_str())) {
|
2015-09-15 06:05:41 +02:00
|
|
|
fb_queue_erase(partition.c_str());
|
|
|
|
}
|
|
|
|
flash_buf(partition.c_str(), &buf);
|
|
|
|
/* not closing the fd here since the sparse code keeps the fd around
|
|
|
|
* but hasn't mmaped data yet. The tmpfile will get cleaned up when the
|
|
|
|
* program exits.
|
|
|
|
*/
|
|
|
|
};
|
2016-06-29 18:26:44 +02:00
|
|
|
do_for_partitions(transport, images[i].part_name, slot, update, false);
|
2012-09-29 23:46:25 +02:00
|
|
|
}
|
2015-03-19 07:12:44 +01:00
|
|
|
|
|
|
|
CloseArchive(zip);
|
2016-07-14 05:03:25 +02:00
|
|
|
if (slot_override == "all") {
|
|
|
|
set_active(transport, "a");
|
|
|
|
} else {
|
|
|
|
set_active(transport, slot_override);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-06-29 18:26:44 +02:00
|
|
|
static void do_send_signature(const std::string& fn) {
|
|
|
|
std::size_t extension_loc = fn.find(".img");
|
|
|
|
if (extension_loc == std::string::npos) return;
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2016-06-29 18:26:44 +02:00
|
|
|
std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
|
2015-04-08 05:12:50 +02:00
|
|
|
|
|
|
|
int64_t sz;
|
2016-06-29 18:26:44 +02:00
|
|
|
void* data = load_file(fs_sig.c_str(), &sz);
|
2015-04-08 05:12:50 +02:00
|
|
|
if (data == nullptr) return;
|
2016-04-25 23:31:18 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
fb_queue_download("signature", data, sz);
|
|
|
|
fb_queue_command("signature", "installing signature");
|
|
|
|
}
|
|
|
|
|
2016-07-14 05:03:25 +02:00
|
|
|
static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
|
2016-06-29 18:26:44 +02:00
|
|
|
std::string fname;
|
2009-03-04 04:32:55 +01:00
|
|
|
queue_info_dump();
|
|
|
|
|
2011-04-05 02:54:59 +02:00
|
|
|
fb_queue_query_save("product", cur_product, sizeof(cur_product));
|
|
|
|
|
2016-07-14 05:03:25 +02:00
|
|
|
fname = find_item("info", product);
|
2016-04-25 23:31:18 +02:00
|
|
|
if (fname.empty()) die("cannot find android-info.txt");
|
2015-03-19 06:47:09 +01:00
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
int64_t sz;
|
2016-07-14 05:03:25 +02:00
|
|
|
void* data = load_file(fname.c_str(), &sz);
|
2015-04-08 05:12:50 +02:00
|
|
|
if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-03-19 06:47:09 +01:00
|
|
|
setup_requirements(reinterpret_cast<char*>(data), sz);
|
|
|
|
|
2016-06-29 18:26:44 +02:00
|
|
|
std::string secondary;
|
2016-07-14 05:03:25 +02:00
|
|
|
if (!skip_secondary) {
|
2016-06-29 18:26:44 +02:00
|
|
|
if (slot_override != "") {
|
|
|
|
secondary = get_other_slot(transport, slot_override);
|
|
|
|
} else {
|
|
|
|
secondary = get_other_slot(transport);
|
|
|
|
}
|
|
|
|
if (secondary == "") {
|
|
|
|
if (supports_AB(transport)) {
|
|
|
|
fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
|
|
|
|
}
|
2016-07-14 05:03:25 +02:00
|
|
|
skip_secondary = true;
|
2016-06-29 18:26:44 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-19 06:47:09 +01:00
|
|
|
|
2016-06-28 23:48:45 +02:00
|
|
|
for (size_t i = 0; i < arraysize(images); i++) {
|
2016-06-29 18:26:44 +02:00
|
|
|
const char* slot = NULL;
|
|
|
|
if (images[i].is_secondary) {
|
2016-07-14 05:03:25 +02:00
|
|
|
if (!skip_secondary) slot = secondary.c_str();
|
2016-06-29 18:26:44 +02:00
|
|
|
} else {
|
2016-07-14 05:03:25 +02:00
|
|
|
slot = slot_override.c_str();
|
2016-06-29 18:26:44 +02:00
|
|
|
}
|
|
|
|
if (!slot) continue;
|
|
|
|
fname = find_item_given_name(images[i].img_name, product);
|
2015-03-19 06:47:09 +01:00
|
|
|
fastboot_buffer buf;
|
2016-05-11 21:39:27 +02:00
|
|
|
if (!load_buf(transport, fname.c_str(), &buf)) {
|
|
|
|
if (images[i].is_optional) continue;
|
2016-06-29 18:26:44 +02:00
|
|
|
die("could not load '%s': %s\n", images[i].img_name, strerror(errno));
|
2013-06-28 18:54:59 +02:00
|
|
|
}
|
2015-09-15 06:05:41 +02:00
|
|
|
|
|
|
|
auto flashall = [&](const std::string &partition) {
|
2016-04-25 23:31:18 +02:00
|
|
|
do_send_signature(fname.c_str());
|
2015-10-30 19:22:01 +01:00
|
|
|
if (erase_first && needs_erase(transport, partition.c_str())) {
|
2015-09-15 06:05:41 +02:00
|
|
|
fb_queue_erase(partition.c_str());
|
|
|
|
}
|
|
|
|
flash_buf(partition.c_str(), &buf);
|
|
|
|
};
|
2016-06-29 18:26:44 +02:00
|
|
|
do_for_partitions(transport, images[i].part_name, slot, flashall, false);
|
2012-09-29 23:46:25 +02:00
|
|
|
}
|
2016-06-28 04:43:11 +02:00
|
|
|
|
2016-07-14 05:03:25 +02:00
|
|
|
if (slot_override == "all") {
|
|
|
|
set_active(transport, "a");
|
|
|
|
} else {
|
|
|
|
set_active(transport, slot_override);
|
2012-09-29 23:46:25 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define skip(n) do { argc -= (n); argv += (n); } while (0)
|
2011-03-02 08:35:07 +01:00
|
|
|
#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-09-03 05:15:48 +02:00
|
|
|
static int do_bypass_unlock_command(int argc, char **argv)
|
2015-09-01 17:15:23 +02:00
|
|
|
{
|
|
|
|
if (argc <= 2) return 0;
|
|
|
|
skip(2);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process unlock_bootloader, we have to load the message file
|
|
|
|
* and send that to the remote device.
|
|
|
|
*/
|
|
|
|
require(1);
|
2015-09-03 05:33:44 +02:00
|
|
|
|
|
|
|
int64_t sz;
|
|
|
|
void* data = load_file(*argv, &sz);
|
|
|
|
if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
|
2015-09-01 17:15:23 +02:00
|
|
|
fb_queue_download("unlock_message", data, sz);
|
|
|
|
fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
|
|
|
|
skip(1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-03 05:15:48 +02:00
|
|
|
static int do_oem_command(int argc, char **argv)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
char command[256];
|
|
|
|
if (argc <= 1) return 0;
|
2011-02-26 03:38:53 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
command[0] = 0;
|
|
|
|
while(1) {
|
|
|
|
strcat(command,*argv);
|
|
|
|
skip(1);
|
|
|
|
if(argc == 0) break;
|
|
|
|
strcat(command," ");
|
|
|
|
}
|
|
|
|
|
2011-02-26 03:38:53 +01:00
|
|
|
fb_queue_command(command,"");
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-25 02:18:41 +02:00
|
|
|
static int64_t parse_num(const char *arg)
|
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
unsigned long long num;
|
|
|
|
|
|
|
|
num = strtoull(arg, &endptr, 0);
|
|
|
|
if (endptr == arg) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*endptr == 'k' || *endptr == 'K') {
|
|
|
|
if (num >= (-1ULL) / 1024) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
num *= 1024LL;
|
|
|
|
endptr++;
|
|
|
|
} else if (*endptr == 'm' || *endptr == 'M') {
|
|
|
|
if (num >= (-1ULL) / (1024 * 1024)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
num *= 1024LL * 1024LL;
|
|
|
|
endptr++;
|
|
|
|
} else if (*endptr == 'g' || *endptr == 'G') {
|
|
|
|
if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
num *= 1024LL * 1024LL * 1024LL;
|
|
|
|
endptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*endptr != '\0') {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num > INT64_MAX) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
static void fb_perform_format(Transport* transport,
|
2015-10-30 19:49:47 +01:00
|
|
|
const char* partition, int skip_if_not_supported,
|
2015-11-27 10:29:37 +01:00
|
|
|
const char* type_override, const char* size_override,
|
|
|
|
const std::string& initial_dir) {
|
2015-10-30 19:49:47 +01:00
|
|
|
std::string partition_type, partition_size;
|
|
|
|
|
2014-03-12 02:28:15 +01:00
|
|
|
struct fastboot_buffer buf;
|
2015-10-30 19:49:47 +01:00
|
|
|
const char* errMsg = nullptr;
|
|
|
|
const struct fs_generator* gen = nullptr;
|
2014-03-12 02:28:15 +01:00
|
|
|
int fd;
|
|
|
|
|
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
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
|
2014-03-12 02:28:15 +01:00
|
|
|
errMsg = "Can't determine partition type.\n";
|
|
|
|
goto failed;
|
|
|
|
}
|
2014-05-07 00:14:15 +02:00
|
|
|
if (type_override) {
|
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",
|
|
|
|
partition, partition_type.c_str(), type_override);
|
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
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
|
2014-03-12 02:28:15 +01:00
|
|
|
errMsg = "Unable to get partition size\n";
|
|
|
|
goto failed;
|
|
|
|
}
|
2014-05-07 00:14:15 +02:00
|
|
|
if (size_override) {
|
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",
|
|
|
|
partition, partition_size.c_str(), size_override);
|
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
|
|
|
}
|
2015-11-12 16:28:39 +01:00
|
|
|
// Some bootloaders (angler, for example), send spurious leading whitespace.
|
|
|
|
partition_size = android::base::Trim(partition_size);
|
|
|
|
// Some bootloaders (hammerhead, for example) use implicit hex.
|
|
|
|
// This code used to use strtol with base 16.
|
|
|
|
if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + 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;
|
|
|
|
}
|
2015-10-30 19:49:47 +01:00
|
|
|
fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
|
|
|
|
partition_type.c_str());
|
2014-03-12 02:28:15 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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)) {
|
2015-10-30 19:49:47 +01:00
|
|
|
fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
|
|
|
|
return;
|
|
|
|
}
|
2014-03-12 02:28:15 +01:00
|
|
|
|
|
|
|
fd = fileno(tmpfile());
|
2015-11-27 10:29:37 +01:00
|
|
|
if (fs_generator_generate(gen, fd, size, initial_dir)) {
|
2015-10-30 19:49:47 +01:00
|
|
|
fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
|
2014-03-12 02:28:15 +01:00
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-11 21:39:27 +02:00
|
|
|
if (!load_buf_fd(transport, fd, &buf)) {
|
2015-10-30 19:49:47 +01:00
|
|
|
fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
|
2014-03-12 02:28:15 +01:00
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2016-04-25 23:31:18 +02:00
|
|
|
fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
|
2014-03-12 02:28:15 +01:00
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2015-11-12 01:15:30 +01:00
|
|
|
bool wants_wipe = false;
|
|
|
|
bool wants_reboot = false;
|
|
|
|
bool wants_reboot_bootloader = false;
|
2017-01-04 04:39:13 +01:00
|
|
|
bool wants_reboot_emergency = false;
|
2016-09-26 19:26:21 +02:00
|
|
|
bool skip_reboot = false;
|
2015-11-12 01:15:30 +01:00
|
|
|
bool wants_set_active = false;
|
2016-07-14 05:03:25 +02:00
|
|
|
bool skip_secondary = false;
|
2015-04-08 05:12:50 +02:00
|
|
|
bool erase_first = true;
|
2015-11-27 10:29:37 +01:00
|
|
|
bool set_fbe_marker = false;
|
2009-03-04 04:32:55 +01:00
|
|
|
void *data;
|
2015-04-08 05:12:50 +02:00
|
|
|
int64_t sz;
|
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
|
|
|
|
2013-03-08 02:06:41 +01:00
|
|
|
const struct option longopts[] = {
|
|
|
|
{"base", required_argument, 0, 'b'},
|
|
|
|
{"kernel_offset", required_argument, 0, 'k'},
|
2015-11-12 02:29:37 +01:00
|
|
|
{"kernel-offset", required_argument, 0, 'k'},
|
2013-03-08 02:06:41 +01:00
|
|
|
{"page_size", required_argument, 0, 'n'},
|
2015-11-12 02:29:37 +01:00
|
|
|
{"page-size", required_argument, 0, 'n'},
|
2013-03-08 02:06:41 +01:00
|
|
|
{"ramdisk_offset", required_argument, 0, 'r'},
|
2015-11-12 02:29:37 +01:00
|
|
|
{"ramdisk-offset", required_argument, 0, 'r'},
|
2014-01-15 03:27:25 +01:00
|
|
|
{"tags_offset", required_argument, 0, 't'},
|
2015-11-12 02:29:37 +01:00
|
|
|
{"tags-offset", required_argument, 0, 't'},
|
2015-06-02 22:50:00 +02:00
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{"unbuffered", no_argument, 0, 0},
|
|
|
|
{"version", no_argument, 0, 0},
|
2015-09-15 06:05:41 +02:00
|
|
|
{"slot", required_argument, 0, 0},
|
2015-11-12 01:15:30 +01:00
|
|
|
{"set_active", optional_argument, 0, 'a'},
|
2015-11-12 02:29:37 +01:00
|
|
|
{"set-active", optional_argument, 0, 'a'},
|
2016-07-14 05:03:25 +02:00
|
|
|
{"skip-secondary", no_argument, 0, 0},
|
2016-09-26 19:26:21 +02:00
|
|
|
{"skip-reboot", no_argument, 0, 0},
|
2015-11-27 10:29:37 +01:00
|
|
|
#if !defined(_WIN32)
|
|
|
|
{"wipe-and-use-fbe", no_argument, 0, 0},
|
|
|
|
#endif
|
2013-03-08 02:06:41 +01:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
2012-05-23 02:53:34 +02:00
|
|
|
|
|
|
|
serial = getenv("ANDROID_SERIAL");
|
|
|
|
|
|
|
|
while (1) {
|
2015-11-12 01:15:30 +01:00
|
|
|
int c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:ha::", longopts, &longindex);
|
2012-05-23 02:53:34 +02:00
|
|
|
if (c < 0) {
|
|
|
|
break;
|
|
|
|
}
|
2013-03-08 02:06:41 +01:00
|
|
|
/* Alphabetical cases */
|
2012-05-23 02:53:34 +02:00
|
|
|
switch (c) {
|
2015-11-12 01:15:30 +01:00
|
|
|
case 'a':
|
|
|
|
wants_set_active = true;
|
|
|
|
if (optarg)
|
|
|
|
next_active = optarg;
|
|
|
|
break;
|
2012-05-23 02:53:34 +02:00
|
|
|
case 'b':
|
|
|
|
base_addr = strtoul(optarg, 0, 16);
|
|
|
|
break;
|
2013-03-08 02:06:41 +01:00
|
|
|
case 'c':
|
|
|
|
cmdline = optarg;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
case 'i': {
|
2015-04-08 05:12:50 +02:00
|
|
|
char *endptr = nullptr;
|
2013-03-08 02:06:41 +01:00
|
|
|
unsigned long val;
|
|
|
|
|
|
|
|
val = strtoul(optarg, &endptr, 0);
|
|
|
|
if (!endptr || *endptr != '\0' || (val & ~0xffff))
|
|
|
|
die("invalid vendor id '%s'", optarg);
|
|
|
|
vendor_id = (unsigned short)val;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'k':
|
|
|
|
kernel_offset = strtoul(optarg, 0, 16);
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
long_listing = 1;
|
|
|
|
break;
|
2012-05-23 02:53:34 +02:00
|
|
|
case 'n':
|
2015-04-08 05:12:50 +02:00
|
|
|
page_size = (unsigned)strtoul(optarg, nullptr, 0);
|
2012-05-23 02:53:34 +02:00
|
|
|
if (!page_size) die("invalid page size");
|
|
|
|
break;
|
2013-03-08 02:06:41 +01:00
|
|
|
case 'p':
|
|
|
|
product = optarg;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
ramdisk_offset = strtoul(optarg, 0, 16);
|
|
|
|
break;
|
2014-01-15 03:27:25 +01:00
|
|
|
case 't':
|
|
|
|
tags_offset = strtoul(optarg, 0, 16);
|
|
|
|
break;
|
2012-05-23 02:53:34 +02:00
|
|
|
case 's':
|
|
|
|
serial = optarg;
|
|
|
|
break;
|
2012-05-25 02:18:41 +02:00
|
|
|
case 'S':
|
|
|
|
sparse_limit = parse_num(optarg);
|
|
|
|
if (sparse_limit < 0) {
|
|
|
|
die("invalid sparse limit");
|
|
|
|
}
|
|
|
|
break;
|
2013-03-08 02:06:41 +01:00
|
|
|
case 'u':
|
2015-04-08 05:12:50 +02:00
|
|
|
erase_first = false;
|
2012-05-23 02:53:34 +02:00
|
|
|
break;
|
2013-03-08 02:06:41 +01:00
|
|
|
case 'w':
|
2015-11-12 01:15:30 +01:00
|
|
|
wants_wipe = true;
|
2012-05-23 02:53:34 +02:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
return 1;
|
2014-11-24 11:29:34 +01:00
|
|
|
case 0:
|
|
|
|
if (strcmp("unbuffered", longopts[longindex].name) == 0) {
|
2015-04-08 05:12:50 +02:00
|
|
|
setvbuf(stdout, nullptr, _IONBF, 0);
|
|
|
|
setvbuf(stderr, nullptr, _IONBF, 0);
|
2015-06-02 22:50:00 +02:00
|
|
|
} else if (strcmp("version", longopts[longindex].name) == 0) {
|
|
|
|
fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
|
|
|
|
return 0;
|
2015-09-15 06:05:41 +02:00
|
|
|
} else if (strcmp("slot", longopts[longindex].name) == 0) {
|
|
|
|
slot_override = std::string(optarg);
|
2016-07-14 05:03:25 +02:00
|
|
|
} else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
|
|
|
|
skip_secondary = true;
|
2016-09-26 19:26:21 +02:00
|
|
|
} else if (strcmp("skip-reboot", longopts[longindex].name) == 0 ) {
|
|
|
|
skip_reboot = true;
|
2015-11-27 10:29:37 +01:00
|
|
|
#if !defined(_WIN32)
|
|
|
|
} else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
|
|
|
|
wants_wipe = true;
|
|
|
|
set_fbe_marker = true;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Internal error in options processing for %s\n",
|
|
|
|
longopts[longindex].name);
|
|
|
|
return 1;
|
2014-11-24 11:29:34 +01:00
|
|
|
}
|
|
|
|
break;
|
2012-05-23 02:53:34 +02:00
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2012-05-23 02:53:34 +02:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2015-11-12 01:15:30 +01:00
|
|
|
if (argc == 0 && !wants_wipe && !wants_set_active) {
|
2009-03-04 04:32:55 +01:00
|
|
|
usage();
|
2010-04-23 21:38:51 +02:00
|
|
|
return 1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2012-07-25 01:36:41 +02:00
|
|
|
if (argc > 0 && !strcmp(*argv, "devices")) {
|
2012-05-23 02:53:34 +02:00
|
|
|
skip(1);
|
2009-03-04 04:32:55 +01:00
|
|
|
list_devices();
|
2011-02-26 03:38:53 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-30 03:17:06 +02:00
|
|
|
if (argc > 0 && !strcmp(*argv, "help")) {
|
|
|
|
usage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-07-01 04:25:31 +02:00
|
|
|
if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
|
|
|
|
fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
|
|
|
|
}
|
|
|
|
if (slot_override != "") slot_override = verify_slot(transport, slot_override);
|
|
|
|
if (next_active != "") next_active = verify_slot(transport, next_active, false);
|
2015-11-12 01:15:30 +01:00
|
|
|
|
|
|
|
if (wants_set_active) {
|
|
|
|
if (next_active == "") {
|
|
|
|
if (slot_override == "") {
|
2016-06-28 04:43:11 +02:00
|
|
|
std::string current_slot;
|
|
|
|
if (fb_getvar(transport, "current-slot", ¤t_slot)) {
|
2016-07-01 04:25:31 +02:00
|
|
|
next_active = verify_slot(transport, current_slot, false);
|
2016-06-28 04:43:11 +02:00
|
|
|
} else {
|
|
|
|
wants_set_active = false;
|
|
|
|
}
|
2015-11-12 01:15:30 +01:00
|
|
|
} else {
|
2016-07-01 04:25:31 +02:00
|
|
|
next_active = verify_slot(transport, slot_override, false);
|
2015-11-12 01:15:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-08 00:38:53 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
while (argc > 0) {
|
2015-10-30 19:49:47 +01:00
|
|
|
if (!strcmp(*argv, "getvar")) {
|
2009-03-04 04:32:55 +01:00
|
|
|
require(2);
|
|
|
|
fb_queue_display(argv[1], argv[1]);
|
|
|
|
skip(2);
|
|
|
|
} else if(!strcmp(*argv, "erase")) {
|
|
|
|
require(2);
|
2012-09-29 23:46:25 +02:00
|
|
|
|
2015-09-15 06:05:41 +02:00
|
|
|
auto erase = [&](const std::string &partition) {
|
2015-10-30 19:22:01 +01:00
|
|
|
std::string partition_type;
|
|
|
|
if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
|
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
|
|
|
|
2015-09-15 06:05:41 +02:00
|
|
|
fb_queue_erase(partition.c_str());
|
|
|
|
};
|
2016-07-01 04:25:31 +02:00
|
|
|
do_for_partitions(transport, argv[1], slot_override, erase, true);
|
2009-03-04 04:32:55 +01:00
|
|
|
skip(2);
|
2014-05-07 00:14:15 +02:00
|
|
|
} else if(!strncmp(*argv, "format", strlen("format"))) {
|
|
|
|
char *overrides;
|
2015-04-08 05:12:50 +02:00
|
|
|
char *type_override = nullptr;
|
|
|
|
char *size_override = nullptr;
|
2011-12-16 02:50:18 +01:00
|
|
|
require(2);
|
2014-05-07 00:14:15 +02:00
|
|
|
/*
|
|
|
|
* Parsing for: "format[:[type][:[size]]]"
|
|
|
|
* Some valid things:
|
|
|
|
* - select ontly the size, and leave default fs type:
|
|
|
|
* format::0x4000000 userdata
|
|
|
|
* - default fs type and size:
|
|
|
|
* format userdata
|
|
|
|
* format:: userdata
|
|
|
|
*/
|
|
|
|
overrides = strchr(*argv, ':');
|
|
|
|
if (overrides) {
|
|
|
|
overrides++;
|
|
|
|
size_override = strchr(overrides, ':');
|
|
|
|
if (size_override) {
|
|
|
|
size_override[0] = '\0';
|
|
|
|
size_override++;
|
|
|
|
}
|
|
|
|
type_override = overrides;
|
|
|
|
}
|
2015-04-08 05:12:50 +02:00
|
|
|
if (type_override && !type_override[0]) type_override = nullptr;
|
|
|
|
if (size_override && !size_override[0]) size_override = nullptr;
|
2015-09-15 06:05:41 +02:00
|
|
|
|
|
|
|
auto format = [&](const std::string &partition) {
|
2015-10-30 19:22:01 +01:00
|
|
|
if (erase_first && needs_erase(transport, partition.c_str())) {
|
2015-09-15 06:05:41 +02:00
|
|
|
fb_queue_erase(partition.c_str());
|
|
|
|
}
|
2015-11-27 10:29:37 +01:00
|
|
|
fb_perform_format(transport, partition.c_str(), 0,
|
|
|
|
type_override, size_override, "");
|
2015-09-15 06:05:41 +02:00
|
|
|
};
|
2016-07-01 04:25:31 +02:00
|
|
|
do_for_partitions(transport, argv[1], slot_override, format, true);
|
2011-12-16 02:50:18 +01:00
|
|
|
skip(2);
|
2009-03-04 04:32:55 +01:00
|
|
|
} else if(!strcmp(*argv, "signature")) {
|
|
|
|
require(2);
|
|
|
|
data = load_file(argv[1], &sz);
|
2015-04-08 05:12:50 +02:00
|
|
|
if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
|
2009-03-04 04:32:55 +01:00
|
|
|
if (sz != 256) die("signature must be 256 bytes");
|
|
|
|
fb_queue_download("signature", data, sz);
|
|
|
|
fb_queue_command("signature", "installing signature");
|
|
|
|
skip(2);
|
|
|
|
} else if(!strcmp(*argv, "reboot")) {
|
2015-11-12 01:15:30 +01:00
|
|
|
wants_reboot = true;
|
2009-03-04 04:32:55 +01:00
|
|
|
skip(1);
|
2015-02-25 19:02:00 +01:00
|
|
|
if (argc > 0) {
|
|
|
|
if (!strcmp(*argv, "bootloader")) {
|
2015-11-12 01:15:30 +01:00
|
|
|
wants_reboot = false;
|
|
|
|
wants_reboot_bootloader = true;
|
2015-02-25 19:02:00 +01:00
|
|
|
skip(1);
|
2017-01-26 04:07:46 +01:00
|
|
|
} else if (!strcmp(*argv, "emergency")) {
|
2017-01-04 04:39:13 +01:00
|
|
|
wants_reboot = false;
|
|
|
|
wants_reboot_emergency = true;
|
|
|
|
skip(1);
|
|
|
|
}
|
2015-02-25 19:02:00 +01:00
|
|
|
}
|
|
|
|
require(0);
|
2009-03-04 04:32:55 +01:00
|
|
|
} else if(!strcmp(*argv, "reboot-bootloader")) {
|
2015-11-12 01:15:30 +01:00
|
|
|
wants_reboot_bootloader = true;
|
2009-03-04 04:32:55 +01:00
|
|
|
skip(1);
|
|
|
|
} else if (!strcmp(*argv, "continue")) {
|
|
|
|
fb_queue_command("continue", "resuming boot");
|
|
|
|
skip(1);
|
|
|
|
} else if(!strcmp(*argv, "boot")) {
|
|
|
|
char *kname = 0;
|
|
|
|
char *rname = 0;
|
2014-07-17 17:17:54 +02:00
|
|
|
char *sname = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
skip(1);
|
|
|
|
if (argc > 0) {
|
|
|
|
kname = argv[0];
|
|
|
|
skip(1);
|
|
|
|
}
|
|
|
|
if (argc > 0) {
|
|
|
|
rname = argv[0];
|
|
|
|
skip(1);
|
|
|
|
}
|
2014-07-17 17:17:54 +02:00
|
|
|
if (argc > 0) {
|
|
|
|
sname = argv[0];
|
|
|
|
skip(1);
|
|
|
|
}
|
|
|
|
data = load_bootable_image(kname, rname, sname, &sz, cmdline);
|
2009-03-04 04:32:55 +01:00
|
|
|
if (data == 0) return 1;
|
|
|
|
fb_queue_download("boot.img", data, sz);
|
|
|
|
fb_queue_command("boot", "booting");
|
|
|
|
} else if(!strcmp(*argv, "flash")) {
|
2016-04-25 23:31:18 +02:00
|
|
|
char* pname = argv[1];
|
|
|
|
std::string fname;
|
2009-03-04 04:32:55 +01:00
|
|
|
require(2);
|
|
|
|
if (argc > 2) {
|
|
|
|
fname = argv[2];
|
|
|
|
skip(3);
|
|
|
|
} else {
|
|
|
|
fname = find_item(pname, product);
|
|
|
|
skip(2);
|
|
|
|
}
|
2016-04-25 23:31:18 +02:00
|
|
|
if (fname.empty()) die("cannot determine image filename for '%s'", pname);
|
2015-09-15 06:05:41 +02:00
|
|
|
|
|
|
|
auto flash = [&](const std::string &partition) {
|
2015-10-30 19:22:01 +01:00
|
|
|
if (erase_first && needs_erase(transport, partition.c_str())) {
|
2015-09-15 06:05:41 +02:00
|
|
|
fb_queue_erase(partition.c_str());
|
|
|
|
}
|
2016-04-25 23:31:18 +02:00
|
|
|
do_flash(transport, partition.c_str(), fname.c_str());
|
2015-09-15 06:05:41 +02:00
|
|
|
};
|
2016-07-01 04:25:31 +02:00
|
|
|
do_for_partitions(transport, pname, slot_override, flash, true);
|
2009-03-04 04:32:55 +01:00
|
|
|
} else if(!strcmp(*argv, "flash:raw")) {
|
|
|
|
char *kname = argv[2];
|
|
|
|
char *rname = 0;
|
2014-07-17 17:17:54 +02:00
|
|
|
char *sname = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
require(3);
|
2014-07-17 17:17:54 +02:00
|
|
|
skip(3);
|
|
|
|
if (argc > 0) {
|
|
|
|
rname = argv[0];
|
|
|
|
skip(1);
|
|
|
|
}
|
|
|
|
if (argc > 0) {
|
|
|
|
sname = argv[0];
|
|
|
|
skip(1);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2014-07-17 17:17:54 +02:00
|
|
|
data = load_bootable_image(kname, rname, sname, &sz, cmdline);
|
2009-03-04 04:32:55 +01:00
|
|
|
if (data == 0) die("cannot load bootable image");
|
2015-09-15 06:05:41 +02:00
|
|
|
auto flashraw = [&](const std::string &partition) {
|
|
|
|
fb_queue_flash(partition.c_str(), data, sz);
|
|
|
|
};
|
2016-07-01 04:25:31 +02:00
|
|
|
do_for_partitions(transport, argv[1], slot_override, flashraw, true);
|
2009-03-04 04:32:55 +01:00
|
|
|
} else if(!strcmp(*argv, "flashall")) {
|
|
|
|
skip(1);
|
2016-06-29 18:26:44 +02:00
|
|
|
if (slot_override == "all") {
|
2016-07-14 05:03:25 +02:00
|
|
|
fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
|
|
|
|
do_flashall(transport, slot_override, erase_first, true);
|
2016-06-29 18:26:44 +02:00
|
|
|
} else {
|
2016-07-14 05:03:25 +02:00
|
|
|
do_flashall(transport, slot_override, erase_first, skip_secondary);
|
2016-06-29 18:26:44 +02:00
|
|
|
}
|
2015-11-12 01:15:30 +01:00
|
|
|
wants_reboot = true;
|
2009-03-04 04:32:55 +01:00
|
|
|
} else if(!strcmp(*argv, "update")) {
|
2016-07-14 05:03:25 +02:00
|
|
|
bool slot_all = (slot_override == "all");
|
|
|
|
if (slot_all) {
|
|
|
|
fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
if (argc > 1) {
|
2016-07-14 05:03:25 +02:00
|
|
|
do_update(transport, argv[1], slot_override, erase_first, skip_secondary || slot_all);
|
2009-03-04 04:32:55 +01:00
|
|
|
skip(2);
|
|
|
|
} else {
|
2016-07-14 05:03:25 +02:00
|
|
|
do_update(transport, "update.zip", slot_override, erase_first, skip_secondary || slot_all);
|
2009-03-04 04:32:55 +01:00
|
|
|
skip(1);
|
|
|
|
}
|
2016-09-26 19:26:21 +02:00
|
|
|
wants_reboot = true;
|
2015-11-26 00:17:10 +01:00
|
|
|
} else if(!strcmp(*argv, "set_active")) {
|
|
|
|
require(2);
|
2016-07-01 04:25:31 +02:00
|
|
|
std::string slot = verify_slot(transport, std::string(argv[1]), false);
|
2016-12-16 01:16:10 +01:00
|
|
|
// Legacy support: verify_slot() removes leading underscores, we need to put them back
|
|
|
|
// in for old bootloaders. Legacy bootloaders do not have the slot-count variable but
|
|
|
|
// do have slot-suffixes.
|
|
|
|
std::string var;
|
|
|
|
if (!fb_getvar(transport, "slot-count", &var) &&
|
|
|
|
fb_getvar(transport, "slot-suffixes", &var)) {
|
|
|
|
slot = "_" + slot;
|
|
|
|
}
|
2015-11-26 00:17:10 +01:00
|
|
|
fb_set_active(slot.c_str());
|
|
|
|
skip(2);
|
2009-03-04 04:32:55 +01:00
|
|
|
} else if(!strcmp(*argv, "oem")) {
|
|
|
|
argc = do_oem_command(argc, argv);
|
2015-09-01 17:15:23 +02:00
|
|
|
} else if(!strcmp(*argv, "flashing")) {
|
|
|
|
if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
|
|
|
|
!strcmp(*(argv+1), "lock") ||
|
|
|
|
!strcmp(*(argv+1), "unlock_critical") ||
|
|
|
|
!strcmp(*(argv+1), "lock_critical") ||
|
|
|
|
!strcmp(*(argv+1), "get_unlock_ability") ||
|
|
|
|
!strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
|
|
|
|
!strcmp(*(argv+1), "lock_bootloader"))) {
|
|
|
|
argc = do_oem_command(argc, argv);
|
|
|
|
} else
|
|
|
|
if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
|
|
|
|
argc = do_bypass_unlock_command(argc, argv);
|
2015-05-16 01:43:47 +02:00
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
|
|
|
usage();
|
2011-02-26 03:38:53 +01:00
|
|
|
return 1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wants_wipe) {
|
2015-10-30 19:49:47 +01:00
|
|
|
fprintf(stderr, "wiping userdata...\n");
|
2009-03-04 04:32:55 +01:00
|
|
|
fb_queue_erase("userdata");
|
2015-11-27 10:29:37 +01:00
|
|
|
if (set_fbe_marker) {
|
|
|
|
fprintf(stderr, "setting FBE marker...\n");
|
|
|
|
std::string initial_userdata_dir = create_fbemarker_tmpdir();
|
|
|
|
if (initial_userdata_dir.empty()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
|
|
|
|
delete_fbemarker_tmpdir(initial_userdata_dir);
|
|
|
|
} else {
|
|
|
|
fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
|
|
|
|
}
|
2015-10-30 19:49:47 +01:00
|
|
|
|
|
|
|
std::string cache_type;
|
2015-10-30 19:22:01 +01:00
|
|
|
if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
|
2015-10-30 19:49:47 +01:00
|
|
|
fprintf(stderr, "wiping cache...\n");
|
|
|
|
fb_queue_erase("cache");
|
2015-11-27 10:29:37 +01:00
|
|
|
fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
|
2015-10-30 19:49:47 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-11-12 01:15:30 +01:00
|
|
|
if (wants_set_active) {
|
|
|
|
fb_set_active(next_active.c_str());
|
|
|
|
}
|
2016-09-26 19:26:21 +02:00
|
|
|
if (wants_reboot && !skip_reboot) {
|
2009-03-04 04:32:55 +01:00
|
|
|
fb_queue_reboot();
|
2014-06-24 17:04:54 +02:00
|
|
|
fb_queue_wait_for_disconnect();
|
2009-03-04 04:32:55 +01:00
|
|
|
} else if (wants_reboot_bootloader) {
|
|
|
|
fb_queue_command("reboot-bootloader", "rebooting into bootloader");
|
2013-10-02 15:35:38 +02:00
|
|
|
fb_queue_wait_for_disconnect();
|
2017-01-04 04:39:13 +01:00
|
|
|
} else if (wants_reboot_emergency) {
|
|
|
|
fb_queue_command("reboot-emergency", "rebooting into emergency download (EDL) mode");
|
|
|
|
fb_queue_wait_for_disconnect();
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|