blob: e5b54d09d421f13b19a8949c4abad3436a8dd6fb [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 Hughes2810d002016-04-25 14:31:18 -070052#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080053#include <android-base/strings.h>
Colin Crossf8387882012-05-24 17:18:41 -070054#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070055#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056
Elliott Hughes253c18d2015-03-18 22:47:09 -070057#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080058#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070060#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080061#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070062#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080063#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070064#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
Colin Crossf8387882012-05-24 17:18:41 -070066#ifndef O_BINARY
67#define O_BINARY 0
68#endif
69
Rom Lemarchand622810c2013-06-28 09:54:59 -070070#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
71
Wink Savilleb98762f2011-04-04 17:54:59 -070072char cur_product[FB_RESPONSE_SZ + 1];
73
David Pursell2ec418a2016-01-20 08:32:08 -080074static const char* serial = nullptr;
75static const char* product = nullptr;
76static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070078static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070079static int64_t sparse_limit = -1;
80static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081
Elliott Hughesfc797672015-04-07 20:12:50 -070082static unsigned page_size = 2048;
83static unsigned base_addr = 0x10000000;
84static unsigned kernel_offset = 0x00008000;
85static unsigned ramdisk_offset = 0x01000000;
86static unsigned second_offset = 0x00f00000;
87static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080088
Paul Crowley8f7f56e2015-11-27 09:29:37 +000089static const std::string convert_fbe_marker_filename("convert_fbe");
90
Rom Lemarchand622810c2013-06-28 09:54:59 -070091enum fb_buffer_type {
92 FB_BUFFER,
93 FB_BUFFER_SPARSE,
94};
95
96struct fastboot_buffer {
97 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070098 void* data;
99 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700100};
101
102static struct {
103 char img_name[13];
104 char sig_name[13];
105 char part_name[9];
106 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700107} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700108 {"boot.img", "boot.sig", "boot", false},
109 {"recovery.img", "recovery.sig", "recovery", true},
110 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700111 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700112};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700113
Elliott Hughes2810d002016-04-25 14:31:18 -0700114static std::string find_item(const char* item, const char* product) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700115 const char *fn;
Elliott Hughes2810d002016-04-25 14:31:18 -0700116 if (!strcmp(item,"boot")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117 fn = "boot.img";
118 } else if(!strcmp(item,"recovery")) {
119 fn = "recovery.img";
120 } else if(!strcmp(item,"system")) {
121 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700122 } else if(!strcmp(item,"vendor")) {
123 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124 } else if(!strcmp(item,"userdata")) {
125 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700126 } else if(!strcmp(item,"cache")) {
127 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128 } else if(!strcmp(item,"info")) {
129 fn = "android-info.txt";
130 } else {
131 fprintf(stderr,"unknown partition '%s'\n", item);
Elliott Hughes2810d002016-04-25 14:31:18 -0700132 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133 }
134
Elliott Hughes2810d002016-04-25 14:31:18 -0700135 if (product) {
136 std::string path = get_my_path();
137 path.erase(path.find_last_of('/'));
138 return android::base::StringPrintf("%s/../../../target/product/%s/%s",
139 path.c_str(), product, fn);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800141
Elliott Hughes2810d002016-04-25 14:31:18 -0700142 char* dir = getenv("ANDROID_PRODUCT_OUT");
143 if (dir == nullptr || dir[0] == '\0') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800146
Elliott Hughes2810d002016-04-25 14:31:18 -0700147 return android::base::StringPrintf("%s/%s", dir, fn);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148}
149
Elliott Hughesfc797672015-04-07 20:12:50 -0700150static int64_t get_file_size(int fd) {
151 struct stat sb;
152 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700153}
154
Elliott Hughesfc797672015-04-07 20:12:50 -0700155static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800156 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700157 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158
Elliott Hughesfc797672015-04-07 20:12:50 -0700159 *sz = get_file_size(fd);
160 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700161 goto oops;
162 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163
Elliott Hughesfc797672015-04-07 20:12:50 -0700164 data = (char*) malloc(*sz);
165 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166
Elliott Hughesfc797672015-04-07 20:12:50 -0700167 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 close(fd);
169
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170 return data;
171
172oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800173 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 close(fd);
175 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800176 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 return 0;
178}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179
Elliott Hughes2810d002016-04-25 14:31:18 -0700180static void* load_file(const std::string& path, int64_t* sz) {
181 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700182 if (fd == -1) return nullptr;
183 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700184}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185
Elliott Hughesfc797672015-04-07 20:12:50 -0700186static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700187 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700188 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700189 return -1;
190 }
191
192 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
193 return -1;
194 }
195
Scott Anderson13081c62012-04-06 12:39:30 -0700196 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700198 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
199 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200 return 0;
201}
202
Elliott Hughesfc797672015-04-07 20:12:50 -0700203static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800204 return match_fastboot_with_serial(info, serial);
205}
206
Elliott Hughesfc797672015-04-07 20:12:50 -0700207static int list_devices_callback(usb_ifc_info* info) {
208 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800209 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700210 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800211 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700212 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 if (!serial[0]) {
214 serial = "????????????";
215 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700216 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700217 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800218 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700219 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800220 printf("%-22s fastboot", serial.c_str());
221 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700222 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800223 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224 }
225
226 return -1;
227}
228
David Pursell2ec418a2016-01-20 08:32:08 -0800229// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
230// a specific device, otherwise the first USB device found will be used.
231//
232// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
233// Otherwise it blocks until the target is available.
234//
235// The returned Transport is a singleton, so multiple calls to this function will return the same
236// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700237static Transport* open_device() {
238 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800239 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240
David Pursell2ec418a2016-01-20 08:32:08 -0800241 if (transport != nullptr) {
242 return transport;
243 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800244
David Pursell4601c972016-02-05 15:35:09 -0800245 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800246 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800247 int port = 0;
248 if (serial != nullptr) {
249 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800250
David Pursell4601c972016-02-05 15:35:09 -0800251 if (android::base::StartsWith(serial, "tcp:")) {
252 protocol = Socket::Protocol::kTcp;
253 port = tcp::kDefaultPort;
254 net_address = serial + strlen("tcp:");
255 } else if (android::base::StartsWith(serial, "udp:")) {
256 protocol = Socket::Protocol::kUdp;
257 port = udp::kDefaultPort;
258 net_address = serial + strlen("udp:");
259 }
260
261 if (net_address != nullptr) {
262 std::string error;
263 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
264 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
265 error.c_str());
266 return nullptr;
267 }
David Pursell2ec418a2016-01-20 08:32:08 -0800268 }
269 }
270
271 while (true) {
272 if (!host.empty()) {
273 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800274 if (protocol == Socket::Protocol::kTcp) {
275 transport = tcp::Connect(host, port, &error).release();
276 } else if (protocol == Socket::Protocol::kUdp) {
277 transport = udp::Connect(host, port, &error).release();
278 }
279
David Pursell2ec418a2016-01-20 08:32:08 -0800280 if (transport == nullptr && announce) {
281 fprintf(stderr, "error: %s\n", error.c_str());
282 }
283 } else {
284 transport = usb_open(match_fastboot);
285 }
286
287 if (transport != nullptr) {
288 return transport;
289 }
290
David Pursell0b156632015-10-30 11:22:01 -0700291 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800292 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700293 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700295 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 }
297}
298
Elliott Hughesfc797672015-04-07 20:12:50 -0700299static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 // We don't actually open a USB device here,
301 // just getting our callback called so we can
302 // list all the connected devices.
303 usb_open(list_devices_callback);
304}
305
Elliott Hughesfc797672015-04-07 20:12:50 -0700306static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 fprintf(stderr,
308/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
309 "usage: fastboot [ <option> ] <command>\n"
310 "\n"
311 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700312 " update <filename> Reflash device from update.zip.\n"
313 " flashall Flash boot, system, vendor, and --\n"
314 " if found -- recovery.\n"
315 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
316 " flashing lock Locks the device. Prevents flashing.\n"
317 " flashing unlock Unlocks the device. Allows flashing\n"
318 " any partition except\n"
319 " bootloader-related partitions.\n"
320 " flashing lock_critical Prevents flashing bootloader-related\n"
321 " partitions.\n"
322 " flashing unlock_critical Enables flashing bootloader-related\n"
323 " partitions.\n"
324 " flashing get_unlock_ability Queries bootloader to see if the\n"
325 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700326 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700327 " unlock nonce.\n"
328 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700329 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700330 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700331 " erase <partition> Erase a flash partition.\n"
332 " format[:[<fs type>][:[<size>]] <partition>\n"
333 " Format a flash partition. Can\n"
334 " override the fs type and/or size\n"
335 " the bootloader reports.\n"
336 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800337 " set_active <suffix> Sets the active slot. If slots are\n"
338 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700339 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
340 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
341 " Create bootimage and flash it.\n"
342 " devices [-l] List all connected devices [with\n"
343 " device paths].\n"
344 " continue Continue with autoboot.\n"
345 " reboot [bootloader] Reboot device [into bootloader].\n"
346 " reboot-bootloader Reboot device into bootloader.\n"
347 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348 "\n"
349 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700350 " -w Erase userdata and cache (and format\n"
351 " if supported by partition type).\n"
352 " -u Do not erase partition before\n"
353 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800354 " -s <specific device> Specify a device. For USB, provide either\n"
355 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700356 " For ethernet, provide an address in the\n"
357 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800358 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700359 " -p <product> Specify product name.\n"
360 " -c <cmdline> Override kernel commandline.\n"
361 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800362 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700363 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800364 " --kernel-offset Specify a custom kernel offset.\n"
365 " (default: 0x00008000)\n"
366 " --ramdisk-offset Specify a custom ramdisk offset.\n"
367 " (default: 0x01000000)\n"
368 " --tags-offset Specify a custom tags offset.\n"
369 " (default: 0x00000100)\n"
370 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700371 " (default: 2048).\n"
372 " -S <size>[K|M|G] Automatically sparse files greater\n"
373 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700374 " --slot <suffix> Specify slot suffix to be used if the\n"
375 " device supports slots. This will be\n"
376 " added to all partition names that use\n"
377 " slots. 'all' can be given to refer\n"
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800378 " to all slots. 'other' can be given to\n"
379 " refer to a non-current slot. If this\n"
380 " flag is not used, slotted partitions\n"
381 " will default to the current active slot.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800382 " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800383 " provided, this will default to the value\n"
384 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800385 " supported, this does nothing. This will\n"
386 " run after all non-reboot commands.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000387#if !defined(_WIN32)
388 " --wipe-and-use-fbe On devices which support it,\n"
389 " erase userdata and cache, and\n"
390 " enable file-based encryption\n"
391#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800392 " --unbuffered Do not buffer input or output.\n"
393 " --version Display version.\n"
394 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800396}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000397
Elliott Hughesfc797672015-04-07 20:12:50 -0700398static void* load_bootable_image(const char* kernel, const char* ramdisk,
399 const char* secondstage, int64_t* sz,
400 const char* cmdline) {
401 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 fprintf(stderr, "no image specified\n");
403 return 0;
404 }
405
Elliott Hughesfc797672015-04-07 20:12:50 -0700406 int64_t ksize;
407 void* kdata = load_file(kernel, &ksize);
408 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800409 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410 return 0;
411 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800412
Elliott Hughesfc797672015-04-07 20:12:50 -0700413 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700415 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800416
Elliott Hughesfc797672015-04-07 20:12:50 -0700417 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
419 return 0;
420 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800421
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422 *sz = ksize;
423 return kdata;
424 }
425
Elliott Hughesfc797672015-04-07 20:12:50 -0700426 void* rdata = nullptr;
427 int64_t rsize = 0;
428 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700430 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800431 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432 return 0;
433 }
434 }
435
Elliott Hughesfc797672015-04-07 20:12:50 -0700436 void* sdata = nullptr;
437 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200438 if (secondstage) {
439 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700440 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200441 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
442 return 0;
443 }
444 }
445
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700447 int64_t bsize = 0;
448 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800449 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200450 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800451 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700452 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453 fprintf(stderr,"failed to create boot.img\n");
454 return 0;
455 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700456 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
457 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800459
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 return bdata;
461}
462
Elliott Hughesfc797672015-04-07 20:12:50 -0700463static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464{
Yusuke Sato07447542015-06-25 14:39:19 -0700465 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700466 ZipEntry zip_entry;
467 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700468 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000469 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 }
471
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700472 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700474 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700475 if (data == nullptr) {
476 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000477 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478 }
479
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700480 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
481 if (error != 0) {
482 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000484 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000486
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 return data;
488}
489
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700490#if defined(_WIN32)
491
492// TODO: move this to somewhere it can be shared.
493
494#include <windows.h>
495
496// Windows' tmpfile(3) requires administrator rights because
497// it creates temporary files in the root directory.
498static FILE* win32_tmpfile() {
499 char temp_path[PATH_MAX];
500 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
501 if (nchars == 0 || nchars >= sizeof(temp_path)) {
502 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
503 return nullptr;
504 }
505
506 char filename[PATH_MAX];
507 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
508 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
509 return nullptr;
510 }
511
512 return fopen(filename, "w+bTD");
513}
514
515#define tmpfile win32_tmpfile
516
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000517static std::string make_temporary_directory() {
518 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
519 return "";
520}
521
522#else
523
524static std::string make_temporary_directory() {
525 const char *tmpdir = getenv("TMPDIR");
526 if (tmpdir == nullptr) {
527 tmpdir = P_tmpdir;
528 }
529 std::string result = std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
530 if (mkdtemp(&result[0]) == NULL) {
531 fprintf(stderr, "Unable to create temporary directory: %s\n",
532 strerror(errno));
533 return "";
534 }
535 return result;
536}
537
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700538#endif
539
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000540static std::string create_fbemarker_tmpdir() {
541 std::string dir = make_temporary_directory();
542 if (dir.empty()) {
543 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
544 return "";
545 }
546 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
547 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
548 if (fd == -1) {
549 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
550 marker_file.c_str(), errno, strerror(errno));
551 return "";
552 }
553 close(fd);
554 return dir;
555}
556
557static void delete_fbemarker_tmpdir(const std::string& dir) {
558 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
559 if (unlink(marker_file.c_str()) == -1) {
560 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
561 marker_file.c_str(), errno, strerror(errno));
562 return;
563 }
564 if (rmdir(dir.c_str()) == -1) {
565 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
566 dir.c_str(), errno, strerror(errno));
567 return;
568 }
569}
570
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700571static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
572 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700573 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700574 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
575 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700576 return -1;
577 }
578
Yusuke Sato07447542015-06-25 14:39:19 -0700579 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700580 ZipEntry zip_entry;
581 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
582 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000583 return -1;
584 }
585
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700586 int fd = fileno(fp);
587 int error = ExtractEntryToFile(zip, &zip_entry, fd);
588 if (error != 0) {
589 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
590 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700591 }
592
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000593 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700594 return fd;
595}
596
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597static char *strip(char *s)
598{
599 int n;
600 while(*s && isspace(*s)) s++;
601 n = strlen(s);
602 while(n-- > 0) {
603 if(!isspace(s[n])) break;
604 s[n] = 0;
605 }
606 return s;
607}
608
609#define MAX_OPTIONS 32
610static int setup_requirement_line(char *name)
611{
612 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700613 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800614 unsigned n, count;
615 char *x;
616 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800617
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800618 if (!strncmp(name, "reject ", 7)) {
619 name += 7;
620 invert = 1;
621 } else if (!strncmp(name, "require ", 8)) {
622 name += 8;
623 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700624 } else if (!strncmp(name, "require-for-product:", 20)) {
625 // Get the product and point name past it
626 prod = name + 20;
627 name = strchr(name, ' ');
628 if (!name) return -1;
629 *name = 0;
630 name += 1;
631 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800632 }
633
634 x = strchr(name, '=');
635 if (x == 0) return 0;
636 *x = 0;
637 val[0] = x + 1;
638
639 for(count = 1; count < MAX_OPTIONS; count++) {
640 x = strchr(val[count - 1],'|');
641 if (x == 0) break;
642 *x = 0;
643 val[count] = x + 1;
644 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800645
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800646 name = strip(name);
647 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800648
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800649 name = strip(name);
650 if (name == 0) return -1;
651
Elliott Hughes253c18d2015-03-18 22:47:09 -0700652 const char* var = name;
653 // Work around an unfortunate name mismatch.
654 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800655
Elliott Hughes253c18d2015-03-18 22:47:09 -0700656 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800657 if (out == 0) return -1;
658
659 for(n = 0; n < count; n++) {
660 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700661 if (out[n] == 0) {
662 for(size_t i = 0; i < n; ++i) {
663 free((char*) out[i]);
664 }
665 free(out);
666 return -1;
667 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668 }
669
Elliott Hughes253c18d2015-03-18 22:47:09 -0700670 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800671 return 0;
672}
673
Elliott Hughesfc797672015-04-07 20:12:50 -0700674static void setup_requirements(char* data, int64_t sz) {
675 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800676 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700677 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800678 *s++ = 0;
679 if (setup_requirement_line(data)) {
680 die("out of memory");
681 }
682 data = s;
683 } else {
684 s++;
685 }
686 }
687}
688
Elliott Hughesfc797672015-04-07 20:12:50 -0700689static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 fb_queue_notice("--------------------------------------------");
691 fb_queue_display("version-bootloader", "Bootloader Version...");
692 fb_queue_display("version-baseband", "Baseband Version.....");
693 fb_queue_display("serialno", "Serial Number........");
694 fb_queue_notice("--------------------------------------------");
695}
696
Rom Lemarchand622810c2013-06-28 09:54:59 -0700697static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700698{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700699 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700700 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700701 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700702 }
703
Elliott Hughesfc797672015-04-07 20:12:50 -0700704 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700705 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700706 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700707 }
708
Elliott Hughes253c18d2015-03-18 22:47:09 -0700709 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700710 if (!out_s) {
711 die("Failed to allocate sparse file array\n");
712 }
713
714 files = sparse_file_resparse(s, max_size, out_s, files);
715 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700716 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700717 }
718
719 return out_s;
720}
721
David Pursell0b156632015-10-30 11:22:01 -0700722static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700723 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700724 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
725 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800726 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700727 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700728 }
729
Elliott Hughes77c0e662015-11-02 15:51:12 -0800730 // Some bootloaders (angler, for example) send spurious whitespace too.
731 max_download_size = android::base::Trim(max_download_size);
732
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700733 uint64_t limit;
734 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800735 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700736 return 0;
737 }
738 if (limit > 0) {
739 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
740 }
Colin Crossf8387882012-05-24 17:18:41 -0700741 return limit;
742}
743
David Pursell0b156632015-10-30 11:22:01 -0700744static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700745 int64_t limit;
746
747 if (sparse_limit == 0) {
748 return 0;
749 } else if (sparse_limit > 0) {
750 limit = sparse_limit;
751 } else {
752 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700753 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700754 }
755 if (target_sparse_limit > 0) {
756 limit = target_sparse_limit;
757 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700758 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700759 }
760 }
761
762 if (size > limit) {
763 return limit;
764 }
765
766 return 0;
767}
768
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700769// Until we get lazy inode table init working in make_ext4fs, we need to
770// erase partitions of type ext4 before flashing a filesystem so no stale
771// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700772static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800773 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700774 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800775 return false;
776 }
777 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700778}
779
David Pursell0b156632015-10-30 11:22:01 -0700780static int load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700781 int64_t sz = get_file_size(fd);
782 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000783 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700784 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700785
Elliott Hughesfc797672015-04-07 20:12:50 -0700786 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700787 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700788 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700789 sparse_file** s = load_sparse_files(fd, limit);
790 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700791 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700792 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700793 buf->type = FB_BUFFER_SPARSE;
794 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700795 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700796 void* data = load_fd(fd, &sz);
797 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700798 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700799 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000800 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700801 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700802
803 return 0;
804}
805
David Pursell0b156632015-10-30 11:22:01 -0700806static int load_buf(Transport* transport, const char *fname, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700807{
808 int fd;
809
810 fd = open(fname, O_RDONLY | O_BINARY);
811 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700812 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700813 }
814
David Pursell0b156632015-10-30 11:22:01 -0700815 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700816}
817
818static void flash_buf(const char *pname, struct fastboot_buffer *buf)
819{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700820 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700821
822 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800823 case FB_BUFFER_SPARSE: {
824 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700825 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700826 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700827 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800828 sparse_files.emplace_back(*s, sz);
829 ++s;
830 }
831
832 for (size_t i = 0; i < sparse_files.size(); ++i) {
833 const auto& pair = sparse_files[i];
834 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700835 }
836 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800837 }
838
Rom Lemarchand622810c2013-06-28 09:54:59 -0700839 case FB_BUFFER:
840 fb_queue_flash(pname, buf->data, buf->sz);
841 break;
842 default:
843 die("unknown buffer type: %d", buf->type);
844 }
845}
846
David Pursell0b156632015-10-30 11:22:01 -0700847static std::vector<std::string> get_suffixes(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700848 std::vector<std::string> suffixes;
849 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700850 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700851 die("Could not get suffixes.\n");
852 }
853 return android::base::Split(suffix_list, ",");
854}
855
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800856static std::string verify_slot(Transport* transport, const char *slot, bool allow_all) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700857 if (strcmp(slot, "all") == 0) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800858 if (allow_all) {
859 return "all";
860 } else {
861 std::vector<std::string> suffixes = get_suffixes(transport);
862 if (!suffixes.empty()) {
863 return suffixes[0];
864 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800865 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800866 }
867 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700868 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800869
David Pursell0b156632015-10-30 11:22:01 -0700870 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800871
872 if (strcmp(slot, "other") == 0) {
873 std::string current_slot;
874 if (!fb_getvar(transport, "current-slot", &current_slot)) {
875 die("Failed to identify current slot.");
876 }
877 if (!suffixes.empty()) {
878 for (size_t i = 0; i < suffixes.size(); i++) {
879 if (current_slot == suffixes[i])
880 return suffixes[(i+1)%suffixes.size()];
881 }
882 } else {
883 die("No known slots.");
884 }
885 }
886
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700887 for (const std::string &suffix : suffixes) {
888 if (suffix == slot)
889 return slot;
890 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800891 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700892 for (const std::string &suffix : suffixes) {
893 fprintf(stderr, "%s\n", suffix.c_str());
894 }
895 exit(1);
896}
897
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800898static std::string verify_slot(Transport* transport, const char *slot) {
899 return verify_slot(transport, slot, true);
900}
901
David Pursell0b156632015-10-30 11:22:01 -0700902static void do_for_partition(Transport* transport, const char *part, const char *slot,
903 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800904 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700905 std::string current_slot;
906
David Pursell0b156632015-10-30 11:22:01 -0700907 if (!fb_getvar(transport, std::string("has-slot:")+part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800908 /* If has-slot is not supported, the answer is no. */
909 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700910 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800911 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700912 if (!slot || slot[0] == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700913 if (!fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700914 die("Failed to identify current slot.\n");
915 }
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800916 func(std::string(part) + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700917 } else {
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800918 func(std::string(part) + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700919 }
920 } else {
921 if (force_slot && slot && slot[0]) {
David Pursell0b156632015-10-30 11:22:01 -0700922 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
923 part, slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700924 }
925 func(part);
926 }
927}
928
David Pursell0b156632015-10-30 11:22:01 -0700929/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
930 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
931 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
932 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700933 */
David Pursell0b156632015-10-30 11:22:01 -0700934static void do_for_partitions(Transport* transport, const char *part, const char *slot,
935 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800936 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700937
938 if (slot && strcmp(slot, "all") == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700939 if (!fb_getvar(transport, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700940 die("Could not check if partition %s has slot.", part);
941 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800942 if (has_slot == "yes") {
David Pursell0b156632015-10-30 11:22:01 -0700943 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700944 for (std::string &suffix : suffixes) {
David Pursell0b156632015-10-30 11:22:01 -0700945 do_for_partition(transport, part, suffix.c_str(), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700946 }
947 } else {
David Pursell0b156632015-10-30 11:22:01 -0700948 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700949 }
950 } else {
David Pursell0b156632015-10-30 11:22:01 -0700951 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700952 }
953}
954
David Pursell0b156632015-10-30 11:22:01 -0700955static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700956 struct fastboot_buffer buf;
957
David Pursell0b156632015-10-30 11:22:01 -0700958 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700959 die("cannot load '%s'", fname);
960 }
961 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700962}
963
Elliott Hughesfc797672015-04-07 20:12:50 -0700964static void do_update_signature(ZipArchiveHandle zip, char* fn) {
965 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700966 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700967 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800968 fb_queue_download("signature", data, sz);
969 fb_queue_command("signature", "installing signature");
970}
971
David Pursell0b156632015-10-30 11:22:01 -0700972static 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 -0800973 queue_info_dump();
974
Wink Savilleb98762f2011-04-04 17:54:59 -0700975 fb_queue_query_save("product", cur_product, sizeof(cur_product));
976
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700977 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700978 int error = OpenArchive(filename, &zip);
979 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100980 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700981 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700982 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800983
Elliott Hughesfc797672015-04-07 20:12:50 -0700984 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700985 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700986 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100987 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700988 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800989 }
990
Elliott Hughes253c18d2015-03-18 22:47:09 -0700991 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800992
Elliott Hughesacdbe922015-06-02 21:37:05 -0700993 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700994 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700995 if (fd == -1) {
996 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700997 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700998 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100999 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001000 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001001 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001002 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -07001003 int rc = load_buf_fd(transport, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001004 if (rc) die("cannot load %s from flash", images[i].img_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001005
1006 auto update = [&](const std::string &partition) {
1007 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001008 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001009 fb_queue_erase(partition.c_str());
1010 }
1011 flash_buf(partition.c_str(), &buf);
1012 /* not closing the fd here since the sparse code keeps the fd around
1013 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
1014 * program exits.
1015 */
1016 };
David Pursell0b156632015-10-30 11:22:01 -07001017 do_for_partitions(transport, images[i].part_name, slot_override, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001018 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001019
1020 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001021}
1022
Elliott Hughes2810d002016-04-25 14:31:18 -07001023static void do_send_signature(const char* filename) {
1024 if (android::base::EndsWith(filename, ".img") == false) {
1025 return;
1026 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001027
Elliott Hughes2810d002016-04-25 14:31:18 -07001028 std::string sig_path = filename;
1029 sig_path.erase(sig_path.size() - 4);
1030 sig_path += ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001031
1032 int64_t sz;
Elliott Hughes2810d002016-04-25 14:31:18 -07001033 void* data = load_file(sig_path, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001034 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001035
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001036 fb_queue_download("signature", data, sz);
1037 fb_queue_command("signature", "installing signature");
1038}
1039
David Pursell0b156632015-10-30 11:22:01 -07001040static void do_flashall(Transport* transport, const char* slot_override, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001041 queue_info_dump();
1042
Wink Savilleb98762f2011-04-04 17:54:59 -07001043 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1044
Elliott Hughes2810d002016-04-25 14:31:18 -07001045 std::string fname = find_item("info", product);
1046 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001047
Elliott Hughesfc797672015-04-07 20:12:50 -07001048 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001049 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001050 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001051
1052 setup_requirements(reinterpret_cast<char*>(data), sz);
1053
1054 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001055 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001056 fastboot_buffer buf;
Elliott Hughes2810d002016-04-25 14:31:18 -07001057 if (load_buf(transport, fname.c_str(), &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001058 if (images[i].is_optional)
1059 continue;
1060 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001061 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001062
1063 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001064 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001065 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001066 fb_queue_erase(partition.c_str());
1067 }
1068 flash_buf(partition.c_str(), &buf);
1069 };
David Pursell0b156632015-10-30 11:22:01 -07001070 do_for_partitions(transport, images[i].part_name, slot_override, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001071 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001072}
1073
1074#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001075#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001076
Elliott Hughes45be42e2015-09-02 20:15:48 -07001077static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001078{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001079 if (argc <= 2) return 0;
1080 skip(2);
1081
1082 /*
1083 * Process unlock_bootloader, we have to load the message file
1084 * and send that to the remote device.
1085 */
1086 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001087
1088 int64_t sz;
1089 void* data = load_file(*argv, &sz);
1090 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001091 fb_queue_download("unlock_message", data, sz);
1092 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1093 skip(1);
1094 return 0;
1095}
1096
Elliott Hughes45be42e2015-09-02 20:15:48 -07001097static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001098{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001099 char command[256];
1100 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001101
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001102 command[0] = 0;
1103 while(1) {
1104 strcat(command,*argv);
1105 skip(1);
1106 if(argc == 0) break;
1107 strcat(command," ");
1108 }
1109
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001110 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001111 return 0;
1112}
1113
Colin Crossf8387882012-05-24 17:18:41 -07001114static int64_t parse_num(const char *arg)
1115{
1116 char *endptr;
1117 unsigned long long num;
1118
1119 num = strtoull(arg, &endptr, 0);
1120 if (endptr == arg) {
1121 return -1;
1122 }
1123
1124 if (*endptr == 'k' || *endptr == 'K') {
1125 if (num >= (-1ULL) / 1024) {
1126 return -1;
1127 }
1128 num *= 1024LL;
1129 endptr++;
1130 } else if (*endptr == 'm' || *endptr == 'M') {
1131 if (num >= (-1ULL) / (1024 * 1024)) {
1132 return -1;
1133 }
1134 num *= 1024LL * 1024LL;
1135 endptr++;
1136 } else if (*endptr == 'g' || *endptr == 'G') {
1137 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1138 return -1;
1139 }
1140 num *= 1024LL * 1024LL * 1024LL;
1141 endptr++;
1142 }
1143
1144 if (*endptr != '\0') {
1145 return -1;
1146 }
1147
1148 if (num > INT64_MAX) {
1149 return -1;
1150 }
1151
1152 return num;
1153}
1154
David Pursell0b156632015-10-30 11:22:01 -07001155static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001156 const char* partition, int skip_if_not_supported,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001157 const char* type_override, const char* size_override,
1158 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001159 std::string partition_type, partition_size;
1160
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001161 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001162 const char* errMsg = nullptr;
1163 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001164 int fd;
1165
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001166 unsigned int limit = INT_MAX;
1167 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001168 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001169 }
1170 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001171 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001172 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001173
David Pursell0b156632015-10-30 11:22:01 -07001174 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001175 errMsg = "Can't determine partition type.\n";
1176 goto failed;
1177 }
JP Abgrall7e859742014-05-06 15:14:15 -07001178 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001179 if (partition_type != type_override) {
1180 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1181 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001182 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001183 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001184 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001185
David Pursell0b156632015-10-30 11:22:01 -07001186 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001187 errMsg = "Unable to get partition size\n";
1188 goto failed;
1189 }
JP Abgrall7e859742014-05-06 15:14:15 -07001190 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001191 if (partition_size != size_override) {
1192 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1193 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001194 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001195 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001196 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001197 // Some bootloaders (angler, for example), send spurious leading whitespace.
1198 partition_size = android::base::Trim(partition_size);
1199 // Some bootloaders (hammerhead, for example) use implicit hex.
1200 // This code used to use strtol with base 16.
1201 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001202
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001203 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001204 if (!gen) {
1205 if (skip_if_not_supported) {
1206 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001207 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001208 return;
1209 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001210 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1211 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001212 return;
1213 }
1214
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001215 int64_t size;
1216 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1217 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1218 return;
1219 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001220
1221 fd = fileno(tmpfile());
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001222 if (fs_generator_generate(gen, fd, size, initial_dir)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001223 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001224 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001225 return;
1226 }
1227
David Pursell0b156632015-10-30 11:22:01 -07001228 if (load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001229 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001230 close(fd);
1231 return;
1232 }
1233 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001234 return;
1235
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001236failed:
1237 if (skip_if_not_supported) {
1238 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001239 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001240 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001241 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001242}
1243
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001244int main(int argc, char **argv)
1245{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001246 bool wants_wipe = false;
1247 bool wants_reboot = false;
1248 bool wants_reboot_bootloader = false;
1249 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001250 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001251 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001252 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001253 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001254 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001255 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001256 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001257
JP Abgrall7b8970c2013-03-07 17:06:41 -08001258 const struct option longopts[] = {
1259 {"base", required_argument, 0, 'b'},
1260 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001261 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001262 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001263 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001264 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001265 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001266 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001267 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001268 {"help", no_argument, 0, 'h'},
1269 {"unbuffered", no_argument, 0, 0},
1270 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001271 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001272 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001273 {"set-active", optional_argument, 0, 'a'},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001274#if !defined(_WIN32)
1275 {"wipe-and-use-fbe", no_argument, 0, 0},
1276#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001277 {0, 0, 0, 0}
1278 };
Colin Cross8879f982012-05-22 17:53:34 -07001279
1280 serial = getenv("ANDROID_SERIAL");
1281
1282 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001283 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 -07001284 if (c < 0) {
1285 break;
1286 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001287 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001288 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001289 case 'a':
1290 wants_set_active = true;
1291 if (optarg)
1292 next_active = optarg;
1293 break;
Colin Cross8879f982012-05-22 17:53:34 -07001294 case 'b':
1295 base_addr = strtoul(optarg, 0, 16);
1296 break;
Colin Cross8879f982012-05-22 17:53:34 -07001297 case 'c':
1298 cmdline = optarg;
1299 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001300 case 'h':
1301 usage();
1302 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001303 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001304 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001305 unsigned long val;
1306
1307 val = strtoul(optarg, &endptr, 0);
1308 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1309 die("invalid vendor id '%s'", optarg);
1310 vendor_id = (unsigned short)val;
1311 break;
1312 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001313 case 'k':
1314 kernel_offset = strtoul(optarg, 0, 16);
1315 break;
1316 case 'l':
1317 long_listing = 1;
1318 break;
1319 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001320 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001321 if (!page_size) die("invalid page size");
1322 break;
1323 case 'p':
1324 product = optarg;
1325 break;
1326 case 'r':
1327 ramdisk_offset = strtoul(optarg, 0, 16);
1328 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001329 case 't':
1330 tags_offset = strtoul(optarg, 0, 16);
1331 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001332 case 's':
1333 serial = optarg;
1334 break;
1335 case 'S':
1336 sparse_limit = parse_num(optarg);
1337 if (sparse_limit < 0) {
1338 die("invalid sparse limit");
1339 }
1340 break;
1341 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001342 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001343 break;
1344 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001345 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001346 break;
Colin Cross8879f982012-05-22 17:53:34 -07001347 case '?':
1348 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001349 case 0:
1350 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001351 setvbuf(stdout, nullptr, _IONBF, 0);
1352 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001353 } else if (strcmp("version", longopts[longindex].name) == 0) {
1354 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1355 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001356 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1357 slot_override = std::string(optarg);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001358#if !defined(_WIN32)
1359 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1360 wants_wipe = true;
1361 set_fbe_marker = true;
1362#endif
1363 } else {
1364 fprintf(stderr, "Internal error in options processing for %s\n",
1365 longopts[longindex].name);
1366 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001367 }
1368 break;
Colin Cross8879f982012-05-22 17:53:34 -07001369 default:
1370 abort();
1371 }
1372 }
1373
1374 argc -= optind;
1375 argv += optind;
1376
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001377 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001378 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001379 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001380 }
1381
Colin Cross8fb6e062012-07-24 16:36:41 -07001382 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001383 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001384 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001385 return 0;
1386 }
1387
Colin Crossc7b75dc2012-08-29 18:17:06 -07001388 if (argc > 0 && !strcmp(*argv, "help")) {
1389 usage();
1390 return 0;
1391 }
1392
David Pursell0b156632015-10-30 11:22:01 -07001393 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001394 if (transport == nullptr) {
1395 return 1;
1396 }
1397
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001398 if (slot_override != "")
David Pursell0b156632015-10-30 11:22:01 -07001399 slot_override = verify_slot(transport, slot_override.c_str());
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001400 if (next_active != "")
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001401 next_active = verify_slot(transport, next_active.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001402
1403 if (wants_set_active) {
1404 if (next_active == "") {
1405 if (slot_override == "") {
1406 wants_set_active = false;
1407 } else {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001408 next_active = verify_slot(transport, slot_override.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001409 }
1410 }
1411 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001412
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001413 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001414 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001415 require(2);
1416 fb_queue_display(argv[1], argv[1]);
1417 skip(2);
1418 } else if(!strcmp(*argv, "erase")) {
1419 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001420
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001421 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001422 std::string partition_type;
1423 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001424 fs_get_generator(partition_type) != nullptr) {
1425 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1426 partition_type.c_str());
1427 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001428
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001429 fb_queue_erase(partition.c_str());
1430 };
David Pursell0b156632015-10-30 11:22:01 -07001431 do_for_partitions(transport, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001432 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001433 } else if(!strncmp(*argv, "format", strlen("format"))) {
1434 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001435 char *type_override = nullptr;
1436 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001437 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001438 /*
1439 * Parsing for: "format[:[type][:[size]]]"
1440 * Some valid things:
1441 * - select ontly the size, and leave default fs type:
1442 * format::0x4000000 userdata
1443 * - default fs type and size:
1444 * format userdata
1445 * format:: userdata
1446 */
1447 overrides = strchr(*argv, ':');
1448 if (overrides) {
1449 overrides++;
1450 size_override = strchr(overrides, ':');
1451 if (size_override) {
1452 size_override[0] = '\0';
1453 size_override++;
1454 }
1455 type_override = overrides;
1456 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001457 if (type_override && !type_override[0]) type_override = nullptr;
1458 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001459
1460 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001461 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001462 fb_queue_erase(partition.c_str());
1463 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001464 fb_perform_format(transport, partition.c_str(), 0,
1465 type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001466 };
David Pursell0b156632015-10-30 11:22:01 -07001467 do_for_partitions(transport, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001468 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001469 } else if(!strcmp(*argv, "signature")) {
1470 require(2);
1471 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001472 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001473 if (sz != 256) die("signature must be 256 bytes");
1474 fb_queue_download("signature", data, sz);
1475 fb_queue_command("signature", "installing signature");
1476 skip(2);
1477 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001478 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001479 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001480 if (argc > 0) {
1481 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001482 wants_reboot = false;
1483 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001484 skip(1);
1485 }
1486 }
1487 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001488 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001489 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001490 skip(1);
1491 } else if (!strcmp(*argv, "continue")) {
1492 fb_queue_command("continue", "resuming boot");
1493 skip(1);
1494 } else if(!strcmp(*argv, "boot")) {
1495 char *kname = 0;
1496 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001497 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001498 skip(1);
1499 if (argc > 0) {
1500 kname = argv[0];
1501 skip(1);
1502 }
1503 if (argc > 0) {
1504 rname = argv[0];
1505 skip(1);
1506 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001507 if (argc > 0) {
1508 sname = argv[0];
1509 skip(1);
1510 }
1511 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001512 if (data == 0) return 1;
1513 fb_queue_download("boot.img", data, sz);
1514 fb_queue_command("boot", "booting");
1515 } else if(!strcmp(*argv, "flash")) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001516 char* pname = argv[1];
1517 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001518 require(2);
1519 if (argc > 2) {
1520 fname = argv[2];
1521 skip(3);
1522 } else {
1523 fname = find_item(pname, product);
1524 skip(2);
1525 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001526 if (fname.empty()) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001527
1528 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001529 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001530 fb_queue_erase(partition.c_str());
1531 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001532 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001533 };
David Pursell0b156632015-10-30 11:22:01 -07001534 do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001535 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001536 char *kname = argv[2];
1537 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001538 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001539 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001540 skip(3);
1541 if (argc > 0) {
1542 rname = argv[0];
1543 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001544 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001545 if (argc > 0) {
1546 sname = argv[0];
1547 skip(1);
1548 }
1549 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001550 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001551 auto flashraw = [&](const std::string &partition) {
1552 fb_queue_flash(partition.c_str(), data, sz);
1553 };
David Pursell0b156632015-10-30 11:22:01 -07001554 do_for_partitions(transport, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001555 } else if(!strcmp(*argv, "flashall")) {
1556 skip(1);
David Pursell0b156632015-10-30 11:22:01 -07001557 do_flashall(transport, slot_override.c_str(), erase_first);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001558 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001559 } else if(!strcmp(*argv, "update")) {
1560 if (argc > 1) {
David Pursell0b156632015-10-30 11:22:01 -07001561 do_update(transport, argv[1], slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001562 skip(2);
1563 } else {
David Pursell0b156632015-10-30 11:22:01 -07001564 do_update(transport, "update.zip", slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001565 skip(1);
1566 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001567 wants_reboot = 1;
1568 } else if(!strcmp(*argv, "set_active")) {
1569 require(2);
1570 std::string slot = verify_slot(transport, argv[1], false);
1571 fb_set_active(slot.c_str());
1572 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001573 } else if(!strcmp(*argv, "oem")) {
1574 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001575 } else if(!strcmp(*argv, "flashing")) {
1576 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1577 !strcmp(*(argv+1), "lock") ||
1578 !strcmp(*(argv+1), "unlock_critical") ||
1579 !strcmp(*(argv+1), "lock_critical") ||
1580 !strcmp(*(argv+1), "get_unlock_ability") ||
1581 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1582 !strcmp(*(argv+1), "lock_bootloader"))) {
1583 argc = do_oem_command(argc, argv);
1584 } else
1585 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1586 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001587 } else {
1588 usage();
1589 return 1;
1590 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001591 } else {
1592 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001593 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001594 }
1595 }
1596
1597 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001598 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001599 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001600 if (set_fbe_marker) {
1601 fprintf(stderr, "setting FBE marker...\n");
1602 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1603 if (initial_userdata_dir.empty()) {
1604 return 1;
1605 }
1606 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
1607 delete_fbemarker_tmpdir(initial_userdata_dir);
1608 } else {
1609 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
1610 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001611
1612 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001613 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001614 fprintf(stderr, "wiping cache...\n");
1615 fb_queue_erase("cache");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001616 fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001617 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001618 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001619 if (wants_set_active) {
1620 fb_set_active(next_active.c_str());
1621 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001622 if (wants_reboot) {
1623 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001624 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001625 } else if (wants_reboot_bootloader) {
1626 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001627 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001628 }
1629
David Pursell0b156632015-10-30 11:22:01 -07001630 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001631}