blob: 5b663668e4bcb71fc077baedfaee979ae7285922 [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>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070045#include <functional>
Colin Crossf8387882012-05-24 17:18:41 -070046
Elliott Hughes4f713192015-12-04 22:00:26 -080047#include <android-base/parseint.h>
48#include <android-base/strings.h>
Colin Crossf8387882012-05-24 17:18:41 -070049#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070050#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/strings.h>
53#include <android-base/parseint.h>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070054
Elliott Hughes253c18d2015-03-18 22:47:09 -070055#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080056#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070058#include "fs.h"
David Pursell0b156632015-10-30 11:22:01 -070059#include "transport.h"
60#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
Colin Crossf8387882012-05-24 17:18:41 -070062#ifndef O_BINARY
63#define O_BINARY 0
64#endif
65
Rom Lemarchand622810c2013-06-28 09:54:59 -070066#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
67
Wink Savilleb98762f2011-04-04 17:54:59 -070068char cur_product[FB_RESPONSE_SZ + 1];
69
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070static const char *serial = 0;
71static const char *product = 0;
72static const char *cmdline = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070074static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070075static int64_t sparse_limit = -1;
76static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077
Elliott Hughesfc797672015-04-07 20:12:50 -070078static unsigned page_size = 2048;
79static unsigned base_addr = 0x10000000;
80static unsigned kernel_offset = 0x00008000;
81static unsigned ramdisk_offset = 0x01000000;
82static unsigned second_offset = 0x00f00000;
83static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080084
Rom Lemarchand622810c2013-06-28 09:54:59 -070085enum fb_buffer_type {
86 FB_BUFFER,
87 FB_BUFFER_SPARSE,
88};
89
90struct fastboot_buffer {
91 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070092 void* data;
93 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070094};
95
96static struct {
97 char img_name[13];
98 char sig_name[13];
99 char part_name[9];
100 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700101} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700102 {"boot.img", "boot.sig", "boot", false},
103 {"recovery.img", "recovery.sig", "recovery", true},
104 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700105 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700106};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700107
Elliott Hughesfc797672015-04-07 20:12:50 -0700108static char* find_item(const char* item, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700110 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111 char path[PATH_MAX + 128];
112
113 if(!strcmp(item,"boot")) {
114 fn = "boot.img";
115 } else if(!strcmp(item,"recovery")) {
116 fn = "recovery.img";
117 } else if(!strcmp(item,"system")) {
118 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700119 } else if(!strcmp(item,"vendor")) {
120 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121 } else if(!strcmp(item,"userdata")) {
122 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700123 } else if(!strcmp(item,"cache")) {
124 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 } else if(!strcmp(item,"info")) {
126 fn = "android-info.txt";
127 } else {
128 fprintf(stderr,"unknown partition '%s'\n", item);
129 return 0;
130 }
131
132 if(product) {
133 get_my_path(path);
134 sprintf(path + strlen(path),
135 "../../../target/product/%s/%s", product, fn);
136 return strdup(path);
137 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800138
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 dir = getenv("ANDROID_PRODUCT_OUT");
140 if((dir == 0) || (dir[0] == 0)) {
141 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
142 return 0;
143 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800144
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145 sprintf(path, "%s/%s", dir, fn);
146 return strdup(path);
147}
148
Elliott Hughesfc797672015-04-07 20:12:50 -0700149static int64_t get_file_size(int fd) {
150 struct stat sb;
151 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700152}
153
Elliott Hughesfc797672015-04-07 20:12:50 -0700154static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800155 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700156 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157
Elliott Hughesfc797672015-04-07 20:12:50 -0700158 *sz = get_file_size(fd);
159 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700160 goto oops;
161 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162
Elliott Hughesfc797672015-04-07 20:12:50 -0700163 data = (char*) malloc(*sz);
164 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165
Elliott Hughesfc797672015-04-07 20:12:50 -0700166 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 close(fd);
168
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169 return data;
170
171oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800172 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 close(fd);
174 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800175 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 return 0;
177}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178
Elliott Hughesfc797672015-04-07 20:12:50 -0700179static void* load_file(const char* fn, int64_t* sz) {
180 int fd = open(fn, O_RDONLY | O_BINARY);
181 if (fd == -1) return nullptr;
182 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700183}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184
Elliott Hughesfc797672015-04-07 20:12:50 -0700185static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700186 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700187 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700188 return -1;
189 }
190
191 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
192 return -1;
193 }
194
Scott Anderson13081c62012-04-06 12:39:30 -0700195 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700197 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
198 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 return 0;
200}
201
Elliott Hughesfc797672015-04-07 20:12:50 -0700202static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800203 return match_fastboot_with_serial(info, serial);
204}
205
Elliott Hughesfc797672015-04-07 20:12:50 -0700206static int list_devices_callback(usb_ifc_info* info) {
207 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800208 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700209 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800210 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700211 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212 if (!serial[0]) {
213 serial = "????????????";
214 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700215 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700216 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800217 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700218 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800219 printf("%-22s fastboot", serial.c_str());
220 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700221 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800222 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 }
224
225 return -1;
226}
227
David Pursell0b156632015-10-30 11:22:01 -0700228static Transport* open_device() {
229 static Transport* transport = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230 int announce = 1;
231
David Pursell0b156632015-10-30 11:22:01 -0700232 if (transport) return transport;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800233
David Pursell0b156632015-10-30 11:22:01 -0700234 for (;;) {
235 transport = usb_open(match_fastboot);
236 if (transport) return transport;
237 if (announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800238 announce = 0;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700239 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700241 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 }
243}
244
Elliott Hughesfc797672015-04-07 20:12:50 -0700245static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 // We don't actually open a USB device here,
247 // just getting our callback called so we can
248 // list all the connected devices.
249 usb_open(list_devices_callback);
250}
251
Elliott Hughesfc797672015-04-07 20:12:50 -0700252static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 fprintf(stderr,
254/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
255 "usage: fastboot [ <option> ] <command>\n"
256 "\n"
257 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700258 " update <filename> Reflash device from update.zip.\n"
259 " flashall Flash boot, system, vendor, and --\n"
260 " if found -- recovery.\n"
261 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
262 " flashing lock Locks the device. Prevents flashing.\n"
263 " flashing unlock Unlocks the device. Allows flashing\n"
264 " any partition except\n"
265 " bootloader-related partitions.\n"
266 " flashing lock_critical Prevents flashing bootloader-related\n"
267 " partitions.\n"
268 " flashing unlock_critical Enables flashing bootloader-related\n"
269 " partitions.\n"
270 " flashing get_unlock_ability Queries bootloader to see if the\n"
271 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700272 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700273 " unlock nonce.\n"
274 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700275 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700276 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700277 " erase <partition> Erase a flash partition.\n"
278 " format[:[<fs type>][:[<size>]] <partition>\n"
279 " Format a flash partition. Can\n"
280 " override the fs type and/or size\n"
281 " the bootloader reports.\n"
282 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800283 " set_active <suffix> Sets the active slot. If slots are\n"
284 " not supported, this does nothing.\n"
285 " note: suffixes starting with a '-'\n"
286 " must use set_active -- <suffix>\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700287 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
288 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
289 " Create bootimage and flash it.\n"
290 " devices [-l] List all connected devices [with\n"
291 " device paths].\n"
292 " continue Continue with autoboot.\n"
293 " reboot [bootloader] Reboot device [into bootloader].\n"
294 " reboot-bootloader Reboot device into bootloader.\n"
295 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 "\n"
297 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700298 " -w Erase userdata and cache (and format\n"
299 " if supported by partition type).\n"
300 " -u Do not erase partition before\n"
301 " formatting.\n"
302 " -s <specific device> Specify device serial number\n"
303 " or path to device port.\n"
304 " -p <product> Specify product name.\n"
305 " -c <cmdline> Override kernel commandline.\n"
306 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800307 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700308 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800309 " --kernel-offset Specify a custom kernel offset.\n"
310 " (default: 0x00008000)\n"
311 " --ramdisk-offset Specify a custom ramdisk offset.\n"
312 " (default: 0x01000000)\n"
313 " --tags-offset Specify a custom tags offset.\n"
314 " (default: 0x00000100)\n"
315 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700316 " (default: 2048).\n"
317 " -S <size>[K|M|G] Automatically sparse files greater\n"
318 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700319 " --slot <suffix> Specify slot suffix to be used if the\n"
320 " device supports slots. This will be\n"
321 " added to all partition names that use\n"
322 " slots. 'all' can be given to refer\n"
323 " to all slots. If this is not given,\n"
324 " slotted partitions will default to\n"
325 " the current active slot.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800326 " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800327 " provided, this will default to the value\n"
328 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800329 " supported, this does nothing. This will\n"
330 " run after all non-reboot commands.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800331 " --unbuffered Do not buffer input or output.\n"
332 " --version Display version.\n"
333 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335}
Elliott Hughesfc797672015-04-07 20:12:50 -0700336static void* load_bootable_image(const char* kernel, const char* ramdisk,
337 const char* secondstage, int64_t* sz,
338 const char* cmdline) {
339 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340 fprintf(stderr, "no image specified\n");
341 return 0;
342 }
343
Elliott Hughesfc797672015-04-07 20:12:50 -0700344 int64_t ksize;
345 void* kdata = load_file(kernel, &ksize);
346 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800347 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348 return 0;
349 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800350
Elliott Hughesfc797672015-04-07 20:12:50 -0700351 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700353 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800354
Elliott Hughesfc797672015-04-07 20:12:50 -0700355 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
357 return 0;
358 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800359
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360 *sz = ksize;
361 return kdata;
362 }
363
Elliott Hughesfc797672015-04-07 20:12:50 -0700364 void* rdata = nullptr;
365 int64_t rsize = 0;
366 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700368 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800369 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370 return 0;
371 }
372 }
373
Elliott Hughesfc797672015-04-07 20:12:50 -0700374 void* sdata = nullptr;
375 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200376 if (secondstage) {
377 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700378 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200379 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
380 return 0;
381 }
382 }
383
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700385 int64_t bsize = 0;
386 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800387 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200388 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800389 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700390 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391 fprintf(stderr,"failed to create boot.img\n");
392 return 0;
393 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700394 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
395 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800396 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800397
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 return bdata;
399}
400
Elliott Hughesfc797672015-04-07 20:12:50 -0700401static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402{
Yusuke Sato07447542015-06-25 14:39:19 -0700403 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700404 ZipEntry zip_entry;
405 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700406 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000407 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408 }
409
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700410 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700412 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700413 if (data == nullptr) {
414 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000415 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416 }
417
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700418 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
419 if (error != 0) {
420 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000422 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000424
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425 return data;
426}
427
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700428#if defined(_WIN32)
429
430// TODO: move this to somewhere it can be shared.
431
432#include <windows.h>
433
434// Windows' tmpfile(3) requires administrator rights because
435// it creates temporary files in the root directory.
436static FILE* win32_tmpfile() {
437 char temp_path[PATH_MAX];
438 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
439 if (nchars == 0 || nchars >= sizeof(temp_path)) {
440 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
441 return nullptr;
442 }
443
444 char filename[PATH_MAX];
445 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
446 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
447 return nullptr;
448 }
449
450 return fopen(filename, "w+bTD");
451}
452
453#define tmpfile win32_tmpfile
454
455#endif
456
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700457static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
458 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700459 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700460 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
461 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700462 return -1;
463 }
464
Yusuke Sato07447542015-06-25 14:39:19 -0700465 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700466 ZipEntry zip_entry;
467 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
468 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000469 return -1;
470 }
471
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700472 int fd = fileno(fp);
473 int error = ExtractEntryToFile(zip, &zip_entry, fd);
474 if (error != 0) {
475 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
476 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700477 }
478
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000479 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700480 return fd;
481}
482
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483static char *strip(char *s)
484{
485 int n;
486 while(*s && isspace(*s)) s++;
487 n = strlen(s);
488 while(n-- > 0) {
489 if(!isspace(s[n])) break;
490 s[n] = 0;
491 }
492 return s;
493}
494
495#define MAX_OPTIONS 32
496static int setup_requirement_line(char *name)
497{
498 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700499 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500 unsigned n, count;
501 char *x;
502 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800503
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 if (!strncmp(name, "reject ", 7)) {
505 name += 7;
506 invert = 1;
507 } else if (!strncmp(name, "require ", 8)) {
508 name += 8;
509 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700510 } else if (!strncmp(name, "require-for-product:", 20)) {
511 // Get the product and point name past it
512 prod = name + 20;
513 name = strchr(name, ' ');
514 if (!name) return -1;
515 *name = 0;
516 name += 1;
517 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518 }
519
520 x = strchr(name, '=');
521 if (x == 0) return 0;
522 *x = 0;
523 val[0] = x + 1;
524
525 for(count = 1; count < MAX_OPTIONS; count++) {
526 x = strchr(val[count - 1],'|');
527 if (x == 0) break;
528 *x = 0;
529 val[count] = x + 1;
530 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800531
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532 name = strip(name);
533 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800534
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535 name = strip(name);
536 if (name == 0) return -1;
537
Elliott Hughes253c18d2015-03-18 22:47:09 -0700538 const char* var = name;
539 // Work around an unfortunate name mismatch.
540 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800541
Elliott Hughes253c18d2015-03-18 22:47:09 -0700542 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543 if (out == 0) return -1;
544
545 for(n = 0; n < count; n++) {
546 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700547 if (out[n] == 0) {
548 for(size_t i = 0; i < n; ++i) {
549 free((char*) out[i]);
550 }
551 free(out);
552 return -1;
553 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800554 }
555
Elliott Hughes253c18d2015-03-18 22:47:09 -0700556 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800557 return 0;
558}
559
Elliott Hughesfc797672015-04-07 20:12:50 -0700560static void setup_requirements(char* data, int64_t sz) {
561 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800562 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700563 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800564 *s++ = 0;
565 if (setup_requirement_line(data)) {
566 die("out of memory");
567 }
568 data = s;
569 } else {
570 s++;
571 }
572 }
573}
574
Elliott Hughesfc797672015-04-07 20:12:50 -0700575static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800576 fb_queue_notice("--------------------------------------------");
577 fb_queue_display("version-bootloader", "Bootloader Version...");
578 fb_queue_display("version-baseband", "Baseband Version.....");
579 fb_queue_display("serialno", "Serial Number........");
580 fb_queue_notice("--------------------------------------------");
581}
582
Rom Lemarchand622810c2013-06-28 09:54:59 -0700583static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700584{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700585 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700586 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700587 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700588 }
589
Elliott Hughesfc797672015-04-07 20:12:50 -0700590 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700591 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700592 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700593 }
594
Elliott Hughes253c18d2015-03-18 22:47:09 -0700595 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700596 if (!out_s) {
597 die("Failed to allocate sparse file array\n");
598 }
599
600 files = sparse_file_resparse(s, max_size, out_s, files);
601 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700602 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700603 }
604
605 return out_s;
606}
607
David Pursell0b156632015-10-30 11:22:01 -0700608static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700609 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700610 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
611 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800612 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700613 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700614 }
615
Elliott Hughes77c0e662015-11-02 15:51:12 -0800616 // Some bootloaders (angler, for example) send spurious whitespace too.
617 max_download_size = android::base::Trim(max_download_size);
618
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700619 uint64_t limit;
620 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800621 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700622 return 0;
623 }
624 if (limit > 0) {
625 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
626 }
Colin Crossf8387882012-05-24 17:18:41 -0700627 return limit;
628}
629
David Pursell0b156632015-10-30 11:22:01 -0700630static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700631 int64_t limit;
632
633 if (sparse_limit == 0) {
634 return 0;
635 } else if (sparse_limit > 0) {
636 limit = sparse_limit;
637 } else {
638 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700639 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700640 }
641 if (target_sparse_limit > 0) {
642 limit = target_sparse_limit;
643 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700644 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700645 }
646 }
647
648 if (size > limit) {
649 return limit;
650 }
651
652 return 0;
653}
654
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700655// Until we get lazy inode table init working in make_ext4fs, we need to
656// erase partitions of type ext4 before flashing a filesystem so no stale
657// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700658static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800659 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700660 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800661 return false;
662 }
663 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700664}
665
David Pursell0b156632015-10-30 11:22:01 -0700666static int load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700667 int64_t sz = get_file_size(fd);
668 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000669 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700670 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700671
Elliott Hughesfc797672015-04-07 20:12:50 -0700672 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700673 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700674 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700675 sparse_file** s = load_sparse_files(fd, limit);
676 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700677 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700678 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700679 buf->type = FB_BUFFER_SPARSE;
680 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700681 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700682 void* data = load_fd(fd, &sz);
683 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700684 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700685 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000686 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700687 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700688
689 return 0;
690}
691
David Pursell0b156632015-10-30 11:22:01 -0700692static int load_buf(Transport* transport, const char *fname, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700693{
694 int fd;
695
696 fd = open(fname, O_RDONLY | O_BINARY);
697 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700698 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700699 }
700
David Pursell0b156632015-10-30 11:22:01 -0700701 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700702}
703
704static void flash_buf(const char *pname, struct fastboot_buffer *buf)
705{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700706 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700707
708 switch (buf->type) {
709 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700710 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700711 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700712 int64_t sz = sparse_file_len(*s, true, false);
713 fb_queue_flash_sparse(pname, *s++, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700714 }
715 break;
716 case FB_BUFFER:
717 fb_queue_flash(pname, buf->data, buf->sz);
718 break;
719 default:
720 die("unknown buffer type: %d", buf->type);
721 }
722}
723
David Pursell0b156632015-10-30 11:22:01 -0700724static std::vector<std::string> get_suffixes(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700725 std::vector<std::string> suffixes;
726 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700727 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700728 die("Could not get suffixes.\n");
729 }
730 return android::base::Split(suffix_list, ",");
731}
732
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800733static std::string verify_slot(Transport* transport, const char *slot, bool allow_all) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700734 if (strcmp(slot, "all") == 0) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800735 if (allow_all) {
736 return "all";
737 } else {
738 std::vector<std::string> suffixes = get_suffixes(transport);
739 if (!suffixes.empty()) {
740 return suffixes[0];
741 } else {
742 fprintf(stderr, "No known slots.\n");
743 exit(1);
744 }
745 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700746 }
David Pursell0b156632015-10-30 11:22:01 -0700747 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700748 for (const std::string &suffix : suffixes) {
749 if (suffix == slot)
750 return slot;
751 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800752 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700753 for (const std::string &suffix : suffixes) {
754 fprintf(stderr, "%s\n", suffix.c_str());
755 }
756 exit(1);
757}
758
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800759static std::string verify_slot(Transport* transport, const char *slot) {
760 return verify_slot(transport, slot, true);
761}
762
David Pursell0b156632015-10-30 11:22:01 -0700763static void do_for_partition(Transport* transport, const char *part, const char *slot,
764 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800765 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700766 std::string current_slot;
767
David Pursell0b156632015-10-30 11:22:01 -0700768 if (!fb_getvar(transport, std::string("has-slot:")+part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800769 /* If has-slot is not supported, the answer is no. */
770 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700771 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800772 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700773 if (!slot || slot[0] == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700774 if (!fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700775 die("Failed to identify current slot.\n");
776 }
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800777 func(std::string(part) + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700778 } else {
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800779 func(std::string(part) + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700780 }
781 } else {
782 if (force_slot && slot && slot[0]) {
David Pursell0b156632015-10-30 11:22:01 -0700783 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
784 part, slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700785 }
786 func(part);
787 }
788}
789
David Pursell0b156632015-10-30 11:22:01 -0700790/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
791 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
792 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
793 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700794 */
David Pursell0b156632015-10-30 11:22:01 -0700795static void do_for_partitions(Transport* transport, const char *part, const char *slot,
796 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800797 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700798
799 if (slot && strcmp(slot, "all") == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700800 if (!fb_getvar(transport, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700801 die("Could not check if partition %s has slot.", part);
802 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800803 if (has_slot == "yes") {
David Pursell0b156632015-10-30 11:22:01 -0700804 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700805 for (std::string &suffix : suffixes) {
David Pursell0b156632015-10-30 11:22:01 -0700806 do_for_partition(transport, part, suffix.c_str(), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700807 }
808 } else {
David Pursell0b156632015-10-30 11:22:01 -0700809 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700810 }
811 } else {
David Pursell0b156632015-10-30 11:22:01 -0700812 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700813 }
814}
815
David Pursell0b156632015-10-30 11:22:01 -0700816static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700817 struct fastboot_buffer buf;
818
David Pursell0b156632015-10-30 11:22:01 -0700819 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700820 die("cannot load '%s'", fname);
821 }
822 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700823}
824
Elliott Hughesfc797672015-04-07 20:12:50 -0700825static void do_update_signature(ZipArchiveHandle zip, char* fn) {
826 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700827 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700828 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800829 fb_queue_download("signature", data, sz);
830 fb_queue_command("signature", "installing signature");
831}
832
David Pursell0b156632015-10-30 11:22:01 -0700833static 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 -0800834 queue_info_dump();
835
Wink Savilleb98762f2011-04-04 17:54:59 -0700836 fb_queue_query_save("product", cur_product, sizeof(cur_product));
837
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700838 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700839 int error = OpenArchive(filename, &zip);
840 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100841 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700842 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700843 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800844
Elliott Hughesfc797672015-04-07 20:12:50 -0700845 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700846 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700847 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100848 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700849 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800850 }
851
Elliott Hughes253c18d2015-03-18 22:47:09 -0700852 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800853
Elliott Hughesacdbe922015-06-02 21:37:05 -0700854 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700855 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700856 if (fd == -1) {
857 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700858 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700859 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100860 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700861 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700862 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700863 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -0700864 int rc = load_buf_fd(transport, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700865 if (rc) die("cannot load %s from flash", images[i].img_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700866
867 auto update = [&](const std::string &partition) {
868 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -0700869 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700870 fb_queue_erase(partition.c_str());
871 }
872 flash_buf(partition.c_str(), &buf);
873 /* not closing the fd here since the sparse code keeps the fd around
874 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
875 * program exits.
876 */
877 };
David Pursell0b156632015-10-30 11:22:01 -0700878 do_for_partitions(transport, images[i].part_name, slot_override, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800879 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700880
881 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800882}
883
Elliott Hughesfc797672015-04-07 20:12:50 -0700884static void do_send_signature(char* fn) {
885 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800886 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -0700887
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800888 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800889
Elliott Hughesfc797672015-04-07 20:12:50 -0700890 strcpy(xtn, ".sig");
891
892 int64_t sz;
893 void* data = load_file(fn, &sz);
894 strcpy(xtn, ".img");
895 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800896 fb_queue_download("signature", data, sz);
897 fb_queue_command("signature", "installing signature");
898}
899
David Pursell0b156632015-10-30 11:22:01 -0700900static void do_flashall(Transport* transport, const char* slot_override, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800901 queue_info_dump();
902
Wink Savilleb98762f2011-04-04 17:54:59 -0700903 fb_queue_query_save("product", cur_product, sizeof(cur_product));
904
Elliott Hughes253c18d2015-03-18 22:47:09 -0700905 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -0700906 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800907
Elliott Hughesfc797672015-04-07 20:12:50 -0700908 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700909 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700910 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700911
912 setup_requirements(reinterpret_cast<char*>(data), sz);
913
914 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700915 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700916 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -0700917 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700918 if (images[i].is_optional)
919 continue;
920 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700921 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700922
923 auto flashall = [&](const std::string &partition) {
924 do_send_signature(fname);
David Pursell0b156632015-10-30 11:22:01 -0700925 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700926 fb_queue_erase(partition.c_str());
927 }
928 flash_buf(partition.c_str(), &buf);
929 };
David Pursell0b156632015-10-30 11:22:01 -0700930 do_for_partitions(transport, images[i].part_name, slot_override, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800931 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800932}
933
934#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800935#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800936
Elliott Hughes45be42e2015-09-02 20:15:48 -0700937static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -0700938{
Patrick Tjin51e8b032015-09-01 08:15:23 -0700939 if (argc <= 2) return 0;
940 skip(2);
941
942 /*
943 * Process unlock_bootloader, we have to load the message file
944 * and send that to the remote device.
945 */
946 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -0700947
948 int64_t sz;
949 void* data = load_file(*argv, &sz);
950 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -0700951 fb_queue_download("unlock_message", data, sz);
952 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
953 skip(1);
954 return 0;
955}
956
Elliott Hughes45be42e2015-09-02 20:15:48 -0700957static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800958{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800959 char command[256];
960 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800961
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800962 command[0] = 0;
963 while(1) {
964 strcat(command,*argv);
965 skip(1);
966 if(argc == 0) break;
967 strcat(command," ");
968 }
969
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800970 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800971 return 0;
972}
973
Colin Crossf8387882012-05-24 17:18:41 -0700974static int64_t parse_num(const char *arg)
975{
976 char *endptr;
977 unsigned long long num;
978
979 num = strtoull(arg, &endptr, 0);
980 if (endptr == arg) {
981 return -1;
982 }
983
984 if (*endptr == 'k' || *endptr == 'K') {
985 if (num >= (-1ULL) / 1024) {
986 return -1;
987 }
988 num *= 1024LL;
989 endptr++;
990 } else if (*endptr == 'm' || *endptr == 'M') {
991 if (num >= (-1ULL) / (1024 * 1024)) {
992 return -1;
993 }
994 num *= 1024LL * 1024LL;
995 endptr++;
996 } else if (*endptr == 'g' || *endptr == 'G') {
997 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
998 return -1;
999 }
1000 num *= 1024LL * 1024LL * 1024LL;
1001 endptr++;
1002 }
1003
1004 if (*endptr != '\0') {
1005 return -1;
1006 }
1007
1008 if (num > INT64_MAX) {
1009 return -1;
1010 }
1011
1012 return num;
1013}
1014
David Pursell0b156632015-10-30 11:22:01 -07001015static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001016 const char* partition, int skip_if_not_supported,
1017 const char* type_override, const char* size_override) {
1018 std::string partition_type, partition_size;
1019
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001020 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001021 const char* errMsg = nullptr;
1022 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001023 int fd;
1024
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001025 unsigned int limit = INT_MAX;
1026 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001027 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001028 }
1029 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001030 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001031 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001032
David Pursell0b156632015-10-30 11:22:01 -07001033 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001034 errMsg = "Can't determine partition type.\n";
1035 goto failed;
1036 }
JP Abgrall7e859742014-05-06 15:14:15 -07001037 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001038 if (partition_type != type_override) {
1039 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1040 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001041 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001042 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001043 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001044
David Pursell0b156632015-10-30 11:22:01 -07001045 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001046 errMsg = "Unable to get partition size\n";
1047 goto failed;
1048 }
JP Abgrall7e859742014-05-06 15:14:15 -07001049 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001050 if (partition_size != size_override) {
1051 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1052 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001053 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001054 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001055 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001056 // Some bootloaders (angler, for example), send spurious leading whitespace.
1057 partition_size = android::base::Trim(partition_size);
1058 // Some bootloaders (hammerhead, for example) use implicit hex.
1059 // This code used to use strtol with base 16.
1060 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001061
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001062 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001063 if (!gen) {
1064 if (skip_if_not_supported) {
1065 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001066 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001067 return;
1068 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001069 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1070 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001071 return;
1072 }
1073
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001074 int64_t size;
1075 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1076 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1077 return;
1078 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001079
1080 fd = fileno(tmpfile());
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001081 if (fs_generator_generate(gen, fd, size)) {
1082 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001083 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001084 return;
1085 }
1086
David Pursell0b156632015-10-30 11:22:01 -07001087 if (load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001088 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001089 close(fd);
1090 return;
1091 }
1092 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001093 return;
1094
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001095failed:
1096 if (skip_if_not_supported) {
1097 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001098 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001099 }
1100 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
1101}
1102
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001103int main(int argc, char **argv)
1104{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001105 bool wants_wipe = false;
1106 bool wants_reboot = false;
1107 bool wants_reboot_bootloader = false;
1108 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001109 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001110 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001111 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001112 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001113 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001114 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001115
JP Abgrall7b8970c2013-03-07 17:06:41 -08001116 const struct option longopts[] = {
1117 {"base", required_argument, 0, 'b'},
1118 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001119 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001120 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001121 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001122 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001123 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001124 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001125 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001126 {"help", no_argument, 0, 'h'},
1127 {"unbuffered", no_argument, 0, 0},
1128 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001129 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001130 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001131 {"set-active", optional_argument, 0, 'a'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001132 {0, 0, 0, 0}
1133 };
Colin Cross8879f982012-05-22 17:53:34 -07001134
1135 serial = getenv("ANDROID_SERIAL");
1136
1137 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001138 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 -07001139 if (c < 0) {
1140 break;
1141 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001142 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001143 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001144 case 'a':
1145 wants_set_active = true;
1146 if (optarg)
1147 next_active = optarg;
1148 break;
Colin Cross8879f982012-05-22 17:53:34 -07001149 case 'b':
1150 base_addr = strtoul(optarg, 0, 16);
1151 break;
Colin Cross8879f982012-05-22 17:53:34 -07001152 case 'c':
1153 cmdline = optarg;
1154 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001155 case 'h':
1156 usage();
1157 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001158 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001159 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001160 unsigned long val;
1161
1162 val = strtoul(optarg, &endptr, 0);
1163 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1164 die("invalid vendor id '%s'", optarg);
1165 vendor_id = (unsigned short)val;
1166 break;
1167 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001168 case 'k':
1169 kernel_offset = strtoul(optarg, 0, 16);
1170 break;
1171 case 'l':
1172 long_listing = 1;
1173 break;
1174 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001175 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001176 if (!page_size) die("invalid page size");
1177 break;
1178 case 'p':
1179 product = optarg;
1180 break;
1181 case 'r':
1182 ramdisk_offset = strtoul(optarg, 0, 16);
1183 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001184 case 't':
1185 tags_offset = strtoul(optarg, 0, 16);
1186 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001187 case 's':
1188 serial = optarg;
1189 break;
1190 case 'S':
1191 sparse_limit = parse_num(optarg);
1192 if (sparse_limit < 0) {
1193 die("invalid sparse limit");
1194 }
1195 break;
1196 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001197 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001198 break;
1199 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001200 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001201 break;
Colin Cross8879f982012-05-22 17:53:34 -07001202 case '?':
1203 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001204 case 0:
1205 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001206 setvbuf(stdout, nullptr, _IONBF, 0);
1207 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001208 } else if (strcmp("version", longopts[longindex].name) == 0) {
1209 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1210 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001211 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1212 slot_override = std::string(optarg);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001213 }
1214 break;
Colin Cross8879f982012-05-22 17:53:34 -07001215 default:
1216 abort();
1217 }
1218 }
1219
1220 argc -= optind;
1221 argv += optind;
1222
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001223 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001224 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001225 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001226 }
1227
Colin Cross8fb6e062012-07-24 16:36:41 -07001228 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001229 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001230 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001231 return 0;
1232 }
1233
Colin Crossc7b75dc2012-08-29 18:17:06 -07001234 if (argc > 0 && !strcmp(*argv, "help")) {
1235 usage();
1236 return 0;
1237 }
1238
David Pursell0b156632015-10-30 11:22:01 -07001239 Transport* transport = open_device();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001240 if (slot_override != "")
David Pursell0b156632015-10-30 11:22:01 -07001241 slot_override = verify_slot(transport, slot_override.c_str());
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001242 if (next_active != "")
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001243 next_active = verify_slot(transport, next_active.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001244
1245 if (wants_set_active) {
1246 if (next_active == "") {
1247 if (slot_override == "") {
1248 wants_set_active = false;
1249 } else {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001250 next_active = verify_slot(transport, slot_override.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001251 }
1252 }
1253 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001254
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001255 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001256 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001257 require(2);
1258 fb_queue_display(argv[1], argv[1]);
1259 skip(2);
1260 } else if(!strcmp(*argv, "erase")) {
1261 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001262
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001263 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001264 std::string partition_type;
1265 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001266 fs_get_generator(partition_type) != nullptr) {
1267 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1268 partition_type.c_str());
1269 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001270
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001271 fb_queue_erase(partition.c_str());
1272 };
David Pursell0b156632015-10-30 11:22:01 -07001273 do_for_partitions(transport, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001274 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001275 } else if(!strncmp(*argv, "format", strlen("format"))) {
1276 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001277 char *type_override = nullptr;
1278 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001279 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001280 /*
1281 * Parsing for: "format[:[type][:[size]]]"
1282 * Some valid things:
1283 * - select ontly the size, and leave default fs type:
1284 * format::0x4000000 userdata
1285 * - default fs type and size:
1286 * format userdata
1287 * format:: userdata
1288 */
1289 overrides = strchr(*argv, ':');
1290 if (overrides) {
1291 overrides++;
1292 size_override = strchr(overrides, ':');
1293 if (size_override) {
1294 size_override[0] = '\0';
1295 size_override++;
1296 }
1297 type_override = overrides;
1298 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001299 if (type_override && !type_override[0]) type_override = nullptr;
1300 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001301
1302 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001303 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001304 fb_queue_erase(partition.c_str());
1305 }
David Pursell0b156632015-10-30 11:22:01 -07001306 fb_perform_format(transport, partition.c_str(), 0, type_override, size_override);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001307 };
David Pursell0b156632015-10-30 11:22:01 -07001308 do_for_partitions(transport, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001309 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001310 } else if(!strcmp(*argv, "signature")) {
1311 require(2);
1312 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001313 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001314 if (sz != 256) die("signature must be 256 bytes");
1315 fb_queue_download("signature", data, sz);
1316 fb_queue_command("signature", "installing signature");
1317 skip(2);
1318 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001319 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001320 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001321 if (argc > 0) {
1322 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001323 wants_reboot = false;
1324 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001325 skip(1);
1326 }
1327 }
1328 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001329 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001330 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001331 skip(1);
1332 } else if (!strcmp(*argv, "continue")) {
1333 fb_queue_command("continue", "resuming boot");
1334 skip(1);
1335 } else if(!strcmp(*argv, "boot")) {
1336 char *kname = 0;
1337 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001338 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001339 skip(1);
1340 if (argc > 0) {
1341 kname = argv[0];
1342 skip(1);
1343 }
1344 if (argc > 0) {
1345 rname = argv[0];
1346 skip(1);
1347 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001348 if (argc > 0) {
1349 sname = argv[0];
1350 skip(1);
1351 }
1352 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001353 if (data == 0) return 1;
1354 fb_queue_download("boot.img", data, sz);
1355 fb_queue_command("boot", "booting");
1356 } else if(!strcmp(*argv, "flash")) {
1357 char *pname = argv[1];
1358 char *fname = 0;
1359 require(2);
1360 if (argc > 2) {
1361 fname = argv[2];
1362 skip(3);
1363 } else {
1364 fname = find_item(pname, product);
1365 skip(2);
1366 }
1367 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001368
1369 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001370 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001371 fb_queue_erase(partition.c_str());
1372 }
David Pursell0b156632015-10-30 11:22:01 -07001373 do_flash(transport, partition.c_str(), fname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001374 };
David Pursell0b156632015-10-30 11:22:01 -07001375 do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001376 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001377 char *kname = argv[2];
1378 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001379 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001380 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001381 skip(3);
1382 if (argc > 0) {
1383 rname = argv[0];
1384 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001385 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001386 if (argc > 0) {
1387 sname = argv[0];
1388 skip(1);
1389 }
1390 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001391 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001392 auto flashraw = [&](const std::string &partition) {
1393 fb_queue_flash(partition.c_str(), data, sz);
1394 };
David Pursell0b156632015-10-30 11:22:01 -07001395 do_for_partitions(transport, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001396 } else if(!strcmp(*argv, "flashall")) {
1397 skip(1);
David Pursell0b156632015-10-30 11:22:01 -07001398 do_flashall(transport, slot_override.c_str(), erase_first);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001399 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001400 } else if(!strcmp(*argv, "update")) {
1401 if (argc > 1) {
David Pursell0b156632015-10-30 11:22:01 -07001402 do_update(transport, argv[1], slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001403 skip(2);
1404 } else {
David Pursell0b156632015-10-30 11:22:01 -07001405 do_update(transport, "update.zip", slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001406 skip(1);
1407 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001408 wants_reboot = 1;
1409 } else if(!strcmp(*argv, "set_active")) {
1410 require(2);
1411 std::string slot = verify_slot(transport, argv[1], false);
1412 fb_set_active(slot.c_str());
1413 skip(2);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001414 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001415 } else if(!strcmp(*argv, "oem")) {
1416 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001417 } else if(!strcmp(*argv, "flashing")) {
1418 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1419 !strcmp(*(argv+1), "lock") ||
1420 !strcmp(*(argv+1), "unlock_critical") ||
1421 !strcmp(*(argv+1), "lock_critical") ||
1422 !strcmp(*(argv+1), "get_unlock_ability") ||
1423 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1424 !strcmp(*(argv+1), "lock_bootloader"))) {
1425 argc = do_oem_command(argc, argv);
1426 } else
1427 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1428 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001429 } else {
1430 usage();
1431 return 1;
1432 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001433 } else {
1434 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001435 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001436 }
1437 }
1438
1439 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001440 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001441 fb_queue_erase("userdata");
David Pursell0b156632015-10-30 11:22:01 -07001442 fb_perform_format(transport, "userdata", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001443
1444 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001445 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001446 fprintf(stderr, "wiping cache...\n");
1447 fb_queue_erase("cache");
David Pursell0b156632015-10-30 11:22:01 -07001448 fb_perform_format(transport, "cache", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001449 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001450 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001451 if (wants_set_active) {
1452 fb_set_active(next_active.c_str());
1453 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001454 if (wants_reboot) {
1455 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001456 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001457 } else if (wants_reboot_bootloader) {
1458 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001459 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001460 }
1461
David Pursell0b156632015-10-30 11:22:01 -07001462 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001463}