blob: 7c7d417c5431bc15cfcf635cb9f59369228dbfe8 [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"
62#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063
Colin Crossf8387882012-05-24 17:18:41 -070064#ifndef O_BINARY
65#define O_BINARY 0
66#endif
67
Rom Lemarchand622810c2013-06-28 09:54:59 -070068#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
69
Wink Savilleb98762f2011-04-04 17:54:59 -070070char cur_product[FB_RESPONSE_SZ + 1];
71
David Pursell2ec418a2016-01-20 08:32:08 -080072static const char* serial = nullptr;
73static const char* product = nullptr;
74static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070076static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070077static int64_t sparse_limit = -1;
78static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079
Elliott Hughesfc797672015-04-07 20:12:50 -070080static unsigned page_size = 2048;
81static unsigned base_addr = 0x10000000;
82static unsigned kernel_offset = 0x00008000;
83static unsigned ramdisk_offset = 0x01000000;
84static unsigned second_offset = 0x00f00000;
85static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080086
Paul Crowley8f7f56e2015-11-27 09:29:37 +000087static const std::string convert_fbe_marker_filename("convert_fbe");
88
Rom Lemarchand622810c2013-06-28 09:54:59 -070089enum fb_buffer_type {
90 FB_BUFFER,
91 FB_BUFFER_SPARSE,
92};
93
94struct fastboot_buffer {
95 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070096 void* data;
97 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070098};
99
100static struct {
101 char img_name[13];
102 char sig_name[13];
103 char part_name[9];
104 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700105} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700106 {"boot.img", "boot.sig", "boot", false},
107 {"recovery.img", "recovery.sig", "recovery", true},
108 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700109 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700110};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700111
Elliott Hughesfc797672015-04-07 20:12:50 -0700112static char* find_item(const char* item, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700114 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115 char path[PATH_MAX + 128];
116
117 if(!strcmp(item,"boot")) {
118 fn = "boot.img";
119 } else if(!strcmp(item,"recovery")) {
120 fn = "recovery.img";
121 } else if(!strcmp(item,"system")) {
122 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700123 } else if(!strcmp(item,"vendor")) {
124 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 } else if(!strcmp(item,"userdata")) {
126 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700127 } else if(!strcmp(item,"cache")) {
128 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129 } else if(!strcmp(item,"info")) {
130 fn = "android-info.txt";
131 } else {
132 fprintf(stderr,"unknown partition '%s'\n", item);
133 return 0;
134 }
135
136 if(product) {
137 get_my_path(path);
138 sprintf(path + strlen(path),
139 "../../../target/product/%s/%s", product, fn);
140 return strdup(path);
141 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800142
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 dir = getenv("ANDROID_PRODUCT_OUT");
144 if((dir == 0) || (dir[0] == 0)) {
145 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
146 return 0;
147 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800148
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 sprintf(path, "%s/%s", dir, fn);
150 return strdup(path);
151}
152
Elliott Hughesfc797672015-04-07 20:12:50 -0700153static int64_t get_file_size(int fd) {
154 struct stat sb;
155 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700156}
157
Elliott Hughesfc797672015-04-07 20:12:50 -0700158static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800159 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700160 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161
Elliott Hughesfc797672015-04-07 20:12:50 -0700162 *sz = get_file_size(fd);
163 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700164 goto oops;
165 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166
Elliott Hughesfc797672015-04-07 20:12:50 -0700167 data = (char*) malloc(*sz);
168 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169
Elliott Hughesfc797672015-04-07 20:12:50 -0700170 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171 close(fd);
172
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 return data;
174
175oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800176 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 close(fd);
178 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800179 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 return 0;
181}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182
Elliott Hughesfc797672015-04-07 20:12:50 -0700183static void* load_file(const char* fn, int64_t* sz) {
184 int fd = open(fn, O_RDONLY | O_BINARY);
185 if (fd == -1) return nullptr;
186 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700187}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188
Elliott Hughesfc797672015-04-07 20:12:50 -0700189static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700190 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700191 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700192 return -1;
193 }
194
195 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
196 return -1;
197 }
198
Scott Anderson13081c62012-04-06 12:39:30 -0700199 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700201 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
202 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 return 0;
204}
205
Elliott Hughesfc797672015-04-07 20:12:50 -0700206static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800207 return match_fastboot_with_serial(info, serial);
208}
209
Elliott Hughesfc797672015-04-07 20:12:50 -0700210static int list_devices_callback(usb_ifc_info* info) {
211 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800212 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700213 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800214 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700215 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216 if (!serial[0]) {
217 serial = "????????????";
218 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700219 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700220 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800221 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700222 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800223 printf("%-22s fastboot", serial.c_str());
224 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700225 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800226 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227 }
228
229 return -1;
230}
231
David Pursell2ec418a2016-01-20 08:32:08 -0800232// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
233// a specific device, otherwise the first USB device found will be used.
234//
235// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
236// Otherwise it blocks until the target is available.
237//
238// The returned Transport is a singleton, so multiple calls to this function will return the same
239// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700240static Transport* open_device() {
241 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800242 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243
David Pursell2ec418a2016-01-20 08:32:08 -0800244 if (transport != nullptr) {
245 return transport;
246 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800247
David Pursell2ec418a2016-01-20 08:32:08 -0800248 std::string host;
249 int port = tcp::kDefaultPort;
250 if (serial != nullptr && android::base::StartsWith(serial, "tcp:")) {
251 std::string error;
252 const char* address = serial + strlen("tcp:");
253
254 if (!android::base::ParseNetAddress(address, &host, &port, nullptr, &error)) {
255 fprintf(stderr, "error: Invalid network address '%s': %s\n", address, error.c_str());
256 return nullptr;
257 }
258 }
259
260 while (true) {
261 if (!host.empty()) {
262 std::string error;
263 transport = tcp::Connect(host, port, &error).release();
264 if (transport == nullptr && announce) {
265 fprintf(stderr, "error: %s\n", error.c_str());
266 }
267 } else {
268 transport = usb_open(match_fastboot);
269 }
270
271 if (transport != nullptr) {
272 return transport;
273 }
274
David Pursell0b156632015-10-30 11:22:01 -0700275 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800276 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700277 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700279 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280 }
281}
282
Elliott Hughesfc797672015-04-07 20:12:50 -0700283static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284 // We don't actually open a USB device here,
285 // just getting our callback called so we can
286 // list all the connected devices.
287 usb_open(list_devices_callback);
288}
289
Elliott Hughesfc797672015-04-07 20:12:50 -0700290static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291 fprintf(stderr,
292/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
293 "usage: fastboot [ <option> ] <command>\n"
294 "\n"
295 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700296 " update <filename> Reflash device from update.zip.\n"
297 " flashall Flash boot, system, vendor, and --\n"
298 " if found -- recovery.\n"
299 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
300 " flashing lock Locks the device. Prevents flashing.\n"
301 " flashing unlock Unlocks the device. Allows flashing\n"
302 " any partition except\n"
303 " bootloader-related partitions.\n"
304 " flashing lock_critical Prevents flashing bootloader-related\n"
305 " partitions.\n"
306 " flashing unlock_critical Enables flashing bootloader-related\n"
307 " partitions.\n"
308 " flashing get_unlock_ability Queries bootloader to see if the\n"
309 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700310 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700311 " unlock nonce.\n"
312 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700313 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700314 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700315 " erase <partition> Erase a flash partition.\n"
316 " format[:[<fs type>][:[<size>]] <partition>\n"
317 " Format a flash partition. Can\n"
318 " override the fs type and/or size\n"
319 " the bootloader reports.\n"
320 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800321 " set_active <suffix> Sets the active slot. If slots are\n"
322 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700323 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
324 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
325 " Create bootimage and flash it.\n"
326 " devices [-l] List all connected devices [with\n"
327 " device paths].\n"
328 " continue Continue with autoboot.\n"
329 " reboot [bootloader] Reboot device [into bootloader].\n"
330 " reboot-bootloader Reboot device into bootloader.\n"
331 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332 "\n"
333 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700334 " -w Erase userdata and cache (and format\n"
335 " if supported by partition type).\n"
336 " -u Do not erase partition before\n"
337 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800338 " -s <specific device> Specify a device. For USB, provide either\n"
339 " a serial number or path to device port.\n"
340 " For TCP, provide an address in the form\n"
341 " tcp:<hostname>[:port].\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700342 " -p <product> Specify product name.\n"
343 " -c <cmdline> Override kernel commandline.\n"
344 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800345 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700346 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800347 " --kernel-offset Specify a custom kernel offset.\n"
348 " (default: 0x00008000)\n"
349 " --ramdisk-offset Specify a custom ramdisk offset.\n"
350 " (default: 0x01000000)\n"
351 " --tags-offset Specify a custom tags offset.\n"
352 " (default: 0x00000100)\n"
353 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700354 " (default: 2048).\n"
355 " -S <size>[K|M|G] Automatically sparse files greater\n"
356 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700357 " --slot <suffix> Specify slot suffix to be used if the\n"
358 " device supports slots. This will be\n"
359 " added to all partition names that use\n"
360 " slots. 'all' can be given to refer\n"
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800361 " to all slots. 'other' can be given to\n"
362 " refer to a non-current slot. If this\n"
363 " flag is not used, slotted partitions\n"
364 " will default to the current active slot.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800365 " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800366 " provided, this will default to the value\n"
367 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800368 " supported, this does nothing. This will\n"
369 " run after all non-reboot commands.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000370#if !defined(_WIN32)
371 " --wipe-and-use-fbe On devices which support it,\n"
372 " erase userdata and cache, and\n"
373 " enable file-based encryption\n"
374#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800375 " --unbuffered Do not buffer input or output.\n"
376 " --version Display version.\n"
377 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000380
Elliott Hughesfc797672015-04-07 20:12:50 -0700381static void* load_bootable_image(const char* kernel, const char* ramdisk,
382 const char* secondstage, int64_t* sz,
383 const char* cmdline) {
384 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 fprintf(stderr, "no image specified\n");
386 return 0;
387 }
388
Elliott Hughesfc797672015-04-07 20:12:50 -0700389 int64_t ksize;
390 void* kdata = load_file(kernel, &ksize);
391 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800392 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393 return 0;
394 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800395
Elliott Hughesfc797672015-04-07 20:12:50 -0700396 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700398 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800399
Elliott Hughesfc797672015-04-07 20:12:50 -0700400 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
402 return 0;
403 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800404
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 *sz = ksize;
406 return kdata;
407 }
408
Elliott Hughesfc797672015-04-07 20:12:50 -0700409 void* rdata = nullptr;
410 int64_t rsize = 0;
411 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700413 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800414 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415 return 0;
416 }
417 }
418
Elliott Hughesfc797672015-04-07 20:12:50 -0700419 void* sdata = nullptr;
420 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200421 if (secondstage) {
422 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700423 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200424 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
425 return 0;
426 }
427 }
428
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700430 int64_t bsize = 0;
431 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800432 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200433 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800434 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700435 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436 fprintf(stderr,"failed to create boot.img\n");
437 return 0;
438 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700439 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
440 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800442
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443 return bdata;
444}
445
Elliott Hughesfc797672015-04-07 20:12:50 -0700446static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800447{
Yusuke Sato07447542015-06-25 14:39:19 -0700448 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700449 ZipEntry zip_entry;
450 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700451 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000452 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453 }
454
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700455 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700457 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700458 if (data == nullptr) {
459 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000460 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461 }
462
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700463 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
464 if (error != 0) {
465 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000467 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000469
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 return data;
471}
472
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700473#if defined(_WIN32)
474
475// TODO: move this to somewhere it can be shared.
476
477#include <windows.h>
478
479// Windows' tmpfile(3) requires administrator rights because
480// it creates temporary files in the root directory.
481static FILE* win32_tmpfile() {
482 char temp_path[PATH_MAX];
483 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
484 if (nchars == 0 || nchars >= sizeof(temp_path)) {
485 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
486 return nullptr;
487 }
488
489 char filename[PATH_MAX];
490 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
491 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
492 return nullptr;
493 }
494
495 return fopen(filename, "w+bTD");
496}
497
498#define tmpfile win32_tmpfile
499
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000500static std::string make_temporary_directory() {
501 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
502 return "";
503}
504
505#else
506
507static std::string make_temporary_directory() {
508 const char *tmpdir = getenv("TMPDIR");
509 if (tmpdir == nullptr) {
510 tmpdir = P_tmpdir;
511 }
512 std::string result = std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
513 if (mkdtemp(&result[0]) == NULL) {
514 fprintf(stderr, "Unable to create temporary directory: %s\n",
515 strerror(errno));
516 return "";
517 }
518 return result;
519}
520
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700521#endif
522
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000523static std::string create_fbemarker_tmpdir() {
524 std::string dir = make_temporary_directory();
525 if (dir.empty()) {
526 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
527 return "";
528 }
529 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
530 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
531 if (fd == -1) {
532 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
533 marker_file.c_str(), errno, strerror(errno));
534 return "";
535 }
536 close(fd);
537 return dir;
538}
539
540static void delete_fbemarker_tmpdir(const std::string& dir) {
541 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
542 if (unlink(marker_file.c_str()) == -1) {
543 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
544 marker_file.c_str(), errno, strerror(errno));
545 return;
546 }
547 if (rmdir(dir.c_str()) == -1) {
548 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
549 dir.c_str(), errno, strerror(errno));
550 return;
551 }
552}
553
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700554static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
555 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700556 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700557 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
558 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700559 return -1;
560 }
561
Yusuke Sato07447542015-06-25 14:39:19 -0700562 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700563 ZipEntry zip_entry;
564 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
565 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000566 return -1;
567 }
568
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700569 int fd = fileno(fp);
570 int error = ExtractEntryToFile(zip, &zip_entry, fd);
571 if (error != 0) {
572 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
573 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700574 }
575
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000576 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700577 return fd;
578}
579
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800580static char *strip(char *s)
581{
582 int n;
583 while(*s && isspace(*s)) s++;
584 n = strlen(s);
585 while(n-- > 0) {
586 if(!isspace(s[n])) break;
587 s[n] = 0;
588 }
589 return s;
590}
591
592#define MAX_OPTIONS 32
593static int setup_requirement_line(char *name)
594{
595 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700596 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597 unsigned n, count;
598 char *x;
599 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800600
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800601 if (!strncmp(name, "reject ", 7)) {
602 name += 7;
603 invert = 1;
604 } else if (!strncmp(name, "require ", 8)) {
605 name += 8;
606 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700607 } else if (!strncmp(name, "require-for-product:", 20)) {
608 // Get the product and point name past it
609 prod = name + 20;
610 name = strchr(name, ' ');
611 if (!name) return -1;
612 *name = 0;
613 name += 1;
614 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800615 }
616
617 x = strchr(name, '=');
618 if (x == 0) return 0;
619 *x = 0;
620 val[0] = x + 1;
621
622 for(count = 1; count < MAX_OPTIONS; count++) {
623 x = strchr(val[count - 1],'|');
624 if (x == 0) break;
625 *x = 0;
626 val[count] = x + 1;
627 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800628
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800629 name = strip(name);
630 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800631
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800632 name = strip(name);
633 if (name == 0) return -1;
634
Elliott Hughes253c18d2015-03-18 22:47:09 -0700635 const char* var = name;
636 // Work around an unfortunate name mismatch.
637 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800638
Elliott Hughes253c18d2015-03-18 22:47:09 -0700639 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800640 if (out == 0) return -1;
641
642 for(n = 0; n < count; n++) {
643 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700644 if (out[n] == 0) {
645 for(size_t i = 0; i < n; ++i) {
646 free((char*) out[i]);
647 }
648 free(out);
649 return -1;
650 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800651 }
652
Elliott Hughes253c18d2015-03-18 22:47:09 -0700653 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800654 return 0;
655}
656
Elliott Hughesfc797672015-04-07 20:12:50 -0700657static void setup_requirements(char* data, int64_t sz) {
658 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800659 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700660 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661 *s++ = 0;
662 if (setup_requirement_line(data)) {
663 die("out of memory");
664 }
665 data = s;
666 } else {
667 s++;
668 }
669 }
670}
671
Elliott Hughesfc797672015-04-07 20:12:50 -0700672static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800673 fb_queue_notice("--------------------------------------------");
674 fb_queue_display("version-bootloader", "Bootloader Version...");
675 fb_queue_display("version-baseband", "Baseband Version.....");
676 fb_queue_display("serialno", "Serial Number........");
677 fb_queue_notice("--------------------------------------------");
678}
679
Rom Lemarchand622810c2013-06-28 09:54:59 -0700680static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700681{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700682 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700683 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700684 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700685 }
686
Elliott Hughesfc797672015-04-07 20:12:50 -0700687 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700688 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700689 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700690 }
691
Elliott Hughes253c18d2015-03-18 22:47:09 -0700692 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700693 if (!out_s) {
694 die("Failed to allocate sparse file array\n");
695 }
696
697 files = sparse_file_resparse(s, max_size, out_s, files);
698 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700699 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700700 }
701
702 return out_s;
703}
704
David Pursell0b156632015-10-30 11:22:01 -0700705static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700706 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700707 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
708 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800709 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700710 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700711 }
712
Elliott Hughes77c0e662015-11-02 15:51:12 -0800713 // Some bootloaders (angler, for example) send spurious whitespace too.
714 max_download_size = android::base::Trim(max_download_size);
715
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700716 uint64_t limit;
717 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800718 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700719 return 0;
720 }
721 if (limit > 0) {
722 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
723 }
Colin Crossf8387882012-05-24 17:18:41 -0700724 return limit;
725}
726
David Pursell0b156632015-10-30 11:22:01 -0700727static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700728 int64_t limit;
729
730 if (sparse_limit == 0) {
731 return 0;
732 } else if (sparse_limit > 0) {
733 limit = sparse_limit;
734 } else {
735 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700736 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700737 }
738 if (target_sparse_limit > 0) {
739 limit = target_sparse_limit;
740 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700741 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700742 }
743 }
744
745 if (size > limit) {
746 return limit;
747 }
748
749 return 0;
750}
751
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700752// Until we get lazy inode table init working in make_ext4fs, we need to
753// erase partitions of type ext4 before flashing a filesystem so no stale
754// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700755static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800756 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700757 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800758 return false;
759 }
760 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700761}
762
David Pursell0b156632015-10-30 11:22:01 -0700763static int load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700764 int64_t sz = get_file_size(fd);
765 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000766 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700767 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700768
Elliott Hughesfc797672015-04-07 20:12:50 -0700769 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700770 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700771 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700772 sparse_file** s = load_sparse_files(fd, limit);
773 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700774 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700775 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700776 buf->type = FB_BUFFER_SPARSE;
777 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700778 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700779 void* data = load_fd(fd, &sz);
780 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700781 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700782 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000783 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700784 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700785
786 return 0;
787}
788
David Pursell0b156632015-10-30 11:22:01 -0700789static int load_buf(Transport* transport, const char *fname, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700790{
791 int fd;
792
793 fd = open(fname, O_RDONLY | O_BINARY);
794 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700795 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700796 }
797
David Pursell0b156632015-10-30 11:22:01 -0700798 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700799}
800
801static void flash_buf(const char *pname, struct fastboot_buffer *buf)
802{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700803 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700804
805 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800806 case FB_BUFFER_SPARSE: {
807 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700808 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700809 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700810 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800811 sparse_files.emplace_back(*s, sz);
812 ++s;
813 }
814
815 for (size_t i = 0; i < sparse_files.size(); ++i) {
816 const auto& pair = sparse_files[i];
817 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700818 }
819 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800820 }
821
Rom Lemarchand622810c2013-06-28 09:54:59 -0700822 case FB_BUFFER:
823 fb_queue_flash(pname, buf->data, buf->sz);
824 break;
825 default:
826 die("unknown buffer type: %d", buf->type);
827 }
828}
829
David Pursell0b156632015-10-30 11:22:01 -0700830static std::vector<std::string> get_suffixes(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700831 std::vector<std::string> suffixes;
832 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700833 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700834 die("Could not get suffixes.\n");
835 }
836 return android::base::Split(suffix_list, ",");
837}
838
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800839static std::string verify_slot(Transport* transport, const char *slot, bool allow_all) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700840 if (strcmp(slot, "all") == 0) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800841 if (allow_all) {
842 return "all";
843 } else {
844 std::vector<std::string> suffixes = get_suffixes(transport);
845 if (!suffixes.empty()) {
846 return suffixes[0];
847 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800848 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800849 }
850 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700851 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800852
David Pursell0b156632015-10-30 11:22:01 -0700853 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800854
855 if (strcmp(slot, "other") == 0) {
856 std::string current_slot;
857 if (!fb_getvar(transport, "current-slot", &current_slot)) {
858 die("Failed to identify current slot.");
859 }
860 if (!suffixes.empty()) {
861 for (size_t i = 0; i < suffixes.size(); i++) {
862 if (current_slot == suffixes[i])
863 return suffixes[(i+1)%suffixes.size()];
864 }
865 } else {
866 die("No known slots.");
867 }
868 }
869
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700870 for (const std::string &suffix : suffixes) {
871 if (suffix == slot)
872 return slot;
873 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800874 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700875 for (const std::string &suffix : suffixes) {
876 fprintf(stderr, "%s\n", suffix.c_str());
877 }
878 exit(1);
879}
880
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800881static std::string verify_slot(Transport* transport, const char *slot) {
882 return verify_slot(transport, slot, true);
883}
884
David Pursell0b156632015-10-30 11:22:01 -0700885static void do_for_partition(Transport* transport, const char *part, const char *slot,
886 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800887 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700888 std::string current_slot;
889
David Pursell0b156632015-10-30 11:22:01 -0700890 if (!fb_getvar(transport, std::string("has-slot:")+part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800891 /* If has-slot is not supported, the answer is no. */
892 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700893 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800894 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700895 if (!slot || slot[0] == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700896 if (!fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700897 die("Failed to identify current slot.\n");
898 }
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800899 func(std::string(part) + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700900 } else {
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800901 func(std::string(part) + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700902 }
903 } else {
904 if (force_slot && slot && slot[0]) {
David Pursell0b156632015-10-30 11:22:01 -0700905 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
906 part, slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700907 }
908 func(part);
909 }
910}
911
David Pursell0b156632015-10-30 11:22:01 -0700912/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
913 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
914 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
915 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700916 */
David Pursell0b156632015-10-30 11:22:01 -0700917static void do_for_partitions(Transport* transport, const char *part, const char *slot,
918 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800919 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700920
921 if (slot && strcmp(slot, "all") == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700922 if (!fb_getvar(transport, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700923 die("Could not check if partition %s has slot.", part);
924 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800925 if (has_slot == "yes") {
David Pursell0b156632015-10-30 11:22:01 -0700926 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700927 for (std::string &suffix : suffixes) {
David Pursell0b156632015-10-30 11:22:01 -0700928 do_for_partition(transport, part, suffix.c_str(), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700929 }
930 } else {
David Pursell0b156632015-10-30 11:22:01 -0700931 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700932 }
933 } else {
David Pursell0b156632015-10-30 11:22:01 -0700934 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700935 }
936}
937
David Pursell0b156632015-10-30 11:22:01 -0700938static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700939 struct fastboot_buffer buf;
940
David Pursell0b156632015-10-30 11:22:01 -0700941 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700942 die("cannot load '%s'", fname);
943 }
944 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700945}
946
Elliott Hughesfc797672015-04-07 20:12:50 -0700947static void do_update_signature(ZipArchiveHandle zip, char* fn) {
948 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700949 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700950 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800951 fb_queue_download("signature", data, sz);
952 fb_queue_command("signature", "installing signature");
953}
954
David Pursell0b156632015-10-30 11:22:01 -0700955static 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 -0800956 queue_info_dump();
957
Wink Savilleb98762f2011-04-04 17:54:59 -0700958 fb_queue_query_save("product", cur_product, sizeof(cur_product));
959
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700960 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700961 int error = OpenArchive(filename, &zip);
962 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100963 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700964 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700965 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800966
Elliott Hughesfc797672015-04-07 20:12:50 -0700967 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700968 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700969 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100970 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700971 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800972 }
973
Elliott Hughes253c18d2015-03-18 22:47:09 -0700974 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800975
Elliott Hughesacdbe922015-06-02 21:37:05 -0700976 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700977 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700978 if (fd == -1) {
979 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700980 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700981 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100982 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700983 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700984 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700985 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -0700986 int rc = load_buf_fd(transport, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700987 if (rc) die("cannot load %s from flash", images[i].img_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700988
989 auto update = [&](const std::string &partition) {
990 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -0700991 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700992 fb_queue_erase(partition.c_str());
993 }
994 flash_buf(partition.c_str(), &buf);
995 /* not closing the fd here since the sparse code keeps the fd around
996 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
997 * program exits.
998 */
999 };
David Pursell0b156632015-10-30 11:22:01 -07001000 do_for_partitions(transport, images[i].part_name, slot_override, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001001 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001002
1003 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001004}
1005
Elliott Hughesfc797672015-04-07 20:12:50 -07001006static void do_send_signature(char* fn) {
1007 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001008 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001009
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001010 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001011
Elliott Hughesfc797672015-04-07 20:12:50 -07001012 strcpy(xtn, ".sig");
1013
1014 int64_t sz;
1015 void* data = load_file(fn, &sz);
1016 strcpy(xtn, ".img");
1017 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001018 fb_queue_download("signature", data, sz);
1019 fb_queue_command("signature", "installing signature");
1020}
1021
David Pursell0b156632015-10-30 11:22:01 -07001022static void do_flashall(Transport* transport, const char* slot_override, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001023 queue_info_dump();
1024
Wink Savilleb98762f2011-04-04 17:54:59 -07001025 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1026
Elliott Hughes253c18d2015-03-18 22:47:09 -07001027 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -07001028 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001029
Elliott Hughesfc797672015-04-07 20:12:50 -07001030 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001031 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001032 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001033
1034 setup_requirements(reinterpret_cast<char*>(data), sz);
1035
1036 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001037 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001038 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -07001039 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001040 if (images[i].is_optional)
1041 continue;
1042 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001043 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001044
1045 auto flashall = [&](const std::string &partition) {
1046 do_send_signature(fname);
David Pursell0b156632015-10-30 11:22:01 -07001047 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001048 fb_queue_erase(partition.c_str());
1049 }
1050 flash_buf(partition.c_str(), &buf);
1051 };
David Pursell0b156632015-10-30 11:22:01 -07001052 do_for_partitions(transport, images[i].part_name, slot_override, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001053 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001054}
1055
1056#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001057#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001058
Elliott Hughes45be42e2015-09-02 20:15:48 -07001059static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001060{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001061 if (argc <= 2) return 0;
1062 skip(2);
1063
1064 /*
1065 * Process unlock_bootloader, we have to load the message file
1066 * and send that to the remote device.
1067 */
1068 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001069
1070 int64_t sz;
1071 void* data = load_file(*argv, &sz);
1072 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001073 fb_queue_download("unlock_message", data, sz);
1074 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1075 skip(1);
1076 return 0;
1077}
1078
Elliott Hughes45be42e2015-09-02 20:15:48 -07001079static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001080{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001081 char command[256];
1082 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001083
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001084 command[0] = 0;
1085 while(1) {
1086 strcat(command,*argv);
1087 skip(1);
1088 if(argc == 0) break;
1089 strcat(command," ");
1090 }
1091
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001092 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001093 return 0;
1094}
1095
Colin Crossf8387882012-05-24 17:18:41 -07001096static int64_t parse_num(const char *arg)
1097{
1098 char *endptr;
1099 unsigned long long num;
1100
1101 num = strtoull(arg, &endptr, 0);
1102 if (endptr == arg) {
1103 return -1;
1104 }
1105
1106 if (*endptr == 'k' || *endptr == 'K') {
1107 if (num >= (-1ULL) / 1024) {
1108 return -1;
1109 }
1110 num *= 1024LL;
1111 endptr++;
1112 } else if (*endptr == 'm' || *endptr == 'M') {
1113 if (num >= (-1ULL) / (1024 * 1024)) {
1114 return -1;
1115 }
1116 num *= 1024LL * 1024LL;
1117 endptr++;
1118 } else if (*endptr == 'g' || *endptr == 'G') {
1119 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1120 return -1;
1121 }
1122 num *= 1024LL * 1024LL * 1024LL;
1123 endptr++;
1124 }
1125
1126 if (*endptr != '\0') {
1127 return -1;
1128 }
1129
1130 if (num > INT64_MAX) {
1131 return -1;
1132 }
1133
1134 return num;
1135}
1136
David Pursell0b156632015-10-30 11:22:01 -07001137static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001138 const char* partition, int skip_if_not_supported,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001139 const char* type_override, const char* size_override,
1140 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001141 std::string partition_type, partition_size;
1142
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001143 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001144 const char* errMsg = nullptr;
1145 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001146 int fd;
1147
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001148 unsigned int limit = INT_MAX;
1149 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001150 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001151 }
1152 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001153 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001154 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001155
David Pursell0b156632015-10-30 11:22:01 -07001156 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001157 errMsg = "Can't determine partition type.\n";
1158 goto failed;
1159 }
JP Abgrall7e859742014-05-06 15:14:15 -07001160 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001161 if (partition_type != type_override) {
1162 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1163 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001164 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001165 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001166 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001167
David Pursell0b156632015-10-30 11:22:01 -07001168 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001169 errMsg = "Unable to get partition size\n";
1170 goto failed;
1171 }
JP Abgrall7e859742014-05-06 15:14:15 -07001172 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001173 if (partition_size != size_override) {
1174 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1175 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001176 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001177 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001178 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001179 // Some bootloaders (angler, for example), send spurious leading whitespace.
1180 partition_size = android::base::Trim(partition_size);
1181 // Some bootloaders (hammerhead, for example) use implicit hex.
1182 // This code used to use strtol with base 16.
1183 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001184
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001185 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001186 if (!gen) {
1187 if (skip_if_not_supported) {
1188 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001189 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001190 return;
1191 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001192 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1193 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001194 return;
1195 }
1196
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001197 int64_t size;
1198 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1199 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1200 return;
1201 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001202
1203 fd = fileno(tmpfile());
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001204 if (fs_generator_generate(gen, fd, size, initial_dir)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001205 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001206 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001207 return;
1208 }
1209
David Pursell0b156632015-10-30 11:22:01 -07001210 if (load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001211 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001212 close(fd);
1213 return;
1214 }
1215 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001216 return;
1217
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001218failed:
1219 if (skip_if_not_supported) {
1220 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001221 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001222 }
1223 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
1224}
1225
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001226int main(int argc, char **argv)
1227{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001228 bool wants_wipe = false;
1229 bool wants_reboot = false;
1230 bool wants_reboot_bootloader = false;
1231 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001232 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001233 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001234 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001235 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001236 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001237 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001238 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001239
JP Abgrall7b8970c2013-03-07 17:06:41 -08001240 const struct option longopts[] = {
1241 {"base", required_argument, 0, 'b'},
1242 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001243 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001244 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001245 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001246 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001247 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001248 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001249 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001250 {"help", no_argument, 0, 'h'},
1251 {"unbuffered", no_argument, 0, 0},
1252 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001253 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001254 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001255 {"set-active", optional_argument, 0, 'a'},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001256#if !defined(_WIN32)
1257 {"wipe-and-use-fbe", no_argument, 0, 0},
1258#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001259 {0, 0, 0, 0}
1260 };
Colin Cross8879f982012-05-22 17:53:34 -07001261
1262 serial = getenv("ANDROID_SERIAL");
1263
1264 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001265 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 -07001266 if (c < 0) {
1267 break;
1268 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001269 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001270 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001271 case 'a':
1272 wants_set_active = true;
1273 if (optarg)
1274 next_active = optarg;
1275 break;
Colin Cross8879f982012-05-22 17:53:34 -07001276 case 'b':
1277 base_addr = strtoul(optarg, 0, 16);
1278 break;
Colin Cross8879f982012-05-22 17:53:34 -07001279 case 'c':
1280 cmdline = optarg;
1281 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001282 case 'h':
1283 usage();
1284 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001285 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001286 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001287 unsigned long val;
1288
1289 val = strtoul(optarg, &endptr, 0);
1290 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1291 die("invalid vendor id '%s'", optarg);
1292 vendor_id = (unsigned short)val;
1293 break;
1294 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001295 case 'k':
1296 kernel_offset = strtoul(optarg, 0, 16);
1297 break;
1298 case 'l':
1299 long_listing = 1;
1300 break;
1301 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001302 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001303 if (!page_size) die("invalid page size");
1304 break;
1305 case 'p':
1306 product = optarg;
1307 break;
1308 case 'r':
1309 ramdisk_offset = strtoul(optarg, 0, 16);
1310 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001311 case 't':
1312 tags_offset = strtoul(optarg, 0, 16);
1313 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001314 case 's':
1315 serial = optarg;
1316 break;
1317 case 'S':
1318 sparse_limit = parse_num(optarg);
1319 if (sparse_limit < 0) {
1320 die("invalid sparse limit");
1321 }
1322 break;
1323 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001324 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001325 break;
1326 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001327 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001328 break;
Colin Cross8879f982012-05-22 17:53:34 -07001329 case '?':
1330 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001331 case 0:
1332 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001333 setvbuf(stdout, nullptr, _IONBF, 0);
1334 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001335 } else if (strcmp("version", longopts[longindex].name) == 0) {
1336 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1337 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001338 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1339 slot_override = std::string(optarg);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001340#if !defined(_WIN32)
1341 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1342 wants_wipe = true;
1343 set_fbe_marker = true;
1344#endif
1345 } else {
1346 fprintf(stderr, "Internal error in options processing for %s\n",
1347 longopts[longindex].name);
1348 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001349 }
1350 break;
Colin Cross8879f982012-05-22 17:53:34 -07001351 default:
1352 abort();
1353 }
1354 }
1355
1356 argc -= optind;
1357 argv += optind;
1358
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001359 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001360 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001361 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001362 }
1363
Colin Cross8fb6e062012-07-24 16:36:41 -07001364 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001365 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001366 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001367 return 0;
1368 }
1369
Colin Crossc7b75dc2012-08-29 18:17:06 -07001370 if (argc > 0 && !strcmp(*argv, "help")) {
1371 usage();
1372 return 0;
1373 }
1374
David Pursell0b156632015-10-30 11:22:01 -07001375 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001376 if (transport == nullptr) {
1377 return 1;
1378 }
1379
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001380 if (slot_override != "")
David Pursell0b156632015-10-30 11:22:01 -07001381 slot_override = verify_slot(transport, slot_override.c_str());
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001382 if (next_active != "")
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001383 next_active = verify_slot(transport, next_active.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001384
1385 if (wants_set_active) {
1386 if (next_active == "") {
1387 if (slot_override == "") {
1388 wants_set_active = false;
1389 } else {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001390 next_active = verify_slot(transport, slot_override.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001391 }
1392 }
1393 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001394
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001395 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001396 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001397 require(2);
1398 fb_queue_display(argv[1], argv[1]);
1399 skip(2);
1400 } else if(!strcmp(*argv, "erase")) {
1401 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001402
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001403 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001404 std::string partition_type;
1405 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001406 fs_get_generator(partition_type) != nullptr) {
1407 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1408 partition_type.c_str());
1409 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001410
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001411 fb_queue_erase(partition.c_str());
1412 };
David Pursell0b156632015-10-30 11:22:01 -07001413 do_for_partitions(transport, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001414 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001415 } else if(!strncmp(*argv, "format", strlen("format"))) {
1416 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001417 char *type_override = nullptr;
1418 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001419 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001420 /*
1421 * Parsing for: "format[:[type][:[size]]]"
1422 * Some valid things:
1423 * - select ontly the size, and leave default fs type:
1424 * format::0x4000000 userdata
1425 * - default fs type and size:
1426 * format userdata
1427 * format:: userdata
1428 */
1429 overrides = strchr(*argv, ':');
1430 if (overrides) {
1431 overrides++;
1432 size_override = strchr(overrides, ':');
1433 if (size_override) {
1434 size_override[0] = '\0';
1435 size_override++;
1436 }
1437 type_override = overrides;
1438 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001439 if (type_override && !type_override[0]) type_override = nullptr;
1440 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001441
1442 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001443 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001444 fb_queue_erase(partition.c_str());
1445 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001446 fb_perform_format(transport, partition.c_str(), 0,
1447 type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001448 };
David Pursell0b156632015-10-30 11:22:01 -07001449 do_for_partitions(transport, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001450 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001451 } else if(!strcmp(*argv, "signature")) {
1452 require(2);
1453 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001454 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001455 if (sz != 256) die("signature must be 256 bytes");
1456 fb_queue_download("signature", data, sz);
1457 fb_queue_command("signature", "installing signature");
1458 skip(2);
1459 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001460 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001461 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001462 if (argc > 0) {
1463 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001464 wants_reboot = false;
1465 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001466 skip(1);
1467 }
1468 }
1469 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001470 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001471 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001472 skip(1);
1473 } else if (!strcmp(*argv, "continue")) {
1474 fb_queue_command("continue", "resuming boot");
1475 skip(1);
1476 } else if(!strcmp(*argv, "boot")) {
1477 char *kname = 0;
1478 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001479 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001480 skip(1);
1481 if (argc > 0) {
1482 kname = argv[0];
1483 skip(1);
1484 }
1485 if (argc > 0) {
1486 rname = argv[0];
1487 skip(1);
1488 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001489 if (argc > 0) {
1490 sname = argv[0];
1491 skip(1);
1492 }
1493 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001494 if (data == 0) return 1;
1495 fb_queue_download("boot.img", data, sz);
1496 fb_queue_command("boot", "booting");
1497 } else if(!strcmp(*argv, "flash")) {
1498 char *pname = argv[1];
1499 char *fname = 0;
1500 require(2);
1501 if (argc > 2) {
1502 fname = argv[2];
1503 skip(3);
1504 } else {
1505 fname = find_item(pname, product);
1506 skip(2);
1507 }
1508 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001509
1510 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001511 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001512 fb_queue_erase(partition.c_str());
1513 }
David Pursell0b156632015-10-30 11:22:01 -07001514 do_flash(transport, partition.c_str(), fname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001515 };
David Pursell0b156632015-10-30 11:22:01 -07001516 do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001517 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001518 char *kname = argv[2];
1519 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001520 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001521 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001522 skip(3);
1523 if (argc > 0) {
1524 rname = argv[0];
1525 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001526 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001527 if (argc > 0) {
1528 sname = argv[0];
1529 skip(1);
1530 }
1531 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001532 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001533 auto flashraw = [&](const std::string &partition) {
1534 fb_queue_flash(partition.c_str(), data, sz);
1535 };
David Pursell0b156632015-10-30 11:22:01 -07001536 do_for_partitions(transport, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001537 } else if(!strcmp(*argv, "flashall")) {
1538 skip(1);
David Pursell0b156632015-10-30 11:22:01 -07001539 do_flashall(transport, slot_override.c_str(), erase_first);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001540 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001541 } else if(!strcmp(*argv, "update")) {
1542 if (argc > 1) {
David Pursell0b156632015-10-30 11:22:01 -07001543 do_update(transport, argv[1], slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001544 skip(2);
1545 } else {
David Pursell0b156632015-10-30 11:22:01 -07001546 do_update(transport, "update.zip", slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001547 skip(1);
1548 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001549 wants_reboot = 1;
1550 } else if(!strcmp(*argv, "set_active")) {
1551 require(2);
1552 std::string slot = verify_slot(transport, argv[1], false);
1553 fb_set_active(slot.c_str());
1554 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001555 } else if(!strcmp(*argv, "oem")) {
1556 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001557 } else if(!strcmp(*argv, "flashing")) {
1558 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1559 !strcmp(*(argv+1), "lock") ||
1560 !strcmp(*(argv+1), "unlock_critical") ||
1561 !strcmp(*(argv+1), "lock_critical") ||
1562 !strcmp(*(argv+1), "get_unlock_ability") ||
1563 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1564 !strcmp(*(argv+1), "lock_bootloader"))) {
1565 argc = do_oem_command(argc, argv);
1566 } else
1567 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1568 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001569 } else {
1570 usage();
1571 return 1;
1572 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001573 } else {
1574 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001575 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001576 }
1577 }
1578
1579 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001580 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001581 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001582 if (set_fbe_marker) {
1583 fprintf(stderr, "setting FBE marker...\n");
1584 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1585 if (initial_userdata_dir.empty()) {
1586 return 1;
1587 }
1588 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
1589 delete_fbemarker_tmpdir(initial_userdata_dir);
1590 } else {
1591 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
1592 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001593
1594 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001595 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001596 fprintf(stderr, "wiping cache...\n");
1597 fb_queue_erase("cache");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001598 fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001599 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001600 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001601 if (wants_set_active) {
1602 fb_set_active(next_active.c_str());
1603 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001604 if (wants_reboot) {
1605 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001606 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001607 } else if (wants_reboot_bootloader) {
1608 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001609 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001610 }
1611
David Pursell0b156632015-10-30 11:22:01 -07001612 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001613}