blob: 8714471e1b17215a8e6d41e14821c8acc10e3ac4 [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 Hughes749ae2d2016-06-28 14:48:45 -070050#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080051#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080052#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070053#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080054#include <android-base/strings.h>
Colin Crossf8387882012-05-24 17:18:41 -070055#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070056#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Elliott Hughes253c18d2015-03-18 22:47:09 -070058#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080059#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070061#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080062#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070063#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080064#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070065#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066
Colin Crossf8387882012-05-24 17:18:41 -070067#ifndef O_BINARY
68#define O_BINARY 0
69#endif
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
Rom Lemarchand622810c2013-06-28 09:54:59 -070088enum fb_buffer_type {
89 FB_BUFFER,
90 FB_BUFFER_SPARSE,
91};
92
93struct fastboot_buffer {
94 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070095 void* data;
96 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070097};
98
99static struct {
Alex Lightbb9b8a52016-06-29 09:26:44 -0700100 char img_name[17];
101 char sig_name[17];
Rom Lemarchand622810c2013-06-28 09:54:59 -0700102 char part_name[9];
103 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700104 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700105} images[] = {
Alex Lightbb9b8a52016-06-29 09:26:44 -0700106 {"boot.img", "boot.sig", "boot", false, false},
107 {"boot_other.img", "boot.sig", "boot", true, true},
108 {"recovery.img", "recovery.sig", "recovery", true, false},
109 {"system.img", "system.sig", "system", false, false},
110 {"system_other.img", "system.sig", "system", true, true},
111 {"vendor.img", "vendor.sig", "vendor", true, false},
112 {"vendor_other.img", "vendor.sig", "vendor", true, true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700113};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700114
Alex Lightbb9b8a52016-06-29 09:26:44 -0700115static std::string find_item_given_name(const char* img_name, const char* product) {
116 if(product) {
117 std::string path = get_my_path();
118 path.erase(path.find_last_of('/'));
119 return android::base::StringPrintf("%s/../../../target/product/%s/%s",
120 path.c_str(), product, img_name);
121 }
122
123 char *dir = getenv("ANDROID_PRODUCT_OUT");
124 if (dir == nullptr || dir[0] == '\0') {
125 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
126 }
127
128 return android::base::StringPrintf("%s/%s", dir, img_name);
129}
130
131std::string find_item(const char* item, const char* product) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700132 const char *fn;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700133
Elliott Hughes2810d002016-04-25 14:31:18 -0700134 if (!strcmp(item,"boot")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 fn = "boot.img";
136 } else if(!strcmp(item,"recovery")) {
137 fn = "recovery.img";
138 } else if(!strcmp(item,"system")) {
139 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700140 } else if(!strcmp(item,"vendor")) {
141 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 } else if(!strcmp(item,"userdata")) {
143 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700144 } else if(!strcmp(item,"cache")) {
145 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 } else if(!strcmp(item,"info")) {
147 fn = "android-info.txt";
148 } else {
149 fprintf(stderr,"unknown partition '%s'\n", item);
Elliott Hughes2810d002016-04-25 14:31:18 -0700150 return "";
Alex Lightbb9b8a52016-06-29 09:26:44 -0700151
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152 }
153
Alex Lightbb9b8a52016-06-29 09:26:44 -0700154 return find_item_given_name(fn, product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155}
156
Elliott Hughesfc797672015-04-07 20:12:50 -0700157static int64_t get_file_size(int fd) {
158 struct stat sb;
159 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700160}
161
Elliott Hughesfc797672015-04-07 20:12:50 -0700162static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800163 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700164 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165
Elliott Hughesfc797672015-04-07 20:12:50 -0700166 *sz = get_file_size(fd);
167 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700168 goto oops;
169 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170
Elliott Hughesfc797672015-04-07 20:12:50 -0700171 data = (char*) malloc(*sz);
172 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173
Elliott Hughesfc797672015-04-07 20:12:50 -0700174 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175 close(fd);
176
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 return data;
178
179oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800180 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181 close(fd);
182 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800183 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184 return 0;
185}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186
Elliott Hughes2810d002016-04-25 14:31:18 -0700187static void* load_file(const std::string& path, int64_t* sz) {
188 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700189 if (fd == -1) return nullptr;
190 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700191}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192
Elliott Hughesfc797672015-04-07 20:12:50 -0700193static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700194 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700195 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700196 return -1;
197 }
198
199 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
200 return -1;
201 }
202
Scott Anderson13081c62012-04-06 12:39:30 -0700203 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700205 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
206 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207 return 0;
208}
209
Elliott Hughesfc797672015-04-07 20:12:50 -0700210static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800211 return match_fastboot_with_serial(info, serial);
212}
213
Elliott Hughesfc797672015-04-07 20:12:50 -0700214static int list_devices_callback(usb_ifc_info* info) {
215 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800216 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700217 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800218 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700219 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 if (!serial[0]) {
221 serial = "????????????";
222 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700223 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700224 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800225 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700226 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800227 printf("%-22s fastboot", serial.c_str());
228 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700229 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800230 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 }
232
233 return -1;
234}
235
David Pursell2ec418a2016-01-20 08:32:08 -0800236// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
237// a specific device, otherwise the first USB device found will be used.
238//
239// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
240// Otherwise it blocks until the target is available.
241//
242// The returned Transport is a singleton, so multiple calls to this function will return the same
243// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700244static Transport* open_device() {
245 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800246 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247
David Pursell2ec418a2016-01-20 08:32:08 -0800248 if (transport != nullptr) {
249 return transport;
250 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800251
David Pursell4601c972016-02-05 15:35:09 -0800252 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800253 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800254 int port = 0;
255 if (serial != nullptr) {
256 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800257
David Pursell4601c972016-02-05 15:35:09 -0800258 if (android::base::StartsWith(serial, "tcp:")) {
259 protocol = Socket::Protocol::kTcp;
260 port = tcp::kDefaultPort;
261 net_address = serial + strlen("tcp:");
262 } else if (android::base::StartsWith(serial, "udp:")) {
263 protocol = Socket::Protocol::kUdp;
264 port = udp::kDefaultPort;
265 net_address = serial + strlen("udp:");
266 }
267
268 if (net_address != nullptr) {
269 std::string error;
270 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
271 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
272 error.c_str());
273 return nullptr;
274 }
David Pursell2ec418a2016-01-20 08:32:08 -0800275 }
276 }
277
278 while (true) {
279 if (!host.empty()) {
280 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800281 if (protocol == Socket::Protocol::kTcp) {
282 transport = tcp::Connect(host, port, &error).release();
283 } else if (protocol == Socket::Protocol::kUdp) {
284 transport = udp::Connect(host, port, &error).release();
285 }
286
David Pursell2ec418a2016-01-20 08:32:08 -0800287 if (transport == nullptr && announce) {
288 fprintf(stderr, "error: %s\n", error.c_str());
289 }
290 } else {
291 transport = usb_open(match_fastboot);
292 }
293
294 if (transport != nullptr) {
295 return transport;
296 }
297
David Pursell0b156632015-10-30 11:22:01 -0700298 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800299 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700300 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700302 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 }
304}
305
Elliott Hughesfc797672015-04-07 20:12:50 -0700306static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 // We don't actually open a USB device here,
308 // just getting our callback called so we can
309 // list all the connected devices.
310 usb_open(list_devices_callback);
311}
312
Elliott Hughesfc797672015-04-07 20:12:50 -0700313static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314 fprintf(stderr,
315/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
316 "usage: fastboot [ <option> ] <command>\n"
317 "\n"
318 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700319 " update <filename> Reflash device from update.zip.\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700320 " Sets the flashed slot as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700321 " flashall Flash boot, system, vendor, and --\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700322 " if found -- recovery. If the device\n"
323 " supports slots, the slot that has\n"
324 " been flashed to is set as active.\n"
Alex Lightbb9b8a52016-06-29 09:26:44 -0700325 " Secondary images may be flashed to\n"
326 " an inactive slot.\n"
327 " flash-primary Same as flashall, but do not flash\n"
328 " secondary images.\n"
329 " flash-secondary Only flashes the secondary images.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700330 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
331 " flashing lock Locks the device. Prevents flashing.\n"
332 " flashing unlock Unlocks the device. Allows flashing\n"
333 " any partition except\n"
334 " bootloader-related partitions.\n"
335 " flashing lock_critical Prevents flashing bootloader-related\n"
336 " partitions.\n"
337 " flashing unlock_critical Enables flashing bootloader-related\n"
338 " partitions.\n"
339 " flashing get_unlock_ability Queries bootloader to see if the\n"
340 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700341 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700342 " unlock nonce.\n"
343 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700344 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700345 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700346 " erase <partition> Erase a flash partition.\n"
347 " format[:[<fs type>][:[<size>]] <partition>\n"
348 " Format a flash partition. Can\n"
349 " override the fs type and/or size\n"
350 " the bootloader reports.\n"
351 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800352 " set_active <suffix> Sets the active slot. If slots are\n"
353 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700354 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
355 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
356 " Create bootimage and flash it.\n"
357 " devices [-l] List all connected devices [with\n"
358 " device paths].\n"
359 " continue Continue with autoboot.\n"
360 " reboot [bootloader] Reboot device [into bootloader].\n"
361 " reboot-bootloader Reboot device into bootloader.\n"
362 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 "\n"
364 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700365 " -w Erase userdata and cache (and format\n"
366 " if supported by partition type).\n"
367 " -u Do not erase partition before\n"
368 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800369 " -s <specific device> Specify a device. For USB, provide either\n"
370 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700371 " For ethernet, provide an address in the\n"
372 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800373 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700374 " -p <product> Specify product name.\n"
375 " -c <cmdline> Override kernel commandline.\n"
376 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800377 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700378 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800379 " --kernel-offset Specify a custom kernel offset.\n"
380 " (default: 0x00008000)\n"
381 " --ramdisk-offset Specify a custom ramdisk offset.\n"
382 " (default: 0x01000000)\n"
383 " --tags-offset Specify a custom tags offset.\n"
384 " (default: 0x00000100)\n"
385 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700386 " (default: 2048).\n"
387 " -S <size>[K|M|G] Automatically sparse files greater\n"
388 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700389 " --slot <suffix> Specify slot suffix to be used if the\n"
390 " device supports slots. This will be\n"
391 " added to all partition names that use\n"
392 " slots. 'all' can be given to refer\n"
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800393 " to all slots. 'other' can be given to\n"
394 " refer to a non-current slot. If this\n"
395 " flag is not used, slotted partitions\n"
396 " will default to the current active slot.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800397 " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800398 " provided, this will default to the value\n"
399 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800400 " supported, this does nothing. This will\n"
401 " run after all non-reboot commands.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800402 " --unbuffered Do not buffer input or output.\n"
403 " --version Display version.\n"
404 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406}
Elliott Hughesfc797672015-04-07 20:12:50 -0700407static void* load_bootable_image(const char* kernel, const char* ramdisk,
408 const char* secondstage, int64_t* sz,
409 const char* cmdline) {
410 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411 fprintf(stderr, "no image specified\n");
412 return 0;
413 }
414
Elliott Hughesfc797672015-04-07 20:12:50 -0700415 int64_t ksize;
416 void* kdata = load_file(kernel, &ksize);
417 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800418 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419 return 0;
420 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800421
Elliott Hughesfc797672015-04-07 20:12:50 -0700422 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700424 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800425
Elliott Hughesfc797672015-04-07 20:12:50 -0700426 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
428 return 0;
429 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800430
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431 *sz = ksize;
432 return kdata;
433 }
434
Elliott Hughesfc797672015-04-07 20:12:50 -0700435 void* rdata = nullptr;
436 int64_t rsize = 0;
437 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700439 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800440 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 return 0;
442 }
443 }
444
Elliott Hughesfc797672015-04-07 20:12:50 -0700445 void* sdata = nullptr;
446 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200447 if (secondstage) {
448 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700449 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200450 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
451 return 0;
452 }
453 }
454
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700456 int64_t bsize = 0;
457 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800458 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200459 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800460 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700461 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 fprintf(stderr,"failed to create boot.img\n");
463 return 0;
464 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700465 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
466 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800468
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 return bdata;
470}
471
Elliott Hughesfc797672015-04-07 20:12:50 -0700472static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473{
Yusuke Sato07447542015-06-25 14:39:19 -0700474 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700475 ZipEntry zip_entry;
476 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700477 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000478 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 }
480
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700481 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700483 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700484 if (data == nullptr) {
485 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000486 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 }
488
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700489 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
490 if (error != 0) {
491 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000493 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000495
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496 return data;
497}
498
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700499#if defined(_WIN32)
500
501// TODO: move this to somewhere it can be shared.
502
503#include <windows.h>
504
505// Windows' tmpfile(3) requires administrator rights because
506// it creates temporary files in the root directory.
507static FILE* win32_tmpfile() {
508 char temp_path[PATH_MAX];
509 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
510 if (nchars == 0 || nchars >= sizeof(temp_path)) {
511 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
512 return nullptr;
513 }
514
515 char filename[PATH_MAX];
516 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
517 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
518 return nullptr;
519 }
520
521 return fopen(filename, "w+bTD");
522}
523
524#define tmpfile win32_tmpfile
525
526#endif
527
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700528static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
529 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700530 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700531 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
532 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700533 return -1;
534 }
535
Yusuke Sato07447542015-06-25 14:39:19 -0700536 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700537 ZipEntry zip_entry;
538 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
539 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700540 fclose(fp);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000541 return -1;
542 }
543
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700544 int fd = fileno(fp);
545 int error = ExtractEntryToFile(zip, &zip_entry, fd);
546 if (error != 0) {
547 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700548 fclose(fp);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700549 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700550 }
551
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000552 lseek(fd, 0, SEEK_SET);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700553 // TODO: We're leaking 'fp' here.
Rom Lemarchand622810c2013-06-28 09:54:59 -0700554 return fd;
555}
556
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800557static char *strip(char *s)
558{
559 int n;
560 while(*s && isspace(*s)) s++;
561 n = strlen(s);
562 while(n-- > 0) {
563 if(!isspace(s[n])) break;
564 s[n] = 0;
565 }
566 return s;
567}
568
569#define MAX_OPTIONS 32
570static int setup_requirement_line(char *name)
571{
572 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700573 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800574 unsigned n, count;
575 char *x;
576 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800577
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800578 if (!strncmp(name, "reject ", 7)) {
579 name += 7;
580 invert = 1;
581 } else if (!strncmp(name, "require ", 8)) {
582 name += 8;
583 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700584 } else if (!strncmp(name, "require-for-product:", 20)) {
585 // Get the product and point name past it
586 prod = name + 20;
587 name = strchr(name, ' ');
588 if (!name) return -1;
589 *name = 0;
590 name += 1;
591 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800592 }
593
594 x = strchr(name, '=');
595 if (x == 0) return 0;
596 *x = 0;
597 val[0] = x + 1;
598
599 for(count = 1; count < MAX_OPTIONS; count++) {
600 x = strchr(val[count - 1],'|');
601 if (x == 0) break;
602 *x = 0;
603 val[count] = x + 1;
604 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800605
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800606 name = strip(name);
607 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800608
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800609 name = strip(name);
610 if (name == 0) return -1;
611
Elliott Hughes253c18d2015-03-18 22:47:09 -0700612 const char* var = name;
613 // Work around an unfortunate name mismatch.
614 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800615
Elliott Hughes253c18d2015-03-18 22:47:09 -0700616 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800617 if (out == 0) return -1;
618
619 for(n = 0; n < count; n++) {
620 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700621 if (out[n] == 0) {
622 for(size_t i = 0; i < n; ++i) {
623 free((char*) out[i]);
624 }
625 free(out);
626 return -1;
627 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800628 }
629
Elliott Hughes253c18d2015-03-18 22:47:09 -0700630 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800631 return 0;
632}
633
Elliott Hughesfc797672015-04-07 20:12:50 -0700634static void setup_requirements(char* data, int64_t sz) {
635 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800636 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700637 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800638 *s++ = 0;
639 if (setup_requirement_line(data)) {
640 die("out of memory");
641 }
642 data = s;
643 } else {
644 s++;
645 }
646 }
647}
648
Elliott Hughesfc797672015-04-07 20:12:50 -0700649static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650 fb_queue_notice("--------------------------------------------");
651 fb_queue_display("version-bootloader", "Bootloader Version...");
652 fb_queue_display("version-baseband", "Baseband Version.....");
653 fb_queue_display("serialno", "Serial Number........");
654 fb_queue_notice("--------------------------------------------");
655}
656
Rom Lemarchand622810c2013-06-28 09:54:59 -0700657static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700658{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700659 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700660 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700661 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700662 }
663
Elliott Hughesfc797672015-04-07 20:12:50 -0700664 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700665 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700666 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700667 }
668
Elliott Hughes253c18d2015-03-18 22:47:09 -0700669 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700670 if (!out_s) {
671 die("Failed to allocate sparse file array\n");
672 }
673
674 files = sparse_file_resparse(s, max_size, out_s, files);
675 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700676 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700677 }
678
679 return out_s;
680}
681
David Pursell0b156632015-10-30 11:22:01 -0700682static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700683 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700684 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
685 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800686 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700687 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700688 }
689
Elliott Hughes77c0e662015-11-02 15:51:12 -0800690 // Some bootloaders (angler, for example) send spurious whitespace too.
691 max_download_size = android::base::Trim(max_download_size);
692
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700693 uint64_t limit;
694 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800695 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700696 return 0;
697 }
698 if (limit > 0) {
699 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
700 }
Colin Crossf8387882012-05-24 17:18:41 -0700701 return limit;
702}
703
David Pursell0b156632015-10-30 11:22:01 -0700704static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700705 int64_t limit;
706
707 if (sparse_limit == 0) {
708 return 0;
709 } else if (sparse_limit > 0) {
710 limit = sparse_limit;
711 } else {
712 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700713 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700714 }
715 if (target_sparse_limit > 0) {
716 limit = target_sparse_limit;
717 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700718 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700719 }
720 }
721
722 if (size > limit) {
723 return limit;
724 }
725
726 return 0;
727}
728
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700729// Until we get lazy inode table init working in make_ext4fs, we need to
730// erase partitions of type ext4 before flashing a filesystem so no stale
731// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700732static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800733 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700734 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800735 return false;
736 }
737 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700738}
739
Elliott Hughes53ec4952016-05-11 12:39:27 -0700740static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700741 int64_t sz = get_file_size(fd);
742 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700743 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700744 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700745
Elliott Hughesfc797672015-04-07 20:12:50 -0700746 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700747 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700748 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700749 sparse_file** s = load_sparse_files(fd, limit);
750 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700751 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700752 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700753 buf->type = FB_BUFFER_SPARSE;
754 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700755 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700756 void* data = load_fd(fd, &sz);
757 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700758 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700759 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000760 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700761 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700762
Elliott Hughes53ec4952016-05-11 12:39:27 -0700763 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700764}
765
Elliott Hughes53ec4952016-05-11 12:39:27 -0700766static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
767 int fd = open(fname, O_RDONLY | O_BINARY);
768 if (fd == -1) {
769 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700770 }
David Pursell0b156632015-10-30 11:22:01 -0700771 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700772}
773
774static void flash_buf(const char *pname, struct fastboot_buffer *buf)
775{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700776 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700777
778 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800779 case FB_BUFFER_SPARSE: {
780 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700781 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700782 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700783 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800784 sparse_files.emplace_back(*s, sz);
785 ++s;
786 }
787
788 for (size_t i = 0; i < sparse_files.size(); ++i) {
789 const auto& pair = sparse_files[i];
790 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700791 }
792 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800793 }
794
Rom Lemarchand622810c2013-06-28 09:54:59 -0700795 case FB_BUFFER:
796 fb_queue_flash(pname, buf->data, buf->sz);
797 break;
798 default:
799 die("unknown buffer type: %d", buf->type);
800 }
801}
802
David Pursell0b156632015-10-30 11:22:01 -0700803static std::vector<std::string> get_suffixes(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700804 std::vector<std::string> suffixes;
805 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700806 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700807 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700808 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700809 suffixes = android::base::Split(suffix_list, ",");
810 // Unfortunately some devices will return an error message in the
811 // guise of a valid value. If we only see only one suffix, it's probably
812 // not real.
813 if (suffixes.size() == 1) {
814 suffixes.clear();
815 }
816 return suffixes;
817}
818
Alex Lightbb9b8a52016-06-29 09:26:44 -0700819static bool supports_AB(Transport* transport) {
820 return !get_suffixes(transport).empty();
821}
822
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700823// Given a current slot, this returns what the 'other' slot is.
Alex Lightbb9b8a52016-06-29 09:26:44 -0700824static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700825 std::vector<std::string> suffixes = get_suffixes(transport);
826
827 if (!suffixes.empty()) {
828 for (size_t i = 0; i < suffixes.size(); i++) {
829 if (current_slot == suffixes[i]) {
830 return suffixes[(i+1)%suffixes.size()];
831 }
832 }
833 }
834 return "";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700835}
836
Alex Lightbb9b8a52016-06-29 09:26:44 -0700837static std::string get_other_slot(Transport* transport) {
838 std::string current_slot;
839 fb_getvar(transport, "current-slot", &current_slot);
840 return get_other_slot(transport, current_slot);
841}
842
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800843static std::string verify_slot(Transport* transport, const char *slot, bool allow_all) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700844 if (strcmp(slot, "all") == 0) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800845 if (allow_all) {
846 return "all";
847 } else {
848 std::vector<std::string> suffixes = get_suffixes(transport);
849 if (!suffixes.empty()) {
850 return suffixes[0];
851 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800852 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800853 }
854 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700855 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800856
David Pursell0b156632015-10-30 11:22:01 -0700857 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800858
859 if (strcmp(slot, "other") == 0) {
860 std::string current_slot;
861 if (!fb_getvar(transport, "current-slot", &current_slot)) {
862 die("Failed to identify current slot.");
863 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700864 std::string other = get_other_slot(transport, current_slot);
865 if (other == "") {
866 die("No known slots.");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800867 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700868 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800869 }
870
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700871 for (const std::string &suffix : suffixes) {
872 if (suffix == slot)
873 return slot;
874 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700875 if (suffixes.empty()) {
876 fprintf(stderr, "Device does not support slots.\n");
877 } else {
878 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
879 for (const std::string &suffix : suffixes) {
880 fprintf(stderr, "%s\n", suffix.c_str());
881 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700882 }
883 exit(1);
884}
885
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800886static std::string verify_slot(Transport* transport, const char *slot) {
887 return verify_slot(transport, slot, true);
888}
889
David Pursell0b156632015-10-30 11:22:01 -0700890static void do_for_partition(Transport* transport, const char *part, const char *slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700891 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800892 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700893 std::string current_slot;
894
David Pursell0b156632015-10-30 11:22:01 -0700895 if (!fb_getvar(transport, std::string("has-slot:")+part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800896 /* If has-slot is not supported, the answer is no. */
897 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700898 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800899 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700900 if (!slot || slot[0] == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700901 if (!fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700902 die("Failed to identify current slot.\n");
903 }
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800904 func(std::string(part) + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700905 } else {
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800906 func(std::string(part) + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700907 }
908 } else {
909 if (force_slot && slot && slot[0]) {
David Pursell0b156632015-10-30 11:22:01 -0700910 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
911 part, slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700912 }
913 func(part);
914 }
915}
916
David Pursell0b156632015-10-30 11:22:01 -0700917/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
918 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
919 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
920 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700921 */
David Pursell0b156632015-10-30 11:22:01 -0700922static void do_for_partitions(Transport* transport, const char *part, const char *slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700923 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800924 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700925
926 if (slot && strcmp(slot, "all") == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700927 if (!fb_getvar(transport, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700928 die("Could not check if partition %s has slot.", part);
929 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800930 if (has_slot == "yes") {
David Pursell0b156632015-10-30 11:22:01 -0700931 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700932 if (suffixes.empty()) {
933 die("Error reading suffixes.\n");
934 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700935 for (std::string &suffix : suffixes) {
David Pursell0b156632015-10-30 11:22:01 -0700936 do_for_partition(transport, part, suffix.c_str(), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700937 }
938 } else {
David Pursell0b156632015-10-30 11:22:01 -0700939 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700940 }
941 } else {
David Pursell0b156632015-10-30 11:22:01 -0700942 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700943 }
944}
945
David Pursell0b156632015-10-30 11:22:01 -0700946static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700947 struct fastboot_buffer buf;
948
Elliott Hughes53ec4952016-05-11 12:39:27 -0700949 if (!load_buf(transport, fname, &buf)) {
950 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700951 }
952 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700953}
954
Elliott Hughesfc797672015-04-07 20:12:50 -0700955static void do_update_signature(ZipArchiveHandle zip, char* fn) {
956 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700957 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700958 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800959 fb_queue_download("signature", data, sz);
960 fb_queue_command("signature", "installing signature");
961}
962
Daniel Rosenberg13454092016-06-27 19:43:11 -0700963// Sets slot_override as the active slot. If slot_override is blank,
964// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -0700965static void set_active(Transport* transport, const std::string& slot_override) {
966 if (!supports_AB(transport)) {
967 return;
968 } else if (slot_override != "") {
969 fb_set_active(slot_override.c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -0700970 } else {
971 std::string current_slot;
972 if (fb_getvar(transport, "current-slot", &current_slot)) {
973 current_slot = verify_slot(transport, current_slot.c_str(), false);
974 fb_set_active(current_slot.c_str());
975 }
976 }
977}
978
Alex Lightbb9b8a52016-06-29 09:26:44 -0700979static 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 -0800980 queue_info_dump();
981
Wink Savilleb98762f2011-04-04 17:54:59 -0700982 fb_queue_query_save("product", cur_product, sizeof(cur_product));
983
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700984 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700985 int error = OpenArchive(filename, &zip);
986 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100987 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700988 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700989 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800990
Elliott Hughesfc797672015-04-07 20:12:50 -0700991 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700992 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700993 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100994 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700995 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800996 }
997
Elliott Hughes253c18d2015-03-18 22:47:09 -0700998 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800999
Alex Lightbb9b8a52016-06-29 09:26:44 -07001000 std::string secondary;
1001 bool update_secondary = slot_override != "all";
1002 if (update_secondary) {
1003 if (slot_override != "") {
1004 secondary = get_other_slot(transport, slot_override);
1005 } else {
1006 secondary = get_other_slot(transport);
1007 }
1008 if (secondary == "") {
1009 if (supports_AB(transport)) {
1010 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1011 }
1012 update_secondary = false;
1013 }
1014 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001015 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001016 const char* slot = slot_override.c_str();
1017 if (images[i].is_secondary) {
1018 if (update_secondary) {
1019 slot = secondary.c_str();
1020 } else {
1021 continue;
1022 }
1023 }
1024
Elliott Hughes253c18d2015-03-18 22:47:09 -07001025 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001026 if (fd == -1) {
1027 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001028 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001029 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001030 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001031 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001032 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001033 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001034 if (!load_buf_fd(transport, fd, &buf)) {
1035 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1036 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001037
1038 auto update = [&](const std::string &partition) {
1039 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001040 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001041 fb_queue_erase(partition.c_str());
1042 }
1043 flash_buf(partition.c_str(), &buf);
1044 /* not closing the fd here since the sparse code keeps the fd around
1045 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
1046 * program exits.
1047 */
1048 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001049 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001050 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001051
1052 CloseArchive(zip);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001053 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001054}
1055
Alex Lightbb9b8a52016-06-29 09:26:44 -07001056static void do_send_signature(const std::string& fn) {
1057 std::size_t extension_loc = fn.find(".img");
1058 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001059
Alex Lightbb9b8a52016-06-29 09:26:44 -07001060 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001061
1062 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001063 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001064 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001065
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001066 fb_queue_download("signature", data, sz);
1067 fb_queue_command("signature", "installing signature");
1068}
1069
Alex Lightbb9b8a52016-06-29 09:26:44 -07001070static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool flash_primary, bool flash_secondary) {
1071 std::string fname;
1072 if (flash_primary) {
1073 queue_info_dump();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001074
Alex Lightbb9b8a52016-06-29 09:26:44 -07001075 fb_queue_query_save("product", cur_product, sizeof(cur_product));
Wink Savilleb98762f2011-04-04 17:54:59 -07001076
Alex Lightbb9b8a52016-06-29 09:26:44 -07001077 fname = find_item("info", product);
1078 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001079
Alex Lightbb9b8a52016-06-29 09:26:44 -07001080 int64_t sz;
1081 void* data = load_file(fname.c_str(), &sz);
1082 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001083
Alex Lightbb9b8a52016-06-29 09:26:44 -07001084 setup_requirements(reinterpret_cast<char*>(data), sz);
1085 }
1086 std::string secondary;
1087 if (flash_secondary) {
1088 if (slot_override != "") {
1089 secondary = get_other_slot(transport, slot_override);
1090 } else {
1091 secondary = get_other_slot(transport);
1092 }
1093 if (secondary == "") {
1094 if (supports_AB(transport)) {
1095 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1096 }
1097 flash_secondary = false;
1098 }
1099 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001100
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001101 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001102 const char* slot = NULL;
1103 if (images[i].is_secondary) {
1104 if (flash_secondary) slot = secondary.c_str();
1105 } else {
1106 if (flash_primary) slot = slot_override.c_str();
1107 }
1108 if (!slot) continue;
1109 fname = find_item_given_name(images[i].img_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001110 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001111 if (!load_buf(transport, fname.c_str(), &buf)) {
1112 if (images[i].is_optional) continue;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001113 die("could not load '%s': %s\n", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001114 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001115
1116 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001117 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001118 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001119 fb_queue_erase(partition.c_str());
1120 }
1121 flash_buf(partition.c_str(), &buf);
1122 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001123 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001124 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001125
Alex Lightbb9b8a52016-06-29 09:26:44 -07001126 if (flash_primary) set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001127}
1128
1129#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001130#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001131
Elliott Hughes45be42e2015-09-02 20:15:48 -07001132static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001133{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001134 if (argc <= 2) return 0;
1135 skip(2);
1136
1137 /*
1138 * Process unlock_bootloader, we have to load the message file
1139 * and send that to the remote device.
1140 */
1141 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001142
1143 int64_t sz;
1144 void* data = load_file(*argv, &sz);
1145 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001146 fb_queue_download("unlock_message", data, sz);
1147 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1148 skip(1);
1149 return 0;
1150}
1151
Elliott Hughes45be42e2015-09-02 20:15:48 -07001152static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001153{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001154 char command[256];
1155 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001156
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001157 command[0] = 0;
1158 while(1) {
1159 strcat(command,*argv);
1160 skip(1);
1161 if(argc == 0) break;
1162 strcat(command," ");
1163 }
1164
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001165 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001166 return 0;
1167}
1168
Colin Crossf8387882012-05-24 17:18:41 -07001169static int64_t parse_num(const char *arg)
1170{
1171 char *endptr;
1172 unsigned long long num;
1173
1174 num = strtoull(arg, &endptr, 0);
1175 if (endptr == arg) {
1176 return -1;
1177 }
1178
1179 if (*endptr == 'k' || *endptr == 'K') {
1180 if (num >= (-1ULL) / 1024) {
1181 return -1;
1182 }
1183 num *= 1024LL;
1184 endptr++;
1185 } else if (*endptr == 'm' || *endptr == 'M') {
1186 if (num >= (-1ULL) / (1024 * 1024)) {
1187 return -1;
1188 }
1189 num *= 1024LL * 1024LL;
1190 endptr++;
1191 } else if (*endptr == 'g' || *endptr == 'G') {
1192 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1193 return -1;
1194 }
1195 num *= 1024LL * 1024LL * 1024LL;
1196 endptr++;
1197 }
1198
1199 if (*endptr != '\0') {
1200 return -1;
1201 }
1202
1203 if (num > INT64_MAX) {
1204 return -1;
1205 }
1206
1207 return num;
1208}
1209
David Pursell0b156632015-10-30 11:22:01 -07001210static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001211 const char* partition, int skip_if_not_supported,
1212 const char* type_override, const char* size_override) {
1213 std::string partition_type, partition_size;
1214
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001215 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001216 const char* errMsg = nullptr;
1217 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001218 int fd;
1219
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001220 unsigned int limit = INT_MAX;
1221 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001222 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001223 }
1224 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001225 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001226 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001227
David Pursell0b156632015-10-30 11:22:01 -07001228 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001229 errMsg = "Can't determine partition type.\n";
1230 goto failed;
1231 }
JP Abgrall7e859742014-05-06 15:14:15 -07001232 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001233 if (partition_type != type_override) {
1234 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1235 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001236 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001237 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001238 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001239
David Pursell0b156632015-10-30 11:22:01 -07001240 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001241 errMsg = "Unable to get partition size\n";
1242 goto failed;
1243 }
JP Abgrall7e859742014-05-06 15:14:15 -07001244 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001245 if (partition_size != size_override) {
1246 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1247 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001248 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001249 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001250 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001251 // Some bootloaders (angler, for example), send spurious leading whitespace.
1252 partition_size = android::base::Trim(partition_size);
1253 // Some bootloaders (hammerhead, for example) use implicit hex.
1254 // This code used to use strtol with base 16.
1255 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001256
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001257 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001258 if (!gen) {
1259 if (skip_if_not_supported) {
1260 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001261 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001262 return;
1263 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001264 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1265 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001266 return;
1267 }
1268
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001269 int64_t size;
1270 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1271 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1272 return;
1273 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001274
1275 fd = fileno(tmpfile());
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001276 if (fs_generator_generate(gen, fd, size)) {
1277 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001278 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001279 return;
1280 }
1281
Elliott Hughes53ec4952016-05-11 12:39:27 -07001282 if (!load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001283 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001284 close(fd);
1285 return;
1286 }
1287 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001288 return;
1289
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001290failed:
1291 if (skip_if_not_supported) {
1292 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001293 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001294 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001295 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001296}
1297
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001298int main(int argc, char **argv)
1299{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001300 bool wants_wipe = false;
1301 bool wants_reboot = false;
1302 bool wants_reboot_bootloader = false;
1303 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001304 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001305 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001306 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001307 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001308 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001309 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001310
JP Abgrall7b8970c2013-03-07 17:06:41 -08001311 const struct option longopts[] = {
1312 {"base", required_argument, 0, 'b'},
1313 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001314 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001315 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001316 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001317 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001318 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001319 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001320 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001321 {"help", no_argument, 0, 'h'},
1322 {"unbuffered", no_argument, 0, 0},
1323 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001324 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001325 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001326 {"set-active", optional_argument, 0, 'a'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001327 {0, 0, 0, 0}
1328 };
Colin Cross8879f982012-05-22 17:53:34 -07001329
1330 serial = getenv("ANDROID_SERIAL");
1331
1332 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001333 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 -07001334 if (c < 0) {
1335 break;
1336 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001337 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001338 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001339 case 'a':
1340 wants_set_active = true;
1341 if (optarg)
1342 next_active = optarg;
1343 break;
Colin Cross8879f982012-05-22 17:53:34 -07001344 case 'b':
1345 base_addr = strtoul(optarg, 0, 16);
1346 break;
Colin Cross8879f982012-05-22 17:53:34 -07001347 case 'c':
1348 cmdline = optarg;
1349 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001350 case 'h':
1351 usage();
1352 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001353 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001354 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001355 unsigned long val;
1356
1357 val = strtoul(optarg, &endptr, 0);
1358 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1359 die("invalid vendor id '%s'", optarg);
1360 vendor_id = (unsigned short)val;
1361 break;
1362 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001363 case 'k':
1364 kernel_offset = strtoul(optarg, 0, 16);
1365 break;
1366 case 'l':
1367 long_listing = 1;
1368 break;
1369 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001370 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001371 if (!page_size) die("invalid page size");
1372 break;
1373 case 'p':
1374 product = optarg;
1375 break;
1376 case 'r':
1377 ramdisk_offset = strtoul(optarg, 0, 16);
1378 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001379 case 't':
1380 tags_offset = strtoul(optarg, 0, 16);
1381 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001382 case 's':
1383 serial = optarg;
1384 break;
1385 case 'S':
1386 sparse_limit = parse_num(optarg);
1387 if (sparse_limit < 0) {
1388 die("invalid sparse limit");
1389 }
1390 break;
1391 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001392 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001393 break;
1394 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001395 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001396 break;
Colin Cross8879f982012-05-22 17:53:34 -07001397 case '?':
1398 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001399 case 0:
1400 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001401 setvbuf(stdout, nullptr, _IONBF, 0);
1402 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001403 } else if (strcmp("version", longopts[longindex].name) == 0) {
1404 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1405 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001406 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1407 slot_override = std::string(optarg);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001408 }
1409 break;
Colin Cross8879f982012-05-22 17:53:34 -07001410 default:
1411 abort();
1412 }
1413 }
1414
1415 argc -= optind;
1416 argv += optind;
1417
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001418 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001419 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001420 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001421 }
1422
Colin Cross8fb6e062012-07-24 16:36:41 -07001423 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001424 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001425 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001426 return 0;
1427 }
1428
Colin Crossc7b75dc2012-08-29 18:17:06 -07001429 if (argc > 0 && !strcmp(*argv, "help")) {
1430 usage();
1431 return 0;
1432 }
1433
David Pursell0b156632015-10-30 11:22:01 -07001434 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001435 if (transport == nullptr) {
1436 return 1;
1437 }
1438
Alex Lightbb9b8a52016-06-29 09:26:44 -07001439 if (slot_override != "") slot_override = verify_slot(transport, slot_override.c_str());
1440 if (next_active != "") next_active = verify_slot(transport, next_active.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001441
1442 if (wants_set_active) {
1443 if (next_active == "") {
1444 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001445 std::string current_slot;
1446 if (fb_getvar(transport, "current-slot", &current_slot)) {
1447 next_active = verify_slot(transport, current_slot.c_str(), false);
1448 } else {
1449 wants_set_active = false;
1450 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001451 } else {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001452 next_active = verify_slot(transport, slot_override.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001453 }
1454 }
1455 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001456
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001457 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001458 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001459 require(2);
1460 fb_queue_display(argv[1], argv[1]);
1461 skip(2);
1462 } else if(!strcmp(*argv, "erase")) {
1463 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001464
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001465 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001466 std::string partition_type;
1467 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001468 fs_get_generator(partition_type) != nullptr) {
1469 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1470 partition_type.c_str());
1471 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001472
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001473 fb_queue_erase(partition.c_str());
1474 };
David Pursell0b156632015-10-30 11:22:01 -07001475 do_for_partitions(transport, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001476 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001477 } else if(!strncmp(*argv, "format", strlen("format"))) {
1478 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001479 char *type_override = nullptr;
1480 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001481 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001482 /*
1483 * Parsing for: "format[:[type][:[size]]]"
1484 * Some valid things:
1485 * - select ontly the size, and leave default fs type:
1486 * format::0x4000000 userdata
1487 * - default fs type and size:
1488 * format userdata
1489 * format:: userdata
1490 */
1491 overrides = strchr(*argv, ':');
1492 if (overrides) {
1493 overrides++;
1494 size_override = strchr(overrides, ':');
1495 if (size_override) {
1496 size_override[0] = '\0';
1497 size_override++;
1498 }
1499 type_override = overrides;
1500 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001501 if (type_override && !type_override[0]) type_override = nullptr;
1502 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001503
1504 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001505 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001506 fb_queue_erase(partition.c_str());
1507 }
David Pursell0b156632015-10-30 11:22:01 -07001508 fb_perform_format(transport, partition.c_str(), 0, type_override, size_override);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001509 };
David Pursell0b156632015-10-30 11:22:01 -07001510 do_for_partitions(transport, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001511 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001512 } else if(!strcmp(*argv, "signature")) {
1513 require(2);
1514 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001515 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001516 if (sz != 256) die("signature must be 256 bytes");
1517 fb_queue_download("signature", data, sz);
1518 fb_queue_command("signature", "installing signature");
1519 skip(2);
1520 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001521 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001522 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001523 if (argc > 0) {
1524 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001525 wants_reboot = false;
1526 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001527 skip(1);
1528 }
1529 }
1530 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001531 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001532 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001533 skip(1);
1534 } else if (!strcmp(*argv, "continue")) {
1535 fb_queue_command("continue", "resuming boot");
1536 skip(1);
1537 } else if(!strcmp(*argv, "boot")) {
1538 char *kname = 0;
1539 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001540 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001541 skip(1);
1542 if (argc > 0) {
1543 kname = argv[0];
1544 skip(1);
1545 }
1546 if (argc > 0) {
1547 rname = argv[0];
1548 skip(1);
1549 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001550 if (argc > 0) {
1551 sname = argv[0];
1552 skip(1);
1553 }
1554 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001555 if (data == 0) return 1;
1556 fb_queue_download("boot.img", data, sz);
1557 fb_queue_command("boot", "booting");
1558 } else if(!strcmp(*argv, "flash")) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001559 char* pname = argv[1];
1560 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001561 require(2);
1562 if (argc > 2) {
1563 fname = argv[2];
1564 skip(3);
1565 } else {
1566 fname = find_item(pname, product);
1567 skip(2);
1568 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001569 if (fname.empty()) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001570
1571 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001572 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001573 fb_queue_erase(partition.c_str());
1574 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001575 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001576 };
David Pursell0b156632015-10-30 11:22:01 -07001577 do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001578 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001579 char *kname = argv[2];
1580 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001581 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001582 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001583 skip(3);
1584 if (argc > 0) {
1585 rname = argv[0];
1586 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001587 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001588 if (argc > 0) {
1589 sname = argv[0];
1590 skip(1);
1591 }
1592 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001593 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001594 auto flashraw = [&](const std::string &partition) {
1595 fb_queue_flash(partition.c_str(), data, sz);
1596 };
David Pursell0b156632015-10-30 11:22:01 -07001597 do_for_partitions(transport, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001598 } else if(!strcmp(*argv, "flashall")) {
1599 skip(1);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001600 if (slot_override == "all") {
1601 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.");
1602 do_flashall(transport, slot_override, erase_first, true, false);
1603 } else {
1604 do_flashall(transport, slot_override, erase_first, true, true);
1605 }
1606 wants_reboot = true;
1607 } else if(!strcmp(*argv, "flash-primary")) {
1608 skip(1);
1609 do_flashall(transport, slot_override, erase_first, true, false);
1610 wants_reboot = true;
1611 } else if(!strcmp(*argv, "flash-secondary")) {
1612 skip(1);
1613 do_flashall(transport, slot_override, erase_first, false, true);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001614 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001615 } else if(!strcmp(*argv, "update")) {
1616 if (argc > 1) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001617 do_update(transport, argv[1], slot_override, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001618 skip(2);
1619 } else {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001620 do_update(transport, "update.zip", slot_override, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001621 skip(1);
1622 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001623 wants_reboot = 1;
1624 } else if(!strcmp(*argv, "set_active")) {
1625 require(2);
1626 std::string slot = verify_slot(transport, argv[1], false);
1627 fb_set_active(slot.c_str());
1628 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001629 } else if(!strcmp(*argv, "oem")) {
1630 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001631 } else if(!strcmp(*argv, "flashing")) {
1632 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1633 !strcmp(*(argv+1), "lock") ||
1634 !strcmp(*(argv+1), "unlock_critical") ||
1635 !strcmp(*(argv+1), "lock_critical") ||
1636 !strcmp(*(argv+1), "get_unlock_ability") ||
1637 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1638 !strcmp(*(argv+1), "lock_bootloader"))) {
1639 argc = do_oem_command(argc, argv);
1640 } else
1641 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1642 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001643 } else {
1644 usage();
1645 return 1;
1646 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001647 } else {
1648 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001649 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001650 }
1651 }
1652
1653 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001654 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001655 fb_queue_erase("userdata");
David Pursell0b156632015-10-30 11:22:01 -07001656 fb_perform_format(transport, "userdata", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001657
1658 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001659 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001660 fprintf(stderr, "wiping cache...\n");
1661 fb_queue_erase("cache");
David Pursell0b156632015-10-30 11:22:01 -07001662 fb_perform_format(transport, "cache", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001663 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001664 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001665 if (wants_set_active) {
1666 fb_set_active(next_active.c_str());
1667 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001668 if (wants_reboot) {
1669 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001670 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001671 } else if (wants_reboot_bootloader) {
1672 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001673 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001674 }
1675
David Pursell0b156632015-10-30 11:22:01 -07001676 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001677}