blob: 5666fa1e9f6ee0d3368a4b2c2bad068b9233ade4 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070029#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <errno.h>
31#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070032#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070033#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070034#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070035#include <stdint.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070041#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070042#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080043
Elliott Hughes290a2282016-11-14 17:08:47 -080044#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070045#include <functional>
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080047#include <utility>
48#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070049
Elliott Hughes82ff3152016-08-31 15:07:18 -070050#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070051#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080053#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070054#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080055#include <android-base/strings.h>
Jin Qian4a335822017-04-18 16:23:18 -070056#include <android-base/test_utils.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050057#include <android-base/unique_fd.h>
Colin Crossf8387882012-05-24 17:18:41 -070058#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070059#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Elliott Hughes253c18d2015-03-18 22:47:09 -070061#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080062#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070064#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080065#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070066#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080067#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070068#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Chris Fries0ea946c2017-04-12 10:25:57 -050070using android::base::unique_fd;
71
Colin Crossf8387882012-05-24 17:18:41 -070072#ifndef O_BINARY
73#define O_BINARY 0
74#endif
75
Wink Savilleb98762f2011-04-04 17:54:59 -070076char cur_product[FB_RESPONSE_SZ + 1];
77
David Pursell2ec418a2016-01-20 08:32:08 -080078static const char* serial = nullptr;
Elliott Hughes577e8b42018-04-05 16:12:47 -070079
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080static unsigned short vendor_id = 0;
Elliott Hughes577e8b42018-04-05 16:12:47 -070081static bool g_long_listing = false;
Chris Fries6a999712017-04-04 09:52:47 -050082// Don't resparse files in too-big chunks.
83// libsparse will support INT_MAX, but this results in large allocations, so
84// let's keep it at 1GB to avoid memory pressure on the host.
85static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Colin Crossf8387882012-05-24 17:18:41 -070086static int64_t sparse_limit = -1;
87static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088
Elliott Hughes577e8b42018-04-05 16:12:47 -070089static unsigned g_base_addr = 0x10000000;
90static boot_img_hdr_v1 g_boot_img_hdr = {};
91static std::string g_cmdline;
JP Abgrall7b8970c2013-03-07 17:06:41 -080092
David Zeuthenb6ea4352017-08-07 14:29:26 -040093static bool g_disable_verity = false;
94static bool g_disable_verification = false;
95
Paul Crowley8f7f56e2015-11-27 09:29:37 +000096static const std::string convert_fbe_marker_filename("convert_fbe");
97
Rom Lemarchand622810c2013-06-28 09:54:59 -070098enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -050099 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -0700100 FB_BUFFER_SPARSE,
101};
102
103struct fastboot_buffer {
104 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700105 void* data;
106 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500107 int fd;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700108};
109
110static struct {
Elliott Hughes45964a82017-05-03 22:43:23 -0700111 const char* nickname;
112 const char* img_name;
113 const char* sig_name;
114 const char* part_name;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700115 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700116 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700117} images[] = {
Elliott Hughes45964a82017-05-03 22:43:23 -0700118 // clang-format off
119 { "boot", "boot.img", "boot.sig", "boot", false, false },
120 { nullptr, "boot_other.img", "boot.sig", "boot", true, true },
121 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, false },
Dmitry Shmidtde8c08c2017-05-15 10:24:04 -0700122 { "dts", "dt.img", "dt.sig", "dts", true, false },
Bowgo Tsai017217e2018-03-29 01:24:12 +0800123 { "odm", "odm.img", "odm.sig", "odm", true, false },
Bowgo Tsai0afc6b02018-03-26 15:45:01 +0800124 { "product", "product.img", "product.sig", "product", true, false },
Elliott Hughes45964a82017-05-03 22:43:23 -0700125 { "recovery", "recovery.img", "recovery.sig", "recovery", true, false },
126 { "system", "system.img", "system.sig", "system", false, false },
127 { nullptr, "system_other.img", "system.sig", "system", true, true },
128 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, false },
129 { "vendor", "vendor.img", "vendor.sig", "vendor", true, false },
130 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, true },
131 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700132};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700133
Elliott Hughes45964a82017-05-03 22:43:23 -0700134static std::string find_item_given_name(const char* img_name) {
135 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700136 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700137 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700138 }
Alex Lightbb9b8a52016-06-29 09:26:44 -0700139 return android::base::StringPrintf("%s/%s", dir, img_name);
140}
141
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700142static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700143 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700144 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700145 return find_item_given_name(images[i].img_name);
146 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147 }
148
Elliott Hughesd6365a72017-05-08 18:04:49 -0700149 if (item == "userdata") return find_item_given_name("userdata.img");
150 if (item == "cache") return find_item_given_name("cache.img");
Elliott Hughes45964a82017-05-03 22:43:23 -0700151
Elliott Hughesd6365a72017-05-08 18:04:49 -0700152 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700153 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154}
155
Elliott Hughesfc797672015-04-07 20:12:50 -0700156static int64_t get_file_size(int fd) {
157 struct stat sb;
158 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700159}
160
Elliott Hughesfc797672015-04-07 20:12:50 -0700161static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800162 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700163 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164
Elliott Hughesfc797672015-04-07 20:12:50 -0700165 *sz = get_file_size(fd);
166 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700167 goto oops;
168 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169
Elliott Hughesfc797672015-04-07 20:12:50 -0700170 data = (char*) malloc(*sz);
171 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172
Elliott Hughesfc797672015-04-07 20:12:50 -0700173 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 close(fd);
175
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 return data;
177
178oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800179 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 close(fd);
181 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800182 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183 return 0;
184}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185
Elliott Hughes2810d002016-04-25 14:31:18 -0700186static void* load_file(const std::string& path, int64_t* sz) {
187 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700188 if (fd == -1) return nullptr;
189 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700190}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
Elliott Hughesfc797672015-04-07 20:12:50 -0700192static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700193 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700194 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700195 return -1;
196 }
197
198 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
199 return -1;
200 }
201
Scott Anderson13081c62012-04-06 12:39:30 -0700202 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700204 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
205 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 return 0;
207}
208
Elliott Hughesfc797672015-04-07 20:12:50 -0700209static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800210 return match_fastboot_with_serial(info, serial);
211}
212
Elliott Hughesfc797672015-04-07 20:12:50 -0700213static int list_devices_callback(usb_ifc_info* info) {
214 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800215 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700216 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800217 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700218 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 if (!serial[0]) {
220 serial = "????????????";
221 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700222 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700223 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800224 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700225 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800226 printf("%-22s fastboot", serial.c_str());
227 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700228 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800229 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230 }
231
232 return -1;
233}
234
David Pursell2ec418a2016-01-20 08:32:08 -0800235// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
236// a specific device, otherwise the first USB device found will be used.
237//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700238// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800239// Otherwise it blocks until the target is available.
240//
241// The returned Transport is a singleton, so multiple calls to this function will return the same
242// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700243static Transport* open_device() {
244 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800245 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246
David Pursell2ec418a2016-01-20 08:32:08 -0800247 if (transport != nullptr) {
248 return transport;
249 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800250
David Pursell4601c972016-02-05 15:35:09 -0800251 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800252 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800253 int port = 0;
254 if (serial != nullptr) {
255 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800256
David Pursell4601c972016-02-05 15:35:09 -0800257 if (android::base::StartsWith(serial, "tcp:")) {
258 protocol = Socket::Protocol::kTcp;
259 port = tcp::kDefaultPort;
260 net_address = serial + strlen("tcp:");
261 } else if (android::base::StartsWith(serial, "udp:")) {
262 protocol = Socket::Protocol::kUdp;
263 port = udp::kDefaultPort;
264 net_address = serial + strlen("udp:");
265 }
266
267 if (net_address != nullptr) {
268 std::string error;
269 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700270 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800271 }
David Pursell2ec418a2016-01-20 08:32:08 -0800272 }
273 }
274
275 while (true) {
276 if (!host.empty()) {
277 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800278 if (protocol == Socket::Protocol::kTcp) {
279 transport = tcp::Connect(host, port, &error).release();
280 } else if (protocol == Socket::Protocol::kUdp) {
281 transport = udp::Connect(host, port, &error).release();
282 }
283
David Pursell2ec418a2016-01-20 08:32:08 -0800284 if (transport == nullptr && announce) {
285 fprintf(stderr, "error: %s\n", error.c_str());
286 }
287 } else {
288 transport = usb_open(match_fastboot);
289 }
290
291 if (transport != nullptr) {
292 return transport;
293 }
294
David Pursell0b156632015-10-30 11:22:01 -0700295 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800296 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700297 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800299 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 }
301}
302
Elliott Hughesfc797672015-04-07 20:12:50 -0700303static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 // We don't actually open a USB device here,
305 // just getting our callback called so we can
306 // list all the connected devices.
307 usb_open(list_devices_callback);
308}
309
Elliott Hughesd6365a72017-05-08 18:04:49 -0700310static void syntax_error(const char* fmt, ...) {
311 fprintf(stderr, "fastboot: usage: ");
312
313 va_list ap;
314 va_start(ap, fmt);
315 vfprintf(stderr, fmt, ap);
316 va_end(ap);
317
318 fprintf(stderr, "\n");
319 exit(1);
320}
321
322static int show_help() {
323 // clang-format off
324 fprintf(stdout,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
Elliott Hughesb76188f2018-04-03 13:30:18 -0700326 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700328 "flashing:\n"
329 " update ZIP Flash all partitions from an update.zip package.\n"
330 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
331 " On A/B devices, flashed slot is set as active.\n"
332 " Secondary images may be flashed to inactive slot.\n"
333 " flash PARTITION [FILENAME]\n"
334 " Flash given partition only.\n"
335 "\n"
336 "basics:\n"
337 " devices [-l] List devices in bootloader (-l: with device paths).\n"
338 " getvar NAME Display given bootloader variable.\n"
339 " reboot [bootloader] Reboot device.\n"
340 "\n"
341 "locking/unlocking:\n"
342 " flashing lock|unlock Lock/unlock partitions for flashing\n"
343 " flashing lock_critical|unlock_critical\n"
344 " Lock/unlock 'critical' bootloader partitions.\n"
345 " flashing get_unlock_ability\n"
346 " Check whether unlocking is allowed (1) or not(0).\n"
347 "\n"
348 "advanced:\n"
349 " erase PARTITION Erase a flash partition.\n"
350 " format[:FS_TYPE[:SIZE]] PARTITION\n"
351 " Format a flash partition.\n"
352 " set_active SLOT Set the active slot.\n"
353 " oem [COMMAND...] Execute OEM-specific command.\n"
354 "\n"
355 "boot image:\n"
356 " boot KERNEL [RAMDISK [SECOND]]\n"
357 " Download and boot kernel from RAM.\n"
358 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
359 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700360 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700361 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
362 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
363 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
364 " --tags-offset Set tags offset (default: 0x00000100).\n"
365 " --page-size BYTES Set flash page size (default: 2048).\n"
366 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700367 " --os-version MAJOR[.MINOR[.PATCH]]\n"
368 " Set boot image OS version (default: 0.0.0).\n"
369 " --os-patch-level YYYY-MM-DD\n"
370 " Set boot image OS security patch level.\n"
371 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700372 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700373 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700374 //" continue Continue with autoboot.\n"
375 //"\n"
376 "Android Things:\n"
377 " stage IN_FILE Sends given file to stage for the next command.\n"
378 " 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 -0800379 "\n"
380 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700381 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700382 " -s SERIAL Specify a USB device.\n"
383 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
384 // TODO: remove -i?
385 " -i VENDOR_ID Filter devices by USB vendor id.\n"
386 " -S SIZE[K|M|G] Use sparse files above this limit (0 to disable).\n"
387 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
388 " non-current slot (default: current active slot).\n"
389 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
390 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
391 " --skip-reboot Don't reboot device after flashing.\n"
392 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
393 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000394#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700395 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000396#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700397 // TODO: remove --unbuffered?
398 " --unbuffered Don't buffer input or output.\n"
399 " --verbose, -v Verbose output.\n"
400 " --version Display version.\n"
401 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700403 // clang-format off
404 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000406
Elliott Hughesd6365a72017-05-08 18:04:49 -0700407static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
Elliott Hughes577e8b42018-04-05 16:12:47 -0700408 const std::string& second_stage, int64_t* sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700409 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700410 void* kdata = load_file(kernel.c_str(), &ksize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700411 if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800412
Elliott Hughesfc797672015-04-07 20:12:50 -0700413 // Is this actually a boot image?
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700414 if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800415 die("cannot load '%s': too short", kernel.c_str());
416 }
Elliott Hughesd6365a72017-05-08 18:04:49 -0700417 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700418 if (!g_cmdline.empty()) {
419 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), g_cmdline);
420 }
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700421 uint32_t header_version_existing =
422 reinterpret_cast<boot_img_hdr_v1*>(kdata)->header_version;
Elliott Hughes577e8b42018-04-05 16:12:47 -0700423 if (g_boot_img_hdr.header_version != header_version_existing) {
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700424 die("header version mismatch, expected: %" PRIu32 " found %" PRIu32 "",
Elliott Hughes577e8b42018-04-05 16:12:47 -0700425 g_boot_img_hdr.header_version, header_version_existing);
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700426 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800427
Elliott Hughes4089d342017-10-27 14:21:12 -0700428 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800429
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430 *sz = ksize;
431 return kdata;
432 }
433
Elliott Hughesfc797672015-04-07 20:12:50 -0700434 void* rdata = nullptr;
435 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700436 if (!ramdisk.empty()) {
437 rdata = load_file(ramdisk.c_str(), &rsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700438 if (rdata == nullptr) die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439 }
440
Elliott Hughesfc797672015-04-07 20:12:50 -0700441 void* sdata = nullptr;
442 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700443 if (!second_stage.empty()) {
444 sdata = load_file(second_stage.c_str(), &ssize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700445 if (sdata == nullptr) die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200446 }
447
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 fprintf(stderr,"creating boot image...\n");
Elliott Hughes577e8b42018-04-05 16:12:47 -0700449 boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, rdata, rsize, sdata, ssize,
450 g_base_addr, g_boot_img_hdr, sz);
Elliott Hughes4089d342017-10-27 14:21:12 -0700451 if (bdata == nullptr) die("failed to create boot.img");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700452
Elliott Hughes577e8b42018-04-05 16:12:47 -0700453 if (!g_cmdline.empty()) bootimg_set_cmdline(bdata, g_cmdline);
454 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", *sz);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800455
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 return bdata;
457}
458
Elliott Hughes23af1122017-11-10 08:42:17 -0800459static void* unzip_to_memory(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700460 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700461 ZipEntry zip_entry;
462 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700463 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Elliott Hughes4089d342017-10-27 14:21:12 -0700464 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465 }
466
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700467 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468
Elliott Hughes23af1122017-11-10 08:42:17 -0800469 fprintf(stderr, "extracting %s (%" PRId64 " MB) to RAM...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700470 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughes4089d342017-10-27 14:21:12 -0700471 if (data == nullptr) die("failed to allocate %" PRId64 " bytes for '%s'", *sz, entry_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700473 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
Elliott Hughes4089d342017-10-27 14:21:12 -0700474 if (error != 0) die("failed to extract '%s': %s", entry_name, ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000475
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476 return data;
477}
478
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700479#if defined(_WIN32)
480
481// TODO: move this to somewhere it can be shared.
482
483#include <windows.h>
484
485// Windows' tmpfile(3) requires administrator rights because
486// it creates temporary files in the root directory.
487static FILE* win32_tmpfile() {
488 char temp_path[PATH_MAX];
489 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
490 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700491 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700492 }
493
494 char filename[PATH_MAX];
495 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700496 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700497 }
498
499 return fopen(filename, "w+bTD");
500}
501
502#define tmpfile win32_tmpfile
503
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000504static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700505 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000506}
507
Elliott Hughes855cdf82018-04-02 14:24:03 -0700508static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700509 // TODO: reimplement to avoid leaking a FILE*.
510 return fileno(tmpfile());
511}
512
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000513#else
514
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700515static std::string make_temporary_template() {
516 const char* tmpdir = getenv("TMPDIR");
517 if (tmpdir == nullptr) tmpdir = P_tmpdir;
518 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
519}
520
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000521static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700522 std::string result(make_temporary_template());
523 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700524 die("unable to create temporary directory: %s", strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000525 }
526 return result;
527}
528
Elliott Hughes855cdf82018-04-02 14:24:03 -0700529static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700530 std::string path_template(make_temporary_template());
531 int fd = mkstemp(&path_template[0]);
532 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700533 die("failed to create temporary file for %s: %s\n", what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700534 }
535 unlink(path_template.c_str());
536 return fd;
537}
538
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700539#endif
540
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000541static std::string create_fbemarker_tmpdir() {
542 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000543 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
544 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
545 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700546 die("unable to create FBE marker file %s locally: %s",
547 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000548 }
549 close(fd);
550 return dir;
551}
552
553static void delete_fbemarker_tmpdir(const std::string& dir) {
554 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
555 if (unlink(marker_file.c_str()) == -1) {
556 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
557 marker_file.c_str(), errno, strerror(errno));
558 return;
559 }
560 if (rmdir(dir.c_str()) == -1) {
561 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
562 dir.c_str(), errno, strerror(errno));
563 return;
564 }
565}
566
Elliott Hughes45964a82017-05-03 22:43:23 -0700567static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700568 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700569
Yusuke Sato07447542015-06-25 14:39:19 -0700570 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700571 ZipEntry zip_entry;
572 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
573 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000574 return -1;
575 }
576
Elliott Hughes23af1122017-11-10 08:42:17 -0800577 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700578 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800579 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700580 int error = ExtractEntryToFile(zip, &zip_entry, fd);
581 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800582 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700583 }
584
Elliott Hughes4089d342017-10-27 14:21:12 -0700585 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800586 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700587 }
588
Elliott Hughes23af1122017-11-10 08:42:17 -0800589 fprintf(stderr, " took %.3fs\n", now() - start);
590
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700591 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700592}
593
Elliott Hughes5620d222018-03-28 08:20:00 -0700594static char* strip(char* s) {
595 while (*s && isspace(*s)) s++;
596
597 int n = strlen(s);
598 while (n-- > 0) {
599 if (!isspace(s[n])) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800600 s[n] = 0;
601 }
602 return s;
603}
604
605#define MAX_OPTIONS 32
Elliott Hughes5620d222018-03-28 08:20:00 -0700606static void check_requirement(Transport* transport, char* line) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800607 char *val[MAX_OPTIONS];
Elliott Hughes5620d222018-03-28 08:20:00 -0700608 unsigned count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800609 char *x;
610 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800611
Elliott Hughes5620d222018-03-28 08:20:00 -0700612 // "require product=alpha|beta|gamma"
613 // "require version-bootloader=1234"
614 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
615 // "require partition-exists=vendor"
616
617 char* name = line;
618 const char* product = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800619 if (!strncmp(name, "reject ", 7)) {
620 name += 7;
621 invert = 1;
622 } else if (!strncmp(name, "require ", 8)) {
623 name += 8;
624 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700625 } else if (!strncmp(name, "require-for-product:", 20)) {
626 // Get the product and point name past it
Elliott Hughes5620d222018-03-28 08:20:00 -0700627 product = name + 20;
Wink Savilleb98762f2011-04-04 17:54:59 -0700628 name = strchr(name, ' ');
Elliott Hughes5620d222018-03-28 08:20:00 -0700629 if (!name) die("android-info.txt syntax error: %s", line);
Wink Savilleb98762f2011-04-04 17:54:59 -0700630 *name = 0;
631 name += 1;
632 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800633 }
634
635 x = strchr(name, '=');
Elliott Hughes5620d222018-03-28 08:20:00 -0700636 if (x == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800637 *x = 0;
638 val[0] = x + 1;
639
Elliott Hughes5620d222018-03-28 08:20:00 -0700640 name = strip(name);
641
642 // "require partition-exists=x" is a special case, added because of the trouble we had when
643 // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
644 // missing out new partitions. A device with new partitions can use "partition-exists" to
645 // override the `is_optional` field in the `images` array.
646 if (!strcmp(name, "partition-exists")) {
647 const char* partition_name = val[0];
648 std::string has_slot;
649 if (!fb_getvar(transport, std::string("has-slot:") + partition_name, &has_slot) ||
650 (has_slot != "yes" && has_slot != "no")) {
651 die("device doesn't have required partition %s!", partition_name);
652 }
653 bool known_partition = false;
654 for (size_t i = 0; i < arraysize(images); ++i) {
655 if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
656 images[i].is_optional = false;
657 known_partition = true;
658 }
659 }
660 if (!known_partition) {
661 die("device requires partition %s which is not known to this version of fastboot",
662 partition_name);
663 }
664 return;
665 }
666
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800667 for(count = 1; count < MAX_OPTIONS; count++) {
668 x = strchr(val[count - 1],'|');
669 if (x == 0) break;
670 *x = 0;
671 val[count] = x + 1;
672 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800673
Elliott Hughes253c18d2015-03-18 22:47:09 -0700674 // Work around an unfortunate name mismatch.
Elliott Hughes5620d222018-03-28 08:20:00 -0700675 const char* var = name;
676 if (!strcmp(name, "board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800677
Elliott Hughes253c18d2015-03-18 22:47:09 -0700678 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
Elliott Hughes5620d222018-03-28 08:20:00 -0700679 if (out == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800680
Elliott Hughes5620d222018-03-28 08:20:00 -0700681 for (size_t i = 0; i < count; ++i) {
682 out[i] = xstrdup(strip(val[i]));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800683 }
684
Elliott Hughes5620d222018-03-28 08:20:00 -0700685 fb_queue_require(product, var, invert, count, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686}
687
Elliott Hughes5620d222018-03-28 08:20:00 -0700688static void check_requirements(Transport* transport, char* data, int64_t sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700689 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700691 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692 *s++ = 0;
Elliott Hughes5620d222018-03-28 08:20:00 -0700693 check_requirement(transport, data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694 data = s;
695 } else {
696 s++;
697 }
698 }
Elliott Hughes5620d222018-03-28 08:20:00 -0700699 if (fb_execute_queue(transport)) die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800700}
701
Elliott Hughesfc797672015-04-07 20:12:50 -0700702static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703 fb_queue_notice("--------------------------------------------");
Elliott Hughes5620d222018-03-28 08:20:00 -0700704 fb_queue_display("Bootloader Version...", "version-bootloader");
705 fb_queue_display("Baseband Version.....", "version-baseband");
706 fb_queue_display("Serial Number........", "serialno");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707 fb_queue_notice("--------------------------------------------");
708}
709
Elliott Hughes4089d342017-10-27 14:21:12 -0700710static struct sparse_file** load_sparse_files(int fd, int max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700711 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700712 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700713
Elliott Hughesfc797672015-04-07 20:12:50 -0700714 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700715 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700716
Elliott Hughes253c18d2015-03-18 22:47:09 -0700717 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700718 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700719
720 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700721 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700722
723 return out_s;
724}
725
David Pursell0b156632015-10-30 11:22:01 -0700726static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700727 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700728 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700729 max_download_size.empty()) {
730 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700731 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700732 }
733
Elliott Hughes77c0e662015-11-02 15:51:12 -0800734 // Some bootloaders (angler, for example) send spurious whitespace too.
735 max_download_size = android::base::Trim(max_download_size);
736
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700737 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700738 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800739 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700740 return 0;
741 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700742 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700743 return limit;
744}
745
David Pursell0b156632015-10-30 11:22:01 -0700746static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700747 int64_t limit;
748
749 if (sparse_limit == 0) {
750 return 0;
751 } else if (sparse_limit > 0) {
752 limit = sparse_limit;
753 } else {
754 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700755 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700756 }
757 if (target_sparse_limit > 0) {
758 limit = target_sparse_limit;
759 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700760 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700761 }
762 }
763
764 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500765 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700766 }
767
768 return 0;
769}
770
Elliott Hughes53ec4952016-05-11 12:39:27 -0700771static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700772 int64_t sz = get_file_size(fd);
773 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700774 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700775 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700776
Elliott Hughesfc797672015-04-07 20:12:50 -0700777 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700778 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700779 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700780 sparse_file** s = load_sparse_files(fd, limit);
781 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700782 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700783 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700784 buf->type = FB_BUFFER_SPARSE;
785 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700786 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500787 buf->type = FB_BUFFER_FD;
788 buf->data = nullptr;
789 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000790 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700791 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700792
Elliott Hughes53ec4952016-05-11 12:39:27 -0700793 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700794}
795
Elliott Hughes53ec4952016-05-11 12:39:27 -0700796static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500797 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
798
Elliott Hughes53ec4952016-05-11 12:39:27 -0700799 if (fd == -1) {
800 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700801 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500802
803 struct stat s;
804 if (fstat(fd, &s)) {
805 return false;
806 }
807 if (!S_ISREG(s.st_mode)) {
808 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
809 return false;
810 }
811
812 return load_buf_fd(transport, fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700813}
814
David Zeuthenb6ea4352017-08-07 14:29:26 -0400815static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
816 // Buffer needs to be at least the size of the VBMeta struct which
817 // is 256 bytes.
818 if (buf->sz < 256) {
819 return;
820 }
821
Elliott Hughes855cdf82018-04-02 14:24:03 -0700822 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400823
824 std::string data;
825 if (!android::base::ReadFdToString(buf->fd, &data)) {
826 die("Failed reading from vbmeta");
827 }
828
829 // There's a 32-bit big endian |flags| field at offset 120 where
830 // bit 0 corresponds to disable-verity and bit 1 corresponds to
831 // disable-verification.
832 //
833 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
834 // the VBMeta struct.
835 if (g_disable_verity) {
836 data[123] |= 0x01;
837 }
838 if (g_disable_verification) {
839 data[123] |= 0x02;
840 }
841
842 if (!android::base::WriteStringToFd(data, fd)) {
843 die("Failed writing to modified vbmeta");
844 }
845 close(buf->fd);
846 buf->fd = fd;
847 lseek(fd, 0, SEEK_SET);
848}
849
Elliott Hughes5620d222018-03-28 08:20:00 -0700850static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700851{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700852 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700853
David Zeuthenb6ea4352017-08-07 14:29:26 -0400854 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
855 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700856 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400857 rewrite_vbmeta_buffer(buf);
858 }
859
Rom Lemarchand622810c2013-06-28 09:54:59 -0700860 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800861 case FB_BUFFER_SPARSE: {
862 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700863 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700864 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700865 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800866 sparse_files.emplace_back(*s, sz);
867 ++s;
868 }
869
870 for (size_t i = 0; i < sparse_files.size(); ++i) {
871 const auto& pair = sparse_files[i];
Elliott Hughes5620d222018-03-28 08:20:00 -0700872 fb_queue_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700873 }
874 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800875 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500876 case FB_BUFFER_FD:
Elliott Hughes5620d222018-03-28 08:20:00 -0700877 fb_queue_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700878 break;
879 default:
880 die("unknown buffer type: %d", buf->type);
881 }
882}
883
Daniel Rosenberg80919472016-06-30 19:25:31 -0700884static std::string get_current_slot(Transport* transport)
885{
886 std::string current_slot;
887 if (fb_getvar(transport, "current-slot", &current_slot)) {
888 if (current_slot == "_a") return "a"; // Legacy support
889 if (current_slot == "_b") return "b"; // Legacy support
890 return current_slot;
891 }
892 return "";
893}
894
895// Legacy support
896static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700897 std::vector<std::string> suffixes;
898 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700899 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700900 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700901 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700902 suffixes = android::base::Split(suffix_list, ",");
903 // Unfortunately some devices will return an error message in the
904 // guise of a valid value. If we only see only one suffix, it's probably
905 // not real.
906 if (suffixes.size() == 1) {
907 suffixes.clear();
908 }
909 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700910}
911
Daniel Rosenberg80919472016-06-30 19:25:31 -0700912// Legacy support
913static bool supports_AB_obsolete(Transport* transport) {
914 return !get_suffixes_obsolete(transport).empty();
915}
916
917static int get_slot_count(Transport* transport) {
918 std::string var;
919 int count;
920 if (!fb_getvar(transport, "slot-count", &var)) {
921 if (supports_AB_obsolete(transport)) return 2; // Legacy support
922 }
Elliott Hughesda46b392016-10-11 17:09:00 -0700923 if (!android::base::ParseInt(var, &count)) return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700924 return count;
925}
926
Alex Lightbb9b8a52016-06-29 09:26:44 -0700927static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700928 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700929}
930
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700931// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700932static std::string get_other_slot(const std::string& current_slot, int count) {
933 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700934
Daniel Rosenberg80919472016-06-30 19:25:31 -0700935 char next = (current_slot[0] - 'a' + 1)%count + 'a';
936 return std::string(1, next);
937}
938
939static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
940 return get_other_slot(current_slot, get_slot_count(transport));
941}
942
943static std::string get_other_slot(Transport* transport, int count) {
944 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700945}
946
Alex Lightbb9b8a52016-06-29 09:26:44 -0700947static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700948 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -0700949}
950
Daniel Rosenberg80919472016-06-30 19:25:31 -0700951static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
952 std::string slot = slot_name;
953 if (slot == "_a") slot = "a"; // Legacy support
954 if (slot == "_b") slot = "b"; // Legacy support
955 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800956 if (allow_all) {
957 return "all";
958 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700959 int count = get_slot_count(transport);
960 if (count > 0) {
961 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800962 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -0700963 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800964 }
965 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700966 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800967
Daniel Rosenberg80919472016-06-30 19:25:31 -0700968 int count = get_slot_count(transport);
Elliott Hughes4089d342017-10-27 14:21:12 -0700969 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800970
Daniel Rosenberg80919472016-06-30 19:25:31 -0700971 if (slot == "other") {
972 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700973 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700974 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800975 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700976 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800977 }
978
Daniel Rosenberg80919472016-06-30 19:25:31 -0700979 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
980
981 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
982 for (int i=0; i<count; i++) {
983 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700984 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700985
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700986 exit(1);
987}
988
Daniel Rosenberg80919472016-06-30 19:25:31 -0700989static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800990 return verify_slot(transport, slot, true);
991}
992
Daniel Rosenberg80919472016-06-30 19:25:31 -0700993static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700994 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800995 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700996 std::string current_slot;
997
Daniel Rosenberg80919472016-06-30 19:25:31 -0700998 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800999 /* If has-slot is not supported, the answer is no. */
1000 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001001 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001002 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001003 if (slot == "") {
1004 current_slot = get_current_slot(transport);
1005 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001006 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001007 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001008 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001009 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001010 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001011 }
1012 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001013 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001014 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001015 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001016 }
1017 func(part);
1018 }
1019}
1020
David Pursell0b156632015-10-30 11:22:01 -07001021/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1022 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1023 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1024 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001025 */
Daniel Rosenberg80919472016-06-30 19:25:31 -07001026static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001027 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001028 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001029
Daniel Rosenberg80919472016-06-30 19:25:31 -07001030 if (slot == "all") {
1031 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001032 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001033 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001034 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001035 for (int i=0; i < get_slot_count(transport); i++) {
1036 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001037 }
1038 } else {
David Pursell0b156632015-10-30 11:22:01 -07001039 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001040 }
1041 } else {
David Pursell0b156632015-10-30 11:22:01 -07001042 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001043 }
1044}
1045
David Pursell0b156632015-10-30 11:22:01 -07001046static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001047 struct fastboot_buffer buf;
1048
Elliott Hughes53ec4952016-05-11 12:39:27 -07001049 if (!load_buf(transport, fname, &buf)) {
1050 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001051 }
1052 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001053}
1054
Elliott Hughes45964a82017-05-03 22:43:23 -07001055static void do_update_signature(ZipArchiveHandle zip, const char* filename) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001056 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001057 void* data = unzip_to_memory(zip, filename, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001058 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001059 fb_queue_download("signature", data, sz);
1060 fb_queue_command("signature", "installing signature");
1061}
1062
Daniel Rosenberg13454092016-06-27 19:43:11 -07001063// Sets slot_override as the active slot. If slot_override is blank,
1064// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -07001065static void set_active(Transport* transport, const std::string& slot_override) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001066 std::string separator = "";
Alex Lightbb9b8a52016-06-29 09:26:44 -07001067 if (!supports_AB(transport)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001068 if (supports_AB_obsolete(transport)) {
1069 separator = "_"; // Legacy support
1070 } else {
1071 return;
1072 }
1073 }
1074 if (slot_override != "") {
Elliott Hughes5620d222018-03-28 08:20:00 -07001075 fb_set_active(separator + slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001076 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001077 std::string current_slot = get_current_slot(transport);
1078 if (current_slot != "") {
Elliott Hughes5620d222018-03-28 08:20:00 -07001079 fb_set_active(separator + current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001080 }
1081 }
1082}
1083
Elliott Hughes59efd442018-04-05 14:24:00 -07001084static void do_update(Transport* transport, const char* filename, const std::string& slot_override, bool skip_secondary) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001085 queue_info_dump();
1086
Wink Savilleb98762f2011-04-04 17:54:59 -07001087 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1088
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001089 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001090 int error = OpenArchive(filename, &zip);
1091 if (error != 0) {
1092 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001093 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001094
Elliott Hughesfc797672015-04-07 20:12:50 -07001095 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001096 void* data = unzip_to_memory(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001097 if (data == nullptr) {
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001098 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001099 }
1100
Elliott Hughes5620d222018-03-28 08:20:00 -07001101 check_requirements(transport, reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001102
Alex Lightbb9b8a52016-06-29 09:26:44 -07001103 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001104 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001105 if (slot_override != "") {
1106 secondary = get_other_slot(transport, slot_override);
1107 } else {
1108 secondary = get_other_slot(transport);
1109 }
1110 if (secondary == "") {
1111 if (supports_AB(transport)) {
1112 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1113 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001114 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001115 }
1116 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001117 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001118 const char* slot = slot_override.c_str();
1119 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001120 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001121 slot = secondary.c_str();
1122 } else {
1123 continue;
1124 }
1125 }
1126
Elliott Hughes253c18d2015-03-18 22:47:09 -07001127 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001128 if (fd == -1) {
1129 if (images[i].is_optional) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001130 continue; // An optional file is missing, so ignore it.
Elliott Hughesacdbe922015-06-02 21:37:05 -07001131 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001132 die("non-optional file %s missing", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001133 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001134
Elliott Hughes253c18d2015-03-18 22:47:09 -07001135 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001136 if (!load_buf_fd(transport, fd, &buf)) {
1137 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1138 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001139
Elliott Hughes4089d342017-10-27 14:21:12 -07001140 auto update = [&](const std::string& partition) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001141 do_update_signature(zip, images[i].sig_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001142 flash_buf(partition.c_str(), &buf);
1143 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001144 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001145 * program exits.
1146 */
1147 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001148 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001149 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001150
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001151 if (slot_override == "all") {
1152 set_active(transport, "a");
1153 } else {
1154 set_active(transport, slot_override);
1155 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001156
1157 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001158}
1159
Alex Lightbb9b8a52016-06-29 09:26:44 -07001160static void do_send_signature(const std::string& fn) {
1161 std::size_t extension_loc = fn.find(".img");
1162 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001163
Alex Lightbb9b8a52016-06-29 09:26:44 -07001164 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001165
1166 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001167 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001168 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001169
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001170 fb_queue_download("signature", data, sz);
1171 fb_queue_command("signature", "installing signature");
1172}
1173
Elliott Hughes59efd442018-04-05 14:24:00 -07001174static void do_flashall(Transport* transport, const std::string& slot_override, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001175 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001176 queue_info_dump();
1177
Wink Savilleb98762f2011-04-04 17:54:59 -07001178 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1179
Elliott Hughes45964a82017-05-03 22:43:23 -07001180 fname = find_item_given_name("android-info.txt");
Elliott Hughes2810d002016-04-25 14:31:18 -07001181 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001182
Elliott Hughesfc797672015-04-07 20:12:50 -07001183 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001184 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001185 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001186
Elliott Hughes5620d222018-03-28 08:20:00 -07001187 check_requirements(transport, reinterpret_cast<char*>(data), sz);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001188
Alex Lightbb9b8a52016-06-29 09:26:44 -07001189 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001190 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001191 if (slot_override != "") {
1192 secondary = get_other_slot(transport, slot_override);
1193 } else {
1194 secondary = get_other_slot(transport);
1195 }
1196 if (secondary == "") {
1197 if (supports_AB(transport)) {
1198 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1199 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001200 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001201 }
1202 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001203
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001204 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001205 const char* slot = NULL;
1206 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001207 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001208 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001209 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001210 }
1211 if (!slot) continue;
Elliott Hughes45964a82017-05-03 22:43:23 -07001212 fname = find_item_given_name(images[i].img_name);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001213 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001214 if (!load_buf(transport, fname.c_str(), &buf)) {
1215 if (images[i].is_optional) continue;
Elliott Hughes4089d342017-10-27 14:21:12 -07001216 die("could not load '%s': %s", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001217 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001218
1219 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001220 do_send_signature(fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001221 flash_buf(partition.c_str(), &buf);
1222 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001223 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001224 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001225
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001226 if (slot_override == "all") {
1227 set_active(transport, "a");
1228 } else {
1229 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001230 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001231}
1232
Elliott Hughesd6365a72017-05-08 18:04:49 -07001233static std::string next_arg(std::vector<std::string>* args) {
1234 if (args->empty()) syntax_error("expected argument");
1235 std::string result = args->front();
1236 args->erase(args->begin());
1237 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001238}
1239
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001240static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001241 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001242
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001243 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001244 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001245 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001246 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001247 fb_queue_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001248}
1249
Colin Crossf8387882012-05-24 17:18:41 -07001250static int64_t parse_num(const char *arg)
1251{
1252 char *endptr;
1253 unsigned long long num;
1254
1255 num = strtoull(arg, &endptr, 0);
1256 if (endptr == arg) {
1257 return -1;
1258 }
1259
1260 if (*endptr == 'k' || *endptr == 'K') {
1261 if (num >= (-1ULL) / 1024) {
1262 return -1;
1263 }
1264 num *= 1024LL;
1265 endptr++;
1266 } else if (*endptr == 'm' || *endptr == 'M') {
1267 if (num >= (-1ULL) / (1024 * 1024)) {
1268 return -1;
1269 }
1270 num *= 1024LL * 1024LL;
1271 endptr++;
1272 } else if (*endptr == 'g' || *endptr == 'G') {
1273 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1274 return -1;
1275 }
1276 num *= 1024LL * 1024LL * 1024LL;
1277 endptr++;
1278 }
1279
1280 if (*endptr != '\0') {
1281 return -1;
1282 }
1283
1284 if (num > INT64_MAX) {
1285 return -1;
1286 }
1287
1288 return num;
1289}
1290
Connor O'Brience16a8a2017-02-06 14:39:31 -08001291static std::string fb_fix_numeric_var(std::string var) {
1292 // Some bootloaders (angler, for example), send spurious leading whitespace.
1293 var = android::base::Trim(var);
1294 // Some bootloaders (hammerhead, for example) use implicit hex.
1295 // This code used to use strtol with base 16.
1296 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1297 return var;
1298}
1299
1300static unsigned fb_get_flash_block_size(Transport* transport, std::string name) {
1301 std::string sizeString;
Elliott Hughes5620d222018-03-28 08:20:00 -07001302 if (!fb_getvar(transport, name, &sizeString) || sizeString.empty()) {
1303 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001304 return 0;
1305 }
1306 sizeString = fb_fix_numeric_var(sizeString);
1307
1308 unsigned size;
1309 if (!android::base::ParseUint(sizeString, &size)) {
1310 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1311 return 0;
1312 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001313 if ((size & (size - 1)) != 0) {
1314 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001315 return 0;
1316 }
1317 return size;
1318}
1319
David Pursell0b156632015-10-30 11:22:01 -07001320static void fb_perform_format(Transport* transport,
Elliott Hughes5620d222018-03-28 08:20:00 -07001321 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001322 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001323 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001324 std::string partition_type, partition_size;
1325
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001326 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001327 const char* errMsg = nullptr;
1328 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001329 TemporaryFile output;
1330 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001331
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001332 unsigned int limit = INT_MAX;
1333 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001334 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001335 }
1336 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001337 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001338 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001339
Elliott Hughes5620d222018-03-28 08:20:00 -07001340 if (!fb_getvar(transport, "partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001341 errMsg = "Can't determine partition type.\n";
1342 goto failed;
1343 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001344 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001345 if (partition_type != type_override) {
1346 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001347 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001348 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001349 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001350 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001351
Elliott Hughes5620d222018-03-28 08:20:00 -07001352 if (!fb_getvar(transport, "partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001353 errMsg = "Unable to get partition size\n";
1354 goto failed;
1355 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001356 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001357 if (partition_size != size_override) {
1358 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001359 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001360 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001361 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001362 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001363 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001364
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001365 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001366 if (!gen) {
1367 if (skip_if_not_supported) {
1368 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001369 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001370 return;
1371 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001372 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1373 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001374 return;
1375 }
1376
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001377 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001378 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001379 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1380 return;
1381 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001382
Connor O'Brience16a8a2017-02-06 14:39:31 -08001383 unsigned eraseBlkSize, logicalBlkSize;
1384 eraseBlkSize = fb_get_flash_block_size(transport, "erase-block-size");
1385 logicalBlkSize = fb_get_flash_block_size(transport, "logical-block-size");
1386
Jin Qian4a335822017-04-18 16:23:18 -07001387 if (fs_generator_generate(gen, output.path, size, initial_dir,
1388 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001389 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001390 return;
1391 }
1392
Jin Qian4a335822017-04-18 16:23:18 -07001393 fd.reset(open(output.path, O_RDONLY));
1394 if (fd == -1) {
1395 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1396 return;
1397 }
1398 if (!load_buf_fd(transport, fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001399 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001400 return;
1401 }
1402 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001403 return;
1404
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001405failed:
1406 if (skip_if_not_supported) {
1407 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001408 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001409 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001410 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001411}
1412
Elliott Hughes11ff3452018-04-09 13:58:41 -07001413int FastBoot::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001414 bool wants_wipe = false;
1415 bool wants_reboot = false;
1416 bool wants_reboot_bootloader = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001417 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001418 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001419 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001420 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001421 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001422 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001423 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001424 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001425 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001426
Elliott Hughes577e8b42018-04-05 16:12:47 -07001427 g_boot_img_hdr.kernel_addr = 0x00008000;
1428 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1429 g_boot_img_hdr.second_addr = 0x00f00000;
1430 g_boot_img_hdr.tags_addr = 0x00000100;
1431 g_boot_img_hdr.page_size = 2048;
1432
JP Abgrall7b8970c2013-03-07 17:06:41 -08001433 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001434 {"base", required_argument, 0, 0},
1435 {"cmdline", required_argument, 0, 0},
1436 {"disable-verification", no_argument, 0, 0},
1437 {"disable-verity", no_argument, 0, 0},
1438 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001439 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001440 {"kernel-offset", required_argument, 0, 0},
1441 {"os-patch-level", required_argument, 0, 0},
1442 {"os-version", required_argument, 0, 0},
1443 {"page-size", required_argument, 0, 0},
1444 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001445 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001446 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001447 {"skip-secondary", no_argument, 0, 0},
1448 {"slot", required_argument, 0, 0},
1449 {"tags-offset", required_argument, 0, 0},
1450 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001451 {"verbose", no_argument, 0, 'v'},
1452 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001453#if !defined(_WIN32)
1454 {"wipe-and-use-fbe", no_argument, 0, 0},
1455#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001456 {0, 0, 0, 0}
1457 };
Colin Cross8879f982012-05-22 17:53:34 -07001458
1459 serial = getenv("ANDROID_SERIAL");
1460
Elliott Hughes577e8b42018-04-05 16:12:47 -07001461 int c;
1462 while ((c = getopt_long(argc, argv, "a::hi:ls:S:vw", longopts, &longindex)) != -1) {
1463 if (c == 0) {
1464 std::string name{longopts[longindex].name};
1465 if (name == "base") {
1466 g_base_addr = strtoul(optarg, 0, 16);
1467 } else if (name == "cmdline") {
1468 g_cmdline = optarg;
1469 } else if (name == "disable-verification") {
1470 g_disable_verification = true;
1471 } else if (name == "disable-verity") {
1472 g_disable_verity = true;
1473 } else if (name == "header-version") {
1474 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1475 } else if (name == "kernel-offset") {
1476 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1477 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001478 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001479 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001480 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001481 } else if (name == "page-size") {
1482 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1483 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1484 } else if (name == "ramdisk-offset") {
1485 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1486 } else if (name == "skip-reboot") {
1487 skip_reboot = true;
1488 } else if (name == "skip-secondary") {
1489 skip_secondary = true;
1490 } else if (name == "slot") {
1491 slot_override = optarg;
1492 } else if (name == "tags-offset") {
1493 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1494 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001495 setvbuf(stdout, nullptr, _IONBF, 0);
1496 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001497 } else if (name == "version") {
Elliott Hughes4d4f64f2017-05-08 11:30:34 -07001498 fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001499 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001500 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001501#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001502 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001503 wants_wipe = true;
1504 set_fbe_marker = true;
1505#endif
1506 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001507 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001508 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001509 } else {
1510 switch (c) {
1511 case 'a':
1512 wants_set_active = true;
1513 if (optarg) next_active = optarg;
1514 break;
1515 case 'h':
1516 return show_help();
1517 case 'i':
1518 {
1519 char *endptr = nullptr;
1520 unsigned long val = strtoul(optarg, &endptr, 0);
1521 if (!endptr || *endptr != '\0' || (val & ~0xffff)) {
1522 die("invalid vendor id '%s'", optarg);
1523 }
1524 vendor_id = (unsigned short)val;
1525 break;
1526 }
1527 case 'l':
1528 g_long_listing = true;
1529 break;
1530 case 's':
1531 serial = optarg;
1532 break;
1533 case 'S':
1534 sparse_limit = parse_num(optarg);
1535 if (sparse_limit < 0) die("invalid sparse limit");
1536 break;
1537 case 'v':
1538 set_verbose();
1539 break;
1540 case 'w':
1541 wants_wipe = true;
1542 break;
1543 case '?':
1544 return 1;
1545 default:
1546 abort();
1547 }
Colin Cross8879f982012-05-22 17:53:34 -07001548 }
1549 }
1550
1551 argc -= optind;
1552 argv += optind;
1553
Elliott Hughesd6365a72017-05-08 18:04:49 -07001554 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001555
Colin Cross8fb6e062012-07-24 16:36:41 -07001556 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001557 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001558 return 0;
1559 }
1560
Colin Crossc7b75dc2012-08-29 18:17:06 -07001561 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001562 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001563 }
1564
David Pursell0b156632015-10-30 11:22:01 -07001565 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001566 if (transport == nullptr) {
1567 return 1;
1568 }
1569
Elliott Hughes5620d222018-03-28 08:20:00 -07001570 const double start = now();
1571
Daniel Rosenberg80919472016-06-30 19:25:31 -07001572 if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
1573 fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
1574 }
1575 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1576 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001577
1578 if (wants_set_active) {
1579 if (next_active == "") {
1580 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001581 std::string current_slot;
1582 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001583 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001584 } else {
1585 wants_set_active = false;
1586 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001587 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001588 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001589 }
1590 }
1591 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001592
Elliott Hughesd6365a72017-05-08 18:04:49 -07001593 std::vector<std::string> args(argv, argv + argc);
1594 while (!args.empty()) {
1595 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001596
Elliott Hughesd6365a72017-05-08 18:04:49 -07001597 if (command == "getvar") {
1598 std::string variable = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001599 fb_queue_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001600 } else if (command == "erase") {
1601 std::string partition = next_arg(&args);
1602 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001603 std::string partition_type;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001604 if (fb_getvar(transport, std::string("partition-type:") + partition,
1605 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001606 fs_get_generator(partition_type) != nullptr) {
1607 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1608 partition_type.c_str());
1609 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001610
Elliott Hughes5620d222018-03-28 08:20:00 -07001611 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001612 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001613 do_for_partitions(transport, partition, slot_override, erase, true);
1614 } else if (android::base::StartsWith(command, "format")) {
1615 // Parsing for: "format[:[type][:[size]]]"
1616 // Some valid things:
1617 // - select only the size, and leave default fs type:
1618 // format::0x4000000 userdata
1619 // - default fs type and size:
1620 // format userdata
1621 // format:: userdata
1622 std::vector<std::string> pieces = android::base::Split(command, ":");
1623 std::string type_override;
1624 if (pieces.size() > 1) type_override = pieces[1].c_str();
1625 std::string size_override;
1626 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001627
Elliott Hughesd6365a72017-05-08 18:04:49 -07001628 std::string partition = next_arg(&args);
1629
1630 auto format = [&](const std::string& partition) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001631 fb_perform_format(transport, partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001632 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001633 do_for_partitions(transport, partition.c_str(), slot_override, format, true);
1634 } else if (command == "signature") {
1635 std::string filename = next_arg(&args);
1636 data = load_file(filename.c_str(), &sz);
1637 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -07001638 if (sz != 256) die("signature must be 256 bytes (got %" PRId64 ")", sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001639 fb_queue_download("signature", data, sz);
1640 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001641 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001642 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001643
1644 if (args.size() == 1) {
1645 std::string what = next_arg(&args);
1646 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001647 wants_reboot = false;
1648 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001649 } else {
1650 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001651 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001652
Elliott Hughesca85df02015-02-25 10:02:00 -08001653 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001654 if (!args.empty()) syntax_error("junk after reboot command");
1655 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001656 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001657 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001658 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001659 } else if (command == "boot") {
1660 std::string kernel = next_arg(&args);
1661 std::string ramdisk;
1662 if (!args.empty()) ramdisk = next_arg(&args);
1663 std::string second_stage;
1664 if (!args.empty()) second_stage = next_arg(&args);
1665
Elliott Hughes577e8b42018-04-05 16:12:47 -07001666 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001667 fb_queue_download("boot.img", data, sz);
1668 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001669 } else if (command == "flash") {
1670 std::string pname = next_arg(&args);
1671
Elliott Hughes2810d002016-04-25 14:31:18 -07001672 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001673 if (!args.empty()) {
1674 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001675 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001676 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001677 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001678 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001679
1680 auto flash = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001681 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001682 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001683 do_for_partitions(transport, pname.c_str(), slot_override, flash, true);
1684 } else if (command == "flash:raw") {
1685 std::string partition = next_arg(&args);
1686 std::string kernel = next_arg(&args);
1687 std::string ramdisk;
1688 if (!args.empty()) ramdisk = next_arg(&args);
1689 std::string second_stage;
1690 if (!args.empty()) second_stage = next_arg(&args);
1691
Elliott Hughes577e8b42018-04-05 16:12:47 -07001692 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001693 auto flashraw = [&](const std::string& partition) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001694 fb_queue_flash(partition, data, sz);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001695 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001696 do_for_partitions(transport, partition, slot_override, flashraw, true);
1697 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001698 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001699 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
Elliott Hughes59efd442018-04-05 14:24:00 -07001700 do_flashall(transport, slot_override, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001701 } else {
Elliott Hughes59efd442018-04-05 14:24:00 -07001702 do_flashall(transport, slot_override, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001703 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001704 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001705 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001706 bool slot_all = (slot_override == "all");
1707 if (slot_all) {
1708 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1709 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001710 std::string filename = "update.zip";
1711 if (!args.empty()) {
1712 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001713 }
Elliott Hughes59efd442018-04-05 14:24:00 -07001714 do_update(transport, filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001715 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001716 } else if (command == "set_active") {
1717 std::string slot = verify_slot(transport, next_arg(&args), false);
1718
David Pursell04396f62016-12-15 16:16:10 -08001719 // Legacy support: verify_slot() removes leading underscores, we need to put them back
1720 // in for old bootloaders. Legacy bootloaders do not have the slot-count variable but
1721 // do have slot-suffixes.
1722 std::string var;
1723 if (!fb_getvar(transport, "slot-count", &var) &&
1724 fb_getvar(transport, "slot-suffixes", &var)) {
1725 slot = "_" + slot;
1726 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001727 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001728 } else if (command == "stage") {
1729 std::string filename = next_arg(&args);
1730
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001731 struct fastboot_buffer buf;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001732 if (!load_buf(transport, filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
1733 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001734 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001735 fb_queue_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001736 } else if (command == "get_staged") {
1737 std::string filename = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001738 fb_queue_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001739 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001740 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001741 } else if (command == "flashing") {
1742 if (args.empty()) {
1743 syntax_error("missing 'flashing' command");
1744 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1745 args[0] == "unlock_critical" ||
1746 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001747 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001748 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001749 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001750 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001751 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001752 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001753 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001754 }
1755 }
1756
1757 if (wants_wipe) {
1758 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001759 if (set_fbe_marker) {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001760 fprintf(stderr, "setting FBE marker on initial userdata...\n");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001761 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Elliott Hughesd6365a72017-05-08 18:04:49 -07001762 fb_perform_format(transport, "userdata", 1, "", "", initial_userdata_dir);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001763 delete_fbemarker_tmpdir(initial_userdata_dir);
1764 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001765 fb_perform_format(transport, "userdata", 1, "", "", "");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001766 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001767
1768 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001769 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001770 fb_queue_erase("cache");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001771 fb_perform_format(transport, "cache", 1, "", "", "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001772 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001773 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001774 if (wants_set_active) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001775 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001776 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001777 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001778 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001779 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001780 } else if (wants_reboot_bootloader) {
1781 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001782 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001783 }
1784
Elliott Hughes5620d222018-03-28 08:20:00 -07001785 int status = fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
1786 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
1787 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001788}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001789
1790void FastBoot::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
1791 unsigned year, month, day;
1792 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1793 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1794 }
1795 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1796 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1797 hdr->SetOsPatchLevel(year, month);
1798}
1799
1800void FastBoot::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
1801 unsigned major = 0, minor = 0, patch = 0;
1802 std::vector<std::string> versions = android::base::Split(arg, ".");
1803 if (versions.size() < 1 || versions.size() > 3 ||
1804 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1805 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1806 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1807 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1808 syntax_error("bad OS version: %s", arg);
1809 }
1810 hdr->SetOsVersion(major, minor, patch);
1811}