blob: 455c66803bade76d78ef1a5418aa486431e22ba6 [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>
Daniel Rosenberg6c985092016-06-28 01:36:21 -070052#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080053#include <android-base/strings.h>
Colin Crossf8387882012-05-24 17:18:41 -070054#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070055#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056
Elliott Hughes253c18d2015-03-18 22:47:09 -070057#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080058#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070060#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080061#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070062#include "transport.h"
David Pursell5a0ec812016-02-05 15:35:09 -080063#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070064#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
Colin Crossf8387882012-05-24 17:18:41 -070066#ifndef O_BINARY
67#define O_BINARY 0
68#endif
69
Rom Lemarchand622810c2013-06-28 09:54:59 -070070#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
71
Wink Savilleb98762f2011-04-04 17:54:59 -070072char cur_product[FB_RESPONSE_SZ + 1];
73
David Pursell2ec418a2016-01-20 08:32:08 -080074static const char* serial = nullptr;
75static const char* product = nullptr;
76static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070078static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070079static int64_t sparse_limit = -1;
80static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081
Elliott Hughesfc797672015-04-07 20:12:50 -070082static unsigned page_size = 2048;
83static unsigned base_addr = 0x10000000;
84static unsigned kernel_offset = 0x00008000;
85static unsigned ramdisk_offset = 0x01000000;
86static unsigned second_offset = 0x00f00000;
87static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080088
Paul Crowley8f7f56e2015-11-27 09:29:37 +000089static const std::string convert_fbe_marker_filename("convert_fbe");
90
Rom Lemarchand622810c2013-06-28 09:54:59 -070091enum fb_buffer_type {
92 FB_BUFFER,
93 FB_BUFFER_SPARSE,
94};
95
96struct fastboot_buffer {
97 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070098 void* data;
99 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700100};
101
102static struct {
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700103 char img_name[17];
104 char sig_name[17];
Rom Lemarchand622810c2013-06-28 09:54:59 -0700105 char part_name[9];
106 bool is_optional;
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700107 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700108} images[] = {
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700109 {"boot.img", "boot.sig", "boot", false, false},
110 {"boot_other.img", "boot.sig", "boot", true, true},
111 {"recovery.img", "recovery.sig", "recovery", true, false},
112 {"system.img", "system.sig", "system", false, false},
113 {"system_other.img", "system.sig", "system", true, true},
114 {"vendor.img", "vendor.sig", "vendor", true, false},
115 {"vendor_other.img", "vendor.sig", "vendor", true, true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700116};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700117
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700118static std::string find_item_given_name(const char* img_name, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 char *dir;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120 char path[PATH_MAX + 128];
121
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700122 if(product) {
123 get_my_path(path);
124 return android::base::StringPrintf("../../../target/product/%s/%s", product, img_name);
125 }
126
127 dir = getenv("ANDROID_PRODUCT_OUT");
128 if((dir == 0) || (dir[0] == 0)) {
129 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
130 }
131
132 return android::base::StringPrintf("%s/%s", dir, img_name);
133}
134
135std::string find_item(const char* item, const char* product) {
136 const char *fn;
137
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 if(!strcmp(item,"boot")) {
139 fn = "boot.img";
140 } else if(!strcmp(item,"recovery")) {
141 fn = "recovery.img";
142 } else if(!strcmp(item,"system")) {
143 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700144 } else if(!strcmp(item,"vendor")) {
145 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 } else if(!strcmp(item,"userdata")) {
147 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700148 } else if(!strcmp(item,"cache")) {
149 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150 } else if(!strcmp(item,"info")) {
151 fn = "android-info.txt";
152 } else {
153 fprintf(stderr,"unknown partition '%s'\n", item);
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700154 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155 }
156
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700157 return find_item_given_name(fn, product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158}
159
Elliott Hughesfc797672015-04-07 20:12:50 -0700160static int64_t get_file_size(int fd) {
161 struct stat sb;
162 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700163}
164
Elliott Hughesfc797672015-04-07 20:12:50 -0700165static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800166 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700167 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168
Elliott Hughesfc797672015-04-07 20:12:50 -0700169 *sz = get_file_size(fd);
170 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700171 goto oops;
172 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173
Elliott Hughesfc797672015-04-07 20:12:50 -0700174 data = (char*) malloc(*sz);
175 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176
Elliott Hughesfc797672015-04-07 20:12:50 -0700177 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178 close(fd);
179
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 return data;
181
182oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800183 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184 close(fd);
185 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800186 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 return 0;
188}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189
Elliott Hughesfc797672015-04-07 20:12:50 -0700190static void* load_file(const char* fn, int64_t* sz) {
191 int fd = open(fn, O_RDONLY | O_BINARY);
192 if (fd == -1) return nullptr;
193 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700194}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195
Elliott Hughesfc797672015-04-07 20:12:50 -0700196static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700197 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700198 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700199 return -1;
200 }
201
202 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
203 return -1;
204 }
205
Scott Anderson13081c62012-04-06 12:39:30 -0700206 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700208 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
209 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210 return 0;
211}
212
Elliott Hughesfc797672015-04-07 20:12:50 -0700213static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800214 return match_fastboot_with_serial(info, serial);
215}
216
Elliott Hughesfc797672015-04-07 20:12:50 -0700217static int list_devices_callback(usb_ifc_info* info) {
218 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800219 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700220 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800221 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700222 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 if (!serial[0]) {
224 serial = "????????????";
225 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700226 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700227 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800228 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700229 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800230 printf("%-22s fastboot", serial.c_str());
231 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700232 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800233 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 }
235
236 return -1;
237}
238
David Pursell2ec418a2016-01-20 08:32:08 -0800239// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
240// a specific device, otherwise the first USB device found will be used.
241//
242// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
243// Otherwise it blocks until the target is available.
244//
245// The returned Transport is a singleton, so multiple calls to this function will return the same
246// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700247static Transport* open_device() {
248 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800249 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250
David Pursell2ec418a2016-01-20 08:32:08 -0800251 if (transport != nullptr) {
252 return transport;
253 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800254
David Pursell5a0ec812016-02-05 15:35:09 -0800255 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800256 std::string host;
David Pursell5a0ec812016-02-05 15:35:09 -0800257 int port = 0;
258 if (serial != nullptr) {
259 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800260
David Pursell5a0ec812016-02-05 15:35:09 -0800261 if (android::base::StartsWith(serial, "tcp:")) {
262 protocol = Socket::Protocol::kTcp;
263 port = tcp::kDefaultPort;
264 net_address = serial + strlen("tcp:");
265 } else if (android::base::StartsWith(serial, "udp:")) {
266 protocol = Socket::Protocol::kUdp;
267 port = udp::kDefaultPort;
268 net_address = serial + strlen("udp:");
269 }
270
271 if (net_address != nullptr) {
272 std::string error;
273 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
274 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
275 error.c_str());
276 return nullptr;
277 }
David Pursell2ec418a2016-01-20 08:32:08 -0800278 }
279 }
280
281 while (true) {
282 if (!host.empty()) {
283 std::string error;
David Pursell5a0ec812016-02-05 15:35:09 -0800284 if (protocol == Socket::Protocol::kTcp) {
285 transport = tcp::Connect(host, port, &error).release();
286 } else if (protocol == Socket::Protocol::kUdp) {
287 transport = udp::Connect(host, port, &error).release();
288 }
289
David Pursell2ec418a2016-01-20 08:32:08 -0800290 if (transport == nullptr && announce) {
291 fprintf(stderr, "error: %s\n", error.c_str());
292 }
293 } else {
294 transport = usb_open(match_fastboot);
295 }
296
297 if (transport != nullptr) {
298 return transport;
299 }
300
David Pursell0b156632015-10-30 11:22:01 -0700301 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800302 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700303 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700305 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306 }
307}
308
Elliott Hughesfc797672015-04-07 20:12:50 -0700309static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310 // We don't actually open a USB device here,
311 // just getting our callback called so we can
312 // list all the connected devices.
313 usb_open(list_devices_callback);
314}
315
Elliott Hughesfc797672015-04-07 20:12:50 -0700316static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317 fprintf(stderr,
318/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
319 "usage: fastboot [ <option> ] <command>\n"
320 "\n"
321 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700322 " update <filename> Reflash device from update.zip.\n"
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -0700323 " Sets the flashed slot as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700324 " flashall Flash boot, system, vendor, and --\n"
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -0700325 " if found -- recovery. If the device\n"
326 " supports slots, the slot that has\n"
327 " been flashed to is set as active.\n"
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700328 " Secondary images may be flashed to\n"
329 " an inactive slot.\n"
330 " flash-primary Same as flashall, but do not flash\n"
331 " secondary images.\n"
332 " flash-secondary Only flashes the secondary images.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700333 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
334 " flashing lock Locks the device. Prevents flashing.\n"
335 " flashing unlock Unlocks the device. Allows flashing\n"
336 " any partition except\n"
337 " bootloader-related partitions.\n"
338 " flashing lock_critical Prevents flashing bootloader-related\n"
339 " partitions.\n"
340 " flashing unlock_critical Enables flashing bootloader-related\n"
341 " partitions.\n"
342 " flashing get_unlock_ability Queries bootloader to see if the\n"
343 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700344 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700345 " unlock nonce.\n"
346 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700347 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700348 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700349 " erase <partition> Erase a flash partition.\n"
350 " format[:[<fs type>][:[<size>]] <partition>\n"
351 " Format a flash partition. Can\n"
352 " override the fs type and/or size\n"
353 " the bootloader reports.\n"
354 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800355 " set_active <suffix> Sets the active slot. If slots are\n"
356 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700357 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
358 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
359 " Create bootimage and flash it.\n"
360 " devices [-l] List all connected devices [with\n"
361 " device paths].\n"
362 " continue Continue with autoboot.\n"
363 " reboot [bootloader] Reboot device [into bootloader].\n"
364 " reboot-bootloader Reboot device into bootloader.\n"
365 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 "\n"
367 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700368 " -w Erase userdata and cache (and format\n"
369 " if supported by partition type).\n"
370 " -u Do not erase partition before\n"
371 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800372 " -s <specific device> Specify a device. For USB, provide either\n"
373 " a serial number or path to device port.\n"
David Pursell5a0ec812016-02-05 15:35:09 -0800374 " For ethernet, provide an address in the"
375 " form <protocol>:<hostname>[:port] where"
376 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700377 " -p <product> Specify product name.\n"
378 " -c <cmdline> Override kernel commandline.\n"
379 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800380 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700381 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800382 " --kernel-offset Specify a custom kernel offset.\n"
383 " (default: 0x00008000)\n"
384 " --ramdisk-offset Specify a custom ramdisk offset.\n"
385 " (default: 0x01000000)\n"
386 " --tags-offset Specify a custom tags offset.\n"
387 " (default: 0x00000100)\n"
388 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700389 " (default: 2048).\n"
390 " -S <size>[K|M|G] Automatically sparse files greater\n"
391 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700392 " --slot <suffix> Specify slot suffix to be used if the\n"
393 " device supports slots. This will be\n"
394 " added to all partition names that use\n"
395 " slots. 'all' can be given to refer\n"
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800396 " to all slots. 'other' can be given to\n"
Daniel Rosenberg8c32da32016-06-27 16:46:50 -0700397 " refer to a non-current slot. If this\n"
398 " flag is not used, slotted partitions\n"
399 " will default to the current active slot.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800400 " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800401 " provided, this will default to the value\n"
402 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800403 " supported, this does nothing. This will\n"
404 " run after all non-reboot commands.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000405#if !defined(_WIN32)
406 " --wipe-and-use-fbe On devices which support it,\n"
407 " erase userdata and cache, and\n"
408 " enable file-based encryption\n"
409#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800410 " --unbuffered Do not buffer input or output.\n"
411 " --version Display version.\n"
412 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000415
Elliott Hughesfc797672015-04-07 20:12:50 -0700416static void* load_bootable_image(const char* kernel, const char* ramdisk,
417 const char* secondstage, int64_t* sz,
418 const char* cmdline) {
419 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420 fprintf(stderr, "no image specified\n");
421 return 0;
422 }
423
Elliott Hughesfc797672015-04-07 20:12:50 -0700424 int64_t ksize;
425 void* kdata = load_file(kernel, &ksize);
426 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800427 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428 return 0;
429 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800430
Elliott Hughesfc797672015-04-07 20:12:50 -0700431 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700433 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800434
Elliott Hughesfc797672015-04-07 20:12:50 -0700435 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
437 return 0;
438 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800439
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440 *sz = ksize;
441 return kdata;
442 }
443
Elliott Hughesfc797672015-04-07 20:12:50 -0700444 void* rdata = nullptr;
445 int64_t rsize = 0;
446 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800447 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700448 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800449 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450 return 0;
451 }
452 }
453
Elliott Hughesfc797672015-04-07 20:12:50 -0700454 void* sdata = nullptr;
455 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200456 if (secondstage) {
457 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700458 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200459 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
460 return 0;
461 }
462 }
463
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700465 int64_t bsize = 0;
466 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800467 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200468 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800469 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700470 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471 fprintf(stderr,"failed to create boot.img\n");
472 return 0;
473 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700474 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
475 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800477
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478 return bdata;
479}
480
Elliott Hughesfc797672015-04-07 20:12:50 -0700481static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482{
Yusuke Sato07447542015-06-25 14:39:19 -0700483 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700484 ZipEntry zip_entry;
485 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700486 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000487 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488 }
489
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700490 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700492 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700493 if (data == nullptr) {
494 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000495 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496 }
497
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700498 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
499 if (error != 0) {
500 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000502 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000504
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 return data;
506}
507
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700508#if defined(_WIN32)
509
510// TODO: move this to somewhere it can be shared.
511
512#include <windows.h>
513
514// Windows' tmpfile(3) requires administrator rights because
515// it creates temporary files in the root directory.
516static FILE* win32_tmpfile() {
517 char temp_path[PATH_MAX];
518 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
519 if (nchars == 0 || nchars >= sizeof(temp_path)) {
520 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
521 return nullptr;
522 }
523
524 char filename[PATH_MAX];
525 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
526 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
527 return nullptr;
528 }
529
530 return fopen(filename, "w+bTD");
531}
532
533#define tmpfile win32_tmpfile
534
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000535static std::string make_temporary_directory() {
536 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
537 return "";
538}
539
540#else
541
542static std::string make_temporary_directory() {
543 const char *tmpdir = getenv("TMPDIR");
544 if (tmpdir == nullptr) {
545 tmpdir = P_tmpdir;
546 }
547 std::string result = std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
548 if (mkdtemp(&result[0]) == NULL) {
549 fprintf(stderr, "Unable to create temporary directory: %s\n",
550 strerror(errno));
551 return "";
552 }
553 return result;
554}
555
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700556#endif
557
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000558static std::string create_fbemarker_tmpdir() {
559 std::string dir = make_temporary_directory();
560 if (dir.empty()) {
561 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
562 return "";
563 }
564 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
565 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
566 if (fd == -1) {
567 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
568 marker_file.c_str(), errno, strerror(errno));
569 return "";
570 }
571 close(fd);
572 return dir;
573}
574
575static void delete_fbemarker_tmpdir(const std::string& dir) {
576 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
577 if (unlink(marker_file.c_str()) == -1) {
578 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
579 marker_file.c_str(), errno, strerror(errno));
580 return;
581 }
582 if (rmdir(dir.c_str()) == -1) {
583 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
584 dir.c_str(), errno, strerror(errno));
585 return;
586 }
587}
588
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700589static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
590 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700591 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700592 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
593 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700594 return -1;
595 }
596
Yusuke Sato07447542015-06-25 14:39:19 -0700597 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700598 ZipEntry zip_entry;
599 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
600 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000601 return -1;
602 }
603
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700604 int fd = fileno(fp);
605 int error = ExtractEntryToFile(zip, &zip_entry, fd);
606 if (error != 0) {
607 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
608 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700609 }
610
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000611 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700612 return fd;
613}
614
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800615static char *strip(char *s)
616{
617 int n;
618 while(*s && isspace(*s)) s++;
619 n = strlen(s);
620 while(n-- > 0) {
621 if(!isspace(s[n])) break;
622 s[n] = 0;
623 }
624 return s;
625}
626
627#define MAX_OPTIONS 32
628static int setup_requirement_line(char *name)
629{
630 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700631 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800632 unsigned n, count;
633 char *x;
634 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800635
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800636 if (!strncmp(name, "reject ", 7)) {
637 name += 7;
638 invert = 1;
639 } else if (!strncmp(name, "require ", 8)) {
640 name += 8;
641 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700642 } else if (!strncmp(name, "require-for-product:", 20)) {
643 // Get the product and point name past it
644 prod = name + 20;
645 name = strchr(name, ' ');
646 if (!name) return -1;
647 *name = 0;
648 name += 1;
649 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650 }
651
652 x = strchr(name, '=');
653 if (x == 0) return 0;
654 *x = 0;
655 val[0] = x + 1;
656
657 for(count = 1; count < MAX_OPTIONS; count++) {
658 x = strchr(val[count - 1],'|');
659 if (x == 0) break;
660 *x = 0;
661 val[count] = x + 1;
662 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800663
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800664 name = strip(name);
665 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800666
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800667 name = strip(name);
668 if (name == 0) return -1;
669
Elliott Hughes253c18d2015-03-18 22:47:09 -0700670 const char* var = name;
671 // Work around an unfortunate name mismatch.
672 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800673
Elliott Hughes253c18d2015-03-18 22:47:09 -0700674 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800675 if (out == 0) return -1;
676
677 for(n = 0; n < count; n++) {
678 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700679 if (out[n] == 0) {
680 for(size_t i = 0; i < n; ++i) {
681 free((char*) out[i]);
682 }
683 free(out);
684 return -1;
685 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686 }
687
Elliott Hughes253c18d2015-03-18 22:47:09 -0700688 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800689 return 0;
690}
691
Elliott Hughesfc797672015-04-07 20:12:50 -0700692static void setup_requirements(char* data, int64_t sz) {
693 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700695 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800696 *s++ = 0;
697 if (setup_requirement_line(data)) {
698 die("out of memory");
699 }
700 data = s;
701 } else {
702 s++;
703 }
704 }
705}
706
Elliott Hughesfc797672015-04-07 20:12:50 -0700707static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800708 fb_queue_notice("--------------------------------------------");
709 fb_queue_display("version-bootloader", "Bootloader Version...");
710 fb_queue_display("version-baseband", "Baseband Version.....");
711 fb_queue_display("serialno", "Serial Number........");
712 fb_queue_notice("--------------------------------------------");
713}
714
Rom Lemarchand622810c2013-06-28 09:54:59 -0700715static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700716{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700717 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700718 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700719 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700720 }
721
Elliott Hughesfc797672015-04-07 20:12:50 -0700722 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700723 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700724 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700725 }
726
Elliott Hughes253c18d2015-03-18 22:47:09 -0700727 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700728 if (!out_s) {
729 die("Failed to allocate sparse file array\n");
730 }
731
732 files = sparse_file_resparse(s, max_size, out_s, files);
733 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700734 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700735 }
736
737 return out_s;
738}
739
David Pursell0b156632015-10-30 11:22:01 -0700740static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700741 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700742 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
743 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800744 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700745 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700746 }
747
Elliott Hughes77c0e662015-11-02 15:51:12 -0800748 // Some bootloaders (angler, for example) send spurious whitespace too.
749 max_download_size = android::base::Trim(max_download_size);
750
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700751 uint64_t limit;
752 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800753 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700754 return 0;
755 }
756 if (limit > 0) {
757 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
758 }
Colin Crossf8387882012-05-24 17:18:41 -0700759 return limit;
760}
761
David Pursell0b156632015-10-30 11:22:01 -0700762static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700763 int64_t limit;
764
765 if (sparse_limit == 0) {
766 return 0;
767 } else if (sparse_limit > 0) {
768 limit = sparse_limit;
769 } else {
770 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700771 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700772 }
773 if (target_sparse_limit > 0) {
774 limit = target_sparse_limit;
775 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700776 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700777 }
778 }
779
780 if (size > limit) {
781 return limit;
782 }
783
784 return 0;
785}
786
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700787// Until we get lazy inode table init working in make_ext4fs, we need to
788// erase partitions of type ext4 before flashing a filesystem so no stale
789// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700790static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800791 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700792 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800793 return false;
794 }
795 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700796}
797
David Pursell0b156632015-10-30 11:22:01 -0700798static int load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700799 int64_t sz = get_file_size(fd);
800 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000801 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700802 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700803
Elliott Hughesfc797672015-04-07 20:12:50 -0700804 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700805 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700806 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700807 sparse_file** s = load_sparse_files(fd, limit);
808 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700809 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700810 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700811 buf->type = FB_BUFFER_SPARSE;
812 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700813 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700814 void* data = load_fd(fd, &sz);
815 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700816 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700817 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000818 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700819 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700820
821 return 0;
822}
823
David Pursell0b156632015-10-30 11:22:01 -0700824static int load_buf(Transport* transport, const char *fname, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700825{
826 int fd;
827
828 fd = open(fname, O_RDONLY | O_BINARY);
829 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700830 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700831 }
832
David Pursell0b156632015-10-30 11:22:01 -0700833 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700834}
835
836static void flash_buf(const char *pname, struct fastboot_buffer *buf)
837{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700838 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700839
840 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800841 case FB_BUFFER_SPARSE: {
842 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700843 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700844 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700845 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800846 sparse_files.emplace_back(*s, sz);
847 ++s;
848 }
849
850 for (size_t i = 0; i < sparse_files.size(); ++i) {
851 const auto& pair = sparse_files[i];
852 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700853 }
854 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800855 }
856
Rom Lemarchand622810c2013-06-28 09:54:59 -0700857 case FB_BUFFER:
858 fb_queue_flash(pname, buf->data, buf->sz);
859 break;
860 default:
861 die("unknown buffer type: %d", buf->type);
862 }
863}
864
David Pursell0b156632015-10-30 11:22:01 -0700865static std::vector<std::string> get_suffixes(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700866 std::vector<std::string> suffixes;
867 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700868 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700869 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700870 }
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700871 suffixes = android::base::Split(suffix_list, ",");
872 // Unfortunately some devices will return an error message in the
873 // guise of a valid value. If we only see only one suffix, it's probably
874 // not real.
875 if (suffixes.size() == 1) {
876 suffixes.clear();
877 }
878 return suffixes;
879}
880
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700881static bool supports_AB(Transport* transport) {
882 return !get_suffixes(transport).empty();
883}
884
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700885// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700886static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700887 std::vector<std::string> suffixes = get_suffixes(transport);
888
889 if (!suffixes.empty()) {
890 for (size_t i = 0; i < suffixes.size(); i++) {
891 if (current_slot == suffixes[i]) {
892 return suffixes[(i+1)%suffixes.size()];
893 }
894 }
895 }
896 return "";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700897}
898
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700899static std::string get_other_slot(Transport* transport) {
900 std::string current_slot;
901 fb_getvar(transport, "current-slot", &current_slot);
902 return get_other_slot(transport, current_slot);
903}
904
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800905static std::string verify_slot(Transport* transport, const char *slot, bool allow_all) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700906 if (strcmp(slot, "all") == 0) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800907 if (allow_all) {
908 return "all";
909 } else {
910 std::vector<std::string> suffixes = get_suffixes(transport);
911 if (!suffixes.empty()) {
912 return suffixes[0];
913 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800914 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800915 }
916 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700917 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800918
David Pursell0b156632015-10-30 11:22:01 -0700919 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800920
921 if (strcmp(slot, "other") == 0) {
Daniel Rosenberg8c32da32016-06-27 16:46:50 -0700922 std::string current_slot;
923 if (!fb_getvar(transport, "current-slot", &current_slot)) {
924 die("Failed to identify current slot.");
925 }
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700926 std::string other = get_other_slot(transport, current_slot);
927 if (other == "") {
928 die("No known slots.");
Daniel Rosenberg8c32da32016-06-27 16:46:50 -0700929 }
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700930 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800931 }
932
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700933 for (const std::string &suffix : suffixes) {
934 if (suffix == slot)
935 return slot;
936 }
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700937 if (suffixes.empty()) {
938 fprintf(stderr, "Device does not support slots.\n");
939 } else {
940 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
941 for (const std::string &suffix : suffixes) {
942 fprintf(stderr, "%s\n", suffix.c_str());
943 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700944 }
945 exit(1);
946}
947
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800948static std::string verify_slot(Transport* transport, const char *slot) {
949 return verify_slot(transport, slot, true);
950}
951
David Pursell0b156632015-10-30 11:22:01 -0700952static void do_for_partition(Transport* transport, const char *part, const char *slot,
953 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800954 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700955 std::string current_slot;
956
David Pursell0b156632015-10-30 11:22:01 -0700957 if (!fb_getvar(transport, std::string("has-slot:")+part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800958 /* If has-slot is not supported, the answer is no. */
959 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700960 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800961 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700962 if (!slot || slot[0] == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700963 if (!fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700964 die("Failed to identify current slot.\n");
965 }
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800966 func(std::string(part) + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700967 } else {
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800968 func(std::string(part) + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700969 }
970 } else {
971 if (force_slot && slot && slot[0]) {
David Pursell0b156632015-10-30 11:22:01 -0700972 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
973 part, slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700974 }
975 func(part);
976 }
977}
978
David Pursell0b156632015-10-30 11:22:01 -0700979/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
980 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
981 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
982 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700983 */
David Pursell0b156632015-10-30 11:22:01 -0700984static void do_for_partitions(Transport* transport, const char *part, const char *slot,
985 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800986 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700987
988 if (slot && strcmp(slot, "all") == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700989 if (!fb_getvar(transport, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700990 die("Could not check if partition %s has slot.", part);
991 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800992 if (has_slot == "yes") {
David Pursell0b156632015-10-30 11:22:01 -0700993 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700994 if (suffixes.empty()) {
995 die("Error reading suffixes.\n");
996 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700997 for (std::string &suffix : suffixes) {
David Pursell0b156632015-10-30 11:22:01 -0700998 do_for_partition(transport, part, suffix.c_str(), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700999 }
1000 } else {
David Pursell0b156632015-10-30 11:22:01 -07001001 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001002 }
1003 } else {
David Pursell0b156632015-10-30 11:22:01 -07001004 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001005 }
1006}
1007
David Pursell0b156632015-10-30 11:22:01 -07001008static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001009 struct fastboot_buffer buf;
1010
David Pursell0b156632015-10-30 11:22:01 -07001011 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001012 die("cannot load '%s'", fname);
1013 }
1014 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001015}
1016
Elliott Hughesfc797672015-04-07 20:12:50 -07001017static void do_update_signature(ZipArchiveHandle zip, char* fn) {
1018 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001019 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001020 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001021 fb_queue_download("signature", data, sz);
1022 fb_queue_command("signature", "installing signature");
1023}
1024
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -07001025// Sets slot_override as the active slot. If slot_override is blank,
1026// set current slot as active instead. This clears slot-unbootable.
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001027static void set_active(Transport* transport, const std::string& slot_override) {
1028 if (slot_override != "") {
1029 fb_set_active(slot_override.c_str());
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -07001030 } else {
1031 std::string current_slot;
1032 if (fb_getvar(transport, "current-slot", &current_slot)) {
1033 current_slot = verify_slot(transport, current_slot.c_str(), false);
1034 fb_set_active(current_slot.c_str());
1035 }
1036 }
1037}
1038
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001039static void do_update(Transport* transport, const char* filename, const std::string& slot_override, bool erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001040 queue_info_dump();
1041
Wink Savilleb98762f2011-04-04 17:54:59 -07001042 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1043
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001044 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001045 int error = OpenArchive(filename, &zip);
1046 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001047 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001048 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001049 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001050
Elliott Hughesfc797672015-04-07 20:12:50 -07001051 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001052 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001053 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001054 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001055 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001056 }
1057
Elliott Hughes253c18d2015-03-18 22:47:09 -07001058 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001059
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001060 std::string secondary;
1061 bool update_secondary = slot_override != "all";
1062 if (update_secondary) {
1063 if (slot_override != "") {
1064 secondary = get_other_slot(transport, slot_override);
1065 } else {
1066 secondary = get_other_slot(transport);
1067 }
1068 if (secondary == "") {
1069 if (supports_AB(transport)) {
1070 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1071 }
1072 update_secondary = false;
1073 }
1074 }
Elliott Hughesacdbe922015-06-02 21:37:05 -07001075 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001076 const char* slot = slot_override.c_str();
1077 if (images[i].is_secondary) {
1078 if (update_secondary) {
1079 slot = secondary.c_str();
1080 } else {
1081 continue;
1082 }
1083 }
1084
Elliott Hughes253c18d2015-03-18 22:47:09 -07001085 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001086 if (fd == -1) {
1087 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001088 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001089 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001090 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001091 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001092 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001093 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -07001094 int rc = load_buf_fd(transport, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001095 if (rc) die("cannot load %s from flash", images[i].img_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001096
1097 auto update = [&](const std::string &partition) {
1098 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001099 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001100 fb_queue_erase(partition.c_str());
1101 }
1102 flash_buf(partition.c_str(), &buf);
1103 /* not closing the fd here since the sparse code keeps the fd around
1104 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
1105 * program exits.
1106 */
1107 };
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001108 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001109 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001110
1111 CloseArchive(zip);
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -07001112 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001113}
1114
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001115static void do_send_signature(const std::string& fn) {
1116 std::size_t extension_loc = fn.find(".img");
1117 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001118
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001119 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001120
1121 int64_t sz;
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001122 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001123 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001124 fb_queue_download("signature", data, sz);
1125 fb_queue_command("signature", "installing signature");
1126}
1127
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001128static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool flash_primary, bool flash_secondary) {
1129 std::string fname;
1130 if (flash_primary) {
1131 queue_info_dump();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001132
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001133 fb_queue_query_save("product", cur_product, sizeof(cur_product));
Wink Savilleb98762f2011-04-04 17:54:59 -07001134
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001135 fname = find_item("info", product);
1136 if (fname == "") die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001137
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001138 int64_t sz;
1139 void* data = load_file(fname.c_str(), &sz);
1140 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001141
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001142 setup_requirements(reinterpret_cast<char*>(data), sz);
1143 }
1144 std::string secondary;
1145 if (flash_secondary) {
1146 if (slot_override != "") {
1147 secondary = get_other_slot(transport, slot_override);
1148 } else {
1149 secondary = get_other_slot(transport);
1150 }
1151 if (secondary == "") {
1152 if (supports_AB(transport)) {
1153 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1154 }
1155 flash_secondary = false;
1156 }
1157 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001158
1159 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001160 const char* slot = NULL;
1161 if (images[i].is_secondary) {
1162 if (flash_secondary) slot = secondary.c_str();
1163 } else {
1164 if (flash_primary) slot = slot_override.c_str();
1165 }
1166 if (!slot) continue;
1167 fname = find_item_given_name(images[i].img_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001168 fastboot_buffer buf;
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001169 if (load_buf(transport, fname.c_str(), &buf)) {
1170 if (images[i].is_optional) continue;
Rom Lemarchand622810c2013-06-28 09:54:59 -07001171 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001172 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001173
1174 auto flashall = [&](const std::string &partition) {
1175 do_send_signature(fname);
David Pursell0b156632015-10-30 11:22:01 -07001176 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001177 fb_queue_erase(partition.c_str());
1178 }
1179 flash_buf(partition.c_str(), &buf);
1180 };
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001181 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001182 }
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -07001183
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001184 if (flash_primary) set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001185}
1186
1187#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001188#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001189
Elliott Hughes45be42e2015-09-02 20:15:48 -07001190static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001191{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001192 if (argc <= 2) return 0;
1193 skip(2);
1194
1195 /*
1196 * Process unlock_bootloader, we have to load the message file
1197 * and send that to the remote device.
1198 */
1199 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001200
1201 int64_t sz;
1202 void* data = load_file(*argv, &sz);
1203 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001204 fb_queue_download("unlock_message", data, sz);
1205 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1206 skip(1);
1207 return 0;
1208}
1209
Elliott Hughes45be42e2015-09-02 20:15:48 -07001210static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001211{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001212 char command[256];
1213 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001214
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001215 command[0] = 0;
1216 while(1) {
1217 strcat(command,*argv);
1218 skip(1);
1219 if(argc == 0) break;
1220 strcat(command," ");
1221 }
1222
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001223 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001224 return 0;
1225}
1226
Colin Crossf8387882012-05-24 17:18:41 -07001227static int64_t parse_num(const char *arg)
1228{
1229 char *endptr;
1230 unsigned long long num;
1231
1232 num = strtoull(arg, &endptr, 0);
1233 if (endptr == arg) {
1234 return -1;
1235 }
1236
1237 if (*endptr == 'k' || *endptr == 'K') {
1238 if (num >= (-1ULL) / 1024) {
1239 return -1;
1240 }
1241 num *= 1024LL;
1242 endptr++;
1243 } else if (*endptr == 'm' || *endptr == 'M') {
1244 if (num >= (-1ULL) / (1024 * 1024)) {
1245 return -1;
1246 }
1247 num *= 1024LL * 1024LL;
1248 endptr++;
1249 } else if (*endptr == 'g' || *endptr == 'G') {
1250 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1251 return -1;
1252 }
1253 num *= 1024LL * 1024LL * 1024LL;
1254 endptr++;
1255 }
1256
1257 if (*endptr != '\0') {
1258 return -1;
1259 }
1260
1261 if (num > INT64_MAX) {
1262 return -1;
1263 }
1264
1265 return num;
1266}
1267
David Pursell0b156632015-10-30 11:22:01 -07001268static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001269 const char* partition, int skip_if_not_supported,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001270 const char* type_override, const char* size_override,
1271 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001272 std::string partition_type, partition_size;
1273
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001274 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001275 const char* errMsg = nullptr;
1276 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001277 int fd;
1278
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001279 unsigned int limit = INT_MAX;
1280 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001281 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001282 }
1283 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001284 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001285 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001286
David Pursell0b156632015-10-30 11:22:01 -07001287 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001288 errMsg = "Can't determine partition type.\n";
1289 goto failed;
1290 }
JP Abgrall7e859742014-05-06 15:14:15 -07001291 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001292 if (partition_type != type_override) {
1293 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1294 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001295 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001296 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001297 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001298
David Pursell0b156632015-10-30 11:22:01 -07001299 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001300 errMsg = "Unable to get partition size\n";
1301 goto failed;
1302 }
JP Abgrall7e859742014-05-06 15:14:15 -07001303 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001304 if (partition_size != size_override) {
1305 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1306 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001307 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001308 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001309 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001310 // Some bootloaders (angler, for example), send spurious leading whitespace.
1311 partition_size = android::base::Trim(partition_size);
1312 // Some bootloaders (hammerhead, for example) use implicit hex.
1313 // This code used to use strtol with base 16.
1314 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001315
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001316 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001317 if (!gen) {
1318 if (skip_if_not_supported) {
1319 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001320 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001321 return;
1322 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001323 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1324 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001325 return;
1326 }
1327
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001328 int64_t size;
1329 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1330 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1331 return;
1332 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001333
1334 fd = fileno(tmpfile());
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001335 if (fs_generator_generate(gen, fd, size, initial_dir)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001336 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001337 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001338 return;
1339 }
1340
David Pursell0b156632015-10-30 11:22:01 -07001341 if (load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001342 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001343 close(fd);
1344 return;
1345 }
1346 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001347 return;
1348
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001349failed:
1350 if (skip_if_not_supported) {
1351 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001352 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001353 }
1354 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
1355}
1356
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001357int main(int argc, char **argv)
1358{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001359 bool wants_wipe = false;
1360 bool wants_reboot = false;
1361 bool wants_reboot_bootloader = false;
1362 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001363 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001364 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001365 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001366 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001367 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001368 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001369 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001370
JP Abgrall7b8970c2013-03-07 17:06:41 -08001371 const struct option longopts[] = {
1372 {"base", required_argument, 0, 'b'},
1373 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001374 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001375 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001376 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001377 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001378 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001379 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001380 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001381 {"help", no_argument, 0, 'h'},
1382 {"unbuffered", no_argument, 0, 0},
1383 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001384 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001385 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001386 {"set-active", optional_argument, 0, 'a'},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001387#if !defined(_WIN32)
1388 {"wipe-and-use-fbe", no_argument, 0, 0},
1389#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001390 {0, 0, 0, 0}
1391 };
Colin Cross8879f982012-05-22 17:53:34 -07001392
1393 serial = getenv("ANDROID_SERIAL");
1394
1395 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001396 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 -07001397 if (c < 0) {
1398 break;
1399 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001400 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001401 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001402 case 'a':
1403 wants_set_active = true;
1404 if (optarg)
1405 next_active = optarg;
1406 break;
Colin Cross8879f982012-05-22 17:53:34 -07001407 case 'b':
1408 base_addr = strtoul(optarg, 0, 16);
1409 break;
Colin Cross8879f982012-05-22 17:53:34 -07001410 case 'c':
1411 cmdline = optarg;
1412 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001413 case 'h':
1414 usage();
1415 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001416 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001417 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001418 unsigned long val;
1419
1420 val = strtoul(optarg, &endptr, 0);
1421 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1422 die("invalid vendor id '%s'", optarg);
1423 vendor_id = (unsigned short)val;
1424 break;
1425 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001426 case 'k':
1427 kernel_offset = strtoul(optarg, 0, 16);
1428 break;
1429 case 'l':
1430 long_listing = 1;
1431 break;
1432 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001433 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001434 if (!page_size) die("invalid page size");
1435 break;
1436 case 'p':
1437 product = optarg;
1438 break;
1439 case 'r':
1440 ramdisk_offset = strtoul(optarg, 0, 16);
1441 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001442 case 't':
1443 tags_offset = strtoul(optarg, 0, 16);
1444 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001445 case 's':
1446 serial = optarg;
1447 break;
1448 case 'S':
1449 sparse_limit = parse_num(optarg);
1450 if (sparse_limit < 0) {
1451 die("invalid sparse limit");
1452 }
1453 break;
1454 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001455 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001456 break;
1457 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001458 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001459 break;
Colin Cross8879f982012-05-22 17:53:34 -07001460 case '?':
1461 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001462 case 0:
1463 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001464 setvbuf(stdout, nullptr, _IONBF, 0);
1465 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001466 } else if (strcmp("version", longopts[longindex].name) == 0) {
1467 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1468 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001469 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1470 slot_override = std::string(optarg);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001471#if !defined(_WIN32)
1472 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1473 wants_wipe = true;
1474 set_fbe_marker = true;
1475#endif
1476 } else {
1477 fprintf(stderr, "Internal error in options processing for %s\n",
1478 longopts[longindex].name);
1479 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001480 }
1481 break;
Colin Cross8879f982012-05-22 17:53:34 -07001482 default:
1483 abort();
1484 }
1485 }
1486
1487 argc -= optind;
1488 argv += optind;
1489
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001490 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001491 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001492 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001493 }
1494
Colin Cross8fb6e062012-07-24 16:36:41 -07001495 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001496 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001497 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001498 return 0;
1499 }
1500
Colin Crossc7b75dc2012-08-29 18:17:06 -07001501 if (argc > 0 && !strcmp(*argv, "help")) {
1502 usage();
1503 return 0;
1504 }
1505
David Pursell0b156632015-10-30 11:22:01 -07001506 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001507 if (transport == nullptr) {
1508 return 1;
1509 }
1510
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001511 if (slot_override != "") slot_override = verify_slot(transport, slot_override.c_str());
1512 if (next_active != "") next_active = verify_slot(transport, next_active.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001513
1514 if (wants_set_active) {
1515 if (next_active == "") {
1516 if (slot_override == "") {
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -07001517 std::string current_slot;
1518 if (fb_getvar(transport, "current-slot", &current_slot)) {
1519 next_active = verify_slot(transport, current_slot.c_str(), false);
1520 } else {
1521 wants_set_active = false;
1522 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001523 } else {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001524 next_active = verify_slot(transport, slot_override.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001525 }
1526 }
1527 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001528
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001529 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001530 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001531 require(2);
1532 fb_queue_display(argv[1], argv[1]);
1533 skip(2);
1534 } else if(!strcmp(*argv, "erase")) {
1535 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001536
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001537 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001538 std::string partition_type;
1539 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001540 fs_get_generator(partition_type) != nullptr) {
1541 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1542 partition_type.c_str());
1543 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001544
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001545 fb_queue_erase(partition.c_str());
1546 };
David Pursell0b156632015-10-30 11:22:01 -07001547 do_for_partitions(transport, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001548 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001549 } else if(!strncmp(*argv, "format", strlen("format"))) {
1550 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001551 char *type_override = nullptr;
1552 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001553 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001554 /*
1555 * Parsing for: "format[:[type][:[size]]]"
1556 * Some valid things:
1557 * - select ontly the size, and leave default fs type:
1558 * format::0x4000000 userdata
1559 * - default fs type and size:
1560 * format userdata
1561 * format:: userdata
1562 */
1563 overrides = strchr(*argv, ':');
1564 if (overrides) {
1565 overrides++;
1566 size_override = strchr(overrides, ':');
1567 if (size_override) {
1568 size_override[0] = '\0';
1569 size_override++;
1570 }
1571 type_override = overrides;
1572 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001573 if (type_override && !type_override[0]) type_override = nullptr;
1574 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001575
1576 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001577 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001578 fb_queue_erase(partition.c_str());
1579 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001580 fb_perform_format(transport, partition.c_str(), 0,
1581 type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001582 };
David Pursell0b156632015-10-30 11:22:01 -07001583 do_for_partitions(transport, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001584 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001585 } else if(!strcmp(*argv, "signature")) {
1586 require(2);
1587 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001588 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001589 if (sz != 256) die("signature must be 256 bytes");
1590 fb_queue_download("signature", data, sz);
1591 fb_queue_command("signature", "installing signature");
1592 skip(2);
1593 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001594 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001595 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001596 if (argc > 0) {
1597 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001598 wants_reboot = false;
1599 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001600 skip(1);
1601 }
1602 }
1603 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001604 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001605 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001606 skip(1);
1607 } else if (!strcmp(*argv, "continue")) {
1608 fb_queue_command("continue", "resuming boot");
1609 skip(1);
1610 } else if(!strcmp(*argv, "boot")) {
1611 char *kname = 0;
1612 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001613 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001614 skip(1);
1615 if (argc > 0) {
1616 kname = argv[0];
1617 skip(1);
1618 }
1619 if (argc > 0) {
1620 rname = argv[0];
1621 skip(1);
1622 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001623 if (argc > 0) {
1624 sname = argv[0];
1625 skip(1);
1626 }
1627 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001628 if (data == 0) return 1;
1629 fb_queue_download("boot.img", data, sz);
1630 fb_queue_command("boot", "booting");
1631 } else if(!strcmp(*argv, "flash")) {
1632 char *pname = argv[1];
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001633 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001634 require(2);
1635 if (argc > 2) {
1636 fname = argv[2];
1637 skip(3);
1638 } else {
1639 fname = find_item(pname, product);
1640 skip(2);
1641 }
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001642 if (fname == "") die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001643
1644 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001645 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001646 fb_queue_erase(partition.c_str());
1647 }
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001648 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001649 };
David Pursell0b156632015-10-30 11:22:01 -07001650 do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001651 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001652 char *kname = argv[2];
1653 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001654 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001655 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001656 skip(3);
1657 if (argc > 0) {
1658 rname = argv[0];
1659 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001660 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001661 if (argc > 0) {
1662 sname = argv[0];
1663 skip(1);
1664 }
1665 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001666 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001667 auto flashraw = [&](const std::string &partition) {
1668 fb_queue_flash(partition.c_str(), data, sz);
1669 };
David Pursell0b156632015-10-30 11:22:01 -07001670 do_for_partitions(transport, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001671 } else if(!strcmp(*argv, "flashall")) {
1672 skip(1);
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001673 if (slot_override == "all") {
1674 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.");
1675 do_flashall(transport, slot_override, erase_first, true, false);
1676 } else {
1677 do_flashall(transport, slot_override, erase_first, true, true);
1678 }
1679 wants_reboot = true;
1680 } else if(!strcmp(*argv, "flash-primary")) {
1681 skip(1);
1682 do_flashall(transport, slot_override, erase_first, true, false);
1683 wants_reboot = true;
1684 } else if(!strcmp(*argv, "flash-secondary")) {
1685 skip(1);
1686 do_flashall(transport, slot_override, erase_first, false, true);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001687 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001688 } else if(!strcmp(*argv, "update")) {
1689 if (argc > 1) {
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001690 do_update(transport, argv[1], slot_override, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001691 skip(2);
1692 } else {
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001693 do_update(transport, "update.zip", slot_override, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001694 skip(1);
1695 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001696 wants_reboot = 1;
1697 } else if(!strcmp(*argv, "set_active")) {
1698 require(2);
1699 std::string slot = verify_slot(transport, argv[1], false);
1700 fb_set_active(slot.c_str());
1701 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001702 } else if(!strcmp(*argv, "oem")) {
1703 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001704 } else if(!strcmp(*argv, "flashing")) {
1705 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1706 !strcmp(*(argv+1), "lock") ||
1707 !strcmp(*(argv+1), "unlock_critical") ||
1708 !strcmp(*(argv+1), "lock_critical") ||
1709 !strcmp(*(argv+1), "get_unlock_ability") ||
1710 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1711 !strcmp(*(argv+1), "lock_bootloader"))) {
1712 argc = do_oem_command(argc, argv);
1713 } else
1714 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1715 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001716 } else {
1717 usage();
1718 return 1;
1719 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001720 } else {
1721 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001722 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001723 }
1724 }
1725
1726 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001727 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001728 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001729 if (set_fbe_marker) {
1730 fprintf(stderr, "setting FBE marker...\n");
1731 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1732 if (initial_userdata_dir.empty()) {
1733 return 1;
1734 }
1735 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
1736 delete_fbemarker_tmpdir(initial_userdata_dir);
1737 } else {
1738 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
1739 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001740
1741 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001742 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001743 fprintf(stderr, "wiping cache...\n");
1744 fb_queue_erase("cache");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001745 fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001746 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001747 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001748 if (wants_set_active) {
1749 fb_set_active(next_active.c_str());
1750 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001751 if (wants_reboot) {
1752 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001753 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001754 } else if (wants_reboot_bootloader) {
1755 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001756 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001757 }
1758
David Pursell0b156632015-10-30 11:22:01 -07001759 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001760}