blob: 41d60119b4fad8e006f95a41457a20305dae78fe [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Colin Crossf8387882012-05-24 17:18:41 -070029#define _LARGEFILE64_SOURCE
30
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070031#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include <errno.h>
33#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070034#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070035#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070036#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070037#include <stdint.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070043#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070044#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080045
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070046#include <functional>
Josh Gao9da9ac52016-01-19 14:50:18 -080047#include <utility>
48#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070049
Elliott Hughes4f713192015-12-04 22:00:26 -080050#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080051#include <android-base/parsenetaddress.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/strings.h>
Colin Crossf8387882012-05-24 17:18:41 -070053#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070054#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055
Elliott Hughes253c18d2015-03-18 22:47:09 -070056#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080057#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070059#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080060#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070061#include "transport.h"
David Pursell5a0ec812016-02-05 15:35:09 -080062#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070063#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064
Colin Crossf8387882012-05-24 17:18:41 -070065#ifndef O_BINARY
66#define O_BINARY 0
67#endif
68
Rom Lemarchand622810c2013-06-28 09:54:59 -070069#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
70
Wink Savilleb98762f2011-04-04 17:54:59 -070071char cur_product[FB_RESPONSE_SZ + 1];
72
David Pursell2ec418a2016-01-20 08:32:08 -080073static const char* serial = nullptr;
74static const char* product = nullptr;
75static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070077static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070078static int64_t sparse_limit = -1;
79static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080
Elliott Hughesfc797672015-04-07 20:12:50 -070081static unsigned page_size = 2048;
82static unsigned base_addr = 0x10000000;
83static unsigned kernel_offset = 0x00008000;
84static unsigned ramdisk_offset = 0x01000000;
85static unsigned second_offset = 0x00f00000;
86static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080087
Paul Crowley8f7f56e2015-11-27 09:29:37 +000088static const std::string convert_fbe_marker_filename("convert_fbe");
89
Rom Lemarchand622810c2013-06-28 09:54:59 -070090enum fb_buffer_type {
91 FB_BUFFER,
92 FB_BUFFER_SPARSE,
93};
94
95struct fastboot_buffer {
96 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070097 void* data;
98 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070099};
100
101static struct {
Alex Light6c60ea82016-06-14 14:29:09 -0700102 char img_name[17];
103 char sig_name[17];
104 char item_name[17];
Rom Lemarchand622810c2013-06-28 09:54:59 -0700105 char part_name[9];
Alex Light6c60ea82016-06-14 14:29:09 -0700106 bool active_slot;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700107 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700108} images[] = {
Alex Light6c60ea82016-06-14 14:29:09 -0700109 {"boot.img", "boot.sig", "boot", "boot", true, false},
110 {"boot_other.img", "boot_other.sig", "boot_other", "boot", false, true},
111 {"recovery.img", "recovery.sig", "recovery", "recovery", true, true},
112 {"system.img", "system.sig", "system", "system", true, false},
113 {"system_other.img", "system_other.sig", "system_other", "system", false, true},
114 {"vendor.img", "vendor.sig", "vendor", "vendor", true, true},
115 {"vendor_other.img", "vendor_other.sig", "vendor_other", "vendor", false, true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700116};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700117
Elliott Hughesfc797672015-04-07 20:12:50 -0700118static char* find_item(const char* item, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700120 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121 char path[PATH_MAX + 128];
122
123 if(!strcmp(item,"boot")) {
124 fn = "boot.img";
125 } else if(!strcmp(item,"recovery")) {
126 fn = "recovery.img";
127 } else if(!strcmp(item,"system")) {
128 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700129 } else if(!strcmp(item,"vendor")) {
130 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131 } else if(!strcmp(item,"userdata")) {
132 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700133 } else if(!strcmp(item,"cache")) {
134 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 } else if(!strcmp(item,"info")) {
136 fn = "android-info.txt";
Alex Light6c60ea82016-06-14 14:29:09 -0700137 } else if(!strcmp(item,"system_other")) {
138 fn = "system_other.img";
139 } else if(!strcmp(item,"boot_other")) {
140 fn = "boot_other.img";
141 } else if(!strcmp(item,"vendor_other")) {
142 fn = "vendor_other.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 } else {
144 fprintf(stderr,"unknown partition '%s'\n", item);
145 return 0;
146 }
147
148 if(product) {
149 get_my_path(path);
150 sprintf(path + strlen(path),
151 "../../../target/product/%s/%s", product, fn);
152 return strdup(path);
153 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800154
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155 dir = getenv("ANDROID_PRODUCT_OUT");
156 if((dir == 0) || (dir[0] == 0)) {
157 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
158 return 0;
159 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800160
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161 sprintf(path, "%s/%s", dir, fn);
162 return strdup(path);
163}
164
Elliott Hughesfc797672015-04-07 20:12:50 -0700165static int64_t get_file_size(int fd) {
166 struct stat sb;
167 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700168}
169
Elliott Hughesfc797672015-04-07 20:12:50 -0700170static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800171 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700172 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173
Elliott Hughesfc797672015-04-07 20:12:50 -0700174 *sz = get_file_size(fd);
175 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700176 goto oops;
177 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178
Elliott Hughesfc797672015-04-07 20:12:50 -0700179 data = (char*) malloc(*sz);
180 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181
Elliott Hughesfc797672015-04-07 20:12:50 -0700182 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183 close(fd);
184
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185 return data;
186
187oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800188 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 close(fd);
190 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800191 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 return 0;
193}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194
Elliott Hughesfc797672015-04-07 20:12:50 -0700195static void* load_file(const char* fn, int64_t* sz) {
196 int fd = open(fn, O_RDONLY | O_BINARY);
197 if (fd == -1) return nullptr;
198 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700199}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200
Elliott Hughesfc797672015-04-07 20:12:50 -0700201static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700202 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700203 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700204 return -1;
205 }
206
207 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
208 return -1;
209 }
210
Scott Anderson13081c62012-04-06 12:39:30 -0700211 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700213 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
214 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215 return 0;
216}
217
Elliott Hughesfc797672015-04-07 20:12:50 -0700218static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800219 return match_fastboot_with_serial(info, serial);
220}
221
Elliott Hughesfc797672015-04-07 20:12:50 -0700222static int list_devices_callback(usb_ifc_info* info) {
223 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800224 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700225 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800226 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700227 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228 if (!serial[0]) {
229 serial = "????????????";
230 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700231 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700232 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800233 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700234 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800235 printf("%-22s fastboot", serial.c_str());
236 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700237 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800238 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 }
240
241 return -1;
242}
243
David Pursell2ec418a2016-01-20 08:32:08 -0800244// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
245// a specific device, otherwise the first USB device found will be used.
246//
247// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
248// Otherwise it blocks until the target is available.
249//
250// The returned Transport is a singleton, so multiple calls to this function will return the same
251// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700252static Transport* open_device() {
253 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800254 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255
David Pursell2ec418a2016-01-20 08:32:08 -0800256 if (transport != nullptr) {
257 return transport;
258 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800259
David Pursell5a0ec812016-02-05 15:35:09 -0800260 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800261 std::string host;
David Pursell5a0ec812016-02-05 15:35:09 -0800262 int port = 0;
263 if (serial != nullptr) {
264 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800265
David Pursell5a0ec812016-02-05 15:35:09 -0800266 if (android::base::StartsWith(serial, "tcp:")) {
267 protocol = Socket::Protocol::kTcp;
268 port = tcp::kDefaultPort;
269 net_address = serial + strlen("tcp:");
270 } else if (android::base::StartsWith(serial, "udp:")) {
271 protocol = Socket::Protocol::kUdp;
272 port = udp::kDefaultPort;
273 net_address = serial + strlen("udp:");
274 }
275
276 if (net_address != nullptr) {
277 std::string error;
278 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
279 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
280 error.c_str());
281 return nullptr;
282 }
David Pursell2ec418a2016-01-20 08:32:08 -0800283 }
284 }
285
286 while (true) {
287 if (!host.empty()) {
288 std::string error;
David Pursell5a0ec812016-02-05 15:35:09 -0800289 if (protocol == Socket::Protocol::kTcp) {
290 transport = tcp::Connect(host, port, &error).release();
291 } else if (protocol == Socket::Protocol::kUdp) {
292 transport = udp::Connect(host, port, &error).release();
293 }
294
David Pursell2ec418a2016-01-20 08:32:08 -0800295 if (transport == nullptr && announce) {
296 fprintf(stderr, "error: %s\n", error.c_str());
297 }
298 } else {
299 transport = usb_open(match_fastboot);
300 }
301
302 if (transport != nullptr) {
303 return transport;
304 }
305
David Pursell0b156632015-10-30 11:22:01 -0700306 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800307 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700308 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700310 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311 }
312}
313
Elliott Hughesfc797672015-04-07 20:12:50 -0700314static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 // We don't actually open a USB device here,
316 // just getting our callback called so we can
317 // list all the connected devices.
318 usb_open(list_devices_callback);
319}
320
Elliott Hughesfc797672015-04-07 20:12:50 -0700321static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 fprintf(stderr,
323/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
324 "usage: fastboot [ <option> ] <command>\n"
325 "\n"
326 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700327 " update <filename> Reflash device from update.zip.\n"
328 " flashall Flash boot, system, vendor, and --\n"
329 " if found -- recovery.\n"
330 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
331 " flashing lock Locks the device. Prevents flashing.\n"
332 " flashing unlock Unlocks the device. Allows flashing\n"
333 " any partition except\n"
334 " bootloader-related partitions.\n"
335 " flashing lock_critical Prevents flashing bootloader-related\n"
336 " partitions.\n"
337 " flashing unlock_critical Enables flashing bootloader-related\n"
338 " partitions.\n"
339 " flashing get_unlock_ability Queries bootloader to see if the\n"
340 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700341 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700342 " unlock nonce.\n"
343 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700344 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700345 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700346 " erase <partition> Erase a flash partition.\n"
347 " format[:[<fs type>][:[<size>]] <partition>\n"
348 " Format a flash partition. Can\n"
349 " override the fs type and/or size\n"
350 " the bootloader reports.\n"
351 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800352 " set_active <suffix> Sets the active slot. If slots are\n"
353 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700354 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
355 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
356 " Create bootimage and flash it.\n"
357 " devices [-l] List all connected devices [with\n"
358 " device paths].\n"
359 " continue Continue with autoboot.\n"
360 " reboot [bootloader] Reboot device [into bootloader].\n"
361 " reboot-bootloader Reboot device into bootloader.\n"
362 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 "\n"
364 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700365 " -w Erase userdata and cache (and format\n"
366 " if supported by partition type).\n"
367 " -u Do not erase partition before\n"
368 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800369 " -s <specific device> Specify a device. For USB, provide either\n"
370 " a serial number or path to device port.\n"
David Pursell5a0ec812016-02-05 15:35:09 -0800371 " For ethernet, provide an address in the"
372 " form <protocol>:<hostname>[:port] where"
373 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700374 " -p <product> Specify product name.\n"
375 " -c <cmdline> Override kernel commandline.\n"
376 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800377 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700378 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800379 " --kernel-offset Specify a custom kernel offset.\n"
380 " (default: 0x00008000)\n"
381 " --ramdisk-offset Specify a custom ramdisk offset.\n"
382 " (default: 0x01000000)\n"
383 " --tags-offset Specify a custom tags offset.\n"
384 " (default: 0x00000100)\n"
385 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700386 " (default: 2048).\n"
387 " -S <size>[K|M|G] Automatically sparse files greater\n"
388 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700389 " --slot <suffix> Specify slot suffix to be used if the\n"
390 " device supports slots. This will be\n"
391 " added to all partition names that use\n"
392 " slots. 'all' can be given to refer\n"
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800393 " to all slots. 'other' can be given to\n"
Alex Light6c60ea82016-06-14 14:29:09 -0700394 " refer to a non-current slot. 'active' can\n"
395 " be given to refer to a current slot only.\n"
396 " If this flag is not given slots will be\n"
397 " written to based on the filename.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800398 " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800399 " provided, this will default to the value\n"
400 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800401 " supported, this does nothing. This will\n"
402 " run after all non-reboot commands.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000403#if !defined(_WIN32)
404 " --wipe-and-use-fbe On devices which support it,\n"
405 " erase userdata and cache, and\n"
406 " enable file-based encryption\n"
407#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800408 " --unbuffered Do not buffer input or output.\n"
409 " --version Display version.\n"
410 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000413
Elliott Hughesfc797672015-04-07 20:12:50 -0700414static void* load_bootable_image(const char* kernel, const char* ramdisk,
415 const char* secondstage, int64_t* sz,
416 const char* cmdline) {
417 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 fprintf(stderr, "no image specified\n");
419 return 0;
420 }
421
Elliott Hughesfc797672015-04-07 20:12:50 -0700422 int64_t ksize;
423 void* kdata = load_file(kernel, &ksize);
424 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800425 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 return 0;
427 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800428
Elliott Hughesfc797672015-04-07 20:12:50 -0700429 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700431 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800432
Elliott Hughesfc797672015-04-07 20:12:50 -0700433 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800434 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
435 return 0;
436 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800437
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438 *sz = ksize;
439 return kdata;
440 }
441
Elliott Hughesfc797672015-04-07 20:12:50 -0700442 void* rdata = nullptr;
443 int64_t rsize = 0;
444 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800445 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700446 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800447 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 return 0;
449 }
450 }
451
Elliott Hughesfc797672015-04-07 20:12:50 -0700452 void* sdata = nullptr;
453 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200454 if (secondstage) {
455 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700456 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200457 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
458 return 0;
459 }
460 }
461
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700463 int64_t bsize = 0;
464 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800465 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200466 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800467 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700468 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 fprintf(stderr,"failed to create boot.img\n");
470 return 0;
471 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700472 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
473 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800475
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476 return bdata;
477}
478
Elliott Hughesfc797672015-04-07 20:12:50 -0700479static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480{
Yusuke Sato07447542015-06-25 14:39:19 -0700481 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700482 ZipEntry zip_entry;
483 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700484 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000485 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800486 }
487
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700488 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800489
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700490 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700491 if (data == nullptr) {
492 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000493 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494 }
495
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700496 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
497 if (error != 0) {
498 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000500 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000502
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 return data;
504}
505
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700506#if defined(_WIN32)
507
508// TODO: move this to somewhere it can be shared.
509
510#include <windows.h>
511
512// Windows' tmpfile(3) requires administrator rights because
513// it creates temporary files in the root directory.
514static FILE* win32_tmpfile() {
515 char temp_path[PATH_MAX];
516 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
517 if (nchars == 0 || nchars >= sizeof(temp_path)) {
518 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
519 return nullptr;
520 }
521
522 char filename[PATH_MAX];
523 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
524 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
525 return nullptr;
526 }
527
528 return fopen(filename, "w+bTD");
529}
530
531#define tmpfile win32_tmpfile
532
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000533static std::string make_temporary_directory() {
534 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
535 return "";
536}
537
538#else
539
540static std::string make_temporary_directory() {
541 const char *tmpdir = getenv("TMPDIR");
542 if (tmpdir == nullptr) {
543 tmpdir = P_tmpdir;
544 }
545 std::string result = std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
546 if (mkdtemp(&result[0]) == NULL) {
547 fprintf(stderr, "Unable to create temporary directory: %s\n",
548 strerror(errno));
549 return "";
550 }
551 return result;
552}
553
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700554#endif
555
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000556static std::string create_fbemarker_tmpdir() {
557 std::string dir = make_temporary_directory();
558 if (dir.empty()) {
559 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
560 return "";
561 }
562 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
563 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
564 if (fd == -1) {
565 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
566 marker_file.c_str(), errno, strerror(errno));
567 return "";
568 }
569 close(fd);
570 return dir;
571}
572
573static void delete_fbemarker_tmpdir(const std::string& dir) {
574 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
575 if (unlink(marker_file.c_str()) == -1) {
576 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
577 marker_file.c_str(), errno, strerror(errno));
578 return;
579 }
580 if (rmdir(dir.c_str()) == -1) {
581 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
582 dir.c_str(), errno, strerror(errno));
583 return;
584 }
585}
586
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700587static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
588 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700589 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700590 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
591 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700592 return -1;
593 }
594
Yusuke Sato07447542015-06-25 14:39:19 -0700595 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700596 ZipEntry zip_entry;
597 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
598 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000599 return -1;
600 }
601
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700602 int fd = fileno(fp);
603 int error = ExtractEntryToFile(zip, &zip_entry, fd);
604 if (error != 0) {
605 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
606 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700607 }
608
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000609 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700610 return fd;
611}
612
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800613static char *strip(char *s)
614{
615 int n;
616 while(*s && isspace(*s)) s++;
617 n = strlen(s);
618 while(n-- > 0) {
619 if(!isspace(s[n])) break;
620 s[n] = 0;
621 }
622 return s;
623}
624
625#define MAX_OPTIONS 32
626static int setup_requirement_line(char *name)
627{
628 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700629 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800630 unsigned n, count;
631 char *x;
632 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800633
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800634 if (!strncmp(name, "reject ", 7)) {
635 name += 7;
636 invert = 1;
637 } else if (!strncmp(name, "require ", 8)) {
638 name += 8;
639 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700640 } else if (!strncmp(name, "require-for-product:", 20)) {
641 // Get the product and point name past it
642 prod = name + 20;
643 name = strchr(name, ' ');
644 if (!name) return -1;
645 *name = 0;
646 name += 1;
647 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800648 }
649
650 x = strchr(name, '=');
651 if (x == 0) return 0;
652 *x = 0;
653 val[0] = x + 1;
654
655 for(count = 1; count < MAX_OPTIONS; count++) {
656 x = strchr(val[count - 1],'|');
657 if (x == 0) break;
658 *x = 0;
659 val[count] = x + 1;
660 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800661
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800662 name = strip(name);
663 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800664
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800665 name = strip(name);
666 if (name == 0) return -1;
667
Elliott Hughes253c18d2015-03-18 22:47:09 -0700668 const char* var = name;
669 // Work around an unfortunate name mismatch.
670 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800671
Elliott Hughes253c18d2015-03-18 22:47:09 -0700672 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800673 if (out == 0) return -1;
674
675 for(n = 0; n < count; n++) {
676 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700677 if (out[n] == 0) {
678 for(size_t i = 0; i < n; ++i) {
679 free((char*) out[i]);
680 }
681 free(out);
682 return -1;
683 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800684 }
685
Elliott Hughes253c18d2015-03-18 22:47:09 -0700686 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800687 return 0;
688}
689
Elliott Hughesfc797672015-04-07 20:12:50 -0700690static void setup_requirements(char* data, int64_t sz) {
691 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700693 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694 *s++ = 0;
695 if (setup_requirement_line(data)) {
696 die("out of memory");
697 }
698 data = s;
699 } else {
700 s++;
701 }
702 }
703}
704
Elliott Hughesfc797672015-04-07 20:12:50 -0700705static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800706 fb_queue_notice("--------------------------------------------");
707 fb_queue_display("version-bootloader", "Bootloader Version...");
708 fb_queue_display("version-baseband", "Baseband Version.....");
709 fb_queue_display("serialno", "Serial Number........");
710 fb_queue_notice("--------------------------------------------");
711}
712
Rom Lemarchand622810c2013-06-28 09:54:59 -0700713static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700714{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700715 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700716 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700717 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700718 }
719
Elliott Hughesfc797672015-04-07 20:12:50 -0700720 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700721 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700722 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700723 }
724
Elliott Hughes253c18d2015-03-18 22:47:09 -0700725 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700726 if (!out_s) {
727 die("Failed to allocate sparse file array\n");
728 }
729
730 files = sparse_file_resparse(s, max_size, out_s, files);
731 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700732 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700733 }
734
735 return out_s;
736}
737
David Pursell0b156632015-10-30 11:22:01 -0700738static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700739 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700740 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
741 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800742 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700743 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700744 }
745
Elliott Hughes77c0e662015-11-02 15:51:12 -0800746 // Some bootloaders (angler, for example) send spurious whitespace too.
747 max_download_size = android::base::Trim(max_download_size);
748
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700749 uint64_t limit;
750 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800751 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700752 return 0;
753 }
754 if (limit > 0) {
755 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
756 }
Colin Crossf8387882012-05-24 17:18:41 -0700757 return limit;
758}
759
David Pursell0b156632015-10-30 11:22:01 -0700760static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700761 int64_t limit;
762
763 if (sparse_limit == 0) {
764 return 0;
765 } else if (sparse_limit > 0) {
766 limit = sparse_limit;
767 } else {
768 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700769 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700770 }
771 if (target_sparse_limit > 0) {
772 limit = target_sparse_limit;
773 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700774 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700775 }
776 }
777
778 if (size > limit) {
779 return limit;
780 }
781
782 return 0;
783}
784
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700785// Until we get lazy inode table init working in make_ext4fs, we need to
786// erase partitions of type ext4 before flashing a filesystem so no stale
787// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700788static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800789 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700790 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800791 return false;
792 }
793 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700794}
795
David Pursell0b156632015-10-30 11:22:01 -0700796static int load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700797 int64_t sz = get_file_size(fd);
798 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000799 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700800 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700801
Elliott Hughesfc797672015-04-07 20:12:50 -0700802 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700803 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700804 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700805 sparse_file** s = load_sparse_files(fd, limit);
806 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700807 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700808 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700809 buf->type = FB_BUFFER_SPARSE;
810 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700811 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700812 void* data = load_fd(fd, &sz);
813 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700814 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700815 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000816 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700817 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700818
819 return 0;
820}
821
David Pursell0b156632015-10-30 11:22:01 -0700822static int load_buf(Transport* transport, const char *fname, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700823{
824 int fd;
825
826 fd = open(fname, O_RDONLY | O_BINARY);
827 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700828 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700829 }
830
David Pursell0b156632015-10-30 11:22:01 -0700831 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700832}
833
834static void flash_buf(const char *pname, struct fastboot_buffer *buf)
835{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700836 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700837
838 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800839 case FB_BUFFER_SPARSE: {
840 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700841 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700842 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700843 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800844 sparse_files.emplace_back(*s, sz);
845 ++s;
846 }
847
848 for (size_t i = 0; i < sparse_files.size(); ++i) {
849 const auto& pair = sparse_files[i];
850 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700851 }
852 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800853 }
854
Rom Lemarchand622810c2013-06-28 09:54:59 -0700855 case FB_BUFFER:
856 fb_queue_flash(pname, buf->data, buf->sz);
857 break;
858 default:
859 die("unknown buffer type: %d", buf->type);
860 }
861}
862
David Pursell0b156632015-10-30 11:22:01 -0700863static std::vector<std::string> get_suffixes(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700864 std::vector<std::string> suffixes;
865 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700866 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700867 die("Could not get suffixes.\n");
868 }
869 return android::base::Split(suffix_list, ",");
870}
871
Alex Light6c60ea82016-06-14 14:29:09 -0700872// Returns a std::string of the slot name 'active_offset' after the active slot
873static std::string get_slot_name(Transport* transport, size_t active_offset) {
874 std::vector<std::string> suffixes = get_suffixes(transport);
875 std::string current_slot;
876 if (!fb_getvar(transport, "current-slot", &current_slot)) {
877 die("Failed to identify current slot.");
878 }
879 if (active_offset >= suffixes.size()) {
880 die("active slot offset larger than total number of slots!");
881 }
882 if (!suffixes.empty()) {
883 for (size_t i = 0; i < suffixes.size(); i++) {
884 if (current_slot == suffixes[i])
885 return suffixes[(i + active_offset) % suffixes.size()];
886 }
887 } else {
888 die("No known slots.");
889 }
890 die("Unable to find current slot");
891 return "";
892}
893
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800894static std::string verify_slot(Transport* transport, const char *slot, bool allow_all) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700895 if (strcmp(slot, "all") == 0) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800896 if (allow_all) {
897 return "all";
898 } else {
899 std::vector<std::string> suffixes = get_suffixes(transport);
900 if (!suffixes.empty()) {
901 return suffixes[0];
902 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800903 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800904 }
905 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700906 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800907
David Pursell0b156632015-10-30 11:22:01 -0700908 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800909
910 if (strcmp(slot, "other") == 0) {
Alex Light6c60ea82016-06-14 14:29:09 -0700911 return get_slot_name(transport, 1);
912 } else if (strcmp(slot, "active") == 0) {
913 return get_slot_name(transport, 0);
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800914 }
915
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700916 for (const std::string &suffix : suffixes) {
917 if (suffix == slot)
918 return slot;
919 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800920 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700921 for (const std::string &suffix : suffixes) {
922 fprintf(stderr, "%s\n", suffix.c_str());
923 }
924 exit(1);
925}
926
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800927static std::string verify_slot(Transport* transport, const char *slot) {
928 return verify_slot(transport, slot, true);
929}
930
David Pursell0b156632015-10-30 11:22:01 -0700931static void do_for_partition(Transport* transport, const char *part, const char *slot,
932 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800933 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700934 std::string current_slot;
935
David Pursell0b156632015-10-30 11:22:01 -0700936 if (!fb_getvar(transport, std::string("has-slot:")+part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800937 /* If has-slot is not supported, the answer is no. */
938 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700939 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800940 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700941 if (!slot || slot[0] == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700942 if (!fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700943 die("Failed to identify current slot.\n");
944 }
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800945 func(std::string(part) + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700946 } else {
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800947 func(std::string(part) + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700948 }
949 } else {
950 if (force_slot && slot && slot[0]) {
David Pursell0b156632015-10-30 11:22:01 -0700951 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
952 part, slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700953 }
954 func(part);
955 }
956}
957
David Pursell0b156632015-10-30 11:22:01 -0700958/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
959 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
960 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
961 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700962 */
David Pursell0b156632015-10-30 11:22:01 -0700963static void do_for_partitions(Transport* transport, const char *part, const char *slot,
964 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800965 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700966
967 if (slot && strcmp(slot, "all") == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700968 if (!fb_getvar(transport, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700969 die("Could not check if partition %s has slot.", part);
970 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800971 if (has_slot == "yes") {
David Pursell0b156632015-10-30 11:22:01 -0700972 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700973 for (std::string &suffix : suffixes) {
David Pursell0b156632015-10-30 11:22:01 -0700974 do_for_partition(transport, part, suffix.c_str(), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700975 }
976 } else {
David Pursell0b156632015-10-30 11:22:01 -0700977 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700978 }
979 } else {
David Pursell0b156632015-10-30 11:22:01 -0700980 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700981 }
982}
983
David Pursell0b156632015-10-30 11:22:01 -0700984static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700985 struct fastboot_buffer buf;
986
David Pursell0b156632015-10-30 11:22:01 -0700987 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700988 die("cannot load '%s'", fname);
989 }
990 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700991}
992
Elliott Hughesfc797672015-04-07 20:12:50 -0700993static void do_update_signature(ZipArchiveHandle zip, char* fn) {
994 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700995 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700996 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800997 fb_queue_download("signature", data, sz);
998 fb_queue_command("signature", "installing signature");
999}
1000
David Pursell0b156632015-10-30 11:22:01 -07001001static 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 -08001002 queue_info_dump();
1003
Wink Savilleb98762f2011-04-04 17:54:59 -07001004 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1005
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001006 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001007 int error = OpenArchive(filename, &zip);
1008 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001009 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001010 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001011 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001012
Elliott Hughesfc797672015-04-07 20:12:50 -07001013 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001014 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001015 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001016 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001017 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001018 }
1019
Elliott Hughes253c18d2015-03-18 22:47:09 -07001020 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001021
Elliott Hughesacdbe922015-06-02 21:37:05 -07001022 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -07001023 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001024 if (fd == -1) {
1025 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001026 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001027 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001028 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001029 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001030 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001031 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -07001032 int rc = load_buf_fd(transport, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001033 if (rc) die("cannot load %s from flash", images[i].img_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001034
1035 auto update = [&](const std::string &partition) {
1036 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001037 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001038 fb_queue_erase(partition.c_str());
1039 }
1040 flash_buf(partition.c_str(), &buf);
1041 /* not closing the fd here since the sparse code keeps the fd around
1042 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
1043 * program exits.
1044 */
1045 };
David Pursell0b156632015-10-30 11:22:01 -07001046 do_for_partitions(transport, images[i].part_name, slot_override, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001047 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001048
1049 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001050}
1051
Elliott Hughesfc797672015-04-07 20:12:50 -07001052static void do_send_signature(char* fn) {
1053 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001054 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001055
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001056 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001057
Elliott Hughesfc797672015-04-07 20:12:50 -07001058 strcpy(xtn, ".sig");
1059
1060 int64_t sz;
1061 void* data = load_file(fn, &sz);
1062 strcpy(xtn, ".img");
1063 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001064 fb_queue_download("signature", data, sz);
1065 fb_queue_command("signature", "installing signature");
1066}
1067
David Pursell0b156632015-10-30 11:22:01 -07001068static void do_flashall(Transport* transport, const char* slot_override, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001069 queue_info_dump();
Alex Light6c60ea82016-06-14 14:29:09 -07001070 const bool has_slot_override = slot_override != nullptr && strcmp(slot_override, "") != 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001071
Wink Savilleb98762f2011-04-04 17:54:59 -07001072 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1073
Elliott Hughes253c18d2015-03-18 22:47:09 -07001074 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -07001075 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001076
Elliott Hughesfc797672015-04-07 20:12:50 -07001077 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001078 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001079 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001080
1081 setup_requirements(reinterpret_cast<char*>(data), sz);
1082
1083 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Alex Light6c60ea82016-06-14 14:29:09 -07001084 if (has_slot_override && !images[i].active_slot) {
1085 // We will not do anything with _other files if we are given an explicit slot to use.
1086 continue;
1087 }
1088 fname = find_item(images[i].item_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001089 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -07001090 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001091 if (images[i].is_optional)
1092 continue;
1093 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001094 }
Alex Light6c60ea82016-06-14 14:29:09 -07001095 // Get the actual slot we are writing to based on the filename. This is because we need to
1096 // sometimes write the 'active + 1'th slot for first boot optimization reasons. This is
1097 // defined by the images array unless one is explicitly provided.
1098 std::string real_slot_override;
1099 if (has_slot_override) {
1100 real_slot_override = slot_override;
1101 } else {
1102 real_slot_override = get_slot_name(transport, images[i].active_slot ? 0 : 1);
1103 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001104
1105 auto flashall = [&](const std::string &partition) {
1106 do_send_signature(fname);
David Pursell0b156632015-10-30 11:22:01 -07001107 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001108 fb_queue_erase(partition.c_str());
1109 }
1110 flash_buf(partition.c_str(), &buf);
1111 };
Alex Light6c60ea82016-06-14 14:29:09 -07001112 do_for_partitions(transport,
1113 images[i].part_name,
1114 real_slot_override.c_str(),
1115 flashall,
1116 false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001117 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001118}
1119
1120#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001121#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001122
Elliott Hughes45be42e2015-09-02 20:15:48 -07001123static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001124{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001125 if (argc <= 2) return 0;
1126 skip(2);
1127
1128 /*
1129 * Process unlock_bootloader, we have to load the message file
1130 * and send that to the remote device.
1131 */
1132 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001133
1134 int64_t sz;
1135 void* data = load_file(*argv, &sz);
1136 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001137 fb_queue_download("unlock_message", data, sz);
1138 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1139 skip(1);
1140 return 0;
1141}
1142
Elliott Hughes45be42e2015-09-02 20:15:48 -07001143static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001144{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001145 char command[256];
1146 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001147
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001148 command[0] = 0;
1149 while(1) {
1150 strcat(command,*argv);
1151 skip(1);
1152 if(argc == 0) break;
1153 strcat(command," ");
1154 }
1155
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001156 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001157 return 0;
1158}
1159
Colin Crossf8387882012-05-24 17:18:41 -07001160static int64_t parse_num(const char *arg)
1161{
1162 char *endptr;
1163 unsigned long long num;
1164
1165 num = strtoull(arg, &endptr, 0);
1166 if (endptr == arg) {
1167 return -1;
1168 }
1169
1170 if (*endptr == 'k' || *endptr == 'K') {
1171 if (num >= (-1ULL) / 1024) {
1172 return -1;
1173 }
1174 num *= 1024LL;
1175 endptr++;
1176 } else if (*endptr == 'm' || *endptr == 'M') {
1177 if (num >= (-1ULL) / (1024 * 1024)) {
1178 return -1;
1179 }
1180 num *= 1024LL * 1024LL;
1181 endptr++;
1182 } else if (*endptr == 'g' || *endptr == 'G') {
1183 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1184 return -1;
1185 }
1186 num *= 1024LL * 1024LL * 1024LL;
1187 endptr++;
1188 }
1189
1190 if (*endptr != '\0') {
1191 return -1;
1192 }
1193
1194 if (num > INT64_MAX) {
1195 return -1;
1196 }
1197
1198 return num;
1199}
1200
David Pursell0b156632015-10-30 11:22:01 -07001201static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001202 const char* partition, int skip_if_not_supported,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001203 const char* type_override, const char* size_override,
1204 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001205 std::string partition_type, partition_size;
1206
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001207 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001208 const char* errMsg = nullptr;
1209 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001210 int fd;
1211
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001212 unsigned int limit = INT_MAX;
1213 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001214 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001215 }
1216 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001217 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001218 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001219
David Pursell0b156632015-10-30 11:22:01 -07001220 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001221 errMsg = "Can't determine partition type.\n";
1222 goto failed;
1223 }
JP Abgrall7e859742014-05-06 15:14:15 -07001224 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001225 if (partition_type != type_override) {
1226 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1227 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001228 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001229 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001230 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001231
David Pursell0b156632015-10-30 11:22:01 -07001232 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001233 errMsg = "Unable to get partition size\n";
1234 goto failed;
1235 }
JP Abgrall7e859742014-05-06 15:14:15 -07001236 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001237 if (partition_size != size_override) {
1238 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1239 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001240 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001241 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001242 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001243 // Some bootloaders (angler, for example), send spurious leading whitespace.
1244 partition_size = android::base::Trim(partition_size);
1245 // Some bootloaders (hammerhead, for example) use implicit hex.
1246 // This code used to use strtol with base 16.
1247 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001248
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001249 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001250 if (!gen) {
1251 if (skip_if_not_supported) {
1252 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001253 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001254 return;
1255 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001256 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1257 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001258 return;
1259 }
1260
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001261 int64_t size;
1262 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1263 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1264 return;
1265 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001266
1267 fd = fileno(tmpfile());
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001268 if (fs_generator_generate(gen, fd, size, initial_dir)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001269 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001270 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001271 return;
1272 }
1273
David Pursell0b156632015-10-30 11:22:01 -07001274 if (load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001275 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001276 close(fd);
1277 return;
1278 }
1279 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001280 return;
1281
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001282failed:
1283 if (skip_if_not_supported) {
1284 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001285 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001286 }
1287 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
1288}
1289
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001290int main(int argc, char **argv)
1291{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001292 bool wants_wipe = false;
1293 bool wants_reboot = false;
1294 bool wants_reboot_bootloader = false;
1295 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001296 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001297 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001298 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001299 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001300 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001301 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001302 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001303
JP Abgrall7b8970c2013-03-07 17:06:41 -08001304 const struct option longopts[] = {
1305 {"base", required_argument, 0, 'b'},
1306 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001307 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001308 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001309 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001310 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001311 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001312 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001313 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001314 {"help", no_argument, 0, 'h'},
1315 {"unbuffered", no_argument, 0, 0},
1316 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001317 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001318 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001319 {"set-active", optional_argument, 0, 'a'},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001320#if !defined(_WIN32)
1321 {"wipe-and-use-fbe", no_argument, 0, 0},
1322#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001323 {0, 0, 0, 0}
1324 };
Colin Cross8879f982012-05-22 17:53:34 -07001325
1326 serial = getenv("ANDROID_SERIAL");
1327
1328 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001329 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 -07001330 if (c < 0) {
1331 break;
1332 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001333 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001334 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001335 case 'a':
1336 wants_set_active = true;
1337 if (optarg)
1338 next_active = optarg;
1339 break;
Colin Cross8879f982012-05-22 17:53:34 -07001340 case 'b':
1341 base_addr = strtoul(optarg, 0, 16);
1342 break;
Colin Cross8879f982012-05-22 17:53:34 -07001343 case 'c':
1344 cmdline = optarg;
1345 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001346 case 'h':
1347 usage();
1348 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001349 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001350 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001351 unsigned long val;
1352
1353 val = strtoul(optarg, &endptr, 0);
1354 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1355 die("invalid vendor id '%s'", optarg);
1356 vendor_id = (unsigned short)val;
1357 break;
1358 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001359 case 'k':
1360 kernel_offset = strtoul(optarg, 0, 16);
1361 break;
1362 case 'l':
1363 long_listing = 1;
1364 break;
1365 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001366 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001367 if (!page_size) die("invalid page size");
1368 break;
1369 case 'p':
1370 product = optarg;
1371 break;
1372 case 'r':
1373 ramdisk_offset = strtoul(optarg, 0, 16);
1374 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001375 case 't':
1376 tags_offset = strtoul(optarg, 0, 16);
1377 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001378 case 's':
1379 serial = optarg;
1380 break;
1381 case 'S':
1382 sparse_limit = parse_num(optarg);
1383 if (sparse_limit < 0) {
1384 die("invalid sparse limit");
1385 }
1386 break;
1387 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001388 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001389 break;
1390 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001391 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001392 break;
Colin Cross8879f982012-05-22 17:53:34 -07001393 case '?':
1394 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001395 case 0:
1396 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001397 setvbuf(stdout, nullptr, _IONBF, 0);
1398 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001399 } else if (strcmp("version", longopts[longindex].name) == 0) {
1400 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1401 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001402 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1403 slot_override = std::string(optarg);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001404#if !defined(_WIN32)
1405 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1406 wants_wipe = true;
1407 set_fbe_marker = true;
1408#endif
1409 } else {
1410 fprintf(stderr, "Internal error in options processing for %s\n",
1411 longopts[longindex].name);
1412 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001413 }
1414 break;
Colin Cross8879f982012-05-22 17:53:34 -07001415 default:
1416 abort();
1417 }
1418 }
1419
1420 argc -= optind;
1421 argv += optind;
1422
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001423 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001424 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001425 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001426 }
1427
Colin Cross8fb6e062012-07-24 16:36:41 -07001428 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001429 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001430 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001431 return 0;
1432 }
1433
Colin Crossc7b75dc2012-08-29 18:17:06 -07001434 if (argc > 0 && !strcmp(*argv, "help")) {
1435 usage();
1436 return 0;
1437 }
1438
David Pursell0b156632015-10-30 11:22:01 -07001439 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001440 if (transport == nullptr) {
1441 return 1;
1442 }
1443
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001444 if (slot_override != "")
David Pursell0b156632015-10-30 11:22:01 -07001445 slot_override = verify_slot(transport, slot_override.c_str());
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001446 if (next_active != "")
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001447 next_active = verify_slot(transport, next_active.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001448
1449 if (wants_set_active) {
1450 if (next_active == "") {
1451 if (slot_override == "") {
1452 wants_set_active = false;
1453 } else {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001454 next_active = verify_slot(transport, slot_override.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001455 }
1456 }
1457 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001458
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001459 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001460 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001461 require(2);
1462 fb_queue_display(argv[1], argv[1]);
1463 skip(2);
1464 } else if(!strcmp(*argv, "erase")) {
1465 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001466
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001467 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001468 std::string partition_type;
1469 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001470 fs_get_generator(partition_type) != nullptr) {
1471 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1472 partition_type.c_str());
1473 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001474
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001475 fb_queue_erase(partition.c_str());
1476 };
David Pursell0b156632015-10-30 11:22:01 -07001477 do_for_partitions(transport, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001478 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001479 } else if(!strncmp(*argv, "format", strlen("format"))) {
1480 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001481 char *type_override = nullptr;
1482 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001483 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001484 /*
1485 * Parsing for: "format[:[type][:[size]]]"
1486 * Some valid things:
1487 * - select ontly the size, and leave default fs type:
1488 * format::0x4000000 userdata
1489 * - default fs type and size:
1490 * format userdata
1491 * format:: userdata
1492 */
1493 overrides = strchr(*argv, ':');
1494 if (overrides) {
1495 overrides++;
1496 size_override = strchr(overrides, ':');
1497 if (size_override) {
1498 size_override[0] = '\0';
1499 size_override++;
1500 }
1501 type_override = overrides;
1502 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001503 if (type_override && !type_override[0]) type_override = nullptr;
1504 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001505
1506 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001507 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001508 fb_queue_erase(partition.c_str());
1509 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001510 fb_perform_format(transport, partition.c_str(), 0,
1511 type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001512 };
David Pursell0b156632015-10-30 11:22:01 -07001513 do_for_partitions(transport, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001514 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001515 } else if(!strcmp(*argv, "signature")) {
1516 require(2);
1517 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001518 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001519 if (sz != 256) die("signature must be 256 bytes");
1520 fb_queue_download("signature", data, sz);
1521 fb_queue_command("signature", "installing signature");
1522 skip(2);
1523 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001524 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001525 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001526 if (argc > 0) {
1527 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001528 wants_reboot = false;
1529 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001530 skip(1);
1531 }
1532 }
1533 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001534 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001535 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001536 skip(1);
1537 } else if (!strcmp(*argv, "continue")) {
1538 fb_queue_command("continue", "resuming boot");
1539 skip(1);
1540 } else if(!strcmp(*argv, "boot")) {
1541 char *kname = 0;
1542 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001543 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001544 skip(1);
1545 if (argc > 0) {
1546 kname = argv[0];
1547 skip(1);
1548 }
1549 if (argc > 0) {
1550 rname = argv[0];
1551 skip(1);
1552 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001553 if (argc > 0) {
1554 sname = argv[0];
1555 skip(1);
1556 }
1557 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001558 if (data == 0) return 1;
1559 fb_queue_download("boot.img", data, sz);
1560 fb_queue_command("boot", "booting");
1561 } else if(!strcmp(*argv, "flash")) {
1562 char *pname = argv[1];
1563 char *fname = 0;
1564 require(2);
1565 if (argc > 2) {
1566 fname = argv[2];
1567 skip(3);
1568 } else {
1569 fname = find_item(pname, product);
1570 skip(2);
1571 }
1572 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001573
1574 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001575 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001576 fb_queue_erase(partition.c_str());
1577 }
David Pursell0b156632015-10-30 11:22:01 -07001578 do_flash(transport, partition.c_str(), fname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001579 };
David Pursell0b156632015-10-30 11:22:01 -07001580 do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001581 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001582 char *kname = argv[2];
1583 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001584 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001585 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001586 skip(3);
1587 if (argc > 0) {
1588 rname = argv[0];
1589 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001590 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001591 if (argc > 0) {
1592 sname = argv[0];
1593 skip(1);
1594 }
1595 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001596 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001597 auto flashraw = [&](const std::string &partition) {
1598 fb_queue_flash(partition.c_str(), data, sz);
1599 };
David Pursell0b156632015-10-30 11:22:01 -07001600 do_for_partitions(transport, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001601 } else if(!strcmp(*argv, "flashall")) {
1602 skip(1);
David Pursell0b156632015-10-30 11:22:01 -07001603 do_flashall(transport, slot_override.c_str(), erase_first);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001604 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001605 } else if(!strcmp(*argv, "update")) {
1606 if (argc > 1) {
David Pursell0b156632015-10-30 11:22:01 -07001607 do_update(transport, argv[1], slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001608 skip(2);
1609 } else {
David Pursell0b156632015-10-30 11:22:01 -07001610 do_update(transport, "update.zip", slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001611 skip(1);
1612 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001613 wants_reboot = 1;
1614 } else if(!strcmp(*argv, "set_active")) {
1615 require(2);
1616 std::string slot = verify_slot(transport, argv[1], false);
1617 fb_set_active(slot.c_str());
1618 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001619 } else if(!strcmp(*argv, "oem")) {
1620 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001621 } else if(!strcmp(*argv, "flashing")) {
1622 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1623 !strcmp(*(argv+1), "lock") ||
1624 !strcmp(*(argv+1), "unlock_critical") ||
1625 !strcmp(*(argv+1), "lock_critical") ||
1626 !strcmp(*(argv+1), "get_unlock_ability") ||
1627 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1628 !strcmp(*(argv+1), "lock_bootloader"))) {
1629 argc = do_oem_command(argc, argv);
1630 } else
1631 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1632 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001633 } else {
1634 usage();
1635 return 1;
1636 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001637 } else {
1638 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001639 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001640 }
1641 }
1642
1643 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001644 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001645 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001646 if (set_fbe_marker) {
1647 fprintf(stderr, "setting FBE marker...\n");
1648 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1649 if (initial_userdata_dir.empty()) {
1650 return 1;
1651 }
1652 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
1653 delete_fbemarker_tmpdir(initial_userdata_dir);
1654 } else {
1655 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
1656 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001657
1658 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001659 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001660 fprintf(stderr, "wiping cache...\n");
1661 fb_queue_erase("cache");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001662 fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001663 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001664 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001665 if (wants_set_active) {
1666 fb_set_active(next_active.c_str());
1667 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001668 if (wants_reboot) {
1669 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001670 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001671 } else if (wants_reboot_bootloader) {
1672 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001673 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001674 }
1675
David Pursell0b156632015-10-30 11:22:01 -07001676 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001677}