blob: 1aef567f20567cdb44675049b96faeabf8be7ab5 [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>
Dan Willemsenab971b52018-08-29 14:58:02 -070058#include <build/version.h>
59#include <platform_tools_version.h>
Colin Crossf8387882012-05-24 17:18:41 -070060#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070061#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062
Elliott Hughes253c18d2015-03-18 22:47:09 -070063#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080064#include "diagnose_usb.h"
Aaron Wisnerdb511202018-06-26 15:38:35 -050065#include "engine.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070066#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080067#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070068#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080069#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070070#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071
Chris Fries0ea946c2017-04-12 10:25:57 -050072using android::base::unique_fd;
73
Colin Crossf8387882012-05-24 17:18:41 -070074#ifndef O_BINARY
75#define O_BINARY 0
76#endif
77
Wink Savilleb98762f2011-04-04 17:54:59 -070078char cur_product[FB_RESPONSE_SZ + 1];
79
David Pursell2ec418a2016-01-20 08:32:08 -080080static const char* serial = nullptr;
Elliott Hughes577e8b42018-04-05 16:12:47 -070081
Elliott Hughes577e8b42018-04-05 16:12:47 -070082static bool g_long_listing = false;
Chris Fries6a999712017-04-04 09:52:47 -050083// Don't resparse files in too-big chunks.
84// libsparse will support INT_MAX, but this results in large allocations, so
85// let's keep it at 1GB to avoid memory pressure on the host.
86static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Elliott Hughes542370d2018-04-19 19:49:44 -070087static uint64_t sparse_limit = 0;
Colin Crossf8387882012-05-24 17:18:41 -070088static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089
Elliott Hughes577e8b42018-04-05 16:12:47 -070090static unsigned g_base_addr = 0x10000000;
91static boot_img_hdr_v1 g_boot_img_hdr = {};
92static std::string g_cmdline;
JP Abgrall7b8970c2013-03-07 17:06:41 -080093
David Zeuthenb6ea4352017-08-07 14:29:26 -040094static bool g_disable_verity = false;
95static bool g_disable_verification = false;
96
Paul Crowley8f7f56e2015-11-27 09:29:37 +000097static const std::string convert_fbe_marker_filename("convert_fbe");
98
Rom Lemarchand622810c2013-06-28 09:54:59 -070099enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -0500100 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -0700101 FB_BUFFER_SPARSE,
102};
103
104struct fastboot_buffer {
105 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700106 void* data;
107 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500108 int fd;
David Anderson32e376f2018-08-16 13:43:11 -0700109 int64_t image_size;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700110};
111
David Anderson0debda02018-08-28 13:54:03 -0700112enum class ImageType {
113 // Must be flashed for device to boot into the kernel.
114 BootCritical,
115 // Normal partition to be flashed during "flashall".
116 Normal,
117 // Partition that is never flashed during "flashall".
118 Extra
119};
120
David Anderson32e376f2018-08-16 13:43:11 -0700121struct Image {
Elliott Hughes45964a82017-05-03 22:43:23 -0700122 const char* nickname;
123 const char* img_name;
124 const char* sig_name;
125 const char* part_name;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700126 bool optional_if_no_image;
David Anderson0debda02018-08-28 13:54:03 -0700127 ImageType type;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700128 bool IsSecondary() const { return nickname == nullptr; }
David Anderson32e376f2018-08-16 13:43:11 -0700129};
130
131static Image images[] = {
Dario Frenic7ea1af2018-05-25 16:07:19 +0100132 // clang-format off
David Anderson0debda02018-08-28 13:54:03 -0700133 { "boot", "boot.img", "boot.sig", "boot", false, ImageType::BootCritical },
134 { nullptr, "boot_other.img", "boot.sig", "boot", true, ImageType::Normal },
135 { "cache", "cache.img", "cache.sig", "cache", true, ImageType::Extra },
136 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, ImageType::BootCritical },
137 { "dts", "dt.img", "dt.sig", "dts", true, ImageType::BootCritical },
138 { "odm", "odm.img", "odm.sig", "odm", true, ImageType::Normal },
139 { "product", "product.img", "product.sig", "product", true, ImageType::Normal },
Dario Freniab5583b2018-08-17 01:01:25 +0100140 { "product_services",
141 "product_services.img",
142 "product_services.sig",
David Anderson8cdea7f2018-08-06 15:36:00 -0700143 "product_services",
David Anderson0debda02018-08-28 13:54:03 -0700144 true, ImageType::Normal },
145 { "recovery", "recovery.img", "recovery.sig", "recovery", true, ImageType::BootCritical },
146 { "super", "super.img", "super.sig", "super", true, ImageType::Extra },
147 { "system", "system.img", "system.sig", "system", false, ImageType::Normal },
148 { nullptr, "system_other.img", "system.sig", "system", true, ImageType::Normal },
149 { "userdata", "userdata.img", "userdata.sig", "userdata", true, ImageType::Extra },
150 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, ImageType::BootCritical },
151 { "vendor", "vendor.img", "vendor.sig", "vendor", true, ImageType::Normal },
152 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, ImageType::Normal },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100153 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700154};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700155
David Anderson0debda02018-08-28 13:54:03 -0700156static std::string find_item_given_name(const std::string& img_name) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700157 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700158 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700159 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700160 }
David Anderson0debda02018-08-28 13:54:03 -0700161 return std::string(dir) + "/" + img_name;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700162}
163
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700164static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700165 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700166 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700167 return find_item_given_name(images[i].img_name);
168 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169 }
170
Elliott Hughesd6365a72017-05-08 18:04:49 -0700171 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700172 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173}
174
Elliott Hughesfc797672015-04-07 20:12:50 -0700175static int64_t get_file_size(int fd) {
176 struct stat sb;
177 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700178}
179
Elliott Hughesfc797672015-04-07 20:12:50 -0700180static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800181 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700182 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183
Elliott Hughesfc797672015-04-07 20:12:50 -0700184 *sz = get_file_size(fd);
185 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700186 goto oops;
187 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188
Elliott Hughesfc797672015-04-07 20:12:50 -0700189 data = (char*) malloc(*sz);
190 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
Elliott Hughesfc797672015-04-07 20:12:50 -0700192 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 close(fd);
194
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 return data;
196
197oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800198 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 close(fd);
200 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800201 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 return 0;
203}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204
Elliott Hughes2810d002016-04-25 14:31:18 -0700205static void* load_file(const std::string& path, int64_t* sz) {
206 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700207 if (fd == -1) return nullptr;
208 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700209}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210
Elliott Hughesfc797672015-04-07 20:12:50 -0700211static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700212 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
213 return -1;
214 }
215
Scott Anderson13081c62012-04-06 12:39:30 -0700216 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700218 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
219 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 return 0;
221}
222
Elliott Hughesfc797672015-04-07 20:12:50 -0700223static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800224 return match_fastboot_with_serial(info, serial);
225}
226
Elliott Hughesfc797672015-04-07 20:12:50 -0700227static int list_devices_callback(usb_ifc_info* info) {
228 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800229 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700230 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800231 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700232 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 if (!serial[0]) {
234 serial = "????????????";
235 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700236 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700237 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800238 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700239 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800240 printf("%-22s fastboot", serial.c_str());
241 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700242 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800243 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 }
245
246 return -1;
247}
248
David Pursell2ec418a2016-01-20 08:32:08 -0800249// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
250// a specific device, otherwise the first USB device found will be used.
251//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700252// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800253// Otherwise it blocks until the target is available.
254//
255// The returned Transport is a singleton, so multiple calls to this function will return the same
256// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700257static Transport* open_device() {
David Pursell2ec418a2016-01-20 08:32:08 -0800258 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259
David Pursell4601c972016-02-05 15:35:09 -0800260 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800261 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800262 int port = 0;
263 if (serial != nullptr) {
264 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800265
David Pursell4601c972016-02-05 15:35:09 -0800266 if (android::base::StartsWith(serial, "tcp:")) {
267 protocol = Socket::Protocol::kTcp;
268 port = tcp::kDefaultPort;
269 net_address = serial + strlen("tcp:");
270 } else if (android::base::StartsWith(serial, "udp:")) {
271 protocol = Socket::Protocol::kUdp;
272 port = udp::kDefaultPort;
273 net_address = serial + strlen("udp:");
274 }
275
276 if (net_address != nullptr) {
277 std::string error;
278 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700279 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800280 }
David Pursell2ec418a2016-01-20 08:32:08 -0800281 }
282 }
283
David Anderson1d887432018-08-27 16:47:32 -0700284 Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800285 while (true) {
286 if (!host.empty()) {
287 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800288 if (protocol == Socket::Protocol::kTcp) {
289 transport = tcp::Connect(host, port, &error).release();
290 } else if (protocol == Socket::Protocol::kUdp) {
291 transport = udp::Connect(host, port, &error).release();
292 }
293
David Pursell2ec418a2016-01-20 08:32:08 -0800294 if (transport == nullptr && announce) {
295 fprintf(stderr, "error: %s\n", error.c_str());
296 }
297 } else {
298 transport = usb_open(match_fastboot);
299 }
300
301 if (transport != nullptr) {
302 return transport;
303 }
304
David Pursell0b156632015-10-30 11:22:01 -0700305 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800306 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700307 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800309 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310 }
311}
312
Elliott Hughesfc797672015-04-07 20:12:50 -0700313static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314 // We don't actually open a USB device here,
315 // just getting our callback called so we can
316 // list all the connected devices.
317 usb_open(list_devices_callback);
318}
319
Elliott Hughesd6365a72017-05-08 18:04:49 -0700320static void syntax_error(const char* fmt, ...) {
321 fprintf(stderr, "fastboot: usage: ");
322
323 va_list ap;
324 va_start(ap, fmt);
325 vfprintf(stderr, fmt, ap);
326 va_end(ap);
327
328 fprintf(stderr, "\n");
329 exit(1);
330}
331
332static int show_help() {
333 // clang-format off
334 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700335// 1 2 3 4 5 6 7 8
336// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
Elliott Hughesb76188f2018-04-03 13:30:18 -0700337 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700339 "flashing:\n"
340 " update ZIP Flash all partitions from an update.zip package.\n"
341 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
342 " On A/B devices, flashed slot is set as active.\n"
343 " Secondary images may be flashed to inactive slot.\n"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700344 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
345 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700346 "\n"
347 "basics:\n"
348 " devices [-l] List devices in bootloader (-l: with device paths).\n"
349 " getvar NAME Display given bootloader variable.\n"
350 " reboot [bootloader] Reboot device.\n"
351 "\n"
352 "locking/unlocking:\n"
353 " flashing lock|unlock Lock/unlock partitions for flashing\n"
354 " flashing lock_critical|unlock_critical\n"
355 " Lock/unlock 'critical' bootloader partitions.\n"
356 " flashing get_unlock_ability\n"
357 " Check whether unlocking is allowed (1) or not(0).\n"
358 "\n"
359 "advanced:\n"
360 " erase PARTITION Erase a flash partition.\n"
361 " format[:FS_TYPE[:SIZE]] PARTITION\n"
362 " Format a flash partition.\n"
363 " set_active SLOT Set the active slot.\n"
364 " oem [COMMAND...] Execute OEM-specific command.\n"
365 "\n"
366 "boot image:\n"
367 " boot KERNEL [RAMDISK [SECOND]]\n"
368 " Download and boot kernel from RAM.\n"
369 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
370 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700371 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700372 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
373 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
374 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
375 " --tags-offset Set tags offset (default: 0x00000100).\n"
376 " --page-size BYTES Set flash page size (default: 2048).\n"
377 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700378 " --os-version MAJOR[.MINOR[.PATCH]]\n"
379 " Set boot image OS version (default: 0.0.0).\n"
380 " --os-patch-level YYYY-MM-DD\n"
381 " Set boot image OS security patch level.\n"
382 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700383 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700384 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700385 //" continue Continue with autoboot.\n"
386 //"\n"
387 "Android Things:\n"
388 " stage IN_FILE Sends given file to stage for the next command.\n"
389 " 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 -0800390 "\n"
391 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700392 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700393 " -s SERIAL Specify a USB device.\n"
394 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700395 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700396 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
397 " non-current slot (default: current active slot).\n"
398 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
399 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
400 " --skip-reboot Don't reboot device after flashing.\n"
401 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
402 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000403#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700404 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000405#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700406 // TODO: remove --unbuffered?
407 " --unbuffered Don't buffer input or output.\n"
408 " --verbose, -v Verbose output.\n"
409 " --version Display version.\n"
410 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700412 // clang-format off
413 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000415
Elliott Hughesd6365a72017-05-08 18:04:49 -0700416static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
Elliott Hughes577e8b42018-04-05 16:12:47 -0700417 const std::string& second_stage, int64_t* sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700418 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700419 void* kdata = load_file(kernel.c_str(), &ksize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700420 if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800421
Elliott Hughesfc797672015-04-07 20:12:50 -0700422 // Is this actually a boot image?
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700423 if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800424 die("cannot load '%s': too short", kernel.c_str());
425 }
Elliott Hughesd6365a72017-05-08 18:04:49 -0700426 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700427 if (!g_cmdline.empty()) {
428 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), g_cmdline);
429 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800430
Elliott Hughes4089d342017-10-27 14:21:12 -0700431 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800432
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433 *sz = ksize;
434 return kdata;
435 }
436
Elliott Hughesfc797672015-04-07 20:12:50 -0700437 void* rdata = nullptr;
438 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700439 if (!ramdisk.empty()) {
440 rdata = load_file(ramdisk.c_str(), &rsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700441 if (rdata == nullptr) die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442 }
443
Elliott Hughesfc797672015-04-07 20:12:50 -0700444 void* sdata = nullptr;
445 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700446 if (!second_stage.empty()) {
447 sdata = load_file(second_stage.c_str(), &ssize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700448 if (sdata == nullptr) die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200449 }
450
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451 fprintf(stderr,"creating boot image...\n");
Elliott Hughes577e8b42018-04-05 16:12:47 -0700452 boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, rdata, rsize, sdata, ssize,
453 g_base_addr, g_boot_img_hdr, sz);
Elliott Hughes4089d342017-10-27 14:21:12 -0700454 if (bdata == nullptr) die("failed to create boot.img");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700455
Elliott Hughes577e8b42018-04-05 16:12:47 -0700456 if (!g_cmdline.empty()) bootimg_set_cmdline(bdata, g_cmdline);
457 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", *sz);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800458
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459 return bdata;
460}
461
Elliott Hughes23af1122017-11-10 08:42:17 -0800462static void* unzip_to_memory(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700463 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700464 ZipEntry zip_entry;
465 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700466 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Elliott Hughes4089d342017-10-27 14:21:12 -0700467 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 }
469
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700470 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471
Elliott Hughes23af1122017-11-10 08:42:17 -0800472 fprintf(stderr, "extracting %s (%" PRId64 " MB) to RAM...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700473 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughes4089d342017-10-27 14:21:12 -0700474 if (data == nullptr) die("failed to allocate %" PRId64 " bytes for '%s'", *sz, entry_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700476 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
Elliott Hughes4089d342017-10-27 14:21:12 -0700477 if (error != 0) die("failed to extract '%s': %s", entry_name, ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000478
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 return data;
480}
481
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700482#if defined(_WIN32)
483
484// TODO: move this to somewhere it can be shared.
485
486#include <windows.h>
487
488// Windows' tmpfile(3) requires administrator rights because
489// it creates temporary files in the root directory.
490static FILE* win32_tmpfile() {
491 char temp_path[PATH_MAX];
492 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
493 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700494 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700495 }
496
497 char filename[PATH_MAX];
498 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700499 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700500 }
501
502 return fopen(filename, "w+bTD");
503}
504
505#define tmpfile win32_tmpfile
506
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000507static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700508 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000509}
510
Elliott Hughes855cdf82018-04-02 14:24:03 -0700511static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700512 // TODO: reimplement to avoid leaking a FILE*.
513 return fileno(tmpfile());
514}
515
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000516#else
517
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700518static std::string make_temporary_template() {
519 const char* tmpdir = getenv("TMPDIR");
520 if (tmpdir == nullptr) tmpdir = P_tmpdir;
521 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
522}
523
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000524static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700525 std::string result(make_temporary_template());
526 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700527 die("unable to create temporary directory with template %s: %s",
528 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000529 }
530 return result;
531}
532
Elliott Hughes855cdf82018-04-02 14:24:03 -0700533static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700534 std::string path_template(make_temporary_template());
535 int fd = mkstemp(&path_template[0]);
536 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700537 die("failed to create temporary file for %s with template %s: %s\n",
538 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700539 }
540 unlink(path_template.c_str());
541 return fd;
542}
543
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700544#endif
545
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000546static std::string create_fbemarker_tmpdir() {
547 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000548 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
549 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
550 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700551 die("unable to create FBE marker file %s locally: %s",
552 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000553 }
554 close(fd);
555 return dir;
556}
557
558static void delete_fbemarker_tmpdir(const std::string& dir) {
559 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
560 if (unlink(marker_file.c_str()) == -1) {
561 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
562 marker_file.c_str(), errno, strerror(errno));
563 return;
564 }
565 if (rmdir(dir.c_str()) == -1) {
566 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
567 dir.c_str(), errno, strerror(errno));
568 return;
569 }
570}
571
Elliott Hughes45964a82017-05-03 22:43:23 -0700572static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700573 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700574
Yusuke Sato07447542015-06-25 14:39:19 -0700575 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700576 ZipEntry zip_entry;
577 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
578 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
David Andersoncf444f32018-08-29 14:15:49 -0700579 errno = ENOENT;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000580 return -1;
581 }
582
Elliott Hughes23af1122017-11-10 08:42:17 -0800583 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700584 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800585 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700586 int error = ExtractEntryToFile(zip, &zip_entry, fd);
587 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800588 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700589 }
590
Elliott Hughes4089d342017-10-27 14:21:12 -0700591 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800592 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700593 }
594
Elliott Hughes23af1122017-11-10 08:42:17 -0800595 fprintf(stderr, " took %.3fs\n", now() - start);
596
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700597 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700598}
599
Elliott Hughes5620d222018-03-28 08:20:00 -0700600static char* strip(char* s) {
601 while (*s && isspace(*s)) s++;
602
603 int n = strlen(s);
604 while (n-- > 0) {
605 if (!isspace(s[n])) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800606 s[n] = 0;
607 }
608 return s;
609}
610
611#define MAX_OPTIONS 32
Aaron Wisnerdb511202018-06-26 15:38:35 -0500612static void check_requirement(char* line) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800613 char *val[MAX_OPTIONS];
Elliott Hughes5620d222018-03-28 08:20:00 -0700614 unsigned count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800615 char *x;
616 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800617
Elliott Hughes5620d222018-03-28 08:20:00 -0700618 // "require product=alpha|beta|gamma"
619 // "require version-bootloader=1234"
620 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
621 // "require partition-exists=vendor"
622
623 char* name = line;
624 const char* product = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800625 if (!strncmp(name, "reject ", 7)) {
626 name += 7;
627 invert = 1;
628 } else if (!strncmp(name, "require ", 8)) {
629 name += 8;
630 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700631 } else if (!strncmp(name, "require-for-product:", 20)) {
632 // Get the product and point name past it
Elliott Hughes5620d222018-03-28 08:20:00 -0700633 product = name + 20;
Wink Savilleb98762f2011-04-04 17:54:59 -0700634 name = strchr(name, ' ');
Elliott Hughes5620d222018-03-28 08:20:00 -0700635 if (!name) die("android-info.txt syntax error: %s", line);
Wink Savilleb98762f2011-04-04 17:54:59 -0700636 *name = 0;
637 name += 1;
638 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800639 }
640
641 x = strchr(name, '=');
Elliott Hughes5620d222018-03-28 08:20:00 -0700642 if (x == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800643 *x = 0;
644 val[0] = x + 1;
645
Elliott Hughes5620d222018-03-28 08:20:00 -0700646 name = strip(name);
647
648 // "require partition-exists=x" is a special case, added because of the trouble we had when
649 // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
650 // missing out new partitions. A device with new partitions can use "partition-exists" to
David Anderson0debda02018-08-28 13:54:03 -0700651 // override the fields `optional_if_no_image` in the `images` array.
Elliott Hughes5620d222018-03-28 08:20:00 -0700652 if (!strcmp(name, "partition-exists")) {
653 const char* partition_name = val[0];
654 std::string has_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500655 if (!fb_getvar(std::string("has-slot:") + partition_name, &has_slot) ||
Elliott Hughes5620d222018-03-28 08:20:00 -0700656 (has_slot != "yes" && has_slot != "no")) {
657 die("device doesn't have required partition %s!", partition_name);
658 }
659 bool known_partition = false;
660 for (size_t i = 0; i < arraysize(images); ++i) {
661 if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700662 images[i].optional_if_no_image = false;
Elliott Hughes5620d222018-03-28 08:20:00 -0700663 known_partition = true;
664 }
665 }
666 if (!known_partition) {
667 die("device requires partition %s which is not known to this version of fastboot",
668 partition_name);
669 }
670 return;
671 }
672
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800673 for(count = 1; count < MAX_OPTIONS; count++) {
674 x = strchr(val[count - 1],'|');
675 if (x == 0) break;
676 *x = 0;
677 val[count] = x + 1;
678 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800679
Elliott Hughes253c18d2015-03-18 22:47:09 -0700680 // Work around an unfortunate name mismatch.
Elliott Hughes5620d222018-03-28 08:20:00 -0700681 const char* var = name;
682 if (!strcmp(name, "board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800683
Elliott Hughes253c18d2015-03-18 22:47:09 -0700684 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
Elliott Hughes5620d222018-03-28 08:20:00 -0700685 if (out == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686
Elliott Hughes5620d222018-03-28 08:20:00 -0700687 for (size_t i = 0; i < count; ++i) {
688 out[i] = xstrdup(strip(val[i]));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800689 }
690
Elliott Hughes5620d222018-03-28 08:20:00 -0700691 fb_queue_require(product, var, invert, count, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692}
693
Aaron Wisnerdb511202018-06-26 15:38:35 -0500694static void check_requirements(char* data, int64_t sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700695 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800696 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700697 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800698 *s++ = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500699 check_requirement(data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800700 data = s;
701 } else {
702 s++;
703 }
704 }
Aaron Wisnerdb511202018-06-26 15:38:35 -0500705 if (fb_execute_queue()) die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800706}
707
Elliott Hughesfc797672015-04-07 20:12:50 -0700708static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800709 fb_queue_notice("--------------------------------------------");
Elliott Hughes5620d222018-03-28 08:20:00 -0700710 fb_queue_display("Bootloader Version...", "version-bootloader");
711 fb_queue_display("Baseband Version.....", "version-baseband");
712 fb_queue_display("Serial Number........", "serialno");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800713 fb_queue_notice("--------------------------------------------");
714}
715
Tao Bao41cf35f2018-04-24 10:54:21 -0700716static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700717 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700718 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700719
Tao Bao41cf35f2018-04-24 10:54:21 -0700720 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
721 die("invalid max size %" PRId64, max_size);
722 }
723
Elliott Hughesfc797672015-04-07 20:12:50 -0700724 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700725 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700726
Elliott Hughes253c18d2015-03-18 22:47:09 -0700727 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700728 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700729
730 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700731 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700732
733 return out_s;
734}
735
Aaron Wisnerdb511202018-06-26 15:38:35 -0500736static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700737 std::string max_download_size;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500738 if (!fb_getvar("max-download-size", &max_download_size) ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700739 max_download_size.empty()) {
740 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700741 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700742 }
743
Elliott Hughes77c0e662015-11-02 15:51:12 -0800744 // Some bootloaders (angler, for example) send spurious whitespace too.
745 max_download_size = android::base::Trim(max_download_size);
746
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700747 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700748 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800749 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700750 return 0;
751 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700752 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700753 return limit;
754}
755
Aaron Wisnerdb511202018-06-26 15:38:35 -0500756static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700757 int64_t limit = sparse_limit;
758 if (limit == 0) {
759 // Unlimited, so see what the target device's limit is.
760 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700761 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500762 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700763 }
764 if (target_sparse_limit > 0) {
765 limit = target_sparse_limit;
766 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700767 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700768 }
769 }
770
771 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500772 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700773 }
774
775 return 0;
776}
777
Aaron Wisnerdb511202018-06-26 15:38:35 -0500778static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700779 int64_t sz = get_file_size(fd);
780 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700781 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700782 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700783
David Anderson32e376f2018-08-16 13:43:11 -0700784 if (sparse_file* s = sparse_file_import_auto(fd, false, false)) {
785 buf->image_size = sparse_file_len(s, false, false);
786 sparse_file_destroy(s);
787 } else {
788 buf->image_size = sz;
789 }
790
Elliott Hughesfc797672015-04-07 20:12:50 -0700791 lseek64(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500792 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700793 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700794 sparse_file** s = load_sparse_files(fd, limit);
795 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700796 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700797 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700798 buf->type = FB_BUFFER_SPARSE;
799 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700800 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500801 buf->type = FB_BUFFER_FD;
802 buf->data = nullptr;
803 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000804 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700805 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700806
Elliott Hughes53ec4952016-05-11 12:39:27 -0700807 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700808}
809
Aaron Wisnerdb511202018-06-26 15:38:35 -0500810static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500811 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
812
Elliott Hughes53ec4952016-05-11 12:39:27 -0700813 if (fd == -1) {
814 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700815 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500816
817 struct stat s;
818 if (fstat(fd, &s)) {
819 return false;
820 }
821 if (!S_ISREG(s.st_mode)) {
822 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
823 return false;
824 }
825
Aaron Wisnerdb511202018-06-26 15:38:35 -0500826 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700827}
828
David Zeuthenb6ea4352017-08-07 14:29:26 -0400829static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
830 // Buffer needs to be at least the size of the VBMeta struct which
831 // is 256 bytes.
832 if (buf->sz < 256) {
833 return;
834 }
835
Elliott Hughes855cdf82018-04-02 14:24:03 -0700836 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400837
838 std::string data;
839 if (!android::base::ReadFdToString(buf->fd, &data)) {
840 die("Failed reading from vbmeta");
841 }
842
843 // There's a 32-bit big endian |flags| field at offset 120 where
844 // bit 0 corresponds to disable-verity and bit 1 corresponds to
845 // disable-verification.
846 //
847 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
848 // the VBMeta struct.
849 if (g_disable_verity) {
850 data[123] |= 0x01;
851 }
852 if (g_disable_verification) {
853 data[123] |= 0x02;
854 }
855
856 if (!android::base::WriteStringToFd(data, fd)) {
857 die("Failed writing to modified vbmeta");
858 }
859 close(buf->fd);
860 buf->fd = fd;
861 lseek(fd, 0, SEEK_SET);
862}
863
Elliott Hughes5620d222018-03-28 08:20:00 -0700864static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700865{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700866 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700867
David Zeuthenb6ea4352017-08-07 14:29:26 -0400868 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
869 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700870 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400871 rewrite_vbmeta_buffer(buf);
872 }
873
Rom Lemarchand622810c2013-06-28 09:54:59 -0700874 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800875 case FB_BUFFER_SPARSE: {
876 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700877 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700878 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700879 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800880 sparse_files.emplace_back(*s, sz);
881 ++s;
882 }
883
884 for (size_t i = 0; i < sparse_files.size(); ++i) {
885 const auto& pair = sparse_files[i];
Elliott Hughes5620d222018-03-28 08:20:00 -0700886 fb_queue_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700887 }
888 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800889 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500890 case FB_BUFFER_FD:
Elliott Hughes5620d222018-03-28 08:20:00 -0700891 fb_queue_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700892 break;
893 default:
894 die("unknown buffer type: %d", buf->type);
895 }
896}
897
Aaron Wisnerdb511202018-06-26 15:38:35 -0500898static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700899 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500900 if (!fb_getvar("current-slot", &current_slot)) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700901 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700902}
903
Aaron Wisnerdb511202018-06-26 15:38:35 -0500904static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700905 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700906 int count = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500907 if (!fb_getvar("slot-count", &var) || !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700908 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700909 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700910 return count;
911}
912
Aaron Wisnerdb511202018-06-26 15:38:35 -0500913static bool supports_AB() {
914 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700915}
916
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700917// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700918static std::string get_other_slot(const std::string& current_slot, int count) {
919 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700920
Daniel Rosenberg80919472016-06-30 19:25:31 -0700921 char next = (current_slot[0] - 'a' + 1)%count + 'a';
922 return std::string(1, next);
923}
924
Aaron Wisnerdb511202018-06-26 15:38:35 -0500925static std::string get_other_slot(const std::string& current_slot) {
926 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -0700927}
928
Aaron Wisnerdb511202018-06-26 15:38:35 -0500929static std::string get_other_slot(int count) {
930 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700931}
932
Aaron Wisnerdb511202018-06-26 15:38:35 -0500933static std::string get_other_slot() {
934 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -0700935}
936
Aaron Wisnerdb511202018-06-26 15:38:35 -0500937static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700938 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700939 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800940 if (allow_all) {
941 return "all";
942 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500943 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700944 if (count > 0) {
945 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800946 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -0700947 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800948 }
949 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700950 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800951
Aaron Wisnerdb511202018-06-26 15:38:35 -0500952 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -0700953 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800954
Daniel Rosenberg80919472016-06-30 19:25:31 -0700955 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500956 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700957 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700958 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800959 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700960 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800961 }
962
Daniel Rosenberg80919472016-06-30 19:25:31 -0700963 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
964
965 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
966 for (int i=0; i<count; i++) {
967 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700968 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700969
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700970 exit(1);
971}
972
Aaron Wisnerdb511202018-06-26 15:38:35 -0500973static std::string verify_slot(const std::string& slot) {
974 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800975}
976
Aaron Wisnerdb511202018-06-26 15:38:35 -0500977static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700978 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800979 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700980 std::string current_slot;
981
Aaron Wisnerdb511202018-06-26 15:38:35 -0500982 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800983 /* If has-slot is not supported, the answer is no. */
984 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700985 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800986 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700987 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500988 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700989 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700990 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700991 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700992 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700993 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700994 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700995 }
996 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700997 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -0700998 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -0700999 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001000 }
1001 func(part);
1002 }
1003}
1004
David Pursell0b156632015-10-30 11:22:01 -07001005/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1006 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1007 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1008 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001009 */
Aaron Wisnerdb511202018-06-26 15:38:35 -05001010static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001011 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001012 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001013
Daniel Rosenberg80919472016-06-30 19:25:31 -07001014 if (slot == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001015 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001016 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001017 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001018 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001019 for (int i=0; i < get_slot_count(); i++) {
1020 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001021 }
1022 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001023 do_for_partition(part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001024 }
1025 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001026 do_for_partition(part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001027 }
1028}
1029
Aaron Wisnerdb511202018-06-26 15:38:35 -05001030static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001031 struct fastboot_buffer buf;
1032
Aaron Wisnerdb511202018-06-26 15:38:35 -05001033 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001034 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001035 }
1036 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001037}
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
David Anderson32e376f2018-08-16 13:43:11 -07001078static bool is_logical(const std::string& partition) {
1079 std::string value;
1080 return fb_getvar("is-logical:" + partition, &value) && value == "yes";
1081}
1082
David Anderson1d887432018-08-27 16:47:32 -07001083static void reboot_to_userspace_fastboot() {
1084 if (!fb_reboot_to_userspace()) {
1085 die("Must reboot to userspace fastboot to flash logical partitions");
1086 }
1087
1088 // Give the current connection time to close.
1089 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
1090
1091 fb_reinit(open_device());
1092}
1093
David Andersoncf444f32018-08-29 14:15:49 -07001094class ImageSource {
1095 public:
1096 virtual void* ReadFile(const std::string& name, int64_t* size) const = 0;
1097 virtual int OpenFile(const std::string& name) const = 0;
1098};
David Anderson32e376f2018-08-16 13:43:11 -07001099
David Andersoncf444f32018-08-29 14:15:49 -07001100class FlashAllTool {
1101 public:
1102 FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe);
David Anderson32e376f2018-08-16 13:43:11 -07001103
David Andersoncf444f32018-08-29 14:15:49 -07001104 void Flash();
David Anderson32e376f2018-08-16 13:43:11 -07001105
David Andersoncf444f32018-08-29 14:15:49 -07001106 private:
1107 void CheckRequirements();
1108 void DetermineSecondarySlot();
1109 void CollectImages();
1110 void FlashImages(const std::vector<std::pair<const Image*, std::string>>& images);
1111 void FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf);
1112 void UpdateSuperPartition();
David Anderson32e376f2018-08-16 13:43:11 -07001113
David Andersoncf444f32018-08-29 14:15:49 -07001114 const ImageSource& source_;
1115 std::string slot_override_;
1116 bool skip_secondary_;
1117 bool wipe_;
1118 std::string secondary_slot_;
1119 std::vector<std::pair<const Image*, std::string>> boot_images_;
1120 std::vector<std::pair<const Image*, std::string>> os_images_;
1121};
1122
1123FlashAllTool::FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe)
1124 : source_(source),
1125 slot_override_(slot_override),
1126 skip_secondary_(skip_secondary),
1127 wipe_(wipe)
1128{
David Anderson32e376f2018-08-16 13:43:11 -07001129}
1130
David Andersoncf444f32018-08-29 14:15:49 -07001131void FlashAllTool::Flash() {
1132 CheckRequirements();
1133 DetermineSecondarySlot();
1134 CollectImages();
David Anderson32e376f2018-08-16 13:43:11 -07001135
David Anderson95d40932018-08-28 12:14:33 -07001136 // First flash boot partitions. We allow this to happen either in userspace
1137 // or in bootloader fastboot.
David Andersoncf444f32018-08-29 14:15:49 -07001138 FlashImages(boot_images_);
David Anderson95d40932018-08-28 12:14:33 -07001139
1140 // Sync the super partition. This will reboot to userspace fastboot if needed.
David Andersoncf444f32018-08-29 14:15:49 -07001141 UpdateSuperPartition();
David Anderson95d40932018-08-28 12:14:33 -07001142
1143 // Resize any logical partition to 0, so each partition is reset to 0
1144 // extents, and will achieve more optimal allocation.
David Andersoncf444f32018-08-29 14:15:49 -07001145 for (const auto& [image, slot] : os_images_) {
David Anderson32e376f2018-08-16 13:43:11 -07001146 auto resize_partition = [](const std::string& partition) -> void {
1147 if (is_logical(partition)) {
1148 fb_queue_resize_partition(partition, "0");
1149 }
1150 };
David Anderson95d40932018-08-28 12:14:33 -07001151 do_for_partitions(image->part_name, slot, resize_partition, false);
David Anderson32e376f2018-08-16 13:43:11 -07001152 }
1153
David Anderson95d40932018-08-28 12:14:33 -07001154 // Flash OS images, resizing logical partitions as needed.
David Andersoncf444f32018-08-29 14:15:49 -07001155 FlashImages(os_images_);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001156
David Andersoncf444f32018-08-29 14:15:49 -07001157 if (slot_override_ == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001158 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001159 } else {
David Andersoncf444f32018-08-29 14:15:49 -07001160 set_active(slot_override_);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001161 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001162}
1163
David Andersoncf444f32018-08-29 14:15:49 -07001164void FlashAllTool::CheckRequirements() {
1165 int64_t sz;
1166 void* data = source_.ReadFile("android-info.txt", &sz);
1167 if (data == nullptr) {
1168 die("could not read android-info.txt");
1169 }
1170 check_requirements(reinterpret_cast<char*>(data), sz);
1171}
1172
1173void FlashAllTool::DetermineSecondarySlot() {
1174 if (skip_secondary_) {
1175 return;
1176 }
1177 if (slot_override_ != "") {
1178 secondary_slot_ = get_other_slot(slot_override_);
1179 } else {
1180 secondary_slot_ = get_other_slot();
1181 }
1182 if (secondary_slot_ == "") {
1183 if (supports_AB()) {
1184 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1185 }
1186 skip_secondary_ = true;
1187 }
1188}
1189
1190void FlashAllTool::CollectImages() {
1191 for (size_t i = 0; i < arraysize(images); ++i) {
1192 std::string slot = slot_override_;
1193 if (images[i].IsSecondary()) {
1194 if (skip_secondary_) {
1195 continue;
1196 }
1197 slot = secondary_slot_;
1198 }
1199 if (images[i].type == ImageType::BootCritical) {
1200 boot_images_.emplace_back(&images[i], slot);
1201 } else if (images[i].type == ImageType::Normal) {
1202 os_images_.emplace_back(&images[i], slot);
1203 }
1204 }
1205}
1206
1207void FlashAllTool::FlashImages(const std::vector<std::pair<const Image*, std::string>>& images) {
1208 for (const auto& [image, slot] : images) {
1209 fastboot_buffer buf;
1210 int fd = source_.OpenFile(image->img_name);
1211 if (fd < 0 || !load_buf_fd(fd, &buf)) {
1212 if (image->optional_if_no_image) {
1213 continue;
1214 }
1215 die("could not load '%s': %s", image->img_name, strerror(errno));
1216 }
1217 FlashImage(*image, slot, &buf);
1218 }
1219}
1220
1221void FlashAllTool::FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf) {
1222 auto flash = [&, this](const std::string& partition_name) {
1223 int64_t sz;
1224 void* data = source_.ReadFile(image.sig_name, &sz);
1225 if (data) {
1226 fb_queue_download("signature", data, sz);
1227 fb_queue_command("signature", "installing signature");
1228 }
1229
1230 if (is_logical(partition_name)) {
1231 fb_queue_resize_partition(partition_name, std::to_string(buf->image_size));
1232 }
1233 flash_buf(partition_name.c_str(), buf);
1234 };
1235 do_for_partitions(image.part_name, slot, flash, false);
1236}
1237
1238void FlashAllTool::UpdateSuperPartition() {
1239 if (!if_partition_exists("super", "")) {
1240 return;
1241 }
1242
1243 int fd = source_.OpenFile("super_empty.img");
1244 if (fd < 0) {
1245 return;
1246 }
1247 if (!is_userspace_fastboot()) {
1248 reboot_to_userspace_fastboot();
1249 }
1250 fb_queue_download_fd("super", fd, get_file_size(fd));
1251
1252 std::string command = "update-super:super";
1253 if (wipe_) {
1254 command += ":wipe";
1255 }
1256 fb_queue_command(command, "Updating super partition");
1257
1258 // We need these commands to have finished before proceeding, since
1259 // otherwise "getvar is-logical" may not return a correct answer below.
1260 fb_execute_queue();
1261}
1262
1263class ZipImageSource final : public ImageSource {
1264 public:
1265 explicit ZipImageSource(ZipArchiveHandle zip) : zip_(zip) {}
1266 void* ReadFile(const std::string& name, int64_t* size) const override;
1267 int OpenFile(const std::string& name) const override;
1268
1269 private:
1270 ZipArchiveHandle zip_;
1271};
1272
1273void* ZipImageSource::ReadFile(const std::string& name, int64_t* size) const {
1274 return unzip_to_memory(zip_, name.c_str(), size);
1275}
1276
1277int ZipImageSource::OpenFile(const std::string& name) const {
1278 return unzip_to_file(zip_, name.c_str());
1279}
1280
1281static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
1282 queue_info_dump();
1283
1284 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1285
1286 ZipArchiveHandle zip;
1287 int error = OpenArchive(filename, &zip);
1288 if (error != 0) {
1289 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
1290 }
1291
1292 FlashAllTool tool(ZipImageSource(zip), slot_override, skip_secondary, false);
1293 tool.Flash();
1294
1295 CloseArchive(zip);
1296}
1297
1298class LocalImageSource final : public ImageSource {
1299 public:
1300 void* ReadFile(const std::string& name, int64_t* size) const override;
1301 int OpenFile(const std::string& name) const override;
1302};
1303
1304void* LocalImageSource::ReadFile(const std::string& name, int64_t* size) const {
1305 auto path = find_item_given_name(name);
1306 if (path.empty()) {
1307 return nullptr;
1308 }
1309 return load_file(path.c_str(), size);
1310}
1311
1312int LocalImageSource::OpenFile(const std::string& name) const {
1313 auto path = find_item_given_name(name);
1314 return open(path.c_str(), O_RDONLY);
1315}
1316
1317static void do_flashall(const std::string& slot_override, bool skip_secondary, bool wipe) {
1318 std::string fname;
1319 queue_info_dump();
1320
1321 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1322
1323 FlashAllTool tool(LocalImageSource(), slot_override, skip_secondary, wipe);
1324 tool.Flash();
1325}
1326
Elliott Hughesd6365a72017-05-08 18:04:49 -07001327static std::string next_arg(std::vector<std::string>* args) {
1328 if (args->empty()) syntax_error("expected argument");
1329 std::string result = args->front();
1330 args->erase(args->begin());
1331 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001332}
1333
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001334static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001335 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001336
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001337 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001338 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001339 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001340 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001341 fb_queue_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001342}
1343
Connor O'Brience16a8a2017-02-06 14:39:31 -08001344static std::string fb_fix_numeric_var(std::string var) {
1345 // Some bootloaders (angler, for example), send spurious leading whitespace.
1346 var = android::base::Trim(var);
1347 // Some bootloaders (hammerhead, for example) use implicit hex.
1348 // This code used to use strtol with base 16.
1349 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1350 return var;
1351}
1352
Aaron Wisnerdb511202018-06-26 15:38:35 -05001353static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001354 std::string sizeString;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001355 if (!fb_getvar(name, &sizeString) || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001356 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001357 return 0;
1358 }
1359 sizeString = fb_fix_numeric_var(sizeString);
1360
1361 unsigned size;
1362 if (!android::base::ParseUint(sizeString, &size)) {
1363 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1364 return 0;
1365 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001366 if ((size & (size - 1)) != 0) {
1367 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001368 return 0;
1369 }
1370 return size;
1371}
1372
Aaron Wisnerdb511202018-06-26 15:38:35 -05001373static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001374 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001375 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001376 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001377 std::string partition_type, partition_size;
1378
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001379 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001380 const char* errMsg = nullptr;
1381 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001382 TemporaryFile output;
1383 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001384
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001385 unsigned int limit = INT_MAX;
1386 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001387 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001388 }
1389 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001390 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001391 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001392
Aaron Wisnerdb511202018-06-26 15:38:35 -05001393 if (!fb_getvar("partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001394 errMsg = "Can't determine partition type.\n";
1395 goto failed;
1396 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001397 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001398 if (partition_type != type_override) {
1399 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001400 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001401 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001402 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001403 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001404
Aaron Wisnerdb511202018-06-26 15:38:35 -05001405 if (!fb_getvar("partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001406 errMsg = "Unable to get partition size\n";
1407 goto failed;
1408 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001409 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001410 if (partition_size != size_override) {
1411 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001412 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001413 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001414 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001415 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001416 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001417
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001418 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001419 if (!gen) {
1420 if (skip_if_not_supported) {
1421 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001422 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001423 return;
1424 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001425 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1426 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001427 return;
1428 }
1429
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001430 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001431 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001432 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1433 return;
1434 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001435
Connor O'Brience16a8a2017-02-06 14:39:31 -08001436 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001437 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1438 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001439
Jin Qian4a335822017-04-18 16:23:18 -07001440 if (fs_generator_generate(gen, output.path, size, initial_dir,
1441 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001442 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001443 return;
1444 }
1445
Jin Qian4a335822017-04-18 16:23:18 -07001446 fd.reset(open(output.path, O_RDONLY));
1447 if (fd == -1) {
1448 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1449 return;
1450 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001451 if (!load_buf_fd(fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001452 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001453 return;
1454 }
1455 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001456 return;
1457
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001458failed:
1459 if (skip_if_not_supported) {
1460 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001461 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001462 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001463 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001464}
1465
Aaron Wisnerdb511202018-06-26 15:38:35 -05001466int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001467 bool wants_wipe = false;
1468 bool wants_reboot = false;
1469 bool wants_reboot_bootloader = false;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001470 bool wants_reboot_recovery = false;
1471 bool wants_reboot_fastboot = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001472 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001473 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001474 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001475 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001476 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001477 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001478 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001479 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001480 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001481
Elliott Hughes577e8b42018-04-05 16:12:47 -07001482 g_boot_img_hdr.kernel_addr = 0x00008000;
1483 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1484 g_boot_img_hdr.second_addr = 0x00f00000;
1485 g_boot_img_hdr.tags_addr = 0x00000100;
1486 g_boot_img_hdr.page_size = 2048;
1487
JP Abgrall7b8970c2013-03-07 17:06:41 -08001488 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001489 {"base", required_argument, 0, 0},
1490 {"cmdline", required_argument, 0, 0},
1491 {"disable-verification", no_argument, 0, 0},
1492 {"disable-verity", no_argument, 0, 0},
1493 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001494 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001495 {"kernel-offset", required_argument, 0, 0},
1496 {"os-patch-level", required_argument, 0, 0},
1497 {"os-version", required_argument, 0, 0},
1498 {"page-size", required_argument, 0, 0},
1499 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001500 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001501 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001502 {"skip-secondary", no_argument, 0, 0},
1503 {"slot", required_argument, 0, 0},
1504 {"tags-offset", required_argument, 0, 0},
1505 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001506 {"verbose", no_argument, 0, 'v'},
1507 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001508#if !defined(_WIN32)
1509 {"wipe-and-use-fbe", no_argument, 0, 0},
1510#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001511 {0, 0, 0, 0}
1512 };
Colin Cross8879f982012-05-22 17:53:34 -07001513
1514 serial = getenv("ANDROID_SERIAL");
1515
Elliott Hughes577e8b42018-04-05 16:12:47 -07001516 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001517 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001518 if (c == 0) {
1519 std::string name{longopts[longindex].name};
1520 if (name == "base") {
1521 g_base_addr = strtoul(optarg, 0, 16);
1522 } else if (name == "cmdline") {
1523 g_cmdline = optarg;
1524 } else if (name == "disable-verification") {
1525 g_disable_verification = true;
1526 } else if (name == "disable-verity") {
1527 g_disable_verity = true;
1528 } else if (name == "header-version") {
1529 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1530 } else if (name == "kernel-offset") {
1531 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1532 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001533 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001534 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001535 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001536 } else if (name == "page-size") {
1537 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1538 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1539 } else if (name == "ramdisk-offset") {
1540 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1541 } else if (name == "skip-reboot") {
1542 skip_reboot = true;
1543 } else if (name == "skip-secondary") {
1544 skip_secondary = true;
1545 } else if (name == "slot") {
1546 slot_override = optarg;
1547 } else if (name == "tags-offset") {
1548 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1549 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001550 setvbuf(stdout, nullptr, _IONBF, 0);
1551 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001552 } else if (name == "version") {
Dan Willemsenab971b52018-08-29 14:58:02 -07001553 fprintf(stdout, "fastboot version %s-%s\n", PLATFORM_TOOLS_VERSION, android::build::GetBuildNumber().c_str());
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001554 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001555 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001556#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001557 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001558 wants_wipe = true;
1559 set_fbe_marker = true;
1560#endif
1561 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001562 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001563 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001564 } else {
1565 switch (c) {
1566 case 'a':
1567 wants_set_active = true;
1568 if (optarg) next_active = optarg;
1569 break;
1570 case 'h':
1571 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001572 case 'l':
1573 g_long_listing = true;
1574 break;
1575 case 's':
1576 serial = optarg;
1577 break;
1578 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001579 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1580 die("invalid sparse limit %s", optarg);
1581 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001582 break;
1583 case 'v':
1584 set_verbose();
1585 break;
1586 case 'w':
1587 wants_wipe = true;
1588 break;
1589 case '?':
1590 return 1;
1591 default:
1592 abort();
1593 }
Colin Cross8879f982012-05-22 17:53:34 -07001594 }
1595 }
1596
1597 argc -= optind;
1598 argv += optind;
1599
Elliott Hughesd6365a72017-05-08 18:04:49 -07001600 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001601
Colin Cross8fb6e062012-07-24 16:36:41 -07001602 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001603 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001604 return 0;
1605 }
1606
Colin Crossc7b75dc2012-08-29 18:17:06 -07001607 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001608 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001609 }
1610
David Pursell0b156632015-10-30 11:22:01 -07001611 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001612 if (transport == nullptr) {
1613 return 1;
1614 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001615 fastboot::FastBootDriver fb(transport);
1616 fb_init(fb);
David Pursell2ec418a2016-01-20 08:32:08 -08001617
Elliott Hughes5620d222018-03-28 08:20:00 -07001618 const double start = now();
1619
Aaron Wisnerdb511202018-06-26 15:38:35 -05001620 if (slot_override != "") slot_override = verify_slot(slot_override);
1621 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001622
1623 if (wants_set_active) {
1624 if (next_active == "") {
1625 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001626 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001627 if (fb_getvar("current-slot", &current_slot)) {
1628 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001629 } else {
1630 wants_set_active = false;
1631 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001632 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001633 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001634 }
1635 }
1636 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001637
Elliott Hughesd6365a72017-05-08 18:04:49 -07001638 std::vector<std::string> args(argv, argv + argc);
1639 while (!args.empty()) {
1640 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001641
Elliott Hughesd6365a72017-05-08 18:04:49 -07001642 if (command == "getvar") {
1643 std::string variable = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001644 fb_queue_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001645 } else if (command == "erase") {
1646 std::string partition = next_arg(&args);
1647 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001648 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001649 if (fb_getvar(std::string("partition-type:") + partition,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001650 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001651 fs_get_generator(partition_type) != nullptr) {
1652 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1653 partition_type.c_str());
1654 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001655
Elliott Hughes5620d222018-03-28 08:20:00 -07001656 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001657 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001658 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001659 } else if (android::base::StartsWith(command, "format")) {
1660 // Parsing for: "format[:[type][:[size]]]"
1661 // Some valid things:
1662 // - select only the size, and leave default fs type:
1663 // format::0x4000000 userdata
1664 // - default fs type and size:
1665 // format userdata
1666 // format:: userdata
1667 std::vector<std::string> pieces = android::base::Split(command, ":");
1668 std::string type_override;
1669 if (pieces.size() > 1) type_override = pieces[1].c_str();
1670 std::string size_override;
1671 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001672
Elliott Hughesd6365a72017-05-08 18:04:49 -07001673 std::string partition = next_arg(&args);
1674
1675 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001676 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001677 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001678 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001679 } else if (command == "signature") {
1680 std::string filename = next_arg(&args);
1681 data = load_file(filename.c_str(), &sz);
1682 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -07001683 if (sz != 256) die("signature must be 256 bytes (got %" PRId64 ")", sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001684 fb_queue_download("signature", data, sz);
1685 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001686 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001687 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001688
1689 if (args.size() == 1) {
1690 std::string what = next_arg(&args);
1691 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001692 wants_reboot = false;
1693 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001694 } else if (what == "recovery") {
1695 wants_reboot = false;
1696 wants_reboot_recovery = true;
1697 } else if (what == "fastboot") {
1698 wants_reboot = false;
1699 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001700 } else {
1701 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001702 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001703
Elliott Hughesca85df02015-02-25 10:02:00 -08001704 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001705 if (!args.empty()) syntax_error("junk after reboot command");
1706 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001707 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001708 } else if (command == "reboot-recovery") {
1709 wants_reboot_recovery = true;
1710 } else if (command == "reboot-fastboot") {
1711 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001712 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001713 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001714 } else if (command == "boot") {
1715 std::string kernel = next_arg(&args);
1716 std::string ramdisk;
1717 if (!args.empty()) ramdisk = next_arg(&args);
1718 std::string second_stage;
1719 if (!args.empty()) second_stage = next_arg(&args);
1720
Elliott Hughes577e8b42018-04-05 16:12:47 -07001721 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001722 fb_queue_download("boot.img", data, sz);
1723 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001724 } else if (command == "flash") {
1725 std::string pname = next_arg(&args);
1726
Elliott Hughes2810d002016-04-25 14:31:18 -07001727 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001728 if (!args.empty()) {
1729 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001730 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001731 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001732 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001733 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001734
1735 auto flash = [&](const std::string &partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001736 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001737 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001738 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001739 } else if (command == "flash:raw") {
1740 std::string partition = next_arg(&args);
1741 std::string kernel = next_arg(&args);
1742 std::string ramdisk;
1743 if (!args.empty()) ramdisk = next_arg(&args);
1744 std::string second_stage;
1745 if (!args.empty()) second_stage = next_arg(&args);
1746
Elliott Hughes577e8b42018-04-05 16:12:47 -07001747 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001748 auto flashraw = [&](const std::string& partition) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001749 fb_queue_flash(partition, data, sz);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001750 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001751 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001752 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001753 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001754 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
David Anderson32e376f2018-08-16 13:43:11 -07001755 do_flashall(slot_override, true, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001756 } else {
David Anderson32e376f2018-08-16 13:43:11 -07001757 do_flashall(slot_override, skip_secondary, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001758 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001759 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001760 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001761 bool slot_all = (slot_override == "all");
1762 if (slot_all) {
1763 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1764 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001765 std::string filename = "update.zip";
1766 if (!args.empty()) {
1767 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001768 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001769 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001770 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001771 } else if (command == "set_active") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001772 std::string slot = verify_slot(next_arg(&args), false);
Elliott Hughes5620d222018-03-28 08:20:00 -07001773 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001774 } else if (command == "stage") {
1775 std::string filename = next_arg(&args);
1776
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001777 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001778 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001779 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001780 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001781 fb_queue_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001782 } else if (command == "get_staged") {
1783 std::string filename = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001784 fb_queue_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001785 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001786 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001787 } else if (command == "flashing") {
1788 if (args.empty()) {
1789 syntax_error("missing 'flashing' command");
1790 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1791 args[0] == "unlock_critical" ||
1792 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001793 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001794 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001795 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001796 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001797 }
David Anderson0d4277d2018-07-31 13:27:37 -07001798 } else if (command == "create-logical-partition") {
1799 std::string partition = next_arg(&args);
1800 std::string size = next_arg(&args);
1801 fb_queue_create_partition(partition, size);
1802 } else if (command == "delete-logical-partition") {
1803 std::string partition = next_arg(&args);
1804 fb_queue_delete_partition(partition);
1805 } else if (command == "resize-logical-partition") {
1806 std::string partition = next_arg(&args);
1807 std::string size = next_arg(&args);
1808 fb_queue_resize_partition(partition, size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001809 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001810 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001811 }
1812 }
1813
1814 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001815 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1816 for (const auto& partition : partitions) {
1817 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001818 if (!fb_getvar(std::string{"partition-type:"} + partition, &partition_type)) continue;
Paul Crowley4d170062018-04-24 17:06:30 -07001819 if (partition_type.empty()) continue;
1820 fb_queue_erase(partition);
1821 if (partition == "userdata" && set_fbe_marker) {
1822 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1823 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001824 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001825 delete_fbemarker_tmpdir(initial_userdata_dir);
1826 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001827 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001828 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001829 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001830 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001831 if (wants_set_active) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001832 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001833 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001834 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001835 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001836 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001837 } else if (wants_reboot_bootloader) {
1838 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001839 fb_queue_wait_for_disconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001840 } else if (wants_reboot_recovery) {
1841 fb_queue_command("reboot-recovery", "rebooting into recovery");
1842 fb_queue_wait_for_disconnect();
1843 } else if (wants_reboot_fastboot) {
1844 fb_queue_command("reboot-fastboot", "rebooting into fastboot");
1845 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001846 }
1847
Aaron Wisnerdb511202018-06-26 15:38:35 -05001848 int status = fb_execute_queue() ? EXIT_FAILURE : EXIT_SUCCESS;
Elliott Hughes5620d222018-03-28 08:20:00 -07001849 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
David Anderson03de6452018-09-04 14:32:54 -07001850
1851 if (Transport* old_transport = fb.set_transport(nullptr)) {
1852 delete old_transport;
1853 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001854 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001855}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001856
Aaron Wisnerdb511202018-06-26 15:38:35 -05001857void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001858 unsigned year, month, day;
1859 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1860 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1861 }
1862 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1863 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1864 hdr->SetOsPatchLevel(year, month);
1865}
1866
Aaron Wisnerdb511202018-06-26 15:38:35 -05001867void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001868 unsigned major = 0, minor = 0, patch = 0;
1869 std::vector<std::string> versions = android::base::Split(arg, ".");
1870 if (versions.size() < 1 || versions.size() > 3 ||
1871 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1872 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1873 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1874 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1875 syntax_error("bad OS version: %s", arg);
1876 }
1877 hdr->SetOsVersion(major, minor, patch);
1878}