blob: 321e6bad783e164d085045ad9aa53969cf250466 [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;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700107};
108
109static struct {
Elliott Hughes45964a82017-05-03 22:43:23 -0700110 const char* nickname;
111 const char* img_name;
112 const char* sig_name;
113 const char* part_name;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700114 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700115 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700116} images[] = {
Dario Frenic7ea1af2018-05-25 16:07:19 +0100117 // clang-format off
Elliott Hughes45964a82017-05-03 22:43:23 -0700118 { "boot", "boot.img", "boot.sig", "boot", false, false },
119 { nullptr, "boot_other.img", "boot.sig", "boot", true, true },
120 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, false },
Dmitry Shmidtde8c08c2017-05-15 10:24:04 -0700121 { "dts", "dt.img", "dt.sig", "dts", true, false },
Bowgo Tsai017217e2018-03-29 01:24:12 +0800122 { "odm", "odm.img", "odm.sig", "odm", true, false },
Bowgo Tsai0afc6b02018-03-26 15:45:01 +0800123 { "product", "product.img", "product.sig", "product", true, false },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100124 { "product-services",
125 "product-services.img",
126 "product-services.sig",
127 "product-services",
128 true, false },
Elliott Hughes45964a82017-05-03 22:43:23 -0700129 { "recovery", "recovery.img", "recovery.sig", "recovery", true, false },
Hridya Valsaraju94943ee2018-08-01 10:09:49 -0700130 { "super", "super.img", "super.sig", "super", true, false },
Elliott Hughes45964a82017-05-03 22:43:23 -0700131 { "system", "system.img", "system.sig", "system", false, false },
132 { nullptr, "system_other.img", "system.sig", "system", true, true },
133 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, false },
134 { "vendor", "vendor.img", "vendor.sig", "vendor", true, false },
135 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, true },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100136 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700137};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700138
Elliott Hughes45964a82017-05-03 22:43:23 -0700139static std::string find_item_given_name(const char* img_name) {
140 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700141 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700142 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700143 }
Alex Lightbb9b8a52016-06-29 09:26:44 -0700144 return android::base::StringPrintf("%s/%s", dir, img_name);
145}
146
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700147static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700148 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700149 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700150 return find_item_given_name(images[i].img_name);
151 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152 }
153
Elliott Hughesd6365a72017-05-08 18:04:49 -0700154 if (item == "userdata") return find_item_given_name("userdata.img");
155 if (item == "cache") return find_item_given_name("cache.img");
Elliott Hughes45964a82017-05-03 22:43:23 -0700156
Elliott Hughesd6365a72017-05-08 18:04:49 -0700157 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700158 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159}
160
Elliott Hughesfc797672015-04-07 20:12:50 -0700161static int64_t get_file_size(int fd) {
162 struct stat sb;
163 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700164}
165
Elliott Hughesfc797672015-04-07 20:12:50 -0700166static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800167 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700168 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169
Elliott Hughesfc797672015-04-07 20:12:50 -0700170 *sz = get_file_size(fd);
171 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700172 goto oops;
173 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174
Elliott Hughesfc797672015-04-07 20:12:50 -0700175 data = (char*) malloc(*sz);
176 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177
Elliott Hughesfc797672015-04-07 20:12:50 -0700178 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179 close(fd);
180
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181 return data;
182
183oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800184 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185 close(fd);
186 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800187 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188 return 0;
189}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190
Elliott Hughes2810d002016-04-25 14:31:18 -0700191static void* load_file(const std::string& path, int64_t* sz) {
192 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700193 if (fd == -1) return nullptr;
194 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700195}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196
Elliott Hughesfc797672015-04-07 20:12:50 -0700197static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700198 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
199 return -1;
200 }
201
Scott Anderson13081c62012-04-06 12:39:30 -0700202 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700204 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
205 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 return 0;
207}
208
Elliott Hughesfc797672015-04-07 20:12:50 -0700209static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800210 return match_fastboot_with_serial(info, serial);
211}
212
Elliott Hughesfc797672015-04-07 20:12:50 -0700213static int list_devices_callback(usb_ifc_info* info) {
214 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800215 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700216 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800217 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700218 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 if (!serial[0]) {
220 serial = "????????????";
221 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700222 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700223 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800224 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700225 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800226 printf("%-22s fastboot", serial.c_str());
227 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700228 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800229 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230 }
231
232 return -1;
233}
234
David Pursell2ec418a2016-01-20 08:32:08 -0800235// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
236// a specific device, otherwise the first USB device found will be used.
237//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700238// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800239// Otherwise it blocks until the target is available.
240//
241// The returned Transport is a singleton, so multiple calls to this function will return the same
242// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700243static Transport* open_device() {
244 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800245 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246
David Pursell2ec418a2016-01-20 08:32:08 -0800247 if (transport != nullptr) {
248 return transport;
249 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800250
David Pursell4601c972016-02-05 15:35:09 -0800251 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800252 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800253 int port = 0;
254 if (serial != nullptr) {
255 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800256
David Pursell4601c972016-02-05 15:35:09 -0800257 if (android::base::StartsWith(serial, "tcp:")) {
258 protocol = Socket::Protocol::kTcp;
259 port = tcp::kDefaultPort;
260 net_address = serial + strlen("tcp:");
261 } else if (android::base::StartsWith(serial, "udp:")) {
262 protocol = Socket::Protocol::kUdp;
263 port = udp::kDefaultPort;
264 net_address = serial + strlen("udp:");
265 }
266
267 if (net_address != nullptr) {
268 std::string error;
269 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700270 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800271 }
David Pursell2ec418a2016-01-20 08:32:08 -0800272 }
273 }
274
275 while (true) {
276 if (!host.empty()) {
277 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800278 if (protocol == Socket::Protocol::kTcp) {
279 transport = tcp::Connect(host, port, &error).release();
280 } else if (protocol == Socket::Protocol::kUdp) {
281 transport = udp::Connect(host, port, &error).release();
282 }
283
David Pursell2ec418a2016-01-20 08:32:08 -0800284 if (transport == nullptr && announce) {
285 fprintf(stderr, "error: %s\n", error.c_str());
286 }
287 } else {
288 transport = usb_open(match_fastboot);
289 }
290
291 if (transport != nullptr) {
292 return transport;
293 }
294
David Pursell0b156632015-10-30 11:22:01 -0700295 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800296 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700297 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800299 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 }
301}
302
Elliott Hughesfc797672015-04-07 20:12:50 -0700303static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 // We don't actually open a USB device here,
305 // just getting our callback called so we can
306 // list all the connected devices.
307 usb_open(list_devices_callback);
308}
309
Elliott Hughesd6365a72017-05-08 18:04:49 -0700310static void syntax_error(const char* fmt, ...) {
311 fprintf(stderr, "fastboot: usage: ");
312
313 va_list ap;
314 va_start(ap, fmt);
315 vfprintf(stderr, fmt, ap);
316 va_end(ap);
317
318 fprintf(stderr, "\n");
319 exit(1);
320}
321
322static int show_help() {
323 // clang-format off
324 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700325// 1 2 3 4 5 6 7 8
326// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
Elliott Hughesb76188f2018-04-03 13:30:18 -0700327 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700329 "flashing:\n"
330 " update ZIP Flash all partitions from an update.zip package.\n"
331 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
332 " On A/B devices, flashed slot is set as active.\n"
333 " Secondary images may be flashed to inactive slot.\n"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700334 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
335 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700336 "\n"
337 "basics:\n"
338 " devices [-l] List devices in bootloader (-l: with device paths).\n"
339 " getvar NAME Display given bootloader variable.\n"
340 " reboot [bootloader] Reboot device.\n"
341 "\n"
342 "locking/unlocking:\n"
343 " flashing lock|unlock Lock/unlock partitions for flashing\n"
344 " flashing lock_critical|unlock_critical\n"
345 " Lock/unlock 'critical' bootloader partitions.\n"
346 " flashing get_unlock_ability\n"
347 " Check whether unlocking is allowed (1) or not(0).\n"
348 "\n"
349 "advanced:\n"
350 " erase PARTITION Erase a flash partition.\n"
351 " format[:FS_TYPE[:SIZE]] PARTITION\n"
352 " Format a flash partition.\n"
353 " set_active SLOT Set the active slot.\n"
354 " oem [COMMAND...] Execute OEM-specific command.\n"
355 "\n"
356 "boot image:\n"
357 " boot KERNEL [RAMDISK [SECOND]]\n"
358 " Download and boot kernel from RAM.\n"
359 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
360 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700361 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700362 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
363 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
364 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
365 " --tags-offset Set tags offset (default: 0x00000100).\n"
366 " --page-size BYTES Set flash page size (default: 2048).\n"
367 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700368 " --os-version MAJOR[.MINOR[.PATCH]]\n"
369 " Set boot image OS version (default: 0.0.0).\n"
370 " --os-patch-level YYYY-MM-DD\n"
371 " Set boot image OS security patch level.\n"
372 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700373 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700374 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700375 //" continue Continue with autoboot.\n"
376 //"\n"
377 "Android Things:\n"
378 " stage IN_FILE Sends given file to stage for the next command.\n"
379 " 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 -0800380 "\n"
381 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700382 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700383 " -s SERIAL Specify a USB device.\n"
384 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700385 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700386 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
387 " non-current slot (default: current active slot).\n"
388 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
389 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
390 " --skip-reboot Don't reboot device after flashing.\n"
391 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
392 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000393#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700394 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000395#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700396 // TODO: remove --unbuffered?
397 " --unbuffered Don't buffer input or output.\n"
398 " --verbose, -v Verbose output.\n"
399 " --version Display version.\n"
400 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700402 // clang-format off
403 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000405
Elliott Hughesd6365a72017-05-08 18:04:49 -0700406static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
Elliott Hughes577e8b42018-04-05 16:12:47 -0700407 const std::string& second_stage, int64_t* sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700408 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700409 void* kdata = load_file(kernel.c_str(), &ksize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700410 if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800411
Elliott Hughesfc797672015-04-07 20:12:50 -0700412 // Is this actually a boot image?
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700413 if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800414 die("cannot load '%s': too short", kernel.c_str());
415 }
Elliott Hughesd6365a72017-05-08 18:04:49 -0700416 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700417 if (!g_cmdline.empty()) {
418 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), g_cmdline);
419 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800420
Elliott Hughes4089d342017-10-27 14:21:12 -0700421 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800422
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423 *sz = ksize;
424 return kdata;
425 }
426
Elliott Hughesfc797672015-04-07 20:12:50 -0700427 void* rdata = nullptr;
428 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700429 if (!ramdisk.empty()) {
430 rdata = load_file(ramdisk.c_str(), &rsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700431 if (rdata == nullptr) die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432 }
433
Elliott Hughesfc797672015-04-07 20:12:50 -0700434 void* sdata = nullptr;
435 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700436 if (!second_stage.empty()) {
437 sdata = load_file(second_stage.c_str(), &ssize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700438 if (sdata == nullptr) die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200439 }
440
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 fprintf(stderr,"creating boot image...\n");
Elliott Hughes577e8b42018-04-05 16:12:47 -0700442 boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, rdata, rsize, sdata, ssize,
443 g_base_addr, g_boot_img_hdr, sz);
Elliott Hughes4089d342017-10-27 14:21:12 -0700444 if (bdata == nullptr) die("failed to create boot.img");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700445
Elliott Hughes577e8b42018-04-05 16:12:47 -0700446 if (!g_cmdline.empty()) bootimg_set_cmdline(bdata, g_cmdline);
447 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", *sz);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800448
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 return bdata;
450}
451
Elliott Hughes23af1122017-11-10 08:42:17 -0800452static void* unzip_to_memory(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700453 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700454 ZipEntry zip_entry;
455 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700456 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Elliott Hughes4089d342017-10-27 14:21:12 -0700457 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458 }
459
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700460 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461
Elliott Hughes23af1122017-11-10 08:42:17 -0800462 fprintf(stderr, "extracting %s (%" PRId64 " MB) to RAM...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700463 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughes4089d342017-10-27 14:21:12 -0700464 if (data == nullptr) die("failed to allocate %" PRId64 " bytes for '%s'", *sz, entry_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700466 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
Elliott Hughes4089d342017-10-27 14:21:12 -0700467 if (error != 0) die("failed to extract '%s': %s", entry_name, ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000468
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 return data;
470}
471
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700472#if defined(_WIN32)
473
474// TODO: move this to somewhere it can be shared.
475
476#include <windows.h>
477
478// Windows' tmpfile(3) requires administrator rights because
479// it creates temporary files in the root directory.
480static FILE* win32_tmpfile() {
481 char temp_path[PATH_MAX];
482 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
483 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700484 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700485 }
486
487 char filename[PATH_MAX];
488 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700489 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700490 }
491
492 return fopen(filename, "w+bTD");
493}
494
495#define tmpfile win32_tmpfile
496
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000497static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700498 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000499}
500
Elliott Hughes855cdf82018-04-02 14:24:03 -0700501static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700502 // TODO: reimplement to avoid leaking a FILE*.
503 return fileno(tmpfile());
504}
505
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000506#else
507
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700508static std::string make_temporary_template() {
509 const char* tmpdir = getenv("TMPDIR");
510 if (tmpdir == nullptr) tmpdir = P_tmpdir;
511 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
512}
513
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000514static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700515 std::string result(make_temporary_template());
516 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700517 die("unable to create temporary directory with template %s: %s",
518 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000519 }
520 return result;
521}
522
Elliott Hughes855cdf82018-04-02 14:24:03 -0700523static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700524 std::string path_template(make_temporary_template());
525 int fd = mkstemp(&path_template[0]);
526 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700527 die("failed to create temporary file for %s with template %s: %s\n",
528 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700529 }
530 unlink(path_template.c_str());
531 return fd;
532}
533
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700534#endif
535
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000536static std::string create_fbemarker_tmpdir() {
537 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000538 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
539 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
540 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700541 die("unable to create FBE marker file %s locally: %s",
542 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000543 }
544 close(fd);
545 return dir;
546}
547
548static void delete_fbemarker_tmpdir(const std::string& dir) {
549 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
550 if (unlink(marker_file.c_str()) == -1) {
551 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
552 marker_file.c_str(), errno, strerror(errno));
553 return;
554 }
555 if (rmdir(dir.c_str()) == -1) {
556 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
557 dir.c_str(), errno, strerror(errno));
558 return;
559 }
560}
561
Elliott Hughes45964a82017-05-03 22:43:23 -0700562static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700563 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700564
Yusuke Sato07447542015-06-25 14:39:19 -0700565 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700566 ZipEntry zip_entry;
567 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
568 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000569 return -1;
570 }
571
Elliott Hughes23af1122017-11-10 08:42:17 -0800572 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700573 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800574 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700575 int error = ExtractEntryToFile(zip, &zip_entry, fd);
576 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800577 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700578 }
579
Elliott Hughes4089d342017-10-27 14:21:12 -0700580 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800581 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700582 }
583
Elliott Hughes23af1122017-11-10 08:42:17 -0800584 fprintf(stderr, " took %.3fs\n", now() - start);
585
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700586 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700587}
588
Elliott Hughes5620d222018-03-28 08:20:00 -0700589static char* strip(char* s) {
590 while (*s && isspace(*s)) s++;
591
592 int n = strlen(s);
593 while (n-- > 0) {
594 if (!isspace(s[n])) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800595 s[n] = 0;
596 }
597 return s;
598}
599
600#define MAX_OPTIONS 32
Aaron Wisnerdb511202018-06-26 15:38:35 -0500601static void check_requirement(char* line) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800602 char *val[MAX_OPTIONS];
Elliott Hughes5620d222018-03-28 08:20:00 -0700603 unsigned count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800604 char *x;
605 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800606
Elliott Hughes5620d222018-03-28 08:20:00 -0700607 // "require product=alpha|beta|gamma"
608 // "require version-bootloader=1234"
609 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
610 // "require partition-exists=vendor"
611
612 char* name = line;
613 const char* product = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800614 if (!strncmp(name, "reject ", 7)) {
615 name += 7;
616 invert = 1;
617 } else if (!strncmp(name, "require ", 8)) {
618 name += 8;
619 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700620 } else if (!strncmp(name, "require-for-product:", 20)) {
621 // Get the product and point name past it
Elliott Hughes5620d222018-03-28 08:20:00 -0700622 product = name + 20;
Wink Savilleb98762f2011-04-04 17:54:59 -0700623 name = strchr(name, ' ');
Elliott Hughes5620d222018-03-28 08:20:00 -0700624 if (!name) die("android-info.txt syntax error: %s", line);
Wink Savilleb98762f2011-04-04 17:54:59 -0700625 *name = 0;
626 name += 1;
627 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800628 }
629
630 x = strchr(name, '=');
Elliott Hughes5620d222018-03-28 08:20:00 -0700631 if (x == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800632 *x = 0;
633 val[0] = x + 1;
634
Elliott Hughes5620d222018-03-28 08:20:00 -0700635 name = strip(name);
636
637 // "require partition-exists=x" is a special case, added because of the trouble we had when
638 // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
639 // missing out new partitions. A device with new partitions can use "partition-exists" to
640 // override the `is_optional` field in the `images` array.
641 if (!strcmp(name, "partition-exists")) {
642 const char* partition_name = val[0];
643 std::string has_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500644 if (!fb_getvar(std::string("has-slot:") + partition_name, &has_slot) ||
Elliott Hughes5620d222018-03-28 08:20:00 -0700645 (has_slot != "yes" && has_slot != "no")) {
646 die("device doesn't have required partition %s!", partition_name);
647 }
648 bool known_partition = false;
649 for (size_t i = 0; i < arraysize(images); ++i) {
650 if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
651 images[i].is_optional = false;
652 known_partition = true;
653 }
654 }
655 if (!known_partition) {
656 die("device requires partition %s which is not known to this version of fastboot",
657 partition_name);
658 }
659 return;
660 }
661
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800662 for(count = 1; count < MAX_OPTIONS; count++) {
663 x = strchr(val[count - 1],'|');
664 if (x == 0) break;
665 *x = 0;
666 val[count] = x + 1;
667 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800668
Elliott Hughes253c18d2015-03-18 22:47:09 -0700669 // Work around an unfortunate name mismatch.
Elliott Hughes5620d222018-03-28 08:20:00 -0700670 const char* var = name;
671 if (!strcmp(name, "board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800672
Elliott Hughes253c18d2015-03-18 22:47:09 -0700673 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
Elliott Hughes5620d222018-03-28 08:20:00 -0700674 if (out == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800675
Elliott Hughes5620d222018-03-28 08:20:00 -0700676 for (size_t i = 0; i < count; ++i) {
677 out[i] = xstrdup(strip(val[i]));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800678 }
679
Elliott Hughes5620d222018-03-28 08:20:00 -0700680 fb_queue_require(product, var, invert, count, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800681}
682
Aaron Wisnerdb511202018-06-26 15:38:35 -0500683static void check_requirements(char* data, int64_t sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700684 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800685 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700686 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800687 *s++ = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500688 check_requirement(data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800689 data = s;
690 } else {
691 s++;
692 }
693 }
Aaron Wisnerdb511202018-06-26 15:38:35 -0500694 if (fb_execute_queue()) die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695}
696
Elliott Hughesfc797672015-04-07 20:12:50 -0700697static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800698 fb_queue_notice("--------------------------------------------");
Elliott Hughes5620d222018-03-28 08:20:00 -0700699 fb_queue_display("Bootloader Version...", "version-bootloader");
700 fb_queue_display("Baseband Version.....", "version-baseband");
701 fb_queue_display("Serial Number........", "serialno");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800702 fb_queue_notice("--------------------------------------------");
703}
704
Tao Bao41cf35f2018-04-24 10:54:21 -0700705static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700706 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700707 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700708
Tao Bao41cf35f2018-04-24 10:54:21 -0700709 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
710 die("invalid max size %" PRId64, max_size);
711 }
712
Elliott Hughesfc797672015-04-07 20:12:50 -0700713 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700714 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700715
Elliott Hughes253c18d2015-03-18 22:47:09 -0700716 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700717 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700718
719 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700720 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700721
722 return out_s;
723}
724
Aaron Wisnerdb511202018-06-26 15:38:35 -0500725static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700726 std::string max_download_size;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500727 if (!fb_getvar("max-download-size", &max_download_size) ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700728 max_download_size.empty()) {
729 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700730 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700731 }
732
Elliott Hughes77c0e662015-11-02 15:51:12 -0800733 // Some bootloaders (angler, for example) send spurious whitespace too.
734 max_download_size = android::base::Trim(max_download_size);
735
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700736 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700737 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800738 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700739 return 0;
740 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700741 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700742 return limit;
743}
744
Aaron Wisnerdb511202018-06-26 15:38:35 -0500745static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700746 int64_t limit = sparse_limit;
747 if (limit == 0) {
748 // Unlimited, so see what the target device's limit is.
749 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700750 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500751 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700752 }
753 if (target_sparse_limit > 0) {
754 limit = target_sparse_limit;
755 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700756 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700757 }
758 }
759
760 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500761 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700762 }
763
764 return 0;
765}
766
Aaron Wisnerdb511202018-06-26 15:38:35 -0500767static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700768 int64_t sz = get_file_size(fd);
769 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700770 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700771 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700772
Elliott Hughesfc797672015-04-07 20:12:50 -0700773 lseek64(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500774 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700775 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700776 sparse_file** s = load_sparse_files(fd, limit);
777 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700778 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700779 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700780 buf->type = FB_BUFFER_SPARSE;
781 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700782 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500783 buf->type = FB_BUFFER_FD;
784 buf->data = nullptr;
785 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000786 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700787 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700788
Elliott Hughes53ec4952016-05-11 12:39:27 -0700789 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700790}
791
Aaron Wisnerdb511202018-06-26 15:38:35 -0500792static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500793 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
794
Elliott Hughes53ec4952016-05-11 12:39:27 -0700795 if (fd == -1) {
796 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700797 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500798
799 struct stat s;
800 if (fstat(fd, &s)) {
801 return false;
802 }
803 if (!S_ISREG(s.st_mode)) {
804 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
805 return false;
806 }
807
Aaron Wisnerdb511202018-06-26 15:38:35 -0500808 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700809}
810
David Zeuthenb6ea4352017-08-07 14:29:26 -0400811static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
812 // Buffer needs to be at least the size of the VBMeta struct which
813 // is 256 bytes.
814 if (buf->sz < 256) {
815 return;
816 }
817
Elliott Hughes855cdf82018-04-02 14:24:03 -0700818 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400819
820 std::string data;
821 if (!android::base::ReadFdToString(buf->fd, &data)) {
822 die("Failed reading from vbmeta");
823 }
824
825 // There's a 32-bit big endian |flags| field at offset 120 where
826 // bit 0 corresponds to disable-verity and bit 1 corresponds to
827 // disable-verification.
828 //
829 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
830 // the VBMeta struct.
831 if (g_disable_verity) {
832 data[123] |= 0x01;
833 }
834 if (g_disable_verification) {
835 data[123] |= 0x02;
836 }
837
838 if (!android::base::WriteStringToFd(data, fd)) {
839 die("Failed writing to modified vbmeta");
840 }
841 close(buf->fd);
842 buf->fd = fd;
843 lseek(fd, 0, SEEK_SET);
844}
845
Elliott Hughes5620d222018-03-28 08:20:00 -0700846static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700847{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700848 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700849
David Zeuthenb6ea4352017-08-07 14:29:26 -0400850 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
851 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700852 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400853 rewrite_vbmeta_buffer(buf);
854 }
855
Rom Lemarchand622810c2013-06-28 09:54:59 -0700856 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800857 case FB_BUFFER_SPARSE: {
858 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700859 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700860 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700861 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800862 sparse_files.emplace_back(*s, sz);
863 ++s;
864 }
865
866 for (size_t i = 0; i < sparse_files.size(); ++i) {
867 const auto& pair = sparse_files[i];
Elliott Hughes5620d222018-03-28 08:20:00 -0700868 fb_queue_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700869 }
870 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800871 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500872 case FB_BUFFER_FD:
Elliott Hughes5620d222018-03-28 08:20:00 -0700873 fb_queue_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700874 break;
875 default:
876 die("unknown buffer type: %d", buf->type);
877 }
878}
879
Aaron Wisnerdb511202018-06-26 15:38:35 -0500880static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700881 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500882 if (!fb_getvar("current-slot", &current_slot)) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700883 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700884}
885
Aaron Wisnerdb511202018-06-26 15:38:35 -0500886static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700887 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700888 int count = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500889 if (!fb_getvar("slot-count", &var) || !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700890 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700891 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700892 return count;
893}
894
Aaron Wisnerdb511202018-06-26 15:38:35 -0500895static bool supports_AB() {
896 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700897}
898
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700899// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700900static std::string get_other_slot(const std::string& current_slot, int count) {
901 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700902
Daniel Rosenberg80919472016-06-30 19:25:31 -0700903 char next = (current_slot[0] - 'a' + 1)%count + 'a';
904 return std::string(1, next);
905}
906
Aaron Wisnerdb511202018-06-26 15:38:35 -0500907static std::string get_other_slot(const std::string& current_slot) {
908 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -0700909}
910
Aaron Wisnerdb511202018-06-26 15:38:35 -0500911static std::string get_other_slot(int count) {
912 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700913}
914
Aaron Wisnerdb511202018-06-26 15:38:35 -0500915static std::string get_other_slot() {
916 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -0700917}
918
Aaron Wisnerdb511202018-06-26 15:38:35 -0500919static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700920 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700921 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800922 if (allow_all) {
923 return "all";
924 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500925 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700926 if (count > 0) {
927 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800928 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -0700929 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800930 }
931 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700932 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800933
Aaron Wisnerdb511202018-06-26 15:38:35 -0500934 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -0700935 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800936
Daniel Rosenberg80919472016-06-30 19:25:31 -0700937 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500938 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700939 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700940 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800941 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700942 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800943 }
944
Daniel Rosenberg80919472016-06-30 19:25:31 -0700945 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
946
947 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
948 for (int i=0; i<count; i++) {
949 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700950 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700951
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700952 exit(1);
953}
954
Aaron Wisnerdb511202018-06-26 15:38:35 -0500955static std::string verify_slot(const std::string& slot) {
956 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800957}
958
Aaron Wisnerdb511202018-06-26 15:38:35 -0500959static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700960 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800961 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700962 std::string current_slot;
963
Aaron Wisnerdb511202018-06-26 15:38:35 -0500964 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800965 /* If has-slot is not supported, the answer is no. */
966 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700967 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800968 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700969 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500970 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700971 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700972 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700973 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700974 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700975 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700976 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700977 }
978 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700979 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -0700980 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -0700981 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700982 }
983 func(part);
984 }
985}
986
David Pursell0b156632015-10-30 11:22:01 -0700987/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
988 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
989 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
990 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700991 */
Aaron Wisnerdb511202018-06-26 15:38:35 -0500992static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700993 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800994 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700995
Daniel Rosenberg80919472016-06-30 19:25:31 -0700996 if (slot == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500997 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700998 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700999 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001000 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001001 for (int i=0; i < get_slot_count(); i++) {
1002 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001003 }
1004 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001005 do_for_partition(part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001006 }
1007 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001008 do_for_partition(part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001009 }
1010}
1011
Aaron Wisnerdb511202018-06-26 15:38:35 -05001012static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001013 struct fastboot_buffer buf;
1014
Aaron Wisnerdb511202018-06-26 15:38:35 -05001015 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001016 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001017 }
1018 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001019}
1020
Elliott Hughes45964a82017-05-03 22:43:23 -07001021static void do_update_signature(ZipArchiveHandle zip, const char* filename) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001022 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001023 void* data = unzip_to_memory(zip, filename, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001024 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001025 fb_queue_download("signature", data, sz);
1026 fb_queue_command("signature", "installing signature");
1027}
1028
Daniel Rosenberg13454092016-06-27 19:43:11 -07001029// Sets slot_override as the active slot. If slot_override is blank,
1030// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001031static void set_active(const std::string& slot_override) {
1032 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001033
Daniel Rosenberg80919472016-06-30 19:25:31 -07001034 if (slot_override != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001035 fb_set_active(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001036 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001037 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001038 if (current_slot != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001039 fb_set_active(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001040 }
1041 }
1042}
1043
Aaron Wisnerdb511202018-06-26 15:38:35 -05001044static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001045 queue_info_dump();
1046
Wink Savilleb98762f2011-04-04 17:54:59 -07001047 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1048
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001049 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001050 int error = OpenArchive(filename, &zip);
1051 if (error != 0) {
1052 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001053 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001054
Elliott Hughesfc797672015-04-07 20:12:50 -07001055 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001056 void* data = unzip_to_memory(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001057 if (data == nullptr) {
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001058 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001059 }
1060
Aaron Wisnerdb511202018-06-26 15:38:35 -05001061 check_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001062
Alex Lightbb9b8a52016-06-29 09:26:44 -07001063 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001064 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001065 if (slot_override != "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001066 secondary = get_other_slot(slot_override);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001067 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001068 secondary = get_other_slot();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001069 }
1070 if (secondary == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001071 if (supports_AB()) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001072 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1073 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001074 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001075 }
1076 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001077 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001078 const char* slot = slot_override.c_str();
1079 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001080 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001081 slot = secondary.c_str();
1082 } else {
1083 continue;
1084 }
1085 }
1086
Elliott Hughes253c18d2015-03-18 22:47:09 -07001087 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001088 if (fd == -1) {
1089 if (images[i].is_optional) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001090 continue; // An optional file is missing, so ignore it.
Elliott Hughesacdbe922015-06-02 21:37:05 -07001091 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001092 die("non-optional file %s missing", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001093 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001094
Elliott Hughes253c18d2015-03-18 22:47:09 -07001095 fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001096 if (!load_buf_fd(fd, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001097 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1098 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001099
Elliott Hughes4089d342017-10-27 14:21:12 -07001100 auto update = [&](const std::string& partition) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001101 do_update_signature(zip, images[i].sig_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001102 flash_buf(partition.c_str(), &buf);
1103 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001104 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001105 * program exits.
1106 */
1107 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001108 do_for_partitions(images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001109 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001110
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001111 if (slot_override == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001112 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001113 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001114 set_active(slot_override);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001115 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001116
1117 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001118}
1119
Alex Lightbb9b8a52016-06-29 09:26:44 -07001120static void do_send_signature(const std::string& fn) {
1121 std::size_t extension_loc = fn.find(".img");
1122 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001123
Alex Lightbb9b8a52016-06-29 09:26:44 -07001124 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001125
1126 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001127 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001128 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001129
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001130 fb_queue_download("signature", data, sz);
1131 fb_queue_command("signature", "installing signature");
1132}
1133
Aaron Wisnerdb511202018-06-26 15:38:35 -05001134static void do_flashall(const std::string& slot_override, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001135 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001136 queue_info_dump();
1137
Wink Savilleb98762f2011-04-04 17:54:59 -07001138 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1139
Elliott Hughes45964a82017-05-03 22:43:23 -07001140 fname = find_item_given_name("android-info.txt");
Elliott Hughes2810d002016-04-25 14:31:18 -07001141 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001142
Elliott Hughesfc797672015-04-07 20:12:50 -07001143 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001144 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001145 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001146
Aaron Wisnerdb511202018-06-26 15:38:35 -05001147 check_requirements(reinterpret_cast<char*>(data), sz);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001148
Alex Lightbb9b8a52016-06-29 09:26:44 -07001149 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001150 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001151 if (slot_override != "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001152 secondary = get_other_slot(slot_override);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001153 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001154 secondary = get_other_slot();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001155 }
1156 if (secondary == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001157 if (supports_AB()) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001158 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1159 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001160 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001161 }
1162 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001163
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001164 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001165 const char* slot = NULL;
1166 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001167 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001168 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001169 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001170 }
1171 if (!slot) continue;
Elliott Hughes45964a82017-05-03 22:43:23 -07001172 fname = find_item_given_name(images[i].img_name);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001173 fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001174 if (!load_buf(fname.c_str(), &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001175 if (images[i].is_optional) continue;
Elliott Hughes4089d342017-10-27 14:21:12 -07001176 die("could not load '%s': %s", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001177 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001178
1179 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001180 do_send_signature(fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001181 flash_buf(partition.c_str(), &buf);
1182 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001183 do_for_partitions(images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001184 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001185
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001186 if (slot_override == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001187 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001188 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001189 set_active(slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001190 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001191}
1192
Elliott Hughesd6365a72017-05-08 18:04:49 -07001193static std::string next_arg(std::vector<std::string>* args) {
1194 if (args->empty()) syntax_error("expected argument");
1195 std::string result = args->front();
1196 args->erase(args->begin());
1197 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001198}
1199
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001200static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001201 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001202
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001203 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001204 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001205 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001206 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001207 fb_queue_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001208}
1209
Connor O'Brience16a8a2017-02-06 14:39:31 -08001210static std::string fb_fix_numeric_var(std::string var) {
1211 // Some bootloaders (angler, for example), send spurious leading whitespace.
1212 var = android::base::Trim(var);
1213 // Some bootloaders (hammerhead, for example) use implicit hex.
1214 // This code used to use strtol with base 16.
1215 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1216 return var;
1217}
1218
Aaron Wisnerdb511202018-06-26 15:38:35 -05001219static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001220 std::string sizeString;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001221 if (!fb_getvar(name, &sizeString) || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001222 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001223 return 0;
1224 }
1225 sizeString = fb_fix_numeric_var(sizeString);
1226
1227 unsigned size;
1228 if (!android::base::ParseUint(sizeString, &size)) {
1229 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1230 return 0;
1231 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001232 if ((size & (size - 1)) != 0) {
1233 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001234 return 0;
1235 }
1236 return size;
1237}
1238
Aaron Wisnerdb511202018-06-26 15:38:35 -05001239static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001240 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001241 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001242 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001243 std::string partition_type, partition_size;
1244
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001245 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001246 const char* errMsg = nullptr;
1247 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001248 TemporaryFile output;
1249 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001250
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001251 unsigned int limit = INT_MAX;
1252 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001253 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001254 }
1255 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001256 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001257 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001258
Aaron Wisnerdb511202018-06-26 15:38:35 -05001259 if (!fb_getvar("partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001260 errMsg = "Can't determine partition type.\n";
1261 goto failed;
1262 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001263 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001264 if (partition_type != type_override) {
1265 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001266 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001267 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001268 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001269 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001270
Aaron Wisnerdb511202018-06-26 15:38:35 -05001271 if (!fb_getvar("partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001272 errMsg = "Unable to get partition size\n";
1273 goto failed;
1274 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001275 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001276 if (partition_size != size_override) {
1277 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001278 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001279 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001280 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001281 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001282 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001283
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001284 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001285 if (!gen) {
1286 if (skip_if_not_supported) {
1287 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001288 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001289 return;
1290 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001291 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1292 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001293 return;
1294 }
1295
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001296 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001297 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001298 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1299 return;
1300 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001301
Connor O'Brience16a8a2017-02-06 14:39:31 -08001302 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001303 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1304 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001305
Jin Qian4a335822017-04-18 16:23:18 -07001306 if (fs_generator_generate(gen, output.path, size, initial_dir,
1307 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001308 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001309 return;
1310 }
1311
Jin Qian4a335822017-04-18 16:23:18 -07001312 fd.reset(open(output.path, O_RDONLY));
1313 if (fd == -1) {
1314 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1315 return;
1316 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001317 if (!load_buf_fd(fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001318 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001319 return;
1320 }
1321 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001322 return;
1323
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001324failed:
1325 if (skip_if_not_supported) {
1326 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001327 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001328 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001329 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001330}
1331
Aaron Wisnerdb511202018-06-26 15:38:35 -05001332int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001333 bool wants_wipe = false;
1334 bool wants_reboot = false;
1335 bool wants_reboot_bootloader = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001336 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001337 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001338 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001339 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001340 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001341 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001342 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001343 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001344 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001345
Elliott Hughes577e8b42018-04-05 16:12:47 -07001346 g_boot_img_hdr.kernel_addr = 0x00008000;
1347 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1348 g_boot_img_hdr.second_addr = 0x00f00000;
1349 g_boot_img_hdr.tags_addr = 0x00000100;
1350 g_boot_img_hdr.page_size = 2048;
1351
JP Abgrall7b8970c2013-03-07 17:06:41 -08001352 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001353 {"base", required_argument, 0, 0},
1354 {"cmdline", required_argument, 0, 0},
1355 {"disable-verification", no_argument, 0, 0},
1356 {"disable-verity", no_argument, 0, 0},
1357 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001358 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001359 {"kernel-offset", required_argument, 0, 0},
1360 {"os-patch-level", required_argument, 0, 0},
1361 {"os-version", required_argument, 0, 0},
1362 {"page-size", required_argument, 0, 0},
1363 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001364 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001365 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001366 {"skip-secondary", no_argument, 0, 0},
1367 {"slot", required_argument, 0, 0},
1368 {"tags-offset", required_argument, 0, 0},
1369 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001370 {"verbose", no_argument, 0, 'v'},
1371 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001372#if !defined(_WIN32)
1373 {"wipe-and-use-fbe", no_argument, 0, 0},
1374#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001375 {0, 0, 0, 0}
1376 };
Colin Cross8879f982012-05-22 17:53:34 -07001377
1378 serial = getenv("ANDROID_SERIAL");
1379
Elliott Hughes577e8b42018-04-05 16:12:47 -07001380 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001381 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001382 if (c == 0) {
1383 std::string name{longopts[longindex].name};
1384 if (name == "base") {
1385 g_base_addr = strtoul(optarg, 0, 16);
1386 } else if (name == "cmdline") {
1387 g_cmdline = optarg;
1388 } else if (name == "disable-verification") {
1389 g_disable_verification = true;
1390 } else if (name == "disable-verity") {
1391 g_disable_verity = true;
1392 } else if (name == "header-version") {
1393 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1394 } else if (name == "kernel-offset") {
1395 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1396 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001397 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001398 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001399 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001400 } else if (name == "page-size") {
1401 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1402 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1403 } else if (name == "ramdisk-offset") {
1404 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1405 } else if (name == "skip-reboot") {
1406 skip_reboot = true;
1407 } else if (name == "skip-secondary") {
1408 skip_secondary = true;
1409 } else if (name == "slot") {
1410 slot_override = optarg;
1411 } else if (name == "tags-offset") {
1412 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1413 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001414 setvbuf(stdout, nullptr, _IONBF, 0);
1415 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001416 } else if (name == "version") {
Elliott Hughes4d4f64f2017-05-08 11:30:34 -07001417 fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001418 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001419 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001420#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001421 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001422 wants_wipe = true;
1423 set_fbe_marker = true;
1424#endif
1425 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001426 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001427 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001428 } else {
1429 switch (c) {
1430 case 'a':
1431 wants_set_active = true;
1432 if (optarg) next_active = optarg;
1433 break;
1434 case 'h':
1435 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001436 case 'l':
1437 g_long_listing = true;
1438 break;
1439 case 's':
1440 serial = optarg;
1441 break;
1442 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001443 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1444 die("invalid sparse limit %s", optarg);
1445 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001446 break;
1447 case 'v':
1448 set_verbose();
1449 break;
1450 case 'w':
1451 wants_wipe = true;
1452 break;
1453 case '?':
1454 return 1;
1455 default:
1456 abort();
1457 }
Colin Cross8879f982012-05-22 17:53:34 -07001458 }
1459 }
1460
1461 argc -= optind;
1462 argv += optind;
1463
Elliott Hughesd6365a72017-05-08 18:04:49 -07001464 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001465
Colin Cross8fb6e062012-07-24 16:36:41 -07001466 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001467 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001468 return 0;
1469 }
1470
Colin Crossc7b75dc2012-08-29 18:17:06 -07001471 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001472 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001473 }
1474
David Pursell0b156632015-10-30 11:22:01 -07001475 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001476 if (transport == nullptr) {
1477 return 1;
1478 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001479 fastboot::FastBootDriver fb(transport);
1480 fb_init(fb);
David Pursell2ec418a2016-01-20 08:32:08 -08001481
Elliott Hughes5620d222018-03-28 08:20:00 -07001482 const double start = now();
1483
Aaron Wisnerdb511202018-06-26 15:38:35 -05001484 if (slot_override != "") slot_override = verify_slot(slot_override);
1485 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001486
1487 if (wants_set_active) {
1488 if (next_active == "") {
1489 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001490 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001491 if (fb_getvar("current-slot", &current_slot)) {
1492 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001493 } else {
1494 wants_set_active = false;
1495 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001496 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001497 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001498 }
1499 }
1500 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001501
Elliott Hughesd6365a72017-05-08 18:04:49 -07001502 std::vector<std::string> args(argv, argv + argc);
1503 while (!args.empty()) {
1504 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001505
Elliott Hughesd6365a72017-05-08 18:04:49 -07001506 if (command == "getvar") {
1507 std::string variable = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001508 fb_queue_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001509 } else if (command == "erase") {
1510 std::string partition = next_arg(&args);
1511 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001512 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001513 if (fb_getvar(std::string("partition-type:") + partition,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001514 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001515 fs_get_generator(partition_type) != nullptr) {
1516 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1517 partition_type.c_str());
1518 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001519
Elliott Hughes5620d222018-03-28 08:20:00 -07001520 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001521 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001522 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001523 } else if (android::base::StartsWith(command, "format")) {
1524 // Parsing for: "format[:[type][:[size]]]"
1525 // Some valid things:
1526 // - select only the size, and leave default fs type:
1527 // format::0x4000000 userdata
1528 // - default fs type and size:
1529 // format userdata
1530 // format:: userdata
1531 std::vector<std::string> pieces = android::base::Split(command, ":");
1532 std::string type_override;
1533 if (pieces.size() > 1) type_override = pieces[1].c_str();
1534 std::string size_override;
1535 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001536
Elliott Hughesd6365a72017-05-08 18:04:49 -07001537 std::string partition = next_arg(&args);
1538
1539 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001540 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001541 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001542 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001543 } else if (command == "signature") {
1544 std::string filename = next_arg(&args);
1545 data = load_file(filename.c_str(), &sz);
1546 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -07001547 if (sz != 256) die("signature must be 256 bytes (got %" PRId64 ")", sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001548 fb_queue_download("signature", data, sz);
1549 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001550 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001551 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001552
1553 if (args.size() == 1) {
1554 std::string what = next_arg(&args);
1555 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001556 wants_reboot = false;
1557 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001558 } else {
1559 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001560 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001561
Elliott Hughesca85df02015-02-25 10:02:00 -08001562 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001563 if (!args.empty()) syntax_error("junk after reboot command");
1564 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001565 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001566 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001567 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001568 } else if (command == "boot") {
1569 std::string kernel = next_arg(&args);
1570 std::string ramdisk;
1571 if (!args.empty()) ramdisk = next_arg(&args);
1572 std::string second_stage;
1573 if (!args.empty()) second_stage = next_arg(&args);
1574
Elliott Hughes577e8b42018-04-05 16:12:47 -07001575 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001576 fb_queue_download("boot.img", data, sz);
1577 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001578 } else if (command == "flash") {
1579 std::string pname = next_arg(&args);
1580
Elliott Hughes2810d002016-04-25 14:31:18 -07001581 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001582 if (!args.empty()) {
1583 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001584 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001585 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001586 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001587 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001588
1589 auto flash = [&](const std::string &partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001590 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001591 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001592 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001593 } else if (command == "flash:raw") {
1594 std::string partition = next_arg(&args);
1595 std::string kernel = next_arg(&args);
1596 std::string ramdisk;
1597 if (!args.empty()) ramdisk = next_arg(&args);
1598 std::string second_stage;
1599 if (!args.empty()) second_stage = next_arg(&args);
1600
Elliott Hughes577e8b42018-04-05 16:12:47 -07001601 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001602 auto flashraw = [&](const std::string& partition) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001603 fb_queue_flash(partition, data, sz);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001604 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001605 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001606 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001607 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001608 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
Aaron Wisnerdb511202018-06-26 15:38:35 -05001609 do_flashall(slot_override, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001610 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001611 do_flashall(slot_override, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001612 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001613 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001614 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001615 bool slot_all = (slot_override == "all");
1616 if (slot_all) {
1617 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1618 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001619 std::string filename = "update.zip";
1620 if (!args.empty()) {
1621 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001622 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001623 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001624 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001625 } else if (command == "set_active") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001626 std::string slot = verify_slot(next_arg(&args), false);
Elliott Hughes5620d222018-03-28 08:20:00 -07001627 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001628 } else if (command == "stage") {
1629 std::string filename = next_arg(&args);
1630
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001631 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001632 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001633 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001634 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001635 fb_queue_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001636 } else if (command == "get_staged") {
1637 std::string filename = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001638 fb_queue_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001639 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001640 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001641 } else if (command == "flashing") {
1642 if (args.empty()) {
1643 syntax_error("missing 'flashing' command");
1644 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1645 args[0] == "unlock_critical" ||
1646 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001647 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001648 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001649 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001650 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001651 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001652 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001653 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001654 }
1655 }
1656
1657 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001658 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1659 for (const auto& partition : partitions) {
1660 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001661 if (!fb_getvar(std::string{"partition-type:"} + partition, &partition_type)) continue;
Paul Crowley4d170062018-04-24 17:06:30 -07001662 if (partition_type.empty()) continue;
1663 fb_queue_erase(partition);
1664 if (partition == "userdata" && set_fbe_marker) {
1665 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1666 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001667 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001668 delete_fbemarker_tmpdir(initial_userdata_dir);
1669 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001670 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001671 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001672 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001673 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001674 if (wants_set_active) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001675 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001676 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001677 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001678 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001679 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001680 } else if (wants_reboot_bootloader) {
1681 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001682 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001683 }
1684
Aaron Wisnerdb511202018-06-26 15:38:35 -05001685 int status = fb_execute_queue() ? EXIT_FAILURE : EXIT_SUCCESS;
Elliott Hughes5620d222018-03-28 08:20:00 -07001686 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
1687 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001688}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001689
Aaron Wisnerdb511202018-06-26 15:38:35 -05001690void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001691 unsigned year, month, day;
1692 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1693 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1694 }
1695 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1696 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1697 hdr->SetOsPatchLevel(year, month);
1698}
1699
Aaron Wisnerdb511202018-06-26 15:38:35 -05001700void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001701 unsigned major = 0, minor = 0, patch = 0;
1702 std::vector<std::string> versions = android::base::Split(arg, ".");
1703 if (versions.size() < 1 || versions.size() > 3 ||
1704 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1705 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1706 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1707 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1708 syntax_error("bad OS version: %s", arg);
1709 }
1710 hdr->SetOsVersion(major, minor, patch);
1711}