blob: 636092e9e7e66273ea16ab29486000f875cf1da0 [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
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070046#include <functional>
Josh Gao9da9ac52016-01-19 14:50:18 -080047#include <utility>
48#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070049
Elliott Hughes4f713192015-12-04 22:00:26 -080050#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080051#include <android-base/parsenetaddress.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/strings.h>
Colin Crossf8387882012-05-24 17:18:41 -070053#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070054#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055
Elliott Hughes253c18d2015-03-18 22:47:09 -070056#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080057#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070059#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080060#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070061#include "transport.h"
David Pursell5a0ec812016-02-05 15:35:09 -080062#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070063#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064
Colin Crossf8387882012-05-24 17:18:41 -070065#ifndef O_BINARY
66#define O_BINARY 0
67#endif
68
Rom Lemarchand622810c2013-06-28 09:54:59 -070069#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
70
Wink Savilleb98762f2011-04-04 17:54:59 -070071char cur_product[FB_RESPONSE_SZ + 1];
72
David Pursell2ec418a2016-01-20 08:32:08 -080073static const char* serial = nullptr;
74static const char* product = nullptr;
75static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070077static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070078static int64_t sparse_limit = -1;
79static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080
Elliott Hughesfc797672015-04-07 20:12:50 -070081static unsigned page_size = 2048;
82static unsigned base_addr = 0x10000000;
83static unsigned kernel_offset = 0x00008000;
84static unsigned ramdisk_offset = 0x01000000;
85static unsigned second_offset = 0x00f00000;
86static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080087
Paul Crowley8f7f56e2015-11-27 09:29:37 +000088static const std::string convert_fbe_marker_filename("convert_fbe");
89
Rom Lemarchand622810c2013-06-28 09:54:59 -070090enum fb_buffer_type {
91 FB_BUFFER,
92 FB_BUFFER_SPARSE,
93};
94
95struct fastboot_buffer {
96 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070097 void* data;
98 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070099};
100
101static struct {
102 char img_name[13];
103 char sig_name[13];
104 char part_name[9];
105 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700106} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700107 {"boot.img", "boot.sig", "boot", false},
108 {"recovery.img", "recovery.sig", "recovery", true},
109 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700110 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700111};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700112
Elliott Hughesfc797672015-04-07 20:12:50 -0700113static char* find_item(const char* item, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700115 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116 char path[PATH_MAX + 128];
117
118 if(!strcmp(item,"boot")) {
119 fn = "boot.img";
120 } else if(!strcmp(item,"recovery")) {
121 fn = "recovery.img";
122 } else if(!strcmp(item,"system")) {
123 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700124 } else if(!strcmp(item,"vendor")) {
125 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 } else if(!strcmp(item,"userdata")) {
127 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700128 } else if(!strcmp(item,"cache")) {
129 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 } else if(!strcmp(item,"info")) {
131 fn = "android-info.txt";
132 } else {
133 fprintf(stderr,"unknown partition '%s'\n", item);
134 return 0;
135 }
136
137 if(product) {
138 get_my_path(path);
139 sprintf(path + strlen(path),
140 "../../../target/product/%s/%s", product, fn);
141 return strdup(path);
142 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800143
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 dir = getenv("ANDROID_PRODUCT_OUT");
145 if((dir == 0) || (dir[0] == 0)) {
146 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
147 return 0;
148 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800149
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150 sprintf(path, "%s/%s", dir, fn);
151 return strdup(path);
152}
153
Elliott Hughesfc797672015-04-07 20:12:50 -0700154static int64_t get_file_size(int fd) {
155 struct stat sb;
156 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700157}
158
Elliott Hughesfc797672015-04-07 20:12:50 -0700159static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800160 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700161 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162
Elliott Hughesfc797672015-04-07 20:12:50 -0700163 *sz = get_file_size(fd);
164 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700165 goto oops;
166 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167
Elliott Hughesfc797672015-04-07 20:12:50 -0700168 data = (char*) malloc(*sz);
169 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170
Elliott Hughesfc797672015-04-07 20:12:50 -0700171 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 close(fd);
173
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 return data;
175
176oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800177 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178 close(fd);
179 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800180 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181 return 0;
182}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183
Elliott Hughesfc797672015-04-07 20:12:50 -0700184static void* load_file(const char* fn, int64_t* sz) {
185 int fd = open(fn, O_RDONLY | O_BINARY);
186 if (fd == -1) return nullptr;
187 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700188}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189
Elliott Hughesfc797672015-04-07 20:12:50 -0700190static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700191 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700192 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700193 return -1;
194 }
195
196 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
197 return -1;
198 }
199
Scott Anderson13081c62012-04-06 12:39:30 -0700200 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700202 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
203 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 return 0;
205}
206
Elliott Hughesfc797672015-04-07 20:12:50 -0700207static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800208 return match_fastboot_with_serial(info, serial);
209}
210
Elliott Hughesfc797672015-04-07 20:12:50 -0700211static int list_devices_callback(usb_ifc_info* info) {
212 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800213 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700214 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800215 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700216 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 if (!serial[0]) {
218 serial = "????????????";
219 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700220 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700221 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800222 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700223 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800224 printf("%-22s fastboot", serial.c_str());
225 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700226 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800227 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228 }
229
230 return -1;
231}
232
David Pursell2ec418a2016-01-20 08:32:08 -0800233// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
234// a specific device, otherwise the first USB device found will be used.
235//
236// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
237// Otherwise it blocks until the target is available.
238//
239// The returned Transport is a singleton, so multiple calls to this function will return the same
240// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700241static Transport* open_device() {
242 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800243 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244
David Pursell2ec418a2016-01-20 08:32:08 -0800245 if (transport != nullptr) {
246 return transport;
247 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800248
David Pursell5a0ec812016-02-05 15:35:09 -0800249 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800250 std::string host;
David Pursell5a0ec812016-02-05 15:35:09 -0800251 int port = 0;
252 if (serial != nullptr) {
253 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800254
David Pursell5a0ec812016-02-05 15:35:09 -0800255 if (android::base::StartsWith(serial, "tcp:")) {
256 protocol = Socket::Protocol::kTcp;
257 port = tcp::kDefaultPort;
258 net_address = serial + strlen("tcp:");
259 } else if (android::base::StartsWith(serial, "udp:")) {
260 protocol = Socket::Protocol::kUdp;
261 port = udp::kDefaultPort;
262 net_address = serial + strlen("udp:");
263 }
264
265 if (net_address != nullptr) {
266 std::string error;
267 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
268 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
269 error.c_str());
270 return nullptr;
271 }
David Pursell2ec418a2016-01-20 08:32:08 -0800272 }
273 }
274
275 while (true) {
276 if (!host.empty()) {
277 std::string error;
David Pursell5a0ec812016-02-05 15:35:09 -0800278 if (protocol == Socket::Protocol::kTcp) {
279 transport = tcp::Connect(host, port, &error).release();
280 } else if (protocol == Socket::Protocol::kUdp) {
281 transport = udp::Connect(host, port, &error).release();
282 }
283
David Pursell2ec418a2016-01-20 08:32:08 -0800284 if (transport == nullptr && announce) {
285 fprintf(stderr, "error: %s\n", error.c_str());
286 }
287 } else {
288 transport = usb_open(match_fastboot);
289 }
290
291 if (transport != nullptr) {
292 return transport;
293 }
294
David Pursell0b156632015-10-30 11:22:01 -0700295 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800296 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700297 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700299 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 }
301}
302
Elliott Hughesfc797672015-04-07 20:12:50 -0700303static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 // We don't actually open a USB device here,
305 // just getting our callback called so we can
306 // list all the connected devices.
307 usb_open(list_devices_callback);
308}
309
Elliott Hughesfc797672015-04-07 20:12:50 -0700310static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311 fprintf(stderr,
312/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
313 "usage: fastboot [ <option> ] <command>\n"
314 "\n"
315 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700316 " update <filename> Reflash device from update.zip.\n"
317 " flashall Flash boot, system, vendor, and --\n"
318 " if found -- recovery.\n"
319 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
320 " flashing lock Locks the device. Prevents flashing.\n"
321 " flashing unlock Unlocks the device. Allows flashing\n"
322 " any partition except\n"
323 " bootloader-related partitions.\n"
324 " flashing lock_critical Prevents flashing bootloader-related\n"
325 " partitions.\n"
326 " flashing unlock_critical Enables flashing bootloader-related\n"
327 " partitions.\n"
328 " flashing get_unlock_ability Queries bootloader to see if the\n"
329 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700330 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700331 " unlock nonce.\n"
332 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700333 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700334 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700335 " erase <partition> Erase a flash partition.\n"
336 " format[:[<fs type>][:[<size>]] <partition>\n"
337 " Format a flash partition. Can\n"
338 " override the fs type and/or size\n"
339 " the bootloader reports.\n"
340 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800341 " set_active <suffix> Sets the active slot. If slots are\n"
342 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700343 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
344 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
345 " Create bootimage and flash it.\n"
346 " devices [-l] List all connected devices [with\n"
347 " device paths].\n"
348 " continue Continue with autoboot.\n"
349 " reboot [bootloader] Reboot device [into bootloader].\n"
350 " reboot-bootloader Reboot device into bootloader.\n"
351 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 "\n"
353 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700354 " -w Erase userdata and cache (and format\n"
355 " if supported by partition type).\n"
356 " -u Do not erase partition before\n"
357 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800358 " -s <specific device> Specify a device. For USB, provide either\n"
359 " a serial number or path to device port.\n"
David Pursell5a0ec812016-02-05 15:35:09 -0800360 " For ethernet, provide an address in the"
361 " form <protocol>:<hostname>[:port] where"
362 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700363 " -p <product> Specify product name.\n"
364 " -c <cmdline> Override kernel commandline.\n"
365 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800366 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700367 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800368 " --kernel-offset Specify a custom kernel offset.\n"
369 " (default: 0x00008000)\n"
370 " --ramdisk-offset Specify a custom ramdisk offset.\n"
371 " (default: 0x01000000)\n"
372 " --tags-offset Specify a custom tags offset.\n"
373 " (default: 0x00000100)\n"
374 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700375 " (default: 2048).\n"
376 " -S <size>[K|M|G] Automatically sparse files greater\n"
377 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700378 " --slot <suffix> Specify slot suffix to be used if the\n"
379 " device supports slots. This will be\n"
380 " added to all partition names that use\n"
381 " slots. 'all' can be given to refer\n"
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800382 " to all slots. 'other' can be given to\n"
383 " refer to a non-current slot. If this\n"
384 " flag is not used, slotted partitions\n"
385 " will default to the current active slot.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800386 " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800387 " provided, this will default to the value\n"
388 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800389 " supported, this does nothing. This will\n"
390 " run after all non-reboot commands.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000391#if !defined(_WIN32)
392 " --wipe-and-use-fbe On devices which support it,\n"
393 " erase userdata and cache, and\n"
394 " enable file-based encryption\n"
395#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800396 " --unbuffered Do not buffer input or output.\n"
397 " --version Display version.\n"
398 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000401
Elliott Hughesfc797672015-04-07 20:12:50 -0700402static void* load_bootable_image(const char* kernel, const char* ramdisk,
403 const char* secondstage, int64_t* sz,
404 const char* cmdline) {
405 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406 fprintf(stderr, "no image specified\n");
407 return 0;
408 }
409
Elliott Hughesfc797672015-04-07 20:12:50 -0700410 int64_t ksize;
411 void* kdata = load_file(kernel, &ksize);
412 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800413 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414 return 0;
415 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800416
Elliott Hughesfc797672015-04-07 20:12:50 -0700417 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700419 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800420
Elliott Hughesfc797672015-04-07 20:12:50 -0700421 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
423 return 0;
424 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800425
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 *sz = ksize;
427 return kdata;
428 }
429
Elliott Hughesfc797672015-04-07 20:12:50 -0700430 void* rdata = nullptr;
431 int64_t rsize = 0;
432 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700434 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800435 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436 return 0;
437 }
438 }
439
Elliott Hughesfc797672015-04-07 20:12:50 -0700440 void* sdata = nullptr;
441 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200442 if (secondstage) {
443 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700444 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200445 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
446 return 0;
447 }
448 }
449
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700451 int64_t bsize = 0;
452 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800453 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200454 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800455 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700456 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457 fprintf(stderr,"failed to create boot.img\n");
458 return 0;
459 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700460 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
461 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800463
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464 return bdata;
465}
466
Elliott Hughesfc797672015-04-07 20:12:50 -0700467static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468{
Yusuke Sato07447542015-06-25 14:39:19 -0700469 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700470 ZipEntry zip_entry;
471 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700472 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000473 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 }
475
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700476 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700478 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700479 if (data == nullptr) {
480 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000481 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482 }
483
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700484 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
485 if (error != 0) {
486 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000488 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800489 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000490
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491 return data;
492}
493
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700494#if defined(_WIN32)
495
496// TODO: move this to somewhere it can be shared.
497
498#include <windows.h>
499
500// Windows' tmpfile(3) requires administrator rights because
501// it creates temporary files in the root directory.
502static FILE* win32_tmpfile() {
503 char temp_path[PATH_MAX];
504 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
505 if (nchars == 0 || nchars >= sizeof(temp_path)) {
506 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
507 return nullptr;
508 }
509
510 char filename[PATH_MAX];
511 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
512 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
513 return nullptr;
514 }
515
516 return fopen(filename, "w+bTD");
517}
518
519#define tmpfile win32_tmpfile
520
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000521static std::string make_temporary_directory() {
522 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
523 return "";
524}
525
526#else
527
528static std::string make_temporary_directory() {
529 const char *tmpdir = getenv("TMPDIR");
530 if (tmpdir == nullptr) {
531 tmpdir = P_tmpdir;
532 }
533 std::string result = std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
534 if (mkdtemp(&result[0]) == NULL) {
535 fprintf(stderr, "Unable to create temporary directory: %s\n",
536 strerror(errno));
537 return "";
538 }
539 return result;
540}
541
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700542#endif
543
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000544static std::string create_fbemarker_tmpdir() {
545 std::string dir = make_temporary_directory();
546 if (dir.empty()) {
547 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
548 return "";
549 }
550 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
551 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
552 if (fd == -1) {
553 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
554 marker_file.c_str(), errno, strerror(errno));
555 return "";
556 }
557 close(fd);
558 return dir;
559}
560
561static void delete_fbemarker_tmpdir(const std::string& dir) {
562 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
563 if (unlink(marker_file.c_str()) == -1) {
564 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
565 marker_file.c_str(), errno, strerror(errno));
566 return;
567 }
568 if (rmdir(dir.c_str()) == -1) {
569 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
570 dir.c_str(), errno, strerror(errno));
571 return;
572 }
573}
574
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700575static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
576 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700577 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700578 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
579 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700580 return -1;
581 }
582
Yusuke Sato07447542015-06-25 14:39:19 -0700583 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700584 ZipEntry zip_entry;
585 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
586 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000587 return -1;
588 }
589
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700590 int fd = fileno(fp);
591 int error = ExtractEntryToFile(zip, &zip_entry, fd);
592 if (error != 0) {
593 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
594 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700595 }
596
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000597 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700598 return fd;
599}
600
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800601static char *strip(char *s)
602{
603 int n;
604 while(*s && isspace(*s)) s++;
605 n = strlen(s);
606 while(n-- > 0) {
607 if(!isspace(s[n])) break;
608 s[n] = 0;
609 }
610 return s;
611}
612
613#define MAX_OPTIONS 32
614static int setup_requirement_line(char *name)
615{
616 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700617 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800618 unsigned n, count;
619 char *x;
620 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800621
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800622 if (!strncmp(name, "reject ", 7)) {
623 name += 7;
624 invert = 1;
625 } else if (!strncmp(name, "require ", 8)) {
626 name += 8;
627 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700628 } else if (!strncmp(name, "require-for-product:", 20)) {
629 // Get the product and point name past it
630 prod = name + 20;
631 name = strchr(name, ' ');
632 if (!name) return -1;
633 *name = 0;
634 name += 1;
635 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800636 }
637
638 x = strchr(name, '=');
639 if (x == 0) return 0;
640 *x = 0;
641 val[0] = x + 1;
642
643 for(count = 1; count < MAX_OPTIONS; count++) {
644 x = strchr(val[count - 1],'|');
645 if (x == 0) break;
646 *x = 0;
647 val[count] = x + 1;
648 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800649
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650 name = strip(name);
651 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800652
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800653 name = strip(name);
654 if (name == 0) return -1;
655
Elliott Hughes253c18d2015-03-18 22:47:09 -0700656 const char* var = name;
657 // Work around an unfortunate name mismatch.
658 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800659
Elliott Hughes253c18d2015-03-18 22:47:09 -0700660 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661 if (out == 0) return -1;
662
663 for(n = 0; n < count; n++) {
664 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700665 if (out[n] == 0) {
666 for(size_t i = 0; i < n; ++i) {
667 free((char*) out[i]);
668 }
669 free(out);
670 return -1;
671 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800672 }
673
Elliott Hughes253c18d2015-03-18 22:47:09 -0700674 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800675 return 0;
676}
677
Elliott Hughesfc797672015-04-07 20:12:50 -0700678static void setup_requirements(char* data, int64_t sz) {
679 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800680 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700681 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800682 *s++ = 0;
683 if (setup_requirement_line(data)) {
684 die("out of memory");
685 }
686 data = s;
687 } else {
688 s++;
689 }
690 }
691}
692
Elliott Hughesfc797672015-04-07 20:12:50 -0700693static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694 fb_queue_notice("--------------------------------------------");
695 fb_queue_display("version-bootloader", "Bootloader Version...");
696 fb_queue_display("version-baseband", "Baseband Version.....");
697 fb_queue_display("serialno", "Serial Number........");
698 fb_queue_notice("--------------------------------------------");
699}
700
Rom Lemarchand622810c2013-06-28 09:54:59 -0700701static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700702{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700703 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700704 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700705 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700706 }
707
Elliott Hughesfc797672015-04-07 20:12:50 -0700708 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700709 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700710 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700711 }
712
Elliott Hughes253c18d2015-03-18 22:47:09 -0700713 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700714 if (!out_s) {
715 die("Failed to allocate sparse file array\n");
716 }
717
718 files = sparse_file_resparse(s, max_size, out_s, files);
719 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700720 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700721 }
722
723 return out_s;
724}
725
David Pursell0b156632015-10-30 11:22:01 -0700726static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700727 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700728 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
729 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800730 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700731 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700732 }
733
Elliott Hughes77c0e662015-11-02 15:51:12 -0800734 // Some bootloaders (angler, for example) send spurious whitespace too.
735 max_download_size = android::base::Trim(max_download_size);
736
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700737 uint64_t limit;
738 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800739 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700740 return 0;
741 }
742 if (limit > 0) {
743 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
744 }
Colin Crossf8387882012-05-24 17:18:41 -0700745 return limit;
746}
747
David Pursell0b156632015-10-30 11:22:01 -0700748static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700749 int64_t limit;
750
751 if (sparse_limit == 0) {
752 return 0;
753 } else if (sparse_limit > 0) {
754 limit = sparse_limit;
755 } else {
756 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700757 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700758 }
759 if (target_sparse_limit > 0) {
760 limit = target_sparse_limit;
761 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700762 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700763 }
764 }
765
766 if (size > limit) {
767 return limit;
768 }
769
770 return 0;
771}
772
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700773// Until we get lazy inode table init working in make_ext4fs, we need to
774// erase partitions of type ext4 before flashing a filesystem so no stale
775// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700776static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800777 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700778 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800779 return false;
780 }
781 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700782}
783
David Pursell0b156632015-10-30 11:22:01 -0700784static int load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700785 int64_t sz = get_file_size(fd);
786 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000787 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700788 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700789
Elliott Hughesfc797672015-04-07 20:12:50 -0700790 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700791 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700792 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700793 sparse_file** s = load_sparse_files(fd, limit);
794 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700795 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700796 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700797 buf->type = FB_BUFFER_SPARSE;
798 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700799 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700800 void* data = load_fd(fd, &sz);
801 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700802 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700803 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000804 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700805 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700806
807 return 0;
808}
809
David Pursell0b156632015-10-30 11:22:01 -0700810static int load_buf(Transport* transport, const char *fname, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700811{
812 int fd;
813
814 fd = open(fname, O_RDONLY | O_BINARY);
815 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700816 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700817 }
818
David Pursell0b156632015-10-30 11:22:01 -0700819 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700820}
821
822static void flash_buf(const char *pname, struct fastboot_buffer *buf)
823{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700824 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700825
826 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800827 case FB_BUFFER_SPARSE: {
828 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700829 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700830 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700831 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800832 sparse_files.emplace_back(*s, sz);
833 ++s;
834 }
835
836 for (size_t i = 0; i < sparse_files.size(); ++i) {
837 const auto& pair = sparse_files[i];
838 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700839 }
840 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800841 }
842
Rom Lemarchand622810c2013-06-28 09:54:59 -0700843 case FB_BUFFER:
844 fb_queue_flash(pname, buf->data, buf->sz);
845 break;
846 default:
847 die("unknown buffer type: %d", buf->type);
848 }
849}
850
David Pursell0b156632015-10-30 11:22:01 -0700851static std::vector<std::string> get_suffixes(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700852 std::vector<std::string> suffixes;
853 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700854 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700855 die("Could not get suffixes.\n");
856 }
857 return android::base::Split(suffix_list, ",");
858}
859
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800860static std::string verify_slot(Transport* transport, const char *slot, bool allow_all) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700861 if (strcmp(slot, "all") == 0) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800862 if (allow_all) {
863 return "all";
864 } else {
865 std::vector<std::string> suffixes = get_suffixes(transport);
866 if (!suffixes.empty()) {
867 return suffixes[0];
868 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800869 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800870 }
871 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700872 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800873
David Pursell0b156632015-10-30 11:22:01 -0700874 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800875
876 if (strcmp(slot, "other") == 0) {
877 std::string current_slot;
878 if (!fb_getvar(transport, "current-slot", &current_slot)) {
879 die("Failed to identify current slot.");
880 }
881 if (!suffixes.empty()) {
882 for (size_t i = 0; i < suffixes.size(); i++) {
883 if (current_slot == suffixes[i])
884 return suffixes[(i+1)%suffixes.size()];
885 }
886 } else {
887 die("No known slots.");
888 }
889 }
890
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700891 for (const std::string &suffix : suffixes) {
892 if (suffix == slot)
893 return slot;
894 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800895 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700896 for (const std::string &suffix : suffixes) {
897 fprintf(stderr, "%s\n", suffix.c_str());
898 }
899 exit(1);
900}
901
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800902static std::string verify_slot(Transport* transport, const char *slot) {
903 return verify_slot(transport, slot, true);
904}
905
David Pursell0b156632015-10-30 11:22:01 -0700906static void do_for_partition(Transport* transport, const char *part, const char *slot,
907 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800908 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700909 std::string current_slot;
910
David Pursell0b156632015-10-30 11:22:01 -0700911 if (!fb_getvar(transport, std::string("has-slot:")+part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800912 /* If has-slot is not supported, the answer is no. */
913 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700914 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800915 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700916 if (!slot || slot[0] == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700917 if (!fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700918 die("Failed to identify current slot.\n");
919 }
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800920 func(std::string(part) + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700921 } else {
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800922 func(std::string(part) + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700923 }
924 } else {
925 if (force_slot && slot && slot[0]) {
David Pursell0b156632015-10-30 11:22:01 -0700926 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
927 part, slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700928 }
929 func(part);
930 }
931}
932
David Pursell0b156632015-10-30 11:22:01 -0700933/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
934 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
935 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
936 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700937 */
David Pursell0b156632015-10-30 11:22:01 -0700938static void do_for_partitions(Transport* transport, const char *part, const char *slot,
939 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800940 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700941
942 if (slot && strcmp(slot, "all") == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700943 if (!fb_getvar(transport, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700944 die("Could not check if partition %s has slot.", part);
945 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800946 if (has_slot == "yes") {
David Pursell0b156632015-10-30 11:22:01 -0700947 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700948 for (std::string &suffix : suffixes) {
David Pursell0b156632015-10-30 11:22:01 -0700949 do_for_partition(transport, part, suffix.c_str(), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700950 }
951 } else {
David Pursell0b156632015-10-30 11:22:01 -0700952 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700953 }
954 } else {
David Pursell0b156632015-10-30 11:22:01 -0700955 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700956 }
957}
958
David Pursell0b156632015-10-30 11:22:01 -0700959static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700960 struct fastboot_buffer buf;
961
David Pursell0b156632015-10-30 11:22:01 -0700962 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700963 die("cannot load '%s'", fname);
964 }
965 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700966}
967
Elliott Hughesfc797672015-04-07 20:12:50 -0700968static void do_update_signature(ZipArchiveHandle zip, char* fn) {
969 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700970 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700971 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800972 fb_queue_download("signature", data, sz);
973 fb_queue_command("signature", "installing signature");
974}
975
David Pursell0b156632015-10-30 11:22:01 -0700976static void do_update(Transport* transport, const char* filename, const char* slot_override, bool erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800977 queue_info_dump();
978
Wink Savilleb98762f2011-04-04 17:54:59 -0700979 fb_queue_query_save("product", cur_product, sizeof(cur_product));
980
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700981 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700982 int error = OpenArchive(filename, &zip);
983 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100984 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700985 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700986 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800987
Elliott Hughesfc797672015-04-07 20:12:50 -0700988 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700989 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700990 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100991 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700992 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800993 }
994
Elliott Hughes253c18d2015-03-18 22:47:09 -0700995 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800996
Elliott Hughesacdbe922015-06-02 21:37:05 -0700997 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700998 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700999 if (fd == -1) {
1000 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001001 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001002 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001003 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001004 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001005 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001006 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -07001007 int rc = load_buf_fd(transport, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001008 if (rc) die("cannot load %s from flash", images[i].img_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001009
1010 auto update = [&](const std::string &partition) {
1011 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001012 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001013 fb_queue_erase(partition.c_str());
1014 }
1015 flash_buf(partition.c_str(), &buf);
1016 /* not closing the fd here since the sparse code keeps the fd around
1017 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
1018 * program exits.
1019 */
1020 };
David Pursell0b156632015-10-30 11:22:01 -07001021 do_for_partitions(transport, images[i].part_name, slot_override, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001022 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001023
1024 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001025}
1026
Elliott Hughesfc797672015-04-07 20:12:50 -07001027static void do_send_signature(char* fn) {
1028 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001029 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001030
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001031 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001032
Elliott Hughesfc797672015-04-07 20:12:50 -07001033 strcpy(xtn, ".sig");
1034
1035 int64_t sz;
1036 void* data = load_file(fn, &sz);
1037 strcpy(xtn, ".img");
1038 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001039 fb_queue_download("signature", data, sz);
1040 fb_queue_command("signature", "installing signature");
1041}
1042
David Pursell0b156632015-10-30 11:22:01 -07001043static void do_flashall(Transport* transport, const char* slot_override, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001044 queue_info_dump();
1045
Wink Savilleb98762f2011-04-04 17:54:59 -07001046 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1047
Elliott Hughes253c18d2015-03-18 22:47:09 -07001048 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -07001049 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001050
Elliott Hughesfc797672015-04-07 20:12:50 -07001051 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001052 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001053 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001054
1055 setup_requirements(reinterpret_cast<char*>(data), sz);
1056
1057 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001058 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001059 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -07001060 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001061 if (images[i].is_optional)
1062 continue;
1063 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001064 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001065
1066 auto flashall = [&](const std::string &partition) {
1067 do_send_signature(fname);
David Pursell0b156632015-10-30 11:22:01 -07001068 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001069 fb_queue_erase(partition.c_str());
1070 }
1071 flash_buf(partition.c_str(), &buf);
1072 };
David Pursell0b156632015-10-30 11:22:01 -07001073 do_for_partitions(transport, images[i].part_name, slot_override, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001074 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001075}
1076
1077#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001078#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001079
Elliott Hughes45be42e2015-09-02 20:15:48 -07001080static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001081{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001082 if (argc <= 2) return 0;
1083 skip(2);
1084
1085 /*
1086 * Process unlock_bootloader, we have to load the message file
1087 * and send that to the remote device.
1088 */
1089 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001090
1091 int64_t sz;
1092 void* data = load_file(*argv, &sz);
1093 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001094 fb_queue_download("unlock_message", data, sz);
1095 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1096 skip(1);
1097 return 0;
1098}
1099
Elliott Hughes45be42e2015-09-02 20:15:48 -07001100static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001101{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001102 char command[256];
1103 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001104
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001105 command[0] = 0;
1106 while(1) {
1107 strcat(command,*argv);
1108 skip(1);
1109 if(argc == 0) break;
1110 strcat(command," ");
1111 }
1112
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001113 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001114 return 0;
1115}
1116
Colin Crossf8387882012-05-24 17:18:41 -07001117static int64_t parse_num(const char *arg)
1118{
1119 char *endptr;
1120 unsigned long long num;
1121
1122 num = strtoull(arg, &endptr, 0);
1123 if (endptr == arg) {
1124 return -1;
1125 }
1126
1127 if (*endptr == 'k' || *endptr == 'K') {
1128 if (num >= (-1ULL) / 1024) {
1129 return -1;
1130 }
1131 num *= 1024LL;
1132 endptr++;
1133 } else if (*endptr == 'm' || *endptr == 'M') {
1134 if (num >= (-1ULL) / (1024 * 1024)) {
1135 return -1;
1136 }
1137 num *= 1024LL * 1024LL;
1138 endptr++;
1139 } else if (*endptr == 'g' || *endptr == 'G') {
1140 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1141 return -1;
1142 }
1143 num *= 1024LL * 1024LL * 1024LL;
1144 endptr++;
1145 }
1146
1147 if (*endptr != '\0') {
1148 return -1;
1149 }
1150
1151 if (num > INT64_MAX) {
1152 return -1;
1153 }
1154
1155 return num;
1156}
1157
David Pursell0b156632015-10-30 11:22:01 -07001158static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001159 const char* partition, int skip_if_not_supported,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001160 const char* type_override, const char* size_override,
1161 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001162 std::string partition_type, partition_size;
1163
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001164 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001165 const char* errMsg = nullptr;
1166 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001167 int fd;
1168
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001169 unsigned int limit = INT_MAX;
1170 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001171 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001172 }
1173 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001174 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001175 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001176
David Pursell0b156632015-10-30 11:22:01 -07001177 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001178 errMsg = "Can't determine partition type.\n";
1179 goto failed;
1180 }
JP Abgrall7e859742014-05-06 15:14:15 -07001181 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001182 if (partition_type != type_override) {
1183 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1184 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001185 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001186 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001187 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001188
David Pursell0b156632015-10-30 11:22:01 -07001189 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001190 errMsg = "Unable to get partition size\n";
1191 goto failed;
1192 }
JP Abgrall7e859742014-05-06 15:14:15 -07001193 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001194 if (partition_size != size_override) {
1195 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1196 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001197 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001198 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001199 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001200 // Some bootloaders (angler, for example), send spurious leading whitespace.
1201 partition_size = android::base::Trim(partition_size);
1202 // Some bootloaders (hammerhead, for example) use implicit hex.
1203 // This code used to use strtol with base 16.
1204 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001205
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001206 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001207 if (!gen) {
1208 if (skip_if_not_supported) {
1209 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001210 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001211 return;
1212 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001213 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1214 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001215 return;
1216 }
1217
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001218 int64_t size;
1219 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1220 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1221 return;
1222 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001223
1224 fd = fileno(tmpfile());
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001225 if (fs_generator_generate(gen, fd, size, initial_dir)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001226 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001227 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001228 return;
1229 }
1230
David Pursell0b156632015-10-30 11:22:01 -07001231 if (load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001232 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001233 close(fd);
1234 return;
1235 }
1236 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001237 return;
1238
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001239failed:
1240 if (skip_if_not_supported) {
1241 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001242 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001243 }
1244 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
1245}
1246
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001247int main(int argc, char **argv)
1248{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001249 bool wants_wipe = false;
1250 bool wants_reboot = false;
1251 bool wants_reboot_bootloader = false;
1252 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001253 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001254 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001255 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001256 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001257 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001258 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001259 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001260
JP Abgrall7b8970c2013-03-07 17:06:41 -08001261 const struct option longopts[] = {
1262 {"base", required_argument, 0, 'b'},
1263 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001264 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001265 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001266 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001267 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001268 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001269 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001270 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001271 {"help", no_argument, 0, 'h'},
1272 {"unbuffered", no_argument, 0, 0},
1273 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001274 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001275 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001276 {"set-active", optional_argument, 0, 'a'},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001277#if !defined(_WIN32)
1278 {"wipe-and-use-fbe", no_argument, 0, 0},
1279#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001280 {0, 0, 0, 0}
1281 };
Colin Cross8879f982012-05-22 17:53:34 -07001282
1283 serial = getenv("ANDROID_SERIAL");
1284
1285 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001286 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 -07001287 if (c < 0) {
1288 break;
1289 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001290 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001291 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001292 case 'a':
1293 wants_set_active = true;
1294 if (optarg)
1295 next_active = optarg;
1296 break;
Colin Cross8879f982012-05-22 17:53:34 -07001297 case 'b':
1298 base_addr = strtoul(optarg, 0, 16);
1299 break;
Colin Cross8879f982012-05-22 17:53:34 -07001300 case 'c':
1301 cmdline = optarg;
1302 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001303 case 'h':
1304 usage();
1305 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001306 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001307 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001308 unsigned long val;
1309
1310 val = strtoul(optarg, &endptr, 0);
1311 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1312 die("invalid vendor id '%s'", optarg);
1313 vendor_id = (unsigned short)val;
1314 break;
1315 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001316 case 'k':
1317 kernel_offset = strtoul(optarg, 0, 16);
1318 break;
1319 case 'l':
1320 long_listing = 1;
1321 break;
1322 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001323 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001324 if (!page_size) die("invalid page size");
1325 break;
1326 case 'p':
1327 product = optarg;
1328 break;
1329 case 'r':
1330 ramdisk_offset = strtoul(optarg, 0, 16);
1331 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001332 case 't':
1333 tags_offset = strtoul(optarg, 0, 16);
1334 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001335 case 's':
1336 serial = optarg;
1337 break;
1338 case 'S':
1339 sparse_limit = parse_num(optarg);
1340 if (sparse_limit < 0) {
1341 die("invalid sparse limit");
1342 }
1343 break;
1344 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001345 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001346 break;
1347 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001348 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001349 break;
Colin Cross8879f982012-05-22 17:53:34 -07001350 case '?':
1351 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001352 case 0:
1353 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001354 setvbuf(stdout, nullptr, _IONBF, 0);
1355 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001356 } else if (strcmp("version", longopts[longindex].name) == 0) {
1357 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1358 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001359 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1360 slot_override = std::string(optarg);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001361#if !defined(_WIN32)
1362 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1363 wants_wipe = true;
1364 set_fbe_marker = true;
1365#endif
1366 } else {
1367 fprintf(stderr, "Internal error in options processing for %s\n",
1368 longopts[longindex].name);
1369 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001370 }
1371 break;
Colin Cross8879f982012-05-22 17:53:34 -07001372 default:
1373 abort();
1374 }
1375 }
1376
1377 argc -= optind;
1378 argv += optind;
1379
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001380 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001381 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001382 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001383 }
1384
Colin Cross8fb6e062012-07-24 16:36:41 -07001385 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001386 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001387 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001388 return 0;
1389 }
1390
Colin Crossc7b75dc2012-08-29 18:17:06 -07001391 if (argc > 0 && !strcmp(*argv, "help")) {
1392 usage();
1393 return 0;
1394 }
1395
David Pursell0b156632015-10-30 11:22:01 -07001396 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001397 if (transport == nullptr) {
1398 return 1;
1399 }
1400
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001401 if (slot_override != "")
David Pursell0b156632015-10-30 11:22:01 -07001402 slot_override = verify_slot(transport, slot_override.c_str());
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001403 if (next_active != "")
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001404 next_active = verify_slot(transport, next_active.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001405
1406 if (wants_set_active) {
1407 if (next_active == "") {
1408 if (slot_override == "") {
1409 wants_set_active = false;
1410 } else {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001411 next_active = verify_slot(transport, slot_override.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001412 }
1413 }
1414 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001415
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001416 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001417 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001418 require(2);
1419 fb_queue_display(argv[1], argv[1]);
1420 skip(2);
1421 } else if(!strcmp(*argv, "erase")) {
1422 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001423
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001424 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001425 std::string partition_type;
1426 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001427 fs_get_generator(partition_type) != nullptr) {
1428 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1429 partition_type.c_str());
1430 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001431
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001432 fb_queue_erase(partition.c_str());
1433 };
David Pursell0b156632015-10-30 11:22:01 -07001434 do_for_partitions(transport, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001435 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001436 } else if(!strncmp(*argv, "format", strlen("format"))) {
1437 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001438 char *type_override = nullptr;
1439 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001440 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001441 /*
1442 * Parsing for: "format[:[type][:[size]]]"
1443 * Some valid things:
1444 * - select ontly the size, and leave default fs type:
1445 * format::0x4000000 userdata
1446 * - default fs type and size:
1447 * format userdata
1448 * format:: userdata
1449 */
1450 overrides = strchr(*argv, ':');
1451 if (overrides) {
1452 overrides++;
1453 size_override = strchr(overrides, ':');
1454 if (size_override) {
1455 size_override[0] = '\0';
1456 size_override++;
1457 }
1458 type_override = overrides;
1459 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001460 if (type_override && !type_override[0]) type_override = nullptr;
1461 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001462
1463 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001464 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001465 fb_queue_erase(partition.c_str());
1466 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001467 fb_perform_format(transport, partition.c_str(), 0,
1468 type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001469 };
David Pursell0b156632015-10-30 11:22:01 -07001470 do_for_partitions(transport, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001471 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001472 } else if(!strcmp(*argv, "signature")) {
1473 require(2);
1474 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001475 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001476 if (sz != 256) die("signature must be 256 bytes");
1477 fb_queue_download("signature", data, sz);
1478 fb_queue_command("signature", "installing signature");
1479 skip(2);
1480 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001481 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001482 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001483 if (argc > 0) {
1484 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001485 wants_reboot = false;
1486 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001487 skip(1);
1488 }
1489 }
1490 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001491 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001492 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001493 skip(1);
1494 } else if (!strcmp(*argv, "continue")) {
1495 fb_queue_command("continue", "resuming boot");
1496 skip(1);
1497 } else if(!strcmp(*argv, "boot")) {
1498 char *kname = 0;
1499 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001500 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001501 skip(1);
1502 if (argc > 0) {
1503 kname = argv[0];
1504 skip(1);
1505 }
1506 if (argc > 0) {
1507 rname = argv[0];
1508 skip(1);
1509 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001510 if (argc > 0) {
1511 sname = argv[0];
1512 skip(1);
1513 }
1514 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001515 if (data == 0) return 1;
1516 fb_queue_download("boot.img", data, sz);
1517 fb_queue_command("boot", "booting");
1518 } else if(!strcmp(*argv, "flash")) {
1519 char *pname = argv[1];
1520 char *fname = 0;
1521 require(2);
1522 if (argc > 2) {
1523 fname = argv[2];
1524 skip(3);
1525 } else {
1526 fname = find_item(pname, product);
1527 skip(2);
1528 }
1529 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001530
1531 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001532 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001533 fb_queue_erase(partition.c_str());
1534 }
David Pursell0b156632015-10-30 11:22:01 -07001535 do_flash(transport, partition.c_str(), fname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001536 };
David Pursell0b156632015-10-30 11:22:01 -07001537 do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001538 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001539 char *kname = argv[2];
1540 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001541 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001542 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001543 skip(3);
1544 if (argc > 0) {
1545 rname = argv[0];
1546 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001547 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001548 if (argc > 0) {
1549 sname = argv[0];
1550 skip(1);
1551 }
1552 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001553 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001554 auto flashraw = [&](const std::string &partition) {
1555 fb_queue_flash(partition.c_str(), data, sz);
1556 };
David Pursell0b156632015-10-30 11:22:01 -07001557 do_for_partitions(transport, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001558 } else if(!strcmp(*argv, "flashall")) {
1559 skip(1);
David Pursell0b156632015-10-30 11:22:01 -07001560 do_flashall(transport, slot_override.c_str(), erase_first);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001561 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001562 } else if(!strcmp(*argv, "update")) {
1563 if (argc > 1) {
David Pursell0b156632015-10-30 11:22:01 -07001564 do_update(transport, argv[1], slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001565 skip(2);
1566 } else {
David Pursell0b156632015-10-30 11:22:01 -07001567 do_update(transport, "update.zip", slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001568 skip(1);
1569 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001570 wants_reboot = 1;
1571 } else if(!strcmp(*argv, "set_active")) {
1572 require(2);
1573 std::string slot = verify_slot(transport, argv[1], false);
1574 fb_set_active(slot.c_str());
1575 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001576 } else if(!strcmp(*argv, "oem")) {
1577 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001578 } else if(!strcmp(*argv, "flashing")) {
1579 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1580 !strcmp(*(argv+1), "lock") ||
1581 !strcmp(*(argv+1), "unlock_critical") ||
1582 !strcmp(*(argv+1), "lock_critical") ||
1583 !strcmp(*(argv+1), "get_unlock_ability") ||
1584 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1585 !strcmp(*(argv+1), "lock_bootloader"))) {
1586 argc = do_oem_command(argc, argv);
1587 } else
1588 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1589 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001590 } else {
1591 usage();
1592 return 1;
1593 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001594 } else {
1595 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001596 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001597 }
1598 }
1599
1600 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001601 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001602 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001603 if (set_fbe_marker) {
1604 fprintf(stderr, "setting FBE marker...\n");
1605 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1606 if (initial_userdata_dir.empty()) {
1607 return 1;
1608 }
1609 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
1610 delete_fbemarker_tmpdir(initial_userdata_dir);
1611 } else {
1612 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
1613 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001614
1615 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001616 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001617 fprintf(stderr, "wiping cache...\n");
1618 fb_queue_erase("cache");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001619 fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001620 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001621 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001622 if (wants_set_active) {
1623 fb_set_active(next_active.c_str());
1624 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001625 if (wants_reboot) {
1626 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001627 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001628 } else if (wants_reboot_bootloader) {
1629 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001630 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001631 }
1632
David Pursell0b156632015-10-30 11:22:01 -07001633 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001634}