blob: 862366687e3a06963e437c188eb9c9f24e4a3480 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070029#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <errno.h>
31#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070032#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070033#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070034#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070035#include <stdint.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070041#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070042#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080043
Elliott Hughes290a2282016-11-14 17:08:47 -080044#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070045#include <functional>
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080047#include <utility>
48#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070049
Elliott Hughes82ff3152016-08-31 15:07:18 -070050#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070051#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080053#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070054#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080055#include <android-base/strings.h>
Jin Qian4a335822017-04-18 16:23:18 -070056#include <android-base/test_utils.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050057#include <android-base/unique_fd.h>
Colin Crossf8387882012-05-24 17:18:41 -070058#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070059#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Elliott Hughes253c18d2015-03-18 22:47:09 -070061#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080062#include "diagnose_usb.h"
Aaron Wisnerdb511202018-06-26 15:38:35 -050063#include "engine.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070064#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080065#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070066#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080067#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070068#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Chris Fries0ea946c2017-04-12 10:25:57 -050070using android::base::unique_fd;
71
Colin Crossf8387882012-05-24 17:18:41 -070072#ifndef O_BINARY
73#define O_BINARY 0
74#endif
75
Wink Savilleb98762f2011-04-04 17:54:59 -070076char cur_product[FB_RESPONSE_SZ + 1];
77
David Pursell2ec418a2016-01-20 08:32:08 -080078static const char* serial = nullptr;
Elliott Hughes577e8b42018-04-05 16:12:47 -070079
Elliott Hughes577e8b42018-04-05 16:12:47 -070080static bool g_long_listing = false;
Chris Fries6a999712017-04-04 09:52:47 -050081// Don't resparse files in too-big chunks.
82// libsparse will support INT_MAX, but this results in large allocations, so
83// let's keep it at 1GB to avoid memory pressure on the host.
84static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Elliott Hughes542370d2018-04-19 19:49:44 -070085static uint64_t sparse_limit = 0;
Colin Crossf8387882012-05-24 17:18:41 -070086static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
Elliott Hughes577e8b42018-04-05 16:12:47 -070088static unsigned g_base_addr = 0x10000000;
89static boot_img_hdr_v1 g_boot_img_hdr = {};
90static std::string g_cmdline;
JP Abgrall7b8970c2013-03-07 17:06:41 -080091
David Zeuthenb6ea4352017-08-07 14:29:26 -040092static bool g_disable_verity = false;
93static bool g_disable_verification = false;
94
Paul Crowley8f7f56e2015-11-27 09:29:37 +000095static const std::string convert_fbe_marker_filename("convert_fbe");
96
Rom Lemarchand622810c2013-06-28 09:54:59 -070097enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -050098 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -070099 FB_BUFFER_SPARSE,
100};
101
102struct fastboot_buffer {
103 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700104 void* data;
105 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500106 int fd;
David Anderson32e376f2018-08-16 13:43:11 -0700107 int64_t image_size;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700108};
109
David Anderson32e376f2018-08-16 13:43:11 -0700110struct Image {
Elliott Hughes45964a82017-05-03 22:43:23 -0700111 const char* nickname;
112 const char* img_name;
113 const char* sig_name;
114 const char* part_name;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700115 bool optional_if_no_image;
116 bool optional_if_no_partition;
117 bool IsSecondary() const { return nickname == nullptr; }
David Anderson32e376f2018-08-16 13:43:11 -0700118};
119
120static Image images[] = {
Dario Frenic7ea1af2018-05-25 16:07:19 +0100121 // clang-format off
Elliott Hughes45964a82017-05-03 22:43:23 -0700122 { "boot", "boot.img", "boot.sig", "boot", false, false },
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700123 { nullptr, "boot_other.img", "boot.sig", "boot", true, false },
Elliott Hughes45964a82017-05-03 22:43:23 -0700124 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, false },
Dmitry Shmidtde8c08c2017-05-15 10:24:04 -0700125 { "dts", "dt.img", "dt.sig", "dts", true, false },
Bowgo Tsai017217e2018-03-29 01:24:12 +0800126 { "odm", "odm.img", "odm.sig", "odm", true, false },
Bowgo Tsai0afc6b02018-03-26 15:45:01 +0800127 { "product", "product.img", "product.sig", "product", true, false },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100128 { "product-services",
129 "product-services.img",
130 "product-services.sig",
David Anderson8cdea7f2018-08-06 15:36:00 -0700131 "product_services",
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700132 true, true },
Elliott Hughes45964a82017-05-03 22:43:23 -0700133 { "recovery", "recovery.img", "recovery.sig", "recovery", true, false },
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700134 { "system", "system.img", "system.sig", "system", false, true },
135 { nullptr, "system_other.img", "system.sig", "system", true, false },
Elliott Hughes45964a82017-05-03 22:43:23 -0700136 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, false },
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700137 { "vendor", "vendor.img", "vendor.sig", "vendor", true, true },
138 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, false },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100139 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700140};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700141
Elliott Hughes45964a82017-05-03 22:43:23 -0700142static std::string find_item_given_name(const char* img_name) {
143 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700144 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700145 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700146 }
Alex Lightbb9b8a52016-06-29 09:26:44 -0700147 return android::base::StringPrintf("%s/%s", dir, img_name);
148}
149
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700150static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700151 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700152 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700153 return find_item_given_name(images[i].img_name);
154 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155 }
156
Elliott Hughesd6365a72017-05-08 18:04:49 -0700157 if (item == "userdata") return find_item_given_name("userdata.img");
158 if (item == "cache") return find_item_given_name("cache.img");
Elliott Hughes45964a82017-05-03 22:43:23 -0700159
Elliott Hughesd6365a72017-05-08 18:04:49 -0700160 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700161 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162}
163
Elliott Hughesfc797672015-04-07 20:12:50 -0700164static int64_t get_file_size(int fd) {
165 struct stat sb;
166 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700167}
168
Elliott Hughesfc797672015-04-07 20:12:50 -0700169static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800170 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700171 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172
Elliott Hughesfc797672015-04-07 20:12:50 -0700173 *sz = get_file_size(fd);
174 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700175 goto oops;
176 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177
Elliott Hughesfc797672015-04-07 20:12:50 -0700178 data = (char*) malloc(*sz);
179 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180
Elliott Hughesfc797672015-04-07 20:12:50 -0700181 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182 close(fd);
183
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184 return data;
185
186oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800187 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188 close(fd);
189 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800190 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 return 0;
192}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193
Elliott Hughes2810d002016-04-25 14:31:18 -0700194static void* load_file(const std::string& path, int64_t* sz) {
195 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700196 if (fd == -1) return nullptr;
197 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700198}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199
Elliott Hughesfc797672015-04-07 20:12:50 -0700200static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700201 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
202 return -1;
203 }
204
Scott Anderson13081c62012-04-06 12:39:30 -0700205 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700207 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
208 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 return 0;
210}
211
Elliott Hughesfc797672015-04-07 20:12:50 -0700212static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800213 return match_fastboot_with_serial(info, serial);
214}
215
Elliott Hughesfc797672015-04-07 20:12:50 -0700216static int list_devices_callback(usb_ifc_info* info) {
217 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800218 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700219 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800220 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700221 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 if (!serial[0]) {
223 serial = "????????????";
224 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700225 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700226 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800227 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700228 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800229 printf("%-22s fastboot", serial.c_str());
230 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700231 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800232 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 }
234
235 return -1;
236}
237
David Pursell2ec418a2016-01-20 08:32:08 -0800238// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
239// a specific device, otherwise the first USB device found will be used.
240//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700241// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800242// Otherwise it blocks until the target is available.
243//
244// The returned Transport is a singleton, so multiple calls to this function will return the same
245// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700246static Transport* open_device() {
247 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800248 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249
David Pursell2ec418a2016-01-20 08:32:08 -0800250 if (transport != nullptr) {
251 return transport;
252 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800253
David Pursell4601c972016-02-05 15:35:09 -0800254 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800255 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800256 int port = 0;
257 if (serial != nullptr) {
258 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800259
David Pursell4601c972016-02-05 15:35:09 -0800260 if (android::base::StartsWith(serial, "tcp:")) {
261 protocol = Socket::Protocol::kTcp;
262 port = tcp::kDefaultPort;
263 net_address = serial + strlen("tcp:");
264 } else if (android::base::StartsWith(serial, "udp:")) {
265 protocol = Socket::Protocol::kUdp;
266 port = udp::kDefaultPort;
267 net_address = serial + strlen("udp:");
268 }
269
270 if (net_address != nullptr) {
271 std::string error;
272 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700273 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800274 }
David Pursell2ec418a2016-01-20 08:32:08 -0800275 }
276 }
277
278 while (true) {
279 if (!host.empty()) {
280 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800281 if (protocol == Socket::Protocol::kTcp) {
282 transport = tcp::Connect(host, port, &error).release();
283 } else if (protocol == Socket::Protocol::kUdp) {
284 transport = udp::Connect(host, port, &error).release();
285 }
286
David Pursell2ec418a2016-01-20 08:32:08 -0800287 if (transport == nullptr && announce) {
288 fprintf(stderr, "error: %s\n", error.c_str());
289 }
290 } else {
291 transport = usb_open(match_fastboot);
292 }
293
294 if (transport != nullptr) {
295 return transport;
296 }
297
David Pursell0b156632015-10-30 11:22:01 -0700298 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800299 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700300 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800302 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 }
304}
305
Elliott Hughesfc797672015-04-07 20:12:50 -0700306static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 // We don't actually open a USB device here,
308 // just getting our callback called so we can
309 // list all the connected devices.
310 usb_open(list_devices_callback);
311}
312
Elliott Hughesd6365a72017-05-08 18:04:49 -0700313static void syntax_error(const char* fmt, ...) {
314 fprintf(stderr, "fastboot: usage: ");
315
316 va_list ap;
317 va_start(ap, fmt);
318 vfprintf(stderr, fmt, ap);
319 va_end(ap);
320
321 fprintf(stderr, "\n");
322 exit(1);
323}
324
325static int show_help() {
326 // clang-format off
327 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700328// 1 2 3 4 5 6 7 8
329// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
Elliott Hughesb76188f2018-04-03 13:30:18 -0700330 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700332 "flashing:\n"
333 " update ZIP Flash all partitions from an update.zip package.\n"
334 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
335 " On A/B devices, flashed slot is set as active.\n"
336 " Secondary images may be flashed to inactive slot.\n"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700337 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
338 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700339 "\n"
340 "basics:\n"
341 " devices [-l] List devices in bootloader (-l: with device paths).\n"
342 " getvar NAME Display given bootloader variable.\n"
343 " reboot [bootloader] Reboot device.\n"
344 "\n"
345 "locking/unlocking:\n"
346 " flashing lock|unlock Lock/unlock partitions for flashing\n"
347 " flashing lock_critical|unlock_critical\n"
348 " Lock/unlock 'critical' bootloader partitions.\n"
349 " flashing get_unlock_ability\n"
350 " Check whether unlocking is allowed (1) or not(0).\n"
351 "\n"
352 "advanced:\n"
353 " erase PARTITION Erase a flash partition.\n"
354 " format[:FS_TYPE[:SIZE]] PARTITION\n"
355 " Format a flash partition.\n"
356 " set_active SLOT Set the active slot.\n"
357 " oem [COMMAND...] Execute OEM-specific command.\n"
358 "\n"
359 "boot image:\n"
360 " boot KERNEL [RAMDISK [SECOND]]\n"
361 " Download and boot kernel from RAM.\n"
362 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
363 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700364 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700365 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
366 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
367 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
368 " --tags-offset Set tags offset (default: 0x00000100).\n"
369 " --page-size BYTES Set flash page size (default: 2048).\n"
370 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700371 " --os-version MAJOR[.MINOR[.PATCH]]\n"
372 " Set boot image OS version (default: 0.0.0).\n"
373 " --os-patch-level YYYY-MM-DD\n"
374 " Set boot image OS security patch level.\n"
375 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700376 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700377 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700378 //" continue Continue with autoboot.\n"
379 //"\n"
380 "Android Things:\n"
381 " stage IN_FILE Sends given file to stage for the next command.\n"
382 " get_staged OUT_FILE Writes data staged by the last command to a file.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383 "\n"
384 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700385 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700386 " -s SERIAL Specify a USB device.\n"
387 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700388 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700389 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
390 " non-current slot (default: current active slot).\n"
391 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
392 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
393 " --skip-reboot Don't reboot device after flashing.\n"
394 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
395 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000396#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700397 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000398#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700399 // TODO: remove --unbuffered?
400 " --unbuffered Don't buffer input or output.\n"
401 " --verbose, -v Verbose output.\n"
402 " --version Display version.\n"
403 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700405 // clang-format off
406 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000408
Elliott Hughesd6365a72017-05-08 18:04:49 -0700409static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
Elliott Hughes577e8b42018-04-05 16:12:47 -0700410 const std::string& second_stage, int64_t* sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700411 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700412 void* kdata = load_file(kernel.c_str(), &ksize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700413 if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800414
Elliott Hughesfc797672015-04-07 20:12:50 -0700415 // Is this actually a boot image?
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700416 if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800417 die("cannot load '%s': too short", kernel.c_str());
418 }
Elliott Hughesd6365a72017-05-08 18:04:49 -0700419 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700420 if (!g_cmdline.empty()) {
421 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), g_cmdline);
422 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800423
Elliott Hughes4089d342017-10-27 14:21:12 -0700424 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800425
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 *sz = ksize;
427 return kdata;
428 }
429
Elliott Hughesfc797672015-04-07 20:12:50 -0700430 void* rdata = nullptr;
431 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700432 if (!ramdisk.empty()) {
433 rdata = load_file(ramdisk.c_str(), &rsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700434 if (rdata == nullptr) die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435 }
436
Elliott Hughesfc797672015-04-07 20:12:50 -0700437 void* sdata = nullptr;
438 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700439 if (!second_stage.empty()) {
440 sdata = load_file(second_stage.c_str(), &ssize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700441 if (sdata == nullptr) die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200442 }
443
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444 fprintf(stderr,"creating boot image...\n");
Elliott Hughes577e8b42018-04-05 16:12:47 -0700445 boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, rdata, rsize, sdata, ssize,
446 g_base_addr, g_boot_img_hdr, sz);
Elliott Hughes4089d342017-10-27 14:21:12 -0700447 if (bdata == nullptr) die("failed to create boot.img");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700448
Elliott Hughes577e8b42018-04-05 16:12:47 -0700449 if (!g_cmdline.empty()) bootimg_set_cmdline(bdata, g_cmdline);
450 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", *sz);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800451
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452 return bdata;
453}
454
Elliott Hughes23af1122017-11-10 08:42:17 -0800455static void* unzip_to_memory(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700456 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700457 ZipEntry zip_entry;
458 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700459 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Elliott Hughes4089d342017-10-27 14:21:12 -0700460 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461 }
462
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700463 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464
Elliott Hughes23af1122017-11-10 08:42:17 -0800465 fprintf(stderr, "extracting %s (%" PRId64 " MB) to RAM...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700466 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughes4089d342017-10-27 14:21:12 -0700467 if (data == nullptr) die("failed to allocate %" PRId64 " bytes for '%s'", *sz, entry_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700469 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
Elliott Hughes4089d342017-10-27 14:21:12 -0700470 if (error != 0) die("failed to extract '%s': %s", entry_name, ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000471
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472 return data;
473}
474
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700475#if defined(_WIN32)
476
477// TODO: move this to somewhere it can be shared.
478
479#include <windows.h>
480
481// Windows' tmpfile(3) requires administrator rights because
482// it creates temporary files in the root directory.
483static FILE* win32_tmpfile() {
484 char temp_path[PATH_MAX];
485 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
486 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700487 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700488 }
489
490 char filename[PATH_MAX];
491 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700492 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700493 }
494
495 return fopen(filename, "w+bTD");
496}
497
498#define tmpfile win32_tmpfile
499
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000500static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700501 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000502}
503
Elliott Hughes855cdf82018-04-02 14:24:03 -0700504static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700505 // TODO: reimplement to avoid leaking a FILE*.
506 return fileno(tmpfile());
507}
508
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000509#else
510
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700511static std::string make_temporary_template() {
512 const char* tmpdir = getenv("TMPDIR");
513 if (tmpdir == nullptr) tmpdir = P_tmpdir;
514 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
515}
516
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000517static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700518 std::string result(make_temporary_template());
519 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700520 die("unable to create temporary directory with template %s: %s",
521 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000522 }
523 return result;
524}
525
Elliott Hughes855cdf82018-04-02 14:24:03 -0700526static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700527 std::string path_template(make_temporary_template());
528 int fd = mkstemp(&path_template[0]);
529 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700530 die("failed to create temporary file for %s with template %s: %s\n",
531 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700532 }
533 unlink(path_template.c_str());
534 return fd;
535}
536
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700537#endif
538
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000539static std::string create_fbemarker_tmpdir() {
540 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000541 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
542 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
543 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700544 die("unable to create FBE marker file %s locally: %s",
545 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000546 }
547 close(fd);
548 return dir;
549}
550
551static void delete_fbemarker_tmpdir(const std::string& dir) {
552 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
553 if (unlink(marker_file.c_str()) == -1) {
554 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
555 marker_file.c_str(), errno, strerror(errno));
556 return;
557 }
558 if (rmdir(dir.c_str()) == -1) {
559 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
560 dir.c_str(), errno, strerror(errno));
561 return;
562 }
563}
564
Elliott Hughes45964a82017-05-03 22:43:23 -0700565static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700566 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700567
Yusuke Sato07447542015-06-25 14:39:19 -0700568 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700569 ZipEntry zip_entry;
570 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
571 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000572 return -1;
573 }
574
Elliott Hughes23af1122017-11-10 08:42:17 -0800575 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700576 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800577 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700578 int error = ExtractEntryToFile(zip, &zip_entry, fd);
579 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800580 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700581 }
582
Elliott Hughes4089d342017-10-27 14:21:12 -0700583 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800584 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700585 }
586
Elliott Hughes23af1122017-11-10 08:42:17 -0800587 fprintf(stderr, " took %.3fs\n", now() - start);
588
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700589 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700590}
591
Elliott Hughes5620d222018-03-28 08:20:00 -0700592static char* strip(char* s) {
593 while (*s && isspace(*s)) s++;
594
595 int n = strlen(s);
596 while (n-- > 0) {
597 if (!isspace(s[n])) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800598 s[n] = 0;
599 }
600 return s;
601}
602
603#define MAX_OPTIONS 32
Aaron Wisnerdb511202018-06-26 15:38:35 -0500604static void check_requirement(char* line) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800605 char *val[MAX_OPTIONS];
Elliott Hughes5620d222018-03-28 08:20:00 -0700606 unsigned count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800607 char *x;
608 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800609
Elliott Hughes5620d222018-03-28 08:20:00 -0700610 // "require product=alpha|beta|gamma"
611 // "require version-bootloader=1234"
612 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
613 // "require partition-exists=vendor"
614
615 char* name = line;
616 const char* product = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800617 if (!strncmp(name, "reject ", 7)) {
618 name += 7;
619 invert = 1;
620 } else if (!strncmp(name, "require ", 8)) {
621 name += 8;
622 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700623 } else if (!strncmp(name, "require-for-product:", 20)) {
624 // Get the product and point name past it
Elliott Hughes5620d222018-03-28 08:20:00 -0700625 product = name + 20;
Wink Savilleb98762f2011-04-04 17:54:59 -0700626 name = strchr(name, ' ');
Elliott Hughes5620d222018-03-28 08:20:00 -0700627 if (!name) die("android-info.txt syntax error: %s", line);
Wink Savilleb98762f2011-04-04 17:54:59 -0700628 *name = 0;
629 name += 1;
630 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800631 }
632
633 x = strchr(name, '=');
Elliott Hughes5620d222018-03-28 08:20:00 -0700634 if (x == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800635 *x = 0;
636 val[0] = x + 1;
637
Elliott Hughes5620d222018-03-28 08:20:00 -0700638 name = strip(name);
639
640 // "require partition-exists=x" is a special case, added because of the trouble we had when
641 // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
642 // missing out new partitions. A device with new partitions can use "partition-exists" to
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700643 // override the fields `optional_if_no_image` and 'optional_if_no_partition' in the `images`
644 // array.
Elliott Hughes5620d222018-03-28 08:20:00 -0700645 if (!strcmp(name, "partition-exists")) {
646 const char* partition_name = val[0];
647 std::string has_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500648 if (!fb_getvar(std::string("has-slot:") + partition_name, &has_slot) ||
Elliott Hughes5620d222018-03-28 08:20:00 -0700649 (has_slot != "yes" && has_slot != "no")) {
650 die("device doesn't have required partition %s!", partition_name);
651 }
652 bool known_partition = false;
653 for (size_t i = 0; i < arraysize(images); ++i) {
654 if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700655 images[i].optional_if_no_image = false;
656 images[i].optional_if_no_partition = false;
Elliott Hughes5620d222018-03-28 08:20:00 -0700657 known_partition = true;
658 }
659 }
660 if (!known_partition) {
661 die("device requires partition %s which is not known to this version of fastboot",
662 partition_name);
663 }
664 return;
665 }
666
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800667 for(count = 1; count < MAX_OPTIONS; count++) {
668 x = strchr(val[count - 1],'|');
669 if (x == 0) break;
670 *x = 0;
671 val[count] = x + 1;
672 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800673
Elliott Hughes253c18d2015-03-18 22:47:09 -0700674 // Work around an unfortunate name mismatch.
Elliott Hughes5620d222018-03-28 08:20:00 -0700675 const char* var = name;
676 if (!strcmp(name, "board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800677
Elliott Hughes253c18d2015-03-18 22:47:09 -0700678 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
Elliott Hughes5620d222018-03-28 08:20:00 -0700679 if (out == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800680
Elliott Hughes5620d222018-03-28 08:20:00 -0700681 for (size_t i = 0; i < count; ++i) {
682 out[i] = xstrdup(strip(val[i]));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800683 }
684
Elliott Hughes5620d222018-03-28 08:20:00 -0700685 fb_queue_require(product, var, invert, count, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686}
687
Aaron Wisnerdb511202018-06-26 15:38:35 -0500688static void check_requirements(char* data, int64_t sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700689 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700691 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692 *s++ = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500693 check_requirement(data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694 data = s;
695 } else {
696 s++;
697 }
698 }
Aaron Wisnerdb511202018-06-26 15:38:35 -0500699 if (fb_execute_queue()) die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800700}
701
Elliott Hughesfc797672015-04-07 20:12:50 -0700702static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703 fb_queue_notice("--------------------------------------------");
Elliott Hughes5620d222018-03-28 08:20:00 -0700704 fb_queue_display("Bootloader Version...", "version-bootloader");
705 fb_queue_display("Baseband Version.....", "version-baseband");
706 fb_queue_display("Serial Number........", "serialno");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707 fb_queue_notice("--------------------------------------------");
708}
709
Tao Bao41cf35f2018-04-24 10:54:21 -0700710static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700711 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700712 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700713
Tao Bao41cf35f2018-04-24 10:54:21 -0700714 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
715 die("invalid max size %" PRId64, max_size);
716 }
717
Elliott Hughesfc797672015-04-07 20:12:50 -0700718 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700719 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700720
Elliott Hughes253c18d2015-03-18 22:47:09 -0700721 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700722 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700723
724 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700725 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700726
727 return out_s;
728}
729
Aaron Wisnerdb511202018-06-26 15:38:35 -0500730static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700731 std::string max_download_size;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500732 if (!fb_getvar("max-download-size", &max_download_size) ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700733 max_download_size.empty()) {
734 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700735 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700736 }
737
Elliott Hughes77c0e662015-11-02 15:51:12 -0800738 // Some bootloaders (angler, for example) send spurious whitespace too.
739 max_download_size = android::base::Trim(max_download_size);
740
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700741 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700742 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800743 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700744 return 0;
745 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700746 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700747 return limit;
748}
749
Aaron Wisnerdb511202018-06-26 15:38:35 -0500750static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700751 int64_t limit = sparse_limit;
752 if (limit == 0) {
753 // Unlimited, so see what the target device's limit is.
754 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700755 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500756 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700757 }
758 if (target_sparse_limit > 0) {
759 limit = target_sparse_limit;
760 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700761 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700762 }
763 }
764
765 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500766 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700767 }
768
769 return 0;
770}
771
Aaron Wisnerdb511202018-06-26 15:38:35 -0500772static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700773 int64_t sz = get_file_size(fd);
774 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700775 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700776 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700777
David Anderson32e376f2018-08-16 13:43:11 -0700778 if (sparse_file* s = sparse_file_import_auto(fd, false, false)) {
779 buf->image_size = sparse_file_len(s, false, false);
780 sparse_file_destroy(s);
781 } else {
782 buf->image_size = sz;
783 }
784
Elliott Hughesfc797672015-04-07 20:12:50 -0700785 lseek64(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500786 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700787 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700788 sparse_file** s = load_sparse_files(fd, limit);
789 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700790 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700791 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700792 buf->type = FB_BUFFER_SPARSE;
793 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700794 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500795 buf->type = FB_BUFFER_FD;
796 buf->data = nullptr;
797 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000798 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700799 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700800
Elliott Hughes53ec4952016-05-11 12:39:27 -0700801 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700802}
803
Aaron Wisnerdb511202018-06-26 15:38:35 -0500804static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500805 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
806
Elliott Hughes53ec4952016-05-11 12:39:27 -0700807 if (fd == -1) {
808 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700809 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500810
811 struct stat s;
812 if (fstat(fd, &s)) {
813 return false;
814 }
815 if (!S_ISREG(s.st_mode)) {
816 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
817 return false;
818 }
819
Aaron Wisnerdb511202018-06-26 15:38:35 -0500820 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700821}
822
David Zeuthenb6ea4352017-08-07 14:29:26 -0400823static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
824 // Buffer needs to be at least the size of the VBMeta struct which
825 // is 256 bytes.
826 if (buf->sz < 256) {
827 return;
828 }
829
Elliott Hughes855cdf82018-04-02 14:24:03 -0700830 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400831
832 std::string data;
833 if (!android::base::ReadFdToString(buf->fd, &data)) {
834 die("Failed reading from vbmeta");
835 }
836
837 // There's a 32-bit big endian |flags| field at offset 120 where
838 // bit 0 corresponds to disable-verity and bit 1 corresponds to
839 // disable-verification.
840 //
841 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
842 // the VBMeta struct.
843 if (g_disable_verity) {
844 data[123] |= 0x01;
845 }
846 if (g_disable_verification) {
847 data[123] |= 0x02;
848 }
849
850 if (!android::base::WriteStringToFd(data, fd)) {
851 die("Failed writing to modified vbmeta");
852 }
853 close(buf->fd);
854 buf->fd = fd;
855 lseek(fd, 0, SEEK_SET);
856}
857
Elliott Hughes5620d222018-03-28 08:20:00 -0700858static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700859{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700860 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700861
David Zeuthenb6ea4352017-08-07 14:29:26 -0400862 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
863 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700864 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400865 rewrite_vbmeta_buffer(buf);
866 }
867
Rom Lemarchand622810c2013-06-28 09:54:59 -0700868 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800869 case FB_BUFFER_SPARSE: {
870 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700871 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700872 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700873 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800874 sparse_files.emplace_back(*s, sz);
875 ++s;
876 }
877
878 for (size_t i = 0; i < sparse_files.size(); ++i) {
879 const auto& pair = sparse_files[i];
Elliott Hughes5620d222018-03-28 08:20:00 -0700880 fb_queue_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700881 }
882 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800883 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500884 case FB_BUFFER_FD:
Elliott Hughes5620d222018-03-28 08:20:00 -0700885 fb_queue_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700886 break;
887 default:
888 die("unknown buffer type: %d", buf->type);
889 }
890}
891
Aaron Wisnerdb511202018-06-26 15:38:35 -0500892static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700893 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500894 if (!fb_getvar("current-slot", &current_slot)) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700895 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700896}
897
Aaron Wisnerdb511202018-06-26 15:38:35 -0500898static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700899 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700900 int count = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500901 if (!fb_getvar("slot-count", &var) || !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700902 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700903 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700904 return count;
905}
906
Aaron Wisnerdb511202018-06-26 15:38:35 -0500907static bool supports_AB() {
908 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700909}
910
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700911// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700912static std::string get_other_slot(const std::string& current_slot, int count) {
913 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700914
Daniel Rosenberg80919472016-06-30 19:25:31 -0700915 char next = (current_slot[0] - 'a' + 1)%count + 'a';
916 return std::string(1, next);
917}
918
Aaron Wisnerdb511202018-06-26 15:38:35 -0500919static std::string get_other_slot(const std::string& current_slot) {
920 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -0700921}
922
Aaron Wisnerdb511202018-06-26 15:38:35 -0500923static std::string get_other_slot(int count) {
924 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700925}
926
Aaron Wisnerdb511202018-06-26 15:38:35 -0500927static std::string get_other_slot() {
928 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -0700929}
930
Aaron Wisnerdb511202018-06-26 15:38:35 -0500931static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700932 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700933 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800934 if (allow_all) {
935 return "all";
936 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500937 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700938 if (count > 0) {
939 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800940 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -0700941 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800942 }
943 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700944 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800945
Aaron Wisnerdb511202018-06-26 15:38:35 -0500946 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -0700947 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800948
Daniel Rosenberg80919472016-06-30 19:25:31 -0700949 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500950 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700951 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700952 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800953 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700954 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800955 }
956
Daniel Rosenberg80919472016-06-30 19:25:31 -0700957 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
958
959 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
960 for (int i=0; i<count; i++) {
961 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700962 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700963
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700964 exit(1);
965}
966
Aaron Wisnerdb511202018-06-26 15:38:35 -0500967static std::string verify_slot(const std::string& slot) {
968 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800969}
970
Aaron Wisnerdb511202018-06-26 15:38:35 -0500971static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700972 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800973 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700974 std::string current_slot;
975
Aaron Wisnerdb511202018-06-26 15:38:35 -0500976 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800977 /* If has-slot is not supported, the answer is no. */
978 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700979 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800980 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700981 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500982 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700983 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700984 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700985 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700986 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700987 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700988 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700989 }
990 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700991 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -0700992 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -0700993 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700994 }
995 func(part);
996 }
997}
998
David Pursell0b156632015-10-30 11:22:01 -0700999/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1000 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1001 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1002 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001003 */
Aaron Wisnerdb511202018-06-26 15:38:35 -05001004static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001005 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001006 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001007
Daniel Rosenberg80919472016-06-30 19:25:31 -07001008 if (slot == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001009 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001010 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001011 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001012 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001013 for (int i=0; i < get_slot_count(); i++) {
1014 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001015 }
1016 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001017 do_for_partition(part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001018 }
1019 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001020 do_for_partition(part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001021 }
1022}
1023
Aaron Wisnerdb511202018-06-26 15:38:35 -05001024static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001025 struct fastboot_buffer buf;
1026
Aaron Wisnerdb511202018-06-26 15:38:35 -05001027 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001028 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001029 }
1030 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001031}
1032
Elliott Hughes45964a82017-05-03 22:43:23 -07001033static void do_update_signature(ZipArchiveHandle zip, const char* filename) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001034 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001035 void* data = unzip_to_memory(zip, filename, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001036 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001037 fb_queue_download("signature", data, sz);
1038 fb_queue_command("signature", "installing signature");
1039}
1040
Daniel Rosenberg13454092016-06-27 19:43:11 -07001041// Sets slot_override as the active slot. If slot_override is blank,
1042// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001043static void set_active(const std::string& slot_override) {
1044 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001045
Daniel Rosenberg80919472016-06-30 19:25:31 -07001046 if (slot_override != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001047 fb_set_active(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001048 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001049 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001050 if (current_slot != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001051 fb_set_active(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001052 }
1053 }
1054}
1055
David Anderson32e376f2018-08-16 13:43:11 -07001056static bool is_userspace_fastboot() {
1057 std::string value;
1058 return fb_getvar("is-userspace", &value) && value == "yes";
1059}
1060
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001061static bool if_partition_exists(const std::string& partition, const std::string& slot) {
1062 std::string has_slot;
1063 std::string partition_name = partition;
1064
1065 if (fb_getvar("has-slot:" + partition, &has_slot) && has_slot == "yes") {
1066 if (slot == "") {
1067 std::string current_slot = get_current_slot();
1068 if (current_slot == "") {
1069 die("Failed to identify current slot");
1070 }
1071 partition_name += "_" + current_slot;
1072 } else {
1073 partition_name += "_" + slot;
1074 }
1075 }
1076 std::string partition_size;
1077 return fb_getvar("partition-size:" + partition_name, &partition_size);
1078}
1079
Aaron Wisnerdb511202018-06-26 15:38:35 -05001080static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001081 queue_info_dump();
1082
Wink Savilleb98762f2011-04-04 17:54:59 -07001083 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1084
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001085 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001086 int error = OpenArchive(filename, &zip);
1087 if (error != 0) {
1088 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001089 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001090
Elliott Hughesfc797672015-04-07 20:12:50 -07001091 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001092 void* data = unzip_to_memory(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001093 if (data == nullptr) {
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001094 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001095 }
1096
Aaron Wisnerdb511202018-06-26 15:38:35 -05001097 check_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001098
Alex Lightbb9b8a52016-06-29 09:26:44 -07001099 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001100 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001101 if (slot_override != "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001102 secondary = get_other_slot(slot_override);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001103 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001104 secondary = get_other_slot();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001105 }
1106 if (secondary == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001107 if (supports_AB()) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001108 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1109 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001110 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001111 }
1112 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001113 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001114 const char* slot = slot_override.c_str();
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001115 if (images[i].IsSecondary()) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001116 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001117 slot = secondary.c_str();
1118 } else {
1119 continue;
1120 }
1121 }
1122
Elliott Hughes253c18d2015-03-18 22:47:09 -07001123 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001124 if (fd == -1) {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001125 if (images[i].optional_if_no_image) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001126 continue; // An optional file is missing, so ignore it.
Elliott Hughesacdbe922015-06-02 21:37:05 -07001127 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001128 die("non-optional file %s missing", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001129 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001130
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001131 if (images[i].optional_if_no_partition &&
1132 !if_partition_exists(images[i].part_name, slot)) {
1133 continue;
1134 }
1135
Elliott Hughes253c18d2015-03-18 22:47:09 -07001136 fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001137 if (!load_buf_fd(fd, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001138 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1139 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001140
Elliott Hughes4089d342017-10-27 14:21:12 -07001141 auto update = [&](const std::string& partition) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001142 do_update_signature(zip, images[i].sig_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001143 flash_buf(partition.c_str(), &buf);
1144 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001145 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001146 * program exits.
1147 */
1148 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001149 do_for_partitions(images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001150 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001151
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001152 if (slot_override == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001153 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001154 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001155 set_active(slot_override);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001156 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001157
1158 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001159}
1160
Alex Lightbb9b8a52016-06-29 09:26:44 -07001161static void do_send_signature(const std::string& fn) {
1162 std::size_t extension_loc = fn.find(".img");
1163 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001164
Alex Lightbb9b8a52016-06-29 09:26:44 -07001165 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001166
1167 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001168 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001169 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001170
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001171 fb_queue_download("signature", data, sz);
1172 fb_queue_command("signature", "installing signature");
1173}
1174
David Anderson32e376f2018-08-16 13:43:11 -07001175static bool is_logical(const std::string& partition) {
1176 std::string value;
1177 return fb_getvar("is-logical:" + partition, &value) && value == "yes";
1178}
1179
1180static void update_super_partition(bool force_wipe) {
1181 if (!if_partition_exists("super", "")) {
1182 return;
1183 }
1184 std::string image = find_item_given_name("super_empty.img");
1185 if (access(image.c_str(), R_OK) < 0) {
1186 return;
1187 }
1188
1189 if (!is_userspace_fastboot()) {
1190 die("Must have userspace fastboot to flash logical partitions");
1191 }
1192
1193 int fd = open(image.c_str(), O_RDONLY);
1194 if (fd < 0) {
1195 die("could not open '%s': %s", image.c_str(), strerror(errno));
1196 }
1197 fb_queue_download_fd("super", fd, get_file_size(fd));
1198
1199 std::string command = "update-super:super";
1200 if (force_wipe) {
1201 command += ":wipe";
1202 }
1203 fb_queue_command(command, "Updating super partition");
1204
1205 // We need these commands to have finished before proceeding, since
1206 // otherwise "getvar is-logical" may not return a correct answer below.
1207 fb_execute_queue();
1208}
1209
1210static void do_flashall(const std::string& slot_override, bool skip_secondary, bool wipe) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001211 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001212 queue_info_dump();
1213
Wink Savilleb98762f2011-04-04 17:54:59 -07001214 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1215
Elliott Hughes45964a82017-05-03 22:43:23 -07001216 fname = find_item_given_name("android-info.txt");
Elliott Hughes2810d002016-04-25 14:31:18 -07001217 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001218
Elliott Hughesfc797672015-04-07 20:12:50 -07001219 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001220 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001221 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001222
Aaron Wisnerdb511202018-06-26 15:38:35 -05001223 check_requirements(reinterpret_cast<char*>(data), sz);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001224
Alex Lightbb9b8a52016-06-29 09:26:44 -07001225 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001226 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001227 if (slot_override != "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001228 secondary = get_other_slot(slot_override);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001229 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001230 secondary = get_other_slot();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001231 }
1232 if (secondary == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001233 if (supports_AB()) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001234 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1235 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001236 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001237 }
1238 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001239
David Anderson32e376f2018-08-16 13:43:11 -07001240 update_super_partition(wipe);
1241
1242 // List of partitions to flash and their slots.
1243 std::vector<std::pair<const Image*, std::string>> entries;
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001244 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001245 const char* slot = NULL;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001246 if (images[i].IsSecondary()) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001247 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001248 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001249 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001250 }
1251 if (!slot) continue;
David Anderson32e376f2018-08-16 13:43:11 -07001252 entries.emplace_back(&images[i], slot);
1253
1254 // Resize any logical partition to 0, so each partition is reset to 0
1255 // extents, and will achieve more optimal allocation.
1256 auto resize_partition = [](const std::string& partition) -> void {
1257 if (is_logical(partition)) {
1258 fb_queue_resize_partition(partition, "0");
1259 }
1260 };
1261 do_for_partitions(images[i].part_name, slot, resize_partition, false);
1262 }
1263
1264 // Flash each partition in the list if it has a corresponding image.
1265 for (const auto& [image, slot] : entries) {
1266 fname = find_item_given_name(image->img_name);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001267 fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001268 if (!load_buf(fname.c_str(), &buf)) {
David Anderson32e376f2018-08-16 13:43:11 -07001269 if (image->optional_if_no_image) continue;
1270 die("could not load '%s': %s", image->img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001271 }
David Anderson32e376f2018-08-16 13:43:11 -07001272 if (image->optional_if_no_partition &&
1273 !if_partition_exists(image->part_name, slot)) {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001274 continue;
1275 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001276 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001277 do_send_signature(fname.c_str());
David Anderson32e376f2018-08-16 13:43:11 -07001278 if (is_logical(partition)) {
1279 fb_queue_resize_partition(partition, std::to_string(buf.image_size));
1280 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001281 flash_buf(partition.c_str(), &buf);
1282 };
David Anderson32e376f2018-08-16 13:43:11 -07001283 do_for_partitions(image->part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001284 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001285
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001286 if (slot_override == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001287 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001288 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001289 set_active(slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001290 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001291}
1292
Elliott Hughesd6365a72017-05-08 18:04:49 -07001293static std::string next_arg(std::vector<std::string>* args) {
1294 if (args->empty()) syntax_error("expected argument");
1295 std::string result = args->front();
1296 args->erase(args->begin());
1297 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001298}
1299
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001300static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001301 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001302
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001303 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001304 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001305 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001306 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001307 fb_queue_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001308}
1309
Connor O'Brience16a8a2017-02-06 14:39:31 -08001310static std::string fb_fix_numeric_var(std::string var) {
1311 // Some bootloaders (angler, for example), send spurious leading whitespace.
1312 var = android::base::Trim(var);
1313 // Some bootloaders (hammerhead, for example) use implicit hex.
1314 // This code used to use strtol with base 16.
1315 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1316 return var;
1317}
1318
Aaron Wisnerdb511202018-06-26 15:38:35 -05001319static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001320 std::string sizeString;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001321 if (!fb_getvar(name, &sizeString) || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001322 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001323 return 0;
1324 }
1325 sizeString = fb_fix_numeric_var(sizeString);
1326
1327 unsigned size;
1328 if (!android::base::ParseUint(sizeString, &size)) {
1329 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1330 return 0;
1331 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001332 if ((size & (size - 1)) != 0) {
1333 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001334 return 0;
1335 }
1336 return size;
1337}
1338
Aaron Wisnerdb511202018-06-26 15:38:35 -05001339static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001340 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001341 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001342 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001343 std::string partition_type, partition_size;
1344
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001345 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001346 const char* errMsg = nullptr;
1347 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001348 TemporaryFile output;
1349 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001350
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001351 unsigned int limit = INT_MAX;
1352 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001353 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001354 }
1355 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001356 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001357 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001358
Aaron Wisnerdb511202018-06-26 15:38:35 -05001359 if (!fb_getvar("partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001360 errMsg = "Can't determine partition type.\n";
1361 goto failed;
1362 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001363 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001364 if (partition_type != type_override) {
1365 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001366 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001367 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001368 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001369 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001370
Aaron Wisnerdb511202018-06-26 15:38:35 -05001371 if (!fb_getvar("partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001372 errMsg = "Unable to get partition size\n";
1373 goto failed;
1374 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001375 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001376 if (partition_size != size_override) {
1377 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001378 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001379 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001380 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001381 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001382 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001383
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001384 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001385 if (!gen) {
1386 if (skip_if_not_supported) {
1387 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001388 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001389 return;
1390 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001391 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1392 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001393 return;
1394 }
1395
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001396 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001397 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001398 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1399 return;
1400 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001401
Connor O'Brience16a8a2017-02-06 14:39:31 -08001402 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001403 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1404 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001405
Jin Qian4a335822017-04-18 16:23:18 -07001406 if (fs_generator_generate(gen, output.path, size, initial_dir,
1407 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001408 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001409 return;
1410 }
1411
Jin Qian4a335822017-04-18 16:23:18 -07001412 fd.reset(open(output.path, O_RDONLY));
1413 if (fd == -1) {
1414 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1415 return;
1416 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001417 if (!load_buf_fd(fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001418 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001419 return;
1420 }
1421 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001422 return;
1423
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001424failed:
1425 if (skip_if_not_supported) {
1426 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001427 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001428 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001429 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001430}
1431
Aaron Wisnerdb511202018-06-26 15:38:35 -05001432int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001433 bool wants_wipe = false;
1434 bool wants_reboot = false;
1435 bool wants_reboot_bootloader = false;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001436 bool wants_reboot_recovery = false;
1437 bool wants_reboot_fastboot = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001438 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001439 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001440 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001441 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001442 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001443 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001444 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001445 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001446 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001447
Elliott Hughes577e8b42018-04-05 16:12:47 -07001448 g_boot_img_hdr.kernel_addr = 0x00008000;
1449 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1450 g_boot_img_hdr.second_addr = 0x00f00000;
1451 g_boot_img_hdr.tags_addr = 0x00000100;
1452 g_boot_img_hdr.page_size = 2048;
1453
JP Abgrall7b8970c2013-03-07 17:06:41 -08001454 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001455 {"base", required_argument, 0, 0},
1456 {"cmdline", required_argument, 0, 0},
1457 {"disable-verification", no_argument, 0, 0},
1458 {"disable-verity", no_argument, 0, 0},
1459 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001460 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001461 {"kernel-offset", required_argument, 0, 0},
1462 {"os-patch-level", required_argument, 0, 0},
1463 {"os-version", required_argument, 0, 0},
1464 {"page-size", required_argument, 0, 0},
1465 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001466 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001467 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001468 {"skip-secondary", no_argument, 0, 0},
1469 {"slot", required_argument, 0, 0},
1470 {"tags-offset", required_argument, 0, 0},
1471 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001472 {"verbose", no_argument, 0, 'v'},
1473 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001474#if !defined(_WIN32)
1475 {"wipe-and-use-fbe", no_argument, 0, 0},
1476#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001477 {0, 0, 0, 0}
1478 };
Colin Cross8879f982012-05-22 17:53:34 -07001479
1480 serial = getenv("ANDROID_SERIAL");
1481
Elliott Hughes577e8b42018-04-05 16:12:47 -07001482 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001483 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001484 if (c == 0) {
1485 std::string name{longopts[longindex].name};
1486 if (name == "base") {
1487 g_base_addr = strtoul(optarg, 0, 16);
1488 } else if (name == "cmdline") {
1489 g_cmdline = optarg;
1490 } else if (name == "disable-verification") {
1491 g_disable_verification = true;
1492 } else if (name == "disable-verity") {
1493 g_disable_verity = true;
1494 } else if (name == "header-version") {
1495 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1496 } else if (name == "kernel-offset") {
1497 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1498 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001499 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001500 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001501 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001502 } else if (name == "page-size") {
1503 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1504 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1505 } else if (name == "ramdisk-offset") {
1506 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1507 } else if (name == "skip-reboot") {
1508 skip_reboot = true;
1509 } else if (name == "skip-secondary") {
1510 skip_secondary = true;
1511 } else if (name == "slot") {
1512 slot_override = optarg;
1513 } else if (name == "tags-offset") {
1514 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1515 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001516 setvbuf(stdout, nullptr, _IONBF, 0);
1517 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001518 } else if (name == "version") {
Elliott Hughes4d4f64f2017-05-08 11:30:34 -07001519 fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001520 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001521 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001522#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001523 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001524 wants_wipe = true;
1525 set_fbe_marker = true;
1526#endif
1527 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001528 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001529 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001530 } else {
1531 switch (c) {
1532 case 'a':
1533 wants_set_active = true;
1534 if (optarg) next_active = optarg;
1535 break;
1536 case 'h':
1537 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001538 case 'l':
1539 g_long_listing = true;
1540 break;
1541 case 's':
1542 serial = optarg;
1543 break;
1544 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001545 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1546 die("invalid sparse limit %s", optarg);
1547 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001548 break;
1549 case 'v':
1550 set_verbose();
1551 break;
1552 case 'w':
1553 wants_wipe = true;
1554 break;
1555 case '?':
1556 return 1;
1557 default:
1558 abort();
1559 }
Colin Cross8879f982012-05-22 17:53:34 -07001560 }
1561 }
1562
1563 argc -= optind;
1564 argv += optind;
1565
Elliott Hughesd6365a72017-05-08 18:04:49 -07001566 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001567
Colin Cross8fb6e062012-07-24 16:36:41 -07001568 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001569 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001570 return 0;
1571 }
1572
Colin Crossc7b75dc2012-08-29 18:17:06 -07001573 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001574 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001575 }
1576
David Pursell0b156632015-10-30 11:22:01 -07001577 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001578 if (transport == nullptr) {
1579 return 1;
1580 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001581 fastboot::FastBootDriver fb(transport);
1582 fb_init(fb);
David Pursell2ec418a2016-01-20 08:32:08 -08001583
Elliott Hughes5620d222018-03-28 08:20:00 -07001584 const double start = now();
1585
Aaron Wisnerdb511202018-06-26 15:38:35 -05001586 if (slot_override != "") slot_override = verify_slot(slot_override);
1587 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001588
1589 if (wants_set_active) {
1590 if (next_active == "") {
1591 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001592 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001593 if (fb_getvar("current-slot", &current_slot)) {
1594 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001595 } else {
1596 wants_set_active = false;
1597 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001598 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001599 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001600 }
1601 }
1602 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001603
Elliott Hughesd6365a72017-05-08 18:04:49 -07001604 std::vector<std::string> args(argv, argv + argc);
1605 while (!args.empty()) {
1606 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001607
Elliott Hughesd6365a72017-05-08 18:04:49 -07001608 if (command == "getvar") {
1609 std::string variable = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001610 fb_queue_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001611 } else if (command == "erase") {
1612 std::string partition = next_arg(&args);
1613 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001614 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001615 if (fb_getvar(std::string("partition-type:") + partition,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001616 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001617 fs_get_generator(partition_type) != nullptr) {
1618 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1619 partition_type.c_str());
1620 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001621
Elliott Hughes5620d222018-03-28 08:20:00 -07001622 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001623 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001624 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001625 } else if (android::base::StartsWith(command, "format")) {
1626 // Parsing for: "format[:[type][:[size]]]"
1627 // Some valid things:
1628 // - select only the size, and leave default fs type:
1629 // format::0x4000000 userdata
1630 // - default fs type and size:
1631 // format userdata
1632 // format:: userdata
1633 std::vector<std::string> pieces = android::base::Split(command, ":");
1634 std::string type_override;
1635 if (pieces.size() > 1) type_override = pieces[1].c_str();
1636 std::string size_override;
1637 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001638
Elliott Hughesd6365a72017-05-08 18:04:49 -07001639 std::string partition = next_arg(&args);
1640
1641 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001642 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001643 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001644 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001645 } else if (command == "signature") {
1646 std::string filename = next_arg(&args);
1647 data = load_file(filename.c_str(), &sz);
1648 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -07001649 if (sz != 256) die("signature must be 256 bytes (got %" PRId64 ")", sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001650 fb_queue_download("signature", data, sz);
1651 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001652 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001653 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001654
1655 if (args.size() == 1) {
1656 std::string what = next_arg(&args);
1657 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001658 wants_reboot = false;
1659 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001660 } else if (what == "recovery") {
1661 wants_reboot = false;
1662 wants_reboot_recovery = true;
1663 } else if (what == "fastboot") {
1664 wants_reboot = false;
1665 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001666 } else {
1667 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001668 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001669
Elliott Hughesca85df02015-02-25 10:02:00 -08001670 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001671 if (!args.empty()) syntax_error("junk after reboot command");
1672 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001673 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001674 } else if (command == "reboot-recovery") {
1675 wants_reboot_recovery = true;
1676 } else if (command == "reboot-fastboot") {
1677 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001678 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001679 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001680 } else if (command == "boot") {
1681 std::string kernel = next_arg(&args);
1682 std::string ramdisk;
1683 if (!args.empty()) ramdisk = next_arg(&args);
1684 std::string second_stage;
1685 if (!args.empty()) second_stage = next_arg(&args);
1686
Elliott Hughes577e8b42018-04-05 16:12:47 -07001687 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001688 fb_queue_download("boot.img", data, sz);
1689 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001690 } else if (command == "flash") {
1691 std::string pname = next_arg(&args);
1692
Elliott Hughes2810d002016-04-25 14:31:18 -07001693 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001694 if (!args.empty()) {
1695 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001696 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001697 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001698 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001699 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001700
1701 auto flash = [&](const std::string &partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001702 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001703 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001704 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001705 } else if (command == "flash:raw") {
1706 std::string partition = next_arg(&args);
1707 std::string kernel = next_arg(&args);
1708 std::string ramdisk;
1709 if (!args.empty()) ramdisk = next_arg(&args);
1710 std::string second_stage;
1711 if (!args.empty()) second_stage = next_arg(&args);
1712
Elliott Hughes577e8b42018-04-05 16:12:47 -07001713 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001714 auto flashraw = [&](const std::string& partition) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001715 fb_queue_flash(partition, data, sz);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001716 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001717 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001718 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001719 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001720 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
David Anderson32e376f2018-08-16 13:43:11 -07001721 do_flashall(slot_override, true, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001722 } else {
David Anderson32e376f2018-08-16 13:43:11 -07001723 do_flashall(slot_override, skip_secondary, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001724 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001725 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001726 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001727 bool slot_all = (slot_override == "all");
1728 if (slot_all) {
1729 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1730 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001731 std::string filename = "update.zip";
1732 if (!args.empty()) {
1733 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001734 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001735 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001736 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001737 } else if (command == "set_active") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001738 std::string slot = verify_slot(next_arg(&args), false);
Elliott Hughes5620d222018-03-28 08:20:00 -07001739 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001740 } else if (command == "stage") {
1741 std::string filename = next_arg(&args);
1742
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001743 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001744 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001745 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001746 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001747 fb_queue_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001748 } else if (command == "get_staged") {
1749 std::string filename = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001750 fb_queue_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001751 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001752 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001753 } else if (command == "flashing") {
1754 if (args.empty()) {
1755 syntax_error("missing 'flashing' command");
1756 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1757 args[0] == "unlock_critical" ||
1758 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001759 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001760 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001761 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001762 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001763 }
David Anderson0d4277d2018-07-31 13:27:37 -07001764 } else if (command == "create-logical-partition") {
1765 std::string partition = next_arg(&args);
1766 std::string size = next_arg(&args);
1767 fb_queue_create_partition(partition, size);
1768 } else if (command == "delete-logical-partition") {
1769 std::string partition = next_arg(&args);
1770 fb_queue_delete_partition(partition);
1771 } else if (command == "resize-logical-partition") {
1772 std::string partition = next_arg(&args);
1773 std::string size = next_arg(&args);
1774 fb_queue_resize_partition(partition, size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001775 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001776 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001777 }
1778 }
1779
1780 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001781 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1782 for (const auto& partition : partitions) {
1783 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001784 if (!fb_getvar(std::string{"partition-type:"} + partition, &partition_type)) continue;
Paul Crowley4d170062018-04-24 17:06:30 -07001785 if (partition_type.empty()) continue;
1786 fb_queue_erase(partition);
1787 if (partition == "userdata" && set_fbe_marker) {
1788 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1789 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001790 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001791 delete_fbemarker_tmpdir(initial_userdata_dir);
1792 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001793 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001794 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001795 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001796 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001797 if (wants_set_active) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001798 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001799 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001800 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001801 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001802 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001803 } else if (wants_reboot_bootloader) {
1804 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001805 fb_queue_wait_for_disconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001806 } else if (wants_reboot_recovery) {
1807 fb_queue_command("reboot-recovery", "rebooting into recovery");
1808 fb_queue_wait_for_disconnect();
1809 } else if (wants_reboot_fastboot) {
1810 fb_queue_command("reboot-fastboot", "rebooting into fastboot");
1811 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001812 }
1813
Aaron Wisnerdb511202018-06-26 15:38:35 -05001814 int status = fb_execute_queue() ? EXIT_FAILURE : EXIT_SUCCESS;
Elliott Hughes5620d222018-03-28 08:20:00 -07001815 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
1816 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001817}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001818
Aaron Wisnerdb511202018-06-26 15:38:35 -05001819void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001820 unsigned year, month, day;
1821 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1822 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1823 }
1824 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1825 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1826 hdr->SetOsPatchLevel(year, month);
1827}
1828
Aaron Wisnerdb511202018-06-26 15:38:35 -05001829void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001830 unsigned major = 0, minor = 0, patch = 0;
1831 std::vector<std::string> versions = android::base::Split(arg, ".");
1832 if (versions.size() < 1 || versions.size() > 3 ||
1833 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1834 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1835 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1836 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1837 syntax_error("bad OS version: %s", arg);
1838 }
1839 hdr->SetOsVersion(major, minor, patch);
1840}