blob: 2ac8452c343ed11d23367466feeb5817c0d55e1d [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 Hughes2fd45a92015-10-30 11:49:47 -070047#include <base/parseint.h>
Elliott Hughes77c0e662015-11-02 15:51:12 -080048#include <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
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070052#include <base/strings.h>
53#include <base/parseint.h>
54
Elliott Hughes253c18d2015-03-18 22:47:09 -070055#include "bootimg_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070057#include "fs.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058
Colin Crossf8387882012-05-24 17:18:41 -070059#ifndef O_BINARY
60#define O_BINARY 0
61#endif
62
Rom Lemarchand622810c2013-06-28 09:54:59 -070063#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
64
Wink Savilleb98762f2011-04-04 17:54:59 -070065char cur_product[FB_RESPONSE_SZ + 1];
66
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067static const char *serial = 0;
68static const char *product = 0;
69static const char *cmdline = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070071static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070072static int64_t sparse_limit = -1;
73static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074
Elliott Hughesfc797672015-04-07 20:12:50 -070075static unsigned page_size = 2048;
76static unsigned base_addr = 0x10000000;
77static unsigned kernel_offset = 0x00008000;
78static unsigned ramdisk_offset = 0x01000000;
79static unsigned second_offset = 0x00f00000;
80static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080081
Rom Lemarchand622810c2013-06-28 09:54:59 -070082enum fb_buffer_type {
83 FB_BUFFER,
84 FB_BUFFER_SPARSE,
85};
86
87struct fastboot_buffer {
88 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070089 void* data;
90 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070091};
92
93static struct {
94 char img_name[13];
95 char sig_name[13];
96 char part_name[9];
97 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -070098} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -070099 {"boot.img", "boot.sig", "boot", false},
100 {"recovery.img", "recovery.sig", "recovery", true},
101 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700102 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700103};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700104
Elliott Hughesfc797672015-04-07 20:12:50 -0700105static char* find_item(const char* item, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700107 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108 char path[PATH_MAX + 128];
109
110 if(!strcmp(item,"boot")) {
111 fn = "boot.img";
112 } else if(!strcmp(item,"recovery")) {
113 fn = "recovery.img";
114 } else if(!strcmp(item,"system")) {
115 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700116 } else if(!strcmp(item,"vendor")) {
117 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118 } else if(!strcmp(item,"userdata")) {
119 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700120 } else if(!strcmp(item,"cache")) {
121 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 } else if(!strcmp(item,"info")) {
123 fn = "android-info.txt";
124 } else {
125 fprintf(stderr,"unknown partition '%s'\n", item);
126 return 0;
127 }
128
129 if(product) {
130 get_my_path(path);
131 sprintf(path + strlen(path),
132 "../../../target/product/%s/%s", product, fn);
133 return strdup(path);
134 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800135
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 dir = getenv("ANDROID_PRODUCT_OUT");
137 if((dir == 0) || (dir[0] == 0)) {
138 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
139 return 0;
140 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800141
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 sprintf(path, "%s/%s", dir, fn);
143 return strdup(path);
144}
145
Elliott Hughesfc797672015-04-07 20:12:50 -0700146static int64_t get_file_size(int fd) {
147 struct stat sb;
148 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700149}
150
Elliott Hughesfc797672015-04-07 20:12:50 -0700151static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800152 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700153 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154
Elliott Hughesfc797672015-04-07 20:12:50 -0700155 *sz = get_file_size(fd);
156 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700157 goto oops;
158 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159
Elliott Hughesfc797672015-04-07 20:12:50 -0700160 data = (char*) malloc(*sz);
161 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162
Elliott Hughesfc797672015-04-07 20:12:50 -0700163 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164 close(fd);
165
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 return data;
167
168oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800169 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170 close(fd);
171 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800172 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 return 0;
174}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175
Elliott Hughesfc797672015-04-07 20:12:50 -0700176static void* load_file(const char* fn, int64_t* sz) {
177 int fd = open(fn, O_RDONLY | O_BINARY);
178 if (fd == -1) return nullptr;
179 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700180}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181
Elliott Hughesfc797672015-04-07 20:12:50 -0700182static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700183 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700184 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700185 return -1;
186 }
187
188 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
189 return -1;
190 }
191
Scott Anderson13081c62012-04-06 12:39:30 -0700192 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700194 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
195 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 return 0;
197}
198
Elliott Hughesfc797672015-04-07 20:12:50 -0700199static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800200 return match_fastboot_with_serial(info, serial);
201}
202
Elliott Hughesfc797672015-04-07 20:12:50 -0700203static int list_devices_callback(usb_ifc_info* info) {
204 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700205 const char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700206 if (!info->writable) {
207 serial = "no permissions"; // like "adb devices"
208 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 if (!serial[0]) {
210 serial = "????????????";
211 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700212 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700213 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700214 printf("%s\tfastboot\n", serial);
Stephen Hines04f89532014-11-26 14:54:43 -0800215 } else if (strcmp("", info->device_path) == 0) {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700216 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700217 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700218 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700219 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 }
221
222 return -1;
223}
224
Elliott Hughesfc797672015-04-07 20:12:50 -0700225static usb_handle* open_device() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226 static usb_handle *usb = 0;
227 int announce = 1;
228
229 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800230
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 for(;;) {
232 usb = usb_open(match_fastboot);
233 if(usb) return usb;
234 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800235 announce = 0;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700236 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700238 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 }
240}
241
Elliott Hughesfc797672015-04-07 20:12:50 -0700242static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 // We don't actually open a USB device here,
244 // just getting our callback called so we can
245 // list all the connected devices.
246 usb_open(list_devices_callback);
247}
248
Elliott Hughesfc797672015-04-07 20:12:50 -0700249static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250 fprintf(stderr,
251/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
252 "usage: fastboot [ <option> ] <command>\n"
253 "\n"
254 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700255 " update <filename> Reflash device from update.zip.\n"
256 " flashall Flash boot, system, vendor, and --\n"
257 " if found -- recovery.\n"
258 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
259 " flashing lock Locks the device. Prevents flashing.\n"
260 " flashing unlock Unlocks the device. Allows flashing\n"
261 " any partition except\n"
262 " bootloader-related partitions.\n"
263 " flashing lock_critical Prevents flashing bootloader-related\n"
264 " partitions.\n"
265 " flashing unlock_critical Enables flashing bootloader-related\n"
266 " partitions.\n"
267 " flashing get_unlock_ability Queries bootloader to see if the\n"
268 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700269 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700270 " unlock nonce.\n"
271 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700272 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700273 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700274 " erase <partition> Erase a flash partition.\n"
275 " format[:[<fs type>][:[<size>]] <partition>\n"
276 " Format a flash partition. Can\n"
277 " override the fs type and/or size\n"
278 " the bootloader reports.\n"
279 " getvar <variable> Display a bootloader variable.\n"
280 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
281 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
282 " Create bootimage and flash it.\n"
283 " devices [-l] List all connected devices [with\n"
284 " device paths].\n"
285 " continue Continue with autoboot.\n"
286 " reboot [bootloader] Reboot device [into bootloader].\n"
287 " reboot-bootloader Reboot device into bootloader.\n"
288 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289 "\n"
290 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700291 " -w Erase userdata and cache (and format\n"
292 " if supported by partition type).\n"
293 " -u Do not erase partition before\n"
294 " formatting.\n"
295 " -s <specific device> Specify device serial number\n"
296 " or path to device port.\n"
297 " -p <product> Specify product name.\n"
298 " -c <cmdline> Override kernel commandline.\n"
299 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800300 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700301 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800302 " --kernel-offset Specify a custom kernel offset.\n"
303 " (default: 0x00008000)\n"
304 " --ramdisk-offset Specify a custom ramdisk offset.\n"
305 " (default: 0x01000000)\n"
306 " --tags-offset Specify a custom tags offset.\n"
307 " (default: 0x00000100)\n"
308 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700309 " (default: 2048).\n"
310 " -S <size>[K|M|G] Automatically sparse files greater\n"
311 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700312 " --slot <suffix> Specify slot suffix to be used if the\n"
313 " device supports slots. This will be\n"
314 " added to all partition names that use\n"
315 " slots. 'all' can be given to refer\n"
316 " to all slots. If this is not given,\n"
317 " slotted partitions will default to\n"
318 " the current active slot.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800319 " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800320 " provided, this will default to the value\n"
321 " given by --slot. If slots are not\n"
322 " supported, this does nothing.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800323 " --unbuffered Do not buffer input or output.\n"
324 " --version Display version.\n"
325 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327}
Elliott Hughesfc797672015-04-07 20:12:50 -0700328static void* load_bootable_image(const char* kernel, const char* ramdisk,
329 const char* secondstage, int64_t* sz,
330 const char* cmdline) {
331 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332 fprintf(stderr, "no image specified\n");
333 return 0;
334 }
335
Elliott Hughesfc797672015-04-07 20:12:50 -0700336 int64_t ksize;
337 void* kdata = load_file(kernel, &ksize);
338 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800339 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340 return 0;
341 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800342
Elliott Hughesfc797672015-04-07 20:12:50 -0700343 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700345 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800346
Elliott Hughesfc797672015-04-07 20:12:50 -0700347 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
349 return 0;
350 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800351
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 *sz = ksize;
353 return kdata;
354 }
355
Elliott Hughesfc797672015-04-07 20:12:50 -0700356 void* rdata = nullptr;
357 int64_t rsize = 0;
358 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700360 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800361 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362 return 0;
363 }
364 }
365
Elliott Hughesfc797672015-04-07 20:12:50 -0700366 void* sdata = nullptr;
367 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200368 if (secondstage) {
369 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700370 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200371 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
372 return 0;
373 }
374 }
375
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700377 int64_t bsize = 0;
378 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800379 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200380 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800381 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700382 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383 fprintf(stderr,"failed to create boot.img\n");
384 return 0;
385 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700386 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
387 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800389
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390 return bdata;
391}
392
Elliott Hughesfc797672015-04-07 20:12:50 -0700393static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394{
Yusuke Sato07447542015-06-25 14:39:19 -0700395 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700396 ZipEntry zip_entry;
397 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700398 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000399 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400 }
401
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700402 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700404 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700405 if (data == nullptr) {
406 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, 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 Hughesa82c89d2015-03-19 11:44:32 -0700410 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
411 if (error != 0) {
412 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000414 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000416
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 return data;
418}
419
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700420#if defined(_WIN32)
421
422// TODO: move this to somewhere it can be shared.
423
424#include <windows.h>
425
426// Windows' tmpfile(3) requires administrator rights because
427// it creates temporary files in the root directory.
428static FILE* win32_tmpfile() {
429 char temp_path[PATH_MAX];
430 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
431 if (nchars == 0 || nchars >= sizeof(temp_path)) {
432 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
433 return nullptr;
434 }
435
436 char filename[PATH_MAX];
437 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
438 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
439 return nullptr;
440 }
441
442 return fopen(filename, "w+bTD");
443}
444
445#define tmpfile win32_tmpfile
446
447#endif
448
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700449static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
450 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700451 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700452 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
453 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700454 return -1;
455 }
456
Yusuke Sato07447542015-06-25 14:39:19 -0700457 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700458 ZipEntry zip_entry;
459 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
460 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000461 return -1;
462 }
463
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700464 int fd = fileno(fp);
465 int error = ExtractEntryToFile(zip, &zip_entry, fd);
466 if (error != 0) {
467 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
468 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700469 }
470
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000471 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700472 return fd;
473}
474
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475static char *strip(char *s)
476{
477 int n;
478 while(*s && isspace(*s)) s++;
479 n = strlen(s);
480 while(n-- > 0) {
481 if(!isspace(s[n])) break;
482 s[n] = 0;
483 }
484 return s;
485}
486
487#define MAX_OPTIONS 32
488static int setup_requirement_line(char *name)
489{
490 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700491 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492 unsigned n, count;
493 char *x;
494 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800495
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496 if (!strncmp(name, "reject ", 7)) {
497 name += 7;
498 invert = 1;
499 } else if (!strncmp(name, "require ", 8)) {
500 name += 8;
501 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700502 } else if (!strncmp(name, "require-for-product:", 20)) {
503 // Get the product and point name past it
504 prod = name + 20;
505 name = strchr(name, ' ');
506 if (!name) return -1;
507 *name = 0;
508 name += 1;
509 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510 }
511
512 x = strchr(name, '=');
513 if (x == 0) return 0;
514 *x = 0;
515 val[0] = x + 1;
516
517 for(count = 1; count < MAX_OPTIONS; count++) {
518 x = strchr(val[count - 1],'|');
519 if (x == 0) break;
520 *x = 0;
521 val[count] = x + 1;
522 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800523
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524 name = strip(name);
525 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800526
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800527 name = strip(name);
528 if (name == 0) return -1;
529
Elliott Hughes253c18d2015-03-18 22:47:09 -0700530 const char* var = name;
531 // Work around an unfortunate name mismatch.
532 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533
Elliott Hughes253c18d2015-03-18 22:47:09 -0700534 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535 if (out == 0) return -1;
536
537 for(n = 0; n < count; n++) {
538 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700539 if (out[n] == 0) {
540 for(size_t i = 0; i < n; ++i) {
541 free((char*) out[i]);
542 }
543 free(out);
544 return -1;
545 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800546 }
547
Elliott Hughes253c18d2015-03-18 22:47:09 -0700548 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800549 return 0;
550}
551
Elliott Hughesfc797672015-04-07 20:12:50 -0700552static void setup_requirements(char* data, int64_t sz) {
553 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800554 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700555 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800556 *s++ = 0;
557 if (setup_requirement_line(data)) {
558 die("out of memory");
559 }
560 data = s;
561 } else {
562 s++;
563 }
564 }
565}
566
Elliott Hughesfc797672015-04-07 20:12:50 -0700567static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800568 fb_queue_notice("--------------------------------------------");
569 fb_queue_display("version-bootloader", "Bootloader Version...");
570 fb_queue_display("version-baseband", "Baseband Version.....");
571 fb_queue_display("serialno", "Serial Number........");
572 fb_queue_notice("--------------------------------------------");
573}
574
Rom Lemarchand622810c2013-06-28 09:54:59 -0700575static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700576{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700577 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700578 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700579 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700580 }
581
Elliott Hughesfc797672015-04-07 20:12:50 -0700582 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700583 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700584 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700585 }
586
Elliott Hughes253c18d2015-03-18 22:47:09 -0700587 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700588 if (!out_s) {
589 die("Failed to allocate sparse file array\n");
590 }
591
592 files = sparse_file_resparse(s, max_size, out_s, files);
593 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700594 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700595 }
596
597 return out_s;
598}
599
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700600static int64_t get_target_sparse_limit(usb_handle* usb) {
601 std::string max_download_size;
Elliott Hughes2030bac2015-11-03 08:14:43 -0800602 if (!fb_getvar(usb, "max-download-size", &max_download_size) || max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800603 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700604 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700605 }
606
Elliott Hughes77c0e662015-11-02 15:51:12 -0800607 // Some bootloaders (angler, for example) send spurious whitespace too.
608 max_download_size = android::base::Trim(max_download_size);
609
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700610 uint64_t limit;
611 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800612 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700613 return 0;
614 }
615 if (limit > 0) {
616 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
617 }
Colin Crossf8387882012-05-24 17:18:41 -0700618 return limit;
619}
620
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700621static int64_t get_sparse_limit(usb_handle* usb, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700622 int64_t limit;
623
624 if (sparse_limit == 0) {
625 return 0;
626 } else if (sparse_limit > 0) {
627 limit = sparse_limit;
628 } else {
629 if (target_sparse_limit == -1) {
630 target_sparse_limit = get_target_sparse_limit(usb);
631 }
632 if (target_sparse_limit > 0) {
633 limit = target_sparse_limit;
634 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700635 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700636 }
637 }
638
639 if (size > limit) {
640 return limit;
641 }
642
643 return 0;
644}
645
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700646// Until we get lazy inode table init working in make_ext4fs, we need to
647// erase partitions of type ext4 before flashing a filesystem so no stale
648// inodes are left lying around. Otherwise, e2fsck gets very upset.
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800649static bool needs_erase(usb_handle* usb, const char* partition) {
650 std::string partition_type;
651 if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) {
652 return false;
653 }
654 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700655}
656
Elliott Hughesfc797672015-04-07 20:12:50 -0700657static int load_buf_fd(usb_handle* usb, int fd, struct fastboot_buffer* buf) {
658 int64_t sz = get_file_size(fd);
659 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000660 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700661 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700662
Elliott Hughesfc797672015-04-07 20:12:50 -0700663 lseek64(fd, 0, SEEK_SET);
664 int64_t limit = get_sparse_limit(usb, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700665 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700666 sparse_file** s = load_sparse_files(fd, limit);
667 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700668 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700669 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700670 buf->type = FB_BUFFER_SPARSE;
671 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700672 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700673 void* data = load_fd(fd, &sz);
674 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700675 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700676 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000677 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700678 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700679
680 return 0;
681}
682
683static int load_buf(usb_handle *usb, const char *fname,
684 struct fastboot_buffer *buf)
685{
686 int fd;
687
688 fd = open(fname, O_RDONLY | O_BINARY);
689 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700690 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700691 }
692
693 return load_buf_fd(usb, fd, buf);
694}
695
696static void flash_buf(const char *pname, struct fastboot_buffer *buf)
697{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700698 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700699
700 switch (buf->type) {
701 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700702 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700703 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700704 int64_t sz = sparse_file_len(*s, true, false);
705 fb_queue_flash_sparse(pname, *s++, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700706 }
707 break;
708 case FB_BUFFER:
709 fb_queue_flash(pname, buf->data, buf->sz);
710 break;
711 default:
712 die("unknown buffer type: %d", buf->type);
713 }
714}
715
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700716static std::vector<std::string> get_suffixes(usb_handle* usb) {
717 std::vector<std::string> suffixes;
718 std::string suffix_list;
719 if (!fb_getvar(usb, "slot-suffixes", &suffix_list)) {
720 die("Could not get suffixes.\n");
721 }
722 return android::base::Split(suffix_list, ",");
723}
724
725static std::string verify_slot(usb_handle* usb, const char *slot) {
726 if (strcmp(slot, "all") == 0) {
727 return "all";
728 }
729 std::vector<std::string> suffixes = get_suffixes(usb);
730 for (const std::string &suffix : suffixes) {
731 if (suffix == slot)
732 return slot;
733 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800734 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700735 for (const std::string &suffix : suffixes) {
736 fprintf(stderr, "%s\n", suffix.c_str());
737 }
738 exit(1);
739}
740
741static void do_for_partition(usb_handle* usb, const char *part, const char *slot, std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800742 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700743 std::string current_slot;
744
Daniel Rosenberga7974792015-11-11 16:13:13 -0800745 if (!fb_getvar(usb, std::string("has-slot:")+part, &has_slot)) {
746 /* If has-slot is not supported, the answer is no. */
747 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700748 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800749 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700750 if (!slot || slot[0] == 0) {
751 if (!fb_getvar(usb, "current-slot", &current_slot)) {
752 die("Failed to identify current slot.\n");
753 }
754 func(std::string(part) + '-' + current_slot);
755 } else {
756 func(std::string(part) + '-' + slot);
757 }
758 } else {
759 if (force_slot && slot && slot[0]) {
760 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n", part, slot);
761 }
762 func(part);
763 }
764}
765
766/* This function will find the real partition name given a base name, and a slot. If slot is NULL or empty,
767 * it will use the current slot. If slot is "all", it will return a list of all possible partition names.
768 * If force_slot is true, it will fail if a slot is specified, and the given partition does not support slots.
769 */
770static void do_for_partitions(usb_handle* usb, const char *part, const char *slot, std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800771 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700772
773 if (slot && strcmp(slot, "all") == 0) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800774 if (!fb_getvar(usb, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700775 die("Could not check if partition %s has slot.", part);
776 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800777 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700778 std::vector<std::string> suffixes = get_suffixes(usb);
779 for (std::string &suffix : suffixes) {
780 do_for_partition(usb, part, suffix.c_str(), func, force_slot);
781 }
782 } else {
783 do_for_partition(usb, part, "", func, force_slot);
784 }
785 } else {
786 do_for_partition(usb, part, slot, func, force_slot);
787 }
788}
789
Elliott Hughesfc797672015-04-07 20:12:50 -0700790static void do_flash(usb_handle* usb, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700791 struct fastboot_buffer buf;
792
793 if (load_buf(usb, fname, &buf)) {
794 die("cannot load '%s'", fname);
795 }
796 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700797}
798
Elliott Hughesfc797672015-04-07 20:12:50 -0700799static void do_update_signature(ZipArchiveHandle zip, char* fn) {
800 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700801 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700802 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800803 fb_queue_download("signature", data, sz);
804 fb_queue_command("signature", "installing signature");
805}
806
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700807static void do_update(usb_handle* usb, const char* filename, const char* slot_override, bool erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800808 queue_info_dump();
809
Wink Savilleb98762f2011-04-04 17:54:59 -0700810 fb_queue_query_save("product", cur_product, sizeof(cur_product));
811
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700812 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700813 int error = OpenArchive(filename, &zip);
814 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100815 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700816 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700817 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800818
Elliott Hughesfc797672015-04-07 20:12:50 -0700819 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700820 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700821 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100822 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700823 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800824 }
825
Elliott Hughes253c18d2015-03-18 22:47:09 -0700826 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800827
Elliott Hughesacdbe922015-06-02 21:37:05 -0700828 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700829 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700830 if (fd == -1) {
831 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700832 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700833 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100834 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700835 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700836 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700837 fastboot_buffer buf;
838 int rc = load_buf_fd(usb, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700839 if (rc) die("cannot load %s from flash", images[i].img_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700840
841 auto update = [&](const std::string &partition) {
842 do_update_signature(zip, images[i].sig_name);
843 if (erase_first && needs_erase(usb, partition.c_str())) {
844 fb_queue_erase(partition.c_str());
845 }
846 flash_buf(partition.c_str(), &buf);
847 /* not closing the fd here since the sparse code keeps the fd around
848 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
849 * program exits.
850 */
851 };
852 do_for_partitions(usb, images[i].part_name, slot_override, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800853 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700854
855 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800856}
857
Elliott Hughesfc797672015-04-07 20:12:50 -0700858static void do_send_signature(char* fn) {
859 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800860 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -0700861
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800862 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800863
Elliott Hughesfc797672015-04-07 20:12:50 -0700864 strcpy(xtn, ".sig");
865
866 int64_t sz;
867 void* data = load_file(fn, &sz);
868 strcpy(xtn, ".img");
869 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800870 fb_queue_download("signature", data, sz);
871 fb_queue_command("signature", "installing signature");
872}
873
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700874static void do_flashall(usb_handle* usb, const char *slot_override, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800875 queue_info_dump();
876
Wink Savilleb98762f2011-04-04 17:54:59 -0700877 fb_queue_query_save("product", cur_product, sizeof(cur_product));
878
Elliott Hughes253c18d2015-03-18 22:47:09 -0700879 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -0700880 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800881
Elliott Hughesfc797672015-04-07 20:12:50 -0700882 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700883 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700884 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700885
886 setup_requirements(reinterpret_cast<char*>(data), sz);
887
888 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700889 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700890 fastboot_buffer buf;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700891 if (load_buf(usb, fname, &buf)) {
892 if (images[i].is_optional)
893 continue;
894 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700895 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700896
897 auto flashall = [&](const std::string &partition) {
898 do_send_signature(fname);
899 if (erase_first && needs_erase(usb, partition.c_str())) {
900 fb_queue_erase(partition.c_str());
901 }
902 flash_buf(partition.c_str(), &buf);
903 };
904 do_for_partitions(usb, images[i].part_name, slot_override, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800905 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800906}
907
908#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800909#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800910
Elliott Hughes45be42e2015-09-02 20:15:48 -0700911static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -0700912{
Patrick Tjin51e8b032015-09-01 08:15:23 -0700913 if (argc <= 2) return 0;
914 skip(2);
915
916 /*
917 * Process unlock_bootloader, we have to load the message file
918 * and send that to the remote device.
919 */
920 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -0700921
922 int64_t sz;
923 void* data = load_file(*argv, &sz);
924 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -0700925 fb_queue_download("unlock_message", data, sz);
926 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
927 skip(1);
928 return 0;
929}
930
Elliott Hughes45be42e2015-09-02 20:15:48 -0700931static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800932{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800933 char command[256];
934 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800935
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800936 command[0] = 0;
937 while(1) {
938 strcat(command,*argv);
939 skip(1);
940 if(argc == 0) break;
941 strcat(command," ");
942 }
943
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800944 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800945 return 0;
946}
947
Colin Crossf8387882012-05-24 17:18:41 -0700948static int64_t parse_num(const char *arg)
949{
950 char *endptr;
951 unsigned long long num;
952
953 num = strtoull(arg, &endptr, 0);
954 if (endptr == arg) {
955 return -1;
956 }
957
958 if (*endptr == 'k' || *endptr == 'K') {
959 if (num >= (-1ULL) / 1024) {
960 return -1;
961 }
962 num *= 1024LL;
963 endptr++;
964 } else if (*endptr == 'm' || *endptr == 'M') {
965 if (num >= (-1ULL) / (1024 * 1024)) {
966 return -1;
967 }
968 num *= 1024LL * 1024LL;
969 endptr++;
970 } else if (*endptr == 'g' || *endptr == 'G') {
971 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
972 return -1;
973 }
974 num *= 1024LL * 1024LL * 1024LL;
975 endptr++;
976 }
977
978 if (*endptr != '\0') {
979 return -1;
980 }
981
982 if (num > INT64_MAX) {
983 return -1;
984 }
985
986 return num;
987}
988
Elliott Hughesfc797672015-04-07 20:12:50 -0700989static void fb_perform_format(usb_handle* usb,
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700990 const char* partition, int skip_if_not_supported,
991 const char* type_override, const char* size_override) {
992 std::string partition_type, partition_size;
993
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700994 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700995 const char* errMsg = nullptr;
996 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700997 int fd;
998
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700999 unsigned int limit = INT_MAX;
1000 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001001 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001002 }
1003 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001004 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001005 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001006
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001007 if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001008 errMsg = "Can't determine partition type.\n";
1009 goto failed;
1010 }
JP Abgrall7e859742014-05-06 15:14:15 -07001011 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001012 if (partition_type != type_override) {
1013 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1014 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001015 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001016 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001017 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001018
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001019 if (!fb_getvar(usb, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001020 errMsg = "Unable to get partition size\n";
1021 goto failed;
1022 }
JP Abgrall7e859742014-05-06 15:14:15 -07001023 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001024 if (partition_size != size_override) {
1025 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1026 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001027 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001028 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001029 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001030
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001031 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001032 if (!gen) {
1033 if (skip_if_not_supported) {
1034 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001035 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001036 return;
1037 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001038 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1039 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001040 return;
1041 }
1042
Elliott Hughes2030bac2015-11-03 08:14:43 -08001043 // Some bootloaders (hammerhead, for example) use implicit hex.
1044 // This code used to use strtol with base 16.
1045 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
1046
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001047 int64_t size;
1048 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1049 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1050 return;
1051 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001052
1053 fd = fileno(tmpfile());
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001054 if (fs_generator_generate(gen, fd, size)) {
1055 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001056 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001057 return;
1058 }
1059
1060 if (load_buf_fd(usb, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001061 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001062 close(fd);
1063 return;
1064 }
1065 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001066 return;
1067
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001068failed:
1069 if (skip_if_not_supported) {
1070 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001071 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001072 }
1073 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
1074}
1075
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001076int main(int argc, char **argv)
1077{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001078 bool wants_wipe = false;
1079 bool wants_reboot = false;
1080 bool wants_reboot_bootloader = false;
1081 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001082 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001083 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001084 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001085 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001086 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001087 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001088
JP Abgrall7b8970c2013-03-07 17:06:41 -08001089 const struct option longopts[] = {
1090 {"base", required_argument, 0, 'b'},
1091 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001092 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001093 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001094 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001095 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001096 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001097 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001098 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001099 {"help", no_argument, 0, 'h'},
1100 {"unbuffered", no_argument, 0, 0},
1101 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001102 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001103 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001104 {"set-active", optional_argument, 0, 'a'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001105 {0, 0, 0, 0}
1106 };
Colin Cross8879f982012-05-22 17:53:34 -07001107
1108 serial = getenv("ANDROID_SERIAL");
1109
1110 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001111 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 -07001112 if (c < 0) {
1113 break;
1114 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001115 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001116 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001117 case 'a':
1118 wants_set_active = true;
1119 if (optarg)
1120 next_active = optarg;
1121 break;
Colin Cross8879f982012-05-22 17:53:34 -07001122 case 'b':
1123 base_addr = strtoul(optarg, 0, 16);
1124 break;
Colin Cross8879f982012-05-22 17:53:34 -07001125 case 'c':
1126 cmdline = optarg;
1127 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001128 case 'h':
1129 usage();
1130 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001131 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001132 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001133 unsigned long val;
1134
1135 val = strtoul(optarg, &endptr, 0);
1136 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1137 die("invalid vendor id '%s'", optarg);
1138 vendor_id = (unsigned short)val;
1139 break;
1140 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001141 case 'k':
1142 kernel_offset = strtoul(optarg, 0, 16);
1143 break;
1144 case 'l':
1145 long_listing = 1;
1146 break;
1147 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001148 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001149 if (!page_size) die("invalid page size");
1150 break;
1151 case 'p':
1152 product = optarg;
1153 break;
1154 case 'r':
1155 ramdisk_offset = strtoul(optarg, 0, 16);
1156 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001157 case 't':
1158 tags_offset = strtoul(optarg, 0, 16);
1159 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001160 case 's':
1161 serial = optarg;
1162 break;
1163 case 'S':
1164 sparse_limit = parse_num(optarg);
1165 if (sparse_limit < 0) {
1166 die("invalid sparse limit");
1167 }
1168 break;
1169 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001170 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001171 break;
1172 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001173 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001174 break;
Colin Cross8879f982012-05-22 17:53:34 -07001175 case '?':
1176 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001177 case 0:
1178 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001179 setvbuf(stdout, nullptr, _IONBF, 0);
1180 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001181 } else if (strcmp("version", longopts[longindex].name) == 0) {
1182 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1183 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001184 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1185 slot_override = std::string(optarg);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001186 }
1187 break;
Colin Cross8879f982012-05-22 17:53:34 -07001188 default:
1189 abort();
1190 }
1191 }
1192
1193 argc -= optind;
1194 argv += optind;
1195
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001196 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001197 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001198 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001199 }
1200
Colin Cross8fb6e062012-07-24 16:36:41 -07001201 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001202 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001203 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001204 return 0;
1205 }
1206
Colin Crossc7b75dc2012-08-29 18:17:06 -07001207 if (argc > 0 && !strcmp(*argv, "help")) {
1208 usage();
1209 return 0;
1210 }
1211
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001212 usb_handle* usb = open_device();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001213 if (slot_override != "")
1214 slot_override = verify_slot(usb, slot_override.c_str());
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001215 if (next_active != "")
1216 next_active = verify_slot(usb, next_active.c_str());
1217
1218 if (wants_set_active) {
1219 if (next_active == "") {
1220 if (slot_override == "") {
1221 wants_set_active = false;
1222 } else {
1223 next_active = slot_override;
1224 }
1225 }
1226 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001227
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001228 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001229 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001230 require(2);
1231 fb_queue_display(argv[1], argv[1]);
1232 skip(2);
1233 } else if(!strcmp(*argv, "erase")) {
1234 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001235
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001236 auto erase = [&](const std::string &partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001237 std::string partition_type;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001238 if (fb_getvar(usb, std::string("partition-type:") + argv[1], &partition_type) &&
1239 fs_get_generator(partition_type) != nullptr) {
1240 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1241 partition_type.c_str());
1242 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001243
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001244 fb_queue_erase(partition.c_str());
1245 };
1246 do_for_partitions(usb, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001247 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001248 } else if(!strncmp(*argv, "format", strlen("format"))) {
1249 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001250 char *type_override = nullptr;
1251 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001252 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001253 /*
1254 * Parsing for: "format[:[type][:[size]]]"
1255 * Some valid things:
1256 * - select ontly the size, and leave default fs type:
1257 * format::0x4000000 userdata
1258 * - default fs type and size:
1259 * format userdata
1260 * format:: userdata
1261 */
1262 overrides = strchr(*argv, ':');
1263 if (overrides) {
1264 overrides++;
1265 size_override = strchr(overrides, ':');
1266 if (size_override) {
1267 size_override[0] = '\0';
1268 size_override++;
1269 }
1270 type_override = overrides;
1271 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001272 if (type_override && !type_override[0]) type_override = nullptr;
1273 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001274
1275 auto format = [&](const std::string &partition) {
1276 if (erase_first && needs_erase(usb, partition.c_str())) {
1277 fb_queue_erase(partition.c_str());
1278 }
1279 fb_perform_format(usb, partition.c_str(), 0, type_override, size_override);
1280 };
1281 do_for_partitions(usb, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001282 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001283 } else if(!strcmp(*argv, "signature")) {
1284 require(2);
1285 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001286 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001287 if (sz != 256) die("signature must be 256 bytes");
1288 fb_queue_download("signature", data, sz);
1289 fb_queue_command("signature", "installing signature");
1290 skip(2);
1291 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001292 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001293 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001294 if (argc > 0) {
1295 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001296 wants_reboot = false;
1297 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001298 skip(1);
1299 }
1300 }
1301 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001302 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001303 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001304 skip(1);
1305 } else if (!strcmp(*argv, "continue")) {
1306 fb_queue_command("continue", "resuming boot");
1307 skip(1);
1308 } else if(!strcmp(*argv, "boot")) {
1309 char *kname = 0;
1310 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001311 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001312 skip(1);
1313 if (argc > 0) {
1314 kname = argv[0];
1315 skip(1);
1316 }
1317 if (argc > 0) {
1318 rname = argv[0];
1319 skip(1);
1320 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001321 if (argc > 0) {
1322 sname = argv[0];
1323 skip(1);
1324 }
1325 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001326 if (data == 0) return 1;
1327 fb_queue_download("boot.img", data, sz);
1328 fb_queue_command("boot", "booting");
1329 } else if(!strcmp(*argv, "flash")) {
1330 char *pname = argv[1];
1331 char *fname = 0;
1332 require(2);
1333 if (argc > 2) {
1334 fname = argv[2];
1335 skip(3);
1336 } else {
1337 fname = find_item(pname, product);
1338 skip(2);
1339 }
1340 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001341
1342 auto flash = [&](const std::string &partition) {
1343 if (erase_first && needs_erase(usb, partition.c_str())) {
1344 fb_queue_erase(partition.c_str());
1345 }
1346 do_flash(usb, partition.c_str(), fname);
1347 };
1348 do_for_partitions(usb, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001349 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001350 char *kname = argv[2];
1351 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001352 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001353 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001354 skip(3);
1355 if (argc > 0) {
1356 rname = argv[0];
1357 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001358 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001359 if (argc > 0) {
1360 sname = argv[0];
1361 skip(1);
1362 }
1363 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001364 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001365 auto flashraw = [&](const std::string &partition) {
1366 fb_queue_flash(partition.c_str(), data, sz);
1367 };
1368 do_for_partitions(usb, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001369 } else if(!strcmp(*argv, "flashall")) {
1370 skip(1);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001371 do_flashall(usb, slot_override.c_str(), erase_first);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001372 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001373 } else if(!strcmp(*argv, "update")) {
1374 if (argc > 1) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001375 do_update(usb, argv[1], slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001376 skip(2);
1377 } else {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001378 do_update(usb, "update.zip", slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001379 skip(1);
1380 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001381 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001382 } else if(!strcmp(*argv, "oem")) {
1383 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001384 } else if(!strcmp(*argv, "flashing")) {
1385 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1386 !strcmp(*(argv+1), "lock") ||
1387 !strcmp(*(argv+1), "unlock_critical") ||
1388 !strcmp(*(argv+1), "lock_critical") ||
1389 !strcmp(*(argv+1), "get_unlock_ability") ||
1390 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1391 !strcmp(*(argv+1), "lock_bootloader"))) {
1392 argc = do_oem_command(argc, argv);
1393 } else
1394 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1395 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001396 } else {
1397 usage();
1398 return 1;
1399 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001400 } else {
1401 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001402 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001403 }
1404 }
1405
1406 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001407 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001408 fb_queue_erase("userdata");
Elliott Hughesfc797672015-04-07 20:12:50 -07001409 fb_perform_format(usb, "userdata", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001410
1411 std::string cache_type;
1412 if (fb_getvar(usb, "partition-type:cache", &cache_type) && !cache_type.empty()) {
1413 fprintf(stderr, "wiping cache...\n");
1414 fb_queue_erase("cache");
1415 fb_perform_format(usb, "cache", 1, nullptr, nullptr);
1416 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001417 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001418 if (wants_set_active) {
1419 fb_set_active(next_active.c_str());
1420 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001421 if (wants_reboot) {
1422 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001423 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001424 } else if (wants_reboot_bootloader) {
1425 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001426 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001427 }
1428
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001429 return fb_execute_queue(usb) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001430}