blob: 263ea17801aee5b65899d3d510cde6914fa2380f [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[] = {
Elliott Hughes45964a82017-05-03 22:43:23 -0700117 // clang-format off
118 { "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 },
Elliott Hughes45964a82017-05-03 22:43:23 -0700124 { "recovery", "recovery.img", "recovery.sig", "recovery", true, false },
125 { "system", "system.img", "system.sig", "system", false, false },
126 { nullptr, "system_other.img", "system.sig", "system", true, true },
127 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, false },
128 { "vendor", "vendor.img", "vendor.sig", "vendor", true, false },
129 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, true },
130 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700131};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700132
Elliott Hughes45964a82017-05-03 22:43:23 -0700133static std::string find_item_given_name(const char* img_name) {
134 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700135 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700136 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700137 }
Alex Lightbb9b8a52016-06-29 09:26:44 -0700138 return android::base::StringPrintf("%s/%s", dir, img_name);
139}
140
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700141static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700142 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700143 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700144 return find_item_given_name(images[i].img_name);
145 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 }
147
Elliott Hughesd6365a72017-05-08 18:04:49 -0700148 if (item == "userdata") return find_item_given_name("userdata.img");
149 if (item == "cache") return find_item_given_name("cache.img");
Elliott Hughes45964a82017-05-03 22:43:23 -0700150
Elliott Hughesd6365a72017-05-08 18:04:49 -0700151 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700152 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153}
154
Elliott Hughesfc797672015-04-07 20:12:50 -0700155static int64_t get_file_size(int fd) {
156 struct stat sb;
157 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700158}
159
Elliott Hughesfc797672015-04-07 20:12:50 -0700160static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800161 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700162 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163
Elliott Hughesfc797672015-04-07 20:12:50 -0700164 *sz = get_file_size(fd);
165 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700166 goto oops;
167 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168
Elliott Hughesfc797672015-04-07 20:12:50 -0700169 data = (char*) malloc(*sz);
170 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171
Elliott Hughesfc797672015-04-07 20:12:50 -0700172 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 close(fd);
174
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175 return data;
176
177oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800178 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179 close(fd);
180 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800181 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182 return 0;
183}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184
Elliott Hughes2810d002016-04-25 14:31:18 -0700185static void* load_file(const std::string& path, int64_t* sz) {
186 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700187 if (fd == -1) return nullptr;
188 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700189}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190
Elliott Hughesfc797672015-04-07 20:12:50 -0700191static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700192 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
193 return -1;
194 }
195
Scott Anderson13081c62012-04-06 12:39:30 -0700196 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700198 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
199 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200 return 0;
201}
202
Elliott Hughesfc797672015-04-07 20:12:50 -0700203static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800204 return match_fastboot_with_serial(info, serial);
205}
206
Elliott Hughesfc797672015-04-07 20:12:50 -0700207static int list_devices_callback(usb_ifc_info* info) {
208 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800209 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700210 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800211 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700212 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 if (!serial[0]) {
214 serial = "????????????";
215 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700216 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700217 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800218 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700219 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800220 printf("%-22s fastboot", serial.c_str());
221 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700222 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800223 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224 }
225
226 return -1;
227}
228
David Pursell2ec418a2016-01-20 08:32:08 -0800229// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
230// a specific device, otherwise the first USB device found will be used.
231//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700232// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800233// Otherwise it blocks until the target is available.
234//
235// The returned Transport is a singleton, so multiple calls to this function will return the same
236// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700237static Transport* open_device() {
238 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800239 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240
David Pursell2ec418a2016-01-20 08:32:08 -0800241 if (transport != nullptr) {
242 return transport;
243 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800244
David Pursell4601c972016-02-05 15:35:09 -0800245 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800246 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800247 int port = 0;
248 if (serial != nullptr) {
249 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800250
David Pursell4601c972016-02-05 15:35:09 -0800251 if (android::base::StartsWith(serial, "tcp:")) {
252 protocol = Socket::Protocol::kTcp;
253 port = tcp::kDefaultPort;
254 net_address = serial + strlen("tcp:");
255 } else if (android::base::StartsWith(serial, "udp:")) {
256 protocol = Socket::Protocol::kUdp;
257 port = udp::kDefaultPort;
258 net_address = serial + strlen("udp:");
259 }
260
261 if (net_address != nullptr) {
262 std::string error;
263 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700264 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800265 }
David Pursell2ec418a2016-01-20 08:32:08 -0800266 }
267 }
268
269 while (true) {
270 if (!host.empty()) {
271 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800272 if (protocol == Socket::Protocol::kTcp) {
273 transport = tcp::Connect(host, port, &error).release();
274 } else if (protocol == Socket::Protocol::kUdp) {
275 transport = udp::Connect(host, port, &error).release();
276 }
277
David Pursell2ec418a2016-01-20 08:32:08 -0800278 if (transport == nullptr && announce) {
279 fprintf(stderr, "error: %s\n", error.c_str());
280 }
281 } else {
282 transport = usb_open(match_fastboot);
283 }
284
285 if (transport != nullptr) {
286 return transport;
287 }
288
David Pursell0b156632015-10-30 11:22:01 -0700289 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800290 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700291 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800293 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294 }
295}
296
Elliott Hughesfc797672015-04-07 20:12:50 -0700297static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 // We don't actually open a USB device here,
299 // just getting our callback called so we can
300 // list all the connected devices.
301 usb_open(list_devices_callback);
302}
303
Elliott Hughesd6365a72017-05-08 18:04:49 -0700304static void syntax_error(const char* fmt, ...) {
305 fprintf(stderr, "fastboot: usage: ");
306
307 va_list ap;
308 va_start(ap, fmt);
309 vfprintf(stderr, fmt, ap);
310 va_end(ap);
311
312 fprintf(stderr, "\n");
313 exit(1);
314}
315
316static int show_help() {
317 // clang-format off
318 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700319// 1 2 3 4 5 6 7 8
320// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
Elliott Hughesb76188f2018-04-03 13:30:18 -0700321 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700323 "flashing:\n"
324 " update ZIP Flash all partitions from an update.zip package.\n"
325 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
326 " On A/B devices, flashed slot is set as active.\n"
327 " Secondary images may be flashed to inactive slot.\n"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700328 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
329 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700330 "\n"
331 "basics:\n"
332 " devices [-l] List devices in bootloader (-l: with device paths).\n"
333 " getvar NAME Display given bootloader variable.\n"
334 " reboot [bootloader] Reboot device.\n"
335 "\n"
336 "locking/unlocking:\n"
337 " flashing lock|unlock Lock/unlock partitions for flashing\n"
338 " flashing lock_critical|unlock_critical\n"
339 " Lock/unlock 'critical' bootloader partitions.\n"
340 " flashing get_unlock_ability\n"
341 " Check whether unlocking is allowed (1) or not(0).\n"
342 "\n"
343 "advanced:\n"
344 " erase PARTITION Erase a flash partition.\n"
345 " format[:FS_TYPE[:SIZE]] PARTITION\n"
346 " Format a flash partition.\n"
347 " set_active SLOT Set the active slot.\n"
348 " oem [COMMAND...] Execute OEM-specific command.\n"
349 "\n"
350 "boot image:\n"
351 " boot KERNEL [RAMDISK [SECOND]]\n"
352 " Download and boot kernel from RAM.\n"
353 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
354 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700355 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700356 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
357 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
358 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
359 " --tags-offset Set tags offset (default: 0x00000100).\n"
360 " --page-size BYTES Set flash page size (default: 2048).\n"
361 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700362 " --os-version MAJOR[.MINOR[.PATCH]]\n"
363 " Set boot image OS version (default: 0.0.0).\n"
364 " --os-patch-level YYYY-MM-DD\n"
365 " Set boot image OS security patch level.\n"
366 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700367 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700368 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700369 //" continue Continue with autoboot.\n"
370 //"\n"
371 "Android Things:\n"
372 " stage IN_FILE Sends given file to stage for the next command.\n"
373 " 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 -0800374 "\n"
375 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700376 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700377 " -s SERIAL Specify a USB device.\n"
378 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700379 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700380 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
381 " non-current slot (default: current active slot).\n"
382 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
383 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
384 " --skip-reboot Don't reboot device after flashing.\n"
385 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
386 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000387#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700388 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000389#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700390 // TODO: remove --unbuffered?
391 " --unbuffered Don't buffer input or output.\n"
392 " --verbose, -v Verbose output.\n"
393 " --version Display version.\n"
394 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700396 // clang-format off
397 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000399
Elliott Hughesd6365a72017-05-08 18:04:49 -0700400static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
Elliott Hughes577e8b42018-04-05 16:12:47 -0700401 const std::string& second_stage, int64_t* sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700402 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700403 void* kdata = load_file(kernel.c_str(), &ksize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700404 if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800405
Elliott Hughesfc797672015-04-07 20:12:50 -0700406 // Is this actually a boot image?
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700407 if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800408 die("cannot load '%s': too short", kernel.c_str());
409 }
Elliott Hughesd6365a72017-05-08 18:04:49 -0700410 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700411 if (!g_cmdline.empty()) {
412 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), g_cmdline);
413 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800414
Elliott Hughes4089d342017-10-27 14:21:12 -0700415 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800416
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 *sz = ksize;
418 return kdata;
419 }
420
Elliott Hughesfc797672015-04-07 20:12:50 -0700421 void* rdata = nullptr;
422 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700423 if (!ramdisk.empty()) {
424 rdata = load_file(ramdisk.c_str(), &rsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700425 if (rdata == nullptr) die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 }
427
Elliott Hughesfc797672015-04-07 20:12:50 -0700428 void* sdata = nullptr;
429 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700430 if (!second_stage.empty()) {
431 sdata = load_file(second_stage.c_str(), &ssize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700432 if (sdata == nullptr) die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200433 }
434
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435 fprintf(stderr,"creating boot image...\n");
Elliott Hughes577e8b42018-04-05 16:12:47 -0700436 boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, rdata, rsize, sdata, ssize,
437 g_base_addr, g_boot_img_hdr, sz);
Elliott Hughes4089d342017-10-27 14:21:12 -0700438 if (bdata == nullptr) die("failed to create boot.img");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700439
Elliott Hughes577e8b42018-04-05 16:12:47 -0700440 if (!g_cmdline.empty()) bootimg_set_cmdline(bdata, g_cmdline);
441 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", *sz);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800442
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443 return bdata;
444}
445
Elliott Hughes23af1122017-11-10 08:42:17 -0800446static void* unzip_to_memory(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700447 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700448 ZipEntry zip_entry;
449 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700450 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Elliott Hughes4089d342017-10-27 14:21:12 -0700451 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452 }
453
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700454 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455
Elliott Hughes23af1122017-11-10 08:42:17 -0800456 fprintf(stderr, "extracting %s (%" PRId64 " MB) to RAM...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700457 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughes4089d342017-10-27 14:21:12 -0700458 if (data == nullptr) die("failed to allocate %" PRId64 " bytes for '%s'", *sz, entry_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700460 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
Elliott Hughes4089d342017-10-27 14:21:12 -0700461 if (error != 0) die("failed to extract '%s': %s", entry_name, ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000462
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 return data;
464}
465
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700466#if defined(_WIN32)
467
468// TODO: move this to somewhere it can be shared.
469
470#include <windows.h>
471
472// Windows' tmpfile(3) requires administrator rights because
473// it creates temporary files in the root directory.
474static FILE* win32_tmpfile() {
475 char temp_path[PATH_MAX];
476 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
477 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700478 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700479 }
480
481 char filename[PATH_MAX];
482 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700483 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700484 }
485
486 return fopen(filename, "w+bTD");
487}
488
489#define tmpfile win32_tmpfile
490
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000491static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700492 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000493}
494
Elliott Hughes855cdf82018-04-02 14:24:03 -0700495static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700496 // TODO: reimplement to avoid leaking a FILE*.
497 return fileno(tmpfile());
498}
499
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000500#else
501
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700502static std::string make_temporary_template() {
503 const char* tmpdir = getenv("TMPDIR");
504 if (tmpdir == nullptr) tmpdir = P_tmpdir;
505 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
506}
507
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000508static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700509 std::string result(make_temporary_template());
510 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700511 die("unable to create temporary directory with template %s: %s",
512 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000513 }
514 return result;
515}
516
Elliott Hughes855cdf82018-04-02 14:24:03 -0700517static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700518 std::string path_template(make_temporary_template());
519 int fd = mkstemp(&path_template[0]);
520 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700521 die("failed to create temporary file for %s with template %s: %s\n",
522 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700523 }
524 unlink(path_template.c_str());
525 return fd;
526}
527
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700528#endif
529
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000530static std::string create_fbemarker_tmpdir() {
531 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000532 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
533 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
534 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700535 die("unable to create FBE marker file %s locally: %s",
536 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000537 }
538 close(fd);
539 return dir;
540}
541
542static void delete_fbemarker_tmpdir(const std::string& dir) {
543 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
544 if (unlink(marker_file.c_str()) == -1) {
545 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
546 marker_file.c_str(), errno, strerror(errno));
547 return;
548 }
549 if (rmdir(dir.c_str()) == -1) {
550 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
551 dir.c_str(), errno, strerror(errno));
552 return;
553 }
554}
555
Elliott Hughes45964a82017-05-03 22:43:23 -0700556static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700557 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700558
Yusuke Sato07447542015-06-25 14:39:19 -0700559 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700560 ZipEntry zip_entry;
561 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
562 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000563 return -1;
564 }
565
Elliott Hughes23af1122017-11-10 08:42:17 -0800566 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700567 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800568 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700569 int error = ExtractEntryToFile(zip, &zip_entry, fd);
570 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800571 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700572 }
573
Elliott Hughes4089d342017-10-27 14:21:12 -0700574 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800575 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700576 }
577
Elliott Hughes23af1122017-11-10 08:42:17 -0800578 fprintf(stderr, " took %.3fs\n", now() - start);
579
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700580 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700581}
582
Elliott Hughes5620d222018-03-28 08:20:00 -0700583static char* strip(char* s) {
584 while (*s && isspace(*s)) s++;
585
586 int n = strlen(s);
587 while (n-- > 0) {
588 if (!isspace(s[n])) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800589 s[n] = 0;
590 }
591 return s;
592}
593
594#define MAX_OPTIONS 32
Aaron Wisnerdb511202018-06-26 15:38:35 -0500595static void check_requirement(char* line) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800596 char *val[MAX_OPTIONS];
Elliott Hughes5620d222018-03-28 08:20:00 -0700597 unsigned count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800598 char *x;
599 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800600
Elliott Hughes5620d222018-03-28 08:20:00 -0700601 // "require product=alpha|beta|gamma"
602 // "require version-bootloader=1234"
603 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
604 // "require partition-exists=vendor"
605
606 char* name = line;
607 const char* product = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800608 if (!strncmp(name, "reject ", 7)) {
609 name += 7;
610 invert = 1;
611 } else if (!strncmp(name, "require ", 8)) {
612 name += 8;
613 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700614 } else if (!strncmp(name, "require-for-product:", 20)) {
615 // Get the product and point name past it
Elliott Hughes5620d222018-03-28 08:20:00 -0700616 product = name + 20;
Wink Savilleb98762f2011-04-04 17:54:59 -0700617 name = strchr(name, ' ');
Elliott Hughes5620d222018-03-28 08:20:00 -0700618 if (!name) die("android-info.txt syntax error: %s", line);
Wink Savilleb98762f2011-04-04 17:54:59 -0700619 *name = 0;
620 name += 1;
621 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800622 }
623
624 x = strchr(name, '=');
Elliott Hughes5620d222018-03-28 08:20:00 -0700625 if (x == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800626 *x = 0;
627 val[0] = x + 1;
628
Elliott Hughes5620d222018-03-28 08:20:00 -0700629 name = strip(name);
630
631 // "require partition-exists=x" is a special case, added because of the trouble we had when
632 // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
633 // missing out new partitions. A device with new partitions can use "partition-exists" to
634 // override the `is_optional` field in the `images` array.
635 if (!strcmp(name, "partition-exists")) {
636 const char* partition_name = val[0];
637 std::string has_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500638 if (!fb_getvar(std::string("has-slot:") + partition_name, &has_slot) ||
Elliott Hughes5620d222018-03-28 08:20:00 -0700639 (has_slot != "yes" && has_slot != "no")) {
640 die("device doesn't have required partition %s!", partition_name);
641 }
642 bool known_partition = false;
643 for (size_t i = 0; i < arraysize(images); ++i) {
644 if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
645 images[i].is_optional = false;
646 known_partition = true;
647 }
648 }
649 if (!known_partition) {
650 die("device requires partition %s which is not known to this version of fastboot",
651 partition_name);
652 }
653 return;
654 }
655
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800656 for(count = 1; count < MAX_OPTIONS; count++) {
657 x = strchr(val[count - 1],'|');
658 if (x == 0) break;
659 *x = 0;
660 val[count] = x + 1;
661 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800662
Elliott Hughes253c18d2015-03-18 22:47:09 -0700663 // Work around an unfortunate name mismatch.
Elliott Hughes5620d222018-03-28 08:20:00 -0700664 const char* var = name;
665 if (!strcmp(name, "board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800666
Elliott Hughes253c18d2015-03-18 22:47:09 -0700667 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
Elliott Hughes5620d222018-03-28 08:20:00 -0700668 if (out == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800669
Elliott Hughes5620d222018-03-28 08:20:00 -0700670 for (size_t i = 0; i < count; ++i) {
671 out[i] = xstrdup(strip(val[i]));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800672 }
673
Elliott Hughes5620d222018-03-28 08:20:00 -0700674 fb_queue_require(product, var, invert, count, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800675}
676
Aaron Wisnerdb511202018-06-26 15:38:35 -0500677static void check_requirements(char* data, int64_t sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700678 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800679 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700680 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800681 *s++ = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500682 check_requirement(data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800683 data = s;
684 } else {
685 s++;
686 }
687 }
Aaron Wisnerdb511202018-06-26 15:38:35 -0500688 if (fb_execute_queue()) die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800689}
690
Elliott Hughesfc797672015-04-07 20:12:50 -0700691static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692 fb_queue_notice("--------------------------------------------");
Elliott Hughes5620d222018-03-28 08:20:00 -0700693 fb_queue_display("Bootloader Version...", "version-bootloader");
694 fb_queue_display("Baseband Version.....", "version-baseband");
695 fb_queue_display("Serial Number........", "serialno");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800696 fb_queue_notice("--------------------------------------------");
697}
698
Tao Bao41cf35f2018-04-24 10:54:21 -0700699static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700700 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700701 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700702
Tao Bao41cf35f2018-04-24 10:54:21 -0700703 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
704 die("invalid max size %" PRId64, max_size);
705 }
706
Elliott Hughesfc797672015-04-07 20:12:50 -0700707 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700708 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700709
Elliott Hughes253c18d2015-03-18 22:47:09 -0700710 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700711 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700712
713 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700714 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700715
716 return out_s;
717}
718
Aaron Wisnerdb511202018-06-26 15:38:35 -0500719static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700720 std::string max_download_size;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500721 if (!fb_getvar("max-download-size", &max_download_size) ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700722 max_download_size.empty()) {
723 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700724 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700725 }
726
Elliott Hughes77c0e662015-11-02 15:51:12 -0800727 // Some bootloaders (angler, for example) send spurious whitespace too.
728 max_download_size = android::base::Trim(max_download_size);
729
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700730 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700731 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800732 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700733 return 0;
734 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700735 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700736 return limit;
737}
738
Aaron Wisnerdb511202018-06-26 15:38:35 -0500739static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700740 int64_t limit = sparse_limit;
741 if (limit == 0) {
742 // Unlimited, so see what the target device's limit is.
743 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700744 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500745 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700746 }
747 if (target_sparse_limit > 0) {
748 limit = target_sparse_limit;
749 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700750 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700751 }
752 }
753
754 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500755 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700756 }
757
758 return 0;
759}
760
Aaron Wisnerdb511202018-06-26 15:38:35 -0500761static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700762 int64_t sz = get_file_size(fd);
763 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700764 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700765 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700766
Elliott Hughesfc797672015-04-07 20:12:50 -0700767 lseek64(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500768 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700769 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700770 sparse_file** s = load_sparse_files(fd, limit);
771 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700772 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700773 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700774 buf->type = FB_BUFFER_SPARSE;
775 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700776 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500777 buf->type = FB_BUFFER_FD;
778 buf->data = nullptr;
779 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000780 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700781 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700782
Elliott Hughes53ec4952016-05-11 12:39:27 -0700783 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700784}
785
Aaron Wisnerdb511202018-06-26 15:38:35 -0500786static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500787 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
788
Elliott Hughes53ec4952016-05-11 12:39:27 -0700789 if (fd == -1) {
790 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700791 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500792
793 struct stat s;
794 if (fstat(fd, &s)) {
795 return false;
796 }
797 if (!S_ISREG(s.st_mode)) {
798 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
799 return false;
800 }
801
Aaron Wisnerdb511202018-06-26 15:38:35 -0500802 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700803}
804
David Zeuthenb6ea4352017-08-07 14:29:26 -0400805static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
806 // Buffer needs to be at least the size of the VBMeta struct which
807 // is 256 bytes.
808 if (buf->sz < 256) {
809 return;
810 }
811
Elliott Hughes855cdf82018-04-02 14:24:03 -0700812 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400813
814 std::string data;
815 if (!android::base::ReadFdToString(buf->fd, &data)) {
816 die("Failed reading from vbmeta");
817 }
818
819 // There's a 32-bit big endian |flags| field at offset 120 where
820 // bit 0 corresponds to disable-verity and bit 1 corresponds to
821 // disable-verification.
822 //
823 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
824 // the VBMeta struct.
825 if (g_disable_verity) {
826 data[123] |= 0x01;
827 }
828 if (g_disable_verification) {
829 data[123] |= 0x02;
830 }
831
832 if (!android::base::WriteStringToFd(data, fd)) {
833 die("Failed writing to modified vbmeta");
834 }
835 close(buf->fd);
836 buf->fd = fd;
837 lseek(fd, 0, SEEK_SET);
838}
839
Elliott Hughes5620d222018-03-28 08:20:00 -0700840static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700841{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700842 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700843
David Zeuthenb6ea4352017-08-07 14:29:26 -0400844 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
845 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700846 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400847 rewrite_vbmeta_buffer(buf);
848 }
849
Rom Lemarchand622810c2013-06-28 09:54:59 -0700850 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800851 case FB_BUFFER_SPARSE: {
852 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700853 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700854 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700855 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800856 sparse_files.emplace_back(*s, sz);
857 ++s;
858 }
859
860 for (size_t i = 0; i < sparse_files.size(); ++i) {
861 const auto& pair = sparse_files[i];
Elliott Hughes5620d222018-03-28 08:20:00 -0700862 fb_queue_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700863 }
864 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800865 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500866 case FB_BUFFER_FD:
Elliott Hughes5620d222018-03-28 08:20:00 -0700867 fb_queue_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700868 break;
869 default:
870 die("unknown buffer type: %d", buf->type);
871 }
872}
873
Aaron Wisnerdb511202018-06-26 15:38:35 -0500874static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700875 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500876 if (!fb_getvar("current-slot", &current_slot)) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700877 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700878}
879
Aaron Wisnerdb511202018-06-26 15:38:35 -0500880static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700881 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700882 int count = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500883 if (!fb_getvar("slot-count", &var) || !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700884 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700885 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700886 return count;
887}
888
Aaron Wisnerdb511202018-06-26 15:38:35 -0500889static bool supports_AB() {
890 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700891}
892
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700893// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700894static std::string get_other_slot(const std::string& current_slot, int count) {
895 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700896
Daniel Rosenberg80919472016-06-30 19:25:31 -0700897 char next = (current_slot[0] - 'a' + 1)%count + 'a';
898 return std::string(1, next);
899}
900
Aaron Wisnerdb511202018-06-26 15:38:35 -0500901static std::string get_other_slot(const std::string& current_slot) {
902 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -0700903}
904
Aaron Wisnerdb511202018-06-26 15:38:35 -0500905static std::string get_other_slot(int count) {
906 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700907}
908
Aaron Wisnerdb511202018-06-26 15:38:35 -0500909static std::string get_other_slot() {
910 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -0700911}
912
Aaron Wisnerdb511202018-06-26 15:38:35 -0500913static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700914 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700915 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800916 if (allow_all) {
917 return "all";
918 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500919 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700920 if (count > 0) {
921 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800922 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -0700923 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800924 }
925 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700926 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800927
Aaron Wisnerdb511202018-06-26 15:38:35 -0500928 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -0700929 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800930
Daniel Rosenberg80919472016-06-30 19:25:31 -0700931 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500932 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700933 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700934 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800935 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700936 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800937 }
938
Daniel Rosenberg80919472016-06-30 19:25:31 -0700939 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
940
941 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
942 for (int i=0; i<count; i++) {
943 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700944 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700945
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700946 exit(1);
947}
948
Aaron Wisnerdb511202018-06-26 15:38:35 -0500949static std::string verify_slot(const std::string& slot) {
950 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800951}
952
Aaron Wisnerdb511202018-06-26 15:38:35 -0500953static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700954 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800955 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700956 std::string current_slot;
957
Aaron Wisnerdb511202018-06-26 15:38:35 -0500958 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800959 /* If has-slot is not supported, the answer is no. */
960 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700961 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800962 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700963 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500964 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700965 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700966 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700967 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700968 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700969 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700970 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700971 }
972 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700973 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -0700974 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -0700975 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700976 }
977 func(part);
978 }
979}
980
David Pursell0b156632015-10-30 11:22:01 -0700981/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
982 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
983 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
984 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700985 */
Aaron Wisnerdb511202018-06-26 15:38:35 -0500986static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700987 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800988 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700989
Daniel Rosenberg80919472016-06-30 19:25:31 -0700990 if (slot == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500991 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700992 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700993 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800994 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500995 for (int i=0; i < get_slot_count(); i++) {
996 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700997 }
998 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500999 do_for_partition(part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001000 }
1001 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001002 do_for_partition(part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001003 }
1004}
1005
Aaron Wisnerdb511202018-06-26 15:38:35 -05001006static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001007 struct fastboot_buffer buf;
1008
Aaron Wisnerdb511202018-06-26 15:38:35 -05001009 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001010 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001011 }
1012 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001013}
1014
Elliott Hughes45964a82017-05-03 22:43:23 -07001015static void do_update_signature(ZipArchiveHandle zip, const char* filename) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001016 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001017 void* data = unzip_to_memory(zip, filename, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001018 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001019 fb_queue_download("signature", data, sz);
1020 fb_queue_command("signature", "installing signature");
1021}
1022
Daniel Rosenberg13454092016-06-27 19:43:11 -07001023// Sets slot_override as the active slot. If slot_override is blank,
1024// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001025static void set_active(const std::string& slot_override) {
1026 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001027
Daniel Rosenberg80919472016-06-30 19:25:31 -07001028 if (slot_override != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001029 fb_set_active(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001030 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001031 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001032 if (current_slot != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001033 fb_set_active(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001034 }
1035 }
1036}
1037
Aaron Wisnerdb511202018-06-26 15:38:35 -05001038static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001039 queue_info_dump();
1040
Wink Savilleb98762f2011-04-04 17:54:59 -07001041 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1042
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001043 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001044 int error = OpenArchive(filename, &zip);
1045 if (error != 0) {
1046 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001047 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001048
Elliott Hughesfc797672015-04-07 20:12:50 -07001049 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001050 void* data = unzip_to_memory(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001051 if (data == nullptr) {
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001052 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001053 }
1054
Aaron Wisnerdb511202018-06-26 15:38:35 -05001055 check_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001056
Alex Lightbb9b8a52016-06-29 09:26:44 -07001057 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001058 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001059 if (slot_override != "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001060 secondary = get_other_slot(slot_override);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001061 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001062 secondary = get_other_slot();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001063 }
1064 if (secondary == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001065 if (supports_AB()) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001066 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1067 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001068 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001069 }
1070 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001071 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001072 const char* slot = slot_override.c_str();
1073 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001074 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001075 slot = secondary.c_str();
1076 } else {
1077 continue;
1078 }
1079 }
1080
Elliott Hughes253c18d2015-03-18 22:47:09 -07001081 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001082 if (fd == -1) {
1083 if (images[i].is_optional) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001084 continue; // An optional file is missing, so ignore it.
Elliott Hughesacdbe922015-06-02 21:37:05 -07001085 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001086 die("non-optional file %s missing", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001087 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001088
Elliott Hughes253c18d2015-03-18 22:47:09 -07001089 fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001090 if (!load_buf_fd(fd, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001091 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1092 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001093
Elliott Hughes4089d342017-10-27 14:21:12 -07001094 auto update = [&](const std::string& partition) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001095 do_update_signature(zip, images[i].sig_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001096 flash_buf(partition.c_str(), &buf);
1097 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001098 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001099 * program exits.
1100 */
1101 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001102 do_for_partitions(images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001103 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001104
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001105 if (slot_override == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001106 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001107 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001108 set_active(slot_override);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001109 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001110
1111 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001112}
1113
Alex Lightbb9b8a52016-06-29 09:26:44 -07001114static void do_send_signature(const std::string& fn) {
1115 std::size_t extension_loc = fn.find(".img");
1116 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001117
Alex Lightbb9b8a52016-06-29 09:26:44 -07001118 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001119
1120 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001121 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001122 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001123
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001124 fb_queue_download("signature", data, sz);
1125 fb_queue_command("signature", "installing signature");
1126}
1127
Aaron Wisnerdb511202018-06-26 15:38:35 -05001128static void do_flashall(const std::string& slot_override, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001129 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001130 queue_info_dump();
1131
Wink Savilleb98762f2011-04-04 17:54:59 -07001132 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1133
Elliott Hughes45964a82017-05-03 22:43:23 -07001134 fname = find_item_given_name("android-info.txt");
Elliott Hughes2810d002016-04-25 14:31:18 -07001135 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001136
Elliott Hughesfc797672015-04-07 20:12:50 -07001137 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001138 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001139 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001140
Aaron Wisnerdb511202018-06-26 15:38:35 -05001141 check_requirements(reinterpret_cast<char*>(data), sz);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001142
Alex Lightbb9b8a52016-06-29 09:26:44 -07001143 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001144 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001145 if (slot_override != "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001146 secondary = get_other_slot(slot_override);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001147 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001148 secondary = get_other_slot();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001149 }
1150 if (secondary == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001151 if (supports_AB()) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001152 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1153 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001154 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001155 }
1156 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001157
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001158 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001159 const char* slot = NULL;
1160 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001161 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001162 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001163 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001164 }
1165 if (!slot) continue;
Elliott Hughes45964a82017-05-03 22:43:23 -07001166 fname = find_item_given_name(images[i].img_name);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001167 fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001168 if (!load_buf(fname.c_str(), &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001169 if (images[i].is_optional) continue;
Elliott Hughes4089d342017-10-27 14:21:12 -07001170 die("could not load '%s': %s", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001171 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001172
1173 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001174 do_send_signature(fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001175 flash_buf(partition.c_str(), &buf);
1176 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001177 do_for_partitions(images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001178 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001179
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001180 if (slot_override == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001181 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001182 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001183 set_active(slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001184 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001185}
1186
Elliott Hughesd6365a72017-05-08 18:04:49 -07001187static std::string next_arg(std::vector<std::string>* args) {
1188 if (args->empty()) syntax_error("expected argument");
1189 std::string result = args->front();
1190 args->erase(args->begin());
1191 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001192}
1193
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001194static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001195 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001196
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001197 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001198 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001199 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001200 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001201 fb_queue_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001202}
1203
Connor O'Brience16a8a2017-02-06 14:39:31 -08001204static std::string fb_fix_numeric_var(std::string var) {
1205 // Some bootloaders (angler, for example), send spurious leading whitespace.
1206 var = android::base::Trim(var);
1207 // Some bootloaders (hammerhead, for example) use implicit hex.
1208 // This code used to use strtol with base 16.
1209 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1210 return var;
1211}
1212
Aaron Wisnerdb511202018-06-26 15:38:35 -05001213static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001214 std::string sizeString;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001215 if (!fb_getvar(name, &sizeString) || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001216 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001217 return 0;
1218 }
1219 sizeString = fb_fix_numeric_var(sizeString);
1220
1221 unsigned size;
1222 if (!android::base::ParseUint(sizeString, &size)) {
1223 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1224 return 0;
1225 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001226 if ((size & (size - 1)) != 0) {
1227 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001228 return 0;
1229 }
1230 return size;
1231}
1232
Aaron Wisnerdb511202018-06-26 15:38:35 -05001233static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001234 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001235 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001236 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001237 std::string partition_type, partition_size;
1238
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001239 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001240 const char* errMsg = nullptr;
1241 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001242 TemporaryFile output;
1243 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001244
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001245 unsigned int limit = INT_MAX;
1246 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001247 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001248 }
1249 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001250 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001251 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001252
Aaron Wisnerdb511202018-06-26 15:38:35 -05001253 if (!fb_getvar("partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001254 errMsg = "Can't determine partition type.\n";
1255 goto failed;
1256 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001257 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001258 if (partition_type != type_override) {
1259 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001260 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001261 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001262 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001263 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001264
Aaron Wisnerdb511202018-06-26 15:38:35 -05001265 if (!fb_getvar("partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001266 errMsg = "Unable to get partition size\n";
1267 goto failed;
1268 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001269 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001270 if (partition_size != size_override) {
1271 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001272 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001273 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001274 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001275 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001276 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001277
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001278 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001279 if (!gen) {
1280 if (skip_if_not_supported) {
1281 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001282 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001283 return;
1284 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001285 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1286 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001287 return;
1288 }
1289
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001290 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001291 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001292 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1293 return;
1294 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001295
Connor O'Brience16a8a2017-02-06 14:39:31 -08001296 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001297 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1298 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001299
Jin Qian4a335822017-04-18 16:23:18 -07001300 if (fs_generator_generate(gen, output.path, size, initial_dir,
1301 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001302 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001303 return;
1304 }
1305
Jin Qian4a335822017-04-18 16:23:18 -07001306 fd.reset(open(output.path, O_RDONLY));
1307 if (fd == -1) {
1308 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1309 return;
1310 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001311 if (!load_buf_fd(fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001312 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001313 return;
1314 }
1315 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001316 return;
1317
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001318failed:
1319 if (skip_if_not_supported) {
1320 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001321 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001322 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001323 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001324}
1325
Aaron Wisnerdb511202018-06-26 15:38:35 -05001326int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001327 bool wants_wipe = false;
1328 bool wants_reboot = false;
1329 bool wants_reboot_bootloader = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001330 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001331 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001332 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001333 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001334 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001335 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001336 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001337 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001338 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001339
Elliott Hughes577e8b42018-04-05 16:12:47 -07001340 g_boot_img_hdr.kernel_addr = 0x00008000;
1341 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1342 g_boot_img_hdr.second_addr = 0x00f00000;
1343 g_boot_img_hdr.tags_addr = 0x00000100;
1344 g_boot_img_hdr.page_size = 2048;
1345
JP Abgrall7b8970c2013-03-07 17:06:41 -08001346 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001347 {"base", required_argument, 0, 0},
1348 {"cmdline", required_argument, 0, 0},
1349 {"disable-verification", no_argument, 0, 0},
1350 {"disable-verity", no_argument, 0, 0},
1351 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001352 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001353 {"kernel-offset", required_argument, 0, 0},
1354 {"os-patch-level", required_argument, 0, 0},
1355 {"os-version", required_argument, 0, 0},
1356 {"page-size", required_argument, 0, 0},
1357 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001358 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001359 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001360 {"skip-secondary", no_argument, 0, 0},
1361 {"slot", required_argument, 0, 0},
1362 {"tags-offset", required_argument, 0, 0},
1363 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001364 {"verbose", no_argument, 0, 'v'},
1365 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001366#if !defined(_WIN32)
1367 {"wipe-and-use-fbe", no_argument, 0, 0},
1368#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001369 {0, 0, 0, 0}
1370 };
Colin Cross8879f982012-05-22 17:53:34 -07001371
1372 serial = getenv("ANDROID_SERIAL");
1373
Elliott Hughes577e8b42018-04-05 16:12:47 -07001374 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001375 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001376 if (c == 0) {
1377 std::string name{longopts[longindex].name};
1378 if (name == "base") {
1379 g_base_addr = strtoul(optarg, 0, 16);
1380 } else if (name == "cmdline") {
1381 g_cmdline = optarg;
1382 } else if (name == "disable-verification") {
1383 g_disable_verification = true;
1384 } else if (name == "disable-verity") {
1385 g_disable_verity = true;
1386 } else if (name == "header-version") {
1387 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1388 } else if (name == "kernel-offset") {
1389 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1390 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001391 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001392 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001393 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001394 } else if (name == "page-size") {
1395 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1396 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1397 } else if (name == "ramdisk-offset") {
1398 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1399 } else if (name == "skip-reboot") {
1400 skip_reboot = true;
1401 } else if (name == "skip-secondary") {
1402 skip_secondary = true;
1403 } else if (name == "slot") {
1404 slot_override = optarg;
1405 } else if (name == "tags-offset") {
1406 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1407 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001408 setvbuf(stdout, nullptr, _IONBF, 0);
1409 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001410 } else if (name == "version") {
Elliott Hughes4d4f64f2017-05-08 11:30:34 -07001411 fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001412 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001413 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001414#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001415 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001416 wants_wipe = true;
1417 set_fbe_marker = true;
1418#endif
1419 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001420 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001421 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001422 } else {
1423 switch (c) {
1424 case 'a':
1425 wants_set_active = true;
1426 if (optarg) next_active = optarg;
1427 break;
1428 case 'h':
1429 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001430 case 'l':
1431 g_long_listing = true;
1432 break;
1433 case 's':
1434 serial = optarg;
1435 break;
1436 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001437 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1438 die("invalid sparse limit %s", optarg);
1439 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001440 break;
1441 case 'v':
1442 set_verbose();
1443 break;
1444 case 'w':
1445 wants_wipe = true;
1446 break;
1447 case '?':
1448 return 1;
1449 default:
1450 abort();
1451 }
Colin Cross8879f982012-05-22 17:53:34 -07001452 }
1453 }
1454
1455 argc -= optind;
1456 argv += optind;
1457
Elliott Hughesd6365a72017-05-08 18:04:49 -07001458 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001459
Colin Cross8fb6e062012-07-24 16:36:41 -07001460 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001461 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001462 return 0;
1463 }
1464
Colin Crossc7b75dc2012-08-29 18:17:06 -07001465 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001466 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001467 }
1468
David Pursell0b156632015-10-30 11:22:01 -07001469 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001470 if (transport == nullptr) {
1471 return 1;
1472 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001473 fastboot::FastBootDriver fb(transport);
1474 fb_init(fb);
David Pursell2ec418a2016-01-20 08:32:08 -08001475
Elliott Hughes5620d222018-03-28 08:20:00 -07001476 const double start = now();
1477
Aaron Wisnerdb511202018-06-26 15:38:35 -05001478 if (slot_override != "") slot_override = verify_slot(slot_override);
1479 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001480
1481 if (wants_set_active) {
1482 if (next_active == "") {
1483 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001484 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001485 if (fb_getvar("current-slot", &current_slot)) {
1486 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001487 } else {
1488 wants_set_active = false;
1489 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001490 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001491 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001492 }
1493 }
1494 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001495
Elliott Hughesd6365a72017-05-08 18:04:49 -07001496 std::vector<std::string> args(argv, argv + argc);
1497 while (!args.empty()) {
1498 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001499
Elliott Hughesd6365a72017-05-08 18:04:49 -07001500 if (command == "getvar") {
1501 std::string variable = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001502 fb_queue_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001503 } else if (command == "erase") {
1504 std::string partition = next_arg(&args);
1505 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001506 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001507 if (fb_getvar(std::string("partition-type:") + partition,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001508 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001509 fs_get_generator(partition_type) != nullptr) {
1510 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1511 partition_type.c_str());
1512 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001513
Elliott Hughes5620d222018-03-28 08:20:00 -07001514 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001515 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001516 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001517 } else if (android::base::StartsWith(command, "format")) {
1518 // Parsing for: "format[:[type][:[size]]]"
1519 // Some valid things:
1520 // - select only the size, and leave default fs type:
1521 // format::0x4000000 userdata
1522 // - default fs type and size:
1523 // format userdata
1524 // format:: userdata
1525 std::vector<std::string> pieces = android::base::Split(command, ":");
1526 std::string type_override;
1527 if (pieces.size() > 1) type_override = pieces[1].c_str();
1528 std::string size_override;
1529 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001530
Elliott Hughesd6365a72017-05-08 18:04:49 -07001531 std::string partition = next_arg(&args);
1532
1533 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001534 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001535 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001536 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001537 } else if (command == "signature") {
1538 std::string filename = next_arg(&args);
1539 data = load_file(filename.c_str(), &sz);
1540 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -07001541 if (sz != 256) die("signature must be 256 bytes (got %" PRId64 ")", sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001542 fb_queue_download("signature", data, sz);
1543 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001544 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001545 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001546
1547 if (args.size() == 1) {
1548 std::string what = next_arg(&args);
1549 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001550 wants_reboot = false;
1551 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001552 } else {
1553 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001554 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001555
Elliott Hughesca85df02015-02-25 10:02:00 -08001556 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001557 if (!args.empty()) syntax_error("junk after reboot command");
1558 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001559 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001560 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001561 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001562 } else if (command == "boot") {
1563 std::string kernel = next_arg(&args);
1564 std::string ramdisk;
1565 if (!args.empty()) ramdisk = next_arg(&args);
1566 std::string second_stage;
1567 if (!args.empty()) second_stage = next_arg(&args);
1568
Elliott Hughes577e8b42018-04-05 16:12:47 -07001569 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001570 fb_queue_download("boot.img", data, sz);
1571 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001572 } else if (command == "flash") {
1573 std::string pname = next_arg(&args);
1574
Elliott Hughes2810d002016-04-25 14:31:18 -07001575 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001576 if (!args.empty()) {
1577 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001578 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001579 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001580 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001581 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001582
1583 auto flash = [&](const std::string &partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001584 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001585 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001586 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001587 } else if (command == "flash:raw") {
1588 std::string partition = next_arg(&args);
1589 std::string kernel = next_arg(&args);
1590 std::string ramdisk;
1591 if (!args.empty()) ramdisk = next_arg(&args);
1592 std::string second_stage;
1593 if (!args.empty()) second_stage = next_arg(&args);
1594
Elliott Hughes577e8b42018-04-05 16:12:47 -07001595 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001596 auto flashraw = [&](const std::string& partition) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001597 fb_queue_flash(partition, data, sz);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001598 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001599 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001600 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001601 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001602 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
Aaron Wisnerdb511202018-06-26 15:38:35 -05001603 do_flashall(slot_override, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001604 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001605 do_flashall(slot_override, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001606 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001607 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001608 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001609 bool slot_all = (slot_override == "all");
1610 if (slot_all) {
1611 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1612 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001613 std::string filename = "update.zip";
1614 if (!args.empty()) {
1615 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001616 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001617 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001618 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001619 } else if (command == "set_active") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001620 std::string slot = verify_slot(next_arg(&args), false);
Elliott Hughes5620d222018-03-28 08:20:00 -07001621 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001622 } else if (command == "stage") {
1623 std::string filename = next_arg(&args);
1624
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001625 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001626 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001627 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001628 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001629 fb_queue_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001630 } else if (command == "get_staged") {
1631 std::string filename = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001632 fb_queue_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001633 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001634 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001635 } else if (command == "flashing") {
1636 if (args.empty()) {
1637 syntax_error("missing 'flashing' command");
1638 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1639 args[0] == "unlock_critical" ||
1640 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001641 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001642 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001643 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001644 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001645 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001646 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001647 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001648 }
1649 }
1650
1651 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001652 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1653 for (const auto& partition : partitions) {
1654 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001655 if (!fb_getvar(std::string{"partition-type:"} + partition, &partition_type)) continue;
Paul Crowley4d170062018-04-24 17:06:30 -07001656 if (partition_type.empty()) continue;
1657 fb_queue_erase(partition);
1658 if (partition == "userdata" && set_fbe_marker) {
1659 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1660 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001661 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001662 delete_fbemarker_tmpdir(initial_userdata_dir);
1663 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001664 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001665 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001666 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001667 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001668 if (wants_set_active) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001669 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001670 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001671 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001672 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001673 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001674 } else if (wants_reboot_bootloader) {
1675 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001676 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001677 }
1678
Aaron Wisnerdb511202018-06-26 15:38:35 -05001679 int status = fb_execute_queue() ? EXIT_FAILURE : EXIT_SUCCESS;
Elliott Hughes5620d222018-03-28 08:20:00 -07001680 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
1681 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001682}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001683
Aaron Wisnerdb511202018-06-26 15:38:35 -05001684void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001685 unsigned year, month, day;
1686 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1687 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1688 }
1689 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1690 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1691 hdr->SetOsPatchLevel(year, month);
1692}
1693
Aaron Wisnerdb511202018-06-26 15:38:35 -05001694void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001695 unsigned major = 0, minor = 0, patch = 0;
1696 std::vector<std::string> versions = android::base::Split(arg, ".");
1697 if (versions.size() < 1 || versions.size() > 3 ||
1698 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1699 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1700 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1701 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1702 syntax_error("bad OS version: %s", arg);
1703 }
1704 hdr->SetOsVersion(major, minor, patch);
1705}