blob: 780ff50db0141551c04d62b98d8eaf3f4517516d [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070029#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <errno.h>
31#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070032#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070033#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070034#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070035#include <stdint.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070041#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070042#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080043
Elliott Hughes290a2282016-11-14 17:08:47 -080044#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070045#include <functional>
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080047#include <utility>
48#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070049
Elliott Hughes82ff3152016-08-31 15:07:18 -070050#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070051#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080053#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070054#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080055#include <android-base/strings.h>
Jin Qian4a335822017-04-18 16:23:18 -070056#include <android-base/test_utils.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050057#include <android-base/unique_fd.h>
Colin Crossf8387882012-05-24 17:18:41 -070058#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070059#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Elliott Hughes253c18d2015-03-18 22:47:09 -070061#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080062#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070064#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080065#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070066#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080067#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070068#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Chris Fries0ea946c2017-04-12 10:25:57 -050070using android::base::unique_fd;
71
Colin Crossf8387882012-05-24 17:18:41 -070072#ifndef O_BINARY
73#define O_BINARY 0
74#endif
75
Wink Savilleb98762f2011-04-04 17:54:59 -070076char cur_product[FB_RESPONSE_SZ + 1];
77
David Pursell2ec418a2016-01-20 08:32:08 -080078static const char* serial = nullptr;
Elliott Hughes577e8b42018-04-05 16:12:47 -070079
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,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
Elliott Hughesb76188f2018-04-03 13:30:18 -0700320 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700322 "flashing:\n"
323 " update ZIP Flash all partitions from an update.zip package.\n"
324 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
325 " On A/B devices, flashed slot is set as active.\n"
326 " Secondary images may be flashed to inactive slot.\n"
327 " flash PARTITION [FILENAME]\n"
328 " Flash given partition only.\n"
329 "\n"
330 "basics:\n"
331 " devices [-l] List devices in bootloader (-l: with device paths).\n"
332 " getvar NAME Display given bootloader variable.\n"
333 " reboot [bootloader] Reboot device.\n"
334 "\n"
335 "locking/unlocking:\n"
336 " flashing lock|unlock Lock/unlock partitions for flashing\n"
337 " flashing lock_critical|unlock_critical\n"
338 " Lock/unlock 'critical' bootloader partitions.\n"
339 " flashing get_unlock_ability\n"
340 " Check whether unlocking is allowed (1) or not(0).\n"
341 "\n"
342 "advanced:\n"
343 " erase PARTITION Erase a flash partition.\n"
344 " format[:FS_TYPE[:SIZE]] PARTITION\n"
345 " Format a flash partition.\n"
346 " set_active SLOT Set the active slot.\n"
347 " oem [COMMAND...] Execute OEM-specific command.\n"
348 "\n"
349 "boot image:\n"
350 " boot KERNEL [RAMDISK [SECOND]]\n"
351 " Download and boot kernel from RAM.\n"
352 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
353 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700354 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700355 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
356 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
357 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
358 " --tags-offset Set tags offset (default: 0x00000100).\n"
359 " --page-size BYTES Set flash page size (default: 2048).\n"
360 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700361 " --os-version MAJOR[.MINOR[.PATCH]]\n"
362 " Set boot image OS version (default: 0.0.0).\n"
363 " --os-patch-level YYYY-MM-DD\n"
364 " Set boot image OS security patch level.\n"
365 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700366 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700367 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700368 //" continue Continue with autoboot.\n"
369 //"\n"
370 "Android Things:\n"
371 " stage IN_FILE Sends given file to stage for the next command.\n"
372 " 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 -0800373 "\n"
374 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700375 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700376 " -s SERIAL Specify a USB device.\n"
377 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700378 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700379 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
380 " non-current slot (default: current active slot).\n"
381 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
382 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
383 " --skip-reboot Don't reboot device after flashing.\n"
384 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
385 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000386#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700387 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000388#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700389 // TODO: remove --unbuffered?
390 " --unbuffered Don't buffer input or output.\n"
391 " --verbose, -v Verbose output.\n"
392 " --version Display version.\n"
393 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700395 // clang-format off
396 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000398
Elliott Hughesd6365a72017-05-08 18:04:49 -0700399static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
Elliott Hughes577e8b42018-04-05 16:12:47 -0700400 const std::string& second_stage, int64_t* sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700401 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700402 void* kdata = load_file(kernel.c_str(), &ksize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700403 if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800404
Elliott Hughesfc797672015-04-07 20:12:50 -0700405 // Is this actually a boot image?
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700406 if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800407 die("cannot load '%s': too short", kernel.c_str());
408 }
Elliott Hughesd6365a72017-05-08 18:04:49 -0700409 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700410 if (!g_cmdline.empty()) {
411 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), g_cmdline);
412 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800413
Elliott Hughes4089d342017-10-27 14:21:12 -0700414 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800415
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416 *sz = ksize;
417 return kdata;
418 }
419
Elliott Hughesfc797672015-04-07 20:12:50 -0700420 void* rdata = nullptr;
421 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700422 if (!ramdisk.empty()) {
423 rdata = load_file(ramdisk.c_str(), &rsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700424 if (rdata == nullptr) die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425 }
426
Elliott Hughesfc797672015-04-07 20:12:50 -0700427 void* sdata = nullptr;
428 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700429 if (!second_stage.empty()) {
430 sdata = load_file(second_stage.c_str(), &ssize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700431 if (sdata == nullptr) die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200432 }
433
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800434 fprintf(stderr,"creating boot image...\n");
Elliott Hughes577e8b42018-04-05 16:12:47 -0700435 boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, rdata, rsize, sdata, ssize,
436 g_base_addr, g_boot_img_hdr, sz);
Elliott Hughes4089d342017-10-27 14:21:12 -0700437 if (bdata == nullptr) die("failed to create boot.img");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700438
Elliott Hughes577e8b42018-04-05 16:12:47 -0700439 if (!g_cmdline.empty()) bootimg_set_cmdline(bdata, g_cmdline);
440 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", *sz);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800441
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442 return bdata;
443}
444
Elliott Hughes23af1122017-11-10 08:42:17 -0800445static void* unzip_to_memory(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700446 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700447 ZipEntry zip_entry;
448 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700449 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Elliott Hughes4089d342017-10-27 14:21:12 -0700450 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451 }
452
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700453 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454
Elliott Hughes23af1122017-11-10 08:42:17 -0800455 fprintf(stderr, "extracting %s (%" PRId64 " MB) to RAM...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700456 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughes4089d342017-10-27 14:21:12 -0700457 if (data == nullptr) die("failed to allocate %" PRId64 " bytes for '%s'", *sz, entry_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700459 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
Elliott Hughes4089d342017-10-27 14:21:12 -0700460 if (error != 0) die("failed to extract '%s': %s", entry_name, ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000461
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 return data;
463}
464
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700465#if defined(_WIN32)
466
467// TODO: move this to somewhere it can be shared.
468
469#include <windows.h>
470
471// Windows' tmpfile(3) requires administrator rights because
472// it creates temporary files in the root directory.
473static FILE* win32_tmpfile() {
474 char temp_path[PATH_MAX];
475 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
476 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700477 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700478 }
479
480 char filename[PATH_MAX];
481 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700482 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700483 }
484
485 return fopen(filename, "w+bTD");
486}
487
488#define tmpfile win32_tmpfile
489
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000490static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700491 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000492}
493
Elliott Hughes855cdf82018-04-02 14:24:03 -0700494static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700495 // TODO: reimplement to avoid leaking a FILE*.
496 return fileno(tmpfile());
497}
498
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000499#else
500
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700501static std::string make_temporary_template() {
502 const char* tmpdir = getenv("TMPDIR");
503 if (tmpdir == nullptr) tmpdir = P_tmpdir;
504 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
505}
506
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000507static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700508 std::string result(make_temporary_template());
509 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700510 die("unable to create temporary directory: %s", strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000511 }
512 return result;
513}
514
Elliott Hughes855cdf82018-04-02 14:24:03 -0700515static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700516 std::string path_template(make_temporary_template());
517 int fd = mkstemp(&path_template[0]);
518 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700519 die("failed to create temporary file for %s: %s\n", what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700520 }
521 unlink(path_template.c_str());
522 return fd;
523}
524
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700525#endif
526
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000527static std::string create_fbemarker_tmpdir() {
528 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000529 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
530 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
531 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700532 die("unable to create FBE marker file %s locally: %s",
533 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000534 }
535 close(fd);
536 return dir;
537}
538
539static void delete_fbemarker_tmpdir(const std::string& dir) {
540 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
541 if (unlink(marker_file.c_str()) == -1) {
542 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
543 marker_file.c_str(), errno, strerror(errno));
544 return;
545 }
546 if (rmdir(dir.c_str()) == -1) {
547 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
548 dir.c_str(), errno, strerror(errno));
549 return;
550 }
551}
552
Elliott Hughes45964a82017-05-03 22:43:23 -0700553static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700554 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700555
Yusuke Sato07447542015-06-25 14:39:19 -0700556 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700557 ZipEntry zip_entry;
558 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
559 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000560 return -1;
561 }
562
Elliott Hughes23af1122017-11-10 08:42:17 -0800563 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700564 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800565 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700566 int error = ExtractEntryToFile(zip, &zip_entry, fd);
567 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800568 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700569 }
570
Elliott Hughes4089d342017-10-27 14:21:12 -0700571 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800572 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700573 }
574
Elliott Hughes23af1122017-11-10 08:42:17 -0800575 fprintf(stderr, " took %.3fs\n", now() - start);
576
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700577 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700578}
579
Elliott Hughes5620d222018-03-28 08:20:00 -0700580static char* strip(char* s) {
581 while (*s && isspace(*s)) s++;
582
583 int n = strlen(s);
584 while (n-- > 0) {
585 if (!isspace(s[n])) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800586 s[n] = 0;
587 }
588 return s;
589}
590
591#define MAX_OPTIONS 32
Elliott Hughes5620d222018-03-28 08:20:00 -0700592static void check_requirement(Transport* transport, char* line) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800593 char *val[MAX_OPTIONS];
Elliott Hughes5620d222018-03-28 08:20:00 -0700594 unsigned count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800595 char *x;
596 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800597
Elliott Hughes5620d222018-03-28 08:20:00 -0700598 // "require product=alpha|beta|gamma"
599 // "require version-bootloader=1234"
600 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
601 // "require partition-exists=vendor"
602
603 char* name = line;
604 const char* product = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800605 if (!strncmp(name, "reject ", 7)) {
606 name += 7;
607 invert = 1;
608 } else if (!strncmp(name, "require ", 8)) {
609 name += 8;
610 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700611 } else if (!strncmp(name, "require-for-product:", 20)) {
612 // Get the product and point name past it
Elliott Hughes5620d222018-03-28 08:20:00 -0700613 product = name + 20;
Wink Savilleb98762f2011-04-04 17:54:59 -0700614 name = strchr(name, ' ');
Elliott Hughes5620d222018-03-28 08:20:00 -0700615 if (!name) die("android-info.txt syntax error: %s", line);
Wink Savilleb98762f2011-04-04 17:54:59 -0700616 *name = 0;
617 name += 1;
618 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800619 }
620
621 x = strchr(name, '=');
Elliott Hughes5620d222018-03-28 08:20:00 -0700622 if (x == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800623 *x = 0;
624 val[0] = x + 1;
625
Elliott Hughes5620d222018-03-28 08:20:00 -0700626 name = strip(name);
627
628 // "require partition-exists=x" is a special case, added because of the trouble we had when
629 // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
630 // missing out new partitions. A device with new partitions can use "partition-exists" to
631 // override the `is_optional` field in the `images` array.
632 if (!strcmp(name, "partition-exists")) {
633 const char* partition_name = val[0];
634 std::string has_slot;
635 if (!fb_getvar(transport, std::string("has-slot:") + partition_name, &has_slot) ||
636 (has_slot != "yes" && has_slot != "no")) {
637 die("device doesn't have required partition %s!", partition_name);
638 }
639 bool known_partition = false;
640 for (size_t i = 0; i < arraysize(images); ++i) {
641 if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
642 images[i].is_optional = false;
643 known_partition = true;
644 }
645 }
646 if (!known_partition) {
647 die("device requires partition %s which is not known to this version of fastboot",
648 partition_name);
649 }
650 return;
651 }
652
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800653 for(count = 1; count < MAX_OPTIONS; count++) {
654 x = strchr(val[count - 1],'|');
655 if (x == 0) break;
656 *x = 0;
657 val[count] = x + 1;
658 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800659
Elliott Hughes253c18d2015-03-18 22:47:09 -0700660 // Work around an unfortunate name mismatch.
Elliott Hughes5620d222018-03-28 08:20:00 -0700661 const char* var = name;
662 if (!strcmp(name, "board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800663
Elliott Hughes253c18d2015-03-18 22:47:09 -0700664 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
Elliott Hughes5620d222018-03-28 08:20:00 -0700665 if (out == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800666
Elliott Hughes5620d222018-03-28 08:20:00 -0700667 for (size_t i = 0; i < count; ++i) {
668 out[i] = xstrdup(strip(val[i]));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800669 }
670
Elliott Hughes5620d222018-03-28 08:20:00 -0700671 fb_queue_require(product, var, invert, count, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800672}
673
Elliott Hughes5620d222018-03-28 08:20:00 -0700674static void check_requirements(Transport* transport, char* data, int64_t sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700675 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800676 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700677 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800678 *s++ = 0;
Elliott Hughes5620d222018-03-28 08:20:00 -0700679 check_requirement(transport, data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800680 data = s;
681 } else {
682 s++;
683 }
684 }
Elliott Hughes5620d222018-03-28 08:20:00 -0700685 if (fb_execute_queue(transport)) die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686}
687
Elliott Hughesfc797672015-04-07 20:12:50 -0700688static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800689 fb_queue_notice("--------------------------------------------");
Elliott Hughes5620d222018-03-28 08:20:00 -0700690 fb_queue_display("Bootloader Version...", "version-bootloader");
691 fb_queue_display("Baseband Version.....", "version-baseband");
692 fb_queue_display("Serial Number........", "serialno");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800693 fb_queue_notice("--------------------------------------------");
694}
695
Elliott Hughes4089d342017-10-27 14:21:12 -0700696static struct sparse_file** load_sparse_files(int fd, int max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700697 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700698 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700699
Elliott Hughesfc797672015-04-07 20:12:50 -0700700 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700701 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700702
Elliott Hughes253c18d2015-03-18 22:47:09 -0700703 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700704 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700705
706 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700707 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700708
709 return out_s;
710}
711
David Pursell0b156632015-10-30 11:22:01 -0700712static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700713 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700714 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700715 max_download_size.empty()) {
716 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700717 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700718 }
719
Elliott Hughes77c0e662015-11-02 15:51:12 -0800720 // Some bootloaders (angler, for example) send spurious whitespace too.
721 max_download_size = android::base::Trim(max_download_size);
722
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700723 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700724 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800725 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700726 return 0;
727 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700728 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700729 return limit;
730}
731
David Pursell0b156632015-10-30 11:22:01 -0700732static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700733 int64_t limit = sparse_limit;
734 if (limit == 0) {
735 // Unlimited, so see what the target device's limit is.
736 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700737 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700738 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700739 }
740 if (target_sparse_limit > 0) {
741 limit = target_sparse_limit;
742 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700743 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700744 }
745 }
746
747 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500748 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700749 }
750
751 return 0;
752}
753
Elliott Hughes53ec4952016-05-11 12:39:27 -0700754static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700755 int64_t sz = get_file_size(fd);
756 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700757 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700758 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700759
Elliott Hughesfc797672015-04-07 20:12:50 -0700760 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700761 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700762 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700763 sparse_file** s = load_sparse_files(fd, limit);
764 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700765 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700766 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700767 buf->type = FB_BUFFER_SPARSE;
768 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700769 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500770 buf->type = FB_BUFFER_FD;
771 buf->data = nullptr;
772 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000773 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700774 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700775
Elliott Hughes53ec4952016-05-11 12:39:27 -0700776 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700777}
778
Elliott Hughes53ec4952016-05-11 12:39:27 -0700779static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500780 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
781
Elliott Hughes53ec4952016-05-11 12:39:27 -0700782 if (fd == -1) {
783 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700784 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500785
786 struct stat s;
787 if (fstat(fd, &s)) {
788 return false;
789 }
790 if (!S_ISREG(s.st_mode)) {
791 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
792 return false;
793 }
794
795 return load_buf_fd(transport, fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700796}
797
David Zeuthenb6ea4352017-08-07 14:29:26 -0400798static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
799 // Buffer needs to be at least the size of the VBMeta struct which
800 // is 256 bytes.
801 if (buf->sz < 256) {
802 return;
803 }
804
Elliott Hughes855cdf82018-04-02 14:24:03 -0700805 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400806
807 std::string data;
808 if (!android::base::ReadFdToString(buf->fd, &data)) {
809 die("Failed reading from vbmeta");
810 }
811
812 // There's a 32-bit big endian |flags| field at offset 120 where
813 // bit 0 corresponds to disable-verity and bit 1 corresponds to
814 // disable-verification.
815 //
816 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
817 // the VBMeta struct.
818 if (g_disable_verity) {
819 data[123] |= 0x01;
820 }
821 if (g_disable_verification) {
822 data[123] |= 0x02;
823 }
824
825 if (!android::base::WriteStringToFd(data, fd)) {
826 die("Failed writing to modified vbmeta");
827 }
828 close(buf->fd);
829 buf->fd = fd;
830 lseek(fd, 0, SEEK_SET);
831}
832
Elliott Hughes5620d222018-03-28 08:20:00 -0700833static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700834{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700835 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700836
David Zeuthenb6ea4352017-08-07 14:29:26 -0400837 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
838 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700839 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400840 rewrite_vbmeta_buffer(buf);
841 }
842
Rom Lemarchand622810c2013-06-28 09:54:59 -0700843 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800844 case FB_BUFFER_SPARSE: {
845 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700846 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700847 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700848 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800849 sparse_files.emplace_back(*s, sz);
850 ++s;
851 }
852
853 for (size_t i = 0; i < sparse_files.size(); ++i) {
854 const auto& pair = sparse_files[i];
Elliott Hughes5620d222018-03-28 08:20:00 -0700855 fb_queue_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700856 }
857 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800858 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500859 case FB_BUFFER_FD:
Elliott Hughes5620d222018-03-28 08:20:00 -0700860 fb_queue_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700861 break;
862 default:
863 die("unknown buffer type: %d", buf->type);
864 }
865}
866
Elliott Hughes42b18a52018-04-10 15:32:21 -0700867static std::string get_current_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700868 std::string current_slot;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700869 if (!fb_getvar(transport, "current-slot", &current_slot)) return "";
870 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700871}
872
873static int get_slot_count(Transport* transport) {
874 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700875 int count = 0;
876 if (!fb_getvar(transport, "slot-count", &var) || !android::base::ParseInt(var, &count)) {
877 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700878 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700879 return count;
880}
881
Alex Lightbb9b8a52016-06-29 09:26:44 -0700882static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700883 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700884}
885
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700886// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700887static std::string get_other_slot(const std::string& current_slot, int count) {
888 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700889
Daniel Rosenberg80919472016-06-30 19:25:31 -0700890 char next = (current_slot[0] - 'a' + 1)%count + 'a';
891 return std::string(1, next);
892}
893
894static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
895 return get_other_slot(current_slot, get_slot_count(transport));
896}
897
898static std::string get_other_slot(Transport* transport, int count) {
899 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700900}
901
Alex Lightbb9b8a52016-06-29 09:26:44 -0700902static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700903 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -0700904}
905
Daniel Rosenberg80919472016-06-30 19:25:31 -0700906static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
907 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700908 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800909 if (allow_all) {
910 return "all";
911 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700912 int count = get_slot_count(transport);
913 if (count > 0) {
914 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800915 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -0700916 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800917 }
918 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700919 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800920
Daniel Rosenberg80919472016-06-30 19:25:31 -0700921 int count = get_slot_count(transport);
Elliott Hughes4089d342017-10-27 14:21:12 -0700922 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800923
Daniel Rosenberg80919472016-06-30 19:25:31 -0700924 if (slot == "other") {
925 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700926 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700927 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800928 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700929 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800930 }
931
Daniel Rosenberg80919472016-06-30 19:25:31 -0700932 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
933
934 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
935 for (int i=0; i<count; i++) {
936 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700937 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700938
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700939 exit(1);
940}
941
Daniel Rosenberg80919472016-06-30 19:25:31 -0700942static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800943 return verify_slot(transport, slot, true);
944}
945
Daniel Rosenberg80919472016-06-30 19:25:31 -0700946static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700947 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800948 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700949 std::string current_slot;
950
Daniel Rosenberg80919472016-06-30 19:25:31 -0700951 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800952 /* If has-slot is not supported, the answer is no. */
953 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700954 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800955 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700956 if (slot == "") {
957 current_slot = get_current_slot(transport);
958 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700959 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700960 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700961 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700962 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700963 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700964 }
965 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700966 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -0700967 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -0700968 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700969 }
970 func(part);
971 }
972}
973
David Pursell0b156632015-10-30 11:22:01 -0700974/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
975 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
976 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
977 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700978 */
Daniel Rosenberg80919472016-06-30 19:25:31 -0700979static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700980 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800981 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700982
Daniel Rosenberg80919472016-06-30 19:25:31 -0700983 if (slot == "all") {
984 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700985 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700986 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800987 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700988 for (int i=0; i < get_slot_count(transport); i++) {
989 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700990 }
991 } else {
David Pursell0b156632015-10-30 11:22:01 -0700992 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700993 }
994 } else {
David Pursell0b156632015-10-30 11:22:01 -0700995 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700996 }
997}
998
David Pursell0b156632015-10-30 11:22:01 -0700999static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001000 struct fastboot_buffer buf;
1001
Elliott Hughes53ec4952016-05-11 12:39:27 -07001002 if (!load_buf(transport, fname, &buf)) {
1003 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001004 }
1005 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001006}
1007
Elliott Hughes45964a82017-05-03 22:43:23 -07001008static void do_update_signature(ZipArchiveHandle zip, const char* filename) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001009 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001010 void* data = unzip_to_memory(zip, filename, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001011 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001012 fb_queue_download("signature", data, sz);
1013 fb_queue_command("signature", "installing signature");
1014}
1015
Daniel Rosenberg13454092016-06-27 19:43:11 -07001016// Sets slot_override as the active slot. If slot_override is blank,
1017// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -07001018static void set_active(Transport* transport, const std::string& slot_override) {
Elliott Hughes42b18a52018-04-10 15:32:21 -07001019 if (!supports_AB(transport)) return;
1020
Daniel Rosenberg80919472016-06-30 19:25:31 -07001021 if (slot_override != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001022 fb_set_active(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001023 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001024 std::string current_slot = get_current_slot(transport);
1025 if (current_slot != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001026 fb_set_active(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001027 }
1028 }
1029}
1030
Elliott Hughes59efd442018-04-05 14:24:00 -07001031static void do_update(Transport* transport, const char* filename, const std::string& slot_override, bool skip_secondary) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001032 queue_info_dump();
1033
Wink Savilleb98762f2011-04-04 17:54:59 -07001034 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1035
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001036 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001037 int error = OpenArchive(filename, &zip);
1038 if (error != 0) {
1039 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001040 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001041
Elliott Hughesfc797672015-04-07 20:12:50 -07001042 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001043 void* data = unzip_to_memory(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001044 if (data == nullptr) {
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001045 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001046 }
1047
Elliott Hughes5620d222018-03-28 08:20:00 -07001048 check_requirements(transport, reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001049
Alex Lightbb9b8a52016-06-29 09:26:44 -07001050 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001051 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001052 if (slot_override != "") {
1053 secondary = get_other_slot(transport, slot_override);
1054 } else {
1055 secondary = get_other_slot(transport);
1056 }
1057 if (secondary == "") {
1058 if (supports_AB(transport)) {
1059 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1060 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001061 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001062 }
1063 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001064 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001065 const char* slot = slot_override.c_str();
1066 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001067 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001068 slot = secondary.c_str();
1069 } else {
1070 continue;
1071 }
1072 }
1073
Elliott Hughes253c18d2015-03-18 22:47:09 -07001074 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001075 if (fd == -1) {
1076 if (images[i].is_optional) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001077 continue; // An optional file is missing, so ignore it.
Elliott Hughesacdbe922015-06-02 21:37:05 -07001078 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001079 die("non-optional file %s missing", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001080 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001081
Elliott Hughes253c18d2015-03-18 22:47:09 -07001082 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001083 if (!load_buf_fd(transport, fd, &buf)) {
1084 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1085 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001086
Elliott Hughes4089d342017-10-27 14:21:12 -07001087 auto update = [&](const std::string& partition) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001088 do_update_signature(zip, images[i].sig_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001089 flash_buf(partition.c_str(), &buf);
1090 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001091 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001092 * program exits.
1093 */
1094 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001095 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001096 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001097
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001098 if (slot_override == "all") {
1099 set_active(transport, "a");
1100 } else {
1101 set_active(transport, slot_override);
1102 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001103
1104 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001105}
1106
Alex Lightbb9b8a52016-06-29 09:26:44 -07001107static void do_send_signature(const std::string& fn) {
1108 std::size_t extension_loc = fn.find(".img");
1109 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001110
Alex Lightbb9b8a52016-06-29 09:26:44 -07001111 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001112
1113 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001114 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001115 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001116
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001117 fb_queue_download("signature", data, sz);
1118 fb_queue_command("signature", "installing signature");
1119}
1120
Elliott Hughes59efd442018-04-05 14:24:00 -07001121static void do_flashall(Transport* transport, const std::string& slot_override, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001122 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001123 queue_info_dump();
1124
Wink Savilleb98762f2011-04-04 17:54:59 -07001125 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1126
Elliott Hughes45964a82017-05-03 22:43:23 -07001127 fname = find_item_given_name("android-info.txt");
Elliott Hughes2810d002016-04-25 14:31:18 -07001128 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001129
Elliott Hughesfc797672015-04-07 20:12:50 -07001130 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001131 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001132 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001133
Elliott Hughes5620d222018-03-28 08:20:00 -07001134 check_requirements(transport, reinterpret_cast<char*>(data), sz);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001135
Alex Lightbb9b8a52016-06-29 09:26:44 -07001136 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001137 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001138 if (slot_override != "") {
1139 secondary = get_other_slot(transport, slot_override);
1140 } else {
1141 secondary = get_other_slot(transport);
1142 }
1143 if (secondary == "") {
1144 if (supports_AB(transport)) {
1145 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1146 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001147 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001148 }
1149 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001150
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001151 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001152 const char* slot = NULL;
1153 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001154 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001155 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001156 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001157 }
1158 if (!slot) continue;
Elliott Hughes45964a82017-05-03 22:43:23 -07001159 fname = find_item_given_name(images[i].img_name);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001160 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001161 if (!load_buf(transport, fname.c_str(), &buf)) {
1162 if (images[i].is_optional) continue;
Elliott Hughes4089d342017-10-27 14:21:12 -07001163 die("could not load '%s': %s", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001164 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001165
1166 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001167 do_send_signature(fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001168 flash_buf(partition.c_str(), &buf);
1169 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001170 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001171 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001172
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001173 if (slot_override == "all") {
1174 set_active(transport, "a");
1175 } else {
1176 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001177 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001178}
1179
Elliott Hughesd6365a72017-05-08 18:04:49 -07001180static std::string next_arg(std::vector<std::string>* args) {
1181 if (args->empty()) syntax_error("expected argument");
1182 std::string result = args->front();
1183 args->erase(args->begin());
1184 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001185}
1186
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001187static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001188 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001189
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001190 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001191 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001192 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001193 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001194 fb_queue_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001195}
1196
Connor O'Brience16a8a2017-02-06 14:39:31 -08001197static std::string fb_fix_numeric_var(std::string var) {
1198 // Some bootloaders (angler, for example), send spurious leading whitespace.
1199 var = android::base::Trim(var);
1200 // Some bootloaders (hammerhead, for example) use implicit hex.
1201 // This code used to use strtol with base 16.
1202 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1203 return var;
1204}
1205
1206static unsigned fb_get_flash_block_size(Transport* transport, std::string name) {
1207 std::string sizeString;
Elliott Hughes5620d222018-03-28 08:20:00 -07001208 if (!fb_getvar(transport, name, &sizeString) || sizeString.empty()) {
1209 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001210 return 0;
1211 }
1212 sizeString = fb_fix_numeric_var(sizeString);
1213
1214 unsigned size;
1215 if (!android::base::ParseUint(sizeString, &size)) {
1216 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1217 return 0;
1218 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001219 if ((size & (size - 1)) != 0) {
1220 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001221 return 0;
1222 }
1223 return size;
1224}
1225
David Pursell0b156632015-10-30 11:22:01 -07001226static void fb_perform_format(Transport* transport,
Elliott Hughes5620d222018-03-28 08:20:00 -07001227 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001228 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001229 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001230 std::string partition_type, partition_size;
1231
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001232 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001233 const char* errMsg = nullptr;
1234 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001235 TemporaryFile output;
1236 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001237
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001238 unsigned int limit = INT_MAX;
1239 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001240 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001241 }
1242 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001243 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001244 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001245
Elliott Hughes5620d222018-03-28 08:20:00 -07001246 if (!fb_getvar(transport, "partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001247 errMsg = "Can't determine partition type.\n";
1248 goto failed;
1249 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001250 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001251 if (partition_type != type_override) {
1252 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001253 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001254 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001255 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001256 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001257
Elliott Hughes5620d222018-03-28 08:20:00 -07001258 if (!fb_getvar(transport, "partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001259 errMsg = "Unable to get partition size\n";
1260 goto failed;
1261 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001262 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001263 if (partition_size != size_override) {
1264 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001265 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001266 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001267 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001268 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001269 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001270
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001271 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001272 if (!gen) {
1273 if (skip_if_not_supported) {
1274 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001275 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001276 return;
1277 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001278 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1279 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001280 return;
1281 }
1282
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001283 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001284 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001285 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1286 return;
1287 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001288
Connor O'Brience16a8a2017-02-06 14:39:31 -08001289 unsigned eraseBlkSize, logicalBlkSize;
1290 eraseBlkSize = fb_get_flash_block_size(transport, "erase-block-size");
1291 logicalBlkSize = fb_get_flash_block_size(transport, "logical-block-size");
1292
Jin Qian4a335822017-04-18 16:23:18 -07001293 if (fs_generator_generate(gen, output.path, size, initial_dir,
1294 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001295 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001296 return;
1297 }
1298
Jin Qian4a335822017-04-18 16:23:18 -07001299 fd.reset(open(output.path, O_RDONLY));
1300 if (fd == -1) {
1301 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1302 return;
1303 }
1304 if (!load_buf_fd(transport, fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001305 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001306 return;
1307 }
1308 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001309 return;
1310
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001311failed:
1312 if (skip_if_not_supported) {
1313 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001314 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001315 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001316 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001317}
1318
Elliott Hughes11ff3452018-04-09 13:58:41 -07001319int FastBoot::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001320 bool wants_wipe = false;
1321 bool wants_reboot = false;
1322 bool wants_reboot_bootloader = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001323 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001324 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001325 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001326 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001327 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001328 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001329 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001330 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001331 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001332
Elliott Hughes577e8b42018-04-05 16:12:47 -07001333 g_boot_img_hdr.kernel_addr = 0x00008000;
1334 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1335 g_boot_img_hdr.second_addr = 0x00f00000;
1336 g_boot_img_hdr.tags_addr = 0x00000100;
1337 g_boot_img_hdr.page_size = 2048;
1338
JP Abgrall7b8970c2013-03-07 17:06:41 -08001339 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001340 {"base", required_argument, 0, 0},
1341 {"cmdline", required_argument, 0, 0},
1342 {"disable-verification", no_argument, 0, 0},
1343 {"disable-verity", no_argument, 0, 0},
1344 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001345 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001346 {"kernel-offset", required_argument, 0, 0},
1347 {"os-patch-level", required_argument, 0, 0},
1348 {"os-version", required_argument, 0, 0},
1349 {"page-size", required_argument, 0, 0},
1350 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001351 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001352 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001353 {"skip-secondary", no_argument, 0, 0},
1354 {"slot", required_argument, 0, 0},
1355 {"tags-offset", required_argument, 0, 0},
1356 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001357 {"verbose", no_argument, 0, 'v'},
1358 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001359#if !defined(_WIN32)
1360 {"wipe-and-use-fbe", no_argument, 0, 0},
1361#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001362 {0, 0, 0, 0}
1363 };
Colin Cross8879f982012-05-22 17:53:34 -07001364
1365 serial = getenv("ANDROID_SERIAL");
1366
Elliott Hughes577e8b42018-04-05 16:12:47 -07001367 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001368 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001369 if (c == 0) {
1370 std::string name{longopts[longindex].name};
1371 if (name == "base") {
1372 g_base_addr = strtoul(optarg, 0, 16);
1373 } else if (name == "cmdline") {
1374 g_cmdline = optarg;
1375 } else if (name == "disable-verification") {
1376 g_disable_verification = true;
1377 } else if (name == "disable-verity") {
1378 g_disable_verity = true;
1379 } else if (name == "header-version") {
1380 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1381 } else if (name == "kernel-offset") {
1382 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1383 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001384 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001385 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001386 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001387 } else if (name == "page-size") {
1388 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1389 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1390 } else if (name == "ramdisk-offset") {
1391 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1392 } else if (name == "skip-reboot") {
1393 skip_reboot = true;
1394 } else if (name == "skip-secondary") {
1395 skip_secondary = true;
1396 } else if (name == "slot") {
1397 slot_override = optarg;
1398 } else if (name == "tags-offset") {
1399 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1400 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001401 setvbuf(stdout, nullptr, _IONBF, 0);
1402 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001403 } else if (name == "version") {
Elliott Hughes4d4f64f2017-05-08 11:30:34 -07001404 fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001405 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001406 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001407#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001408 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001409 wants_wipe = true;
1410 set_fbe_marker = true;
1411#endif
1412 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001413 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001414 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001415 } else {
1416 switch (c) {
1417 case 'a':
1418 wants_set_active = true;
1419 if (optarg) next_active = optarg;
1420 break;
1421 case 'h':
1422 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001423 case 'l':
1424 g_long_listing = true;
1425 break;
1426 case 's':
1427 serial = optarg;
1428 break;
1429 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001430 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1431 die("invalid sparse limit %s", optarg);
1432 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001433 break;
1434 case 'v':
1435 set_verbose();
1436 break;
1437 case 'w':
1438 wants_wipe = true;
1439 break;
1440 case '?':
1441 return 1;
1442 default:
1443 abort();
1444 }
Colin Cross8879f982012-05-22 17:53:34 -07001445 }
1446 }
1447
1448 argc -= optind;
1449 argv += optind;
1450
Elliott Hughesd6365a72017-05-08 18:04:49 -07001451 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001452
Colin Cross8fb6e062012-07-24 16:36:41 -07001453 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001454 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001455 return 0;
1456 }
1457
Colin Crossc7b75dc2012-08-29 18:17:06 -07001458 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001459 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001460 }
1461
David Pursell0b156632015-10-30 11:22:01 -07001462 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001463 if (transport == nullptr) {
1464 return 1;
1465 }
1466
Elliott Hughes5620d222018-03-28 08:20:00 -07001467 const double start = now();
1468
Daniel Rosenberg80919472016-06-30 19:25:31 -07001469 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1470 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001471
1472 if (wants_set_active) {
1473 if (next_active == "") {
1474 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001475 std::string current_slot;
1476 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001477 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001478 } else {
1479 wants_set_active = false;
1480 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001481 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001482 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001483 }
1484 }
1485 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001486
Elliott Hughesd6365a72017-05-08 18:04:49 -07001487 std::vector<std::string> args(argv, argv + argc);
1488 while (!args.empty()) {
1489 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001490
Elliott Hughesd6365a72017-05-08 18:04:49 -07001491 if (command == "getvar") {
1492 std::string variable = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001493 fb_queue_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001494 } else if (command == "erase") {
1495 std::string partition = next_arg(&args);
1496 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001497 std::string partition_type;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001498 if (fb_getvar(transport, std::string("partition-type:") + partition,
1499 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001500 fs_get_generator(partition_type) != nullptr) {
1501 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1502 partition_type.c_str());
1503 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001504
Elliott Hughes5620d222018-03-28 08:20:00 -07001505 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001506 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001507 do_for_partitions(transport, partition, slot_override, erase, true);
1508 } else if (android::base::StartsWith(command, "format")) {
1509 // Parsing for: "format[:[type][:[size]]]"
1510 // Some valid things:
1511 // - select only the size, and leave default fs type:
1512 // format::0x4000000 userdata
1513 // - default fs type and size:
1514 // format userdata
1515 // format:: userdata
1516 std::vector<std::string> pieces = android::base::Split(command, ":");
1517 std::string type_override;
1518 if (pieces.size() > 1) type_override = pieces[1].c_str();
1519 std::string size_override;
1520 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001521
Elliott Hughesd6365a72017-05-08 18:04:49 -07001522 std::string partition = next_arg(&args);
1523
1524 auto format = [&](const std::string& partition) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001525 fb_perform_format(transport, partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001526 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001527 do_for_partitions(transport, partition.c_str(), slot_override, format, true);
1528 } else if (command == "signature") {
1529 std::string filename = next_arg(&args);
1530 data = load_file(filename.c_str(), &sz);
1531 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -07001532 if (sz != 256) die("signature must be 256 bytes (got %" PRId64 ")", sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001533 fb_queue_download("signature", data, sz);
1534 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001535 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001536 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001537
1538 if (args.size() == 1) {
1539 std::string what = next_arg(&args);
1540 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001541 wants_reboot = false;
1542 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001543 } else {
1544 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001545 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001546
Elliott Hughesca85df02015-02-25 10:02:00 -08001547 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001548 if (!args.empty()) syntax_error("junk after reboot command");
1549 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001550 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001551 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001552 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001553 } else if (command == "boot") {
1554 std::string kernel = next_arg(&args);
1555 std::string ramdisk;
1556 if (!args.empty()) ramdisk = next_arg(&args);
1557 std::string second_stage;
1558 if (!args.empty()) second_stage = next_arg(&args);
1559
Elliott Hughes577e8b42018-04-05 16:12:47 -07001560 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001561 fb_queue_download("boot.img", data, sz);
1562 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001563 } else if (command == "flash") {
1564 std::string pname = next_arg(&args);
1565
Elliott Hughes2810d002016-04-25 14:31:18 -07001566 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001567 if (!args.empty()) {
1568 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001569 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001570 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001571 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001572 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001573
1574 auto flash = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001575 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001576 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001577 do_for_partitions(transport, pname.c_str(), slot_override, flash, true);
1578 } else if (command == "flash:raw") {
1579 std::string partition = next_arg(&args);
1580 std::string kernel = next_arg(&args);
1581 std::string ramdisk;
1582 if (!args.empty()) ramdisk = next_arg(&args);
1583 std::string second_stage;
1584 if (!args.empty()) second_stage = next_arg(&args);
1585
Elliott Hughes577e8b42018-04-05 16:12:47 -07001586 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001587 auto flashraw = [&](const std::string& partition) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001588 fb_queue_flash(partition, data, sz);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001589 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001590 do_for_partitions(transport, partition, slot_override, flashraw, true);
1591 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001592 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001593 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
Elliott Hughes59efd442018-04-05 14:24:00 -07001594 do_flashall(transport, slot_override, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001595 } else {
Elliott Hughes59efd442018-04-05 14:24:00 -07001596 do_flashall(transport, slot_override, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001597 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001598 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001599 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001600 bool slot_all = (slot_override == "all");
1601 if (slot_all) {
1602 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1603 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001604 std::string filename = "update.zip";
1605 if (!args.empty()) {
1606 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001607 }
Elliott Hughes59efd442018-04-05 14:24:00 -07001608 do_update(transport, filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001609 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001610 } else if (command == "set_active") {
1611 std::string slot = verify_slot(transport, next_arg(&args), false);
Elliott Hughes5620d222018-03-28 08:20:00 -07001612 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001613 } else if (command == "stage") {
1614 std::string filename = next_arg(&args);
1615
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001616 struct fastboot_buffer buf;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001617 if (!load_buf(transport, filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
1618 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001619 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001620 fb_queue_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001621 } else if (command == "get_staged") {
1622 std::string filename = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001623 fb_queue_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001624 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001625 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001626 } else if (command == "flashing") {
1627 if (args.empty()) {
1628 syntax_error("missing 'flashing' command");
1629 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1630 args[0] == "unlock_critical" ||
1631 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001632 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001633 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001634 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001635 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001636 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001637 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001638 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001639 }
1640 }
1641
1642 if (wants_wipe) {
1643 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001644 if (set_fbe_marker) {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001645 fprintf(stderr, "setting FBE marker on initial userdata...\n");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001646 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Elliott Hughesd6365a72017-05-08 18:04:49 -07001647 fb_perform_format(transport, "userdata", 1, "", "", initial_userdata_dir);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001648 delete_fbemarker_tmpdir(initial_userdata_dir);
1649 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001650 fb_perform_format(transport, "userdata", 1, "", "", "");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001651 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001652
1653 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001654 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001655 fb_queue_erase("cache");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001656 fb_perform_format(transport, "cache", 1, "", "", "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001657 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001658 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001659 if (wants_set_active) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001660 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001661 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001662 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001663 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001664 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001665 } else if (wants_reboot_bootloader) {
1666 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001667 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001668 }
1669
Elliott Hughes5620d222018-03-28 08:20:00 -07001670 int status = fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
1671 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
1672 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001673}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001674
1675void FastBoot::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
1676 unsigned year, month, day;
1677 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1678 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1679 }
1680 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1681 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1682 hdr->SetOsPatchLevel(year, month);
1683}
1684
1685void FastBoot::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
1686 unsigned major = 0, minor = 0, patch = 0;
1687 std::vector<std::string> versions = android::base::Split(arg, ".");
1688 if (versions.size() < 1 || versions.size() > 3 ||
1689 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1690 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1691 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1692 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1693 syntax_error("bad OS version: %s", arg);
1694 }
1695 hdr->SetOsVersion(major, minor, patch);
1696}