blob: 332b35597c723d234f761192b1af133693767702 [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
Colin Crossf8387882012-05-24 17:18:41 -070029#define _LARGEFILE64_SOURCE
30
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070031#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include <errno.h>
33#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070034#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070035#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070036#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070037#include <stdint.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070043#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070044#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080045
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070047#include <functional>
Elliott Hughes290a2282016-11-14 17:08:47 -080048#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080049#include <utility>
50#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070051
Elliott Hughes82ff3152016-08-31 15:07:18 -070052#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070053#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080054#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080055#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070056#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080057#include <android-base/strings.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050058#include <android-base/unique_fd.h>
Colin Crossf8387882012-05-24 17:18:41 -070059#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070060#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
Elliott Hughes253c18d2015-03-18 22:47:09 -070062#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080063#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070065#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080066#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070067#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080068#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070069#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
Chris Fries0ea946c2017-04-12 10:25:57 -050071using android::base::unique_fd;
72
Colin Crossf8387882012-05-24 17:18:41 -070073#ifndef O_BINARY
74#define O_BINARY 0
75#endif
76
Wink Savilleb98762f2011-04-04 17:54:59 -070077char cur_product[FB_RESPONSE_SZ + 1];
78
David Pursell2ec418a2016-01-20 08:32:08 -080079static const char* serial = nullptr;
80static const char* product = nullptr;
81static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070083static int long_listing = 0;
Chris Fries6a999712017-04-04 09:52:47 -050084// Don't resparse files in too-big chunks.
85// libsparse will support INT_MAX, but this results in large allocations, so
86// let's keep it at 1GB to avoid memory pressure on the host.
87static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Colin Crossf8387882012-05-24 17:18:41 -070088static int64_t sparse_limit = -1;
89static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090
Elliott Hughesfc797672015-04-07 20:12:50 -070091static unsigned page_size = 2048;
92static unsigned base_addr = 0x10000000;
93static unsigned kernel_offset = 0x00008000;
94static unsigned ramdisk_offset = 0x01000000;
95static unsigned second_offset = 0x00f00000;
96static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080097
Paul Crowley8f7f56e2015-11-27 09:29:37 +000098static const std::string convert_fbe_marker_filename("convert_fbe");
99
Rom Lemarchand622810c2013-06-28 09:54:59 -0700100enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -0500101 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -0700102 FB_BUFFER_SPARSE,
103};
104
105struct fastboot_buffer {
106 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700107 void* data;
108 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500109 int fd;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700110};
111
112static struct {
Alex Lightbb9b8a52016-06-29 09:26:44 -0700113 char img_name[17];
114 char sig_name[17];
Rom Lemarchand622810c2013-06-28 09:54:59 -0700115 char part_name[9];
116 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700117 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700118} images[] = {
Alex Lightbb9b8a52016-06-29 09:26:44 -0700119 {"boot.img", "boot.sig", "boot", false, false},
120 {"boot_other.img", "boot.sig", "boot", true, true},
121 {"recovery.img", "recovery.sig", "recovery", true, false},
122 {"system.img", "system.sig", "system", false, false},
123 {"system_other.img", "system.sig", "system", true, true},
124 {"vendor.img", "vendor.sig", "vendor", true, false},
125 {"vendor_other.img", "vendor.sig", "vendor", true, true},
Vineeta Srivastava20ef9862017-03-31 13:22:34 -0700126 {"vbmeta.img", "vbmeta.sig", "vbmeta", true, false},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700127};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700128
Alex Lightbb9b8a52016-06-29 09:26:44 -0700129static std::string find_item_given_name(const char* img_name, const char* product) {
130 if(product) {
Elliott Hughes82ff3152016-08-31 15:07:18 -0700131 std::string path = android::base::GetExecutablePath();
Alex Lightbb9b8a52016-06-29 09:26:44 -0700132 path.erase(path.find_last_of('/'));
133 return android::base::StringPrintf("%s/../../../target/product/%s/%s",
134 path.c_str(), product, img_name);
135 }
136
137 char *dir = getenv("ANDROID_PRODUCT_OUT");
138 if (dir == nullptr || dir[0] == '\0') {
139 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
140 }
141
142 return android::base::StringPrintf("%s/%s", dir, img_name);
143}
144
145std::string find_item(const char* item, const char* product) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700146 const char *fn;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700147
Elliott Hughes2810d002016-04-25 14:31:18 -0700148 if (!strcmp(item,"boot")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 fn = "boot.img";
150 } else if(!strcmp(item,"recovery")) {
151 fn = "recovery.img";
152 } else if(!strcmp(item,"system")) {
153 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700154 } else if(!strcmp(item,"vendor")) {
155 fn = "vendor.img";
Vineeta Srivastava20ef9862017-03-31 13:22:34 -0700156 } else if(!strcmp(item,"vbmeta")) {
157 fn = "vbmeta.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 } else if(!strcmp(item,"userdata")) {
159 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700160 } else if(!strcmp(item,"cache")) {
161 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162 } else if(!strcmp(item,"info")) {
163 fn = "android-info.txt";
164 } else {
165 fprintf(stderr,"unknown partition '%s'\n", item);
Elliott Hughes2810d002016-04-25 14:31:18 -0700166 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 }
168
Alex Lightbb9b8a52016-06-29 09:26:44 -0700169 return find_item_given_name(fn, product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170}
171
Elliott Hughesfc797672015-04-07 20:12:50 -0700172static int64_t get_file_size(int fd) {
173 struct stat sb;
174 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700175}
176
Elliott Hughesfc797672015-04-07 20:12:50 -0700177static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800178 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700179 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180
Elliott Hughesfc797672015-04-07 20:12:50 -0700181 *sz = get_file_size(fd);
182 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700183 goto oops;
184 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185
Elliott Hughesfc797672015-04-07 20:12:50 -0700186 data = (char*) malloc(*sz);
187 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188
Elliott Hughesfc797672015-04-07 20:12:50 -0700189 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 close(fd);
191
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 return data;
193
194oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800195 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 close(fd);
197 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800198 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 return 0;
200}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201
Elliott Hughes2810d002016-04-25 14:31:18 -0700202static void* load_file(const std::string& path, int64_t* sz) {
203 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700204 if (fd == -1) return nullptr;
205 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700206}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207
Elliott Hughesfc797672015-04-07 20:12:50 -0700208static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700209 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700210 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700211 return -1;
212 }
213
214 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
215 return -1;
216 }
217
Scott Anderson13081c62012-04-06 12:39:30 -0700218 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700220 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
221 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 return 0;
223}
224
Elliott Hughesfc797672015-04-07 20:12:50 -0700225static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800226 return match_fastboot_with_serial(info, serial);
227}
228
Elliott Hughesfc797672015-04-07 20:12:50 -0700229static int list_devices_callback(usb_ifc_info* info) {
230 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800231 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700232 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800233 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700234 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 if (!serial[0]) {
236 serial = "????????????";
237 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700238 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700239 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800240 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700241 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800242 printf("%-22s fastboot", serial.c_str());
243 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700244 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800245 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 }
247
248 return -1;
249}
250
David Pursell2ec418a2016-01-20 08:32:08 -0800251// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
252// a specific device, otherwise the first USB device found will be used.
253//
254// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
255// Otherwise it blocks until the target is available.
256//
257// The returned Transport is a singleton, so multiple calls to this function will return the same
258// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700259static Transport* open_device() {
260 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800261 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262
David Pursell2ec418a2016-01-20 08:32:08 -0800263 if (transport != nullptr) {
264 return transport;
265 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800266
David Pursell4601c972016-02-05 15:35:09 -0800267 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800268 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800269 int port = 0;
270 if (serial != nullptr) {
271 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800272
David Pursell4601c972016-02-05 15:35:09 -0800273 if (android::base::StartsWith(serial, "tcp:")) {
274 protocol = Socket::Protocol::kTcp;
275 port = tcp::kDefaultPort;
276 net_address = serial + strlen("tcp:");
277 } else if (android::base::StartsWith(serial, "udp:")) {
278 protocol = Socket::Protocol::kUdp;
279 port = udp::kDefaultPort;
280 net_address = serial + strlen("udp:");
281 }
282
283 if (net_address != nullptr) {
284 std::string error;
285 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
286 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
287 error.c_str());
288 return nullptr;
289 }
David Pursell2ec418a2016-01-20 08:32:08 -0800290 }
291 }
292
293 while (true) {
294 if (!host.empty()) {
295 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800296 if (protocol == Socket::Protocol::kTcp) {
297 transport = tcp::Connect(host, port, &error).release();
298 } else if (protocol == Socket::Protocol::kUdp) {
299 transport = udp::Connect(host, port, &error).release();
300 }
301
David Pursell2ec418a2016-01-20 08:32:08 -0800302 if (transport == nullptr && announce) {
303 fprintf(stderr, "error: %s\n", error.c_str());
304 }
305 } else {
306 transport = usb_open(match_fastboot);
307 }
308
309 if (transport != nullptr) {
310 return transport;
311 }
312
David Pursell0b156632015-10-30 11:22:01 -0700313 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800314 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700315 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800317 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318 }
319}
320
Elliott Hughesfc797672015-04-07 20:12:50 -0700321static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 // We don't actually open a USB device here,
323 // just getting our callback called so we can
324 // list all the connected devices.
325 usb_open(list_devices_callback);
326}
327
Elliott Hughesfc797672015-04-07 20:12:50 -0700328static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 fprintf(stderr,
330/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
331 "usage: fastboot [ <option> ] <command>\n"
332 "\n"
333 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700334 " update <filename> Reflash device from update.zip.\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700335 " Sets the flashed slot as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700336 " flashall Flash boot, system, vendor, and --\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700337 " if found -- recovery. If the device\n"
338 " supports slots, the slot that has\n"
339 " been flashed to is set as active.\n"
Alex Lightbb9b8a52016-06-29 09:26:44 -0700340 " Secondary images may be flashed to\n"
341 " an inactive slot.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700342 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
343 " flashing lock Locks the device. Prevents flashing.\n"
344 " flashing unlock Unlocks the device. Allows flashing\n"
345 " any partition except\n"
346 " bootloader-related partitions.\n"
347 " flashing lock_critical Prevents flashing bootloader-related\n"
348 " partitions.\n"
349 " flashing unlock_critical Enables flashing bootloader-related\n"
350 " partitions.\n"
351 " flashing get_unlock_ability Queries bootloader to see if the\n"
352 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700353 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700354 " unlock nonce.\n"
355 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700356 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700357 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700358 " erase <partition> Erase a flash partition.\n"
359 " format[:[<fs type>][:[<size>]] <partition>\n"
360 " Format a flash partition. Can\n"
361 " override the fs type and/or size\n"
362 " the bootloader reports.\n"
363 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700364 " set_active <slot> Sets the active slot. If slots are\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800365 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700366 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
367 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
368 " Create bootimage and flash it.\n"
369 " devices [-l] List all connected devices [with\n"
370 " device paths].\n"
371 " continue Continue with autoboot.\n"
Alexey Polyudove0bfb752017-01-03 19:39:13 -0800372 " reboot [bootloader|emergency] Reboot device [into bootloader or emergency mode].\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700373 " reboot-bootloader Reboot device into bootloader.\n"
374 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 "\n"
376 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700377 " -w Erase userdata and cache (and format\n"
378 " if supported by partition type).\n"
379 " -u Do not erase partition before\n"
380 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800381 " -s <specific device> Specify a device. For USB, provide either\n"
382 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700383 " For ethernet, provide an address in the\n"
384 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800385 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700386 " -p <product> Specify product name.\n"
387 " -c <cmdline> Override kernel commandline.\n"
388 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800389 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700390 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800391 " --kernel-offset Specify a custom kernel offset.\n"
392 " (default: 0x00008000)\n"
393 " --ramdisk-offset Specify a custom ramdisk offset.\n"
394 " (default: 0x01000000)\n"
395 " --tags-offset Specify a custom tags offset.\n"
396 " (default: 0x00000100)\n"
397 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700398 " (default: 2048).\n"
399 " -S <size>[K|M|G] Automatically sparse files greater\n"
400 " than 'size'. 0 to disable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700401 " --slot <slot> Specify slot name to be used if the\n"
402 " device supports slots. All operations\n"
403 " on partitions that support slots will\n"
404 " be done on the slot specified.\n"
405 " 'all' can be given to refer to all slots.\n"
406 " 'other' can be given to refer to a\n"
407 " non-current slot. If this flag is not\n"
408 " used, slotted partitions will default\n"
409 " to the current active slot.\n"
410 " -a, --set-active[=<slot>] Sets the active slot. If no slot is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800411 " provided, this will default to the value\n"
412 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800413 " supported, this does nothing. This will\n"
414 " run after all non-reboot commands.\n"
Daniel Rosenberg92b44762016-07-13 20:03:25 -0700415 " --skip-secondary Will not flash secondary slots when\n"
416 " performing a flashall or update. This\n"
417 " will preserve data on other slots.\n"
Mitchell Wills31dce302016-09-26 10:26:21 -0700418 " --skip-reboot Will not reboot the device when\n"
419 " performing commands that normally\n"
420 " trigger a reboot.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000421#if !defined(_WIN32)
422 " --wipe-and-use-fbe On devices which support it,\n"
423 " erase userdata and cache, and\n"
424 " enable file-based encryption\n"
425#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800426 " --unbuffered Do not buffer input or output.\n"
427 " --version Display version.\n"
428 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000431
Elliott Hughesfc797672015-04-07 20:12:50 -0700432static void* load_bootable_image(const char* kernel, const char* ramdisk,
433 const char* secondstage, int64_t* sz,
434 const char* cmdline) {
435 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436 fprintf(stderr, "no image specified\n");
437 return 0;
438 }
439
Elliott Hughesfc797672015-04-07 20:12:50 -0700440 int64_t ksize;
441 void* kdata = load_file(kernel, &ksize);
442 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800443 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444 return 0;
445 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800446
Elliott Hughesfc797672015-04-07 20:12:50 -0700447 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700449 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800450
Elliott Hughesfc797672015-04-07 20:12:50 -0700451 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
453 return 0;
454 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800455
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 *sz = ksize;
457 return kdata;
458 }
459
Elliott Hughesfc797672015-04-07 20:12:50 -0700460 void* rdata = nullptr;
461 int64_t rsize = 0;
462 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700464 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800465 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466 return 0;
467 }
468 }
469
Elliott Hughesfc797672015-04-07 20:12:50 -0700470 void* sdata = nullptr;
471 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200472 if (secondstage) {
473 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700474 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200475 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
476 return 0;
477 }
478 }
479
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700481 int64_t bsize = 0;
482 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800483 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200484 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800485 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700486 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 fprintf(stderr,"failed to create boot.img\n");
488 return 0;
489 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700490 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
491 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800493
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494 return bdata;
495}
496
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700497static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700498 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700499 ZipEntry zip_entry;
500 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700501 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000502 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 }
504
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700505 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800506
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700507 fprintf(stderr, "extracting %s (%" PRId64 " MB)...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700508 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700509 if (data == nullptr) {
510 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000511 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512 }
513
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700514 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
515 if (error != 0) {
516 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000518 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000520
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521 return data;
522}
523
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700524#if defined(_WIN32)
525
526// TODO: move this to somewhere it can be shared.
527
528#include <windows.h>
529
530// Windows' tmpfile(3) requires administrator rights because
531// it creates temporary files in the root directory.
532static FILE* win32_tmpfile() {
533 char temp_path[PATH_MAX];
534 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
535 if (nchars == 0 || nchars >= sizeof(temp_path)) {
536 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
537 return nullptr;
538 }
539
540 char filename[PATH_MAX];
541 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
542 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
543 return nullptr;
544 }
545
546 return fopen(filename, "w+bTD");
547}
548
549#define tmpfile win32_tmpfile
550
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000551static std::string make_temporary_directory() {
552 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
553 return "";
554}
555
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700556static int make_temporary_fd() {
557 // TODO: reimplement to avoid leaking a FILE*.
558 return fileno(tmpfile());
559}
560
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000561#else
562
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700563static std::string make_temporary_template() {
564 const char* tmpdir = getenv("TMPDIR");
565 if (tmpdir == nullptr) tmpdir = P_tmpdir;
566 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
567}
568
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000569static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700570 std::string result(make_temporary_template());
571 if (mkdtemp(&result[0]) == nullptr) {
572 fprintf(stderr, "Unable to create temporary directory: %s\n", strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000573 return "";
574 }
575 return result;
576}
577
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700578static int make_temporary_fd() {
579 std::string path_template(make_temporary_template());
580 int fd = mkstemp(&path_template[0]);
581 if (fd == -1) {
582 fprintf(stderr, "Unable to create temporary file: %s\n", strerror(errno));
583 return -1;
584 }
585 unlink(path_template.c_str());
586 return fd;
587}
588
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700589#endif
590
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000591static std::string create_fbemarker_tmpdir() {
592 std::string dir = make_temporary_directory();
593 if (dir.empty()) {
594 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
595 return "";
596 }
597 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
598 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
599 if (fd == -1) {
600 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
601 marker_file.c_str(), errno, strerror(errno));
602 return "";
603 }
604 close(fd);
605 return dir;
606}
607
608static void delete_fbemarker_tmpdir(const std::string& dir) {
609 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
610 if (unlink(marker_file.c_str()) == -1) {
611 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
612 marker_file.c_str(), errno, strerror(errno));
613 return;
614 }
615 if (rmdir(dir.c_str()) == -1) {
616 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
617 dir.c_str(), errno, strerror(errno));
618 return;
619 }
620}
621
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700622static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700623 unique_fd fd(make_temporary_fd());
624 if (fd == -1) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700625 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
626 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700627 return -1;
628 }
629
Yusuke Sato07447542015-06-25 14:39:19 -0700630 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700631 ZipEntry zip_entry;
632 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
633 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000634 return -1;
635 }
636
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700637 fprintf(stderr, "extracting %s (%" PRIu32 " MB)...\n", entry_name,
638 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700639 int error = ExtractEntryToFile(zip, &zip_entry, fd);
640 if (error != 0) {
641 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
642 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700643 }
644
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000645 lseek(fd, 0, SEEK_SET);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700646 // TODO: We're leaking 'fp' here.
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700647 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700648}
649
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650static char *strip(char *s)
651{
652 int n;
653 while(*s && isspace(*s)) s++;
654 n = strlen(s);
655 while(n-- > 0) {
656 if(!isspace(s[n])) break;
657 s[n] = 0;
658 }
659 return s;
660}
661
662#define MAX_OPTIONS 32
663static int setup_requirement_line(char *name)
664{
665 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700666 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800667 unsigned n, count;
668 char *x;
669 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800670
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800671 if (!strncmp(name, "reject ", 7)) {
672 name += 7;
673 invert = 1;
674 } else if (!strncmp(name, "require ", 8)) {
675 name += 8;
676 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700677 } else if (!strncmp(name, "require-for-product:", 20)) {
678 // Get the product and point name past it
679 prod = name + 20;
680 name = strchr(name, ' ');
681 if (!name) return -1;
682 *name = 0;
683 name += 1;
684 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800685 }
686
687 x = strchr(name, '=');
688 if (x == 0) return 0;
689 *x = 0;
690 val[0] = x + 1;
691
692 for(count = 1; count < MAX_OPTIONS; count++) {
693 x = strchr(val[count - 1],'|');
694 if (x == 0) break;
695 *x = 0;
696 val[count] = x + 1;
697 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800698
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800699 name = strip(name);
700 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800701
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800702 name = strip(name);
703 if (name == 0) return -1;
704
Elliott Hughes253c18d2015-03-18 22:47:09 -0700705 const char* var = name;
706 // Work around an unfortunate name mismatch.
707 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800708
Elliott Hughes253c18d2015-03-18 22:47:09 -0700709 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800710 if (out == 0) return -1;
711
712 for(n = 0; n < count; n++) {
713 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700714 if (out[n] == 0) {
715 for(size_t i = 0; i < n; ++i) {
716 free((char*) out[i]);
717 }
718 free(out);
719 return -1;
720 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800721 }
722
Elliott Hughes253c18d2015-03-18 22:47:09 -0700723 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800724 return 0;
725}
726
Elliott Hughesfc797672015-04-07 20:12:50 -0700727static void setup_requirements(char* data, int64_t sz) {
728 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800729 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700730 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800731 *s++ = 0;
732 if (setup_requirement_line(data)) {
733 die("out of memory");
734 }
735 data = s;
736 } else {
737 s++;
738 }
739 }
740}
741
Elliott Hughesfc797672015-04-07 20:12:50 -0700742static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800743 fb_queue_notice("--------------------------------------------");
744 fb_queue_display("version-bootloader", "Bootloader Version...");
745 fb_queue_display("version-baseband", "Baseband Version.....");
746 fb_queue_display("serialno", "Serial Number........");
747 fb_queue_notice("--------------------------------------------");
748}
749
Rom Lemarchand622810c2013-06-28 09:54:59 -0700750static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700751{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700752 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700753 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700754 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700755 }
756
Elliott Hughesfc797672015-04-07 20:12:50 -0700757 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700758 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700759 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700760 }
761
Elliott Hughes253c18d2015-03-18 22:47:09 -0700762 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700763 if (!out_s) {
764 die("Failed to allocate sparse file array\n");
765 }
766
767 files = sparse_file_resparse(s, max_size, out_s, files);
768 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700769 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700770 }
771
772 return out_s;
773}
774
David Pursell0b156632015-10-30 11:22:01 -0700775static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700776 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700777 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
778 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800779 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700780 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700781 }
782
Elliott Hughes77c0e662015-11-02 15:51:12 -0800783 // Some bootloaders (angler, for example) send spurious whitespace too.
784 max_download_size = android::base::Trim(max_download_size);
785
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700786 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700787 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800788 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700789 return 0;
790 }
791 if (limit > 0) {
792 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
793 }
Colin Crossf8387882012-05-24 17:18:41 -0700794 return limit;
795}
796
David Pursell0b156632015-10-30 11:22:01 -0700797static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700798 int64_t limit;
799
800 if (sparse_limit == 0) {
801 return 0;
802 } else if (sparse_limit > 0) {
803 limit = sparse_limit;
804 } else {
805 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700806 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700807 }
808 if (target_sparse_limit > 0) {
809 limit = target_sparse_limit;
810 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700811 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700812 }
813 }
814
815 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500816 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700817 }
818
819 return 0;
820}
821
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700822// Until we get lazy inode table init working in make_ext4fs, we need to
823// erase partitions of type ext4 before flashing a filesystem so no stale
824// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700825static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800826 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700827 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800828 return false;
829 }
830 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700831}
832
Elliott Hughes53ec4952016-05-11 12:39:27 -0700833static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700834 int64_t sz = get_file_size(fd);
835 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700836 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700837 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700838
Elliott Hughesfc797672015-04-07 20:12:50 -0700839 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700840 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700841 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700842 sparse_file** s = load_sparse_files(fd, limit);
843 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700844 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700845 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700846 buf->type = FB_BUFFER_SPARSE;
847 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700848 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500849 buf->type = FB_BUFFER_FD;
850 buf->data = nullptr;
851 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000852 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700853 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700854
Elliott Hughes53ec4952016-05-11 12:39:27 -0700855 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700856}
857
Elliott Hughes53ec4952016-05-11 12:39:27 -0700858static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500859 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
860
Elliott Hughes53ec4952016-05-11 12:39:27 -0700861 if (fd == -1) {
862 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700863 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500864
865 struct stat s;
866 if (fstat(fd, &s)) {
867 return false;
868 }
869 if (!S_ISREG(s.st_mode)) {
870 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
871 return false;
872 }
873
874 return load_buf_fd(transport, fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700875}
876
877static void flash_buf(const char *pname, struct fastboot_buffer *buf)
878{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700879 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700880
881 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800882 case FB_BUFFER_SPARSE: {
883 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700884 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700885 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700886 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800887 sparse_files.emplace_back(*s, sz);
888 ++s;
889 }
890
891 for (size_t i = 0; i < sparse_files.size(); ++i) {
892 const auto& pair = sparse_files[i];
893 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700894 }
895 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800896 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500897 case FB_BUFFER_FD:
898 fb_queue_flash_fd(pname, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700899 break;
900 default:
901 die("unknown buffer type: %d", buf->type);
902 }
903}
904
Daniel Rosenberg80919472016-06-30 19:25:31 -0700905static std::string get_current_slot(Transport* transport)
906{
907 std::string current_slot;
908 if (fb_getvar(transport, "current-slot", &current_slot)) {
909 if (current_slot == "_a") return "a"; // Legacy support
910 if (current_slot == "_b") return "b"; // Legacy support
911 return current_slot;
912 }
913 return "";
914}
915
916// Legacy support
917static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700918 std::vector<std::string> suffixes;
919 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700920 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700921 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700922 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700923 suffixes = android::base::Split(suffix_list, ",");
924 // Unfortunately some devices will return an error message in the
925 // guise of a valid value. If we only see only one suffix, it's probably
926 // not real.
927 if (suffixes.size() == 1) {
928 suffixes.clear();
929 }
930 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700931}
932
Daniel Rosenberg80919472016-06-30 19:25:31 -0700933// Legacy support
934static bool supports_AB_obsolete(Transport* transport) {
935 return !get_suffixes_obsolete(transport).empty();
936}
937
938static int get_slot_count(Transport* transport) {
939 std::string var;
940 int count;
941 if (!fb_getvar(transport, "slot-count", &var)) {
942 if (supports_AB_obsolete(transport)) return 2; // Legacy support
943 }
Elliott Hughesda46b392016-10-11 17:09:00 -0700944 if (!android::base::ParseInt(var, &count)) return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700945 return count;
946}
947
Alex Lightbb9b8a52016-06-29 09:26:44 -0700948static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700949 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700950}
951
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700952// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700953static std::string get_other_slot(const std::string& current_slot, int count) {
954 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700955
Daniel Rosenberg80919472016-06-30 19:25:31 -0700956 char next = (current_slot[0] - 'a' + 1)%count + 'a';
957 return std::string(1, next);
958}
959
960static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
961 return get_other_slot(current_slot, get_slot_count(transport));
962}
963
964static std::string get_other_slot(Transport* transport, int count) {
965 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700966}
967
Alex Lightbb9b8a52016-06-29 09:26:44 -0700968static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700969 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -0700970}
971
Daniel Rosenberg80919472016-06-30 19:25:31 -0700972static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
973 std::string slot = slot_name;
974 if (slot == "_a") slot = "a"; // Legacy support
975 if (slot == "_b") slot = "b"; // Legacy support
976 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800977 if (allow_all) {
978 return "all";
979 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700980 int count = get_slot_count(transport);
981 if (count > 0) {
982 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800983 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800984 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800985 }
986 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700987 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800988
Daniel Rosenberg80919472016-06-30 19:25:31 -0700989 int count = get_slot_count(transport);
990 if (count == 0) die("Device does not support slots.\n");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800991
Daniel Rosenberg80919472016-06-30 19:25:31 -0700992 if (slot == "other") {
993 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700994 if (other == "") {
995 die("No known slots.");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800996 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700997 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800998 }
999
Daniel Rosenberg80919472016-06-30 19:25:31 -07001000 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
1001
1002 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
1003 for (int i=0; i<count; i++) {
1004 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001005 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001006
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001007 exit(1);
1008}
1009
Daniel Rosenberg80919472016-06-30 19:25:31 -07001010static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001011 return verify_slot(transport, slot, true);
1012}
1013
Daniel Rosenberg80919472016-06-30 19:25:31 -07001014static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001015 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001016 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001017 std::string current_slot;
1018
Daniel Rosenberg80919472016-06-30 19:25:31 -07001019 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001020 /* If has-slot is not supported, the answer is no. */
1021 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001022 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001023 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001024 if (slot == "") {
1025 current_slot = get_current_slot(transport);
1026 if (current_slot == "") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001027 die("Failed to identify current slot.\n");
1028 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001029 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001030 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001031 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001032 }
1033 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001034 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001035 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001036 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001037 }
1038 func(part);
1039 }
1040}
1041
David Pursell0b156632015-10-30 11:22:01 -07001042/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1043 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1044 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1045 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001046 */
Daniel Rosenberg80919472016-06-30 19:25:31 -07001047static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001048 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001049 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001050
Daniel Rosenberg80919472016-06-30 19:25:31 -07001051 if (slot == "all") {
1052 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
1053 die("Could not check if partition %s has slot.", part.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001054 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001055 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001056 for (int i=0; i < get_slot_count(transport); i++) {
1057 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001058 }
1059 } else {
David Pursell0b156632015-10-30 11:22:01 -07001060 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001061 }
1062 } else {
David Pursell0b156632015-10-30 11:22:01 -07001063 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001064 }
1065}
1066
David Pursell0b156632015-10-30 11:22:01 -07001067static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001068 struct fastboot_buffer buf;
1069
Elliott Hughes53ec4952016-05-11 12:39:27 -07001070 if (!load_buf(transport, fname, &buf)) {
1071 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001072 }
1073 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001074}
1075
Elliott Hughesfc797672015-04-07 20:12:50 -07001076static void do_update_signature(ZipArchiveHandle zip, char* fn) {
1077 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001078 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001079 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001080 fb_queue_download("signature", data, sz);
1081 fb_queue_command("signature", "installing signature");
1082}
1083
Daniel Rosenberg13454092016-06-27 19:43:11 -07001084// Sets slot_override as the active slot. If slot_override is blank,
1085// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -07001086static void set_active(Transport* transport, const std::string& slot_override) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001087 std::string separator = "";
Alex Lightbb9b8a52016-06-29 09:26:44 -07001088 if (!supports_AB(transport)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001089 if (supports_AB_obsolete(transport)) {
1090 separator = "_"; // Legacy support
1091 } else {
1092 return;
1093 }
1094 }
1095 if (slot_override != "") {
1096 fb_set_active((separator + slot_override).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001097 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001098 std::string current_slot = get_current_slot(transport);
1099 if (current_slot != "") {
1100 fb_set_active((separator + current_slot).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001101 }
1102 }
1103}
1104
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001105static void do_update(Transport* transport, const char* filename, const std::string& slot_override, bool erase_first, bool skip_secondary) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001106 queue_info_dump();
1107
Wink Savilleb98762f2011-04-04 17:54:59 -07001108 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1109
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001110 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001111 int error = OpenArchive(filename, &zip);
1112 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001113 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001114 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001115 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001116
Elliott Hughesfc797672015-04-07 20:12:50 -07001117 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001118 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001119 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001120 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001121 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001122 }
1123
Elliott Hughes253c18d2015-03-18 22:47:09 -07001124 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001125
Alex Lightbb9b8a52016-06-29 09:26:44 -07001126 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001127 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001128 if (slot_override != "") {
1129 secondary = get_other_slot(transport, slot_override);
1130 } else {
1131 secondary = get_other_slot(transport);
1132 }
1133 if (secondary == "") {
1134 if (supports_AB(transport)) {
1135 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1136 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001137 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001138 }
1139 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001140 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001141 const char* slot = slot_override.c_str();
1142 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001143 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001144 slot = secondary.c_str();
1145 } else {
1146 continue;
1147 }
1148 }
1149
Elliott Hughes253c18d2015-03-18 22:47:09 -07001150 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001151 if (fd == -1) {
1152 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001153 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001154 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001155 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001156 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001157 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001158 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001159 if (!load_buf_fd(transport, fd, &buf)) {
1160 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1161 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001162
1163 auto update = [&](const std::string &partition) {
1164 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001165 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001166 fb_queue_erase(partition.c_str());
1167 }
1168 flash_buf(partition.c_str(), &buf);
1169 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001170 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001171 * program exits.
1172 */
1173 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001174 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001175 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001176
1177 CloseArchive(zip);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001178 if (slot_override == "all") {
1179 set_active(transport, "a");
1180 } else {
1181 set_active(transport, slot_override);
1182 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001183}
1184
Alex Lightbb9b8a52016-06-29 09:26:44 -07001185static void do_send_signature(const std::string& fn) {
1186 std::size_t extension_loc = fn.find(".img");
1187 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001188
Alex Lightbb9b8a52016-06-29 09:26:44 -07001189 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001190
1191 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001192 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001193 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001194
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001195 fb_queue_download("signature", data, sz);
1196 fb_queue_command("signature", "installing signature");
1197}
1198
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001199static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001200 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001201 queue_info_dump();
1202
Wink Savilleb98762f2011-04-04 17:54:59 -07001203 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1204
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001205 fname = find_item("info", product);
Elliott Hughes2810d002016-04-25 14:31:18 -07001206 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001207
Elliott Hughesfc797672015-04-07 20:12:50 -07001208 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001209 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001210 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001211
1212 setup_requirements(reinterpret_cast<char*>(data), sz);
1213
Alex Lightbb9b8a52016-06-29 09:26:44 -07001214 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001215 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001216 if (slot_override != "") {
1217 secondary = get_other_slot(transport, slot_override);
1218 } else {
1219 secondary = get_other_slot(transport);
1220 }
1221 if (secondary == "") {
1222 if (supports_AB(transport)) {
1223 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1224 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001225 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001226 }
1227 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001228
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001229 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001230 const char* slot = NULL;
1231 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001232 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001233 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001234 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001235 }
1236 if (!slot) continue;
1237 fname = find_item_given_name(images[i].img_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001238 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001239 if (!load_buf(transport, fname.c_str(), &buf)) {
1240 if (images[i].is_optional) continue;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001241 die("could not load '%s': %s\n", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001242 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001243
1244 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001245 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001246 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001247 fb_queue_erase(partition.c_str());
1248 }
1249 flash_buf(partition.c_str(), &buf);
1250 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001251 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001252 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001253
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001254 if (slot_override == "all") {
1255 set_active(transport, "a");
1256 } else {
1257 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001258 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001259}
1260
1261#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001262#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001263
Elliott Hughes45be42e2015-09-02 20:15:48 -07001264static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001265{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001266 if (argc <= 2) return 0;
1267 skip(2);
1268
1269 /*
1270 * Process unlock_bootloader, we have to load the message file
1271 * and send that to the remote device.
1272 */
1273 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001274
1275 int64_t sz;
1276 void* data = load_file(*argv, &sz);
1277 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001278 fb_queue_download("unlock_message", data, sz);
1279 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1280 skip(1);
1281 return 0;
1282}
1283
Elliott Hughes4a667302017-03-15 09:37:28 -07001284static int do_oem_command(int argc, char** argv) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001285 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001286
Elliott Hughes4a667302017-03-15 09:37:28 -07001287 std::string command;
1288 while (argc > 0) {
1289 command += *argv;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001290 skip(1);
Elliott Hughes4a667302017-03-15 09:37:28 -07001291 if (argc != 0) command += " ";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001292 }
1293
Elliott Hughes4a667302017-03-15 09:37:28 -07001294 fb_queue_command(command.c_str(), "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001295 return 0;
1296}
1297
Colin Crossf8387882012-05-24 17:18:41 -07001298static int64_t parse_num(const char *arg)
1299{
1300 char *endptr;
1301 unsigned long long num;
1302
1303 num = strtoull(arg, &endptr, 0);
1304 if (endptr == arg) {
1305 return -1;
1306 }
1307
1308 if (*endptr == 'k' || *endptr == 'K') {
1309 if (num >= (-1ULL) / 1024) {
1310 return -1;
1311 }
1312 num *= 1024LL;
1313 endptr++;
1314 } else if (*endptr == 'm' || *endptr == 'M') {
1315 if (num >= (-1ULL) / (1024 * 1024)) {
1316 return -1;
1317 }
1318 num *= 1024LL * 1024LL;
1319 endptr++;
1320 } else if (*endptr == 'g' || *endptr == 'G') {
1321 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1322 return -1;
1323 }
1324 num *= 1024LL * 1024LL * 1024LL;
1325 endptr++;
1326 }
1327
1328 if (*endptr != '\0') {
1329 return -1;
1330 }
1331
1332 if (num > INT64_MAX) {
1333 return -1;
1334 }
1335
1336 return num;
1337}
1338
Connor O'Brience16a8a2017-02-06 14:39:31 -08001339static std::string fb_fix_numeric_var(std::string var) {
1340 // Some bootloaders (angler, for example), send spurious leading whitespace.
1341 var = android::base::Trim(var);
1342 // Some bootloaders (hammerhead, for example) use implicit hex.
1343 // This code used to use strtol with base 16.
1344 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1345 return var;
1346}
1347
1348static unsigned fb_get_flash_block_size(Transport* transport, std::string name) {
1349 std::string sizeString;
1350 if (!fb_getvar(transport, name.c_str(), &sizeString)) {
1351 /* This device does not report flash block sizes, so return 0 */
1352 return 0;
1353 }
1354 sizeString = fb_fix_numeric_var(sizeString);
1355
1356 unsigned size;
1357 if (!android::base::ParseUint(sizeString, &size)) {
1358 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1359 return 0;
1360 }
1361 if (size < 4096 || (size & (size - 1)) != 0) {
1362 fprintf(stderr, "Invalid %s %u: must be a power of 2 and at least 4096.\n",
1363 name.c_str(), size);
1364 return 0;
1365 }
1366 return size;
1367}
1368
David Pursell0b156632015-10-30 11:22:01 -07001369static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001370 const char* partition, int skip_if_not_supported,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001371 const char* type_override, const char* size_override,
1372 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001373 std::string partition_type, partition_size;
1374
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001375 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001376 const char* errMsg = nullptr;
1377 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001378 int fd;
1379
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001380 unsigned int limit = INT_MAX;
1381 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001382 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001383 }
1384 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001385 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001386 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001387
David Pursell0b156632015-10-30 11:22:01 -07001388 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001389 errMsg = "Can't determine partition type.\n";
1390 goto failed;
1391 }
JP Abgrall7e859742014-05-06 15:14:15 -07001392 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001393 if (partition_type != type_override) {
1394 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1395 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001396 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001397 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001398 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001399
David Pursell0b156632015-10-30 11:22:01 -07001400 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001401 errMsg = "Unable to get partition size\n";
1402 goto failed;
1403 }
JP Abgrall7e859742014-05-06 15:14:15 -07001404 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001405 if (partition_size != size_override) {
1406 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1407 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001408 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001409 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001410 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001411 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001412
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001413 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001414 if (!gen) {
1415 if (skip_if_not_supported) {
1416 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001417 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001418 return;
1419 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001420 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1421 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001422 return;
1423 }
1424
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001425 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001426 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001427 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1428 return;
1429 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001430
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001431 fd = make_temporary_fd();
1432 if (fd == -1) return;
Connor O'Brience16a8a2017-02-06 14:39:31 -08001433
1434 unsigned eraseBlkSize, logicalBlkSize;
1435 eraseBlkSize = fb_get_flash_block_size(transport, "erase-block-size");
1436 logicalBlkSize = fb_get_flash_block_size(transport, "logical-block-size");
1437
1438 if (fs_generator_generate(gen, fd, size, initial_dir, eraseBlkSize, logicalBlkSize)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001439 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001440 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001441 return;
1442 }
1443
Elliott Hughes53ec4952016-05-11 12:39:27 -07001444 if (!load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001445 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001446 close(fd);
1447 return;
1448 }
1449 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001450 return;
1451
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001452failed:
1453 if (skip_if_not_supported) {
1454 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001455 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001456 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001457 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001458}
1459
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001460int main(int argc, char **argv)
1461{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001462 bool wants_wipe = false;
1463 bool wants_reboot = false;
1464 bool wants_reboot_bootloader = false;
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001465 bool wants_reboot_emergency = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001466 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001467 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001468 bool skip_secondary = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001469 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001470 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001471 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001472 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001473 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001474 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001475 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001476
JP Abgrall7b8970c2013-03-07 17:06:41 -08001477 const struct option longopts[] = {
1478 {"base", required_argument, 0, 'b'},
1479 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001480 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001481 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001482 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001483 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001484 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001485 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001486 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001487 {"help", no_argument, 0, 'h'},
1488 {"unbuffered", no_argument, 0, 0},
1489 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001490 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001491 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001492 {"set-active", optional_argument, 0, 'a'},
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001493 {"skip-secondary", no_argument, 0, 0},
Mitchell Wills31dce302016-09-26 10:26:21 -07001494 {"skip-reboot", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001495#if !defined(_WIN32)
1496 {"wipe-and-use-fbe", no_argument, 0, 0},
1497#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001498 {0, 0, 0, 0}
1499 };
Colin Cross8879f982012-05-22 17:53:34 -07001500
1501 serial = getenv("ANDROID_SERIAL");
1502
1503 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001504 int c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:ha::", longopts, &longindex);
Colin Cross8879f982012-05-22 17:53:34 -07001505 if (c < 0) {
1506 break;
1507 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001508 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001509 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001510 case 'a':
1511 wants_set_active = true;
1512 if (optarg)
1513 next_active = optarg;
1514 break;
Colin Cross8879f982012-05-22 17:53:34 -07001515 case 'b':
1516 base_addr = strtoul(optarg, 0, 16);
1517 break;
Colin Cross8879f982012-05-22 17:53:34 -07001518 case 'c':
1519 cmdline = optarg;
1520 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001521 case 'h':
1522 usage();
1523 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001524 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001525 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001526 unsigned long val;
1527
1528 val = strtoul(optarg, &endptr, 0);
1529 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1530 die("invalid vendor id '%s'", optarg);
1531 vendor_id = (unsigned short)val;
1532 break;
1533 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001534 case 'k':
1535 kernel_offset = strtoul(optarg, 0, 16);
1536 break;
1537 case 'l':
1538 long_listing = 1;
1539 break;
1540 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001541 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001542 if (!page_size) die("invalid page size");
1543 break;
1544 case 'p':
1545 product = optarg;
1546 break;
1547 case 'r':
1548 ramdisk_offset = strtoul(optarg, 0, 16);
1549 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001550 case 't':
1551 tags_offset = strtoul(optarg, 0, 16);
1552 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001553 case 's':
1554 serial = optarg;
1555 break;
1556 case 'S':
1557 sparse_limit = parse_num(optarg);
1558 if (sparse_limit < 0) {
1559 die("invalid sparse limit");
1560 }
1561 break;
1562 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001563 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001564 break;
1565 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001566 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001567 break;
Colin Cross8879f982012-05-22 17:53:34 -07001568 case '?':
1569 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001570 case 0:
1571 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001572 setvbuf(stdout, nullptr, _IONBF, 0);
1573 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001574 } else if (strcmp("version", longopts[longindex].name) == 0) {
1575 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001576 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001577 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001578 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1579 slot_override = std::string(optarg);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001580 } else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
1581 skip_secondary = true;
Mitchell Wills31dce302016-09-26 10:26:21 -07001582 } else if (strcmp("skip-reboot", longopts[longindex].name) == 0 ) {
1583 skip_reboot = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001584#if !defined(_WIN32)
1585 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1586 wants_wipe = true;
1587 set_fbe_marker = true;
1588#endif
1589 } else {
1590 fprintf(stderr, "Internal error in options processing for %s\n",
1591 longopts[longindex].name);
1592 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001593 }
1594 break;
Colin Cross8879f982012-05-22 17:53:34 -07001595 default:
1596 abort();
1597 }
1598 }
1599
1600 argc -= optind;
1601 argv += optind;
1602
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001603 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001604 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001605 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001606 }
1607
Colin Cross8fb6e062012-07-24 16:36:41 -07001608 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001609 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001610 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001611 return 0;
1612 }
1613
Colin Crossc7b75dc2012-08-29 18:17:06 -07001614 if (argc > 0 && !strcmp(*argv, "help")) {
1615 usage();
1616 return 0;
1617 }
1618
David Pursell0b156632015-10-30 11:22:01 -07001619 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001620 if (transport == nullptr) {
1621 return 1;
1622 }
1623
Daniel Rosenberg80919472016-06-30 19:25:31 -07001624 if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
1625 fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
1626 }
1627 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1628 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001629
1630 if (wants_set_active) {
1631 if (next_active == "") {
1632 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001633 std::string current_slot;
1634 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001635 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001636 } else {
1637 wants_set_active = false;
1638 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001639 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001640 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001641 }
1642 }
1643 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001644
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001645 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001646 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001647 require(2);
1648 fb_queue_display(argv[1], argv[1]);
1649 skip(2);
1650 } else if(!strcmp(*argv, "erase")) {
1651 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001652
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001653 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001654 std::string partition_type;
1655 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001656 fs_get_generator(partition_type) != nullptr) {
1657 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1658 partition_type.c_str());
1659 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001660
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001661 fb_queue_erase(partition.c_str());
1662 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001663 do_for_partitions(transport, argv[1], slot_override, erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001664 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001665 } else if(!strncmp(*argv, "format", strlen("format"))) {
1666 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001667 char *type_override = nullptr;
1668 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001669 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001670 /*
1671 * Parsing for: "format[:[type][:[size]]]"
1672 * Some valid things:
1673 * - select ontly the size, and leave default fs type:
1674 * format::0x4000000 userdata
1675 * - default fs type and size:
1676 * format userdata
1677 * format:: userdata
1678 */
1679 overrides = strchr(*argv, ':');
1680 if (overrides) {
1681 overrides++;
1682 size_override = strchr(overrides, ':');
1683 if (size_override) {
1684 size_override[0] = '\0';
1685 size_override++;
1686 }
1687 type_override = overrides;
1688 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001689 if (type_override && !type_override[0]) type_override = nullptr;
1690 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001691
1692 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001693 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001694 fb_queue_erase(partition.c_str());
1695 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001696 fb_perform_format(transport, partition.c_str(), 0,
1697 type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001698 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001699 do_for_partitions(transport, argv[1], slot_override, format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001700 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001701 } else if(!strcmp(*argv, "signature")) {
1702 require(2);
1703 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001704 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001705 if (sz != 256) die("signature must be 256 bytes");
1706 fb_queue_download("signature", data, sz);
1707 fb_queue_command("signature", "installing signature");
1708 skip(2);
1709 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001710 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001711 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001712 if (argc > 0) {
1713 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001714 wants_reboot = false;
1715 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001716 skip(1);
Pavlin Radoslavov6adbd4e2017-01-25 19:07:46 -08001717 } else if (!strcmp(*argv, "emergency")) {
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001718 wants_reboot = false;
1719 wants_reboot_emergency = true;
1720 skip(1);
1721 }
Elliott Hughesca85df02015-02-25 10:02:00 -08001722 }
1723 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001724 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001725 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001726 skip(1);
1727 } else if (!strcmp(*argv, "continue")) {
1728 fb_queue_command("continue", "resuming boot");
1729 skip(1);
1730 } else if(!strcmp(*argv, "boot")) {
1731 char *kname = 0;
1732 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001733 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001734 skip(1);
1735 if (argc > 0) {
1736 kname = argv[0];
1737 skip(1);
1738 }
1739 if (argc > 0) {
1740 rname = argv[0];
1741 skip(1);
1742 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001743 if (argc > 0) {
1744 sname = argv[0];
1745 skip(1);
1746 }
1747 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001748 if (data == 0) return 1;
1749 fb_queue_download("boot.img", data, sz);
1750 fb_queue_command("boot", "booting");
1751 } else if(!strcmp(*argv, "flash")) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001752 char* pname = argv[1];
1753 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001754 require(2);
1755 if (argc > 2) {
1756 fname = argv[2];
1757 skip(3);
1758 } else {
1759 fname = find_item(pname, product);
1760 skip(2);
1761 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001762 if (fname.empty()) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001763
1764 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001765 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001766 fb_queue_erase(partition.c_str());
1767 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001768 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001769 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001770 do_for_partitions(transport, pname, slot_override, flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001771 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001772 char *kname = argv[2];
1773 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001774 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001775 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001776 skip(3);
1777 if (argc > 0) {
1778 rname = argv[0];
1779 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001780 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001781 if (argc > 0) {
1782 sname = argv[0];
1783 skip(1);
1784 }
1785 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001786 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001787 auto flashraw = [&](const std::string &partition) {
1788 fb_queue_flash(partition.c_str(), data, sz);
1789 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001790 do_for_partitions(transport, argv[1], slot_override, flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001791 } else if(!strcmp(*argv, "flashall")) {
1792 skip(1);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001793 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001794 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1795 do_flashall(transport, slot_override, erase_first, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001796 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001797 do_flashall(transport, slot_override, erase_first, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001798 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001799 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001800 } else if(!strcmp(*argv, "update")) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001801 bool slot_all = (slot_override == "all");
1802 if (slot_all) {
1803 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1804 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001805 if (argc > 1) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001806 do_update(transport, argv[1], slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001807 skip(2);
1808 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001809 do_update(transport, "update.zip", slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001810 skip(1);
1811 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001812 wants_reboot = true;
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001813 } else if(!strcmp(*argv, "set_active")) {
1814 require(2);
Daniel Rosenberg80919472016-06-30 19:25:31 -07001815 std::string slot = verify_slot(transport, std::string(argv[1]), false);
David Pursell04396f62016-12-15 16:16:10 -08001816 // Legacy support: verify_slot() removes leading underscores, we need to put them back
1817 // in for old bootloaders. Legacy bootloaders do not have the slot-count variable but
1818 // do have slot-suffixes.
1819 std::string var;
1820 if (!fb_getvar(transport, "slot-count", &var) &&
1821 fb_getvar(transport, "slot-suffixes", &var)) {
1822 slot = "_" + slot;
1823 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001824 fb_set_active(slot.c_str());
1825 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001826 } else if(!strcmp(*argv, "oem")) {
1827 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001828 } else if(!strcmp(*argv, "flashing")) {
1829 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1830 !strcmp(*(argv+1), "lock") ||
1831 !strcmp(*(argv+1), "unlock_critical") ||
1832 !strcmp(*(argv+1), "lock_critical") ||
1833 !strcmp(*(argv+1), "get_unlock_ability") ||
1834 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1835 !strcmp(*(argv+1), "lock_bootloader"))) {
1836 argc = do_oem_command(argc, argv);
1837 } else
1838 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1839 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001840 } else {
1841 usage();
1842 return 1;
1843 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001844 } else {
1845 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001846 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001847 }
1848 }
1849
1850 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001851 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001852 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001853 if (set_fbe_marker) {
1854 fprintf(stderr, "setting FBE marker...\n");
1855 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1856 if (initial_userdata_dir.empty()) {
1857 return 1;
1858 }
1859 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
1860 delete_fbemarker_tmpdir(initial_userdata_dir);
1861 } else {
1862 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
1863 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001864
1865 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001866 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001867 fprintf(stderr, "wiping cache...\n");
1868 fb_queue_erase("cache");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001869 fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001870 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001871 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001872 if (wants_set_active) {
1873 fb_set_active(next_active.c_str());
1874 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001875 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001876 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001877 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001878 } else if (wants_reboot_bootloader) {
1879 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001880 fb_queue_wait_for_disconnect();
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001881 } else if (wants_reboot_emergency) {
1882 fb_queue_command("reboot-emergency", "rebooting into emergency download (EDL) mode");
1883 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001884 }
1885
David Pursell0b156632015-10-30 11:22:01 -07001886 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001887}