blob: 2729f41d00bdb7e1b831853ccb6d3efc770a6c92 [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"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063#include "fastboot.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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080static unsigned short vendor_id = 0;
Elliott Hughes577e8b42018-04-05 16:12:47 -070081static bool g_long_listing = false;
Chris Fries6a999712017-04-04 09:52:47 -050082// Don't resparse files in too-big chunks.
83// libsparse will support INT_MAX, but this results in large allocations, so
84// let's keep it at 1GB to avoid memory pressure on the host.
85static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Colin Crossf8387882012-05-24 17:18:41 -070086static int64_t sparse_limit = -1;
87static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088
Elliott Hughes577e8b42018-04-05 16:12:47 -070089static unsigned g_base_addr = 0x10000000;
90static boot_img_hdr_v1 g_boot_img_hdr = {};
91static std::string g_cmdline;
JP Abgrall7b8970c2013-03-07 17:06:41 -080092
David Zeuthenb6ea4352017-08-07 14:29:26 -040093static bool g_disable_verity = false;
94static bool g_disable_verification = false;
95
Paul Crowley8f7f56e2015-11-27 09:29:37 +000096static const std::string convert_fbe_marker_filename("convert_fbe");
97
Rom Lemarchand622810c2013-06-28 09:54:59 -070098enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -050099 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -0700100 FB_BUFFER_SPARSE,
101};
102
103struct fastboot_buffer {
104 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700105 void* data;
106 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500107 int fd;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700108};
109
110static struct {
Elliott Hughes45964a82017-05-03 22:43:23 -0700111 const char* nickname;
112 const char* img_name;
113 const char* sig_name;
114 const char* part_name;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700115 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700116 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700117} images[] = {
Elliott Hughes45964a82017-05-03 22:43:23 -0700118 // clang-format off
119 { "boot", "boot.img", "boot.sig", "boot", false, false },
120 { nullptr, "boot_other.img", "boot.sig", "boot", true, true },
121 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, false },
Dmitry Shmidtde8c08c2017-05-15 10:24:04 -0700122 { "dts", "dt.img", "dt.sig", "dts", true, false },
Bowgo Tsai017217e2018-03-29 01:24:12 +0800123 { "odm", "odm.img", "odm.sig", "odm", true, false },
Bowgo Tsai0afc6b02018-03-26 15:45:01 +0800124 { "product", "product.img", "product.sig", "product", true, false },
Elliott Hughes45964a82017-05-03 22:43:23 -0700125 { "recovery", "recovery.img", "recovery.sig", "recovery", true, false },
126 { "system", "system.img", "system.sig", "system", false, false },
127 { nullptr, "system_other.img", "system.sig", "system", true, true },
128 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, false },
129 { "vendor", "vendor.img", "vendor.sig", "vendor", true, false },
130 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, true },
131 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700132};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700133
Elliott Hughes45964a82017-05-03 22:43:23 -0700134static std::string find_item_given_name(const char* img_name) {
135 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700136 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700137 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700138 }
Alex Lightbb9b8a52016-06-29 09:26:44 -0700139 return android::base::StringPrintf("%s/%s", dir, img_name);
140}
141
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700142static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700143 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700144 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700145 return find_item_given_name(images[i].img_name);
146 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147 }
148
Elliott Hughesd6365a72017-05-08 18:04:49 -0700149 if (item == "userdata") return find_item_given_name("userdata.img");
150 if (item == "cache") return find_item_given_name("cache.img");
Elliott Hughes45964a82017-05-03 22:43:23 -0700151
Elliott Hughesd6365a72017-05-08 18:04:49 -0700152 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700153 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154}
155
Elliott Hughesfc797672015-04-07 20:12:50 -0700156static int64_t get_file_size(int fd) {
157 struct stat sb;
158 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700159}
160
Elliott Hughesfc797672015-04-07 20:12:50 -0700161static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800162 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700163 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164
Elliott Hughesfc797672015-04-07 20:12:50 -0700165 *sz = get_file_size(fd);
166 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700167 goto oops;
168 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169
Elliott Hughesfc797672015-04-07 20:12:50 -0700170 data = (char*) malloc(*sz);
171 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172
Elliott Hughesfc797672015-04-07 20:12:50 -0700173 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 close(fd);
175
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 return data;
177
178oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800179 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 close(fd);
181 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800182 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183 return 0;
184}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185
Elliott Hughes2810d002016-04-25 14:31:18 -0700186static void* load_file(const std::string& path, int64_t* sz) {
187 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700188 if (fd == -1) return nullptr;
189 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700190}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
Elliott Hughesfc797672015-04-07 20:12:50 -0700192static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700193 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700194 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700195 return -1;
196 }
197
198 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,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
Elliott Hughesb76188f2018-04-03 13:30:18 -0700326 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700328 "flashing:\n"
329 " update ZIP Flash all partitions from an update.zip package.\n"
330 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
331 " On A/B devices, flashed slot is set as active.\n"
332 " Secondary images may be flashed to inactive slot.\n"
333 " flash PARTITION [FILENAME]\n"
334 " Flash given partition only.\n"
335 "\n"
336 "basics:\n"
337 " devices [-l] List devices in bootloader (-l: with device paths).\n"
338 " getvar NAME Display given bootloader variable.\n"
339 " reboot [bootloader] Reboot device.\n"
340 "\n"
341 "locking/unlocking:\n"
342 " flashing lock|unlock Lock/unlock partitions for flashing\n"
343 " flashing lock_critical|unlock_critical\n"
344 " Lock/unlock 'critical' bootloader partitions.\n"
345 " flashing get_unlock_ability\n"
346 " Check whether unlocking is allowed (1) or not(0).\n"
347 "\n"
348 "advanced:\n"
349 " erase PARTITION Erase a flash partition.\n"
350 " format[:FS_TYPE[:SIZE]] PARTITION\n"
351 " Format a flash partition.\n"
352 " set_active SLOT Set the active slot.\n"
353 " oem [COMMAND...] Execute OEM-specific command.\n"
354 "\n"
355 "boot image:\n"
356 " boot KERNEL [RAMDISK [SECOND]]\n"
357 " Download and boot kernel from RAM.\n"
358 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
359 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700360 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700361 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
362 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
363 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
364 " --tags-offset Set tags offset (default: 0x00000100).\n"
365 " --page-size BYTES Set flash page size (default: 2048).\n"
366 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700367 " --os-version MAJOR[.MINOR[.PATCH]]\n"
368 " Set boot image OS version (default: 0.0.0).\n"
369 " --os-patch-level YYYY-MM-DD\n"
370 " Set boot image OS security patch level.\n"
371 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700372 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700373 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700374 //" continue Continue with autoboot.\n"
375 //"\n"
376 "Android Things:\n"
377 " stage IN_FILE Sends given file to stage for the next command.\n"
378 " 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 -0800379 "\n"
380 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700381 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700382 " -s SERIAL Specify a USB device.\n"
383 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
384 // TODO: remove -i?
385 " -i VENDOR_ID Filter devices by USB vendor id.\n"
386 " -S SIZE[K|M|G] Use sparse files above this limit (0 to disable).\n"
387 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
388 " non-current slot (default: current active slot).\n"
389 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
390 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
391 " --skip-reboot Don't reboot device after flashing.\n"
392 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
393 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000394#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700395 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000396#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700397 // TODO: remove --unbuffered?
398 " --unbuffered Don't buffer input or output.\n"
399 " --verbose, -v Verbose output.\n"
400 " --version Display version.\n"
401 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700403 // clang-format off
404 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000406
Elliott Hughesd6365a72017-05-08 18:04:49 -0700407static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
Elliott Hughes577e8b42018-04-05 16:12:47 -0700408 const std::string& second_stage, int64_t* sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700409 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700410 void* kdata = load_file(kernel.c_str(), &ksize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700411 if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800412
Elliott Hughesfc797672015-04-07 20:12:50 -0700413 // Is this actually a boot image?
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700414 if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800415 die("cannot load '%s': too short", kernel.c_str());
416 }
Elliott Hughesd6365a72017-05-08 18:04:49 -0700417 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700418 if (!g_cmdline.empty()) {
419 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), g_cmdline);
420 }
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700421 uint32_t header_version_existing =
422 reinterpret_cast<boot_img_hdr_v1*>(kdata)->header_version;
Elliott Hughes577e8b42018-04-05 16:12:47 -0700423 if (g_boot_img_hdr.header_version != header_version_existing) {
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700424 die("header version mismatch, expected: %" PRIu32 " found %" PRIu32 "",
Elliott Hughes577e8b42018-04-05 16:12:47 -0700425 g_boot_img_hdr.header_version, header_version_existing);
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700426 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800427
Elliott Hughes4089d342017-10-27 14:21:12 -0700428 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800429
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430 *sz = ksize;
431 return kdata;
432 }
433
Elliott Hughesfc797672015-04-07 20:12:50 -0700434 void* rdata = nullptr;
435 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700436 if (!ramdisk.empty()) {
437 rdata = load_file(ramdisk.c_str(), &rsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700438 if (rdata == nullptr) die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439 }
440
Elliott Hughesfc797672015-04-07 20:12:50 -0700441 void* sdata = nullptr;
442 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700443 if (!second_stage.empty()) {
444 sdata = load_file(second_stage.c_str(), &ssize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700445 if (sdata == nullptr) die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200446 }
447
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 fprintf(stderr,"creating boot image...\n");
Elliott Hughes577e8b42018-04-05 16:12:47 -0700449 boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, rdata, rsize, sdata, ssize,
450 g_base_addr, g_boot_img_hdr, sz);
Elliott Hughes4089d342017-10-27 14:21:12 -0700451 if (bdata == nullptr) die("failed to create boot.img");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700452
Elliott Hughes577e8b42018-04-05 16:12:47 -0700453 if (!g_cmdline.empty()) bootimg_set_cmdline(bdata, g_cmdline);
454 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", *sz);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800455
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 return bdata;
457}
458
Elliott Hughes23af1122017-11-10 08:42:17 -0800459static void* unzip_to_memory(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700460 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700461 ZipEntry zip_entry;
462 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700463 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Elliott Hughes4089d342017-10-27 14:21:12 -0700464 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465 }
466
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700467 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468
Elliott Hughes23af1122017-11-10 08:42:17 -0800469 fprintf(stderr, "extracting %s (%" PRId64 " MB) to RAM...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700470 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughes4089d342017-10-27 14:21:12 -0700471 if (data == nullptr) die("failed to allocate %" PRId64 " bytes for '%s'", *sz, entry_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700473 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
Elliott Hughes4089d342017-10-27 14:21:12 -0700474 if (error != 0) die("failed to extract '%s': %s", entry_name, ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000475
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476 return data;
477}
478
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700479#if defined(_WIN32)
480
481// TODO: move this to somewhere it can be shared.
482
483#include <windows.h>
484
485// Windows' tmpfile(3) requires administrator rights because
486// it creates temporary files in the root directory.
487static FILE* win32_tmpfile() {
488 char temp_path[PATH_MAX];
489 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
490 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700491 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700492 }
493
494 char filename[PATH_MAX];
495 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700496 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700497 }
498
499 return fopen(filename, "w+bTD");
500}
501
502#define tmpfile win32_tmpfile
503
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000504static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700505 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000506}
507
Elliott Hughes855cdf82018-04-02 14:24:03 -0700508static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700509 // TODO: reimplement to avoid leaking a FILE*.
510 return fileno(tmpfile());
511}
512
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000513#else
514
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700515static std::string make_temporary_template() {
516 const char* tmpdir = getenv("TMPDIR");
517 if (tmpdir == nullptr) tmpdir = P_tmpdir;
518 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
519}
520
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000521static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700522 std::string result(make_temporary_template());
523 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700524 die("unable to create temporary directory: %s", strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000525 }
526 return result;
527}
528
Elliott Hughes855cdf82018-04-02 14:24:03 -0700529static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700530 std::string path_template(make_temporary_template());
531 int fd = mkstemp(&path_template[0]);
532 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700533 die("failed to create temporary file for %s: %s\n", what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700534 }
535 unlink(path_template.c_str());
536 return fd;
537}
538
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700539#endif
540
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000541static std::string create_fbemarker_tmpdir() {
542 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000543 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
544 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
545 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700546 die("unable to create FBE marker file %s locally: %s",
547 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000548 }
549 close(fd);
550 return dir;
551}
552
553static void delete_fbemarker_tmpdir(const std::string& dir) {
554 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
555 if (unlink(marker_file.c_str()) == -1) {
556 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
557 marker_file.c_str(), errno, strerror(errno));
558 return;
559 }
560 if (rmdir(dir.c_str()) == -1) {
561 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
562 dir.c_str(), errno, strerror(errno));
563 return;
564 }
565}
566
Elliott Hughes45964a82017-05-03 22:43:23 -0700567static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700568 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700569
Yusuke Sato07447542015-06-25 14:39:19 -0700570 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700571 ZipEntry zip_entry;
572 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
573 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000574 return -1;
575 }
576
Elliott Hughes23af1122017-11-10 08:42:17 -0800577 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700578 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800579 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700580 int error = ExtractEntryToFile(zip, &zip_entry, fd);
581 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800582 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700583 }
584
Elliott Hughes4089d342017-10-27 14:21:12 -0700585 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800586 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700587 }
588
Elliott Hughes23af1122017-11-10 08:42:17 -0800589 fprintf(stderr, " took %.3fs\n", now() - start);
590
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700591 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700592}
593
Elliott Hughes5620d222018-03-28 08:20:00 -0700594static char* strip(char* s) {
595 while (*s && isspace(*s)) s++;
596
597 int n = strlen(s);
598 while (n-- > 0) {
599 if (!isspace(s[n])) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800600 s[n] = 0;
601 }
602 return s;
603}
604
605#define MAX_OPTIONS 32
Elliott Hughes5620d222018-03-28 08:20:00 -0700606static void check_requirement(Transport* transport, char* line) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800607 char *val[MAX_OPTIONS];
Elliott Hughes5620d222018-03-28 08:20:00 -0700608 unsigned count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800609 char *x;
610 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800611
Elliott Hughes5620d222018-03-28 08:20:00 -0700612 // "require product=alpha|beta|gamma"
613 // "require version-bootloader=1234"
614 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
615 // "require partition-exists=vendor"
616
617 char* name = line;
618 const char* product = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800619 if (!strncmp(name, "reject ", 7)) {
620 name += 7;
621 invert = 1;
622 } else if (!strncmp(name, "require ", 8)) {
623 name += 8;
624 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700625 } else if (!strncmp(name, "require-for-product:", 20)) {
626 // Get the product and point name past it
Elliott Hughes5620d222018-03-28 08:20:00 -0700627 product = name + 20;
Wink Savilleb98762f2011-04-04 17:54:59 -0700628 name = strchr(name, ' ');
Elliott Hughes5620d222018-03-28 08:20:00 -0700629 if (!name) die("android-info.txt syntax error: %s", line);
Wink Savilleb98762f2011-04-04 17:54:59 -0700630 *name = 0;
631 name += 1;
632 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800633 }
634
635 x = strchr(name, '=');
Elliott Hughes5620d222018-03-28 08:20:00 -0700636 if (x == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800637 *x = 0;
638 val[0] = x + 1;
639
Elliott Hughes5620d222018-03-28 08:20:00 -0700640 name = strip(name);
641
642 // "require partition-exists=x" is a special case, added because of the trouble we had when
643 // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
644 // missing out new partitions. A device with new partitions can use "partition-exists" to
645 // override the `is_optional` field in the `images` array.
646 if (!strcmp(name, "partition-exists")) {
647 const char* partition_name = val[0];
648 std::string has_slot;
649 if (!fb_getvar(transport, std::string("has-slot:") + partition_name, &has_slot) ||
650 (has_slot != "yes" && has_slot != "no")) {
651 die("device doesn't have required partition %s!", partition_name);
652 }
653 bool known_partition = false;
654 for (size_t i = 0; i < arraysize(images); ++i) {
655 if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
656 images[i].is_optional = false;
657 known_partition = true;
658 }
659 }
660 if (!known_partition) {
661 die("device requires partition %s which is not known to this version of fastboot",
662 partition_name);
663 }
664 return;
665 }
666
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800667 for(count = 1; count < MAX_OPTIONS; count++) {
668 x = strchr(val[count - 1],'|');
669 if (x == 0) break;
670 *x = 0;
671 val[count] = x + 1;
672 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800673
Elliott Hughes253c18d2015-03-18 22:47:09 -0700674 // Work around an unfortunate name mismatch.
Elliott Hughes5620d222018-03-28 08:20:00 -0700675 const char* var = name;
676 if (!strcmp(name, "board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800677
Elliott Hughes253c18d2015-03-18 22:47:09 -0700678 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
Elliott Hughes5620d222018-03-28 08:20:00 -0700679 if (out == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800680
Elliott Hughes5620d222018-03-28 08:20:00 -0700681 for (size_t i = 0; i < count; ++i) {
682 out[i] = xstrdup(strip(val[i]));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800683 }
684
Elliott Hughes5620d222018-03-28 08:20:00 -0700685 fb_queue_require(product, var, invert, count, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686}
687
Elliott Hughes5620d222018-03-28 08:20:00 -0700688static void check_requirements(Transport* transport, char* data, int64_t sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700689 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700691 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692 *s++ = 0;
Elliott Hughes5620d222018-03-28 08:20:00 -0700693 check_requirement(transport, data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694 data = s;
695 } else {
696 s++;
697 }
698 }
Elliott Hughes5620d222018-03-28 08:20:00 -0700699 if (fb_execute_queue(transport)) die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800700}
701
Elliott Hughesfc797672015-04-07 20:12:50 -0700702static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703 fb_queue_notice("--------------------------------------------");
Elliott Hughes5620d222018-03-28 08:20:00 -0700704 fb_queue_display("Bootloader Version...", "version-bootloader");
705 fb_queue_display("Baseband Version.....", "version-baseband");
706 fb_queue_display("Serial Number........", "serialno");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707 fb_queue_notice("--------------------------------------------");
708}
709
Elliott Hughes4089d342017-10-27 14:21:12 -0700710static struct sparse_file** load_sparse_files(int fd, int max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700711 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700712 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700713
Elliott Hughesfc797672015-04-07 20:12:50 -0700714 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700715 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700716
Elliott Hughes253c18d2015-03-18 22:47:09 -0700717 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700718 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700719
720 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700721 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700722
723 return out_s;
724}
725
David Pursell0b156632015-10-30 11:22:01 -0700726static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700727 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700728 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700729 max_download_size.empty()) {
730 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700731 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700732 }
733
Elliott Hughes77c0e662015-11-02 15:51:12 -0800734 // Some bootloaders (angler, for example) send spurious whitespace too.
735 max_download_size = android::base::Trim(max_download_size);
736
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700737 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700738 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800739 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700740 return 0;
741 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700742 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700743 return limit;
744}
745
David Pursell0b156632015-10-30 11:22:01 -0700746static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700747 int64_t limit;
748
749 if (sparse_limit == 0) {
750 return 0;
751 } else if (sparse_limit > 0) {
752 limit = sparse_limit;
753 } else {
754 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700755 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700756 }
757 if (target_sparse_limit > 0) {
758 limit = target_sparse_limit;
759 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700760 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700761 }
762 }
763
764 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500765 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700766 }
767
768 return 0;
769}
770
Elliott Hughes53ec4952016-05-11 12:39:27 -0700771static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700772 int64_t sz = get_file_size(fd);
773 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700774 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700775 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700776
Elliott Hughesfc797672015-04-07 20:12:50 -0700777 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700778 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700779 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700780 sparse_file** s = load_sparse_files(fd, limit);
781 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700782 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700783 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700784 buf->type = FB_BUFFER_SPARSE;
785 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700786 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500787 buf->type = FB_BUFFER_FD;
788 buf->data = nullptr;
789 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000790 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700791 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700792
Elliott Hughes53ec4952016-05-11 12:39:27 -0700793 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700794}
795
Elliott Hughes53ec4952016-05-11 12:39:27 -0700796static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500797 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
798
Elliott Hughes53ec4952016-05-11 12:39:27 -0700799 if (fd == -1) {
800 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700801 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500802
803 struct stat s;
804 if (fstat(fd, &s)) {
805 return false;
806 }
807 if (!S_ISREG(s.st_mode)) {
808 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
809 return false;
810 }
811
812 return load_buf_fd(transport, fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700813}
814
David Zeuthenb6ea4352017-08-07 14:29:26 -0400815static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
816 // Buffer needs to be at least the size of the VBMeta struct which
817 // is 256 bytes.
818 if (buf->sz < 256) {
819 return;
820 }
821
Elliott Hughes855cdf82018-04-02 14:24:03 -0700822 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400823
824 std::string data;
825 if (!android::base::ReadFdToString(buf->fd, &data)) {
826 die("Failed reading from vbmeta");
827 }
828
829 // There's a 32-bit big endian |flags| field at offset 120 where
830 // bit 0 corresponds to disable-verity and bit 1 corresponds to
831 // disable-verification.
832 //
833 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
834 // the VBMeta struct.
835 if (g_disable_verity) {
836 data[123] |= 0x01;
837 }
838 if (g_disable_verification) {
839 data[123] |= 0x02;
840 }
841
842 if (!android::base::WriteStringToFd(data, fd)) {
843 die("Failed writing to modified vbmeta");
844 }
845 close(buf->fd);
846 buf->fd = fd;
847 lseek(fd, 0, SEEK_SET);
848}
849
Elliott Hughes5620d222018-03-28 08:20:00 -0700850static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700851{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700852 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700853
David Zeuthenb6ea4352017-08-07 14:29:26 -0400854 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
855 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700856 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400857 rewrite_vbmeta_buffer(buf);
858 }
859
Rom Lemarchand622810c2013-06-28 09:54:59 -0700860 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800861 case FB_BUFFER_SPARSE: {
862 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700863 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700864 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700865 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800866 sparse_files.emplace_back(*s, sz);
867 ++s;
868 }
869
870 for (size_t i = 0; i < sparse_files.size(); ++i) {
871 const auto& pair = sparse_files[i];
Elliott Hughes5620d222018-03-28 08:20:00 -0700872 fb_queue_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700873 }
874 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800875 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500876 case FB_BUFFER_FD:
Elliott Hughes5620d222018-03-28 08:20:00 -0700877 fb_queue_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700878 break;
879 default:
880 die("unknown buffer type: %d", buf->type);
881 }
882}
883
Elliott Hughes42b18a52018-04-10 15:32:21 -0700884static std::string get_current_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700885 std::string current_slot;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700886 if (!fb_getvar(transport, "current-slot", &current_slot)) return "";
887 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700888}
889
890static int get_slot_count(Transport* transport) {
891 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700892 int count = 0;
893 if (!fb_getvar(transport, "slot-count", &var) || !android::base::ParseInt(var, &count)) {
894 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700895 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700896 return count;
897}
898
Alex Lightbb9b8a52016-06-29 09:26:44 -0700899static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700900 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700901}
902
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700903// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700904static std::string get_other_slot(const std::string& current_slot, int count) {
905 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700906
Daniel Rosenberg80919472016-06-30 19:25:31 -0700907 char next = (current_slot[0] - 'a' + 1)%count + 'a';
908 return std::string(1, next);
909}
910
911static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
912 return get_other_slot(current_slot, get_slot_count(transport));
913}
914
915static std::string get_other_slot(Transport* transport, int count) {
916 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700917}
918
Alex Lightbb9b8a52016-06-29 09:26:44 -0700919static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700920 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -0700921}
922
Daniel Rosenberg80919472016-06-30 19:25:31 -0700923static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
924 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700925 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800926 if (allow_all) {
927 return "all";
928 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700929 int count = get_slot_count(transport);
930 if (count > 0) {
931 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800932 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -0700933 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800934 }
935 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700936 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800937
Daniel Rosenberg80919472016-06-30 19:25:31 -0700938 int count = get_slot_count(transport);
Elliott Hughes4089d342017-10-27 14:21:12 -0700939 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800940
Daniel Rosenberg80919472016-06-30 19:25:31 -0700941 if (slot == "other") {
942 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700943 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700944 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800945 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700946 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800947 }
948
Daniel Rosenberg80919472016-06-30 19:25:31 -0700949 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
950
951 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
952 for (int i=0; i<count; i++) {
953 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700954 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700955
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700956 exit(1);
957}
958
Daniel Rosenberg80919472016-06-30 19:25:31 -0700959static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800960 return verify_slot(transport, slot, true);
961}
962
Daniel Rosenberg80919472016-06-30 19:25:31 -0700963static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700964 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800965 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700966 std::string current_slot;
967
Daniel Rosenberg80919472016-06-30 19:25:31 -0700968 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800969 /* If has-slot is not supported, the answer is no. */
970 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700971 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800972 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700973 if (slot == "") {
974 current_slot = get_current_slot(transport);
975 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700976 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700977 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700978 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700979 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700980 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700981 }
982 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700983 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -0700984 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -0700985 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700986 }
987 func(part);
988 }
989}
990
David Pursell0b156632015-10-30 11:22:01 -0700991/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
992 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
993 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
994 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700995 */
Daniel Rosenberg80919472016-06-30 19:25:31 -0700996static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700997 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800998 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700999
Daniel Rosenberg80919472016-06-30 19:25:31 -07001000 if (slot == "all") {
1001 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001002 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001003 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001004 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001005 for (int i=0; i < get_slot_count(transport); i++) {
1006 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001007 }
1008 } else {
David Pursell0b156632015-10-30 11:22:01 -07001009 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001010 }
1011 } else {
David Pursell0b156632015-10-30 11:22:01 -07001012 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001013 }
1014}
1015
David Pursell0b156632015-10-30 11:22:01 -07001016static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001017 struct fastboot_buffer buf;
1018
Elliott Hughes53ec4952016-05-11 12:39:27 -07001019 if (!load_buf(transport, fname, &buf)) {
1020 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001021 }
1022 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001023}
1024
Elliott Hughes45964a82017-05-03 22:43:23 -07001025static void do_update_signature(ZipArchiveHandle zip, const char* filename) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001026 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001027 void* data = unzip_to_memory(zip, filename, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001028 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001029 fb_queue_download("signature", data, sz);
1030 fb_queue_command("signature", "installing signature");
1031}
1032
Daniel Rosenberg13454092016-06-27 19:43:11 -07001033// Sets slot_override as the active slot. If slot_override is blank,
1034// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -07001035static void set_active(Transport* transport, const std::string& slot_override) {
Elliott Hughes42b18a52018-04-10 15:32:21 -07001036 if (!supports_AB(transport)) return;
1037
Daniel Rosenberg80919472016-06-30 19:25:31 -07001038 std::string separator = "";
Daniel Rosenberg80919472016-06-30 19:25:31 -07001039 if (slot_override != "") {
Elliott Hughes5620d222018-03-28 08:20:00 -07001040 fb_set_active(separator + slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001041 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001042 std::string current_slot = get_current_slot(transport);
1043 if (current_slot != "") {
Elliott Hughes5620d222018-03-28 08:20:00 -07001044 fb_set_active(separator + current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001045 }
1046 }
1047}
1048
Elliott Hughes59efd442018-04-05 14:24:00 -07001049static void do_update(Transport* transport, const char* filename, const std::string& slot_override, bool skip_secondary) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001050 queue_info_dump();
1051
Wink Savilleb98762f2011-04-04 17:54:59 -07001052 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1053
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001054 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001055 int error = OpenArchive(filename, &zip);
1056 if (error != 0) {
1057 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001058 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001059
Elliott Hughesfc797672015-04-07 20:12:50 -07001060 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001061 void* data = unzip_to_memory(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001062 if (data == nullptr) {
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001063 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001064 }
1065
Elliott Hughes5620d222018-03-28 08:20:00 -07001066 check_requirements(transport, reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001067
Alex Lightbb9b8a52016-06-29 09:26:44 -07001068 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001069 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001070 if (slot_override != "") {
1071 secondary = get_other_slot(transport, slot_override);
1072 } else {
1073 secondary = get_other_slot(transport);
1074 }
1075 if (secondary == "") {
1076 if (supports_AB(transport)) {
1077 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1078 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001079 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001080 }
1081 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001082 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001083 const char* slot = slot_override.c_str();
1084 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001085 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001086 slot = secondary.c_str();
1087 } else {
1088 continue;
1089 }
1090 }
1091
Elliott Hughes253c18d2015-03-18 22:47:09 -07001092 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001093 if (fd == -1) {
1094 if (images[i].is_optional) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001095 continue; // An optional file is missing, so ignore it.
Elliott Hughesacdbe922015-06-02 21:37:05 -07001096 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001097 die("non-optional file %s missing", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001098 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001099
Elliott Hughes253c18d2015-03-18 22:47:09 -07001100 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001101 if (!load_buf_fd(transport, fd, &buf)) {
1102 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1103 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001104
Elliott Hughes4089d342017-10-27 14:21:12 -07001105 auto update = [&](const std::string& partition) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001106 do_update_signature(zip, images[i].sig_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001107 flash_buf(partition.c_str(), &buf);
1108 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001109 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001110 * program exits.
1111 */
1112 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001113 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001114 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001115
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001116 if (slot_override == "all") {
1117 set_active(transport, "a");
1118 } else {
1119 set_active(transport, slot_override);
1120 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001121
1122 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001123}
1124
Alex Lightbb9b8a52016-06-29 09:26:44 -07001125static void do_send_signature(const std::string& fn) {
1126 std::size_t extension_loc = fn.find(".img");
1127 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001128
Alex Lightbb9b8a52016-06-29 09:26:44 -07001129 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001130
1131 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001132 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001133 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001134
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001135 fb_queue_download("signature", data, sz);
1136 fb_queue_command("signature", "installing signature");
1137}
1138
Elliott Hughes59efd442018-04-05 14:24:00 -07001139static void do_flashall(Transport* transport, const std::string& slot_override, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001140 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001141 queue_info_dump();
1142
Wink Savilleb98762f2011-04-04 17:54:59 -07001143 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1144
Elliott Hughes45964a82017-05-03 22:43:23 -07001145 fname = find_item_given_name("android-info.txt");
Elliott Hughes2810d002016-04-25 14:31:18 -07001146 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001147
Elliott Hughesfc797672015-04-07 20:12:50 -07001148 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001149 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001150 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001151
Elliott Hughes5620d222018-03-28 08:20:00 -07001152 check_requirements(transport, reinterpret_cast<char*>(data), sz);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001153
Alex Lightbb9b8a52016-06-29 09:26:44 -07001154 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001155 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001156 if (slot_override != "") {
1157 secondary = get_other_slot(transport, slot_override);
1158 } else {
1159 secondary = get_other_slot(transport);
1160 }
1161 if (secondary == "") {
1162 if (supports_AB(transport)) {
1163 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1164 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001165 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001166 }
1167 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001168
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001169 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001170 const char* slot = NULL;
1171 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001172 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001173 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001174 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001175 }
1176 if (!slot) continue;
Elliott Hughes45964a82017-05-03 22:43:23 -07001177 fname = find_item_given_name(images[i].img_name);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001178 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001179 if (!load_buf(transport, fname.c_str(), &buf)) {
1180 if (images[i].is_optional) continue;
Elliott Hughes4089d342017-10-27 14:21:12 -07001181 die("could not load '%s': %s", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001182 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001183
1184 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001185 do_send_signature(fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001186 flash_buf(partition.c_str(), &buf);
1187 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001188 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001189 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001190
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001191 if (slot_override == "all") {
1192 set_active(transport, "a");
1193 } else {
1194 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001195 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001196}
1197
Elliott Hughesd6365a72017-05-08 18:04:49 -07001198static std::string next_arg(std::vector<std::string>* args) {
1199 if (args->empty()) syntax_error("expected argument");
1200 std::string result = args->front();
1201 args->erase(args->begin());
1202 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001203}
1204
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001205static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001206 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001207
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001208 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001209 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001210 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001211 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001212 fb_queue_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001213}
1214
Colin Crossf8387882012-05-24 17:18:41 -07001215static int64_t parse_num(const char *arg)
1216{
1217 char *endptr;
1218 unsigned long long num;
1219
1220 num = strtoull(arg, &endptr, 0);
1221 if (endptr == arg) {
1222 return -1;
1223 }
1224
1225 if (*endptr == 'k' || *endptr == 'K') {
1226 if (num >= (-1ULL) / 1024) {
1227 return -1;
1228 }
1229 num *= 1024LL;
1230 endptr++;
1231 } else if (*endptr == 'm' || *endptr == 'M') {
1232 if (num >= (-1ULL) / (1024 * 1024)) {
1233 return -1;
1234 }
1235 num *= 1024LL * 1024LL;
1236 endptr++;
1237 } else if (*endptr == 'g' || *endptr == 'G') {
1238 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1239 return -1;
1240 }
1241 num *= 1024LL * 1024LL * 1024LL;
1242 endptr++;
1243 }
1244
1245 if (*endptr != '\0') {
1246 return -1;
1247 }
1248
1249 if (num > INT64_MAX) {
1250 return -1;
1251 }
1252
1253 return num;
1254}
1255
Connor O'Brience16a8a2017-02-06 14:39:31 -08001256static std::string fb_fix_numeric_var(std::string var) {
1257 // Some bootloaders (angler, for example), send spurious leading whitespace.
1258 var = android::base::Trim(var);
1259 // Some bootloaders (hammerhead, for example) use implicit hex.
1260 // This code used to use strtol with base 16.
1261 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1262 return var;
1263}
1264
1265static unsigned fb_get_flash_block_size(Transport* transport, std::string name) {
1266 std::string sizeString;
Elliott Hughes5620d222018-03-28 08:20:00 -07001267 if (!fb_getvar(transport, name, &sizeString) || sizeString.empty()) {
1268 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001269 return 0;
1270 }
1271 sizeString = fb_fix_numeric_var(sizeString);
1272
1273 unsigned size;
1274 if (!android::base::ParseUint(sizeString, &size)) {
1275 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1276 return 0;
1277 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001278 if ((size & (size - 1)) != 0) {
1279 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001280 return 0;
1281 }
1282 return size;
1283}
1284
David Pursell0b156632015-10-30 11:22:01 -07001285static void fb_perform_format(Transport* transport,
Elliott Hughes5620d222018-03-28 08:20:00 -07001286 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001287 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001288 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001289 std::string partition_type, partition_size;
1290
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001291 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001292 const char* errMsg = nullptr;
1293 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001294 TemporaryFile output;
1295 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001296
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001297 unsigned int limit = INT_MAX;
1298 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001299 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001300 }
1301 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001302 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001303 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001304
Elliott Hughes5620d222018-03-28 08:20:00 -07001305 if (!fb_getvar(transport, "partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001306 errMsg = "Can't determine partition type.\n";
1307 goto failed;
1308 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001309 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001310 if (partition_type != type_override) {
1311 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001312 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001313 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001314 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001315 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001316
Elliott Hughes5620d222018-03-28 08:20:00 -07001317 if (!fb_getvar(transport, "partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001318 errMsg = "Unable to get partition size\n";
1319 goto failed;
1320 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001321 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001322 if (partition_size != size_override) {
1323 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001324 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001325 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001326 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001327 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001328 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001329
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001330 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001331 if (!gen) {
1332 if (skip_if_not_supported) {
1333 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001334 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001335 return;
1336 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001337 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1338 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001339 return;
1340 }
1341
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001342 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001343 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001344 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1345 return;
1346 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001347
Connor O'Brience16a8a2017-02-06 14:39:31 -08001348 unsigned eraseBlkSize, logicalBlkSize;
1349 eraseBlkSize = fb_get_flash_block_size(transport, "erase-block-size");
1350 logicalBlkSize = fb_get_flash_block_size(transport, "logical-block-size");
1351
Jin Qian4a335822017-04-18 16:23:18 -07001352 if (fs_generator_generate(gen, output.path, size, initial_dir,
1353 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001354 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001355 return;
1356 }
1357
Jin Qian4a335822017-04-18 16:23:18 -07001358 fd.reset(open(output.path, O_RDONLY));
1359 if (fd == -1) {
1360 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1361 return;
1362 }
1363 if (!load_buf_fd(transport, fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001364 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001365 return;
1366 }
1367 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001368 return;
1369
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001370failed:
1371 if (skip_if_not_supported) {
1372 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001373 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001374 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001375 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001376}
1377
Elliott Hughes11ff3452018-04-09 13:58:41 -07001378int FastBoot::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001379 bool wants_wipe = false;
1380 bool wants_reboot = false;
1381 bool wants_reboot_bootloader = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001382 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001383 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001384 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001385 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001386 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001387 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001388 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001389 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001390 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001391
Elliott Hughes577e8b42018-04-05 16:12:47 -07001392 g_boot_img_hdr.kernel_addr = 0x00008000;
1393 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1394 g_boot_img_hdr.second_addr = 0x00f00000;
1395 g_boot_img_hdr.tags_addr = 0x00000100;
1396 g_boot_img_hdr.page_size = 2048;
1397
JP Abgrall7b8970c2013-03-07 17:06:41 -08001398 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001399 {"base", required_argument, 0, 0},
1400 {"cmdline", required_argument, 0, 0},
1401 {"disable-verification", no_argument, 0, 0},
1402 {"disable-verity", no_argument, 0, 0},
1403 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001404 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001405 {"kernel-offset", required_argument, 0, 0},
1406 {"os-patch-level", required_argument, 0, 0},
1407 {"os-version", required_argument, 0, 0},
1408 {"page-size", required_argument, 0, 0},
1409 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001410 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001411 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001412 {"skip-secondary", no_argument, 0, 0},
1413 {"slot", required_argument, 0, 0},
1414 {"tags-offset", required_argument, 0, 0},
1415 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001416 {"verbose", no_argument, 0, 'v'},
1417 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001418#if !defined(_WIN32)
1419 {"wipe-and-use-fbe", no_argument, 0, 0},
1420#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001421 {0, 0, 0, 0}
1422 };
Colin Cross8879f982012-05-22 17:53:34 -07001423
1424 serial = getenv("ANDROID_SERIAL");
1425
Elliott Hughes577e8b42018-04-05 16:12:47 -07001426 int c;
1427 while ((c = getopt_long(argc, argv, "a::hi:ls:S:vw", longopts, &longindex)) != -1) {
1428 if (c == 0) {
1429 std::string name{longopts[longindex].name};
1430 if (name == "base") {
1431 g_base_addr = strtoul(optarg, 0, 16);
1432 } else if (name == "cmdline") {
1433 g_cmdline = optarg;
1434 } else if (name == "disable-verification") {
1435 g_disable_verification = true;
1436 } else if (name == "disable-verity") {
1437 g_disable_verity = true;
1438 } else if (name == "header-version") {
1439 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1440 } else if (name == "kernel-offset") {
1441 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1442 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001443 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001444 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001445 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001446 } else if (name == "page-size") {
1447 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1448 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1449 } else if (name == "ramdisk-offset") {
1450 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1451 } else if (name == "skip-reboot") {
1452 skip_reboot = true;
1453 } else if (name == "skip-secondary") {
1454 skip_secondary = true;
1455 } else if (name == "slot") {
1456 slot_override = optarg;
1457 } else if (name == "tags-offset") {
1458 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1459 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001460 setvbuf(stdout, nullptr, _IONBF, 0);
1461 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001462 } else if (name == "version") {
Elliott Hughes4d4f64f2017-05-08 11:30:34 -07001463 fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001464 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001465 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001466#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001467 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001468 wants_wipe = true;
1469 set_fbe_marker = true;
1470#endif
1471 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001472 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001473 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001474 } else {
1475 switch (c) {
1476 case 'a':
1477 wants_set_active = true;
1478 if (optarg) next_active = optarg;
1479 break;
1480 case 'h':
1481 return show_help();
1482 case 'i':
1483 {
1484 char *endptr = nullptr;
1485 unsigned long val = strtoul(optarg, &endptr, 0);
1486 if (!endptr || *endptr != '\0' || (val & ~0xffff)) {
1487 die("invalid vendor id '%s'", optarg);
1488 }
1489 vendor_id = (unsigned short)val;
1490 break;
1491 }
1492 case 'l':
1493 g_long_listing = true;
1494 break;
1495 case 's':
1496 serial = optarg;
1497 break;
1498 case 'S':
1499 sparse_limit = parse_num(optarg);
1500 if (sparse_limit < 0) die("invalid sparse limit");
1501 break;
1502 case 'v':
1503 set_verbose();
1504 break;
1505 case 'w':
1506 wants_wipe = true;
1507 break;
1508 case '?':
1509 return 1;
1510 default:
1511 abort();
1512 }
Colin Cross8879f982012-05-22 17:53:34 -07001513 }
1514 }
1515
1516 argc -= optind;
1517 argv += optind;
1518
Elliott Hughesd6365a72017-05-08 18:04:49 -07001519 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001520
Colin Cross8fb6e062012-07-24 16:36:41 -07001521 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001522 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001523 return 0;
1524 }
1525
Colin Crossc7b75dc2012-08-29 18:17:06 -07001526 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001527 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001528 }
1529
David Pursell0b156632015-10-30 11:22:01 -07001530 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001531 if (transport == nullptr) {
1532 return 1;
1533 }
1534
Elliott Hughes5620d222018-03-28 08:20:00 -07001535 const double start = now();
1536
Daniel Rosenberg80919472016-06-30 19:25:31 -07001537 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1538 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001539
1540 if (wants_set_active) {
1541 if (next_active == "") {
1542 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001543 std::string current_slot;
1544 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001545 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001546 } else {
1547 wants_set_active = false;
1548 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001549 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001550 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001551 }
1552 }
1553 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001554
Elliott Hughesd6365a72017-05-08 18:04:49 -07001555 std::vector<std::string> args(argv, argv + argc);
1556 while (!args.empty()) {
1557 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001558
Elliott Hughesd6365a72017-05-08 18:04:49 -07001559 if (command == "getvar") {
1560 std::string variable = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001561 fb_queue_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001562 } else if (command == "erase") {
1563 std::string partition = next_arg(&args);
1564 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001565 std::string partition_type;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001566 if (fb_getvar(transport, std::string("partition-type:") + partition,
1567 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001568 fs_get_generator(partition_type) != nullptr) {
1569 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1570 partition_type.c_str());
1571 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001572
Elliott Hughes5620d222018-03-28 08:20:00 -07001573 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001574 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001575 do_for_partitions(transport, partition, slot_override, erase, true);
1576 } else if (android::base::StartsWith(command, "format")) {
1577 // Parsing for: "format[:[type][:[size]]]"
1578 // Some valid things:
1579 // - select only the size, and leave default fs type:
1580 // format::0x4000000 userdata
1581 // - default fs type and size:
1582 // format userdata
1583 // format:: userdata
1584 std::vector<std::string> pieces = android::base::Split(command, ":");
1585 std::string type_override;
1586 if (pieces.size() > 1) type_override = pieces[1].c_str();
1587 std::string size_override;
1588 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001589
Elliott Hughesd6365a72017-05-08 18:04:49 -07001590 std::string partition = next_arg(&args);
1591
1592 auto format = [&](const std::string& partition) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001593 fb_perform_format(transport, partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001594 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001595 do_for_partitions(transport, partition.c_str(), slot_override, format, true);
1596 } else if (command == "signature") {
1597 std::string filename = next_arg(&args);
1598 data = load_file(filename.c_str(), &sz);
1599 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -07001600 if (sz != 256) die("signature must be 256 bytes (got %" PRId64 ")", sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001601 fb_queue_download("signature", data, sz);
1602 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001603 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001604 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001605
1606 if (args.size() == 1) {
1607 std::string what = next_arg(&args);
1608 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001609 wants_reboot = false;
1610 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001611 } else {
1612 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001613 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001614
Elliott Hughesca85df02015-02-25 10:02:00 -08001615 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001616 if (!args.empty()) syntax_error("junk after reboot command");
1617 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001618 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001619 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001620 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001621 } else if (command == "boot") {
1622 std::string kernel = next_arg(&args);
1623 std::string ramdisk;
1624 if (!args.empty()) ramdisk = next_arg(&args);
1625 std::string second_stage;
1626 if (!args.empty()) second_stage = next_arg(&args);
1627
Elliott Hughes577e8b42018-04-05 16:12:47 -07001628 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001629 fb_queue_download("boot.img", data, sz);
1630 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001631 } else if (command == "flash") {
1632 std::string pname = next_arg(&args);
1633
Elliott Hughes2810d002016-04-25 14:31:18 -07001634 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001635 if (!args.empty()) {
1636 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001637 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001638 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001639 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001640 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001641
1642 auto flash = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001643 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001644 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001645 do_for_partitions(transport, pname.c_str(), slot_override, flash, true);
1646 } else if (command == "flash:raw") {
1647 std::string partition = next_arg(&args);
1648 std::string kernel = next_arg(&args);
1649 std::string ramdisk;
1650 if (!args.empty()) ramdisk = next_arg(&args);
1651 std::string second_stage;
1652 if (!args.empty()) second_stage = next_arg(&args);
1653
Elliott Hughes577e8b42018-04-05 16:12:47 -07001654 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001655 auto flashraw = [&](const std::string& partition) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001656 fb_queue_flash(partition, data, sz);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001657 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001658 do_for_partitions(transport, partition, slot_override, flashraw, true);
1659 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001660 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001661 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
Elliott Hughes59efd442018-04-05 14:24:00 -07001662 do_flashall(transport, slot_override, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001663 } else {
Elliott Hughes59efd442018-04-05 14:24:00 -07001664 do_flashall(transport, slot_override, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001665 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001666 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001667 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001668 bool slot_all = (slot_override == "all");
1669 if (slot_all) {
1670 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1671 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001672 std::string filename = "update.zip";
1673 if (!args.empty()) {
1674 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001675 }
Elliott Hughes59efd442018-04-05 14:24:00 -07001676 do_update(transport, filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001677 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001678 } else if (command == "set_active") {
1679 std::string slot = verify_slot(transport, next_arg(&args), false);
Elliott Hughes5620d222018-03-28 08:20:00 -07001680 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001681 } else if (command == "stage") {
1682 std::string filename = next_arg(&args);
1683
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001684 struct fastboot_buffer buf;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001685 if (!load_buf(transport, filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
1686 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001687 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001688 fb_queue_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001689 } else if (command == "get_staged") {
1690 std::string filename = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001691 fb_queue_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001692 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001693 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001694 } else if (command == "flashing") {
1695 if (args.empty()) {
1696 syntax_error("missing 'flashing' command");
1697 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1698 args[0] == "unlock_critical" ||
1699 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001700 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001701 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001702 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001703 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001704 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001705 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001706 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001707 }
1708 }
1709
1710 if (wants_wipe) {
1711 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001712 if (set_fbe_marker) {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001713 fprintf(stderr, "setting FBE marker on initial userdata...\n");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001714 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Elliott Hughesd6365a72017-05-08 18:04:49 -07001715 fb_perform_format(transport, "userdata", 1, "", "", initial_userdata_dir);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001716 delete_fbemarker_tmpdir(initial_userdata_dir);
1717 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001718 fb_perform_format(transport, "userdata", 1, "", "", "");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001719 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001720
1721 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001722 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001723 fb_queue_erase("cache");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001724 fb_perform_format(transport, "cache", 1, "", "", "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001725 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001726 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001727 if (wants_set_active) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001728 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001729 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001730 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001731 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001732 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001733 } else if (wants_reboot_bootloader) {
1734 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001735 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001736 }
1737
Elliott Hughes5620d222018-03-28 08:20:00 -07001738 int status = fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
1739 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
1740 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001741}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001742
1743void FastBoot::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
1744 unsigned year, month, day;
1745 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1746 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1747 }
1748 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1749 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1750 hdr->SetOsPatchLevel(year, month);
1751}
1752
1753void FastBoot::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
1754 unsigned major = 0, minor = 0, patch = 0;
1755 std::vector<std::string> versions = android::base::Split(arg, ".");
1756 if (versions.size() < 1 || versions.size() > 3 ||
1757 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1758 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1759 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1760 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1761 syntax_error("bad OS version: %s", arg);
1762 }
1763 hdr->SetOsVersion(major, minor, patch);
1764}