blob: 6cf544fb56a93acf6c9233be523ff7feab7ea852 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070029#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <errno.h>
31#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070032#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070033#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070034#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070035#include <stdint.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070041#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070042#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080043
Elliott Hughes290a2282016-11-14 17:08:47 -080044#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070045#include <functional>
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080047#include <utility>
48#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070049
Elliott Hughes82ff3152016-08-31 15:07:18 -070050#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070051#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080053#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070054#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080055#include <android-base/strings.h>
Jin Qian4a335822017-04-18 16:23:18 -070056#include <android-base/test_utils.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050057#include <android-base/unique_fd.h>
Colin Crossf8387882012-05-24 17:18:41 -070058#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070059#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Elliott Hughes253c18d2015-03-18 22:47:09 -070061#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080062#include "diagnose_usb.h"
Aaron Wisnerdb511202018-06-26 15:38:35 -050063#include "engine.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070064#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080065#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070066#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080067#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070068#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Chris Fries0ea946c2017-04-12 10:25:57 -050070using android::base::unique_fd;
71
Colin Crossf8387882012-05-24 17:18:41 -070072#ifndef O_BINARY
73#define O_BINARY 0
74#endif
75
Wink Savilleb98762f2011-04-04 17:54:59 -070076char cur_product[FB_RESPONSE_SZ + 1];
77
David Pursell2ec418a2016-01-20 08:32:08 -080078static const char* serial = nullptr;
Elliott Hughes577e8b42018-04-05 16:12:47 -070079
Elliott Hughes577e8b42018-04-05 16:12:47 -070080static bool g_long_listing = false;
Chris Fries6a999712017-04-04 09:52:47 -050081// Don't resparse files in too-big chunks.
82// libsparse will support INT_MAX, but this results in large allocations, so
83// let's keep it at 1GB to avoid memory pressure on the host.
84static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Elliott Hughes542370d2018-04-19 19:49:44 -070085static uint64_t sparse_limit = 0;
Colin Crossf8387882012-05-24 17:18:41 -070086static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
Elliott Hughes577e8b42018-04-05 16:12:47 -070088static unsigned g_base_addr = 0x10000000;
89static boot_img_hdr_v1 g_boot_img_hdr = {};
90static std::string g_cmdline;
JP Abgrall7b8970c2013-03-07 17:06:41 -080091
David Zeuthenb6ea4352017-08-07 14:29:26 -040092static bool g_disable_verity = false;
93static bool g_disable_verification = false;
94
Paul Crowley8f7f56e2015-11-27 09:29:37 +000095static const std::string convert_fbe_marker_filename("convert_fbe");
96
Rom Lemarchand622810c2013-06-28 09:54:59 -070097enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -050098 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -070099 FB_BUFFER_SPARSE,
100};
101
102struct fastboot_buffer {
103 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700104 void* data;
105 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500106 int fd;
David Anderson32e376f2018-08-16 13:43:11 -0700107 int64_t image_size;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700108};
109
David Anderson32e376f2018-08-16 13:43:11 -0700110struct Image {
Elliott Hughes45964a82017-05-03 22:43:23 -0700111 const char* nickname;
112 const char* img_name;
113 const char* sig_name;
114 const char* part_name;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700115 bool optional_if_no_image;
116 bool optional_if_no_partition;
Mark Salyzyn8fb0fb82018-08-24 14:37:11 -0700117 bool flashall;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700118 bool IsSecondary() const { return nickname == nullptr; }
David Anderson32e376f2018-08-16 13:43:11 -0700119};
120
121static Image images[] = {
Dario Frenic7ea1af2018-05-25 16:07:19 +0100122 // clang-format off
Mark Salyzyn8fb0fb82018-08-24 14:37:11 -0700123 { "boot", "boot.img", "boot.sig", "boot", false, false, true, },
124 { nullptr, "boot_other.img", "boot.sig", "boot", true, false, true, },
125 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, false, true, },
126 { "dts", "dt.img", "dt.sig", "dts", true, false, true, },
127 { "odm", "odm.img", "odm.sig", "odm", true, false, true, },
128 { "product", "product.img", "product.sig", "product", true, false, true, },
Dario Freniab5583b2018-08-17 01:01:25 +0100129 { "product_services",
130 "product_services.img",
131 "product_services.sig",
David Anderson8cdea7f2018-08-06 15:36:00 -0700132 "product_services",
Mark Salyzyn8fb0fb82018-08-24 14:37:11 -0700133 true, true, true, },
134 { "recovery", "recovery.img", "recovery.sig", "recovery", true, false, true, },
135 { "system", "system.img", "system.sig", "system", false, true, true, },
136 { nullptr, "system_other.img", "system.sig", "system", true, false, true, },
137 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, false, true, },
138 { "vendor", "vendor.img", "vendor.sig", "vendor", true, true, true, },
139 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, false, true, },
140 { "super", "super.img", "super.sig", "super", true, true, false, },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100141 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700142};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700143
Elliott Hughes45964a82017-05-03 22:43:23 -0700144static std::string find_item_given_name(const char* img_name) {
145 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700146 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700147 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700148 }
Alex Lightbb9b8a52016-06-29 09:26:44 -0700149 return android::base::StringPrintf("%s/%s", dir, img_name);
150}
151
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700152static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700153 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700154 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700155 return find_item_given_name(images[i].img_name);
156 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157 }
158
Elliott Hughesd6365a72017-05-08 18:04:49 -0700159 if (item == "userdata") return find_item_given_name("userdata.img");
160 if (item == "cache") return find_item_given_name("cache.img");
Elliott Hughes45964a82017-05-03 22:43:23 -0700161
Elliott Hughesd6365a72017-05-08 18:04:49 -0700162 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700163 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164}
165
Elliott Hughesfc797672015-04-07 20:12:50 -0700166static int64_t get_file_size(int fd) {
167 struct stat sb;
168 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700169}
170
Elliott Hughesfc797672015-04-07 20:12:50 -0700171static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800172 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700173 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174
Elliott Hughesfc797672015-04-07 20:12:50 -0700175 *sz = get_file_size(fd);
176 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700177 goto oops;
178 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179
Elliott Hughesfc797672015-04-07 20:12:50 -0700180 data = (char*) malloc(*sz);
181 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182
Elliott Hughesfc797672015-04-07 20:12:50 -0700183 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184 close(fd);
185
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 return data;
187
188oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800189 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 close(fd);
191 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800192 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 return 0;
194}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195
Elliott Hughes2810d002016-04-25 14:31:18 -0700196static void* load_file(const std::string& path, int64_t* sz) {
197 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700198 if (fd == -1) return nullptr;
199 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700200}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201
Elliott Hughesfc797672015-04-07 20:12:50 -0700202static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700203 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
204 return -1;
205 }
206
Scott Anderson13081c62012-04-06 12:39:30 -0700207 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700209 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
210 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 return 0;
212}
213
Elliott Hughesfc797672015-04-07 20:12:50 -0700214static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800215 return match_fastboot_with_serial(info, serial);
216}
217
Elliott Hughesfc797672015-04-07 20:12:50 -0700218static int list_devices_callback(usb_ifc_info* info) {
219 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800220 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700221 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800222 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700223 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224 if (!serial[0]) {
225 serial = "????????????";
226 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700227 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700228 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800229 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700230 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800231 printf("%-22s fastboot", serial.c_str());
232 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700233 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800234 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 }
236
237 return -1;
238}
239
David Pursell2ec418a2016-01-20 08:32:08 -0800240// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
241// a specific device, otherwise the first USB device found will be used.
242//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700243// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800244// Otherwise it blocks until the target is available.
245//
246// The returned Transport is a singleton, so multiple calls to this function will return the same
247// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700248static Transport* open_device() {
David Pursell2ec418a2016-01-20 08:32:08 -0800249 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250
David Pursell4601c972016-02-05 15:35:09 -0800251 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800252 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800253 int port = 0;
254 if (serial != nullptr) {
255 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800256
David Pursell4601c972016-02-05 15:35:09 -0800257 if (android::base::StartsWith(serial, "tcp:")) {
258 protocol = Socket::Protocol::kTcp;
259 port = tcp::kDefaultPort;
260 net_address = serial + strlen("tcp:");
261 } else if (android::base::StartsWith(serial, "udp:")) {
262 protocol = Socket::Protocol::kUdp;
263 port = udp::kDefaultPort;
264 net_address = serial + strlen("udp:");
265 }
266
267 if (net_address != nullptr) {
268 std::string error;
269 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700270 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800271 }
David Pursell2ec418a2016-01-20 08:32:08 -0800272 }
273 }
274
David Anderson1d887432018-08-27 16:47:32 -0700275 Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800276 while (true) {
277 if (!host.empty()) {
278 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800279 if (protocol == Socket::Protocol::kTcp) {
280 transport = tcp::Connect(host, port, &error).release();
281 } else if (protocol == Socket::Protocol::kUdp) {
282 transport = udp::Connect(host, port, &error).release();
283 }
284
David Pursell2ec418a2016-01-20 08:32:08 -0800285 if (transport == nullptr && announce) {
286 fprintf(stderr, "error: %s\n", error.c_str());
287 }
288 } else {
289 transport = usb_open(match_fastboot);
290 }
291
292 if (transport != nullptr) {
293 return transport;
294 }
295
David Pursell0b156632015-10-30 11:22:01 -0700296 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800297 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700298 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800300 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301 }
302}
303
Elliott Hughesfc797672015-04-07 20:12:50 -0700304static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305 // We don't actually open a USB device here,
306 // just getting our callback called so we can
307 // list all the connected devices.
308 usb_open(list_devices_callback);
309}
310
Elliott Hughesd6365a72017-05-08 18:04:49 -0700311static void syntax_error(const char* fmt, ...) {
312 fprintf(stderr, "fastboot: usage: ");
313
314 va_list ap;
315 va_start(ap, fmt);
316 vfprintf(stderr, fmt, ap);
317 va_end(ap);
318
319 fprintf(stderr, "\n");
320 exit(1);
321}
322
323static int show_help() {
324 // clang-format off
325 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700326// 1 2 3 4 5 6 7 8
327// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
Elliott Hughesb76188f2018-04-03 13:30:18 -0700328 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700330 "flashing:\n"
331 " update ZIP Flash all partitions from an update.zip package.\n"
332 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
333 " On A/B devices, flashed slot is set as active.\n"
334 " Secondary images may be flashed to inactive slot.\n"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700335 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
336 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700337 "\n"
338 "basics:\n"
339 " devices [-l] List devices in bootloader (-l: with device paths).\n"
340 " getvar NAME Display given bootloader variable.\n"
341 " reboot [bootloader] Reboot device.\n"
342 "\n"
343 "locking/unlocking:\n"
344 " flashing lock|unlock Lock/unlock partitions for flashing\n"
345 " flashing lock_critical|unlock_critical\n"
346 " Lock/unlock 'critical' bootloader partitions.\n"
347 " flashing get_unlock_ability\n"
348 " Check whether unlocking is allowed (1) or not(0).\n"
349 "\n"
350 "advanced:\n"
351 " erase PARTITION Erase a flash partition.\n"
352 " format[:FS_TYPE[:SIZE]] PARTITION\n"
353 " Format a flash partition.\n"
354 " set_active SLOT Set the active slot.\n"
355 " oem [COMMAND...] Execute OEM-specific command.\n"
356 "\n"
357 "boot image:\n"
358 " boot KERNEL [RAMDISK [SECOND]]\n"
359 " Download and boot kernel from RAM.\n"
360 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
361 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700362 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700363 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
364 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
365 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
366 " --tags-offset Set tags offset (default: 0x00000100).\n"
367 " --page-size BYTES Set flash page size (default: 2048).\n"
368 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700369 " --os-version MAJOR[.MINOR[.PATCH]]\n"
370 " Set boot image OS version (default: 0.0.0).\n"
371 " --os-patch-level YYYY-MM-DD\n"
372 " Set boot image OS security patch level.\n"
373 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700374 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700375 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700376 //" continue Continue with autoboot.\n"
377 //"\n"
378 "Android Things:\n"
379 " stage IN_FILE Sends given file to stage for the next command.\n"
380 " 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 -0800381 "\n"
382 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700383 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700384 " -s SERIAL Specify a USB device.\n"
385 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700386 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700387 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
388 " non-current slot (default: current active slot).\n"
389 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
390 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
391 " --skip-reboot Don't reboot device after flashing.\n"
392 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
393 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000394#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700395 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000396#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700397 // TODO: remove --unbuffered?
398 " --unbuffered Don't buffer input or output.\n"
399 " --verbose, -v Verbose output.\n"
400 " --version Display version.\n"
401 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700403 // clang-format off
404 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000406
Elliott Hughesd6365a72017-05-08 18:04:49 -0700407static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
Elliott Hughes577e8b42018-04-05 16:12:47 -0700408 const std::string& second_stage, int64_t* sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700409 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700410 void* kdata = load_file(kernel.c_str(), &ksize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700411 if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800412
Elliott Hughesfc797672015-04-07 20:12:50 -0700413 // Is this actually a boot image?
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700414 if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800415 die("cannot load '%s': too short", kernel.c_str());
416 }
Elliott Hughesd6365a72017-05-08 18:04:49 -0700417 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700418 if (!g_cmdline.empty()) {
419 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), g_cmdline);
420 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800421
Elliott Hughes4089d342017-10-27 14:21:12 -0700422 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800423
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424 *sz = ksize;
425 return kdata;
426 }
427
Elliott Hughesfc797672015-04-07 20:12:50 -0700428 void* rdata = nullptr;
429 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700430 if (!ramdisk.empty()) {
431 rdata = load_file(ramdisk.c_str(), &rsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700432 if (rdata == nullptr) die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433 }
434
Elliott Hughesfc797672015-04-07 20:12:50 -0700435 void* sdata = nullptr;
436 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700437 if (!second_stage.empty()) {
438 sdata = load_file(second_stage.c_str(), &ssize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700439 if (sdata == nullptr) die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200440 }
441
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442 fprintf(stderr,"creating boot image...\n");
Elliott Hughes577e8b42018-04-05 16:12:47 -0700443 boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, rdata, rsize, sdata, ssize,
444 g_base_addr, g_boot_img_hdr, sz);
Elliott Hughes4089d342017-10-27 14:21:12 -0700445 if (bdata == nullptr) die("failed to create boot.img");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700446
Elliott Hughes577e8b42018-04-05 16:12:47 -0700447 if (!g_cmdline.empty()) bootimg_set_cmdline(bdata, g_cmdline);
448 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", *sz);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800449
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450 return bdata;
451}
452
Elliott Hughes23af1122017-11-10 08:42:17 -0800453static void* unzip_to_memory(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700454 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700455 ZipEntry zip_entry;
456 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700457 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Elliott Hughes4089d342017-10-27 14:21:12 -0700458 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459 }
460
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700461 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462
Elliott Hughes23af1122017-11-10 08:42:17 -0800463 fprintf(stderr, "extracting %s (%" PRId64 " MB) to RAM...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700464 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughes4089d342017-10-27 14:21:12 -0700465 if (data == nullptr) die("failed to allocate %" PRId64 " bytes for '%s'", *sz, entry_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700467 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
Elliott Hughes4089d342017-10-27 14:21:12 -0700468 if (error != 0) die("failed to extract '%s': %s", entry_name, ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000469
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 return data;
471}
472
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700473#if defined(_WIN32)
474
475// TODO: move this to somewhere it can be shared.
476
477#include <windows.h>
478
479// Windows' tmpfile(3) requires administrator rights because
480// it creates temporary files in the root directory.
481static FILE* win32_tmpfile() {
482 char temp_path[PATH_MAX];
483 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
484 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700485 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700486 }
487
488 char filename[PATH_MAX];
489 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700490 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700491 }
492
493 return fopen(filename, "w+bTD");
494}
495
496#define tmpfile win32_tmpfile
497
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000498static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700499 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000500}
501
Elliott Hughes855cdf82018-04-02 14:24:03 -0700502static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700503 // TODO: reimplement to avoid leaking a FILE*.
504 return fileno(tmpfile());
505}
506
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000507#else
508
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700509static std::string make_temporary_template() {
510 const char* tmpdir = getenv("TMPDIR");
511 if (tmpdir == nullptr) tmpdir = P_tmpdir;
512 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
513}
514
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000515static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700516 std::string result(make_temporary_template());
517 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700518 die("unable to create temporary directory with template %s: %s",
519 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000520 }
521 return result;
522}
523
Elliott Hughes855cdf82018-04-02 14:24:03 -0700524static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700525 std::string path_template(make_temporary_template());
526 int fd = mkstemp(&path_template[0]);
527 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700528 die("failed to create temporary file for %s with template %s: %s\n",
529 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700530 }
531 unlink(path_template.c_str());
532 return fd;
533}
534
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700535#endif
536
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000537static std::string create_fbemarker_tmpdir() {
538 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000539 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
540 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
541 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700542 die("unable to create FBE marker file %s locally: %s",
543 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000544 }
545 close(fd);
546 return dir;
547}
548
549static void delete_fbemarker_tmpdir(const std::string& dir) {
550 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
551 if (unlink(marker_file.c_str()) == -1) {
552 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
553 marker_file.c_str(), errno, strerror(errno));
554 return;
555 }
556 if (rmdir(dir.c_str()) == -1) {
557 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
558 dir.c_str(), errno, strerror(errno));
559 return;
560 }
561}
562
Elliott Hughes45964a82017-05-03 22:43:23 -0700563static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700564 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700565
Yusuke Sato07447542015-06-25 14:39:19 -0700566 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700567 ZipEntry zip_entry;
568 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
569 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000570 return -1;
571 }
572
Elliott Hughes23af1122017-11-10 08:42:17 -0800573 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700574 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800575 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700576 int error = ExtractEntryToFile(zip, &zip_entry, fd);
577 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800578 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700579 }
580
Elliott Hughes4089d342017-10-27 14:21:12 -0700581 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800582 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700583 }
584
Elliott Hughes23af1122017-11-10 08:42:17 -0800585 fprintf(stderr, " took %.3fs\n", now() - start);
586
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700587 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700588}
589
Elliott Hughes5620d222018-03-28 08:20:00 -0700590static char* strip(char* s) {
591 while (*s && isspace(*s)) s++;
592
593 int n = strlen(s);
594 while (n-- > 0) {
595 if (!isspace(s[n])) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800596 s[n] = 0;
597 }
598 return s;
599}
600
601#define MAX_OPTIONS 32
Aaron Wisnerdb511202018-06-26 15:38:35 -0500602static void check_requirement(char* line) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800603 char *val[MAX_OPTIONS];
Elliott Hughes5620d222018-03-28 08:20:00 -0700604 unsigned count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800605 char *x;
606 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800607
Elliott Hughes5620d222018-03-28 08:20:00 -0700608 // "require product=alpha|beta|gamma"
609 // "require version-bootloader=1234"
610 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
611 // "require partition-exists=vendor"
612
613 char* name = line;
614 const char* product = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800615 if (!strncmp(name, "reject ", 7)) {
616 name += 7;
617 invert = 1;
618 } else if (!strncmp(name, "require ", 8)) {
619 name += 8;
620 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700621 } else if (!strncmp(name, "require-for-product:", 20)) {
622 // Get the product and point name past it
Elliott Hughes5620d222018-03-28 08:20:00 -0700623 product = name + 20;
Wink Savilleb98762f2011-04-04 17:54:59 -0700624 name = strchr(name, ' ');
Elliott Hughes5620d222018-03-28 08:20:00 -0700625 if (!name) die("android-info.txt syntax error: %s", line);
Wink Savilleb98762f2011-04-04 17:54:59 -0700626 *name = 0;
627 name += 1;
628 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800629 }
630
631 x = strchr(name, '=');
Elliott Hughes5620d222018-03-28 08:20:00 -0700632 if (x == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800633 *x = 0;
634 val[0] = x + 1;
635
Elliott Hughes5620d222018-03-28 08:20:00 -0700636 name = strip(name);
637
638 // "require partition-exists=x" is a special case, added because of the trouble we had when
639 // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
640 // missing out new partitions. A device with new partitions can use "partition-exists" to
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700641 // override the fields `optional_if_no_image` and 'optional_if_no_partition' in the `images`
642 // array.
Elliott Hughes5620d222018-03-28 08:20:00 -0700643 if (!strcmp(name, "partition-exists")) {
644 const char* partition_name = val[0];
645 std::string has_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500646 if (!fb_getvar(std::string("has-slot:") + partition_name, &has_slot) ||
Elliott Hughes5620d222018-03-28 08:20:00 -0700647 (has_slot != "yes" && has_slot != "no")) {
648 die("device doesn't have required partition %s!", partition_name);
649 }
650 bool known_partition = false;
651 for (size_t i = 0; i < arraysize(images); ++i) {
652 if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700653 images[i].optional_if_no_image = false;
654 images[i].optional_if_no_partition = false;
Elliott Hughes5620d222018-03-28 08:20:00 -0700655 known_partition = true;
656 }
657 }
658 if (!known_partition) {
659 die("device requires partition %s which is not known to this version of fastboot",
660 partition_name);
661 }
662 return;
663 }
664
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800665 for(count = 1; count < MAX_OPTIONS; count++) {
666 x = strchr(val[count - 1],'|');
667 if (x == 0) break;
668 *x = 0;
669 val[count] = x + 1;
670 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800671
Elliott Hughes253c18d2015-03-18 22:47:09 -0700672 // Work around an unfortunate name mismatch.
Elliott Hughes5620d222018-03-28 08:20:00 -0700673 const char* var = name;
674 if (!strcmp(name, "board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800675
Elliott Hughes253c18d2015-03-18 22:47:09 -0700676 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
Elliott Hughes5620d222018-03-28 08:20:00 -0700677 if (out == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800678
Elliott Hughes5620d222018-03-28 08:20:00 -0700679 for (size_t i = 0; i < count; ++i) {
680 out[i] = xstrdup(strip(val[i]));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800681 }
682
Elliott Hughes5620d222018-03-28 08:20:00 -0700683 fb_queue_require(product, var, invert, count, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800684}
685
Aaron Wisnerdb511202018-06-26 15:38:35 -0500686static void check_requirements(char* data, int64_t sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700687 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700689 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 *s++ = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500691 check_requirement(data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692 data = s;
693 } else {
694 s++;
695 }
696 }
Aaron Wisnerdb511202018-06-26 15:38:35 -0500697 if (fb_execute_queue()) die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800698}
699
Elliott Hughesfc797672015-04-07 20:12:50 -0700700static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800701 fb_queue_notice("--------------------------------------------");
Elliott Hughes5620d222018-03-28 08:20:00 -0700702 fb_queue_display("Bootloader Version...", "version-bootloader");
703 fb_queue_display("Baseband Version.....", "version-baseband");
704 fb_queue_display("Serial Number........", "serialno");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800705 fb_queue_notice("--------------------------------------------");
706}
707
Tao Bao41cf35f2018-04-24 10:54:21 -0700708static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700709 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700710 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700711
Tao Bao41cf35f2018-04-24 10:54:21 -0700712 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
713 die("invalid max size %" PRId64, max_size);
714 }
715
Elliott Hughesfc797672015-04-07 20:12:50 -0700716 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700717 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700718
Elliott Hughes253c18d2015-03-18 22:47:09 -0700719 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700720 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700721
722 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700723 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700724
725 return out_s;
726}
727
Aaron Wisnerdb511202018-06-26 15:38:35 -0500728static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700729 std::string max_download_size;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500730 if (!fb_getvar("max-download-size", &max_download_size) ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700731 max_download_size.empty()) {
732 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700733 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700734 }
735
Elliott Hughes77c0e662015-11-02 15:51:12 -0800736 // Some bootloaders (angler, for example) send spurious whitespace too.
737 max_download_size = android::base::Trim(max_download_size);
738
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700739 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700740 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800741 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700742 return 0;
743 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700744 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700745 return limit;
746}
747
Aaron Wisnerdb511202018-06-26 15:38:35 -0500748static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700749 int64_t limit = sparse_limit;
750 if (limit == 0) {
751 // Unlimited, so see what the target device's limit is.
752 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700753 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500754 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700755 }
756 if (target_sparse_limit > 0) {
757 limit = target_sparse_limit;
758 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700759 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700760 }
761 }
762
763 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500764 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700765 }
766
767 return 0;
768}
769
Aaron Wisnerdb511202018-06-26 15:38:35 -0500770static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700771 int64_t sz = get_file_size(fd);
772 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700773 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700774 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700775
David Anderson32e376f2018-08-16 13:43:11 -0700776 if (sparse_file* s = sparse_file_import_auto(fd, false, false)) {
777 buf->image_size = sparse_file_len(s, false, false);
778 sparse_file_destroy(s);
779 } else {
780 buf->image_size = sz;
781 }
782
Elliott Hughesfc797672015-04-07 20:12:50 -0700783 lseek64(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500784 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700785 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700786 sparse_file** s = load_sparse_files(fd, limit);
787 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700788 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700789 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700790 buf->type = FB_BUFFER_SPARSE;
791 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700792 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500793 buf->type = FB_BUFFER_FD;
794 buf->data = nullptr;
795 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000796 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700797 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700798
Elliott Hughes53ec4952016-05-11 12:39:27 -0700799 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700800}
801
Aaron Wisnerdb511202018-06-26 15:38:35 -0500802static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500803 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
804
Elliott Hughes53ec4952016-05-11 12:39:27 -0700805 if (fd == -1) {
806 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700807 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500808
809 struct stat s;
810 if (fstat(fd, &s)) {
811 return false;
812 }
813 if (!S_ISREG(s.st_mode)) {
814 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
815 return false;
816 }
817
Aaron Wisnerdb511202018-06-26 15:38:35 -0500818 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700819}
820
David Zeuthenb6ea4352017-08-07 14:29:26 -0400821static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
822 // Buffer needs to be at least the size of the VBMeta struct which
823 // is 256 bytes.
824 if (buf->sz < 256) {
825 return;
826 }
827
Elliott Hughes855cdf82018-04-02 14:24:03 -0700828 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400829
830 std::string data;
831 if (!android::base::ReadFdToString(buf->fd, &data)) {
832 die("Failed reading from vbmeta");
833 }
834
835 // There's a 32-bit big endian |flags| field at offset 120 where
836 // bit 0 corresponds to disable-verity and bit 1 corresponds to
837 // disable-verification.
838 //
839 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
840 // the VBMeta struct.
841 if (g_disable_verity) {
842 data[123] |= 0x01;
843 }
844 if (g_disable_verification) {
845 data[123] |= 0x02;
846 }
847
848 if (!android::base::WriteStringToFd(data, fd)) {
849 die("Failed writing to modified vbmeta");
850 }
851 close(buf->fd);
852 buf->fd = fd;
853 lseek(fd, 0, SEEK_SET);
854}
855
Elliott Hughes5620d222018-03-28 08:20:00 -0700856static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700857{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700858 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700859
David Zeuthenb6ea4352017-08-07 14:29:26 -0400860 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
861 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700862 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400863 rewrite_vbmeta_buffer(buf);
864 }
865
Rom Lemarchand622810c2013-06-28 09:54:59 -0700866 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800867 case FB_BUFFER_SPARSE: {
868 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700869 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700870 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700871 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800872 sparse_files.emplace_back(*s, sz);
873 ++s;
874 }
875
876 for (size_t i = 0; i < sparse_files.size(); ++i) {
877 const auto& pair = sparse_files[i];
Elliott Hughes5620d222018-03-28 08:20:00 -0700878 fb_queue_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700879 }
880 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800881 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500882 case FB_BUFFER_FD:
Elliott Hughes5620d222018-03-28 08:20:00 -0700883 fb_queue_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700884 break;
885 default:
886 die("unknown buffer type: %d", buf->type);
887 }
888}
889
Aaron Wisnerdb511202018-06-26 15:38:35 -0500890static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700891 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500892 if (!fb_getvar("current-slot", &current_slot)) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700893 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700894}
895
Aaron Wisnerdb511202018-06-26 15:38:35 -0500896static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700897 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700898 int count = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500899 if (!fb_getvar("slot-count", &var) || !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700900 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700901 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700902 return count;
903}
904
Aaron Wisnerdb511202018-06-26 15:38:35 -0500905static bool supports_AB() {
906 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700907}
908
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700909// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700910static std::string get_other_slot(const std::string& current_slot, int count) {
911 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700912
Daniel Rosenberg80919472016-06-30 19:25:31 -0700913 char next = (current_slot[0] - 'a' + 1)%count + 'a';
914 return std::string(1, next);
915}
916
Aaron Wisnerdb511202018-06-26 15:38:35 -0500917static std::string get_other_slot(const std::string& current_slot) {
918 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -0700919}
920
Aaron Wisnerdb511202018-06-26 15:38:35 -0500921static std::string get_other_slot(int count) {
922 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700923}
924
Aaron Wisnerdb511202018-06-26 15:38:35 -0500925static std::string get_other_slot() {
926 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -0700927}
928
Aaron Wisnerdb511202018-06-26 15:38:35 -0500929static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700930 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700931 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800932 if (allow_all) {
933 return "all";
934 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500935 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700936 if (count > 0) {
937 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800938 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -0700939 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800940 }
941 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700942 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800943
Aaron Wisnerdb511202018-06-26 15:38:35 -0500944 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -0700945 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800946
Daniel Rosenberg80919472016-06-30 19:25:31 -0700947 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500948 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700949 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700950 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800951 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700952 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800953 }
954
Daniel Rosenberg80919472016-06-30 19:25:31 -0700955 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
956
957 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
958 for (int i=0; i<count; i++) {
959 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700960 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700961
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700962 exit(1);
963}
964
Aaron Wisnerdb511202018-06-26 15:38:35 -0500965static std::string verify_slot(const std::string& slot) {
966 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800967}
968
Aaron Wisnerdb511202018-06-26 15:38:35 -0500969static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700970 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800971 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700972 std::string current_slot;
973
Aaron Wisnerdb511202018-06-26 15:38:35 -0500974 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800975 /* If has-slot is not supported, the answer is no. */
976 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700977 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800978 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700979 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500980 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700981 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700982 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700983 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700984 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700985 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700986 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700987 }
988 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700989 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -0700990 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -0700991 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700992 }
993 func(part);
994 }
995}
996
David Pursell0b156632015-10-30 11:22:01 -0700997/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
998 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
999 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1000 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001001 */
Aaron Wisnerdb511202018-06-26 15:38:35 -05001002static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001003 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001004 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001005
Daniel Rosenberg80919472016-06-30 19:25:31 -07001006 if (slot == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001007 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001008 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001009 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001010 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001011 for (int i=0; i < get_slot_count(); i++) {
1012 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001013 }
1014 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001015 do_for_partition(part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001016 }
1017 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001018 do_for_partition(part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001019 }
1020}
1021
Aaron Wisnerdb511202018-06-26 15:38:35 -05001022static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001023 struct fastboot_buffer buf;
1024
Aaron Wisnerdb511202018-06-26 15:38:35 -05001025 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001026 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001027 }
1028 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001029}
1030
Elliott Hughes45964a82017-05-03 22:43:23 -07001031static void do_update_signature(ZipArchiveHandle zip, const char* filename) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001032 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001033 void* data = unzip_to_memory(zip, filename, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001034 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001035 fb_queue_download("signature", data, sz);
1036 fb_queue_command("signature", "installing signature");
1037}
1038
Daniel Rosenberg13454092016-06-27 19:43:11 -07001039// Sets slot_override as the active slot. If slot_override is blank,
1040// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001041static void set_active(const std::string& slot_override) {
1042 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001043
Daniel Rosenberg80919472016-06-30 19:25:31 -07001044 if (slot_override != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001045 fb_set_active(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001046 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001047 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001048 if (current_slot != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001049 fb_set_active(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001050 }
1051 }
1052}
1053
David Anderson32e376f2018-08-16 13:43:11 -07001054static bool is_userspace_fastboot() {
1055 std::string value;
1056 return fb_getvar("is-userspace", &value) && value == "yes";
1057}
1058
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001059static bool if_partition_exists(const std::string& partition, const std::string& slot) {
1060 std::string has_slot;
1061 std::string partition_name = partition;
1062
1063 if (fb_getvar("has-slot:" + partition, &has_slot) && has_slot == "yes") {
1064 if (slot == "") {
1065 std::string current_slot = get_current_slot();
1066 if (current_slot == "") {
1067 die("Failed to identify current slot");
1068 }
1069 partition_name += "_" + current_slot;
1070 } else {
1071 partition_name += "_" + slot;
1072 }
1073 }
1074 std::string partition_size;
1075 return fb_getvar("partition-size:" + partition_name, &partition_size);
1076}
1077
Aaron Wisnerdb511202018-06-26 15:38:35 -05001078static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001079 queue_info_dump();
1080
Wink Savilleb98762f2011-04-04 17:54:59 -07001081 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1082
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001083 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001084 int error = OpenArchive(filename, &zip);
1085 if (error != 0) {
1086 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001087 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001088
Elliott Hughesfc797672015-04-07 20:12:50 -07001089 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001090 void* data = unzip_to_memory(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001091 if (data == nullptr) {
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001092 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001093 }
1094
Aaron Wisnerdb511202018-06-26 15:38:35 -05001095 check_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001096
Alex Lightbb9b8a52016-06-29 09:26:44 -07001097 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001098 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001099 if (slot_override != "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001100 secondary = get_other_slot(slot_override);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001101 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001102 secondary = get_other_slot();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001103 }
1104 if (secondary == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001105 if (supports_AB()) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001106 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1107 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001108 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001109 }
1110 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001111 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001112 const char* slot = slot_override.c_str();
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001113 if (images[i].IsSecondary()) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001114 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001115 slot = secondary.c_str();
1116 } else {
1117 continue;
1118 }
1119 }
1120
Elliott Hughes253c18d2015-03-18 22:47:09 -07001121 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001122 if (fd == -1) {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001123 if (images[i].optional_if_no_image) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001124 continue; // An optional file is missing, so ignore it.
Elliott Hughesacdbe922015-06-02 21:37:05 -07001125 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001126 die("non-optional file %s missing", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001127 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001128
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001129 if (images[i].optional_if_no_partition &&
1130 !if_partition_exists(images[i].part_name, slot)) {
1131 continue;
1132 }
1133
Elliott Hughes253c18d2015-03-18 22:47:09 -07001134 fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001135 if (!load_buf_fd(fd, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001136 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1137 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001138
Elliott Hughes4089d342017-10-27 14:21:12 -07001139 auto update = [&](const std::string& partition) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001140 do_update_signature(zip, images[i].sig_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001141 flash_buf(partition.c_str(), &buf);
1142 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001143 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001144 * program exits.
1145 */
1146 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001147 do_for_partitions(images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001148 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001149
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001150 if (slot_override == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001151 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001152 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001153 set_active(slot_override);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001154 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001155
1156 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001157}
1158
Alex Lightbb9b8a52016-06-29 09:26:44 -07001159static void do_send_signature(const std::string& fn) {
1160 std::size_t extension_loc = fn.find(".img");
1161 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001162
Alex Lightbb9b8a52016-06-29 09:26:44 -07001163 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001164
1165 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001166 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001167 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001168
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001169 fb_queue_download("signature", data, sz);
1170 fb_queue_command("signature", "installing signature");
1171}
1172
David Anderson32e376f2018-08-16 13:43:11 -07001173static bool is_logical(const std::string& partition) {
1174 std::string value;
1175 return fb_getvar("is-logical:" + partition, &value) && value == "yes";
1176}
1177
David Anderson1d887432018-08-27 16:47:32 -07001178static void reboot_to_userspace_fastboot() {
1179 if (!fb_reboot_to_userspace()) {
1180 die("Must reboot to userspace fastboot to flash logical partitions");
1181 }
1182
1183 // Give the current connection time to close.
1184 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
1185
1186 fb_reinit(open_device());
1187}
1188
David Anderson32e376f2018-08-16 13:43:11 -07001189static void update_super_partition(bool force_wipe) {
1190 if (!if_partition_exists("super", "")) {
1191 return;
1192 }
1193 std::string image = find_item_given_name("super_empty.img");
1194 if (access(image.c_str(), R_OK) < 0) {
1195 return;
1196 }
1197
1198 if (!is_userspace_fastboot()) {
David Anderson1d887432018-08-27 16:47:32 -07001199 reboot_to_userspace_fastboot();
David Anderson32e376f2018-08-16 13:43:11 -07001200 }
1201
1202 int fd = open(image.c_str(), O_RDONLY);
1203 if (fd < 0) {
1204 die("could not open '%s': %s", image.c_str(), strerror(errno));
1205 }
1206 fb_queue_download_fd("super", fd, get_file_size(fd));
1207
1208 std::string command = "update-super:super";
1209 if (force_wipe) {
1210 command += ":wipe";
1211 }
1212 fb_queue_command(command, "Updating super partition");
1213
1214 // We need these commands to have finished before proceeding, since
1215 // otherwise "getvar is-logical" may not return a correct answer below.
1216 fb_execute_queue();
1217}
1218
1219static void do_flashall(const std::string& slot_override, bool skip_secondary, bool wipe) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001220 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001221 queue_info_dump();
1222
Wink Savilleb98762f2011-04-04 17:54:59 -07001223 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1224
Elliott Hughes45964a82017-05-03 22:43:23 -07001225 fname = find_item_given_name("android-info.txt");
Elliott Hughes2810d002016-04-25 14:31:18 -07001226 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001227
Elliott Hughesfc797672015-04-07 20:12:50 -07001228 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001229 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001230 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001231
Aaron Wisnerdb511202018-06-26 15:38:35 -05001232 check_requirements(reinterpret_cast<char*>(data), sz);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001233
Alex Lightbb9b8a52016-06-29 09:26:44 -07001234 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001235 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001236 if (slot_override != "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001237 secondary = get_other_slot(slot_override);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001238 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001239 secondary = get_other_slot();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001240 }
1241 if (secondary == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001242 if (supports_AB()) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001243 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1244 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001245 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001246 }
1247 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001248
David Anderson32e376f2018-08-16 13:43:11 -07001249 update_super_partition(wipe);
1250
1251 // List of partitions to flash and their slots.
1252 std::vector<std::pair<const Image*, std::string>> entries;
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001253 for (size_t i = 0; i < arraysize(images); i++) {
Mark Salyzyn8fb0fb82018-08-24 14:37:11 -07001254 if (!images[i].flashall) continue;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001255 const char* slot = NULL;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001256 if (images[i].IsSecondary()) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001257 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001258 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001259 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001260 }
1261 if (!slot) continue;
David Anderson32e376f2018-08-16 13:43:11 -07001262 entries.emplace_back(&images[i], slot);
1263
1264 // Resize any logical partition to 0, so each partition is reset to 0
1265 // extents, and will achieve more optimal allocation.
1266 auto resize_partition = [](const std::string& partition) -> void {
1267 if (is_logical(partition)) {
1268 fb_queue_resize_partition(partition, "0");
1269 }
1270 };
1271 do_for_partitions(images[i].part_name, slot, resize_partition, false);
1272 }
1273
1274 // Flash each partition in the list if it has a corresponding image.
1275 for (const auto& [image, slot] : entries) {
1276 fname = find_item_given_name(image->img_name);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001277 fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001278 if (!load_buf(fname.c_str(), &buf)) {
David Anderson32e376f2018-08-16 13:43:11 -07001279 if (image->optional_if_no_image) continue;
1280 die("could not load '%s': %s", image->img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001281 }
David Anderson32e376f2018-08-16 13:43:11 -07001282 if (image->optional_if_no_partition &&
1283 !if_partition_exists(image->part_name, slot)) {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001284 continue;
1285 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001286 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001287 do_send_signature(fname.c_str());
David Anderson32e376f2018-08-16 13:43:11 -07001288 if (is_logical(partition)) {
1289 fb_queue_resize_partition(partition, std::to_string(buf.image_size));
1290 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001291 flash_buf(partition.c_str(), &buf);
1292 };
David Anderson32e376f2018-08-16 13:43:11 -07001293 do_for_partitions(image->part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001294 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001295
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001296 if (slot_override == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001297 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001298 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001299 set_active(slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001300 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001301}
1302
Elliott Hughesd6365a72017-05-08 18:04:49 -07001303static std::string next_arg(std::vector<std::string>* args) {
1304 if (args->empty()) syntax_error("expected argument");
1305 std::string result = args->front();
1306 args->erase(args->begin());
1307 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001308}
1309
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001310static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001311 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001312
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001313 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001314 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001315 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001316 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001317 fb_queue_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001318}
1319
Connor O'Brience16a8a2017-02-06 14:39:31 -08001320static std::string fb_fix_numeric_var(std::string var) {
1321 // Some bootloaders (angler, for example), send spurious leading whitespace.
1322 var = android::base::Trim(var);
1323 // Some bootloaders (hammerhead, for example) use implicit hex.
1324 // This code used to use strtol with base 16.
1325 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1326 return var;
1327}
1328
Aaron Wisnerdb511202018-06-26 15:38:35 -05001329static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001330 std::string sizeString;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001331 if (!fb_getvar(name, &sizeString) || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001332 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001333 return 0;
1334 }
1335 sizeString = fb_fix_numeric_var(sizeString);
1336
1337 unsigned size;
1338 if (!android::base::ParseUint(sizeString, &size)) {
1339 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1340 return 0;
1341 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001342 if ((size & (size - 1)) != 0) {
1343 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001344 return 0;
1345 }
1346 return size;
1347}
1348
Aaron Wisnerdb511202018-06-26 15:38:35 -05001349static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001350 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001351 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001352 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001353 std::string partition_type, partition_size;
1354
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001355 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001356 const char* errMsg = nullptr;
1357 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001358 TemporaryFile output;
1359 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001360
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001361 unsigned int limit = INT_MAX;
1362 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001363 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001364 }
1365 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001366 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001367 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001368
Aaron Wisnerdb511202018-06-26 15:38:35 -05001369 if (!fb_getvar("partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001370 errMsg = "Can't determine partition type.\n";
1371 goto failed;
1372 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001373 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001374 if (partition_type != type_override) {
1375 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001376 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001377 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001378 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001379 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001380
Aaron Wisnerdb511202018-06-26 15:38:35 -05001381 if (!fb_getvar("partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001382 errMsg = "Unable to get partition size\n";
1383 goto failed;
1384 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001385 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001386 if (partition_size != size_override) {
1387 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001388 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001389 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001390 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001391 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001392 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001393
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001394 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001395 if (!gen) {
1396 if (skip_if_not_supported) {
1397 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001398 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001399 return;
1400 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001401 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1402 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001403 return;
1404 }
1405
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001406 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001407 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001408 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1409 return;
1410 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001411
Connor O'Brience16a8a2017-02-06 14:39:31 -08001412 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001413 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1414 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001415
Jin Qian4a335822017-04-18 16:23:18 -07001416 if (fs_generator_generate(gen, output.path, size, initial_dir,
1417 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001418 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001419 return;
1420 }
1421
Jin Qian4a335822017-04-18 16:23:18 -07001422 fd.reset(open(output.path, O_RDONLY));
1423 if (fd == -1) {
1424 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1425 return;
1426 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001427 if (!load_buf_fd(fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001428 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001429 return;
1430 }
1431 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001432 return;
1433
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001434failed:
1435 if (skip_if_not_supported) {
1436 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001437 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001438 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001439 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001440}
1441
Aaron Wisnerdb511202018-06-26 15:38:35 -05001442int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001443 bool wants_wipe = false;
1444 bool wants_reboot = false;
1445 bool wants_reboot_bootloader = false;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001446 bool wants_reboot_recovery = false;
1447 bool wants_reboot_fastboot = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001448 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001449 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001450 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001451 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001452 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001453 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001454 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001455 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001456 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001457
Elliott Hughes577e8b42018-04-05 16:12:47 -07001458 g_boot_img_hdr.kernel_addr = 0x00008000;
1459 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1460 g_boot_img_hdr.second_addr = 0x00f00000;
1461 g_boot_img_hdr.tags_addr = 0x00000100;
1462 g_boot_img_hdr.page_size = 2048;
1463
JP Abgrall7b8970c2013-03-07 17:06:41 -08001464 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001465 {"base", required_argument, 0, 0},
1466 {"cmdline", required_argument, 0, 0},
1467 {"disable-verification", no_argument, 0, 0},
1468 {"disable-verity", no_argument, 0, 0},
1469 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001470 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001471 {"kernel-offset", required_argument, 0, 0},
1472 {"os-patch-level", required_argument, 0, 0},
1473 {"os-version", required_argument, 0, 0},
1474 {"page-size", required_argument, 0, 0},
1475 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001476 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001477 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001478 {"skip-secondary", no_argument, 0, 0},
1479 {"slot", required_argument, 0, 0},
1480 {"tags-offset", required_argument, 0, 0},
1481 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001482 {"verbose", no_argument, 0, 'v'},
1483 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001484#if !defined(_WIN32)
1485 {"wipe-and-use-fbe", no_argument, 0, 0},
1486#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001487 {0, 0, 0, 0}
1488 };
Colin Cross8879f982012-05-22 17:53:34 -07001489
1490 serial = getenv("ANDROID_SERIAL");
1491
Elliott Hughes577e8b42018-04-05 16:12:47 -07001492 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001493 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001494 if (c == 0) {
1495 std::string name{longopts[longindex].name};
1496 if (name == "base") {
1497 g_base_addr = strtoul(optarg, 0, 16);
1498 } else if (name == "cmdline") {
1499 g_cmdline = optarg;
1500 } else if (name == "disable-verification") {
1501 g_disable_verification = true;
1502 } else if (name == "disable-verity") {
1503 g_disable_verity = true;
1504 } else if (name == "header-version") {
1505 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1506 } else if (name == "kernel-offset") {
1507 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1508 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001509 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001510 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001511 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001512 } else if (name == "page-size") {
1513 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1514 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1515 } else if (name == "ramdisk-offset") {
1516 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1517 } else if (name == "skip-reboot") {
1518 skip_reboot = true;
1519 } else if (name == "skip-secondary") {
1520 skip_secondary = true;
1521 } else if (name == "slot") {
1522 slot_override = optarg;
1523 } else if (name == "tags-offset") {
1524 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1525 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001526 setvbuf(stdout, nullptr, _IONBF, 0);
1527 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001528 } else if (name == "version") {
Elliott Hughes4d4f64f2017-05-08 11:30:34 -07001529 fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001530 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001531 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001532#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001533 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001534 wants_wipe = true;
1535 set_fbe_marker = true;
1536#endif
1537 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001538 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001539 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001540 } else {
1541 switch (c) {
1542 case 'a':
1543 wants_set_active = true;
1544 if (optarg) next_active = optarg;
1545 break;
1546 case 'h':
1547 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001548 case 'l':
1549 g_long_listing = true;
1550 break;
1551 case 's':
1552 serial = optarg;
1553 break;
1554 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001555 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1556 die("invalid sparse limit %s", optarg);
1557 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001558 break;
1559 case 'v':
1560 set_verbose();
1561 break;
1562 case 'w':
1563 wants_wipe = true;
1564 break;
1565 case '?':
1566 return 1;
1567 default:
1568 abort();
1569 }
Colin Cross8879f982012-05-22 17:53:34 -07001570 }
1571 }
1572
1573 argc -= optind;
1574 argv += optind;
1575
Elliott Hughesd6365a72017-05-08 18:04:49 -07001576 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001577
Colin Cross8fb6e062012-07-24 16:36:41 -07001578 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001579 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001580 return 0;
1581 }
1582
Colin Crossc7b75dc2012-08-29 18:17:06 -07001583 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001584 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001585 }
1586
David Pursell0b156632015-10-30 11:22:01 -07001587 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001588 if (transport == nullptr) {
1589 return 1;
1590 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001591 fastboot::FastBootDriver fb(transport);
1592 fb_init(fb);
David Pursell2ec418a2016-01-20 08:32:08 -08001593
Elliott Hughes5620d222018-03-28 08:20:00 -07001594 const double start = now();
1595
Aaron Wisnerdb511202018-06-26 15:38:35 -05001596 if (slot_override != "") slot_override = verify_slot(slot_override);
1597 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001598
1599 if (wants_set_active) {
1600 if (next_active == "") {
1601 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001602 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001603 if (fb_getvar("current-slot", &current_slot)) {
1604 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001605 } else {
1606 wants_set_active = false;
1607 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001608 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001609 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001610 }
1611 }
1612 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001613
Elliott Hughesd6365a72017-05-08 18:04:49 -07001614 std::vector<std::string> args(argv, argv + argc);
1615 while (!args.empty()) {
1616 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001617
Elliott Hughesd6365a72017-05-08 18:04:49 -07001618 if (command == "getvar") {
1619 std::string variable = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001620 fb_queue_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001621 } else if (command == "erase") {
1622 std::string partition = next_arg(&args);
1623 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001624 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001625 if (fb_getvar(std::string("partition-type:") + partition,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001626 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001627 fs_get_generator(partition_type) != nullptr) {
1628 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1629 partition_type.c_str());
1630 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001631
Elliott Hughes5620d222018-03-28 08:20:00 -07001632 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001633 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001634 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001635 } else if (android::base::StartsWith(command, "format")) {
1636 // Parsing for: "format[:[type][:[size]]]"
1637 // Some valid things:
1638 // - select only the size, and leave default fs type:
1639 // format::0x4000000 userdata
1640 // - default fs type and size:
1641 // format userdata
1642 // format:: userdata
1643 std::vector<std::string> pieces = android::base::Split(command, ":");
1644 std::string type_override;
1645 if (pieces.size() > 1) type_override = pieces[1].c_str();
1646 std::string size_override;
1647 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001648
Elliott Hughesd6365a72017-05-08 18:04:49 -07001649 std::string partition = next_arg(&args);
1650
1651 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001652 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001653 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001654 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001655 } else if (command == "signature") {
1656 std::string filename = next_arg(&args);
1657 data = load_file(filename.c_str(), &sz);
1658 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -07001659 if (sz != 256) die("signature must be 256 bytes (got %" PRId64 ")", sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001660 fb_queue_download("signature", data, sz);
1661 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001662 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001663 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001664
1665 if (args.size() == 1) {
1666 std::string what = next_arg(&args);
1667 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001668 wants_reboot = false;
1669 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001670 } else if (what == "recovery") {
1671 wants_reboot = false;
1672 wants_reboot_recovery = true;
1673 } else if (what == "fastboot") {
1674 wants_reboot = false;
1675 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001676 } else {
1677 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001678 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001679
Elliott Hughesca85df02015-02-25 10:02:00 -08001680 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001681 if (!args.empty()) syntax_error("junk after reboot command");
1682 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001683 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001684 } else if (command == "reboot-recovery") {
1685 wants_reboot_recovery = true;
1686 } else if (command == "reboot-fastboot") {
1687 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001688 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001689 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001690 } else if (command == "boot") {
1691 std::string kernel = next_arg(&args);
1692 std::string ramdisk;
1693 if (!args.empty()) ramdisk = next_arg(&args);
1694 std::string second_stage;
1695 if (!args.empty()) second_stage = next_arg(&args);
1696
Elliott Hughes577e8b42018-04-05 16:12:47 -07001697 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001698 fb_queue_download("boot.img", data, sz);
1699 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001700 } else if (command == "flash") {
1701 std::string pname = next_arg(&args);
1702
Elliott Hughes2810d002016-04-25 14:31:18 -07001703 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001704 if (!args.empty()) {
1705 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001706 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001707 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001708 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001709 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001710
1711 auto flash = [&](const std::string &partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001712 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001713 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001714 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001715 } else if (command == "flash:raw") {
1716 std::string partition = next_arg(&args);
1717 std::string kernel = next_arg(&args);
1718 std::string ramdisk;
1719 if (!args.empty()) ramdisk = next_arg(&args);
1720 std::string second_stage;
1721 if (!args.empty()) second_stage = next_arg(&args);
1722
Elliott Hughes577e8b42018-04-05 16:12:47 -07001723 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001724 auto flashraw = [&](const std::string& partition) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001725 fb_queue_flash(partition, data, sz);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001726 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001727 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001728 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001729 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001730 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
David Anderson32e376f2018-08-16 13:43:11 -07001731 do_flashall(slot_override, true, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001732 } else {
David Anderson32e376f2018-08-16 13:43:11 -07001733 do_flashall(slot_override, skip_secondary, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001734 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001735 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001736 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001737 bool slot_all = (slot_override == "all");
1738 if (slot_all) {
1739 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1740 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001741 std::string filename = "update.zip";
1742 if (!args.empty()) {
1743 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001744 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001745 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001746 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001747 } else if (command == "set_active") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001748 std::string slot = verify_slot(next_arg(&args), false);
Elliott Hughes5620d222018-03-28 08:20:00 -07001749 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001750 } else if (command == "stage") {
1751 std::string filename = next_arg(&args);
1752
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001753 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001754 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001755 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001756 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001757 fb_queue_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001758 } else if (command == "get_staged") {
1759 std::string filename = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001760 fb_queue_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001761 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001762 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001763 } else if (command == "flashing") {
1764 if (args.empty()) {
1765 syntax_error("missing 'flashing' command");
1766 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1767 args[0] == "unlock_critical" ||
1768 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001769 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001770 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001771 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001772 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001773 }
David Anderson0d4277d2018-07-31 13:27:37 -07001774 } else if (command == "create-logical-partition") {
1775 std::string partition = next_arg(&args);
1776 std::string size = next_arg(&args);
1777 fb_queue_create_partition(partition, size);
1778 } else if (command == "delete-logical-partition") {
1779 std::string partition = next_arg(&args);
1780 fb_queue_delete_partition(partition);
1781 } else if (command == "resize-logical-partition") {
1782 std::string partition = next_arg(&args);
1783 std::string size = next_arg(&args);
1784 fb_queue_resize_partition(partition, size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001785 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001786 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001787 }
1788 }
1789
1790 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001791 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1792 for (const auto& partition : partitions) {
1793 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001794 if (!fb_getvar(std::string{"partition-type:"} + partition, &partition_type)) continue;
Paul Crowley4d170062018-04-24 17:06:30 -07001795 if (partition_type.empty()) continue;
1796 fb_queue_erase(partition);
1797 if (partition == "userdata" && set_fbe_marker) {
1798 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1799 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001800 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001801 delete_fbemarker_tmpdir(initial_userdata_dir);
1802 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001803 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001804 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001805 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001806 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001807 if (wants_set_active) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001808 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001809 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001810 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001811 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001812 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001813 } else if (wants_reboot_bootloader) {
1814 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001815 fb_queue_wait_for_disconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001816 } else if (wants_reboot_recovery) {
1817 fb_queue_command("reboot-recovery", "rebooting into recovery");
1818 fb_queue_wait_for_disconnect();
1819 } else if (wants_reboot_fastboot) {
1820 fb_queue_command("reboot-fastboot", "rebooting into fastboot");
1821 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001822 }
1823
Aaron Wisnerdb511202018-06-26 15:38:35 -05001824 int status = fb_execute_queue() ? EXIT_FAILURE : EXIT_SUCCESS;
Elliott Hughes5620d222018-03-28 08:20:00 -07001825 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
1826 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001827}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001828
Aaron Wisnerdb511202018-06-26 15:38:35 -05001829void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001830 unsigned year, month, day;
1831 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1832 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1833 }
1834 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1835 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1836 hdr->SetOsPatchLevel(year, month);
1837}
1838
Aaron Wisnerdb511202018-06-26 15:38:35 -05001839void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001840 unsigned major = 0, minor = 0, patch = 0;
1841 std::vector<std::string> versions = android::base::Split(arg, ".");
1842 if (versions.size() < 1 || versions.size() > 3 ||
1843 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1844 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1845 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1846 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1847 syntax_error("bad OS version: %s", arg);
1848 }
1849 hdr->SetOsVersion(major, minor, patch);
1850}