blob: c830321a858c2d95dd8ed5172a2abb1f0a0357f1 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070029#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <errno.h>
31#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070032#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070033#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070034#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070035#include <stdint.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070041#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070042#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080043
Elliott Hughes290a2282016-11-14 17:08:47 -080044#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070045#include <functional>
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080047#include <utility>
48#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070049
Elliott Hughes82ff3152016-08-31 15:07:18 -070050#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070051#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080053#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070054#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080055#include <android-base/strings.h>
Jin Qian4a335822017-04-18 16:23:18 -070056#include <android-base/test_utils.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050057#include <android-base/unique_fd.h>
Colin Crossf8387882012-05-24 17:18:41 -070058#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070059#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Elliott Hughes253c18d2015-03-18 22:47:09 -070061#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080062#include "diagnose_usb.h"
Aaron Wisnerdb511202018-06-26 15:38:35 -050063#include "engine.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070064#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080065#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070066#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080067#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070068#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Chris Fries0ea946c2017-04-12 10:25:57 -050070using android::base::unique_fd;
71
Colin Crossf8387882012-05-24 17:18:41 -070072#ifndef O_BINARY
73#define O_BINARY 0
74#endif
75
Wink Savilleb98762f2011-04-04 17:54:59 -070076char cur_product[FB_RESPONSE_SZ + 1];
77
David Pursell2ec418a2016-01-20 08:32:08 -080078static const char* serial = nullptr;
Elliott Hughes577e8b42018-04-05 16:12:47 -070079
Elliott Hughes577e8b42018-04-05 16:12:47 -070080static bool g_long_listing = false;
Chris Fries6a999712017-04-04 09:52:47 -050081// Don't resparse files in too-big chunks.
82// libsparse will support INT_MAX, but this results in large allocations, so
83// let's keep it at 1GB to avoid memory pressure on the host.
84static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Elliott Hughes542370d2018-04-19 19:49:44 -070085static uint64_t sparse_limit = 0;
Colin Crossf8387882012-05-24 17:18:41 -070086static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
Elliott Hughes577e8b42018-04-05 16:12:47 -070088static unsigned g_base_addr = 0x10000000;
89static boot_img_hdr_v1 g_boot_img_hdr = {};
90static std::string g_cmdline;
JP Abgrall7b8970c2013-03-07 17:06:41 -080091
David Zeuthenb6ea4352017-08-07 14:29:26 -040092static bool g_disable_verity = false;
93static bool g_disable_verification = false;
94
Paul Crowley8f7f56e2015-11-27 09:29:37 +000095static const std::string convert_fbe_marker_filename("convert_fbe");
96
Rom Lemarchand622810c2013-06-28 09:54:59 -070097enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -050098 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -070099 FB_BUFFER_SPARSE,
100};
101
102struct fastboot_buffer {
103 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700104 void* data;
105 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500106 int fd;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700107};
108
109static struct {
Elliott Hughes45964a82017-05-03 22:43:23 -0700110 const char* nickname;
111 const char* img_name;
112 const char* sig_name;
113 const char* part_name;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700114 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700115 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700116} images[] = {
Dario Frenic7ea1af2018-05-25 16:07:19 +0100117 // clang-format off
Elliott Hughes45964a82017-05-03 22:43:23 -0700118 { "boot", "boot.img", "boot.sig", "boot", false, false },
119 { nullptr, "boot_other.img", "boot.sig", "boot", true, true },
120 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, false },
Dmitry Shmidtde8c08c2017-05-15 10:24:04 -0700121 { "dts", "dt.img", "dt.sig", "dts", true, false },
Bowgo Tsai017217e2018-03-29 01:24:12 +0800122 { "odm", "odm.img", "odm.sig", "odm", true, false },
Bowgo Tsai0afc6b02018-03-26 15:45:01 +0800123 { "product", "product.img", "product.sig", "product", true, false },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100124 { "product-services",
125 "product-services.img",
126 "product-services.sig",
127 "product-services",
128 true, false },
Elliott Hughes45964a82017-05-03 22:43:23 -0700129 { "recovery", "recovery.img", "recovery.sig", "recovery", true, false },
130 { "system", "system.img", "system.sig", "system", false, false },
131 { nullptr, "system_other.img", "system.sig", "system", true, true },
132 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, false },
133 { "vendor", "vendor.img", "vendor.sig", "vendor", true, false },
134 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, true },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100135 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700136};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700137
Elliott Hughes45964a82017-05-03 22:43:23 -0700138static std::string find_item_given_name(const char* img_name) {
139 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700140 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700141 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700142 }
Alex Lightbb9b8a52016-06-29 09:26:44 -0700143 return android::base::StringPrintf("%s/%s", dir, img_name);
144}
145
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700146static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700147 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700148 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700149 return find_item_given_name(images[i].img_name);
150 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151 }
152
Elliott Hughesd6365a72017-05-08 18:04:49 -0700153 if (item == "userdata") return find_item_given_name("userdata.img");
154 if (item == "cache") return find_item_given_name("cache.img");
Elliott Hughes45964a82017-05-03 22:43:23 -0700155
Elliott Hughesd6365a72017-05-08 18:04:49 -0700156 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700157 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158}
159
Elliott Hughesfc797672015-04-07 20:12:50 -0700160static int64_t get_file_size(int fd) {
161 struct stat sb;
162 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700163}
164
Elliott Hughesfc797672015-04-07 20:12:50 -0700165static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800166 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700167 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168
Elliott Hughesfc797672015-04-07 20:12:50 -0700169 *sz = get_file_size(fd);
170 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700171 goto oops;
172 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173
Elliott Hughesfc797672015-04-07 20:12:50 -0700174 data = (char*) malloc(*sz);
175 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176
Elliott Hughesfc797672015-04-07 20:12:50 -0700177 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178 close(fd);
179
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 return data;
181
182oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800183 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184 close(fd);
185 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800186 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 return 0;
188}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189
Elliott Hughes2810d002016-04-25 14:31:18 -0700190static void* load_file(const std::string& path, int64_t* sz) {
191 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700192 if (fd == -1) return nullptr;
193 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700194}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195
Elliott Hughesfc797672015-04-07 20:12:50 -0700196static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700197 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
198 return -1;
199 }
200
Scott Anderson13081c62012-04-06 12:39:30 -0700201 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700203 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
204 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205 return 0;
206}
207
Elliott Hughesfc797672015-04-07 20:12:50 -0700208static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800209 return match_fastboot_with_serial(info, serial);
210}
211
Elliott Hughesfc797672015-04-07 20:12:50 -0700212static int list_devices_callback(usb_ifc_info* info) {
213 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800214 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700215 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800216 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700217 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 if (!serial[0]) {
219 serial = "????????????";
220 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700221 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700222 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800223 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700224 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800225 printf("%-22s fastboot", serial.c_str());
226 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700227 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800228 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229 }
230
231 return -1;
232}
233
David Pursell2ec418a2016-01-20 08:32:08 -0800234// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
235// a specific device, otherwise the first USB device found will be used.
236//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700237// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800238// Otherwise it blocks until the target is available.
239//
240// The returned Transport is a singleton, so multiple calls to this function will return the same
241// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700242static Transport* open_device() {
243 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800244 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245
David Pursell2ec418a2016-01-20 08:32:08 -0800246 if (transport != nullptr) {
247 return transport;
248 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800249
David Pursell4601c972016-02-05 15:35:09 -0800250 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800251 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800252 int port = 0;
253 if (serial != nullptr) {
254 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800255
David Pursell4601c972016-02-05 15:35:09 -0800256 if (android::base::StartsWith(serial, "tcp:")) {
257 protocol = Socket::Protocol::kTcp;
258 port = tcp::kDefaultPort;
259 net_address = serial + strlen("tcp:");
260 } else if (android::base::StartsWith(serial, "udp:")) {
261 protocol = Socket::Protocol::kUdp;
262 port = udp::kDefaultPort;
263 net_address = serial + strlen("udp:");
264 }
265
266 if (net_address != nullptr) {
267 std::string error;
268 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700269 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800270 }
David Pursell2ec418a2016-01-20 08:32:08 -0800271 }
272 }
273
274 while (true) {
275 if (!host.empty()) {
276 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800277 if (protocol == Socket::Protocol::kTcp) {
278 transport = tcp::Connect(host, port, &error).release();
279 } else if (protocol == Socket::Protocol::kUdp) {
280 transport = udp::Connect(host, port, &error).release();
281 }
282
David Pursell2ec418a2016-01-20 08:32:08 -0800283 if (transport == nullptr && announce) {
284 fprintf(stderr, "error: %s\n", error.c_str());
285 }
286 } else {
287 transport = usb_open(match_fastboot);
288 }
289
290 if (transport != nullptr) {
291 return transport;
292 }
293
David Pursell0b156632015-10-30 11:22:01 -0700294 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800295 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700296 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800298 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 }
300}
301
Elliott Hughesfc797672015-04-07 20:12:50 -0700302static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 // We don't actually open a USB device here,
304 // just getting our callback called so we can
305 // list all the connected devices.
306 usb_open(list_devices_callback);
307}
308
Elliott Hughesd6365a72017-05-08 18:04:49 -0700309static void syntax_error(const char* fmt, ...) {
310 fprintf(stderr, "fastboot: usage: ");
311
312 va_list ap;
313 va_start(ap, fmt);
314 vfprintf(stderr, fmt, ap);
315 va_end(ap);
316
317 fprintf(stderr, "\n");
318 exit(1);
319}
320
321static int show_help() {
322 // clang-format off
323 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700324// 1 2 3 4 5 6 7 8
325// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
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"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700333 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
334 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700335 "\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"
Elliott Hughes542370d2018-04-19 19:49:44 -0700384 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700385 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
386 " non-current slot (default: current active slot).\n"
387 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
388 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
389 " --skip-reboot Don't reboot device after flashing.\n"
390 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
391 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000392#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700393 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000394#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700395 // TODO: remove --unbuffered?
396 " --unbuffered Don't buffer input or output.\n"
397 " --verbose, -v Verbose output.\n"
398 " --version Display version.\n"
399 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700401 // clang-format off
402 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000404
Elliott Hughesd6365a72017-05-08 18:04:49 -0700405static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
Elliott Hughes577e8b42018-04-05 16:12:47 -0700406 const std::string& second_stage, int64_t* sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700407 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700408 void* kdata = load_file(kernel.c_str(), &ksize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700409 if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800410
Elliott Hughesfc797672015-04-07 20:12:50 -0700411 // Is this actually a boot image?
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700412 if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800413 die("cannot load '%s': too short", kernel.c_str());
414 }
Elliott Hughesd6365a72017-05-08 18:04:49 -0700415 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700416 if (!g_cmdline.empty()) {
417 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), g_cmdline);
418 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800419
Elliott Hughes4089d342017-10-27 14:21:12 -0700420 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800421
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422 *sz = ksize;
423 return kdata;
424 }
425
Elliott Hughesfc797672015-04-07 20:12:50 -0700426 void* rdata = nullptr;
427 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700428 if (!ramdisk.empty()) {
429 rdata = load_file(ramdisk.c_str(), &rsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700430 if (rdata == nullptr) die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431 }
432
Elliott Hughesfc797672015-04-07 20:12:50 -0700433 void* sdata = nullptr;
434 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700435 if (!second_stage.empty()) {
436 sdata = load_file(second_stage.c_str(), &ssize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700437 if (sdata == nullptr) die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200438 }
439
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440 fprintf(stderr,"creating boot image...\n");
Elliott Hughes577e8b42018-04-05 16:12:47 -0700441 boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, rdata, rsize, sdata, ssize,
442 g_base_addr, g_boot_img_hdr, sz);
Elliott Hughes4089d342017-10-27 14:21:12 -0700443 if (bdata == nullptr) die("failed to create boot.img");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700444
Elliott Hughes577e8b42018-04-05 16:12:47 -0700445 if (!g_cmdline.empty()) bootimg_set_cmdline(bdata, g_cmdline);
446 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", *sz);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800447
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 return bdata;
449}
450
Elliott Hughes23af1122017-11-10 08:42:17 -0800451static void* unzip_to_memory(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700452 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700453 ZipEntry zip_entry;
454 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700455 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Elliott Hughes4089d342017-10-27 14:21:12 -0700456 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457 }
458
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700459 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460
Elliott Hughes23af1122017-11-10 08:42:17 -0800461 fprintf(stderr, "extracting %s (%" PRId64 " MB) to RAM...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700462 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughes4089d342017-10-27 14:21:12 -0700463 if (data == nullptr) die("failed to allocate %" PRId64 " bytes for '%s'", *sz, entry_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700465 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
Elliott Hughes4089d342017-10-27 14:21:12 -0700466 if (error != 0) die("failed to extract '%s': %s", entry_name, ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000467
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 return data;
469}
470
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700471#if defined(_WIN32)
472
473// TODO: move this to somewhere it can be shared.
474
475#include <windows.h>
476
477// Windows' tmpfile(3) requires administrator rights because
478// it creates temporary files in the root directory.
479static FILE* win32_tmpfile() {
480 char temp_path[PATH_MAX];
481 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
482 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700483 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700484 }
485
486 char filename[PATH_MAX];
487 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700488 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700489 }
490
491 return fopen(filename, "w+bTD");
492}
493
494#define tmpfile win32_tmpfile
495
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000496static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700497 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000498}
499
Elliott Hughes855cdf82018-04-02 14:24:03 -0700500static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700501 // TODO: reimplement to avoid leaking a FILE*.
502 return fileno(tmpfile());
503}
504
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000505#else
506
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700507static std::string make_temporary_template() {
508 const char* tmpdir = getenv("TMPDIR");
509 if (tmpdir == nullptr) tmpdir = P_tmpdir;
510 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
511}
512
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000513static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700514 std::string result(make_temporary_template());
515 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700516 die("unable to create temporary directory with template %s: %s",
517 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000518 }
519 return result;
520}
521
Elliott Hughes855cdf82018-04-02 14:24:03 -0700522static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700523 std::string path_template(make_temporary_template());
524 int fd = mkstemp(&path_template[0]);
525 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700526 die("failed to create temporary file for %s with template %s: %s\n",
527 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700528 }
529 unlink(path_template.c_str());
530 return fd;
531}
532
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700533#endif
534
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000535static std::string create_fbemarker_tmpdir() {
536 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000537 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
538 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
539 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700540 die("unable to create FBE marker file %s locally: %s",
541 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000542 }
543 close(fd);
544 return dir;
545}
546
547static void delete_fbemarker_tmpdir(const std::string& dir) {
548 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
549 if (unlink(marker_file.c_str()) == -1) {
550 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
551 marker_file.c_str(), errno, strerror(errno));
552 return;
553 }
554 if (rmdir(dir.c_str()) == -1) {
555 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
556 dir.c_str(), errno, strerror(errno));
557 return;
558 }
559}
560
Elliott Hughes45964a82017-05-03 22:43:23 -0700561static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700562 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700563
Yusuke Sato07447542015-06-25 14:39:19 -0700564 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700565 ZipEntry zip_entry;
566 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
567 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000568 return -1;
569 }
570
Elliott Hughes23af1122017-11-10 08:42:17 -0800571 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700572 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800573 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700574 int error = ExtractEntryToFile(zip, &zip_entry, fd);
575 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800576 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700577 }
578
Elliott Hughes4089d342017-10-27 14:21:12 -0700579 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800580 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700581 }
582
Elliott Hughes23af1122017-11-10 08:42:17 -0800583 fprintf(stderr, " took %.3fs\n", now() - start);
584
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700585 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700586}
587
Elliott Hughes5620d222018-03-28 08:20:00 -0700588static char* strip(char* s) {
589 while (*s && isspace(*s)) s++;
590
591 int n = strlen(s);
592 while (n-- > 0) {
593 if (!isspace(s[n])) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800594 s[n] = 0;
595 }
596 return s;
597}
598
599#define MAX_OPTIONS 32
Aaron Wisnerdb511202018-06-26 15:38:35 -0500600static void check_requirement(char* line) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800601 char *val[MAX_OPTIONS];
Elliott Hughes5620d222018-03-28 08:20:00 -0700602 unsigned count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800603 char *x;
604 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800605
Elliott Hughes5620d222018-03-28 08:20:00 -0700606 // "require product=alpha|beta|gamma"
607 // "require version-bootloader=1234"
608 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
609 // "require partition-exists=vendor"
610
611 char* name = line;
612 const char* product = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800613 if (!strncmp(name, "reject ", 7)) {
614 name += 7;
615 invert = 1;
616 } else if (!strncmp(name, "require ", 8)) {
617 name += 8;
618 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700619 } else if (!strncmp(name, "require-for-product:", 20)) {
620 // Get the product and point name past it
Elliott Hughes5620d222018-03-28 08:20:00 -0700621 product = name + 20;
Wink Savilleb98762f2011-04-04 17:54:59 -0700622 name = strchr(name, ' ');
Elliott Hughes5620d222018-03-28 08:20:00 -0700623 if (!name) die("android-info.txt syntax error: %s", line);
Wink Savilleb98762f2011-04-04 17:54:59 -0700624 *name = 0;
625 name += 1;
626 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800627 }
628
629 x = strchr(name, '=');
Elliott Hughes5620d222018-03-28 08:20:00 -0700630 if (x == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800631 *x = 0;
632 val[0] = x + 1;
633
Elliott Hughes5620d222018-03-28 08:20:00 -0700634 name = strip(name);
635
636 // "require partition-exists=x" is a special case, added because of the trouble we had when
637 // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
638 // missing out new partitions. A device with new partitions can use "partition-exists" to
639 // override the `is_optional` field in the `images` array.
640 if (!strcmp(name, "partition-exists")) {
641 const char* partition_name = val[0];
642 std::string has_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500643 if (!fb_getvar(std::string("has-slot:") + partition_name, &has_slot) ||
Elliott Hughes5620d222018-03-28 08:20:00 -0700644 (has_slot != "yes" && has_slot != "no")) {
645 die("device doesn't have required partition %s!", partition_name);
646 }
647 bool known_partition = false;
648 for (size_t i = 0; i < arraysize(images); ++i) {
649 if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
650 images[i].is_optional = false;
651 known_partition = true;
652 }
653 }
654 if (!known_partition) {
655 die("device requires partition %s which is not known to this version of fastboot",
656 partition_name);
657 }
658 return;
659 }
660
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661 for(count = 1; count < MAX_OPTIONS; count++) {
662 x = strchr(val[count - 1],'|');
663 if (x == 0) break;
664 *x = 0;
665 val[count] = x + 1;
666 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800667
Elliott Hughes253c18d2015-03-18 22:47:09 -0700668 // Work around an unfortunate name mismatch.
Elliott Hughes5620d222018-03-28 08:20:00 -0700669 const char* var = name;
670 if (!strcmp(name, "board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800671
Elliott Hughes253c18d2015-03-18 22:47:09 -0700672 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
Elliott Hughes5620d222018-03-28 08:20:00 -0700673 if (out == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800674
Elliott Hughes5620d222018-03-28 08:20:00 -0700675 for (size_t i = 0; i < count; ++i) {
676 out[i] = xstrdup(strip(val[i]));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800677 }
678
Elliott Hughes5620d222018-03-28 08:20:00 -0700679 fb_queue_require(product, var, invert, count, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800680}
681
Aaron Wisnerdb511202018-06-26 15:38:35 -0500682static void check_requirements(char* data, int64_t sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700683 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800684 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700685 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686 *s++ = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500687 check_requirement(data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688 data = s;
689 } else {
690 s++;
691 }
692 }
Aaron Wisnerdb511202018-06-26 15:38:35 -0500693 if (fb_execute_queue()) die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694}
695
Elliott Hughesfc797672015-04-07 20:12:50 -0700696static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800697 fb_queue_notice("--------------------------------------------");
Elliott Hughes5620d222018-03-28 08:20:00 -0700698 fb_queue_display("Bootloader Version...", "version-bootloader");
699 fb_queue_display("Baseband Version.....", "version-baseband");
700 fb_queue_display("Serial Number........", "serialno");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800701 fb_queue_notice("--------------------------------------------");
702}
703
Tao Bao41cf35f2018-04-24 10:54:21 -0700704static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700705 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700706 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700707
Tao Bao41cf35f2018-04-24 10:54:21 -0700708 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
709 die("invalid max size %" PRId64, max_size);
710 }
711
Elliott Hughesfc797672015-04-07 20:12:50 -0700712 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700713 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700714
Elliott Hughes253c18d2015-03-18 22:47:09 -0700715 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700716 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700717
718 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700719 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700720
721 return out_s;
722}
723
Aaron Wisnerdb511202018-06-26 15:38:35 -0500724static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700725 std::string max_download_size;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500726 if (!fb_getvar("max-download-size", &max_download_size) ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700727 max_download_size.empty()) {
728 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700729 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700730 }
731
Elliott Hughes77c0e662015-11-02 15:51:12 -0800732 // Some bootloaders (angler, for example) send spurious whitespace too.
733 max_download_size = android::base::Trim(max_download_size);
734
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700735 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700736 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800737 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700738 return 0;
739 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700740 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700741 return limit;
742}
743
Aaron Wisnerdb511202018-06-26 15:38:35 -0500744static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700745 int64_t limit = sparse_limit;
746 if (limit == 0) {
747 // Unlimited, so see what the target device's limit is.
748 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700749 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500750 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700751 }
752 if (target_sparse_limit > 0) {
753 limit = target_sparse_limit;
754 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700755 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700756 }
757 }
758
759 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500760 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700761 }
762
763 return 0;
764}
765
Aaron Wisnerdb511202018-06-26 15:38:35 -0500766static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700767 int64_t sz = get_file_size(fd);
768 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700769 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700770 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700771
Elliott Hughesfc797672015-04-07 20:12:50 -0700772 lseek64(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500773 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700774 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700775 sparse_file** s = load_sparse_files(fd, limit);
776 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700777 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700778 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700779 buf->type = FB_BUFFER_SPARSE;
780 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700781 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500782 buf->type = FB_BUFFER_FD;
783 buf->data = nullptr;
784 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000785 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700786 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700787
Elliott Hughes53ec4952016-05-11 12:39:27 -0700788 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700789}
790
Aaron Wisnerdb511202018-06-26 15:38:35 -0500791static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500792 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
793
Elliott Hughes53ec4952016-05-11 12:39:27 -0700794 if (fd == -1) {
795 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700796 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500797
798 struct stat s;
799 if (fstat(fd, &s)) {
800 return false;
801 }
802 if (!S_ISREG(s.st_mode)) {
803 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
804 return false;
805 }
806
Aaron Wisnerdb511202018-06-26 15:38:35 -0500807 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700808}
809
David Zeuthenb6ea4352017-08-07 14:29:26 -0400810static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
811 // Buffer needs to be at least the size of the VBMeta struct which
812 // is 256 bytes.
813 if (buf->sz < 256) {
814 return;
815 }
816
Elliott Hughes855cdf82018-04-02 14:24:03 -0700817 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400818
819 std::string data;
820 if (!android::base::ReadFdToString(buf->fd, &data)) {
821 die("Failed reading from vbmeta");
822 }
823
824 // There's a 32-bit big endian |flags| field at offset 120 where
825 // bit 0 corresponds to disable-verity and bit 1 corresponds to
826 // disable-verification.
827 //
828 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
829 // the VBMeta struct.
830 if (g_disable_verity) {
831 data[123] |= 0x01;
832 }
833 if (g_disable_verification) {
834 data[123] |= 0x02;
835 }
836
837 if (!android::base::WriteStringToFd(data, fd)) {
838 die("Failed writing to modified vbmeta");
839 }
840 close(buf->fd);
841 buf->fd = fd;
842 lseek(fd, 0, SEEK_SET);
843}
844
Elliott Hughes5620d222018-03-28 08:20:00 -0700845static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700846{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700847 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700848
David Zeuthenb6ea4352017-08-07 14:29:26 -0400849 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
850 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700851 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400852 rewrite_vbmeta_buffer(buf);
853 }
854
Rom Lemarchand622810c2013-06-28 09:54:59 -0700855 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800856 case FB_BUFFER_SPARSE: {
857 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700858 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700859 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700860 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800861 sparse_files.emplace_back(*s, sz);
862 ++s;
863 }
864
865 for (size_t i = 0; i < sparse_files.size(); ++i) {
866 const auto& pair = sparse_files[i];
Elliott Hughes5620d222018-03-28 08:20:00 -0700867 fb_queue_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700868 }
869 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800870 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500871 case FB_BUFFER_FD:
Elliott Hughes5620d222018-03-28 08:20:00 -0700872 fb_queue_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700873 break;
874 default:
875 die("unknown buffer type: %d", buf->type);
876 }
877}
878
Aaron Wisnerdb511202018-06-26 15:38:35 -0500879static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700880 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500881 if (!fb_getvar("current-slot", &current_slot)) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700882 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700883}
884
Aaron Wisnerdb511202018-06-26 15:38:35 -0500885static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700886 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700887 int count = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500888 if (!fb_getvar("slot-count", &var) || !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700889 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700890 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700891 return count;
892}
893
Aaron Wisnerdb511202018-06-26 15:38:35 -0500894static bool supports_AB() {
895 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700896}
897
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700898// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700899static std::string get_other_slot(const std::string& current_slot, int count) {
900 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700901
Daniel Rosenberg80919472016-06-30 19:25:31 -0700902 char next = (current_slot[0] - 'a' + 1)%count + 'a';
903 return std::string(1, next);
904}
905
Aaron Wisnerdb511202018-06-26 15:38:35 -0500906static std::string get_other_slot(const std::string& current_slot) {
907 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -0700908}
909
Aaron Wisnerdb511202018-06-26 15:38:35 -0500910static std::string get_other_slot(int count) {
911 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700912}
913
Aaron Wisnerdb511202018-06-26 15:38:35 -0500914static std::string get_other_slot() {
915 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -0700916}
917
Aaron Wisnerdb511202018-06-26 15:38:35 -0500918static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700919 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700920 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800921 if (allow_all) {
922 return "all";
923 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500924 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700925 if (count > 0) {
926 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800927 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -0700928 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800929 }
930 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700931 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800932
Aaron Wisnerdb511202018-06-26 15:38:35 -0500933 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -0700934 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800935
Daniel Rosenberg80919472016-06-30 19:25:31 -0700936 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500937 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700938 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700939 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800940 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700941 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800942 }
943
Daniel Rosenberg80919472016-06-30 19:25:31 -0700944 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
945
946 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
947 for (int i=0; i<count; i++) {
948 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700949 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700950
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700951 exit(1);
952}
953
Aaron Wisnerdb511202018-06-26 15:38:35 -0500954static std::string verify_slot(const std::string& slot) {
955 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800956}
957
Aaron Wisnerdb511202018-06-26 15:38:35 -0500958static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700959 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800960 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700961 std::string current_slot;
962
Aaron Wisnerdb511202018-06-26 15:38:35 -0500963 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800964 /* If has-slot is not supported, the answer is no. */
965 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700966 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800967 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700968 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500969 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700970 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700971 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700972 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700973 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700974 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700975 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700976 }
977 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700978 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -0700979 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -0700980 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700981 }
982 func(part);
983 }
984}
985
David Pursell0b156632015-10-30 11:22:01 -0700986/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
987 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
988 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
989 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700990 */
Aaron Wisnerdb511202018-06-26 15:38:35 -0500991static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700992 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800993 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700994
Daniel Rosenberg80919472016-06-30 19:25:31 -0700995 if (slot == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500996 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700997 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700998 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800999 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001000 for (int i=0; i < get_slot_count(); i++) {
1001 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001002 }
1003 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001004 do_for_partition(part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001005 }
1006 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001007 do_for_partition(part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001008 }
1009}
1010
Aaron Wisnerdb511202018-06-26 15:38:35 -05001011static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001012 struct fastboot_buffer buf;
1013
Aaron Wisnerdb511202018-06-26 15:38:35 -05001014 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001015 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001016 }
1017 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001018}
1019
Elliott Hughes45964a82017-05-03 22:43:23 -07001020static void do_update_signature(ZipArchiveHandle zip, const char* filename) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001021 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001022 void* data = unzip_to_memory(zip, filename, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001023 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001024 fb_queue_download("signature", data, sz);
1025 fb_queue_command("signature", "installing signature");
1026}
1027
Daniel Rosenberg13454092016-06-27 19:43:11 -07001028// Sets slot_override as the active slot. If slot_override is blank,
1029// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001030static void set_active(const std::string& slot_override) {
1031 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001032
Daniel Rosenberg80919472016-06-30 19:25:31 -07001033 if (slot_override != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001034 fb_set_active(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001035 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001036 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001037 if (current_slot != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001038 fb_set_active(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001039 }
1040 }
1041}
1042
Aaron Wisnerdb511202018-06-26 15:38:35 -05001043static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001044 queue_info_dump();
1045
Wink Savilleb98762f2011-04-04 17:54:59 -07001046 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1047
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001048 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001049 int error = OpenArchive(filename, &zip);
1050 if (error != 0) {
1051 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001052 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001053
Elliott Hughesfc797672015-04-07 20:12:50 -07001054 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001055 void* data = unzip_to_memory(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001056 if (data == nullptr) {
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001057 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001058 }
1059
Aaron Wisnerdb511202018-06-26 15:38:35 -05001060 check_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001061
Alex Lightbb9b8a52016-06-29 09:26:44 -07001062 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001063 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001064 if (slot_override != "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001065 secondary = get_other_slot(slot_override);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001066 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001067 secondary = get_other_slot();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001068 }
1069 if (secondary == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001070 if (supports_AB()) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001071 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1072 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001073 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001074 }
1075 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001076 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001077 const char* slot = slot_override.c_str();
1078 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001079 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001080 slot = secondary.c_str();
1081 } else {
1082 continue;
1083 }
1084 }
1085
Elliott Hughes253c18d2015-03-18 22:47:09 -07001086 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001087 if (fd == -1) {
1088 if (images[i].is_optional) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001089 continue; // An optional file is missing, so ignore it.
Elliott Hughesacdbe922015-06-02 21:37:05 -07001090 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001091 die("non-optional file %s missing", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001092 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001093
Elliott Hughes253c18d2015-03-18 22:47:09 -07001094 fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001095 if (!load_buf_fd(fd, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001096 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1097 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001098
Elliott Hughes4089d342017-10-27 14:21:12 -07001099 auto update = [&](const std::string& partition) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001100 do_update_signature(zip, images[i].sig_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001101 flash_buf(partition.c_str(), &buf);
1102 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001103 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001104 * program exits.
1105 */
1106 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001107 do_for_partitions(images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001108 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001109
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001110 if (slot_override == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001111 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001112 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001113 set_active(slot_override);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001114 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001115
1116 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001117}
1118
Alex Lightbb9b8a52016-06-29 09:26:44 -07001119static void do_send_signature(const std::string& fn) {
1120 std::size_t extension_loc = fn.find(".img");
1121 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001122
Alex Lightbb9b8a52016-06-29 09:26:44 -07001123 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001124
1125 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001126 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001127 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001128
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001129 fb_queue_download("signature", data, sz);
1130 fb_queue_command("signature", "installing signature");
1131}
1132
Aaron Wisnerdb511202018-06-26 15:38:35 -05001133static void do_flashall(const std::string& slot_override, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001134 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001135 queue_info_dump();
1136
Wink Savilleb98762f2011-04-04 17:54:59 -07001137 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1138
Elliott Hughes45964a82017-05-03 22:43:23 -07001139 fname = find_item_given_name("android-info.txt");
Elliott Hughes2810d002016-04-25 14:31:18 -07001140 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001141
Elliott Hughesfc797672015-04-07 20:12:50 -07001142 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001143 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001144 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001145
Aaron Wisnerdb511202018-06-26 15:38:35 -05001146 check_requirements(reinterpret_cast<char*>(data), sz);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001147
Alex Lightbb9b8a52016-06-29 09:26:44 -07001148 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001149 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001150 if (slot_override != "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001151 secondary = get_other_slot(slot_override);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001152 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001153 secondary = get_other_slot();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001154 }
1155 if (secondary == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001156 if (supports_AB()) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001157 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1158 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001159 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001160 }
1161 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001162
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001163 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001164 const char* slot = NULL;
1165 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001166 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001167 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001168 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001169 }
1170 if (!slot) continue;
Elliott Hughes45964a82017-05-03 22:43:23 -07001171 fname = find_item_given_name(images[i].img_name);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001172 fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001173 if (!load_buf(fname.c_str(), &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001174 if (images[i].is_optional) continue;
Elliott Hughes4089d342017-10-27 14:21:12 -07001175 die("could not load '%s': %s", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001176 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001177
1178 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001179 do_send_signature(fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001180 flash_buf(partition.c_str(), &buf);
1181 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001182 do_for_partitions(images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001183 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001184
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001185 if (slot_override == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001186 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001187 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001188 set_active(slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001189 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001190}
1191
Elliott Hughesd6365a72017-05-08 18:04:49 -07001192static std::string next_arg(std::vector<std::string>* args) {
1193 if (args->empty()) syntax_error("expected argument");
1194 std::string result = args->front();
1195 args->erase(args->begin());
1196 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001197}
1198
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001199static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001200 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001201
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001202 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001203 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001204 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001205 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001206 fb_queue_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001207}
1208
Connor O'Brience16a8a2017-02-06 14:39:31 -08001209static std::string fb_fix_numeric_var(std::string var) {
1210 // Some bootloaders (angler, for example), send spurious leading whitespace.
1211 var = android::base::Trim(var);
1212 // Some bootloaders (hammerhead, for example) use implicit hex.
1213 // This code used to use strtol with base 16.
1214 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1215 return var;
1216}
1217
Aaron Wisnerdb511202018-06-26 15:38:35 -05001218static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001219 std::string sizeString;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001220 if (!fb_getvar(name, &sizeString) || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001221 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001222 return 0;
1223 }
1224 sizeString = fb_fix_numeric_var(sizeString);
1225
1226 unsigned size;
1227 if (!android::base::ParseUint(sizeString, &size)) {
1228 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1229 return 0;
1230 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001231 if ((size & (size - 1)) != 0) {
1232 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001233 return 0;
1234 }
1235 return size;
1236}
1237
Aaron Wisnerdb511202018-06-26 15:38:35 -05001238static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001239 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001240 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001241 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001242 std::string partition_type, partition_size;
1243
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001244 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001245 const char* errMsg = nullptr;
1246 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001247 TemporaryFile output;
1248 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001249
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001250 unsigned int limit = INT_MAX;
1251 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001252 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001253 }
1254 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001255 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001256 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001257
Aaron Wisnerdb511202018-06-26 15:38:35 -05001258 if (!fb_getvar("partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001259 errMsg = "Can't determine partition type.\n";
1260 goto failed;
1261 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001262 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001263 if (partition_type != type_override) {
1264 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001265 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001266 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001267 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001268 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001269
Aaron Wisnerdb511202018-06-26 15:38:35 -05001270 if (!fb_getvar("partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001271 errMsg = "Unable to get partition size\n";
1272 goto failed;
1273 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001274 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001275 if (partition_size != size_override) {
1276 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001277 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001278 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001279 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001280 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001281 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001282
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001283 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001284 if (!gen) {
1285 if (skip_if_not_supported) {
1286 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001287 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001288 return;
1289 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001290 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1291 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001292 return;
1293 }
1294
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001295 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001296 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001297 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1298 return;
1299 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001300
Connor O'Brience16a8a2017-02-06 14:39:31 -08001301 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001302 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1303 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001304
Jin Qian4a335822017-04-18 16:23:18 -07001305 if (fs_generator_generate(gen, output.path, size, initial_dir,
1306 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001307 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001308 return;
1309 }
1310
Jin Qian4a335822017-04-18 16:23:18 -07001311 fd.reset(open(output.path, O_RDONLY));
1312 if (fd == -1) {
1313 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1314 return;
1315 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001316 if (!load_buf_fd(fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001317 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001318 return;
1319 }
1320 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001321 return;
1322
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001323failed:
1324 if (skip_if_not_supported) {
1325 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001326 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001327 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001328 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001329}
1330
Aaron Wisnerdb511202018-06-26 15:38:35 -05001331int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001332 bool wants_wipe = false;
1333 bool wants_reboot = false;
1334 bool wants_reboot_bootloader = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001335 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001336 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001337 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001338 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001339 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001340 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001341 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001342 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001343 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001344
Elliott Hughes577e8b42018-04-05 16:12:47 -07001345 g_boot_img_hdr.kernel_addr = 0x00008000;
1346 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1347 g_boot_img_hdr.second_addr = 0x00f00000;
1348 g_boot_img_hdr.tags_addr = 0x00000100;
1349 g_boot_img_hdr.page_size = 2048;
1350
JP Abgrall7b8970c2013-03-07 17:06:41 -08001351 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001352 {"base", required_argument, 0, 0},
1353 {"cmdline", required_argument, 0, 0},
1354 {"disable-verification", no_argument, 0, 0},
1355 {"disable-verity", no_argument, 0, 0},
1356 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001357 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001358 {"kernel-offset", required_argument, 0, 0},
1359 {"os-patch-level", required_argument, 0, 0},
1360 {"os-version", required_argument, 0, 0},
1361 {"page-size", required_argument, 0, 0},
1362 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001363 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001364 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001365 {"skip-secondary", no_argument, 0, 0},
1366 {"slot", required_argument, 0, 0},
1367 {"tags-offset", required_argument, 0, 0},
1368 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001369 {"verbose", no_argument, 0, 'v'},
1370 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001371#if !defined(_WIN32)
1372 {"wipe-and-use-fbe", no_argument, 0, 0},
1373#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001374 {0, 0, 0, 0}
1375 };
Colin Cross8879f982012-05-22 17:53:34 -07001376
1377 serial = getenv("ANDROID_SERIAL");
1378
Elliott Hughes577e8b42018-04-05 16:12:47 -07001379 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001380 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001381 if (c == 0) {
1382 std::string name{longopts[longindex].name};
1383 if (name == "base") {
1384 g_base_addr = strtoul(optarg, 0, 16);
1385 } else if (name == "cmdline") {
1386 g_cmdline = optarg;
1387 } else if (name == "disable-verification") {
1388 g_disable_verification = true;
1389 } else if (name == "disable-verity") {
1390 g_disable_verity = true;
1391 } else if (name == "header-version") {
1392 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1393 } else if (name == "kernel-offset") {
1394 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1395 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001396 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001397 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001398 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001399 } else if (name == "page-size") {
1400 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1401 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1402 } else if (name == "ramdisk-offset") {
1403 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1404 } else if (name == "skip-reboot") {
1405 skip_reboot = true;
1406 } else if (name == "skip-secondary") {
1407 skip_secondary = true;
1408 } else if (name == "slot") {
1409 slot_override = optarg;
1410 } else if (name == "tags-offset") {
1411 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1412 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001413 setvbuf(stdout, nullptr, _IONBF, 0);
1414 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001415 } else if (name == "version") {
Elliott Hughes4d4f64f2017-05-08 11:30:34 -07001416 fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001417 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001418 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001419#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001420 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001421 wants_wipe = true;
1422 set_fbe_marker = true;
1423#endif
1424 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001425 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001426 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001427 } else {
1428 switch (c) {
1429 case 'a':
1430 wants_set_active = true;
1431 if (optarg) next_active = optarg;
1432 break;
1433 case 'h':
1434 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001435 case 'l':
1436 g_long_listing = true;
1437 break;
1438 case 's':
1439 serial = optarg;
1440 break;
1441 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001442 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1443 die("invalid sparse limit %s", optarg);
1444 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001445 break;
1446 case 'v':
1447 set_verbose();
1448 break;
1449 case 'w':
1450 wants_wipe = true;
1451 break;
1452 case '?':
1453 return 1;
1454 default:
1455 abort();
1456 }
Colin Cross8879f982012-05-22 17:53:34 -07001457 }
1458 }
1459
1460 argc -= optind;
1461 argv += optind;
1462
Elliott Hughesd6365a72017-05-08 18:04:49 -07001463 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001464
Colin Cross8fb6e062012-07-24 16:36:41 -07001465 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001466 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001467 return 0;
1468 }
1469
Colin Crossc7b75dc2012-08-29 18:17:06 -07001470 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001471 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001472 }
1473
David Pursell0b156632015-10-30 11:22:01 -07001474 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001475 if (transport == nullptr) {
1476 return 1;
1477 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001478 fastboot::FastBootDriver fb(transport);
1479 fb_init(fb);
David Pursell2ec418a2016-01-20 08:32:08 -08001480
Elliott Hughes5620d222018-03-28 08:20:00 -07001481 const double start = now();
1482
Aaron Wisnerdb511202018-06-26 15:38:35 -05001483 if (slot_override != "") slot_override = verify_slot(slot_override);
1484 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001485
1486 if (wants_set_active) {
1487 if (next_active == "") {
1488 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001489 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001490 if (fb_getvar("current-slot", &current_slot)) {
1491 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001492 } else {
1493 wants_set_active = false;
1494 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001495 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001496 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001497 }
1498 }
1499 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001500
Elliott Hughesd6365a72017-05-08 18:04:49 -07001501 std::vector<std::string> args(argv, argv + argc);
1502 while (!args.empty()) {
1503 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001504
Elliott Hughesd6365a72017-05-08 18:04:49 -07001505 if (command == "getvar") {
1506 std::string variable = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001507 fb_queue_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001508 } else if (command == "erase") {
1509 std::string partition = next_arg(&args);
1510 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001511 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001512 if (fb_getvar(std::string("partition-type:") + partition,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001513 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001514 fs_get_generator(partition_type) != nullptr) {
1515 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1516 partition_type.c_str());
1517 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001518
Elliott Hughes5620d222018-03-28 08:20:00 -07001519 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001520 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001521 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001522 } else if (android::base::StartsWith(command, "format")) {
1523 // Parsing for: "format[:[type][:[size]]]"
1524 // Some valid things:
1525 // - select only the size, and leave default fs type:
1526 // format::0x4000000 userdata
1527 // - default fs type and size:
1528 // format userdata
1529 // format:: userdata
1530 std::vector<std::string> pieces = android::base::Split(command, ":");
1531 std::string type_override;
1532 if (pieces.size() > 1) type_override = pieces[1].c_str();
1533 std::string size_override;
1534 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001535
Elliott Hughesd6365a72017-05-08 18:04:49 -07001536 std::string partition = next_arg(&args);
1537
1538 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001539 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001540 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001541 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001542 } else if (command == "signature") {
1543 std::string filename = next_arg(&args);
1544 data = load_file(filename.c_str(), &sz);
1545 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -07001546 if (sz != 256) die("signature must be 256 bytes (got %" PRId64 ")", sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001547 fb_queue_download("signature", data, sz);
1548 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001549 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001550 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001551
1552 if (args.size() == 1) {
1553 std::string what = next_arg(&args);
1554 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001555 wants_reboot = false;
1556 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001557 } else {
1558 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001559 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001560
Elliott Hughesca85df02015-02-25 10:02:00 -08001561 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001562 if (!args.empty()) syntax_error("junk after reboot command");
1563 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001564 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001565 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001566 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001567 } else if (command == "boot") {
1568 std::string kernel = next_arg(&args);
1569 std::string ramdisk;
1570 if (!args.empty()) ramdisk = next_arg(&args);
1571 std::string second_stage;
1572 if (!args.empty()) second_stage = next_arg(&args);
1573
Elliott Hughes577e8b42018-04-05 16:12:47 -07001574 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001575 fb_queue_download("boot.img", data, sz);
1576 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001577 } else if (command == "flash") {
1578 std::string pname = next_arg(&args);
1579
Elliott Hughes2810d002016-04-25 14:31:18 -07001580 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001581 if (!args.empty()) {
1582 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001583 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001584 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001585 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001586 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001587
1588 auto flash = [&](const std::string &partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001589 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001590 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001591 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001592 } else if (command == "flash:raw") {
1593 std::string partition = next_arg(&args);
1594 std::string kernel = next_arg(&args);
1595 std::string ramdisk;
1596 if (!args.empty()) ramdisk = next_arg(&args);
1597 std::string second_stage;
1598 if (!args.empty()) second_stage = next_arg(&args);
1599
Elliott Hughes577e8b42018-04-05 16:12:47 -07001600 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001601 auto flashraw = [&](const std::string& partition) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001602 fb_queue_flash(partition, data, sz);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001603 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001604 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001605 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001606 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001607 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
Aaron Wisnerdb511202018-06-26 15:38:35 -05001608 do_flashall(slot_override, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001609 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001610 do_flashall(slot_override, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001611 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001612 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001613 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001614 bool slot_all = (slot_override == "all");
1615 if (slot_all) {
1616 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1617 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001618 std::string filename = "update.zip";
1619 if (!args.empty()) {
1620 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001621 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001622 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001623 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001624 } else if (command == "set_active") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001625 std::string slot = verify_slot(next_arg(&args), false);
Elliott Hughes5620d222018-03-28 08:20:00 -07001626 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001627 } else if (command == "stage") {
1628 std::string filename = next_arg(&args);
1629
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001630 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001631 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001632 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001633 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001634 fb_queue_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001635 } else if (command == "get_staged") {
1636 std::string filename = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001637 fb_queue_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001638 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001639 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001640 } else if (command == "flashing") {
1641 if (args.empty()) {
1642 syntax_error("missing 'flashing' command");
1643 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1644 args[0] == "unlock_critical" ||
1645 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001646 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001647 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001648 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001649 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001650 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001651 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001652 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001653 }
1654 }
1655
1656 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001657 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1658 for (const auto& partition : partitions) {
1659 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001660 if (!fb_getvar(std::string{"partition-type:"} + partition, &partition_type)) continue;
Paul Crowley4d170062018-04-24 17:06:30 -07001661 if (partition_type.empty()) continue;
1662 fb_queue_erase(partition);
1663 if (partition == "userdata" && set_fbe_marker) {
1664 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1665 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001666 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001667 delete_fbemarker_tmpdir(initial_userdata_dir);
1668 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001669 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001670 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001671 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001672 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001673 if (wants_set_active) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001674 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001675 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001676 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001677 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001678 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001679 } else if (wants_reboot_bootloader) {
1680 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001681 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001682 }
1683
Aaron Wisnerdb511202018-06-26 15:38:35 -05001684 int status = fb_execute_queue() ? EXIT_FAILURE : EXIT_SUCCESS;
Elliott Hughes5620d222018-03-28 08:20:00 -07001685 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
1686 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001687}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001688
Aaron Wisnerdb511202018-06-26 15:38:35 -05001689void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001690 unsigned year, month, day;
1691 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1692 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1693 }
1694 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1695 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1696 hdr->SetOsPatchLevel(year, month);
1697}
1698
Aaron Wisnerdb511202018-06-26 15:38:35 -05001699void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001700 unsigned major = 0, minor = 0, patch = 0;
1701 std::vector<std::string> versions = android::base::Split(arg, ".");
1702 if (versions.size() < 1 || versions.size() > 3 ||
1703 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1704 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1705 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1706 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1707 syntax_error("bad OS version: %s", arg);
1708 }
1709 hdr->SetOsVersion(major, minor, patch);
1710}