blob: 7e3d21272fef4c63bfaecc19ea2811a98d9fb654 [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 {
Daniel Rosenberg8c32da32016-06-27 16:46:50 -0700102 char img_name[13];
103 char sig_name[13];
Rom Lemarchand622810c2013-06-28 09:54:59 -0700104 char part_name[9];
105 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700106} images[] = {
Daniel Rosenberg8c32da32016-06-27 16:46:50 -0700107 {"boot.img", "boot.sig", "boot", false},
108 {"recovery.img", "recovery.sig", "recovery", true},
109 {"system.img", "system.sig", "system", false},
110 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700111};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700112
Elliott Hughesfc797672015-04-07 20:12:50 -0700113static char* find_item(const char* item, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700115 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116 char path[PATH_MAX + 128];
117
118 if(!strcmp(item,"boot")) {
119 fn = "boot.img";
120 } else if(!strcmp(item,"recovery")) {
121 fn = "recovery.img";
122 } else if(!strcmp(item,"system")) {
123 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700124 } else if(!strcmp(item,"vendor")) {
125 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 } else if(!strcmp(item,"userdata")) {
127 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700128 } else if(!strcmp(item,"cache")) {
129 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 } else if(!strcmp(item,"info")) {
131 fn = "android-info.txt";
132 } else {
133 fprintf(stderr,"unknown partition '%s'\n", item);
134 return 0;
135 }
136
137 if(product) {
138 get_my_path(path);
139 sprintf(path + strlen(path),
140 "../../../target/product/%s/%s", product, fn);
141 return strdup(path);
142 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800143
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 dir = getenv("ANDROID_PRODUCT_OUT");
145 if((dir == 0) || (dir[0] == 0)) {
146 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
147 return 0;
148 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800149
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150 sprintf(path, "%s/%s", dir, fn);
151 return strdup(path);
152}
153
Elliott Hughesfc797672015-04-07 20:12:50 -0700154static int64_t get_file_size(int fd) {
155 struct stat sb;
156 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700157}
158
Elliott Hughesfc797672015-04-07 20:12:50 -0700159static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800160 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700161 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162
Elliott Hughesfc797672015-04-07 20:12:50 -0700163 *sz = get_file_size(fd);
164 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700165 goto oops;
166 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167
Elliott Hughesfc797672015-04-07 20:12:50 -0700168 data = (char*) malloc(*sz);
169 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170
Elliott Hughesfc797672015-04-07 20:12:50 -0700171 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 close(fd);
173
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 return data;
175
176oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800177 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178 close(fd);
179 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800180 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181 return 0;
182}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183
Elliott Hughesfc797672015-04-07 20:12:50 -0700184static void* load_file(const char* fn, int64_t* sz) {
185 int fd = open(fn, O_RDONLY | O_BINARY);
186 if (fd == -1) return nullptr;
187 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700188}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189
Elliott Hughesfc797672015-04-07 20:12:50 -0700190static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700191 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700192 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700193 return -1;
194 }
195
196 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
197 return -1;
198 }
199
Scott Anderson13081c62012-04-06 12:39:30 -0700200 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700202 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
203 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 return 0;
205}
206
Elliott Hughesfc797672015-04-07 20:12:50 -0700207static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800208 return match_fastboot_with_serial(info, serial);
209}
210
Elliott Hughesfc797672015-04-07 20:12:50 -0700211static int list_devices_callback(usb_ifc_info* info) {
212 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800213 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700214 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800215 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700216 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 if (!serial[0]) {
218 serial = "????????????";
219 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700220 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700221 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800222 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700223 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800224 printf("%-22s fastboot", serial.c_str());
225 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700226 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800227 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228 }
229
230 return -1;
231}
232
David Pursell2ec418a2016-01-20 08:32:08 -0800233// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
234// a specific device, otherwise the first USB device found will be used.
235//
236// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
237// Otherwise it blocks until the target is available.
238//
239// The returned Transport is a singleton, so multiple calls to this function will return the same
240// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700241static Transport* open_device() {
242 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800243 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244
David Pursell2ec418a2016-01-20 08:32:08 -0800245 if (transport != nullptr) {
246 return transport;
247 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800248
David Pursell5a0ec812016-02-05 15:35:09 -0800249 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800250 std::string host;
David Pursell5a0ec812016-02-05 15:35:09 -0800251 int port = 0;
252 if (serial != nullptr) {
253 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800254
David Pursell5a0ec812016-02-05 15:35:09 -0800255 if (android::base::StartsWith(serial, "tcp:")) {
256 protocol = Socket::Protocol::kTcp;
257 port = tcp::kDefaultPort;
258 net_address = serial + strlen("tcp:");
259 } else if (android::base::StartsWith(serial, "udp:")) {
260 protocol = Socket::Protocol::kUdp;
261 port = udp::kDefaultPort;
262 net_address = serial + strlen("udp:");
263 }
264
265 if (net_address != nullptr) {
266 std::string error;
267 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
268 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
269 error.c_str());
270 return nullptr;
271 }
David Pursell2ec418a2016-01-20 08:32:08 -0800272 }
273 }
274
275 while (true) {
276 if (!host.empty()) {
277 std::string error;
David Pursell5a0ec812016-02-05 15:35:09 -0800278 if (protocol == Socket::Protocol::kTcp) {
279 transport = tcp::Connect(host, port, &error).release();
280 } else if (protocol == Socket::Protocol::kUdp) {
281 transport = udp::Connect(host, port, &error).release();
282 }
283
David Pursell2ec418a2016-01-20 08:32:08 -0800284 if (transport == nullptr && announce) {
285 fprintf(stderr, "error: %s\n", error.c_str());
286 }
287 } else {
288 transport = usb_open(match_fastboot);
289 }
290
291 if (transport != nullptr) {
292 return transport;
293 }
294
David Pursell0b156632015-10-30 11:22:01 -0700295 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800296 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700297 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700299 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 }
301}
302
Elliott Hughesfc797672015-04-07 20:12:50 -0700303static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 // We don't actually open a USB device here,
305 // just getting our callback called so we can
306 // list all the connected devices.
307 usb_open(list_devices_callback);
308}
309
Elliott Hughesfc797672015-04-07 20:12:50 -0700310static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311 fprintf(stderr,
312/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
313 "usage: fastboot [ <option> ] <command>\n"
314 "\n"
315 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700316 " update <filename> Reflash device from update.zip.\n"
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -0700317 " Sets the flashed slot as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700318 " flashall Flash boot, system, vendor, and --\n"
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -0700319 " if found -- recovery. If the device\n"
320 " supports slots, the slot that has\n"
321 " been flashed to is set as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700322 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
323 " flashing lock Locks the device. Prevents flashing.\n"
324 " flashing unlock Unlocks the device. Allows flashing\n"
325 " any partition except\n"
326 " bootloader-related partitions.\n"
327 " flashing lock_critical Prevents flashing bootloader-related\n"
328 " partitions.\n"
329 " flashing unlock_critical Enables flashing bootloader-related\n"
330 " partitions.\n"
331 " flashing get_unlock_ability Queries bootloader to see if the\n"
332 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700333 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700334 " unlock nonce.\n"
335 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700336 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700337 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700338 " erase <partition> Erase a flash partition.\n"
339 " format[:[<fs type>][:[<size>]] <partition>\n"
340 " Format a flash partition. Can\n"
341 " override the fs type and/or size\n"
342 " the bootloader reports.\n"
343 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800344 " set_active <suffix> Sets the active slot. If slots are\n"
345 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700346 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
347 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
348 " Create bootimage and flash it.\n"
349 " devices [-l] List all connected devices [with\n"
350 " device paths].\n"
351 " continue Continue with autoboot.\n"
352 " reboot [bootloader] Reboot device [into bootloader].\n"
353 " reboot-bootloader Reboot device into bootloader.\n"
354 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355 "\n"
356 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700357 " -w Erase userdata and cache (and format\n"
358 " if supported by partition type).\n"
359 " -u Do not erase partition before\n"
360 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800361 " -s <specific device> Specify a device. For USB, provide either\n"
362 " a serial number or path to device port.\n"
David Pursell5a0ec812016-02-05 15:35:09 -0800363 " For ethernet, provide an address in the"
364 " form <protocol>:<hostname>[:port] where"
365 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700366 " -p <product> Specify product name.\n"
367 " -c <cmdline> Override kernel commandline.\n"
368 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800369 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700370 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800371 " --kernel-offset Specify a custom kernel offset.\n"
372 " (default: 0x00008000)\n"
373 " --ramdisk-offset Specify a custom ramdisk offset.\n"
374 " (default: 0x01000000)\n"
375 " --tags-offset Specify a custom tags offset.\n"
376 " (default: 0x00000100)\n"
377 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700378 " (default: 2048).\n"
379 " -S <size>[K|M|G] Automatically sparse files greater\n"
380 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700381 " --slot <suffix> Specify slot suffix to be used if the\n"
382 " device supports slots. This will be\n"
383 " added to all partition names that use\n"
384 " slots. 'all' can be given to refer\n"
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800385 " to all slots. 'other' can be given to\n"
Daniel Rosenberg8c32da32016-06-27 16:46:50 -0700386 " refer to a non-current slot. If this\n"
387 " flag is not used, slotted partitions\n"
388 " will default to the current active slot.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800389 " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800390 " provided, this will default to the value\n"
391 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800392 " supported, this does nothing. This will\n"
393 " run after all non-reboot commands.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000394#if !defined(_WIN32)
395 " --wipe-and-use-fbe On devices which support it,\n"
396 " erase userdata and cache, and\n"
397 " enable file-based encryption\n"
398#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800399 " --unbuffered Do not buffer input or output.\n"
400 " --version Display version.\n"
401 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000404
Elliott Hughesfc797672015-04-07 20:12:50 -0700405static void* load_bootable_image(const char* kernel, const char* ramdisk,
406 const char* secondstage, int64_t* sz,
407 const char* cmdline) {
408 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409 fprintf(stderr, "no image specified\n");
410 return 0;
411 }
412
Elliott Hughesfc797672015-04-07 20:12:50 -0700413 int64_t ksize;
414 void* kdata = load_file(kernel, &ksize);
415 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800416 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 return 0;
418 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800419
Elliott Hughesfc797672015-04-07 20:12:50 -0700420 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700422 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800423
Elliott Hughesfc797672015-04-07 20:12:50 -0700424 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
426 return 0;
427 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800428
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429 *sz = ksize;
430 return kdata;
431 }
432
Elliott Hughesfc797672015-04-07 20:12:50 -0700433 void* rdata = nullptr;
434 int64_t rsize = 0;
435 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700437 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800438 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439 return 0;
440 }
441 }
442
Elliott Hughesfc797672015-04-07 20:12:50 -0700443 void* sdata = nullptr;
444 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200445 if (secondstage) {
446 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700447 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200448 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
449 return 0;
450 }
451 }
452
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700454 int64_t bsize = 0;
455 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800456 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200457 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800458 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700459 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 fprintf(stderr,"failed to create boot.img\n");
461 return 0;
462 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700463 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
464 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800466
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467 return bdata;
468}
469
Elliott Hughesfc797672015-04-07 20:12:50 -0700470static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471{
Yusuke Sato07447542015-06-25 14:39:19 -0700472 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700473 ZipEntry zip_entry;
474 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700475 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000476 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 }
478
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700479 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700481 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700482 if (data == nullptr) {
483 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000484 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485 }
486
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700487 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
488 if (error != 0) {
489 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000491 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000493
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494 return data;
495}
496
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700497#if defined(_WIN32)
498
499// TODO: move this to somewhere it can be shared.
500
501#include <windows.h>
502
503// Windows' tmpfile(3) requires administrator rights because
504// it creates temporary files in the root directory.
505static FILE* win32_tmpfile() {
506 char temp_path[PATH_MAX];
507 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
508 if (nchars == 0 || nchars >= sizeof(temp_path)) {
509 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
510 return nullptr;
511 }
512
513 char filename[PATH_MAX];
514 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
515 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
516 return nullptr;
517 }
518
519 return fopen(filename, "w+bTD");
520}
521
522#define tmpfile win32_tmpfile
523
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000524static std::string make_temporary_directory() {
525 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
526 return "";
527}
528
529#else
530
531static std::string make_temporary_directory() {
532 const char *tmpdir = getenv("TMPDIR");
533 if (tmpdir == nullptr) {
534 tmpdir = P_tmpdir;
535 }
536 std::string result = std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
537 if (mkdtemp(&result[0]) == NULL) {
538 fprintf(stderr, "Unable to create temporary directory: %s\n",
539 strerror(errno));
540 return "";
541 }
542 return result;
543}
544
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700545#endif
546
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000547static std::string create_fbemarker_tmpdir() {
548 std::string dir = make_temporary_directory();
549 if (dir.empty()) {
550 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
551 return "";
552 }
553 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
554 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
555 if (fd == -1) {
556 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
557 marker_file.c_str(), errno, strerror(errno));
558 return "";
559 }
560 close(fd);
561 return dir;
562}
563
564static void delete_fbemarker_tmpdir(const std::string& dir) {
565 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
566 if (unlink(marker_file.c_str()) == -1) {
567 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
568 marker_file.c_str(), errno, strerror(errno));
569 return;
570 }
571 if (rmdir(dir.c_str()) == -1) {
572 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
573 dir.c_str(), errno, strerror(errno));
574 return;
575 }
576}
577
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700578static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
579 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700580 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700581 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
582 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700583 return -1;
584 }
585
Yusuke Sato07447542015-06-25 14:39:19 -0700586 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700587 ZipEntry zip_entry;
588 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
589 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000590 return -1;
591 }
592
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700593 int fd = fileno(fp);
594 int error = ExtractEntryToFile(zip, &zip_entry, fd);
595 if (error != 0) {
596 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
597 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700598 }
599
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000600 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700601 return fd;
602}
603
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800604static char *strip(char *s)
605{
606 int n;
607 while(*s && isspace(*s)) s++;
608 n = strlen(s);
609 while(n-- > 0) {
610 if(!isspace(s[n])) break;
611 s[n] = 0;
612 }
613 return s;
614}
615
616#define MAX_OPTIONS 32
617static int setup_requirement_line(char *name)
618{
619 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700620 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800621 unsigned n, count;
622 char *x;
623 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800624
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800625 if (!strncmp(name, "reject ", 7)) {
626 name += 7;
627 invert = 1;
628 } else if (!strncmp(name, "require ", 8)) {
629 name += 8;
630 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700631 } else if (!strncmp(name, "require-for-product:", 20)) {
632 // Get the product and point name past it
633 prod = name + 20;
634 name = strchr(name, ' ');
635 if (!name) return -1;
636 *name = 0;
637 name += 1;
638 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800639 }
640
641 x = strchr(name, '=');
642 if (x == 0) return 0;
643 *x = 0;
644 val[0] = x + 1;
645
646 for(count = 1; count < MAX_OPTIONS; count++) {
647 x = strchr(val[count - 1],'|');
648 if (x == 0) break;
649 *x = 0;
650 val[count] = x + 1;
651 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800652
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800653 name = strip(name);
654 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800655
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800656 name = strip(name);
657 if (name == 0) return -1;
658
Elliott Hughes253c18d2015-03-18 22:47:09 -0700659 const char* var = name;
660 // Work around an unfortunate name mismatch.
661 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800662
Elliott Hughes253c18d2015-03-18 22:47:09 -0700663 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800664 if (out == 0) return -1;
665
666 for(n = 0; n < count; n++) {
667 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700668 if (out[n] == 0) {
669 for(size_t i = 0; i < n; ++i) {
670 free((char*) out[i]);
671 }
672 free(out);
673 return -1;
674 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800675 }
676
Elliott Hughes253c18d2015-03-18 22:47:09 -0700677 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800678 return 0;
679}
680
Elliott Hughesfc797672015-04-07 20:12:50 -0700681static void setup_requirements(char* data, int64_t sz) {
682 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800683 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700684 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800685 *s++ = 0;
686 if (setup_requirement_line(data)) {
687 die("out of memory");
688 }
689 data = s;
690 } else {
691 s++;
692 }
693 }
694}
695
Elliott Hughesfc797672015-04-07 20:12:50 -0700696static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800697 fb_queue_notice("--------------------------------------------");
698 fb_queue_display("version-bootloader", "Bootloader Version...");
699 fb_queue_display("version-baseband", "Baseband Version.....");
700 fb_queue_display("serialno", "Serial Number........");
701 fb_queue_notice("--------------------------------------------");
702}
703
Rom Lemarchand622810c2013-06-28 09:54:59 -0700704static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700705{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700706 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700707 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700708 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700709 }
710
Elliott Hughesfc797672015-04-07 20:12:50 -0700711 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700712 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700713 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700714 }
715
Elliott Hughes253c18d2015-03-18 22:47:09 -0700716 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700717 if (!out_s) {
718 die("Failed to allocate sparse file array\n");
719 }
720
721 files = sparse_file_resparse(s, max_size, out_s, files);
722 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700723 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700724 }
725
726 return out_s;
727}
728
David Pursell0b156632015-10-30 11:22:01 -0700729static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700730 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700731 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
732 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800733 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700734 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700735 }
736
Elliott Hughes77c0e662015-11-02 15:51:12 -0800737 // Some bootloaders (angler, for example) send spurious whitespace too.
738 max_download_size = android::base::Trim(max_download_size);
739
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700740 uint64_t limit;
741 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800742 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700743 return 0;
744 }
745 if (limit > 0) {
746 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
747 }
Colin Crossf8387882012-05-24 17:18:41 -0700748 return limit;
749}
750
David Pursell0b156632015-10-30 11:22:01 -0700751static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700752 int64_t limit;
753
754 if (sparse_limit == 0) {
755 return 0;
756 } else if (sparse_limit > 0) {
757 limit = sparse_limit;
758 } else {
759 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700760 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700761 }
762 if (target_sparse_limit > 0) {
763 limit = target_sparse_limit;
764 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700765 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700766 }
767 }
768
769 if (size > limit) {
770 return limit;
771 }
772
773 return 0;
774}
775
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700776// Until we get lazy inode table init working in make_ext4fs, we need to
777// erase partitions of type ext4 before flashing a filesystem so no stale
778// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700779static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800780 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700781 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800782 return false;
783 }
784 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700785}
786
David Pursell0b156632015-10-30 11:22:01 -0700787static int load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700788 int64_t sz = get_file_size(fd);
789 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000790 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700791 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700792
Elliott Hughesfc797672015-04-07 20:12:50 -0700793 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700794 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700795 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700796 sparse_file** s = load_sparse_files(fd, limit);
797 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700798 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700799 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700800 buf->type = FB_BUFFER_SPARSE;
801 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700802 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700803 void* data = load_fd(fd, &sz);
804 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700805 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700806 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000807 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700808 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700809
810 return 0;
811}
812
David Pursell0b156632015-10-30 11:22:01 -0700813static int load_buf(Transport* transport, const char *fname, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700814{
815 int fd;
816
817 fd = open(fname, O_RDONLY | O_BINARY);
818 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700819 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700820 }
821
David Pursell0b156632015-10-30 11:22:01 -0700822 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700823}
824
825static void flash_buf(const char *pname, struct fastboot_buffer *buf)
826{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700827 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700828
829 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800830 case FB_BUFFER_SPARSE: {
831 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700832 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700833 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700834 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800835 sparse_files.emplace_back(*s, sz);
836 ++s;
837 }
838
839 for (size_t i = 0; i < sparse_files.size(); ++i) {
840 const auto& pair = sparse_files[i];
841 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700842 }
843 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800844 }
845
Rom Lemarchand622810c2013-06-28 09:54:59 -0700846 case FB_BUFFER:
847 fb_queue_flash(pname, buf->data, buf->sz);
848 break;
849 default:
850 die("unknown buffer type: %d", buf->type);
851 }
852}
853
David Pursell0b156632015-10-30 11:22:01 -0700854static std::vector<std::string> get_suffixes(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700855 std::vector<std::string> suffixes;
856 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700857 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700858 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700859 }
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700860 suffixes = android::base::Split(suffix_list, ",");
861 // Unfortunately some devices will return an error message in the
862 // guise of a valid value. If we only see only one suffix, it's probably
863 // not real.
864 if (suffixes.size() == 1) {
865 suffixes.clear();
866 }
867 return suffixes;
868}
869
870// Given a current slot, this returns what the 'other' slot is.
871static std::string get_other_slot(Transport* transport, std::string& current_slot) {
872 std::vector<std::string> suffixes = get_suffixes(transport);
873
874 if (!suffixes.empty()) {
875 for (size_t i = 0; i < suffixes.size(); i++) {
876 if (current_slot == suffixes[i]) {
877 return suffixes[(i+1)%suffixes.size()];
878 }
879 }
880 }
881 return "";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700882}
883
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800884static std::string verify_slot(Transport* transport, const char *slot, bool allow_all) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700885 if (strcmp(slot, "all") == 0) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800886 if (allow_all) {
887 return "all";
888 } else {
889 std::vector<std::string> suffixes = get_suffixes(transport);
890 if (!suffixes.empty()) {
891 return suffixes[0];
892 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800893 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800894 }
895 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700896 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800897
David Pursell0b156632015-10-30 11:22:01 -0700898 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800899
900 if (strcmp(slot, "other") == 0) {
Daniel Rosenberg8c32da32016-06-27 16:46:50 -0700901 std::string current_slot;
902 if (!fb_getvar(transport, "current-slot", &current_slot)) {
903 die("Failed to identify current slot.");
904 }
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700905 std::string other = get_other_slot(transport, current_slot);
906 if (other == "") {
907 die("No known slots.");
Daniel Rosenberg8c32da32016-06-27 16:46:50 -0700908 }
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700909 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800910 }
911
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700912 for (const std::string &suffix : suffixes) {
913 if (suffix == slot)
914 return slot;
915 }
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700916 if (suffixes.empty()) {
917 fprintf(stderr, "Device does not support slots.\n");
918 } else {
919 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
920 for (const std::string &suffix : suffixes) {
921 fprintf(stderr, "%s\n", suffix.c_str());
922 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700923 }
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 Rosenberg190d9682016-06-27 23:03:48 -0700973 if (suffixes.empty()) {
974 die("Error reading suffixes.\n");
975 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700976 for (std::string &suffix : suffixes) {
David Pursell0b156632015-10-30 11:22:01 -0700977 do_for_partition(transport, part, suffix.c_str(), 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, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700981 }
982 } else {
David Pursell0b156632015-10-30 11:22:01 -0700983 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700984 }
985}
986
David Pursell0b156632015-10-30 11:22:01 -0700987static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700988 struct fastboot_buffer buf;
989
David Pursell0b156632015-10-30 11:22:01 -0700990 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700991 die("cannot load '%s'", fname);
992 }
993 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700994}
995
Elliott Hughesfc797672015-04-07 20:12:50 -0700996static void do_update_signature(ZipArchiveHandle zip, char* fn) {
997 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700998 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700999 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001000 fb_queue_download("signature", data, sz);
1001 fb_queue_command("signature", "installing signature");
1002}
1003
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -07001004// Sets slot_override as the active slot. If slot_override is blank,
1005// set current slot as active instead. This clears slot-unbootable.
1006static void set_active(Transport* transport, const char* slot_override) {
1007 if (slot_override && slot_override[0]) {
1008 fb_set_active(slot_override);
1009 } else {
1010 std::string current_slot;
1011 if (fb_getvar(transport, "current-slot", &current_slot)) {
1012 current_slot = verify_slot(transport, current_slot.c_str(), false);
1013 fb_set_active(current_slot.c_str());
1014 }
1015 }
1016}
1017
David Pursell0b156632015-10-30 11:22:01 -07001018static 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 -08001019 queue_info_dump();
1020
Wink Savilleb98762f2011-04-04 17:54:59 -07001021 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1022
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001023 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001024 int error = OpenArchive(filename, &zip);
1025 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001026 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001027 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001028 }
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 = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001032 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001033 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001034 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001035 }
1036
Elliott Hughes253c18d2015-03-18 22:47:09 -07001037 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001038
Elliott Hughesacdbe922015-06-02 21:37:05 -07001039 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -07001040 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001041 if (fd == -1) {
1042 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001043 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001044 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001045 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001046 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001047 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001048 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -07001049 int rc = load_buf_fd(transport, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001050 if (rc) die("cannot load %s from flash", images[i].img_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001051
1052 auto update = [&](const std::string &partition) {
1053 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001054 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001055 fb_queue_erase(partition.c_str());
1056 }
1057 flash_buf(partition.c_str(), &buf);
1058 /* not closing the fd here since the sparse code keeps the fd around
1059 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
1060 * program exits.
1061 */
1062 };
David Pursell0b156632015-10-30 11:22:01 -07001063 do_for_partitions(transport, images[i].part_name, slot_override, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001064 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001065
1066 CloseArchive(zip);
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -07001067 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001068}
1069
Elliott Hughesfc797672015-04-07 20:12:50 -07001070static void do_send_signature(char* fn) {
1071 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001072 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001073
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001074 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001075
Elliott Hughesfc797672015-04-07 20:12:50 -07001076 strcpy(xtn, ".sig");
1077
1078 int64_t sz;
1079 void* data = load_file(fn, &sz);
1080 strcpy(xtn, ".img");
1081 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001082 fb_queue_download("signature", data, sz);
1083 fb_queue_command("signature", "installing signature");
1084}
1085
David Pursell0b156632015-10-30 11:22:01 -07001086static void do_flashall(Transport* transport, const char* slot_override, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001087 queue_info_dump();
1088
Wink Savilleb98762f2011-04-04 17:54:59 -07001089 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1090
Elliott Hughes253c18d2015-03-18 22:47:09 -07001091 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -07001092 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001093
Elliott Hughesfc797672015-04-07 20:12:50 -07001094 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001095 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001096 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001097
1098 setup_requirements(reinterpret_cast<char*>(data), sz);
1099
1100 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Daniel Rosenberg8c32da32016-06-27 16:46:50 -07001101 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001102 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -07001103 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001104 if (images[i].is_optional)
1105 continue;
1106 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001107 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001108
1109 auto flashall = [&](const std::string &partition) {
1110 do_send_signature(fname);
David Pursell0b156632015-10-30 11:22:01 -07001111 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001112 fb_queue_erase(partition.c_str());
1113 }
1114 flash_buf(partition.c_str(), &buf);
1115 };
Daniel Rosenberg8c32da32016-06-27 16:46:50 -07001116 do_for_partitions(transport, images[i].part_name, slot_override, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001117 }
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -07001118
1119 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001120}
1121
1122#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001123#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001124
Elliott Hughes45be42e2015-09-02 20:15:48 -07001125static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001126{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001127 if (argc <= 2) return 0;
1128 skip(2);
1129
1130 /*
1131 * Process unlock_bootloader, we have to load the message file
1132 * and send that to the remote device.
1133 */
1134 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001135
1136 int64_t sz;
1137 void* data = load_file(*argv, &sz);
1138 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001139 fb_queue_download("unlock_message", data, sz);
1140 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1141 skip(1);
1142 return 0;
1143}
1144
Elliott Hughes45be42e2015-09-02 20:15:48 -07001145static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001146{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001147 char command[256];
1148 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001149
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001150 command[0] = 0;
1151 while(1) {
1152 strcat(command,*argv);
1153 skip(1);
1154 if(argc == 0) break;
1155 strcat(command," ");
1156 }
1157
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001158 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001159 return 0;
1160}
1161
Colin Crossf8387882012-05-24 17:18:41 -07001162static int64_t parse_num(const char *arg)
1163{
1164 char *endptr;
1165 unsigned long long num;
1166
1167 num = strtoull(arg, &endptr, 0);
1168 if (endptr == arg) {
1169 return -1;
1170 }
1171
1172 if (*endptr == 'k' || *endptr == 'K') {
1173 if (num >= (-1ULL) / 1024) {
1174 return -1;
1175 }
1176 num *= 1024LL;
1177 endptr++;
1178 } else if (*endptr == 'm' || *endptr == 'M') {
1179 if (num >= (-1ULL) / (1024 * 1024)) {
1180 return -1;
1181 }
1182 num *= 1024LL * 1024LL;
1183 endptr++;
1184 } else if (*endptr == 'g' || *endptr == 'G') {
1185 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1186 return -1;
1187 }
1188 num *= 1024LL * 1024LL * 1024LL;
1189 endptr++;
1190 }
1191
1192 if (*endptr != '\0') {
1193 return -1;
1194 }
1195
1196 if (num > INT64_MAX) {
1197 return -1;
1198 }
1199
1200 return num;
1201}
1202
David Pursell0b156632015-10-30 11:22:01 -07001203static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001204 const char* partition, int skip_if_not_supported,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001205 const char* type_override, const char* size_override,
1206 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001207 std::string partition_type, partition_size;
1208
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001209 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001210 const char* errMsg = nullptr;
1211 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001212 int fd;
1213
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001214 unsigned int limit = INT_MAX;
1215 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001216 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001217 }
1218 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001219 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001220 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001221
David Pursell0b156632015-10-30 11:22:01 -07001222 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001223 errMsg = "Can't determine partition type.\n";
1224 goto failed;
1225 }
JP Abgrall7e859742014-05-06 15:14:15 -07001226 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001227 if (partition_type != type_override) {
1228 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1229 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001230 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001231 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001232 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001233
David Pursell0b156632015-10-30 11:22:01 -07001234 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001235 errMsg = "Unable to get partition size\n";
1236 goto failed;
1237 }
JP Abgrall7e859742014-05-06 15:14:15 -07001238 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001239 if (partition_size != size_override) {
1240 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1241 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001242 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001243 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001244 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001245 // Some bootloaders (angler, for example), send spurious leading whitespace.
1246 partition_size = android::base::Trim(partition_size);
1247 // Some bootloaders (hammerhead, for example) use implicit hex.
1248 // This code used to use strtol with base 16.
1249 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001250
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001251 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001252 if (!gen) {
1253 if (skip_if_not_supported) {
1254 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001255 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001256 return;
1257 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001258 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1259 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001260 return;
1261 }
1262
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001263 int64_t size;
1264 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1265 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1266 return;
1267 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001268
1269 fd = fileno(tmpfile());
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001270 if (fs_generator_generate(gen, fd, size, initial_dir)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001271 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001272 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001273 return;
1274 }
1275
David Pursell0b156632015-10-30 11:22:01 -07001276 if (load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001277 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001278 close(fd);
1279 return;
1280 }
1281 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001282 return;
1283
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001284failed:
1285 if (skip_if_not_supported) {
1286 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001287 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001288 }
1289 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
1290}
1291
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001292int main(int argc, char **argv)
1293{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001294 bool wants_wipe = false;
1295 bool wants_reboot = false;
1296 bool wants_reboot_bootloader = false;
1297 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001298 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001299 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001300 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001301 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001302 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001303 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001304 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001305
JP Abgrall7b8970c2013-03-07 17:06:41 -08001306 const struct option longopts[] = {
1307 {"base", required_argument, 0, 'b'},
1308 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001309 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001310 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001311 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001312 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001313 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001314 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001315 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001316 {"help", no_argument, 0, 'h'},
1317 {"unbuffered", no_argument, 0, 0},
1318 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001319 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001320 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001321 {"set-active", optional_argument, 0, 'a'},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001322#if !defined(_WIN32)
1323 {"wipe-and-use-fbe", no_argument, 0, 0},
1324#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001325 {0, 0, 0, 0}
1326 };
Colin Cross8879f982012-05-22 17:53:34 -07001327
1328 serial = getenv("ANDROID_SERIAL");
1329
1330 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001331 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 -07001332 if (c < 0) {
1333 break;
1334 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001335 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001336 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001337 case 'a':
1338 wants_set_active = true;
1339 if (optarg)
1340 next_active = optarg;
1341 break;
Colin Cross8879f982012-05-22 17:53:34 -07001342 case 'b':
1343 base_addr = strtoul(optarg, 0, 16);
1344 break;
Colin Cross8879f982012-05-22 17:53:34 -07001345 case 'c':
1346 cmdline = optarg;
1347 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001348 case 'h':
1349 usage();
1350 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001351 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001352 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001353 unsigned long val;
1354
1355 val = strtoul(optarg, &endptr, 0);
1356 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1357 die("invalid vendor id '%s'", optarg);
1358 vendor_id = (unsigned short)val;
1359 break;
1360 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001361 case 'k':
1362 kernel_offset = strtoul(optarg, 0, 16);
1363 break;
1364 case 'l':
1365 long_listing = 1;
1366 break;
1367 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001368 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001369 if (!page_size) die("invalid page size");
1370 break;
1371 case 'p':
1372 product = optarg;
1373 break;
1374 case 'r':
1375 ramdisk_offset = strtoul(optarg, 0, 16);
1376 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001377 case 't':
1378 tags_offset = strtoul(optarg, 0, 16);
1379 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001380 case 's':
1381 serial = optarg;
1382 break;
1383 case 'S':
1384 sparse_limit = parse_num(optarg);
1385 if (sparse_limit < 0) {
1386 die("invalid sparse limit");
1387 }
1388 break;
1389 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001390 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001391 break;
1392 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001393 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001394 break;
Colin Cross8879f982012-05-22 17:53:34 -07001395 case '?':
1396 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001397 case 0:
1398 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001399 setvbuf(stdout, nullptr, _IONBF, 0);
1400 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001401 } else if (strcmp("version", longopts[longindex].name) == 0) {
1402 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1403 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001404 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1405 slot_override = std::string(optarg);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001406#if !defined(_WIN32)
1407 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1408 wants_wipe = true;
1409 set_fbe_marker = true;
1410#endif
1411 } else {
1412 fprintf(stderr, "Internal error in options processing for %s\n",
1413 longopts[longindex].name);
1414 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001415 }
1416 break;
Colin Cross8879f982012-05-22 17:53:34 -07001417 default:
1418 abort();
1419 }
1420 }
1421
1422 argc -= optind;
1423 argv += optind;
1424
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001425 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001426 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001427 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001428 }
1429
Colin Cross8fb6e062012-07-24 16:36:41 -07001430 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001431 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001432 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001433 return 0;
1434 }
1435
Colin Crossc7b75dc2012-08-29 18:17:06 -07001436 if (argc > 0 && !strcmp(*argv, "help")) {
1437 usage();
1438 return 0;
1439 }
1440
David Pursell0b156632015-10-30 11:22:01 -07001441 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001442 if (transport == nullptr) {
1443 return 1;
1444 }
1445
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001446 if (slot_override != "")
David Pursell0b156632015-10-30 11:22:01 -07001447 slot_override = verify_slot(transport, slot_override.c_str());
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001448 if (next_active != "")
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001449 next_active = verify_slot(transport, next_active.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001450
1451 if (wants_set_active) {
1452 if (next_active == "") {
1453 if (slot_override == "") {
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -07001454 std::string current_slot;
1455 if (fb_getvar(transport, "current-slot", &current_slot)) {
1456 next_active = verify_slot(transport, current_slot.c_str(), false);
1457 } else {
1458 wants_set_active = false;
1459 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001460 } else {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001461 next_active = verify_slot(transport, slot_override.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001462 }
1463 }
1464 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001465
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001466 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001467 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001468 require(2);
1469 fb_queue_display(argv[1], argv[1]);
1470 skip(2);
1471 } else if(!strcmp(*argv, "erase")) {
1472 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001473
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001474 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001475 std::string partition_type;
1476 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001477 fs_get_generator(partition_type) != nullptr) {
1478 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1479 partition_type.c_str());
1480 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001481
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001482 fb_queue_erase(partition.c_str());
1483 };
David Pursell0b156632015-10-30 11:22:01 -07001484 do_for_partitions(transport, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001485 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001486 } else if(!strncmp(*argv, "format", strlen("format"))) {
1487 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001488 char *type_override = nullptr;
1489 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001490 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001491 /*
1492 * Parsing for: "format[:[type][:[size]]]"
1493 * Some valid things:
1494 * - select ontly the size, and leave default fs type:
1495 * format::0x4000000 userdata
1496 * - default fs type and size:
1497 * format userdata
1498 * format:: userdata
1499 */
1500 overrides = strchr(*argv, ':');
1501 if (overrides) {
1502 overrides++;
1503 size_override = strchr(overrides, ':');
1504 if (size_override) {
1505 size_override[0] = '\0';
1506 size_override++;
1507 }
1508 type_override = overrides;
1509 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001510 if (type_override && !type_override[0]) type_override = nullptr;
1511 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001512
1513 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001514 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001515 fb_queue_erase(partition.c_str());
1516 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001517 fb_perform_format(transport, partition.c_str(), 0,
1518 type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001519 };
David Pursell0b156632015-10-30 11:22:01 -07001520 do_for_partitions(transport, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001521 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001522 } else if(!strcmp(*argv, "signature")) {
1523 require(2);
1524 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001525 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001526 if (sz != 256) die("signature must be 256 bytes");
1527 fb_queue_download("signature", data, sz);
1528 fb_queue_command("signature", "installing signature");
1529 skip(2);
1530 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001531 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001532 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001533 if (argc > 0) {
1534 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001535 wants_reboot = false;
1536 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001537 skip(1);
1538 }
1539 }
1540 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001541 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001542 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001543 skip(1);
1544 } else if (!strcmp(*argv, "continue")) {
1545 fb_queue_command("continue", "resuming boot");
1546 skip(1);
1547 } else if(!strcmp(*argv, "boot")) {
1548 char *kname = 0;
1549 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001550 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001551 skip(1);
1552 if (argc > 0) {
1553 kname = argv[0];
1554 skip(1);
1555 }
1556 if (argc > 0) {
1557 rname = argv[0];
1558 skip(1);
1559 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001560 if (argc > 0) {
1561 sname = argv[0];
1562 skip(1);
1563 }
1564 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001565 if (data == 0) return 1;
1566 fb_queue_download("boot.img", data, sz);
1567 fb_queue_command("boot", "booting");
1568 } else if(!strcmp(*argv, "flash")) {
1569 char *pname = argv[1];
1570 char *fname = 0;
1571 require(2);
1572 if (argc > 2) {
1573 fname = argv[2];
1574 skip(3);
1575 } else {
1576 fname = find_item(pname, product);
1577 skip(2);
1578 }
1579 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001580
1581 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001582 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001583 fb_queue_erase(partition.c_str());
1584 }
David Pursell0b156632015-10-30 11:22:01 -07001585 do_flash(transport, partition.c_str(), fname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001586 };
David Pursell0b156632015-10-30 11:22:01 -07001587 do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001588 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001589 char *kname = argv[2];
1590 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001591 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001592 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001593 skip(3);
1594 if (argc > 0) {
1595 rname = argv[0];
1596 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001597 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001598 if (argc > 0) {
1599 sname = argv[0];
1600 skip(1);
1601 }
1602 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001603 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001604 auto flashraw = [&](const std::string &partition) {
1605 fb_queue_flash(partition.c_str(), data, sz);
1606 };
David Pursell0b156632015-10-30 11:22:01 -07001607 do_for_partitions(transport, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001608 } else if(!strcmp(*argv, "flashall")) {
1609 skip(1);
David Pursell0b156632015-10-30 11:22:01 -07001610 do_flashall(transport, slot_override.c_str(), erase_first);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001611 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001612 } else if(!strcmp(*argv, "update")) {
1613 if (argc > 1) {
David Pursell0b156632015-10-30 11:22:01 -07001614 do_update(transport, argv[1], slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001615 skip(2);
1616 } else {
David Pursell0b156632015-10-30 11:22:01 -07001617 do_update(transport, "update.zip", slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001618 skip(1);
1619 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001620 wants_reboot = 1;
1621 } else if(!strcmp(*argv, "set_active")) {
1622 require(2);
1623 std::string slot = verify_slot(transport, argv[1], false);
1624 fb_set_active(slot.c_str());
1625 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001626 } else if(!strcmp(*argv, "oem")) {
1627 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001628 } else if(!strcmp(*argv, "flashing")) {
1629 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1630 !strcmp(*(argv+1), "lock") ||
1631 !strcmp(*(argv+1), "unlock_critical") ||
1632 !strcmp(*(argv+1), "lock_critical") ||
1633 !strcmp(*(argv+1), "get_unlock_ability") ||
1634 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1635 !strcmp(*(argv+1), "lock_bootloader"))) {
1636 argc = do_oem_command(argc, argv);
1637 } else
1638 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1639 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001640 } else {
1641 usage();
1642 return 1;
1643 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001644 } else {
1645 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001646 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001647 }
1648 }
1649
1650 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001651 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001652 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001653 if (set_fbe_marker) {
1654 fprintf(stderr, "setting FBE marker...\n");
1655 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1656 if (initial_userdata_dir.empty()) {
1657 return 1;
1658 }
1659 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
1660 delete_fbemarker_tmpdir(initial_userdata_dir);
1661 } else {
1662 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
1663 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001664
1665 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001666 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001667 fprintf(stderr, "wiping cache...\n");
1668 fb_queue_erase("cache");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001669 fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001670 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001671 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001672 if (wants_set_active) {
1673 fb_set_active(next_active.c_str());
1674 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001675 if (wants_reboot) {
1676 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001677 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001678 } else if (wants_reboot_bootloader) {
1679 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001680 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001681 }
1682
David Pursell0b156632015-10-30 11:22:01 -07001683 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001684}