blob: fa8f19a5862a9eae36206ee557f4b6f8c5c0a9f7 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Colin Crossf8387882012-05-24 17:18:41 -070029#define _LARGEFILE64_SOURCE
30
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070031#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include <errno.h>
33#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070034#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070035#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070036#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070037#include <stdint.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070043#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070044#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080045
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070046#include <functional>
Josh Gao9da9ac52016-01-19 14:50:18 -080047#include <utility>
48#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070049
Elliott Hughes4f713192015-12-04 22:00:26 -080050#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080051#include <android-base/parsenetaddress.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/strings.h>
Colin Crossf8387882012-05-24 17:18:41 -070053#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070054#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055
Elliott Hughes253c18d2015-03-18 22:47:09 -070056#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080057#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070059#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080060#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070061#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080062#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070063#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064
Colin Crossf8387882012-05-24 17:18:41 -070065#ifndef O_BINARY
66#define O_BINARY 0
67#endif
68
Rom Lemarchand622810c2013-06-28 09:54:59 -070069#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
70
Wink Savilleb98762f2011-04-04 17:54:59 -070071char cur_product[FB_RESPONSE_SZ + 1];
72
David Pursell2ec418a2016-01-20 08:32:08 -080073static const char* serial = nullptr;
74static const char* product = nullptr;
75static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070077static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070078static int64_t sparse_limit = -1;
79static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080
Elliott Hughesfc797672015-04-07 20:12:50 -070081static unsigned page_size = 2048;
82static unsigned base_addr = 0x10000000;
83static unsigned kernel_offset = 0x00008000;
84static unsigned ramdisk_offset = 0x01000000;
85static unsigned second_offset = 0x00f00000;
86static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080087
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 {
100 char img_name[13];
101 char sig_name[13];
102 char part_name[9];
103 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700104} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700105 {"boot.img", "boot.sig", "boot", false},
106 {"recovery.img", "recovery.sig", "recovery", true},
107 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700108 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700109};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700110
Elliott Hughesfc797672015-04-07 20:12:50 -0700111static char* find_item(const char* item, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700113 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114 char path[PATH_MAX + 128];
115
116 if(!strcmp(item,"boot")) {
117 fn = "boot.img";
118 } else if(!strcmp(item,"recovery")) {
119 fn = "recovery.img";
120 } else if(!strcmp(item,"system")) {
121 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700122 } else if(!strcmp(item,"vendor")) {
123 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124 } else if(!strcmp(item,"userdata")) {
125 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700126 } else if(!strcmp(item,"cache")) {
127 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128 } else if(!strcmp(item,"info")) {
129 fn = "android-info.txt";
130 } else {
131 fprintf(stderr,"unknown partition '%s'\n", item);
132 return 0;
133 }
134
135 if(product) {
136 get_my_path(path);
137 sprintf(path + strlen(path),
138 "../../../target/product/%s/%s", product, fn);
139 return strdup(path);
140 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800141
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 dir = getenv("ANDROID_PRODUCT_OUT");
143 if((dir == 0) || (dir[0] == 0)) {
144 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
145 return 0;
146 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800147
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148 sprintf(path, "%s/%s", dir, fn);
149 return strdup(path);
150}
151
Elliott Hughesfc797672015-04-07 20:12:50 -0700152static int64_t get_file_size(int fd) {
153 struct stat sb;
154 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700155}
156
Elliott Hughesfc797672015-04-07 20:12:50 -0700157static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800158 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700159 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160
Elliott Hughesfc797672015-04-07 20:12:50 -0700161 *sz = get_file_size(fd);
162 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700163 goto oops;
164 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165
Elliott Hughesfc797672015-04-07 20:12:50 -0700166 data = (char*) malloc(*sz);
167 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168
Elliott Hughesfc797672015-04-07 20:12:50 -0700169 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170 close(fd);
171
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 return data;
173
174oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800175 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 close(fd);
177 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800178 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179 return 0;
180}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181
Elliott Hughesfc797672015-04-07 20:12:50 -0700182static void* load_file(const char* fn, int64_t* sz) {
183 int fd = open(fn, O_RDONLY | O_BINARY);
184 if (fd == -1) return nullptr;
185 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700186}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187
Elliott Hughesfc797672015-04-07 20:12:50 -0700188static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700189 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700190 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700191 return -1;
192 }
193
194 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
195 return -1;
196 }
197
Scott Anderson13081c62012-04-06 12:39:30 -0700198 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700200 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
201 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 return 0;
203}
204
Elliott Hughesfc797672015-04-07 20:12:50 -0700205static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800206 return match_fastboot_with_serial(info, serial);
207}
208
Elliott Hughesfc797672015-04-07 20:12:50 -0700209static int list_devices_callback(usb_ifc_info* info) {
210 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800211 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700212 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800213 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700214 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215 if (!serial[0]) {
216 serial = "????????????";
217 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700218 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700219 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800220 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700221 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800222 printf("%-22s fastboot", serial.c_str());
223 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700224 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800225 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226 }
227
228 return -1;
229}
230
David Pursell2ec418a2016-01-20 08:32:08 -0800231// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
232// a specific device, otherwise the first USB device found will be used.
233//
234// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
235// Otherwise it blocks until the target is available.
236//
237// The returned Transport is a singleton, so multiple calls to this function will return the same
238// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700239static Transport* open_device() {
240 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800241 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242
David Pursell2ec418a2016-01-20 08:32:08 -0800243 if (transport != nullptr) {
244 return transport;
245 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800246
David Pursell4601c972016-02-05 15:35:09 -0800247 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800248 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800249 int port = 0;
250 if (serial != nullptr) {
251 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800252
David Pursell4601c972016-02-05 15:35:09 -0800253 if (android::base::StartsWith(serial, "tcp:")) {
254 protocol = Socket::Protocol::kTcp;
255 port = tcp::kDefaultPort;
256 net_address = serial + strlen("tcp:");
257 } else if (android::base::StartsWith(serial, "udp:")) {
258 protocol = Socket::Protocol::kUdp;
259 port = udp::kDefaultPort;
260 net_address = serial + strlen("udp:");
261 }
262
263 if (net_address != nullptr) {
264 std::string error;
265 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
266 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
267 error.c_str());
268 return nullptr;
269 }
David Pursell2ec418a2016-01-20 08:32:08 -0800270 }
271 }
272
273 while (true) {
274 if (!host.empty()) {
275 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800276 if (protocol == Socket::Protocol::kTcp) {
277 transport = tcp::Connect(host, port, &error).release();
278 } else if (protocol == Socket::Protocol::kUdp) {
279 transport = udp::Connect(host, port, &error).release();
280 }
281
David Pursell2ec418a2016-01-20 08:32:08 -0800282 if (transport == nullptr && announce) {
283 fprintf(stderr, "error: %s\n", error.c_str());
284 }
285 } else {
286 transport = usb_open(match_fastboot);
287 }
288
289 if (transport != nullptr) {
290 return transport;
291 }
292
David Pursell0b156632015-10-30 11:22:01 -0700293 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800294 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700295 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700297 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 }
299}
300
Elliott Hughesfc797672015-04-07 20:12:50 -0700301static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 // We don't actually open a USB device here,
303 // just getting our callback called so we can
304 // list all the connected devices.
305 usb_open(list_devices_callback);
306}
307
Elliott Hughesfc797672015-04-07 20:12:50 -0700308static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 fprintf(stderr,
310/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
311 "usage: fastboot [ <option> ] <command>\n"
312 "\n"
313 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700314 " update <filename> Reflash device from update.zip.\n"
315 " flashall Flash boot, system, vendor, and --\n"
316 " if found -- recovery.\n"
317 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
318 " flashing lock Locks the device. Prevents flashing.\n"
319 " flashing unlock Unlocks the device. Allows flashing\n"
320 " any partition except\n"
321 " bootloader-related partitions.\n"
322 " flashing lock_critical Prevents flashing bootloader-related\n"
323 " partitions.\n"
324 " flashing unlock_critical Enables flashing bootloader-related\n"
325 " partitions.\n"
326 " flashing get_unlock_ability Queries bootloader to see if the\n"
327 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700328 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700329 " unlock nonce.\n"
330 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700331 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700332 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700333 " erase <partition> Erase a flash partition.\n"
334 " format[:[<fs type>][:[<size>]] <partition>\n"
335 " Format a flash partition. Can\n"
336 " override the fs type and/or size\n"
337 " the bootloader reports.\n"
338 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800339 " set_active <suffix> Sets the active slot. If slots are\n"
340 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700341 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
342 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
343 " Create bootimage and flash it.\n"
344 " devices [-l] List all connected devices [with\n"
345 " device paths].\n"
346 " continue Continue with autoboot.\n"
347 " reboot [bootloader] Reboot device [into bootloader].\n"
348 " reboot-bootloader Reboot device into bootloader.\n"
349 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350 "\n"
351 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700352 " -w Erase userdata and cache (and format\n"
353 " if supported by partition type).\n"
354 " -u Do not erase partition before\n"
355 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800356 " -s <specific device> Specify a device. For USB, provide either\n"
357 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700358 " For ethernet, provide an address in the\n"
359 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800360 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700361 " -p <product> Specify product name.\n"
362 " -c <cmdline> Override kernel commandline.\n"
363 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800364 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700365 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800366 " --kernel-offset Specify a custom kernel offset.\n"
367 " (default: 0x00008000)\n"
368 " --ramdisk-offset Specify a custom ramdisk offset.\n"
369 " (default: 0x01000000)\n"
370 " --tags-offset Specify a custom tags offset.\n"
371 " (default: 0x00000100)\n"
372 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700373 " (default: 2048).\n"
374 " -S <size>[K|M|G] Automatically sparse files greater\n"
375 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700376 " --slot <suffix> Specify slot suffix to be used if the\n"
377 " device supports slots. This will be\n"
378 " added to all partition names that use\n"
379 " slots. 'all' can be given to refer\n"
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800380 " to all slots. 'other' can be given to\n"
381 " refer to a non-current slot. If this\n"
382 " flag is not used, slotted partitions\n"
383 " will default to the current active slot.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800384 " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800385 " provided, this will default to the value\n"
386 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800387 " supported, this does nothing. This will\n"
388 " run after all non-reboot commands.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800389 " --unbuffered Do not buffer input or output.\n"
390 " --version Display version.\n"
391 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393}
Elliott Hughesfc797672015-04-07 20:12:50 -0700394static void* load_bootable_image(const char* kernel, const char* ramdisk,
395 const char* secondstage, int64_t* sz,
396 const char* cmdline) {
397 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 fprintf(stderr, "no image specified\n");
399 return 0;
400 }
401
Elliott Hughesfc797672015-04-07 20:12:50 -0700402 int64_t ksize;
403 void* kdata = load_file(kernel, &ksize);
404 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800405 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406 return 0;
407 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800408
Elliott Hughesfc797672015-04-07 20:12:50 -0700409 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700411 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800412
Elliott Hughesfc797672015-04-07 20:12:50 -0700413 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
415 return 0;
416 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800417
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 *sz = ksize;
419 return kdata;
420 }
421
Elliott Hughesfc797672015-04-07 20:12:50 -0700422 void* rdata = nullptr;
423 int64_t rsize = 0;
424 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700426 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800427 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428 return 0;
429 }
430 }
431
Elliott Hughesfc797672015-04-07 20:12:50 -0700432 void* sdata = nullptr;
433 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200434 if (secondstage) {
435 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700436 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200437 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
438 return 0;
439 }
440 }
441
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700443 int64_t bsize = 0;
444 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800445 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200446 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800447 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700448 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 fprintf(stderr,"failed to create boot.img\n");
450 return 0;
451 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700452 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
453 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800455
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 return bdata;
457}
458
Elliott Hughesfc797672015-04-07 20:12:50 -0700459static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460{
Yusuke Sato07447542015-06-25 14:39:19 -0700461 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700462 ZipEntry zip_entry;
463 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700464 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000465 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466 }
467
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700468 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700470 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700471 if (data == nullptr) {
472 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000473 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 }
475
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700476 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
477 if (error != 0) {
478 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000480 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000482
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483 return data;
484}
485
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700486#if defined(_WIN32)
487
488// TODO: move this to somewhere it can be shared.
489
490#include <windows.h>
491
492// Windows' tmpfile(3) requires administrator rights because
493// it creates temporary files in the root directory.
494static FILE* win32_tmpfile() {
495 char temp_path[PATH_MAX];
496 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
497 if (nchars == 0 || nchars >= sizeof(temp_path)) {
498 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
499 return nullptr;
500 }
501
502 char filename[PATH_MAX];
503 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
504 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
505 return nullptr;
506 }
507
508 return fopen(filename, "w+bTD");
509}
510
511#define tmpfile win32_tmpfile
512
513#endif
514
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700515static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
516 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700517 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700518 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
519 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700520 return -1;
521 }
522
Yusuke Sato07447542015-06-25 14:39:19 -0700523 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700524 ZipEntry zip_entry;
525 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
526 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000527 return -1;
528 }
529
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700530 int fd = fileno(fp);
531 int error = ExtractEntryToFile(zip, &zip_entry, fd);
532 if (error != 0) {
533 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
534 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700535 }
536
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000537 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700538 return fd;
539}
540
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800541static char *strip(char *s)
542{
543 int n;
544 while(*s && isspace(*s)) s++;
545 n = strlen(s);
546 while(n-- > 0) {
547 if(!isspace(s[n])) break;
548 s[n] = 0;
549 }
550 return s;
551}
552
553#define MAX_OPTIONS 32
554static int setup_requirement_line(char *name)
555{
556 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700557 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558 unsigned n, count;
559 char *x;
560 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800561
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800562 if (!strncmp(name, "reject ", 7)) {
563 name += 7;
564 invert = 1;
565 } else if (!strncmp(name, "require ", 8)) {
566 name += 8;
567 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700568 } else if (!strncmp(name, "require-for-product:", 20)) {
569 // Get the product and point name past it
570 prod = name + 20;
571 name = strchr(name, ' ');
572 if (!name) return -1;
573 *name = 0;
574 name += 1;
575 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800576 }
577
578 x = strchr(name, '=');
579 if (x == 0) return 0;
580 *x = 0;
581 val[0] = x + 1;
582
583 for(count = 1; count < MAX_OPTIONS; count++) {
584 x = strchr(val[count - 1],'|');
585 if (x == 0) break;
586 *x = 0;
587 val[count] = x + 1;
588 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800589
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800590 name = strip(name);
591 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800592
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800593 name = strip(name);
594 if (name == 0) return -1;
595
Elliott Hughes253c18d2015-03-18 22:47:09 -0700596 const char* var = name;
597 // Work around an unfortunate name mismatch.
598 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599
Elliott Hughes253c18d2015-03-18 22:47:09 -0700600 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800601 if (out == 0) return -1;
602
603 for(n = 0; n < count; n++) {
604 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700605 if (out[n] == 0) {
606 for(size_t i = 0; i < n; ++i) {
607 free((char*) out[i]);
608 }
609 free(out);
610 return -1;
611 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800612 }
613
Elliott Hughes253c18d2015-03-18 22:47:09 -0700614 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800615 return 0;
616}
617
Elliott Hughesfc797672015-04-07 20:12:50 -0700618static void setup_requirements(char* data, int64_t sz) {
619 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800620 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700621 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800622 *s++ = 0;
623 if (setup_requirement_line(data)) {
624 die("out of memory");
625 }
626 data = s;
627 } else {
628 s++;
629 }
630 }
631}
632
Elliott Hughesfc797672015-04-07 20:12:50 -0700633static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800634 fb_queue_notice("--------------------------------------------");
635 fb_queue_display("version-bootloader", "Bootloader Version...");
636 fb_queue_display("version-baseband", "Baseband Version.....");
637 fb_queue_display("serialno", "Serial Number........");
638 fb_queue_notice("--------------------------------------------");
639}
640
Rom Lemarchand622810c2013-06-28 09:54:59 -0700641static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700642{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700643 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700644 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700645 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700646 }
647
Elliott Hughesfc797672015-04-07 20:12:50 -0700648 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700649 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700650 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700651 }
652
Elliott Hughes253c18d2015-03-18 22:47:09 -0700653 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700654 if (!out_s) {
655 die("Failed to allocate sparse file array\n");
656 }
657
658 files = sparse_file_resparse(s, max_size, out_s, files);
659 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700660 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700661 }
662
663 return out_s;
664}
665
David Pursell0b156632015-10-30 11:22:01 -0700666static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700667 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700668 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
669 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800670 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700671 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700672 }
673
Elliott Hughes77c0e662015-11-02 15:51:12 -0800674 // Some bootloaders (angler, for example) send spurious whitespace too.
675 max_download_size = android::base::Trim(max_download_size);
676
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700677 uint64_t limit;
678 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800679 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700680 return 0;
681 }
682 if (limit > 0) {
683 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
684 }
Colin Crossf8387882012-05-24 17:18:41 -0700685 return limit;
686}
687
David Pursell0b156632015-10-30 11:22:01 -0700688static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700689 int64_t limit;
690
691 if (sparse_limit == 0) {
692 return 0;
693 } else if (sparse_limit > 0) {
694 limit = sparse_limit;
695 } else {
696 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700697 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700698 }
699 if (target_sparse_limit > 0) {
700 limit = target_sparse_limit;
701 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700702 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700703 }
704 }
705
706 if (size > limit) {
707 return limit;
708 }
709
710 return 0;
711}
712
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700713// Until we get lazy inode table init working in make_ext4fs, we need to
714// erase partitions of type ext4 before flashing a filesystem so no stale
715// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700716static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800717 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700718 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800719 return false;
720 }
721 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700722}
723
David Pursell0b156632015-10-30 11:22:01 -0700724static int load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700725 int64_t sz = get_file_size(fd);
726 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000727 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700728 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700729
Elliott Hughesfc797672015-04-07 20:12:50 -0700730 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700731 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700732 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700733 sparse_file** s = load_sparse_files(fd, limit);
734 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700735 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700736 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700737 buf->type = FB_BUFFER_SPARSE;
738 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700739 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700740 void* data = load_fd(fd, &sz);
741 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700742 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700743 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000744 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700745 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700746
747 return 0;
748}
749
David Pursell0b156632015-10-30 11:22:01 -0700750static int load_buf(Transport* transport, const char *fname, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700751{
752 int fd;
753
754 fd = open(fname, O_RDONLY | O_BINARY);
755 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700756 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700757 }
758
David Pursell0b156632015-10-30 11:22:01 -0700759 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700760}
761
762static void flash_buf(const char *pname, struct fastboot_buffer *buf)
763{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700764 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700765
766 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800767 case FB_BUFFER_SPARSE: {
768 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700769 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700770 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700771 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800772 sparse_files.emplace_back(*s, sz);
773 ++s;
774 }
775
776 for (size_t i = 0; i < sparse_files.size(); ++i) {
777 const auto& pair = sparse_files[i];
778 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700779 }
780 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800781 }
782
Rom Lemarchand622810c2013-06-28 09:54:59 -0700783 case FB_BUFFER:
784 fb_queue_flash(pname, buf->data, buf->sz);
785 break;
786 default:
787 die("unknown buffer type: %d", buf->type);
788 }
789}
790
David Pursell0b156632015-10-30 11:22:01 -0700791static std::vector<std::string> get_suffixes(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700792 std::vector<std::string> suffixes;
793 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700794 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700795 die("Could not get suffixes.\n");
796 }
797 return android::base::Split(suffix_list, ",");
798}
799
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800800static std::string verify_slot(Transport* transport, const char *slot, bool allow_all) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700801 if (strcmp(slot, "all") == 0) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800802 if (allow_all) {
803 return "all";
804 } else {
805 std::vector<std::string> suffixes = get_suffixes(transport);
806 if (!suffixes.empty()) {
807 return suffixes[0];
808 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800809 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800810 }
811 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700812 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800813
David Pursell0b156632015-10-30 11:22:01 -0700814 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800815
816 if (strcmp(slot, "other") == 0) {
817 std::string current_slot;
818 if (!fb_getvar(transport, "current-slot", &current_slot)) {
819 die("Failed to identify current slot.");
820 }
821 if (!suffixes.empty()) {
822 for (size_t i = 0; i < suffixes.size(); i++) {
823 if (current_slot == suffixes[i])
824 return suffixes[(i+1)%suffixes.size()];
825 }
826 } else {
827 die("No known slots.");
828 }
829 }
830
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700831 for (const std::string &suffix : suffixes) {
832 if (suffix == slot)
833 return slot;
834 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800835 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700836 for (const std::string &suffix : suffixes) {
837 fprintf(stderr, "%s\n", suffix.c_str());
838 }
839 exit(1);
840}
841
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800842static std::string verify_slot(Transport* transport, const char *slot) {
843 return verify_slot(transport, slot, true);
844}
845
David Pursell0b156632015-10-30 11:22:01 -0700846static void do_for_partition(Transport* transport, const char *part, const char *slot,
847 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800848 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700849 std::string current_slot;
850
David Pursell0b156632015-10-30 11:22:01 -0700851 if (!fb_getvar(transport, std::string("has-slot:")+part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800852 /* If has-slot is not supported, the answer is no. */
853 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700854 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800855 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700856 if (!slot || slot[0] == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700857 if (!fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700858 die("Failed to identify current slot.\n");
859 }
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800860 func(std::string(part) + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700861 } else {
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800862 func(std::string(part) + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700863 }
864 } else {
865 if (force_slot && slot && slot[0]) {
David Pursell0b156632015-10-30 11:22:01 -0700866 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
867 part, slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700868 }
869 func(part);
870 }
871}
872
David Pursell0b156632015-10-30 11:22:01 -0700873/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
874 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
875 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
876 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700877 */
David Pursell0b156632015-10-30 11:22:01 -0700878static void do_for_partitions(Transport* transport, const char *part, const char *slot,
879 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800880 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700881
882 if (slot && strcmp(slot, "all") == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700883 if (!fb_getvar(transport, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700884 die("Could not check if partition %s has slot.", part);
885 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800886 if (has_slot == "yes") {
David Pursell0b156632015-10-30 11:22:01 -0700887 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700888 for (std::string &suffix : suffixes) {
David Pursell0b156632015-10-30 11:22:01 -0700889 do_for_partition(transport, part, suffix.c_str(), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700890 }
891 } else {
David Pursell0b156632015-10-30 11:22:01 -0700892 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700893 }
894 } else {
David Pursell0b156632015-10-30 11:22:01 -0700895 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700896 }
897}
898
David Pursell0b156632015-10-30 11:22:01 -0700899static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700900 struct fastboot_buffer buf;
901
David Pursell0b156632015-10-30 11:22:01 -0700902 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700903 die("cannot load '%s'", fname);
904 }
905 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700906}
907
Elliott Hughesfc797672015-04-07 20:12:50 -0700908static void do_update_signature(ZipArchiveHandle zip, char* fn) {
909 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700910 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700911 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800912 fb_queue_download("signature", data, sz);
913 fb_queue_command("signature", "installing signature");
914}
915
David Pursell0b156632015-10-30 11:22:01 -0700916static void do_update(Transport* transport, const char* filename, const char* slot_override, bool erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800917 queue_info_dump();
918
Wink Savilleb98762f2011-04-04 17:54:59 -0700919 fb_queue_query_save("product", cur_product, sizeof(cur_product));
920
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700921 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700922 int error = OpenArchive(filename, &zip);
923 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100924 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700925 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700926 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800927
Elliott Hughesfc797672015-04-07 20:12:50 -0700928 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700929 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700930 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100931 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700932 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800933 }
934
Elliott Hughes253c18d2015-03-18 22:47:09 -0700935 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800936
Elliott Hughesacdbe922015-06-02 21:37:05 -0700937 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700938 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700939 if (fd == -1) {
940 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700941 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700942 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100943 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700944 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700945 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700946 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -0700947 int rc = load_buf_fd(transport, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700948 if (rc) die("cannot load %s from flash", images[i].img_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700949
950 auto update = [&](const std::string &partition) {
951 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -0700952 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700953 fb_queue_erase(partition.c_str());
954 }
955 flash_buf(partition.c_str(), &buf);
956 /* not closing the fd here since the sparse code keeps the fd around
957 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
958 * program exits.
959 */
960 };
David Pursell0b156632015-10-30 11:22:01 -0700961 do_for_partitions(transport, images[i].part_name, slot_override, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800962 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700963
964 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800965}
966
Elliott Hughesfc797672015-04-07 20:12:50 -0700967static void do_send_signature(char* fn) {
968 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800969 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -0700970
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800971 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800972
Elliott Hughesfc797672015-04-07 20:12:50 -0700973 strcpy(xtn, ".sig");
974
975 int64_t sz;
976 void* data = load_file(fn, &sz);
977 strcpy(xtn, ".img");
978 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800979 fb_queue_download("signature", data, sz);
980 fb_queue_command("signature", "installing signature");
981}
982
David Pursell0b156632015-10-30 11:22:01 -0700983static void do_flashall(Transport* transport, const char* slot_override, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800984 queue_info_dump();
985
Wink Savilleb98762f2011-04-04 17:54:59 -0700986 fb_queue_query_save("product", cur_product, sizeof(cur_product));
987
Elliott Hughes253c18d2015-03-18 22:47:09 -0700988 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -0700989 if (fname == nullptr) die("cannot find android-info.txt");
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 = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700993 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700994
995 setup_requirements(reinterpret_cast<char*>(data), sz);
996
997 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700998 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700999 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -07001000 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001001 if (images[i].is_optional)
1002 continue;
1003 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001004 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001005
1006 auto flashall = [&](const std::string &partition) {
1007 do_send_signature(fname);
David Pursell0b156632015-10-30 11:22:01 -07001008 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001009 fb_queue_erase(partition.c_str());
1010 }
1011 flash_buf(partition.c_str(), &buf);
1012 };
David Pursell0b156632015-10-30 11:22:01 -07001013 do_for_partitions(transport, images[i].part_name, slot_override, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001014 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001015}
1016
1017#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001018#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001019
Elliott Hughes45be42e2015-09-02 20:15:48 -07001020static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001021{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001022 if (argc <= 2) return 0;
1023 skip(2);
1024
1025 /*
1026 * Process unlock_bootloader, we have to load the message file
1027 * and send that to the remote device.
1028 */
1029 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001030
1031 int64_t sz;
1032 void* data = load_file(*argv, &sz);
1033 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001034 fb_queue_download("unlock_message", data, sz);
1035 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1036 skip(1);
1037 return 0;
1038}
1039
Elliott Hughes45be42e2015-09-02 20:15:48 -07001040static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001041{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001042 char command[256];
1043 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001044
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001045 command[0] = 0;
1046 while(1) {
1047 strcat(command,*argv);
1048 skip(1);
1049 if(argc == 0) break;
1050 strcat(command," ");
1051 }
1052
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001053 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001054 return 0;
1055}
1056
Colin Crossf8387882012-05-24 17:18:41 -07001057static int64_t parse_num(const char *arg)
1058{
1059 char *endptr;
1060 unsigned long long num;
1061
1062 num = strtoull(arg, &endptr, 0);
1063 if (endptr == arg) {
1064 return -1;
1065 }
1066
1067 if (*endptr == 'k' || *endptr == 'K') {
1068 if (num >= (-1ULL) / 1024) {
1069 return -1;
1070 }
1071 num *= 1024LL;
1072 endptr++;
1073 } else if (*endptr == 'm' || *endptr == 'M') {
1074 if (num >= (-1ULL) / (1024 * 1024)) {
1075 return -1;
1076 }
1077 num *= 1024LL * 1024LL;
1078 endptr++;
1079 } else if (*endptr == 'g' || *endptr == 'G') {
1080 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1081 return -1;
1082 }
1083 num *= 1024LL * 1024LL * 1024LL;
1084 endptr++;
1085 }
1086
1087 if (*endptr != '\0') {
1088 return -1;
1089 }
1090
1091 if (num > INT64_MAX) {
1092 return -1;
1093 }
1094
1095 return num;
1096}
1097
David Pursell0b156632015-10-30 11:22:01 -07001098static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001099 const char* partition, int skip_if_not_supported,
1100 const char* type_override, const char* size_override) {
1101 std::string partition_type, partition_size;
1102
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001103 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001104 const char* errMsg = nullptr;
1105 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001106 int fd;
1107
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001108 unsigned int limit = INT_MAX;
1109 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001110 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001111 }
1112 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001113 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001114 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001115
David Pursell0b156632015-10-30 11:22:01 -07001116 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001117 errMsg = "Can't determine partition type.\n";
1118 goto failed;
1119 }
JP Abgrall7e859742014-05-06 15:14:15 -07001120 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001121 if (partition_type != type_override) {
1122 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1123 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001124 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001125 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001126 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001127
David Pursell0b156632015-10-30 11:22:01 -07001128 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001129 errMsg = "Unable to get partition size\n";
1130 goto failed;
1131 }
JP Abgrall7e859742014-05-06 15:14:15 -07001132 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001133 if (partition_size != size_override) {
1134 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1135 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001136 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001137 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001138 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001139 // Some bootloaders (angler, for example), send spurious leading whitespace.
1140 partition_size = android::base::Trim(partition_size);
1141 // Some bootloaders (hammerhead, for example) use implicit hex.
1142 // This code used to use strtol with base 16.
1143 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001144
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001145 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001146 if (!gen) {
1147 if (skip_if_not_supported) {
1148 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001149 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001150 return;
1151 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001152 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1153 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001154 return;
1155 }
1156
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001157 int64_t size;
1158 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1159 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1160 return;
1161 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001162
1163 fd = fileno(tmpfile());
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001164 if (fs_generator_generate(gen, fd, size)) {
1165 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001166 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001167 return;
1168 }
1169
David Pursell0b156632015-10-30 11:22:01 -07001170 if (load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001171 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001172 close(fd);
1173 return;
1174 }
1175 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001176 return;
1177
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001178failed:
1179 if (skip_if_not_supported) {
1180 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001181 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001182 }
1183 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
1184}
1185
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001186int main(int argc, char **argv)
1187{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001188 bool wants_wipe = false;
1189 bool wants_reboot = false;
1190 bool wants_reboot_bootloader = false;
1191 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001192 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001193 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001194 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001195 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001196 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001197 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001198
JP Abgrall7b8970c2013-03-07 17:06:41 -08001199 const struct option longopts[] = {
1200 {"base", required_argument, 0, 'b'},
1201 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001202 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001203 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001204 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001205 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001206 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001207 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001208 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001209 {"help", no_argument, 0, 'h'},
1210 {"unbuffered", no_argument, 0, 0},
1211 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001212 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001213 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001214 {"set-active", optional_argument, 0, 'a'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001215 {0, 0, 0, 0}
1216 };
Colin Cross8879f982012-05-22 17:53:34 -07001217
1218 serial = getenv("ANDROID_SERIAL");
1219
1220 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001221 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 -07001222 if (c < 0) {
1223 break;
1224 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001225 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001226 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001227 case 'a':
1228 wants_set_active = true;
1229 if (optarg)
1230 next_active = optarg;
1231 break;
Colin Cross8879f982012-05-22 17:53:34 -07001232 case 'b':
1233 base_addr = strtoul(optarg, 0, 16);
1234 break;
Colin Cross8879f982012-05-22 17:53:34 -07001235 case 'c':
1236 cmdline = optarg;
1237 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001238 case 'h':
1239 usage();
1240 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001241 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001242 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001243 unsigned long val;
1244
1245 val = strtoul(optarg, &endptr, 0);
1246 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1247 die("invalid vendor id '%s'", optarg);
1248 vendor_id = (unsigned short)val;
1249 break;
1250 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001251 case 'k':
1252 kernel_offset = strtoul(optarg, 0, 16);
1253 break;
1254 case 'l':
1255 long_listing = 1;
1256 break;
1257 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001258 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001259 if (!page_size) die("invalid page size");
1260 break;
1261 case 'p':
1262 product = optarg;
1263 break;
1264 case 'r':
1265 ramdisk_offset = strtoul(optarg, 0, 16);
1266 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001267 case 't':
1268 tags_offset = strtoul(optarg, 0, 16);
1269 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001270 case 's':
1271 serial = optarg;
1272 break;
1273 case 'S':
1274 sparse_limit = parse_num(optarg);
1275 if (sparse_limit < 0) {
1276 die("invalid sparse limit");
1277 }
1278 break;
1279 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001280 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001281 break;
1282 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001283 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001284 break;
Colin Cross8879f982012-05-22 17:53:34 -07001285 case '?':
1286 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001287 case 0:
1288 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001289 setvbuf(stdout, nullptr, _IONBF, 0);
1290 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001291 } else if (strcmp("version", longopts[longindex].name) == 0) {
1292 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1293 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001294 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1295 slot_override = std::string(optarg);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001296 }
1297 break;
Colin Cross8879f982012-05-22 17:53:34 -07001298 default:
1299 abort();
1300 }
1301 }
1302
1303 argc -= optind;
1304 argv += optind;
1305
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001306 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001307 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001308 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001309 }
1310
Colin Cross8fb6e062012-07-24 16:36:41 -07001311 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001312 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001313 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001314 return 0;
1315 }
1316
Colin Crossc7b75dc2012-08-29 18:17:06 -07001317 if (argc > 0 && !strcmp(*argv, "help")) {
1318 usage();
1319 return 0;
1320 }
1321
David Pursell0b156632015-10-30 11:22:01 -07001322 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001323 if (transport == nullptr) {
1324 return 1;
1325 }
1326
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001327 if (slot_override != "")
David Pursell0b156632015-10-30 11:22:01 -07001328 slot_override = verify_slot(transport, slot_override.c_str());
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001329 if (next_active != "")
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001330 next_active = verify_slot(transport, next_active.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001331
1332 if (wants_set_active) {
1333 if (next_active == "") {
1334 if (slot_override == "") {
1335 wants_set_active = false;
1336 } else {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001337 next_active = verify_slot(transport, slot_override.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001338 }
1339 }
1340 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001341
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001342 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001343 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001344 require(2);
1345 fb_queue_display(argv[1], argv[1]);
1346 skip(2);
1347 } else if(!strcmp(*argv, "erase")) {
1348 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001349
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001350 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001351 std::string partition_type;
1352 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001353 fs_get_generator(partition_type) != nullptr) {
1354 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1355 partition_type.c_str());
1356 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001357
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001358 fb_queue_erase(partition.c_str());
1359 };
David Pursell0b156632015-10-30 11:22:01 -07001360 do_for_partitions(transport, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001361 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001362 } else if(!strncmp(*argv, "format", strlen("format"))) {
1363 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001364 char *type_override = nullptr;
1365 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001366 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001367 /*
1368 * Parsing for: "format[:[type][:[size]]]"
1369 * Some valid things:
1370 * - select ontly the size, and leave default fs type:
1371 * format::0x4000000 userdata
1372 * - default fs type and size:
1373 * format userdata
1374 * format:: userdata
1375 */
1376 overrides = strchr(*argv, ':');
1377 if (overrides) {
1378 overrides++;
1379 size_override = strchr(overrides, ':');
1380 if (size_override) {
1381 size_override[0] = '\0';
1382 size_override++;
1383 }
1384 type_override = overrides;
1385 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001386 if (type_override && !type_override[0]) type_override = nullptr;
1387 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001388
1389 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001390 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001391 fb_queue_erase(partition.c_str());
1392 }
David Pursell0b156632015-10-30 11:22:01 -07001393 fb_perform_format(transport, partition.c_str(), 0, type_override, size_override);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001394 };
David Pursell0b156632015-10-30 11:22:01 -07001395 do_for_partitions(transport, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001396 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001397 } else if(!strcmp(*argv, "signature")) {
1398 require(2);
1399 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001400 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001401 if (sz != 256) die("signature must be 256 bytes");
1402 fb_queue_download("signature", data, sz);
1403 fb_queue_command("signature", "installing signature");
1404 skip(2);
1405 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001406 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001407 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001408 if (argc > 0) {
1409 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001410 wants_reboot = false;
1411 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001412 skip(1);
1413 }
1414 }
1415 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001416 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001417 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001418 skip(1);
1419 } else if (!strcmp(*argv, "continue")) {
1420 fb_queue_command("continue", "resuming boot");
1421 skip(1);
1422 } else if(!strcmp(*argv, "boot")) {
1423 char *kname = 0;
1424 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001425 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001426 skip(1);
1427 if (argc > 0) {
1428 kname = argv[0];
1429 skip(1);
1430 }
1431 if (argc > 0) {
1432 rname = argv[0];
1433 skip(1);
1434 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001435 if (argc > 0) {
1436 sname = argv[0];
1437 skip(1);
1438 }
1439 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001440 if (data == 0) return 1;
1441 fb_queue_download("boot.img", data, sz);
1442 fb_queue_command("boot", "booting");
1443 } else if(!strcmp(*argv, "flash")) {
1444 char *pname = argv[1];
1445 char *fname = 0;
1446 require(2);
1447 if (argc > 2) {
1448 fname = argv[2];
1449 skip(3);
1450 } else {
1451 fname = find_item(pname, product);
1452 skip(2);
1453 }
1454 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001455
1456 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001457 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001458 fb_queue_erase(partition.c_str());
1459 }
David Pursell0b156632015-10-30 11:22:01 -07001460 do_flash(transport, partition.c_str(), fname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001461 };
David Pursell0b156632015-10-30 11:22:01 -07001462 do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001463 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001464 char *kname = argv[2];
1465 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001466 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001467 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001468 skip(3);
1469 if (argc > 0) {
1470 rname = argv[0];
1471 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001472 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001473 if (argc > 0) {
1474 sname = argv[0];
1475 skip(1);
1476 }
1477 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001478 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001479 auto flashraw = [&](const std::string &partition) {
1480 fb_queue_flash(partition.c_str(), data, sz);
1481 };
David Pursell0b156632015-10-30 11:22:01 -07001482 do_for_partitions(transport, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001483 } else if(!strcmp(*argv, "flashall")) {
1484 skip(1);
David Pursell0b156632015-10-30 11:22:01 -07001485 do_flashall(transport, slot_override.c_str(), erase_first);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001486 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001487 } else if(!strcmp(*argv, "update")) {
1488 if (argc > 1) {
David Pursell0b156632015-10-30 11:22:01 -07001489 do_update(transport, argv[1], slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001490 skip(2);
1491 } else {
David Pursell0b156632015-10-30 11:22:01 -07001492 do_update(transport, "update.zip", slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001493 skip(1);
1494 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001495 wants_reboot = 1;
1496 } else if(!strcmp(*argv, "set_active")) {
1497 require(2);
1498 std::string slot = verify_slot(transport, argv[1], false);
1499 fb_set_active(slot.c_str());
1500 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001501 } else if(!strcmp(*argv, "oem")) {
1502 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001503 } else if(!strcmp(*argv, "flashing")) {
1504 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1505 !strcmp(*(argv+1), "lock") ||
1506 !strcmp(*(argv+1), "unlock_critical") ||
1507 !strcmp(*(argv+1), "lock_critical") ||
1508 !strcmp(*(argv+1), "get_unlock_ability") ||
1509 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1510 !strcmp(*(argv+1), "lock_bootloader"))) {
1511 argc = do_oem_command(argc, argv);
1512 } else
1513 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1514 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001515 } else {
1516 usage();
1517 return 1;
1518 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001519 } else {
1520 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001521 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001522 }
1523 }
1524
1525 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001526 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001527 fb_queue_erase("userdata");
David Pursell0b156632015-10-30 11:22:01 -07001528 fb_perform_format(transport, "userdata", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001529
1530 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001531 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001532 fprintf(stderr, "wiping cache...\n");
1533 fb_queue_erase("cache");
David Pursell0b156632015-10-30 11:22:01 -07001534 fb_perform_format(transport, "cache", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001535 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001536 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001537 if (wants_set_active) {
1538 fb_set_active(next_active.c_str());
1539 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001540 if (wants_reboot) {
1541 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001542 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001543 } else if (wants_reboot_bootloader) {
1544 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001545 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001546 }
1547
David Pursell0b156632015-10-30 11:22:01 -07001548 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001549}