blob: 293ff00e8e9c66bcb4107804b8feaf85367eccb9 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070029#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <errno.h>
31#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070032#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070033#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070034#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070035#include <stdint.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070041#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070042#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080043
Elliott Hughes290a2282016-11-14 17:08:47 -080044#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070045#include <functional>
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080047#include <utility>
48#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070049
Elliott Hughes82ff3152016-08-31 15:07:18 -070050#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070051#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080053#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070054#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080055#include <android-base/strings.h>
Jin Qian4a335822017-04-18 16:23:18 -070056#include <android-base/test_utils.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050057#include <android-base/unique_fd.h>
Colin Crossf8387882012-05-24 17:18:41 -070058#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070059#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Elliott Hughes253c18d2015-03-18 22:47:09 -070061#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080062#include "diagnose_usb.h"
Aaron Wisnerdb511202018-06-26 15:38:35 -050063#include "engine.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070064#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080065#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070066#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080067#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070068#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Chris Fries0ea946c2017-04-12 10:25:57 -050070using android::base::unique_fd;
71
Colin Crossf8387882012-05-24 17:18:41 -070072#ifndef O_BINARY
73#define O_BINARY 0
74#endif
75
Wink Savilleb98762f2011-04-04 17:54:59 -070076char cur_product[FB_RESPONSE_SZ + 1];
77
David Pursell2ec418a2016-01-20 08:32:08 -080078static const char* serial = nullptr;
Elliott Hughes577e8b42018-04-05 16:12:47 -070079
Elliott Hughes577e8b42018-04-05 16:12:47 -070080static bool g_long_listing = false;
Chris Fries6a999712017-04-04 09:52:47 -050081// Don't resparse files in too-big chunks.
82// libsparse will support INT_MAX, but this results in large allocations, so
83// let's keep it at 1GB to avoid memory pressure on the host.
84static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Elliott Hughes542370d2018-04-19 19:49:44 -070085static uint64_t sparse_limit = 0;
Colin Crossf8387882012-05-24 17:18:41 -070086static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
Elliott Hughes577e8b42018-04-05 16:12:47 -070088static unsigned g_base_addr = 0x10000000;
89static boot_img_hdr_v1 g_boot_img_hdr = {};
90static std::string g_cmdline;
JP Abgrall7b8970c2013-03-07 17:06:41 -080091
David Zeuthenb6ea4352017-08-07 14:29:26 -040092static bool g_disable_verity = false;
93static bool g_disable_verification = false;
94
Paul Crowley8f7f56e2015-11-27 09:29:37 +000095static const std::string convert_fbe_marker_filename("convert_fbe");
96
Rom Lemarchand622810c2013-06-28 09:54:59 -070097enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -050098 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -070099 FB_BUFFER_SPARSE,
100};
101
102struct fastboot_buffer {
103 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700104 void* data;
105 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500106 int fd;
David Anderson32e376f2018-08-16 13:43:11 -0700107 int64_t image_size;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700108};
109
David Anderson0debda02018-08-28 13:54:03 -0700110enum class ImageType {
111 // Must be flashed for device to boot into the kernel.
112 BootCritical,
113 // Normal partition to be flashed during "flashall".
114 Normal,
115 // Partition that is never flashed during "flashall".
116 Extra
117};
118
David Anderson32e376f2018-08-16 13:43:11 -0700119struct Image {
Elliott Hughes45964a82017-05-03 22:43:23 -0700120 const char* nickname;
121 const char* img_name;
122 const char* sig_name;
123 const char* part_name;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700124 bool optional_if_no_image;
David Anderson0debda02018-08-28 13:54:03 -0700125 ImageType type;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700126 bool IsSecondary() const { return nickname == nullptr; }
David Anderson32e376f2018-08-16 13:43:11 -0700127};
128
129static Image images[] = {
Dario Frenic7ea1af2018-05-25 16:07:19 +0100130 // clang-format off
David Anderson0debda02018-08-28 13:54:03 -0700131 { "boot", "boot.img", "boot.sig", "boot", false, ImageType::BootCritical },
132 { nullptr, "boot_other.img", "boot.sig", "boot", true, ImageType::Normal },
133 { "cache", "cache.img", "cache.sig", "cache", true, ImageType::Extra },
134 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, ImageType::BootCritical },
135 { "dts", "dt.img", "dt.sig", "dts", true, ImageType::BootCritical },
136 { "odm", "odm.img", "odm.sig", "odm", true, ImageType::Normal },
137 { "product", "product.img", "product.sig", "product", true, ImageType::Normal },
Dario Freniab5583b2018-08-17 01:01:25 +0100138 { "product_services",
139 "product_services.img",
140 "product_services.sig",
David Anderson8cdea7f2018-08-06 15:36:00 -0700141 "product_services",
David Anderson0debda02018-08-28 13:54:03 -0700142 true, ImageType::Normal },
143 { "recovery", "recovery.img", "recovery.sig", "recovery", true, ImageType::BootCritical },
144 { "super", "super.img", "super.sig", "super", true, ImageType::Extra },
145 { "system", "system.img", "system.sig", "system", false, ImageType::Normal },
146 { nullptr, "system_other.img", "system.sig", "system", true, ImageType::Normal },
147 { "userdata", "userdata.img", "userdata.sig", "userdata", true, ImageType::Extra },
148 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, ImageType::BootCritical },
149 { "vendor", "vendor.img", "vendor.sig", "vendor", true, ImageType::Normal },
150 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, ImageType::Normal },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100151 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700152};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700153
David Anderson0debda02018-08-28 13:54:03 -0700154static std::string find_item_given_name(const std::string& img_name) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700155 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700156 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700157 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700158 }
David Anderson0debda02018-08-28 13:54:03 -0700159 return std::string(dir) + "/" + img_name;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700160}
161
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700162static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700163 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700164 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700165 return find_item_given_name(images[i].img_name);
166 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 }
168
Elliott Hughesd6365a72017-05-08 18:04:49 -0700169 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700170 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171}
172
Elliott Hughesfc797672015-04-07 20:12:50 -0700173static int64_t get_file_size(int fd) {
174 struct stat sb;
175 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700176}
177
Elliott Hughesfc797672015-04-07 20:12:50 -0700178static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800179 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700180 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181
Elliott Hughesfc797672015-04-07 20:12:50 -0700182 *sz = get_file_size(fd);
183 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700184 goto oops;
185 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186
Elliott Hughesfc797672015-04-07 20:12:50 -0700187 data = (char*) malloc(*sz);
188 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189
Elliott Hughesfc797672015-04-07 20:12:50 -0700190 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 close(fd);
192
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 return data;
194
195oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800196 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197 close(fd);
198 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800199 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200 return 0;
201}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202
Elliott Hughes2810d002016-04-25 14:31:18 -0700203static void* load_file(const std::string& path, int64_t* sz) {
204 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700205 if (fd == -1) return nullptr;
206 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700207}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208
Elliott Hughesfc797672015-04-07 20:12:50 -0700209static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700210 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
211 return -1;
212 }
213
Scott Anderson13081c62012-04-06 12:39:30 -0700214 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700216 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
217 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 return 0;
219}
220
Elliott Hughesfc797672015-04-07 20:12:50 -0700221static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800222 return match_fastboot_with_serial(info, serial);
223}
224
Elliott Hughesfc797672015-04-07 20:12:50 -0700225static int list_devices_callback(usb_ifc_info* info) {
226 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800227 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700228 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800229 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700230 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 if (!serial[0]) {
232 serial = "????????????";
233 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700234 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700235 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800236 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700237 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800238 printf("%-22s fastboot", serial.c_str());
239 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700240 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800241 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 }
243
244 return -1;
245}
246
David Pursell2ec418a2016-01-20 08:32:08 -0800247// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
248// a specific device, otherwise the first USB device found will be used.
249//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700250// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800251// Otherwise it blocks until the target is available.
252//
253// The returned Transport is a singleton, so multiple calls to this function will return the same
254// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700255static Transport* open_device() {
David Pursell2ec418a2016-01-20 08:32:08 -0800256 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257
David Pursell4601c972016-02-05 15:35:09 -0800258 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800259 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800260 int port = 0;
261 if (serial != nullptr) {
262 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800263
David Pursell4601c972016-02-05 15:35:09 -0800264 if (android::base::StartsWith(serial, "tcp:")) {
265 protocol = Socket::Protocol::kTcp;
266 port = tcp::kDefaultPort;
267 net_address = serial + strlen("tcp:");
268 } else if (android::base::StartsWith(serial, "udp:")) {
269 protocol = Socket::Protocol::kUdp;
270 port = udp::kDefaultPort;
271 net_address = serial + strlen("udp:");
272 }
273
274 if (net_address != nullptr) {
275 std::string error;
276 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700277 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800278 }
David Pursell2ec418a2016-01-20 08:32:08 -0800279 }
280 }
281
David Anderson1d887432018-08-27 16:47:32 -0700282 Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800283 while (true) {
284 if (!host.empty()) {
285 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800286 if (protocol == Socket::Protocol::kTcp) {
287 transport = tcp::Connect(host, port, &error).release();
288 } else if (protocol == Socket::Protocol::kUdp) {
289 transport = udp::Connect(host, port, &error).release();
290 }
291
David Pursell2ec418a2016-01-20 08:32:08 -0800292 if (transport == nullptr && announce) {
293 fprintf(stderr, "error: %s\n", error.c_str());
294 }
295 } else {
296 transport = usb_open(match_fastboot);
297 }
298
299 if (transport != nullptr) {
300 return transport;
301 }
302
David Pursell0b156632015-10-30 11:22:01 -0700303 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800304 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700305 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800307 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 }
309}
310
Elliott Hughesfc797672015-04-07 20:12:50 -0700311static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312 // We don't actually open a USB device here,
313 // just getting our callback called so we can
314 // list all the connected devices.
315 usb_open(list_devices_callback);
316}
317
Elliott Hughesd6365a72017-05-08 18:04:49 -0700318static void syntax_error(const char* fmt, ...) {
319 fprintf(stderr, "fastboot: usage: ");
320
321 va_list ap;
322 va_start(ap, fmt);
323 vfprintf(stderr, fmt, ap);
324 va_end(ap);
325
326 fprintf(stderr, "\n");
327 exit(1);
328}
329
330static int show_help() {
331 // clang-format off
332 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700333// 1 2 3 4 5 6 7 8
334// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
Elliott Hughesb76188f2018-04-03 13:30:18 -0700335 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700337 "flashing:\n"
338 " update ZIP Flash all partitions from an update.zip package.\n"
339 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
340 " On A/B devices, flashed slot is set as active.\n"
341 " Secondary images may be flashed to inactive slot.\n"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700342 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
343 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700344 "\n"
345 "basics:\n"
346 " devices [-l] List devices in bootloader (-l: with device paths).\n"
347 " getvar NAME Display given bootloader variable.\n"
348 " reboot [bootloader] Reboot device.\n"
349 "\n"
350 "locking/unlocking:\n"
351 " flashing lock|unlock Lock/unlock partitions for flashing\n"
352 " flashing lock_critical|unlock_critical\n"
353 " Lock/unlock 'critical' bootloader partitions.\n"
354 " flashing get_unlock_ability\n"
355 " Check whether unlocking is allowed (1) or not(0).\n"
356 "\n"
357 "advanced:\n"
358 " erase PARTITION Erase a flash partition.\n"
359 " format[:FS_TYPE[:SIZE]] PARTITION\n"
360 " Format a flash partition.\n"
361 " set_active SLOT Set the active slot.\n"
362 " oem [COMMAND...] Execute OEM-specific command.\n"
363 "\n"
364 "boot image:\n"
365 " boot KERNEL [RAMDISK [SECOND]]\n"
366 " Download and boot kernel from RAM.\n"
367 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
368 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700369 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700370 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
371 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
372 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
373 " --tags-offset Set tags offset (default: 0x00000100).\n"
374 " --page-size BYTES Set flash page size (default: 2048).\n"
375 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700376 " --os-version MAJOR[.MINOR[.PATCH]]\n"
377 " Set boot image OS version (default: 0.0.0).\n"
378 " --os-patch-level YYYY-MM-DD\n"
379 " Set boot image OS security patch level.\n"
380 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700381 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700382 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700383 //" continue Continue with autoboot.\n"
384 //"\n"
385 "Android Things:\n"
386 " stage IN_FILE Sends given file to stage for the next command.\n"
387 " 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 -0800388 "\n"
389 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700390 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700391 " -s SERIAL Specify a USB device.\n"
392 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700393 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700394 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
395 " non-current slot (default: current active slot).\n"
396 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
397 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
398 " --skip-reboot Don't reboot device after flashing.\n"
399 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
400 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000401#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700402 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000403#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700404 // TODO: remove --unbuffered?
405 " --unbuffered Don't buffer input or output.\n"
406 " --verbose, -v Verbose output.\n"
407 " --version Display version.\n"
408 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700410 // clang-format off
411 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000413
Elliott Hughesd6365a72017-05-08 18:04:49 -0700414static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
Elliott Hughes577e8b42018-04-05 16:12:47 -0700415 const std::string& second_stage, int64_t* sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700416 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700417 void* kdata = load_file(kernel.c_str(), &ksize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700418 if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800419
Elliott Hughesfc797672015-04-07 20:12:50 -0700420 // Is this actually a boot image?
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700421 if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800422 die("cannot load '%s': too short", kernel.c_str());
423 }
Elliott Hughesd6365a72017-05-08 18:04:49 -0700424 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700425 if (!g_cmdline.empty()) {
426 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), g_cmdline);
427 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800428
Elliott Hughes4089d342017-10-27 14:21:12 -0700429 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800430
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431 *sz = ksize;
432 return kdata;
433 }
434
Elliott Hughesfc797672015-04-07 20:12:50 -0700435 void* rdata = nullptr;
436 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700437 if (!ramdisk.empty()) {
438 rdata = load_file(ramdisk.c_str(), &rsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700439 if (rdata == nullptr) die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440 }
441
Elliott Hughesfc797672015-04-07 20:12:50 -0700442 void* sdata = nullptr;
443 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700444 if (!second_stage.empty()) {
445 sdata = load_file(second_stage.c_str(), &ssize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700446 if (sdata == nullptr) die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200447 }
448
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 fprintf(stderr,"creating boot image...\n");
Elliott Hughes577e8b42018-04-05 16:12:47 -0700450 boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, rdata, rsize, sdata, ssize,
451 g_base_addr, g_boot_img_hdr, sz);
Elliott Hughes4089d342017-10-27 14:21:12 -0700452 if (bdata == nullptr) die("failed to create boot.img");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700453
Elliott Hughes577e8b42018-04-05 16:12:47 -0700454 if (!g_cmdline.empty()) bootimg_set_cmdline(bdata, g_cmdline);
455 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", *sz);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800456
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457 return bdata;
458}
459
Elliott Hughes23af1122017-11-10 08:42:17 -0800460static void* unzip_to_memory(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700461 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700462 ZipEntry zip_entry;
463 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700464 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Elliott Hughes4089d342017-10-27 14:21:12 -0700465 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466 }
467
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700468 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469
Elliott Hughes23af1122017-11-10 08:42:17 -0800470 fprintf(stderr, "extracting %s (%" PRId64 " MB) to RAM...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700471 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughes4089d342017-10-27 14:21:12 -0700472 if (data == nullptr) die("failed to allocate %" PRId64 " bytes for '%s'", *sz, entry_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700474 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
Elliott Hughes4089d342017-10-27 14:21:12 -0700475 if (error != 0) die("failed to extract '%s': %s", entry_name, ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000476
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 return data;
478}
479
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700480#if defined(_WIN32)
481
482// TODO: move this to somewhere it can be shared.
483
484#include <windows.h>
485
486// Windows' tmpfile(3) requires administrator rights because
487// it creates temporary files in the root directory.
488static FILE* win32_tmpfile() {
489 char temp_path[PATH_MAX];
490 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
491 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700492 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700493 }
494
495 char filename[PATH_MAX];
496 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700497 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700498 }
499
500 return fopen(filename, "w+bTD");
501}
502
503#define tmpfile win32_tmpfile
504
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000505static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700506 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000507}
508
Elliott Hughes855cdf82018-04-02 14:24:03 -0700509static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700510 // TODO: reimplement to avoid leaking a FILE*.
511 return fileno(tmpfile());
512}
513
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000514#else
515
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700516static std::string make_temporary_template() {
517 const char* tmpdir = getenv("TMPDIR");
518 if (tmpdir == nullptr) tmpdir = P_tmpdir;
519 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
520}
521
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000522static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700523 std::string result(make_temporary_template());
524 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700525 die("unable to create temporary directory with template %s: %s",
526 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000527 }
528 return result;
529}
530
Elliott Hughes855cdf82018-04-02 14:24:03 -0700531static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700532 std::string path_template(make_temporary_template());
533 int fd = mkstemp(&path_template[0]);
534 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700535 die("failed to create temporary file for %s with template %s: %s\n",
536 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700537 }
538 unlink(path_template.c_str());
539 return fd;
540}
541
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700542#endif
543
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000544static std::string create_fbemarker_tmpdir() {
545 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000546 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
547 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
548 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700549 die("unable to create FBE marker file %s locally: %s",
550 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000551 }
552 close(fd);
553 return dir;
554}
555
556static void delete_fbemarker_tmpdir(const std::string& dir) {
557 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
558 if (unlink(marker_file.c_str()) == -1) {
559 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
560 marker_file.c_str(), errno, strerror(errno));
561 return;
562 }
563 if (rmdir(dir.c_str()) == -1) {
564 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
565 dir.c_str(), errno, strerror(errno));
566 return;
567 }
568}
569
Elliott Hughes45964a82017-05-03 22:43:23 -0700570static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700571 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700572
Yusuke Sato07447542015-06-25 14:39:19 -0700573 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700574 ZipEntry zip_entry;
575 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
576 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
David Andersoncf444f32018-08-29 14:15:49 -0700577 errno = ENOENT;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000578 return -1;
579 }
580
Elliott Hughes23af1122017-11-10 08:42:17 -0800581 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700582 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800583 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700584 int error = ExtractEntryToFile(zip, &zip_entry, fd);
585 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800586 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700587 }
588
Elliott Hughes4089d342017-10-27 14:21:12 -0700589 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800590 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700591 }
592
Elliott Hughes23af1122017-11-10 08:42:17 -0800593 fprintf(stderr, " took %.3fs\n", now() - start);
594
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700595 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700596}
597
Elliott Hughes5620d222018-03-28 08:20:00 -0700598static char* strip(char* s) {
599 while (*s && isspace(*s)) s++;
600
601 int n = strlen(s);
602 while (n-- > 0) {
603 if (!isspace(s[n])) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800604 s[n] = 0;
605 }
606 return s;
607}
608
609#define MAX_OPTIONS 32
Aaron Wisnerdb511202018-06-26 15:38:35 -0500610static void check_requirement(char* line) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800611 char *val[MAX_OPTIONS];
Elliott Hughes5620d222018-03-28 08:20:00 -0700612 unsigned count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800613 char *x;
614 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800615
Elliott Hughes5620d222018-03-28 08:20:00 -0700616 // "require product=alpha|beta|gamma"
617 // "require version-bootloader=1234"
618 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
619 // "require partition-exists=vendor"
620
621 char* name = line;
622 const char* product = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800623 if (!strncmp(name, "reject ", 7)) {
624 name += 7;
625 invert = 1;
626 } else if (!strncmp(name, "require ", 8)) {
627 name += 8;
628 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700629 } else if (!strncmp(name, "require-for-product:", 20)) {
630 // Get the product and point name past it
Elliott Hughes5620d222018-03-28 08:20:00 -0700631 product = name + 20;
Wink Savilleb98762f2011-04-04 17:54:59 -0700632 name = strchr(name, ' ');
Elliott Hughes5620d222018-03-28 08:20:00 -0700633 if (!name) die("android-info.txt syntax error: %s", line);
Wink Savilleb98762f2011-04-04 17:54:59 -0700634 *name = 0;
635 name += 1;
636 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800637 }
638
639 x = strchr(name, '=');
Elliott Hughes5620d222018-03-28 08:20:00 -0700640 if (x == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800641 *x = 0;
642 val[0] = x + 1;
643
Elliott Hughes5620d222018-03-28 08:20:00 -0700644 name = strip(name);
645
646 // "require partition-exists=x" is a special case, added because of the trouble we had when
647 // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
648 // missing out new partitions. A device with new partitions can use "partition-exists" to
David Anderson0debda02018-08-28 13:54:03 -0700649 // override the fields `optional_if_no_image` in the `images` array.
Elliott Hughes5620d222018-03-28 08:20:00 -0700650 if (!strcmp(name, "partition-exists")) {
651 const char* partition_name = val[0];
652 std::string has_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500653 if (!fb_getvar(std::string("has-slot:") + partition_name, &has_slot) ||
Elliott Hughes5620d222018-03-28 08:20:00 -0700654 (has_slot != "yes" && has_slot != "no")) {
655 die("device doesn't have required partition %s!", partition_name);
656 }
657 bool known_partition = false;
658 for (size_t i = 0; i < arraysize(images); ++i) {
659 if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700660 images[i].optional_if_no_image = false;
Elliott Hughes5620d222018-03-28 08:20:00 -0700661 known_partition = true;
662 }
663 }
664 if (!known_partition) {
665 die("device requires partition %s which is not known to this version of fastboot",
666 partition_name);
667 }
668 return;
669 }
670
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800671 for(count = 1; count < MAX_OPTIONS; count++) {
672 x = strchr(val[count - 1],'|');
673 if (x == 0) break;
674 *x = 0;
675 val[count] = x + 1;
676 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800677
Elliott Hughes253c18d2015-03-18 22:47:09 -0700678 // Work around an unfortunate name mismatch.
Elliott Hughes5620d222018-03-28 08:20:00 -0700679 const char* var = name;
680 if (!strcmp(name, "board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800681
Elliott Hughes253c18d2015-03-18 22:47:09 -0700682 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
Elliott Hughes5620d222018-03-28 08:20:00 -0700683 if (out == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800684
Elliott Hughes5620d222018-03-28 08:20:00 -0700685 for (size_t i = 0; i < count; ++i) {
686 out[i] = xstrdup(strip(val[i]));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800687 }
688
Elliott Hughes5620d222018-03-28 08:20:00 -0700689 fb_queue_require(product, var, invert, count, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690}
691
Aaron Wisnerdb511202018-06-26 15:38:35 -0500692static void check_requirements(char* data, int64_t sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700693 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700695 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800696 *s++ = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500697 check_requirement(data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800698 data = s;
699 } else {
700 s++;
701 }
702 }
Aaron Wisnerdb511202018-06-26 15:38:35 -0500703 if (fb_execute_queue()) die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800704}
705
Elliott Hughesfc797672015-04-07 20:12:50 -0700706static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707 fb_queue_notice("--------------------------------------------");
Elliott Hughes5620d222018-03-28 08:20:00 -0700708 fb_queue_display("Bootloader Version...", "version-bootloader");
709 fb_queue_display("Baseband Version.....", "version-baseband");
710 fb_queue_display("Serial Number........", "serialno");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800711 fb_queue_notice("--------------------------------------------");
712}
713
Tao Bao41cf35f2018-04-24 10:54:21 -0700714static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700715 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700716 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700717
Tao Bao41cf35f2018-04-24 10:54:21 -0700718 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
719 die("invalid max size %" PRId64, max_size);
720 }
721
Elliott Hughesfc797672015-04-07 20:12:50 -0700722 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700723 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700724
Elliott Hughes253c18d2015-03-18 22:47:09 -0700725 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700726 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700727
728 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700729 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700730
731 return out_s;
732}
733
Aaron Wisnerdb511202018-06-26 15:38:35 -0500734static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700735 std::string max_download_size;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500736 if (!fb_getvar("max-download-size", &max_download_size) ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700737 max_download_size.empty()) {
738 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700739 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700740 }
741
Elliott Hughes77c0e662015-11-02 15:51:12 -0800742 // Some bootloaders (angler, for example) send spurious whitespace too.
743 max_download_size = android::base::Trim(max_download_size);
744
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700745 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700746 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800747 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700748 return 0;
749 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700750 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700751 return limit;
752}
753
Aaron Wisnerdb511202018-06-26 15:38:35 -0500754static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700755 int64_t limit = sparse_limit;
756 if (limit == 0) {
757 // Unlimited, so see what the target device's limit is.
758 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700759 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500760 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700761 }
762 if (target_sparse_limit > 0) {
763 limit = target_sparse_limit;
764 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700765 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700766 }
767 }
768
769 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500770 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700771 }
772
773 return 0;
774}
775
Aaron Wisnerdb511202018-06-26 15:38:35 -0500776static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700777 int64_t sz = get_file_size(fd);
778 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700779 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700780 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700781
David Anderson32e376f2018-08-16 13:43:11 -0700782 if (sparse_file* s = sparse_file_import_auto(fd, false, false)) {
783 buf->image_size = sparse_file_len(s, false, false);
784 sparse_file_destroy(s);
785 } else {
786 buf->image_size = sz;
787 }
788
Elliott Hughesfc797672015-04-07 20:12:50 -0700789 lseek64(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500790 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700791 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700792 sparse_file** s = load_sparse_files(fd, limit);
793 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700794 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700795 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700796 buf->type = FB_BUFFER_SPARSE;
797 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700798 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500799 buf->type = FB_BUFFER_FD;
800 buf->data = nullptr;
801 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000802 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700803 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700804
Elliott Hughes53ec4952016-05-11 12:39:27 -0700805 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700806}
807
Aaron Wisnerdb511202018-06-26 15:38:35 -0500808static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500809 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
810
Elliott Hughes53ec4952016-05-11 12:39:27 -0700811 if (fd == -1) {
812 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700813 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500814
815 struct stat s;
816 if (fstat(fd, &s)) {
817 return false;
818 }
819 if (!S_ISREG(s.st_mode)) {
820 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
821 return false;
822 }
823
Aaron Wisnerdb511202018-06-26 15:38:35 -0500824 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700825}
826
David Zeuthenb6ea4352017-08-07 14:29:26 -0400827static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
828 // Buffer needs to be at least the size of the VBMeta struct which
829 // is 256 bytes.
830 if (buf->sz < 256) {
831 return;
832 }
833
Elliott Hughes855cdf82018-04-02 14:24:03 -0700834 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400835
836 std::string data;
837 if (!android::base::ReadFdToString(buf->fd, &data)) {
838 die("Failed reading from vbmeta");
839 }
840
841 // There's a 32-bit big endian |flags| field at offset 120 where
842 // bit 0 corresponds to disable-verity and bit 1 corresponds to
843 // disable-verification.
844 //
845 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
846 // the VBMeta struct.
847 if (g_disable_verity) {
848 data[123] |= 0x01;
849 }
850 if (g_disable_verification) {
851 data[123] |= 0x02;
852 }
853
854 if (!android::base::WriteStringToFd(data, fd)) {
855 die("Failed writing to modified vbmeta");
856 }
857 close(buf->fd);
858 buf->fd = fd;
859 lseek(fd, 0, SEEK_SET);
860}
861
Elliott Hughes5620d222018-03-28 08:20:00 -0700862static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700863{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700864 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700865
David Zeuthenb6ea4352017-08-07 14:29:26 -0400866 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
867 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700868 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400869 rewrite_vbmeta_buffer(buf);
870 }
871
Rom Lemarchand622810c2013-06-28 09:54:59 -0700872 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800873 case FB_BUFFER_SPARSE: {
874 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700875 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700876 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700877 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800878 sparse_files.emplace_back(*s, sz);
879 ++s;
880 }
881
882 for (size_t i = 0; i < sparse_files.size(); ++i) {
883 const auto& pair = sparse_files[i];
Elliott Hughes5620d222018-03-28 08:20:00 -0700884 fb_queue_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700885 }
886 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800887 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500888 case FB_BUFFER_FD:
Elliott Hughes5620d222018-03-28 08:20:00 -0700889 fb_queue_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700890 break;
891 default:
892 die("unknown buffer type: %d", buf->type);
893 }
894}
895
Aaron Wisnerdb511202018-06-26 15:38:35 -0500896static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700897 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500898 if (!fb_getvar("current-slot", &current_slot)) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700899 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700900}
901
Aaron Wisnerdb511202018-06-26 15:38:35 -0500902static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700903 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700904 int count = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500905 if (!fb_getvar("slot-count", &var) || !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700906 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700907 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700908 return count;
909}
910
Aaron Wisnerdb511202018-06-26 15:38:35 -0500911static bool supports_AB() {
912 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700913}
914
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700915// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700916static std::string get_other_slot(const std::string& current_slot, int count) {
917 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700918
Daniel Rosenberg80919472016-06-30 19:25:31 -0700919 char next = (current_slot[0] - 'a' + 1)%count + 'a';
920 return std::string(1, next);
921}
922
Aaron Wisnerdb511202018-06-26 15:38:35 -0500923static std::string get_other_slot(const std::string& current_slot) {
924 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -0700925}
926
Aaron Wisnerdb511202018-06-26 15:38:35 -0500927static std::string get_other_slot(int count) {
928 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700929}
930
Aaron Wisnerdb511202018-06-26 15:38:35 -0500931static std::string get_other_slot() {
932 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -0700933}
934
Aaron Wisnerdb511202018-06-26 15:38:35 -0500935static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700936 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700937 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800938 if (allow_all) {
939 return "all";
940 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500941 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700942 if (count > 0) {
943 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800944 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -0700945 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800946 }
947 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700948 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800949
Aaron Wisnerdb511202018-06-26 15:38:35 -0500950 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -0700951 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800952
Daniel Rosenberg80919472016-06-30 19:25:31 -0700953 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500954 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700955 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700956 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800957 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700958 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800959 }
960
Daniel Rosenberg80919472016-06-30 19:25:31 -0700961 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
962
963 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
964 for (int i=0; i<count; i++) {
965 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700966 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700967
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700968 exit(1);
969}
970
Aaron Wisnerdb511202018-06-26 15:38:35 -0500971static std::string verify_slot(const std::string& slot) {
972 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800973}
974
Aaron Wisnerdb511202018-06-26 15:38:35 -0500975static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700976 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800977 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700978 std::string current_slot;
979
Aaron Wisnerdb511202018-06-26 15:38:35 -0500980 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800981 /* If has-slot is not supported, the answer is no. */
982 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700983 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800984 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700985 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500986 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700987 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700988 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700989 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700990 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700991 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700992 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700993 }
994 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700995 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -0700996 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -0700997 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700998 }
999 func(part);
1000 }
1001}
1002
David Pursell0b156632015-10-30 11:22:01 -07001003/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1004 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1005 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1006 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001007 */
Aaron Wisnerdb511202018-06-26 15:38:35 -05001008static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001009 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001010 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001011
Daniel Rosenberg80919472016-06-30 19:25:31 -07001012 if (slot == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001013 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001014 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001015 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001016 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001017 for (int i=0; i < get_slot_count(); i++) {
1018 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001019 }
1020 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001021 do_for_partition(part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001022 }
1023 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001024 do_for_partition(part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001025 }
1026}
1027
Aaron Wisnerdb511202018-06-26 15:38:35 -05001028static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001029 struct fastboot_buffer buf;
1030
Aaron Wisnerdb511202018-06-26 15:38:35 -05001031 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001032 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001033 }
1034 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001035}
1036
Daniel Rosenberg13454092016-06-27 19:43:11 -07001037// Sets slot_override as the active slot. If slot_override is blank,
1038// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001039static void set_active(const std::string& slot_override) {
1040 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001041
Daniel Rosenberg80919472016-06-30 19:25:31 -07001042 if (slot_override != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001043 fb_set_active(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001044 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001045 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001046 if (current_slot != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001047 fb_set_active(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001048 }
1049 }
1050}
1051
David Anderson32e376f2018-08-16 13:43:11 -07001052static bool is_userspace_fastboot() {
1053 std::string value;
1054 return fb_getvar("is-userspace", &value) && value == "yes";
1055}
1056
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001057static bool if_partition_exists(const std::string& partition, const std::string& slot) {
1058 std::string has_slot;
1059 std::string partition_name = partition;
1060
1061 if (fb_getvar("has-slot:" + partition, &has_slot) && has_slot == "yes") {
1062 if (slot == "") {
1063 std::string current_slot = get_current_slot();
1064 if (current_slot == "") {
1065 die("Failed to identify current slot");
1066 }
1067 partition_name += "_" + current_slot;
1068 } else {
1069 partition_name += "_" + slot;
1070 }
1071 }
1072 std::string partition_size;
1073 return fb_getvar("partition-size:" + partition_name, &partition_size);
1074}
1075
David Anderson32e376f2018-08-16 13:43:11 -07001076static bool is_logical(const std::string& partition) {
1077 std::string value;
1078 return fb_getvar("is-logical:" + partition, &value) && value == "yes";
1079}
1080
David Anderson1d887432018-08-27 16:47:32 -07001081static void reboot_to_userspace_fastboot() {
1082 if (!fb_reboot_to_userspace()) {
1083 die("Must reboot to userspace fastboot to flash logical partitions");
1084 }
1085
1086 // Give the current connection time to close.
1087 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
1088
1089 fb_reinit(open_device());
1090}
1091
David Andersoncf444f32018-08-29 14:15:49 -07001092class ImageSource {
1093 public:
1094 virtual void* ReadFile(const std::string& name, int64_t* size) const = 0;
1095 virtual int OpenFile(const std::string& name) const = 0;
1096};
David Anderson32e376f2018-08-16 13:43:11 -07001097
David Andersoncf444f32018-08-29 14:15:49 -07001098class FlashAllTool {
1099 public:
1100 FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe);
David Anderson32e376f2018-08-16 13:43:11 -07001101
David Andersoncf444f32018-08-29 14:15:49 -07001102 void Flash();
David Anderson32e376f2018-08-16 13:43:11 -07001103
David Andersoncf444f32018-08-29 14:15:49 -07001104 private:
1105 void CheckRequirements();
1106 void DetermineSecondarySlot();
1107 void CollectImages();
1108 void FlashImages(const std::vector<std::pair<const Image*, std::string>>& images);
1109 void FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf);
1110 void UpdateSuperPartition();
David Anderson32e376f2018-08-16 13:43:11 -07001111
David Andersoncf444f32018-08-29 14:15:49 -07001112 const ImageSource& source_;
1113 std::string slot_override_;
1114 bool skip_secondary_;
1115 bool wipe_;
1116 std::string secondary_slot_;
1117 std::vector<std::pair<const Image*, std::string>> boot_images_;
1118 std::vector<std::pair<const Image*, std::string>> os_images_;
1119};
1120
1121FlashAllTool::FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe)
1122 : source_(source),
1123 slot_override_(slot_override),
1124 skip_secondary_(skip_secondary),
1125 wipe_(wipe)
1126{
David Anderson32e376f2018-08-16 13:43:11 -07001127}
1128
David Andersoncf444f32018-08-29 14:15:49 -07001129void FlashAllTool::Flash() {
1130 CheckRequirements();
1131 DetermineSecondarySlot();
1132 CollectImages();
David Anderson32e376f2018-08-16 13:43:11 -07001133
David Anderson95d40932018-08-28 12:14:33 -07001134 // First flash boot partitions. We allow this to happen either in userspace
1135 // or in bootloader fastboot.
David Andersoncf444f32018-08-29 14:15:49 -07001136 FlashImages(boot_images_);
David Anderson95d40932018-08-28 12:14:33 -07001137
1138 // Sync the super partition. This will reboot to userspace fastboot if needed.
David Andersoncf444f32018-08-29 14:15:49 -07001139 UpdateSuperPartition();
David Anderson95d40932018-08-28 12:14:33 -07001140
1141 // Resize any logical partition to 0, so each partition is reset to 0
1142 // extents, and will achieve more optimal allocation.
David Andersoncf444f32018-08-29 14:15:49 -07001143 for (const auto& [image, slot] : os_images_) {
David Anderson32e376f2018-08-16 13:43:11 -07001144 auto resize_partition = [](const std::string& partition) -> void {
1145 if (is_logical(partition)) {
1146 fb_queue_resize_partition(partition, "0");
1147 }
1148 };
David Anderson95d40932018-08-28 12:14:33 -07001149 do_for_partitions(image->part_name, slot, resize_partition, false);
David Anderson32e376f2018-08-16 13:43:11 -07001150 }
1151
David Anderson95d40932018-08-28 12:14:33 -07001152 // Flash OS images, resizing logical partitions as needed.
David Andersoncf444f32018-08-29 14:15:49 -07001153 FlashImages(os_images_);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001154
David Andersoncf444f32018-08-29 14:15:49 -07001155 if (slot_override_ == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001156 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001157 } else {
David Andersoncf444f32018-08-29 14:15:49 -07001158 set_active(slot_override_);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001159 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001160}
1161
David Andersoncf444f32018-08-29 14:15:49 -07001162void FlashAllTool::CheckRequirements() {
1163 int64_t sz;
1164 void* data = source_.ReadFile("android-info.txt", &sz);
1165 if (data == nullptr) {
1166 die("could not read android-info.txt");
1167 }
1168 check_requirements(reinterpret_cast<char*>(data), sz);
1169}
1170
1171void FlashAllTool::DetermineSecondarySlot() {
1172 if (skip_secondary_) {
1173 return;
1174 }
1175 if (slot_override_ != "") {
1176 secondary_slot_ = get_other_slot(slot_override_);
1177 } else {
1178 secondary_slot_ = get_other_slot();
1179 }
1180 if (secondary_slot_ == "") {
1181 if (supports_AB()) {
1182 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1183 }
1184 skip_secondary_ = true;
1185 }
1186}
1187
1188void FlashAllTool::CollectImages() {
1189 for (size_t i = 0; i < arraysize(images); ++i) {
1190 std::string slot = slot_override_;
1191 if (images[i].IsSecondary()) {
1192 if (skip_secondary_) {
1193 continue;
1194 }
1195 slot = secondary_slot_;
1196 }
1197 if (images[i].type == ImageType::BootCritical) {
1198 boot_images_.emplace_back(&images[i], slot);
1199 } else if (images[i].type == ImageType::Normal) {
1200 os_images_.emplace_back(&images[i], slot);
1201 }
1202 }
1203}
1204
1205void FlashAllTool::FlashImages(const std::vector<std::pair<const Image*, std::string>>& images) {
1206 for (const auto& [image, slot] : images) {
1207 fastboot_buffer buf;
1208 int fd = source_.OpenFile(image->img_name);
1209 if (fd < 0 || !load_buf_fd(fd, &buf)) {
1210 if (image->optional_if_no_image) {
1211 continue;
1212 }
1213 die("could not load '%s': %s", image->img_name, strerror(errno));
1214 }
1215 FlashImage(*image, slot, &buf);
1216 }
1217}
1218
1219void FlashAllTool::FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf) {
1220 auto flash = [&, this](const std::string& partition_name) {
1221 int64_t sz;
1222 void* data = source_.ReadFile(image.sig_name, &sz);
1223 if (data) {
1224 fb_queue_download("signature", data, sz);
1225 fb_queue_command("signature", "installing signature");
1226 }
1227
1228 if (is_logical(partition_name)) {
1229 fb_queue_resize_partition(partition_name, std::to_string(buf->image_size));
1230 }
1231 flash_buf(partition_name.c_str(), buf);
1232 };
1233 do_for_partitions(image.part_name, slot, flash, false);
1234}
1235
1236void FlashAllTool::UpdateSuperPartition() {
1237 if (!if_partition_exists("super", "")) {
1238 return;
1239 }
1240
1241 int fd = source_.OpenFile("super_empty.img");
1242 if (fd < 0) {
1243 return;
1244 }
1245 if (!is_userspace_fastboot()) {
1246 reboot_to_userspace_fastboot();
1247 }
1248 fb_queue_download_fd("super", fd, get_file_size(fd));
1249
1250 std::string command = "update-super:super";
1251 if (wipe_) {
1252 command += ":wipe";
1253 }
1254 fb_queue_command(command, "Updating super partition");
1255
1256 // We need these commands to have finished before proceeding, since
1257 // otherwise "getvar is-logical" may not return a correct answer below.
1258 fb_execute_queue();
1259}
1260
1261class ZipImageSource final : public ImageSource {
1262 public:
1263 explicit ZipImageSource(ZipArchiveHandle zip) : zip_(zip) {}
1264 void* ReadFile(const std::string& name, int64_t* size) const override;
1265 int OpenFile(const std::string& name) const override;
1266
1267 private:
1268 ZipArchiveHandle zip_;
1269};
1270
1271void* ZipImageSource::ReadFile(const std::string& name, int64_t* size) const {
1272 return unzip_to_memory(zip_, name.c_str(), size);
1273}
1274
1275int ZipImageSource::OpenFile(const std::string& name) const {
1276 return unzip_to_file(zip_, name.c_str());
1277}
1278
1279static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
1280 queue_info_dump();
1281
1282 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1283
1284 ZipArchiveHandle zip;
1285 int error = OpenArchive(filename, &zip);
1286 if (error != 0) {
1287 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
1288 }
1289
1290 FlashAllTool tool(ZipImageSource(zip), slot_override, skip_secondary, false);
1291 tool.Flash();
1292
1293 CloseArchive(zip);
1294}
1295
1296class LocalImageSource final : public ImageSource {
1297 public:
1298 void* ReadFile(const std::string& name, int64_t* size) const override;
1299 int OpenFile(const std::string& name) const override;
1300};
1301
1302void* LocalImageSource::ReadFile(const std::string& name, int64_t* size) const {
1303 auto path = find_item_given_name(name);
1304 if (path.empty()) {
1305 return nullptr;
1306 }
1307 return load_file(path.c_str(), size);
1308}
1309
1310int LocalImageSource::OpenFile(const std::string& name) const {
1311 auto path = find_item_given_name(name);
1312 return open(path.c_str(), O_RDONLY);
1313}
1314
1315static void do_flashall(const std::string& slot_override, bool skip_secondary, bool wipe) {
1316 std::string fname;
1317 queue_info_dump();
1318
1319 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1320
1321 FlashAllTool tool(LocalImageSource(), slot_override, skip_secondary, wipe);
1322 tool.Flash();
1323}
1324
Elliott Hughesd6365a72017-05-08 18:04:49 -07001325static std::string next_arg(std::vector<std::string>* args) {
1326 if (args->empty()) syntax_error("expected argument");
1327 std::string result = args->front();
1328 args->erase(args->begin());
1329 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001330}
1331
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001332static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001333 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001334
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001335 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001336 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001337 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001338 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001339 fb_queue_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001340}
1341
Connor O'Brience16a8a2017-02-06 14:39:31 -08001342static std::string fb_fix_numeric_var(std::string var) {
1343 // Some bootloaders (angler, for example), send spurious leading whitespace.
1344 var = android::base::Trim(var);
1345 // Some bootloaders (hammerhead, for example) use implicit hex.
1346 // This code used to use strtol with base 16.
1347 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1348 return var;
1349}
1350
Aaron Wisnerdb511202018-06-26 15:38:35 -05001351static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001352 std::string sizeString;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001353 if (!fb_getvar(name, &sizeString) || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001354 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001355 return 0;
1356 }
1357 sizeString = fb_fix_numeric_var(sizeString);
1358
1359 unsigned size;
1360 if (!android::base::ParseUint(sizeString, &size)) {
1361 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1362 return 0;
1363 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001364 if ((size & (size - 1)) != 0) {
1365 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001366 return 0;
1367 }
1368 return size;
1369}
1370
Aaron Wisnerdb511202018-06-26 15:38:35 -05001371static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001372 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001373 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001374 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001375 std::string partition_type, partition_size;
1376
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001377 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001378 const char* errMsg = nullptr;
1379 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001380 TemporaryFile output;
1381 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001382
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001383 unsigned int limit = INT_MAX;
1384 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001385 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001386 }
1387 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001388 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001389 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001390
Aaron Wisnerdb511202018-06-26 15:38:35 -05001391 if (!fb_getvar("partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001392 errMsg = "Can't determine partition type.\n";
1393 goto failed;
1394 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001395 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001396 if (partition_type != type_override) {
1397 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001398 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001399 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001400 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001401 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001402
Aaron Wisnerdb511202018-06-26 15:38:35 -05001403 if (!fb_getvar("partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001404 errMsg = "Unable to get partition size\n";
1405 goto failed;
1406 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001407 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001408 if (partition_size != size_override) {
1409 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001410 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001411 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001412 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001413 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001414 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001415
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001416 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001417 if (!gen) {
1418 if (skip_if_not_supported) {
1419 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001420 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001421 return;
1422 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001423 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1424 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001425 return;
1426 }
1427
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001428 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001429 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001430 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1431 return;
1432 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001433
Connor O'Brience16a8a2017-02-06 14:39:31 -08001434 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001435 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1436 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001437
Jin Qian4a335822017-04-18 16:23:18 -07001438 if (fs_generator_generate(gen, output.path, size, initial_dir,
1439 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001440 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001441 return;
1442 }
1443
Jin Qian4a335822017-04-18 16:23:18 -07001444 fd.reset(open(output.path, O_RDONLY));
1445 if (fd == -1) {
1446 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1447 return;
1448 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001449 if (!load_buf_fd(fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001450 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001451 return;
1452 }
1453 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001454 return;
1455
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001456failed:
1457 if (skip_if_not_supported) {
1458 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001459 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001460 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001461 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001462}
1463
Aaron Wisnerdb511202018-06-26 15:38:35 -05001464int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001465 bool wants_wipe = false;
1466 bool wants_reboot = false;
1467 bool wants_reboot_bootloader = false;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001468 bool wants_reboot_recovery = false;
1469 bool wants_reboot_fastboot = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001470 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001471 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001472 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001473 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001474 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001475 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001476 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001477 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001478 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001479
Elliott Hughes577e8b42018-04-05 16:12:47 -07001480 g_boot_img_hdr.kernel_addr = 0x00008000;
1481 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1482 g_boot_img_hdr.second_addr = 0x00f00000;
1483 g_boot_img_hdr.tags_addr = 0x00000100;
1484 g_boot_img_hdr.page_size = 2048;
1485
JP Abgrall7b8970c2013-03-07 17:06:41 -08001486 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001487 {"base", required_argument, 0, 0},
1488 {"cmdline", required_argument, 0, 0},
1489 {"disable-verification", no_argument, 0, 0},
1490 {"disable-verity", no_argument, 0, 0},
1491 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001492 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001493 {"kernel-offset", required_argument, 0, 0},
1494 {"os-patch-level", required_argument, 0, 0},
1495 {"os-version", required_argument, 0, 0},
1496 {"page-size", required_argument, 0, 0},
1497 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001498 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001499 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001500 {"skip-secondary", no_argument, 0, 0},
1501 {"slot", required_argument, 0, 0},
1502 {"tags-offset", required_argument, 0, 0},
1503 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001504 {"verbose", no_argument, 0, 'v'},
1505 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001506#if !defined(_WIN32)
1507 {"wipe-and-use-fbe", no_argument, 0, 0},
1508#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001509 {0, 0, 0, 0}
1510 };
Colin Cross8879f982012-05-22 17:53:34 -07001511
1512 serial = getenv("ANDROID_SERIAL");
1513
Elliott Hughes577e8b42018-04-05 16:12:47 -07001514 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001515 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001516 if (c == 0) {
1517 std::string name{longopts[longindex].name};
1518 if (name == "base") {
1519 g_base_addr = strtoul(optarg, 0, 16);
1520 } else if (name == "cmdline") {
1521 g_cmdline = optarg;
1522 } else if (name == "disable-verification") {
1523 g_disable_verification = true;
1524 } else if (name == "disable-verity") {
1525 g_disable_verity = true;
1526 } else if (name == "header-version") {
1527 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1528 } else if (name == "kernel-offset") {
1529 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1530 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001531 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001532 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001533 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001534 } else if (name == "page-size") {
1535 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1536 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1537 } else if (name == "ramdisk-offset") {
1538 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1539 } else if (name == "skip-reboot") {
1540 skip_reboot = true;
1541 } else if (name == "skip-secondary") {
1542 skip_secondary = true;
1543 } else if (name == "slot") {
1544 slot_override = optarg;
1545 } else if (name == "tags-offset") {
1546 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1547 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001548 setvbuf(stdout, nullptr, _IONBF, 0);
1549 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001550 } else if (name == "version") {
Elliott Hughes4d4f64f2017-05-08 11:30:34 -07001551 fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001552 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001553 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001554#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001555 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001556 wants_wipe = true;
1557 set_fbe_marker = true;
1558#endif
1559 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001560 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001561 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001562 } else {
1563 switch (c) {
1564 case 'a':
1565 wants_set_active = true;
1566 if (optarg) next_active = optarg;
1567 break;
1568 case 'h':
1569 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001570 case 'l':
1571 g_long_listing = true;
1572 break;
1573 case 's':
1574 serial = optarg;
1575 break;
1576 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001577 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1578 die("invalid sparse limit %s", optarg);
1579 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001580 break;
1581 case 'v':
1582 set_verbose();
1583 break;
1584 case 'w':
1585 wants_wipe = true;
1586 break;
1587 case '?':
1588 return 1;
1589 default:
1590 abort();
1591 }
Colin Cross8879f982012-05-22 17:53:34 -07001592 }
1593 }
1594
1595 argc -= optind;
1596 argv += optind;
1597
Elliott Hughesd6365a72017-05-08 18:04:49 -07001598 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001599
Colin Cross8fb6e062012-07-24 16:36:41 -07001600 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001601 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001602 return 0;
1603 }
1604
Colin Crossc7b75dc2012-08-29 18:17:06 -07001605 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001606 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001607 }
1608
David Pursell0b156632015-10-30 11:22:01 -07001609 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001610 if (transport == nullptr) {
1611 return 1;
1612 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001613 fastboot::FastBootDriver fb(transport);
1614 fb_init(fb);
David Pursell2ec418a2016-01-20 08:32:08 -08001615
Elliott Hughes5620d222018-03-28 08:20:00 -07001616 const double start = now();
1617
Aaron Wisnerdb511202018-06-26 15:38:35 -05001618 if (slot_override != "") slot_override = verify_slot(slot_override);
1619 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001620
1621 if (wants_set_active) {
1622 if (next_active == "") {
1623 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001624 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001625 if (fb_getvar("current-slot", &current_slot)) {
1626 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001627 } else {
1628 wants_set_active = false;
1629 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001630 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001631 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001632 }
1633 }
1634 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001635
Elliott Hughesd6365a72017-05-08 18:04:49 -07001636 std::vector<std::string> args(argv, argv + argc);
1637 while (!args.empty()) {
1638 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001639
Elliott Hughesd6365a72017-05-08 18:04:49 -07001640 if (command == "getvar") {
1641 std::string variable = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001642 fb_queue_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001643 } else if (command == "erase") {
1644 std::string partition = next_arg(&args);
1645 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001646 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001647 if (fb_getvar(std::string("partition-type:") + partition,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001648 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001649 fs_get_generator(partition_type) != nullptr) {
1650 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1651 partition_type.c_str());
1652 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001653
Elliott Hughes5620d222018-03-28 08:20:00 -07001654 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001655 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001656 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001657 } else if (android::base::StartsWith(command, "format")) {
1658 // Parsing for: "format[:[type][:[size]]]"
1659 // Some valid things:
1660 // - select only the size, and leave default fs type:
1661 // format::0x4000000 userdata
1662 // - default fs type and size:
1663 // format userdata
1664 // format:: userdata
1665 std::vector<std::string> pieces = android::base::Split(command, ":");
1666 std::string type_override;
1667 if (pieces.size() > 1) type_override = pieces[1].c_str();
1668 std::string size_override;
1669 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001670
Elliott Hughesd6365a72017-05-08 18:04:49 -07001671 std::string partition = next_arg(&args);
1672
1673 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001674 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001675 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001676 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001677 } else if (command == "signature") {
1678 std::string filename = next_arg(&args);
1679 data = load_file(filename.c_str(), &sz);
1680 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -07001681 if (sz != 256) die("signature must be 256 bytes (got %" PRId64 ")", sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001682 fb_queue_download("signature", data, sz);
1683 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001684 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001685 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001686
1687 if (args.size() == 1) {
1688 std::string what = next_arg(&args);
1689 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001690 wants_reboot = false;
1691 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001692 } else if (what == "recovery") {
1693 wants_reboot = false;
1694 wants_reboot_recovery = true;
1695 } else if (what == "fastboot") {
1696 wants_reboot = false;
1697 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001698 } else {
1699 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001700 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001701
Elliott Hughesca85df02015-02-25 10:02:00 -08001702 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001703 if (!args.empty()) syntax_error("junk after reboot command");
1704 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001705 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001706 } else if (command == "reboot-recovery") {
1707 wants_reboot_recovery = true;
1708 } else if (command == "reboot-fastboot") {
1709 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001710 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001711 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001712 } else if (command == "boot") {
1713 std::string kernel = next_arg(&args);
1714 std::string ramdisk;
1715 if (!args.empty()) ramdisk = next_arg(&args);
1716 std::string second_stage;
1717 if (!args.empty()) second_stage = next_arg(&args);
1718
Elliott Hughes577e8b42018-04-05 16:12:47 -07001719 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001720 fb_queue_download("boot.img", data, sz);
1721 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001722 } else if (command == "flash") {
1723 std::string pname = next_arg(&args);
1724
Elliott Hughes2810d002016-04-25 14:31:18 -07001725 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001726 if (!args.empty()) {
1727 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001728 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001729 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001730 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001731 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001732
1733 auto flash = [&](const std::string &partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001734 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001735 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001736 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001737 } else if (command == "flash:raw") {
1738 std::string partition = next_arg(&args);
1739 std::string kernel = next_arg(&args);
1740 std::string ramdisk;
1741 if (!args.empty()) ramdisk = next_arg(&args);
1742 std::string second_stage;
1743 if (!args.empty()) second_stage = next_arg(&args);
1744
Elliott Hughes577e8b42018-04-05 16:12:47 -07001745 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001746 auto flashraw = [&](const std::string& partition) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001747 fb_queue_flash(partition, data, sz);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001748 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001749 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001750 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001751 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001752 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
David Anderson32e376f2018-08-16 13:43:11 -07001753 do_flashall(slot_override, true, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001754 } else {
David Anderson32e376f2018-08-16 13:43:11 -07001755 do_flashall(slot_override, skip_secondary, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001756 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001757 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001758 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001759 bool slot_all = (slot_override == "all");
1760 if (slot_all) {
1761 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1762 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001763 std::string filename = "update.zip";
1764 if (!args.empty()) {
1765 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001766 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001767 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001768 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001769 } else if (command == "set_active") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001770 std::string slot = verify_slot(next_arg(&args), false);
Elliott Hughes5620d222018-03-28 08:20:00 -07001771 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001772 } else if (command == "stage") {
1773 std::string filename = next_arg(&args);
1774
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001775 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001776 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001777 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001778 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001779 fb_queue_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001780 } else if (command == "get_staged") {
1781 std::string filename = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001782 fb_queue_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001783 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001784 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001785 } else if (command == "flashing") {
1786 if (args.empty()) {
1787 syntax_error("missing 'flashing' command");
1788 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1789 args[0] == "unlock_critical" ||
1790 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001791 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001792 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001793 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001794 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001795 }
David Anderson0d4277d2018-07-31 13:27:37 -07001796 } else if (command == "create-logical-partition") {
1797 std::string partition = next_arg(&args);
1798 std::string size = next_arg(&args);
1799 fb_queue_create_partition(partition, size);
1800 } else if (command == "delete-logical-partition") {
1801 std::string partition = next_arg(&args);
1802 fb_queue_delete_partition(partition);
1803 } else if (command == "resize-logical-partition") {
1804 std::string partition = next_arg(&args);
1805 std::string size = next_arg(&args);
1806 fb_queue_resize_partition(partition, size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001807 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001808 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001809 }
1810 }
1811
1812 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001813 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1814 for (const auto& partition : partitions) {
1815 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001816 if (!fb_getvar(std::string{"partition-type:"} + partition, &partition_type)) continue;
Paul Crowley4d170062018-04-24 17:06:30 -07001817 if (partition_type.empty()) continue;
1818 fb_queue_erase(partition);
1819 if (partition == "userdata" && set_fbe_marker) {
1820 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1821 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001822 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001823 delete_fbemarker_tmpdir(initial_userdata_dir);
1824 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001825 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001826 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001827 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001828 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001829 if (wants_set_active) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001830 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001831 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001832 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001833 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001834 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001835 } else if (wants_reboot_bootloader) {
1836 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001837 fb_queue_wait_for_disconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001838 } else if (wants_reboot_recovery) {
1839 fb_queue_command("reboot-recovery", "rebooting into recovery");
1840 fb_queue_wait_for_disconnect();
1841 } else if (wants_reboot_fastboot) {
1842 fb_queue_command("reboot-fastboot", "rebooting into fastboot");
1843 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001844 }
1845
Aaron Wisnerdb511202018-06-26 15:38:35 -05001846 int status = fb_execute_queue() ? EXIT_FAILURE : EXIT_SUCCESS;
Elliott Hughes5620d222018-03-28 08:20:00 -07001847 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
1848 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001849}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001850
Aaron Wisnerdb511202018-06-26 15:38:35 -05001851void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001852 unsigned year, month, day;
1853 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1854 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1855 }
1856 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1857 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1858 hdr->SetOsPatchLevel(year, month);
1859}
1860
Aaron Wisnerdb511202018-06-26 15:38:35 -05001861void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001862 unsigned major = 0, minor = 0, patch = 0;
1863 std::vector<std::string> versions = android::base::Split(arg, ".");
1864 if (versions.size() < 1 || versions.size() > 3 ||
1865 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1866 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1867 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1868 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1869 syntax_error("bad OS version: %s", arg);
1870 }
1871 hdr->SetOsVersion(major, minor, patch);
1872}