blob: 982545ca6d7368305d12920b4c6e73dafef0f8d1 [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"
Jocelyn Bohr98cc2832017-01-26 19:20:53 -0800374 " oem <parameter1> ... <parameterN> Executes oem specific command.\n"
375 " stage <infile> Sends contents of <infile> to stage for\n"
376 " the next command. Supported only on\n"
377 " Android Things devices.\n"
Jocelyn Bohr91fefad2017-01-27 14:17:56 -0800378 " get_staged <outfile> Receives data to <outfile> staged by the\n"
379 " last command. Supported only on Android\n"
380 " Things devices.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700381 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382 "\n"
383 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700384 " -w Erase userdata and cache (and format\n"
385 " if supported by partition type).\n"
386 " -u Do not erase partition before\n"
387 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800388 " -s <specific device> Specify a device. For USB, provide either\n"
389 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700390 " For ethernet, provide an address in the\n"
391 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800392 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700393 " -p <product> Specify product name.\n"
394 " -c <cmdline> Override kernel commandline.\n"
395 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800396 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700397 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800398 " --kernel-offset Specify a custom kernel offset.\n"
399 " (default: 0x00008000)\n"
400 " --ramdisk-offset Specify a custom ramdisk offset.\n"
401 " (default: 0x01000000)\n"
402 " --tags-offset Specify a custom tags offset.\n"
403 " (default: 0x00000100)\n"
404 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700405 " (default: 2048).\n"
406 " -S <size>[K|M|G] Automatically sparse files greater\n"
407 " than 'size'. 0 to disable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700408 " --slot <slot> Specify slot name to be used if the\n"
409 " device supports slots. All operations\n"
410 " on partitions that support slots will\n"
411 " be done on the slot specified.\n"
412 " 'all' can be given to refer to all slots.\n"
413 " 'other' can be given to refer to a\n"
414 " non-current slot. If this flag is not\n"
415 " used, slotted partitions will default\n"
416 " to the current active slot.\n"
417 " -a, --set-active[=<slot>] Sets the active slot. If no slot is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800418 " provided, this will default to the value\n"
419 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800420 " supported, this does nothing. This will\n"
421 " run after all non-reboot commands.\n"
Daniel Rosenberg92b44762016-07-13 20:03:25 -0700422 " --skip-secondary Will not flash secondary slots when\n"
423 " performing a flashall or update. This\n"
424 " will preserve data on other slots.\n"
Mitchell Wills31dce302016-09-26 10:26:21 -0700425 " --skip-reboot Will not reboot the device when\n"
426 " performing commands that normally\n"
427 " trigger a reboot.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000428#if !defined(_WIN32)
429 " --wipe-and-use-fbe On devices which support it,\n"
430 " erase userdata and cache, and\n"
431 " enable file-based encryption\n"
432#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800433 " --unbuffered Do not buffer input or output.\n"
434 " --version Display version.\n"
435 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000438
Elliott Hughesfc797672015-04-07 20:12:50 -0700439static void* load_bootable_image(const char* kernel, const char* ramdisk,
440 const char* secondstage, int64_t* sz,
441 const char* cmdline) {
442 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443 fprintf(stderr, "no image specified\n");
444 return 0;
445 }
446
Elliott Hughesfc797672015-04-07 20:12:50 -0700447 int64_t ksize;
448 void* kdata = load_file(kernel, &ksize);
449 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800450 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451 return 0;
452 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800453
Elliott Hughesfc797672015-04-07 20:12:50 -0700454 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700456 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800457
Elliott Hughesfc797672015-04-07 20:12:50 -0700458 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
460 return 0;
461 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800462
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 *sz = ksize;
464 return kdata;
465 }
466
Elliott Hughesfc797672015-04-07 20:12:50 -0700467 void* rdata = nullptr;
468 int64_t rsize = 0;
469 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700471 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800472 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 return 0;
474 }
475 }
476
Elliott Hughesfc797672015-04-07 20:12:50 -0700477 void* sdata = nullptr;
478 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200479 if (secondstage) {
480 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700481 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200482 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
483 return 0;
484 }
485 }
486
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700488 int64_t bsize = 0;
489 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800490 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200491 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800492 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700493 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494 fprintf(stderr,"failed to create boot.img\n");
495 return 0;
496 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700497 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
498 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800500
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 return bdata;
502}
503
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700504static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700505 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700506 ZipEntry zip_entry;
507 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700508 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000509 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510 }
511
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700512 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700514 fprintf(stderr, "extracting %s (%" PRId64 " MB)...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700515 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700516 if (data == nullptr) {
517 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000518 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519 }
520
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700521 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
522 if (error != 0) {
523 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000525 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000527
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528 return data;
529}
530
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700531#if defined(_WIN32)
532
533// TODO: move this to somewhere it can be shared.
534
535#include <windows.h>
536
537// Windows' tmpfile(3) requires administrator rights because
538// it creates temporary files in the root directory.
539static FILE* win32_tmpfile() {
540 char temp_path[PATH_MAX];
541 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
542 if (nchars == 0 || nchars >= sizeof(temp_path)) {
543 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
544 return nullptr;
545 }
546
547 char filename[PATH_MAX];
548 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
549 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
550 return nullptr;
551 }
552
553 return fopen(filename, "w+bTD");
554}
555
556#define tmpfile win32_tmpfile
557
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000558static std::string make_temporary_directory() {
559 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
560 return "";
561}
562
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700563static int make_temporary_fd() {
564 // TODO: reimplement to avoid leaking a FILE*.
565 return fileno(tmpfile());
566}
567
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000568#else
569
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700570static std::string make_temporary_template() {
571 const char* tmpdir = getenv("TMPDIR");
572 if (tmpdir == nullptr) tmpdir = P_tmpdir;
573 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
574}
575
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000576static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700577 std::string result(make_temporary_template());
578 if (mkdtemp(&result[0]) == nullptr) {
579 fprintf(stderr, "Unable to create temporary directory: %s\n", strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000580 return "";
581 }
582 return result;
583}
584
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700585static int make_temporary_fd() {
586 std::string path_template(make_temporary_template());
587 int fd = mkstemp(&path_template[0]);
588 if (fd == -1) {
589 fprintf(stderr, "Unable to create temporary file: %s\n", strerror(errno));
590 return -1;
591 }
592 unlink(path_template.c_str());
593 return fd;
594}
595
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700596#endif
597
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000598static std::string create_fbemarker_tmpdir() {
599 std::string dir = make_temporary_directory();
600 if (dir.empty()) {
601 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
602 return "";
603 }
604 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
605 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
606 if (fd == -1) {
607 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
608 marker_file.c_str(), errno, strerror(errno));
609 return "";
610 }
611 close(fd);
612 return dir;
613}
614
615static void delete_fbemarker_tmpdir(const std::string& dir) {
616 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
617 if (unlink(marker_file.c_str()) == -1) {
618 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
619 marker_file.c_str(), errno, strerror(errno));
620 return;
621 }
622 if (rmdir(dir.c_str()) == -1) {
623 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
624 dir.c_str(), errno, strerror(errno));
625 return;
626 }
627}
628
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700629static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700630 unique_fd fd(make_temporary_fd());
631 if (fd == -1) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700632 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
633 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700634 return -1;
635 }
636
Yusuke Sato07447542015-06-25 14:39:19 -0700637 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700638 ZipEntry zip_entry;
639 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
640 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000641 return -1;
642 }
643
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700644 fprintf(stderr, "extracting %s (%" PRIu32 " MB)...\n", entry_name,
645 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700646 int error = ExtractEntryToFile(zip, &zip_entry, fd);
647 if (error != 0) {
648 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
649 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700650 }
651
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000652 lseek(fd, 0, SEEK_SET);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700653 // TODO: We're leaking 'fp' here.
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700654 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700655}
656
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800657static char *strip(char *s)
658{
659 int n;
660 while(*s && isspace(*s)) s++;
661 n = strlen(s);
662 while(n-- > 0) {
663 if(!isspace(s[n])) break;
664 s[n] = 0;
665 }
666 return s;
667}
668
669#define MAX_OPTIONS 32
670static int setup_requirement_line(char *name)
671{
672 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700673 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800674 unsigned n, count;
675 char *x;
676 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800677
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800678 if (!strncmp(name, "reject ", 7)) {
679 name += 7;
680 invert = 1;
681 } else if (!strncmp(name, "require ", 8)) {
682 name += 8;
683 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700684 } else if (!strncmp(name, "require-for-product:", 20)) {
685 // Get the product and point name past it
686 prod = name + 20;
687 name = strchr(name, ' ');
688 if (!name) return -1;
689 *name = 0;
690 name += 1;
691 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692 }
693
694 x = strchr(name, '=');
695 if (x == 0) return 0;
696 *x = 0;
697 val[0] = x + 1;
698
699 for(count = 1; count < MAX_OPTIONS; count++) {
700 x = strchr(val[count - 1],'|');
701 if (x == 0) break;
702 *x = 0;
703 val[count] = x + 1;
704 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800705
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800706 name = strip(name);
707 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800708
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800709 name = strip(name);
710 if (name == 0) return -1;
711
Elliott Hughes253c18d2015-03-18 22:47:09 -0700712 const char* var = name;
713 // Work around an unfortunate name mismatch.
714 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800715
Elliott Hughes253c18d2015-03-18 22:47:09 -0700716 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800717 if (out == 0) return -1;
718
719 for(n = 0; n < count; n++) {
720 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700721 if (out[n] == 0) {
722 for(size_t i = 0; i < n; ++i) {
723 free((char*) out[i]);
724 }
725 free(out);
726 return -1;
727 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800728 }
729
Elliott Hughes253c18d2015-03-18 22:47:09 -0700730 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800731 return 0;
732}
733
Elliott Hughesfc797672015-04-07 20:12:50 -0700734static void setup_requirements(char* data, int64_t sz) {
735 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800736 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700737 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800738 *s++ = 0;
739 if (setup_requirement_line(data)) {
740 die("out of memory");
741 }
742 data = s;
743 } else {
744 s++;
745 }
746 }
747}
748
Elliott Hughesfc797672015-04-07 20:12:50 -0700749static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800750 fb_queue_notice("--------------------------------------------");
751 fb_queue_display("version-bootloader", "Bootloader Version...");
752 fb_queue_display("version-baseband", "Baseband Version.....");
753 fb_queue_display("serialno", "Serial Number........");
754 fb_queue_notice("--------------------------------------------");
755}
756
Rom Lemarchand622810c2013-06-28 09:54:59 -0700757static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700758{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700759 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700760 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700761 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700762 }
763
Elliott Hughesfc797672015-04-07 20:12:50 -0700764 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700765 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700766 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700767 }
768
Elliott Hughes253c18d2015-03-18 22:47:09 -0700769 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700770 if (!out_s) {
771 die("Failed to allocate sparse file array\n");
772 }
773
774 files = sparse_file_resparse(s, max_size, out_s, files);
775 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700776 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700777 }
778
779 return out_s;
780}
781
David Pursell0b156632015-10-30 11:22:01 -0700782static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700783 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700784 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
785 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800786 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700787 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700788 }
789
Elliott Hughes77c0e662015-11-02 15:51:12 -0800790 // Some bootloaders (angler, for example) send spurious whitespace too.
791 max_download_size = android::base::Trim(max_download_size);
792
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700793 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700794 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800795 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700796 return 0;
797 }
798 if (limit > 0) {
799 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
800 }
Colin Crossf8387882012-05-24 17:18:41 -0700801 return limit;
802}
803
David Pursell0b156632015-10-30 11:22:01 -0700804static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700805 int64_t limit;
806
807 if (sparse_limit == 0) {
808 return 0;
809 } else if (sparse_limit > 0) {
810 limit = sparse_limit;
811 } else {
812 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700813 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700814 }
815 if (target_sparse_limit > 0) {
816 limit = target_sparse_limit;
817 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700818 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700819 }
820 }
821
822 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500823 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700824 }
825
826 return 0;
827}
828
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700829// Until we get lazy inode table init working in make_ext4fs, we need to
830// erase partitions of type ext4 before flashing a filesystem so no stale
831// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700832static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800833 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700834 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800835 return false;
836 }
837 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700838}
839
Elliott Hughes53ec4952016-05-11 12:39:27 -0700840static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700841 int64_t sz = get_file_size(fd);
842 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700843 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700844 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700845
Elliott Hughesfc797672015-04-07 20:12:50 -0700846 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700847 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700848 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700849 sparse_file** s = load_sparse_files(fd, limit);
850 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700851 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700852 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700853 buf->type = FB_BUFFER_SPARSE;
854 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700855 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500856 buf->type = FB_BUFFER_FD;
857 buf->data = nullptr;
858 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000859 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700860 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700861
Elliott Hughes53ec4952016-05-11 12:39:27 -0700862 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700863}
864
Elliott Hughes53ec4952016-05-11 12:39:27 -0700865static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500866 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
867
Elliott Hughes53ec4952016-05-11 12:39:27 -0700868 if (fd == -1) {
869 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700870 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500871
872 struct stat s;
873 if (fstat(fd, &s)) {
874 return false;
875 }
876 if (!S_ISREG(s.st_mode)) {
877 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
878 return false;
879 }
880
881 return load_buf_fd(transport, fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700882}
883
884static void flash_buf(const char *pname, struct fastboot_buffer *buf)
885{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700886 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700887
888 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800889 case FB_BUFFER_SPARSE: {
890 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700891 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700892 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700893 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800894 sparse_files.emplace_back(*s, sz);
895 ++s;
896 }
897
898 for (size_t i = 0; i < sparse_files.size(); ++i) {
899 const auto& pair = sparse_files[i];
900 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700901 }
902 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800903 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500904 case FB_BUFFER_FD:
905 fb_queue_flash_fd(pname, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700906 break;
907 default:
908 die("unknown buffer type: %d", buf->type);
909 }
910}
911
Daniel Rosenberg80919472016-06-30 19:25:31 -0700912static std::string get_current_slot(Transport* transport)
913{
914 std::string current_slot;
915 if (fb_getvar(transport, "current-slot", &current_slot)) {
916 if (current_slot == "_a") return "a"; // Legacy support
917 if (current_slot == "_b") return "b"; // Legacy support
918 return current_slot;
919 }
920 return "";
921}
922
923// Legacy support
924static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700925 std::vector<std::string> suffixes;
926 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700927 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700928 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700929 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700930 suffixes = android::base::Split(suffix_list, ",");
931 // Unfortunately some devices will return an error message in the
932 // guise of a valid value. If we only see only one suffix, it's probably
933 // not real.
934 if (suffixes.size() == 1) {
935 suffixes.clear();
936 }
937 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700938}
939
Daniel Rosenberg80919472016-06-30 19:25:31 -0700940// Legacy support
941static bool supports_AB_obsolete(Transport* transport) {
942 return !get_suffixes_obsolete(transport).empty();
943}
944
945static int get_slot_count(Transport* transport) {
946 std::string var;
947 int count;
948 if (!fb_getvar(transport, "slot-count", &var)) {
949 if (supports_AB_obsolete(transport)) return 2; // Legacy support
950 }
Elliott Hughesda46b392016-10-11 17:09:00 -0700951 if (!android::base::ParseInt(var, &count)) return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700952 return count;
953}
954
Alex Lightbb9b8a52016-06-29 09:26:44 -0700955static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700956 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700957}
958
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700959// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700960static std::string get_other_slot(const std::string& current_slot, int count) {
961 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700962
Daniel Rosenberg80919472016-06-30 19:25:31 -0700963 char next = (current_slot[0] - 'a' + 1)%count + 'a';
964 return std::string(1, next);
965}
966
967static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
968 return get_other_slot(current_slot, get_slot_count(transport));
969}
970
971static std::string get_other_slot(Transport* transport, int count) {
972 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700973}
974
Alex Lightbb9b8a52016-06-29 09:26:44 -0700975static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700976 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -0700977}
978
Daniel Rosenberg80919472016-06-30 19:25:31 -0700979static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
980 std::string slot = slot_name;
981 if (slot == "_a") slot = "a"; // Legacy support
982 if (slot == "_b") slot = "b"; // Legacy support
983 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800984 if (allow_all) {
985 return "all";
986 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700987 int count = get_slot_count(transport);
988 if (count > 0) {
989 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800990 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800991 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800992 }
993 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700994 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800995
Daniel Rosenberg80919472016-06-30 19:25:31 -0700996 int count = get_slot_count(transport);
997 if (count == 0) die("Device does not support slots.\n");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800998
Daniel Rosenberg80919472016-06-30 19:25:31 -0700999 if (slot == "other") {
1000 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001001 if (other == "") {
1002 die("No known slots.");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001003 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001004 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001005 }
1006
Daniel Rosenberg80919472016-06-30 19:25:31 -07001007 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
1008
1009 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
1010 for (int i=0; i<count; i++) {
1011 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001012 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001013
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001014 exit(1);
1015}
1016
Daniel Rosenberg80919472016-06-30 19:25:31 -07001017static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001018 return verify_slot(transport, slot, true);
1019}
1020
Daniel Rosenberg80919472016-06-30 19:25:31 -07001021static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001022 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001023 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001024 std::string current_slot;
1025
Daniel Rosenberg80919472016-06-30 19:25:31 -07001026 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001027 /* If has-slot is not supported, the answer is no. */
1028 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001029 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001030 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001031 if (slot == "") {
1032 current_slot = get_current_slot(transport);
1033 if (current_slot == "") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001034 die("Failed to identify current slot.\n");
1035 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001036 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001037 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001038 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001039 }
1040 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001041 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001042 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001043 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001044 }
1045 func(part);
1046 }
1047}
1048
David Pursell0b156632015-10-30 11:22:01 -07001049/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1050 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1051 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1052 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001053 */
Daniel Rosenberg80919472016-06-30 19:25:31 -07001054static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001055 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001056 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001057
Daniel Rosenberg80919472016-06-30 19:25:31 -07001058 if (slot == "all") {
1059 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
1060 die("Could not check if partition %s has slot.", part.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001061 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001062 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001063 for (int i=0; i < get_slot_count(transport); i++) {
1064 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001065 }
1066 } else {
David Pursell0b156632015-10-30 11:22:01 -07001067 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001068 }
1069 } else {
David Pursell0b156632015-10-30 11:22:01 -07001070 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001071 }
1072}
1073
David Pursell0b156632015-10-30 11:22:01 -07001074static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001075 struct fastboot_buffer buf;
1076
Elliott Hughes53ec4952016-05-11 12:39:27 -07001077 if (!load_buf(transport, fname, &buf)) {
1078 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001079 }
1080 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001081}
1082
Elliott Hughesfc797672015-04-07 20:12:50 -07001083static void do_update_signature(ZipArchiveHandle zip, char* fn) {
1084 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001085 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001086 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001087 fb_queue_download("signature", data, sz);
1088 fb_queue_command("signature", "installing signature");
1089}
1090
Daniel Rosenberg13454092016-06-27 19:43:11 -07001091// Sets slot_override as the active slot. If slot_override is blank,
1092// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -07001093static void set_active(Transport* transport, const std::string& slot_override) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001094 std::string separator = "";
Alex Lightbb9b8a52016-06-29 09:26:44 -07001095 if (!supports_AB(transport)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001096 if (supports_AB_obsolete(transport)) {
1097 separator = "_"; // Legacy support
1098 } else {
1099 return;
1100 }
1101 }
1102 if (slot_override != "") {
1103 fb_set_active((separator + slot_override).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001104 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001105 std::string current_slot = get_current_slot(transport);
1106 if (current_slot != "") {
1107 fb_set_active((separator + current_slot).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001108 }
1109 }
1110}
1111
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001112static 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 -08001113 queue_info_dump();
1114
Wink Savilleb98762f2011-04-04 17:54:59 -07001115 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1116
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001117 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001118 int error = OpenArchive(filename, &zip);
1119 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001120 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001121 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001122 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001123
Elliott Hughesfc797672015-04-07 20:12:50 -07001124 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001125 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001126 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001127 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001128 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001129 }
1130
Elliott Hughes253c18d2015-03-18 22:47:09 -07001131 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001132
Alex Lightbb9b8a52016-06-29 09:26:44 -07001133 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001134 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001135 if (slot_override != "") {
1136 secondary = get_other_slot(transport, slot_override);
1137 } else {
1138 secondary = get_other_slot(transport);
1139 }
1140 if (secondary == "") {
1141 if (supports_AB(transport)) {
1142 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1143 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001144 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001145 }
1146 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001147 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001148 const char* slot = slot_override.c_str();
1149 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001150 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001151 slot = secondary.c_str();
1152 } else {
1153 continue;
1154 }
1155 }
1156
Elliott Hughes253c18d2015-03-18 22:47:09 -07001157 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001158 if (fd == -1) {
1159 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001160 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001161 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001162 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001163 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001164 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001165 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001166 if (!load_buf_fd(transport, fd, &buf)) {
1167 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1168 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001169
1170 auto update = [&](const std::string &partition) {
1171 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001172 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001173 fb_queue_erase(partition.c_str());
1174 }
1175 flash_buf(partition.c_str(), &buf);
1176 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001177 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001178 * program exits.
1179 */
1180 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001181 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001182 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001183
1184 CloseArchive(zip);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001185 if (slot_override == "all") {
1186 set_active(transport, "a");
1187 } else {
1188 set_active(transport, slot_override);
1189 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001190}
1191
Alex Lightbb9b8a52016-06-29 09:26:44 -07001192static void do_send_signature(const std::string& fn) {
1193 std::size_t extension_loc = fn.find(".img");
1194 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001195
Alex Lightbb9b8a52016-06-29 09:26:44 -07001196 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001197
1198 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001199 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001200 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001201
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001202 fb_queue_download("signature", data, sz);
1203 fb_queue_command("signature", "installing signature");
1204}
1205
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001206static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001207 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001208 queue_info_dump();
1209
Wink Savilleb98762f2011-04-04 17:54:59 -07001210 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1211
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001212 fname = find_item("info", product);
Elliott Hughes2810d002016-04-25 14:31:18 -07001213 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001214
Elliott Hughesfc797672015-04-07 20:12:50 -07001215 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001216 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001217 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001218
1219 setup_requirements(reinterpret_cast<char*>(data), sz);
1220
Alex Lightbb9b8a52016-06-29 09:26:44 -07001221 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001222 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001223 if (slot_override != "") {
1224 secondary = get_other_slot(transport, slot_override);
1225 } else {
1226 secondary = get_other_slot(transport);
1227 }
1228 if (secondary == "") {
1229 if (supports_AB(transport)) {
1230 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1231 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001232 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001233 }
1234 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001235
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001236 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001237 const char* slot = NULL;
1238 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001239 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001240 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001241 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001242 }
1243 if (!slot) continue;
1244 fname = find_item_given_name(images[i].img_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001245 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001246 if (!load_buf(transport, fname.c_str(), &buf)) {
1247 if (images[i].is_optional) continue;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001248 die("could not load '%s': %s\n", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001249 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001250
1251 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001252 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001253 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001254 fb_queue_erase(partition.c_str());
1255 }
1256 flash_buf(partition.c_str(), &buf);
1257 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001258 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001259 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001260
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001261 if (slot_override == "all") {
1262 set_active(transport, "a");
1263 } else {
1264 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001265 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001266}
1267
1268#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001269#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001270
Elliott Hughes45be42e2015-09-02 20:15:48 -07001271static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001272{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001273 if (argc <= 2) return 0;
1274 skip(2);
1275
1276 /*
1277 * Process unlock_bootloader, we have to load the message file
1278 * and send that to the remote device.
1279 */
1280 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001281
1282 int64_t sz;
1283 void* data = load_file(*argv, &sz);
1284 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001285 fb_queue_download("unlock_message", data, sz);
1286 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1287 skip(1);
1288 return 0;
1289}
1290
Elliott Hughes4a667302017-03-15 09:37:28 -07001291static int do_oem_command(int argc, char** argv) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001292 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001293
Elliott Hughes4a667302017-03-15 09:37:28 -07001294 std::string command;
1295 while (argc > 0) {
1296 command += *argv;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001297 skip(1);
Elliott Hughes4a667302017-03-15 09:37:28 -07001298 if (argc != 0) command += " ";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001299 }
1300
Elliott Hughes4a667302017-03-15 09:37:28 -07001301 fb_queue_command(command.c_str(), "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001302 return 0;
1303}
1304
Colin Crossf8387882012-05-24 17:18:41 -07001305static int64_t parse_num(const char *arg)
1306{
1307 char *endptr;
1308 unsigned long long num;
1309
1310 num = strtoull(arg, &endptr, 0);
1311 if (endptr == arg) {
1312 return -1;
1313 }
1314
1315 if (*endptr == 'k' || *endptr == 'K') {
1316 if (num >= (-1ULL) / 1024) {
1317 return -1;
1318 }
1319 num *= 1024LL;
1320 endptr++;
1321 } else if (*endptr == 'm' || *endptr == 'M') {
1322 if (num >= (-1ULL) / (1024 * 1024)) {
1323 return -1;
1324 }
1325 num *= 1024LL * 1024LL;
1326 endptr++;
1327 } else if (*endptr == 'g' || *endptr == 'G') {
1328 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1329 return -1;
1330 }
1331 num *= 1024LL * 1024LL * 1024LL;
1332 endptr++;
1333 }
1334
1335 if (*endptr != '\0') {
1336 return -1;
1337 }
1338
1339 if (num > INT64_MAX) {
1340 return -1;
1341 }
1342
1343 return num;
1344}
1345
Connor O'Brience16a8a2017-02-06 14:39:31 -08001346static std::string fb_fix_numeric_var(std::string var) {
1347 // Some bootloaders (angler, for example), send spurious leading whitespace.
1348 var = android::base::Trim(var);
1349 // Some bootloaders (hammerhead, for example) use implicit hex.
1350 // This code used to use strtol with base 16.
1351 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1352 return var;
1353}
1354
1355static unsigned fb_get_flash_block_size(Transport* transport, std::string name) {
1356 std::string sizeString;
1357 if (!fb_getvar(transport, name.c_str(), &sizeString)) {
1358 /* This device does not report flash block sizes, so return 0 */
1359 return 0;
1360 }
1361 sizeString = fb_fix_numeric_var(sizeString);
1362
1363 unsigned size;
1364 if (!android::base::ParseUint(sizeString, &size)) {
1365 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1366 return 0;
1367 }
1368 if (size < 4096 || (size & (size - 1)) != 0) {
1369 fprintf(stderr, "Invalid %s %u: must be a power of 2 and at least 4096.\n",
1370 name.c_str(), size);
1371 return 0;
1372 }
1373 return size;
1374}
1375
David Pursell0b156632015-10-30 11:22:01 -07001376static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001377 const char* partition, int skip_if_not_supported,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001378 const char* type_override, const char* size_override,
1379 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001380 std::string partition_type, partition_size;
1381
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001382 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001383 const char* errMsg = nullptr;
1384 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001385 int fd;
1386
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001387 unsigned int limit = INT_MAX;
1388 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001389 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001390 }
1391 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001392 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001393 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001394
David Pursell0b156632015-10-30 11:22:01 -07001395 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001396 errMsg = "Can't determine partition type.\n";
1397 goto failed;
1398 }
JP Abgrall7e859742014-05-06 15:14:15 -07001399 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001400 if (partition_type != type_override) {
1401 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1402 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001403 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001404 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001405 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001406
David Pursell0b156632015-10-30 11:22:01 -07001407 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001408 errMsg = "Unable to get partition size\n";
1409 goto failed;
1410 }
JP Abgrall7e859742014-05-06 15:14:15 -07001411 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001412 if (partition_size != size_override) {
1413 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1414 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001415 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001416 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001417 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001418 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001419
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001420 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001421 if (!gen) {
1422 if (skip_if_not_supported) {
1423 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001424 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001425 return;
1426 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001427 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1428 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001429 return;
1430 }
1431
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001432 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001433 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001434 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1435 return;
1436 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001437
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001438 fd = make_temporary_fd();
1439 if (fd == -1) return;
Connor O'Brience16a8a2017-02-06 14:39:31 -08001440
1441 unsigned eraseBlkSize, logicalBlkSize;
1442 eraseBlkSize = fb_get_flash_block_size(transport, "erase-block-size");
1443 logicalBlkSize = fb_get_flash_block_size(transport, "logical-block-size");
1444
1445 if (fs_generator_generate(gen, fd, size, initial_dir, eraseBlkSize, logicalBlkSize)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001446 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001447 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001448 return;
1449 }
1450
Elliott Hughes53ec4952016-05-11 12:39:27 -07001451 if (!load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001452 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001453 close(fd);
1454 return;
1455 }
1456 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001457 return;
1458
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001459failed:
1460 if (skip_if_not_supported) {
1461 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001462 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001463 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001464 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001465}
1466
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001467int main(int argc, char **argv)
1468{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001469 bool wants_wipe = false;
1470 bool wants_reboot = false;
1471 bool wants_reboot_bootloader = false;
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001472 bool wants_reboot_emergency = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001473 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001474 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001475 bool skip_secondary = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001476 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001477 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001478 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001479 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001480 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001481 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001482 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001483
JP Abgrall7b8970c2013-03-07 17:06:41 -08001484 const struct option longopts[] = {
1485 {"base", required_argument, 0, 'b'},
1486 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001487 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001488 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001489 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001490 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001491 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001492 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001493 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001494 {"help", no_argument, 0, 'h'},
1495 {"unbuffered", no_argument, 0, 0},
1496 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001497 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001498 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001499 {"set-active", optional_argument, 0, 'a'},
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001500 {"skip-secondary", no_argument, 0, 0},
Mitchell Wills31dce302016-09-26 10:26:21 -07001501 {"skip-reboot", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001502#if !defined(_WIN32)
1503 {"wipe-and-use-fbe", no_argument, 0, 0},
1504#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001505 {0, 0, 0, 0}
1506 };
Colin Cross8879f982012-05-22 17:53:34 -07001507
1508 serial = getenv("ANDROID_SERIAL");
1509
1510 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001511 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 -07001512 if (c < 0) {
1513 break;
1514 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001515 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001516 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001517 case 'a':
1518 wants_set_active = true;
1519 if (optarg)
1520 next_active = optarg;
1521 break;
Colin Cross8879f982012-05-22 17:53:34 -07001522 case 'b':
1523 base_addr = strtoul(optarg, 0, 16);
1524 break;
Colin Cross8879f982012-05-22 17:53:34 -07001525 case 'c':
1526 cmdline = optarg;
1527 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001528 case 'h':
1529 usage();
1530 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001531 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001532 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001533 unsigned long val;
1534
1535 val = strtoul(optarg, &endptr, 0);
1536 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1537 die("invalid vendor id '%s'", optarg);
1538 vendor_id = (unsigned short)val;
1539 break;
1540 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001541 case 'k':
1542 kernel_offset = strtoul(optarg, 0, 16);
1543 break;
1544 case 'l':
1545 long_listing = 1;
1546 break;
1547 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001548 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001549 if (!page_size) die("invalid page size");
1550 break;
1551 case 'p':
1552 product = optarg;
1553 break;
1554 case 'r':
1555 ramdisk_offset = strtoul(optarg, 0, 16);
1556 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001557 case 't':
1558 tags_offset = strtoul(optarg, 0, 16);
1559 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001560 case 's':
1561 serial = optarg;
1562 break;
1563 case 'S':
1564 sparse_limit = parse_num(optarg);
1565 if (sparse_limit < 0) {
1566 die("invalid sparse limit");
1567 }
1568 break;
1569 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001570 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001571 break;
1572 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001573 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001574 break;
Colin Cross8879f982012-05-22 17:53:34 -07001575 case '?':
1576 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001577 case 0:
1578 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001579 setvbuf(stdout, nullptr, _IONBF, 0);
1580 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001581 } else if (strcmp("version", longopts[longindex].name) == 0) {
1582 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001583 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001584 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001585 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1586 slot_override = std::string(optarg);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001587 } else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
1588 skip_secondary = true;
Mitchell Wills31dce302016-09-26 10:26:21 -07001589 } else if (strcmp("skip-reboot", longopts[longindex].name) == 0 ) {
1590 skip_reboot = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001591#if !defined(_WIN32)
1592 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1593 wants_wipe = true;
1594 set_fbe_marker = true;
1595#endif
1596 } else {
1597 fprintf(stderr, "Internal error in options processing for %s\n",
1598 longopts[longindex].name);
1599 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001600 }
1601 break;
Colin Cross8879f982012-05-22 17:53:34 -07001602 default:
1603 abort();
1604 }
1605 }
1606
1607 argc -= optind;
1608 argv += optind;
1609
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001610 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001611 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001612 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001613 }
1614
Colin Cross8fb6e062012-07-24 16:36:41 -07001615 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001616 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001617 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001618 return 0;
1619 }
1620
Colin Crossc7b75dc2012-08-29 18:17:06 -07001621 if (argc > 0 && !strcmp(*argv, "help")) {
1622 usage();
1623 return 0;
1624 }
1625
David Pursell0b156632015-10-30 11:22:01 -07001626 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001627 if (transport == nullptr) {
1628 return 1;
1629 }
1630
Daniel Rosenberg80919472016-06-30 19:25:31 -07001631 if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
1632 fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
1633 }
1634 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1635 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001636
1637 if (wants_set_active) {
1638 if (next_active == "") {
1639 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001640 std::string current_slot;
1641 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001642 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001643 } else {
1644 wants_set_active = false;
1645 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001646 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001647 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001648 }
1649 }
1650 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001651
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001652 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001653 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001654 require(2);
1655 fb_queue_display(argv[1], argv[1]);
1656 skip(2);
1657 } else if(!strcmp(*argv, "erase")) {
1658 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001659
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001660 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001661 std::string partition_type;
1662 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001663 fs_get_generator(partition_type) != nullptr) {
1664 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1665 partition_type.c_str());
1666 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001667
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001668 fb_queue_erase(partition.c_str());
1669 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001670 do_for_partitions(transport, argv[1], slot_override, erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001671 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001672 } else if(!strncmp(*argv, "format", strlen("format"))) {
1673 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001674 char *type_override = nullptr;
1675 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001676 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001677 /*
1678 * Parsing for: "format[:[type][:[size]]]"
1679 * Some valid things:
1680 * - select ontly the size, and leave default fs type:
1681 * format::0x4000000 userdata
1682 * - default fs type and size:
1683 * format userdata
1684 * format:: userdata
1685 */
1686 overrides = strchr(*argv, ':');
1687 if (overrides) {
1688 overrides++;
1689 size_override = strchr(overrides, ':');
1690 if (size_override) {
1691 size_override[0] = '\0';
1692 size_override++;
1693 }
1694 type_override = overrides;
1695 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001696 if (type_override && !type_override[0]) type_override = nullptr;
1697 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001698
1699 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001700 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001701 fb_queue_erase(partition.c_str());
1702 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001703 fb_perform_format(transport, partition.c_str(), 0,
1704 type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001705 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001706 do_for_partitions(transport, argv[1], slot_override, format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001707 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001708 } else if(!strcmp(*argv, "signature")) {
1709 require(2);
1710 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001711 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001712 if (sz != 256) die("signature must be 256 bytes");
1713 fb_queue_download("signature", data, sz);
1714 fb_queue_command("signature", "installing signature");
1715 skip(2);
1716 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001717 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001718 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001719 if (argc > 0) {
1720 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001721 wants_reboot = false;
1722 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001723 skip(1);
Pavlin Radoslavov6adbd4e2017-01-25 19:07:46 -08001724 } else if (!strcmp(*argv, "emergency")) {
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001725 wants_reboot = false;
1726 wants_reboot_emergency = true;
1727 skip(1);
1728 }
Elliott Hughesca85df02015-02-25 10:02:00 -08001729 }
1730 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001731 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001732 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001733 skip(1);
1734 } else if (!strcmp(*argv, "continue")) {
1735 fb_queue_command("continue", "resuming boot");
1736 skip(1);
1737 } else if(!strcmp(*argv, "boot")) {
1738 char *kname = 0;
1739 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001740 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001741 skip(1);
1742 if (argc > 0) {
1743 kname = argv[0];
1744 skip(1);
1745 }
1746 if (argc > 0) {
1747 rname = argv[0];
1748 skip(1);
1749 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001750 if (argc > 0) {
1751 sname = argv[0];
1752 skip(1);
1753 }
1754 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001755 if (data == 0) return 1;
1756 fb_queue_download("boot.img", data, sz);
1757 fb_queue_command("boot", "booting");
1758 } else if(!strcmp(*argv, "flash")) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001759 char* pname = argv[1];
1760 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001761 require(2);
1762 if (argc > 2) {
1763 fname = argv[2];
1764 skip(3);
1765 } else {
1766 fname = find_item(pname, product);
1767 skip(2);
1768 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001769 if (fname.empty()) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001770
1771 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001772 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001773 fb_queue_erase(partition.c_str());
1774 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001775 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001776 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001777 do_for_partitions(transport, pname, slot_override, flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001778 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001779 char *kname = argv[2];
1780 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001781 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001782 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001783 skip(3);
1784 if (argc > 0) {
1785 rname = argv[0];
1786 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001787 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001788 if (argc > 0) {
1789 sname = argv[0];
1790 skip(1);
1791 }
1792 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001793 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001794 auto flashraw = [&](const std::string &partition) {
1795 fb_queue_flash(partition.c_str(), data, sz);
1796 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001797 do_for_partitions(transport, argv[1], slot_override, flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001798 } else if(!strcmp(*argv, "flashall")) {
1799 skip(1);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001800 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001801 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1802 do_flashall(transport, slot_override, erase_first, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001803 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001804 do_flashall(transport, slot_override, erase_first, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001805 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001806 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001807 } else if(!strcmp(*argv, "update")) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001808 bool slot_all = (slot_override == "all");
1809 if (slot_all) {
1810 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1811 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001812 if (argc > 1) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001813 do_update(transport, argv[1], slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001814 skip(2);
1815 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001816 do_update(transport, "update.zip", slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001817 skip(1);
1818 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001819 wants_reboot = true;
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001820 } else if(!strcmp(*argv, "set_active")) {
1821 require(2);
Daniel Rosenberg80919472016-06-30 19:25:31 -07001822 std::string slot = verify_slot(transport, std::string(argv[1]), false);
David Pursell04396f62016-12-15 16:16:10 -08001823 // Legacy support: verify_slot() removes leading underscores, we need to put them back
1824 // in for old bootloaders. Legacy bootloaders do not have the slot-count variable but
1825 // do have slot-suffixes.
1826 std::string var;
1827 if (!fb_getvar(transport, "slot-count", &var) &&
1828 fb_getvar(transport, "slot-suffixes", &var)) {
1829 slot = "_" + slot;
1830 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001831 fb_set_active(slot.c_str());
1832 skip(2);
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001833 } else if(!strcmp(*argv, "stage")) {
1834 require(2);
1835 std::string infile(argv[1]);
1836 skip(2);
1837 struct fastboot_buffer buf;
1838 if (!load_buf(transport, infile.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
1839 die("cannot load '%s'", infile.c_str());
1840 }
1841 fb_queue_download_fd(infile.c_str(), buf.fd, buf.sz);
Jocelyn Bohr91fefad2017-01-27 14:17:56 -08001842 } else if(!strcmp(*argv, "get_staged")) {
1843 require(2);
1844 char *outfile = argv[1];
1845 skip(2);
1846 fb_queue_upload(outfile);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001847 } else if(!strcmp(*argv, "oem")) {
1848 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001849 } else if(!strcmp(*argv, "flashing")) {
1850 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1851 !strcmp(*(argv+1), "lock") ||
1852 !strcmp(*(argv+1), "unlock_critical") ||
1853 !strcmp(*(argv+1), "lock_critical") ||
1854 !strcmp(*(argv+1), "get_unlock_ability") ||
1855 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1856 !strcmp(*(argv+1), "lock_bootloader"))) {
1857 argc = do_oem_command(argc, argv);
1858 } else
1859 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1860 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001861 } else {
1862 usage();
1863 return 1;
1864 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001865 } else {
1866 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001867 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001868 }
1869 }
1870
1871 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001872 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001873 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001874 if (set_fbe_marker) {
1875 fprintf(stderr, "setting FBE marker...\n");
1876 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1877 if (initial_userdata_dir.empty()) {
1878 return 1;
1879 }
1880 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
1881 delete_fbemarker_tmpdir(initial_userdata_dir);
1882 } else {
1883 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
1884 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001885
1886 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001887 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001888 fprintf(stderr, "wiping cache...\n");
1889 fb_queue_erase("cache");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001890 fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001891 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001892 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001893 if (wants_set_active) {
1894 fb_set_active(next_active.c_str());
1895 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001896 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001897 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001898 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001899 } else if (wants_reboot_bootloader) {
1900 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001901 fb_queue_wait_for_disconnect();
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001902 } else if (wants_reboot_emergency) {
1903 fb_queue_command("reboot-emergency", "rebooting into emergency download (EDL) mode");
1904 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001905 }
1906
David Pursell0b156632015-10-30 11:22:01 -07001907 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001908}