blob: 765511f58ae26436a514370deb1324bc5dba0353 [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"
269 " erase <partition> Erase a flash partition.\n"
270 " format[:[<fs type>][:[<size>]] <partition>\n"
271 " Format a flash partition. Can\n"
272 " override the fs type and/or size\n"
273 " the bootloader reports.\n"
274 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700275 " set_active <suffix> Sets the active slot. If slots are\n"
276 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700277 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
278 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
279 " Create bootimage and flash it.\n"
280 " devices [-l] List all connected devices [with\n"
281 " device paths].\n"
282 " continue Continue with autoboot.\n"
283 " reboot [bootloader] Reboot device [into bootloader].\n"
284 " reboot-bootloader Reboot device into bootloader.\n"
285 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286 "\n"
287 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700288 " -w Erase userdata and cache (and format\n"
289 " if supported by partition type).\n"
290 " -u Do not erase partition before\n"
291 " formatting.\n"
292 " -s <specific device> Specify device serial number\n"
293 " or path to device port.\n"
294 " -p <product> Specify product name.\n"
295 " -c <cmdline> Override kernel commandline.\n"
296 " -i <vendor id> Specify a custom USB vendor id.\n"
297 " -b <base_addr> Specify a custom kernel base\n"
298 " address (default: 0x10000000).\n"
299 " -n <page size> Specify the nand page size\n"
300 " (default: 2048).\n"
301 " -S <size>[K|M|G] Automatically sparse files greater\n"
302 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700303 " --slot <suffix> Specify slot suffix to be used if the\n"
304 " device supports slots. This will be\n"
305 " added to all partition names that use\n"
306 " slots. 'all' can be given to refer\n"
307 " to all slots. If this is not given,\n"
308 " slotted partitions will default to\n"
309 " the current active slot.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311}
312
Elliott Hughesfc797672015-04-07 20:12:50 -0700313static void* load_bootable_image(const char* kernel, const char* ramdisk,
314 const char* secondstage, int64_t* sz,
315 const char* cmdline) {
316 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317 fprintf(stderr, "no image specified\n");
318 return 0;
319 }
320
Elliott Hughesfc797672015-04-07 20:12:50 -0700321 int64_t ksize;
322 void* kdata = load_file(kernel, &ksize);
323 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800324 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 return 0;
326 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800327
Elliott Hughesfc797672015-04-07 20:12:50 -0700328 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700330 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800331
Elliott Hughesfc797672015-04-07 20:12:50 -0700332 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
334 return 0;
335 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800336
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337 *sz = ksize;
338 return kdata;
339 }
340
Elliott Hughesfc797672015-04-07 20:12:50 -0700341 void* rdata = nullptr;
342 int64_t rsize = 0;
343 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700345 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800346 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 return 0;
348 }
349 }
350
Elliott Hughesfc797672015-04-07 20:12:50 -0700351 void* sdata = nullptr;
352 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200353 if (secondstage) {
354 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700355 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200356 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
357 return 0;
358 }
359 }
360
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700362 int64_t bsize = 0;
363 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800364 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200365 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800366 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700367 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368 fprintf(stderr,"failed to create boot.img\n");
369 return 0;
370 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700371 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
372 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800373 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800374
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 return bdata;
376}
377
Elliott Hughesfc797672015-04-07 20:12:50 -0700378static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379{
Yusuke Sato07447542015-06-25 14:39:19 -0700380 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700381 ZipEntry zip_entry;
382 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700383 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000384 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 }
386
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700387 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700389 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700390 if (data == nullptr) {
391 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000392 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393 }
394
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700395 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
396 if (error != 0) {
397 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000399 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000401
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 return data;
403}
404
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700405#if defined(_WIN32)
406
407// TODO: move this to somewhere it can be shared.
408
409#include <windows.h>
410
411// Windows' tmpfile(3) requires administrator rights because
412// it creates temporary files in the root directory.
413static FILE* win32_tmpfile() {
414 char temp_path[PATH_MAX];
415 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
416 if (nchars == 0 || nchars >= sizeof(temp_path)) {
417 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
418 return nullptr;
419 }
420
421 char filename[PATH_MAX];
422 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
423 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
424 return nullptr;
425 }
426
427 return fopen(filename, "w+bTD");
428}
429
430#define tmpfile win32_tmpfile
431
432#endif
433
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700434static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
435 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700436 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700437 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
438 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700439 return -1;
440 }
441
Yusuke Sato07447542015-06-25 14:39:19 -0700442 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700443 ZipEntry zip_entry;
444 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
445 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000446 return -1;
447 }
448
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700449 int fd = fileno(fp);
450 int error = ExtractEntryToFile(zip, &zip_entry, fd);
451 if (error != 0) {
452 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
453 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700454 }
455
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000456 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700457 return fd;
458}
459
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460static char *strip(char *s)
461{
462 int n;
463 while(*s && isspace(*s)) s++;
464 n = strlen(s);
465 while(n-- > 0) {
466 if(!isspace(s[n])) break;
467 s[n] = 0;
468 }
469 return s;
470}
471
472#define MAX_OPTIONS 32
473static int setup_requirement_line(char *name)
474{
475 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700476 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 unsigned n, count;
478 char *x;
479 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800480
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481 if (!strncmp(name, "reject ", 7)) {
482 name += 7;
483 invert = 1;
484 } else if (!strncmp(name, "require ", 8)) {
485 name += 8;
486 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700487 } else if (!strncmp(name, "require-for-product:", 20)) {
488 // Get the product and point name past it
489 prod = name + 20;
490 name = strchr(name, ' ');
491 if (!name) return -1;
492 *name = 0;
493 name += 1;
494 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 }
496
497 x = strchr(name, '=');
498 if (x == 0) return 0;
499 *x = 0;
500 val[0] = x + 1;
501
502 for(count = 1; count < MAX_OPTIONS; count++) {
503 x = strchr(val[count - 1],'|');
504 if (x == 0) break;
505 *x = 0;
506 val[count] = x + 1;
507 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800508
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 name = strip(name);
510 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800511
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512 name = strip(name);
513 if (name == 0) return -1;
514
Elliott Hughes253c18d2015-03-18 22:47:09 -0700515 const char* var = name;
516 // Work around an unfortunate name mismatch.
517 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518
Elliott Hughes253c18d2015-03-18 22:47:09 -0700519 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520 if (out == 0) return -1;
521
522 for(n = 0; n < count; n++) {
523 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700524 if (out[n] == 0) {
525 for(size_t i = 0; i < n; ++i) {
526 free((char*) out[i]);
527 }
528 free(out);
529 return -1;
530 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531 }
532
Elliott Hughes253c18d2015-03-18 22:47:09 -0700533 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800534 return 0;
535}
536
Elliott Hughesfc797672015-04-07 20:12:50 -0700537static void setup_requirements(char* data, int64_t sz) {
538 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700540 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800541 *s++ = 0;
542 if (setup_requirement_line(data)) {
543 die("out of memory");
544 }
545 data = s;
546 } else {
547 s++;
548 }
549 }
550}
551
Elliott Hughesfc797672015-04-07 20:12:50 -0700552static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800553 fb_queue_notice("--------------------------------------------");
554 fb_queue_display("version-bootloader", "Bootloader Version...");
555 fb_queue_display("version-baseband", "Baseband Version.....");
556 fb_queue_display("serialno", "Serial Number........");
557 fb_queue_notice("--------------------------------------------");
558}
559
Rom Lemarchand622810c2013-06-28 09:54:59 -0700560static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700561{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700562 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700563 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700564 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700565 }
566
Elliott Hughesfc797672015-04-07 20:12:50 -0700567 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700568 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700569 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700570 }
571
Elliott Hughes253c18d2015-03-18 22:47:09 -0700572 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700573 if (!out_s) {
574 die("Failed to allocate sparse file array\n");
575 }
576
577 files = sparse_file_resparse(s, max_size, out_s, files);
578 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700579 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700580 }
581
582 return out_s;
583}
584
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700585static int64_t get_target_sparse_limit(usb_handle* usb) {
586 std::string max_download_size;
587 if (!fb_getvar(usb, "max-download-size", &max_download_size)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800588 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700589 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700590 }
591
Elliott Hughes77c0e662015-11-02 15:51:12 -0800592 // Some bootloaders (angler, for example) send spurious whitespace too.
593 max_download_size = android::base::Trim(max_download_size);
594
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700595 uint64_t limit;
596 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800597 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700598 return 0;
599 }
600 if (limit > 0) {
601 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
602 }
Colin Crossf8387882012-05-24 17:18:41 -0700603 return limit;
604}
605
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700606static int64_t get_sparse_limit(usb_handle* usb, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700607 int64_t limit;
608
609 if (sparse_limit == 0) {
610 return 0;
611 } else if (sparse_limit > 0) {
612 limit = sparse_limit;
613 } else {
614 if (target_sparse_limit == -1) {
615 target_sparse_limit = get_target_sparse_limit(usb);
616 }
617 if (target_sparse_limit > 0) {
618 limit = target_sparse_limit;
619 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700620 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700621 }
622 }
623
624 if (size > limit) {
625 return limit;
626 }
627
628 return 0;
629}
630
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700631// Until we get lazy inode table init working in make_ext4fs, we need to
632// erase partitions of type ext4 before flashing a filesystem so no stale
633// inodes are left lying around. Otherwise, e2fsck gets very upset.
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800634static bool needs_erase(usb_handle* usb, const char* partition) {
635 std::string partition_type;
636 if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) {
637 return false;
638 }
639 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700640}
641
Elliott Hughesfc797672015-04-07 20:12:50 -0700642static int load_buf_fd(usb_handle* usb, int fd, struct fastboot_buffer* buf) {
643 int64_t sz = get_file_size(fd);
644 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000645 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700646 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700647
Elliott Hughesfc797672015-04-07 20:12:50 -0700648 lseek64(fd, 0, SEEK_SET);
649 int64_t limit = get_sparse_limit(usb, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700650 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700651 sparse_file** s = load_sparse_files(fd, limit);
652 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700653 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700654 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700655 buf->type = FB_BUFFER_SPARSE;
656 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700657 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700658 void* data = load_fd(fd, &sz);
659 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700660 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700661 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000662 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700663 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700664
665 return 0;
666}
667
668static int load_buf(usb_handle *usb, const char *fname,
669 struct fastboot_buffer *buf)
670{
671 int fd;
672
673 fd = open(fname, O_RDONLY | O_BINARY);
674 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700675 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700676 }
677
678 return load_buf_fd(usb, fd, buf);
679}
680
681static void flash_buf(const char *pname, struct fastboot_buffer *buf)
682{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700683 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700684
685 switch (buf->type) {
686 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700687 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700688 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700689 int64_t sz = sparse_file_len(*s, true, false);
690 fb_queue_flash_sparse(pname, *s++, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700691 }
692 break;
693 case FB_BUFFER:
694 fb_queue_flash(pname, buf->data, buf->sz);
695 break;
696 default:
697 die("unknown buffer type: %d", buf->type);
698 }
699}
700
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700701static std::vector<std::string> get_suffixes(usb_handle* usb) {
702 std::vector<std::string> suffixes;
703 std::string suffix_list;
704 if (!fb_getvar(usb, "slot-suffixes", &suffix_list)) {
705 die("Could not get suffixes.\n");
706 }
707 return android::base::Split(suffix_list, ",");
708}
709
710static std::string verify_slot(usb_handle* usb, const char *slot) {
711 if (strcmp(slot, "all") == 0) {
712 return "all";
713 }
714 std::vector<std::string> suffixes = get_suffixes(usb);
715 for (const std::string &suffix : suffixes) {
716 if (suffix == slot)
717 return slot;
718 }
719 fprintf(stderr, "Slot %s does not exist. supported slots are:", slot);
720 for (const std::string &suffix : suffixes) {
721 fprintf(stderr, "%s\n", suffix.c_str());
722 }
723 exit(1);
724}
725
726static void do_for_partition(usb_handle* usb, const char *part, const char *slot, std::function<void(const std::string&)> func, bool force_slot) {
727 std::string partition_slot;
728 std::string current_slot;
729
730 if (!fb_getvar(usb, std::string("partition-slot:")+part, &partition_slot)) {
731 /* If partition-slot is not supported, the answer is no. */
732 partition_slot = "";
733 }
734 if (partition_slot == "1") {
735 if (!slot || slot[0] == 0) {
736 if (!fb_getvar(usb, "current-slot", &current_slot)) {
737 die("Failed to identify current slot.\n");
738 }
739 func(std::string(part) + '-' + current_slot);
740 } else {
741 func(std::string(part) + '-' + slot);
742 }
743 } else {
744 if (force_slot && slot && slot[0]) {
745 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n", part, slot);
746 }
747 func(part);
748 }
749}
750
751/* This function will find the real partition name given a base name, and a slot. If slot is NULL or empty,
752 * it will use the current slot. If slot is "all", it will return a list of all possible partition names.
753 * If force_slot is true, it will fail if a slot is specified, and the given partition does not support slots.
754 */
755static void do_for_partitions(usb_handle* usb, const char *part, const char *slot, std::function<void(const std::string&)> func, bool force_slot) {
756 std::string partition_slot;
757
758 if (slot && strcmp(slot, "all") == 0) {
759 if (!fb_getvar(usb, std::string("partition-slot:") + part, &partition_slot)) {
760 die("Could not check if partition %s has slot.", part);
761 }
762 if (partition_slot == "1") {
763 std::vector<std::string> suffixes = get_suffixes(usb);
764 for (std::string &suffix : suffixes) {
765 do_for_partition(usb, part, suffix.c_str(), func, force_slot);
766 }
767 } else {
768 do_for_partition(usb, part, "", func, force_slot);
769 }
770 } else {
771 do_for_partition(usb, part, slot, func, force_slot);
772 }
773}
774
Elliott Hughesfc797672015-04-07 20:12:50 -0700775static void do_flash(usb_handle* usb, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700776 struct fastboot_buffer buf;
777
778 if (load_buf(usb, fname, &buf)) {
779 die("cannot load '%s'", fname);
780 }
781 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700782}
783
Elliott Hughesfc797672015-04-07 20:12:50 -0700784static void do_update_signature(ZipArchiveHandle zip, char* fn) {
785 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700786 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700787 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800788 fb_queue_download("signature", data, sz);
789 fb_queue_command("signature", "installing signature");
790}
791
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700792static 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 -0800793 queue_info_dump();
794
Wink Savilleb98762f2011-04-04 17:54:59 -0700795 fb_queue_query_save("product", cur_product, sizeof(cur_product));
796
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700797 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700798 int error = OpenArchive(filename, &zip);
799 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100800 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700801 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700802 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800803
Elliott Hughesfc797672015-04-07 20:12:50 -0700804 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700805 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700806 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100807 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700808 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800809 }
810
Elliott Hughes253c18d2015-03-18 22:47:09 -0700811 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800812
Elliott Hughesacdbe922015-06-02 21:37:05 -0700813 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700814 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700815 if (fd == -1) {
816 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700817 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700818 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100819 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700820 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700821 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700822 fastboot_buffer buf;
823 int rc = load_buf_fd(usb, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700824 if (rc) die("cannot load %s from flash", images[i].img_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700825
826 auto update = [&](const std::string &partition) {
827 do_update_signature(zip, images[i].sig_name);
828 if (erase_first && needs_erase(usb, partition.c_str())) {
829 fb_queue_erase(partition.c_str());
830 }
831 flash_buf(partition.c_str(), &buf);
832 /* not closing the fd here since the sparse code keeps the fd around
833 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
834 * program exits.
835 */
836 };
837 do_for_partitions(usb, images[i].part_name, slot_override, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800838 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700839
840 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800841}
842
Elliott Hughesfc797672015-04-07 20:12:50 -0700843static void do_send_signature(char* fn) {
844 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800845 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -0700846
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800847 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800848
Elliott Hughesfc797672015-04-07 20:12:50 -0700849 strcpy(xtn, ".sig");
850
851 int64_t sz;
852 void* data = load_file(fn, &sz);
853 strcpy(xtn, ".img");
854 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800855 fb_queue_download("signature", data, sz);
856 fb_queue_command("signature", "installing signature");
857}
858
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700859static void do_flashall(usb_handle* usb, const char *slot_override, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800860 queue_info_dump();
861
Wink Savilleb98762f2011-04-04 17:54:59 -0700862 fb_queue_query_save("product", cur_product, sizeof(cur_product));
863
Elliott Hughes253c18d2015-03-18 22:47:09 -0700864 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -0700865 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800866
Elliott Hughesfc797672015-04-07 20:12:50 -0700867 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700868 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700869 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700870
871 setup_requirements(reinterpret_cast<char*>(data), sz);
872
873 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700874 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700875 fastboot_buffer buf;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700876 if (load_buf(usb, fname, &buf)) {
877 if (images[i].is_optional)
878 continue;
879 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700880 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700881
882 auto flashall = [&](const std::string &partition) {
883 do_send_signature(fname);
884 if (erase_first && needs_erase(usb, partition.c_str())) {
885 fb_queue_erase(partition.c_str());
886 }
887 flash_buf(partition.c_str(), &buf);
888 };
889 do_for_partitions(usb, images[i].part_name, slot_override, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800890 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800891}
892
893#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800894#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800895
Elliott Hughesfc797672015-04-07 20:12:50 -0700896static int do_oem_command(int argc, char** argv) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800897 char command[256];
898 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800899
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800900 command[0] = 0;
901 while(1) {
902 strcat(command,*argv);
903 skip(1);
904 if(argc == 0) break;
905 strcat(command," ");
906 }
907
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800908 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800909 return 0;
910}
911
Colin Crossf8387882012-05-24 17:18:41 -0700912static int64_t parse_num(const char *arg)
913{
914 char *endptr;
915 unsigned long long num;
916
917 num = strtoull(arg, &endptr, 0);
918 if (endptr == arg) {
919 return -1;
920 }
921
922 if (*endptr == 'k' || *endptr == 'K') {
923 if (num >= (-1ULL) / 1024) {
924 return -1;
925 }
926 num *= 1024LL;
927 endptr++;
928 } else if (*endptr == 'm' || *endptr == 'M') {
929 if (num >= (-1ULL) / (1024 * 1024)) {
930 return -1;
931 }
932 num *= 1024LL * 1024LL;
933 endptr++;
934 } else if (*endptr == 'g' || *endptr == 'G') {
935 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
936 return -1;
937 }
938 num *= 1024LL * 1024LL * 1024LL;
939 endptr++;
940 }
941
942 if (*endptr != '\0') {
943 return -1;
944 }
945
946 if (num > INT64_MAX) {
947 return -1;
948 }
949
950 return num;
951}
952
Elliott Hughesfc797672015-04-07 20:12:50 -0700953static void fb_perform_format(usb_handle* usb,
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700954 const char* partition, int skip_if_not_supported,
955 const char* type_override, const char* size_override) {
956 std::string partition_type, partition_size;
957
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700958 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700959 const char* errMsg = nullptr;
960 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700961 int fd;
962
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700963 unsigned int limit = INT_MAX;
964 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700965 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700966 }
967 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700968 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700969 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700970
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700971 if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700972 errMsg = "Can't determine partition type.\n";
973 goto failed;
974 }
JP Abgrall7e859742014-05-06 15:14:15 -0700975 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700976 if (partition_type != type_override) {
977 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
978 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -0700979 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700980 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700981 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700982
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700983 if (!fb_getvar(usb, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700984 errMsg = "Unable to get partition size\n";
985 goto failed;
986 }
JP Abgrall7e859742014-05-06 15:14:15 -0700987 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700988 if (partition_size != size_override) {
989 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
990 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -0700991 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700992 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700993 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700994
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800995 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700996 if (!gen) {
997 if (skip_if_not_supported) {
998 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700999 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001000 return;
1001 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001002 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1003 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001004 return;
1005 }
1006
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001007 int64_t size;
1008 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1009 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1010 return;
1011 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001012
1013 fd = fileno(tmpfile());
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001014 if (fs_generator_generate(gen, fd, size)) {
1015 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001016 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001017 return;
1018 }
1019
1020 if (load_buf_fd(usb, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001021 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001022 close(fd);
1023 return;
1024 }
1025 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001026 return;
1027
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001028failed:
1029 if (skip_if_not_supported) {
1030 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001031 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001032 }
1033 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
1034}
1035
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001036int main(int argc, char **argv)
1037{
1038 int wants_wipe = 0;
1039 int wants_reboot = 0;
1040 int wants_reboot_bootloader = 0;
Elliott Hughesfc797672015-04-07 20:12:50 -07001041 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001042 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001043 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001044 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001045 std::string slot_override;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001046
JP Abgrall7b8970c2013-03-07 17:06:41 -08001047 const struct option longopts[] = {
1048 {"base", required_argument, 0, 'b'},
1049 {"kernel_offset", required_argument, 0, 'k'},
1050 {"page_size", required_argument, 0, 'n'},
1051 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001052 {"tags_offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001053 {"help", no_argument, 0, 'h'},
1054 {"unbuffered", no_argument, 0, 0},
1055 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001056 {"slot", required_argument, 0, 0},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001057 {0, 0, 0, 0}
1058 };
Colin Cross8879f982012-05-22 17:53:34 -07001059
1060 serial = getenv("ANDROID_SERIAL");
1061
1062 while (1) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001063 int c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:h", longopts, &longindex);
Colin Cross8879f982012-05-22 17:53:34 -07001064 if (c < 0) {
1065 break;
1066 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001067 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001068 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -07001069 case 'b':
1070 base_addr = strtoul(optarg, 0, 16);
1071 break;
Colin Cross8879f982012-05-22 17:53:34 -07001072 case 'c':
1073 cmdline = optarg;
1074 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001075 case 'h':
1076 usage();
1077 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001078 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001079 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001080 unsigned long val;
1081
1082 val = strtoul(optarg, &endptr, 0);
1083 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1084 die("invalid vendor id '%s'", optarg);
1085 vendor_id = (unsigned short)val;
1086 break;
1087 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001088 case 'k':
1089 kernel_offset = strtoul(optarg, 0, 16);
1090 break;
1091 case 'l':
1092 long_listing = 1;
1093 break;
1094 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001095 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001096 if (!page_size) die("invalid page size");
1097 break;
1098 case 'p':
1099 product = optarg;
1100 break;
1101 case 'r':
1102 ramdisk_offset = strtoul(optarg, 0, 16);
1103 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001104 case 't':
1105 tags_offset = strtoul(optarg, 0, 16);
1106 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001107 case 's':
1108 serial = optarg;
1109 break;
1110 case 'S':
1111 sparse_limit = parse_num(optarg);
1112 if (sparse_limit < 0) {
1113 die("invalid sparse limit");
1114 }
1115 break;
1116 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001117 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001118 break;
1119 case 'w':
1120 wants_wipe = 1;
1121 break;
Colin Cross8879f982012-05-22 17:53:34 -07001122 case '?':
1123 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001124 case 0:
1125 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001126 setvbuf(stdout, nullptr, _IONBF, 0);
1127 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001128 } else if (strcmp("version", longopts[longindex].name) == 0) {
1129 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1130 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001131 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1132 slot_override = std::string(optarg);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001133 }
1134 break;
Colin Cross8879f982012-05-22 17:53:34 -07001135 default:
1136 abort();
1137 }
1138 }
1139
1140 argc -= optind;
1141 argv += optind;
1142
1143 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001144 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001145 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001146 }
1147
Colin Cross8fb6e062012-07-24 16:36:41 -07001148 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001149 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001150 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001151 return 0;
1152 }
1153
Colin Crossc7b75dc2012-08-29 18:17:06 -07001154 if (argc > 0 && !strcmp(*argv, "help")) {
1155 usage();
1156 return 0;
1157 }
1158
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001159 usb_handle* usb = open_device();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001160 if (slot_override != "")
1161 slot_override = verify_slot(usb, slot_override.c_str());
Elliott Hughes31dbed72009-10-07 15:38:53 -07001162
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001163 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001164 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001165 require(2);
1166 fb_queue_display(argv[1], argv[1]);
1167 skip(2);
1168 } else if(!strcmp(*argv, "erase")) {
1169 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001170
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001171 auto erase = [&](const std::string &partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001172 std::string partition_type;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001173 if (fb_getvar(usb, std::string("partition-type:") + argv[1], &partition_type) &&
1174 fs_get_generator(partition_type) != nullptr) {
1175 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1176 partition_type.c_str());
1177 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001178
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001179 fb_queue_erase(partition.c_str());
1180 };
1181 do_for_partitions(usb, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001182 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001183 } else if(!strncmp(*argv, "format", strlen("format"))) {
1184 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001185 char *type_override = nullptr;
1186 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001187 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001188 /*
1189 * Parsing for: "format[:[type][:[size]]]"
1190 * Some valid things:
1191 * - select ontly the size, and leave default fs type:
1192 * format::0x4000000 userdata
1193 * - default fs type and size:
1194 * format userdata
1195 * format:: userdata
1196 */
1197 overrides = strchr(*argv, ':');
1198 if (overrides) {
1199 overrides++;
1200 size_override = strchr(overrides, ':');
1201 if (size_override) {
1202 size_override[0] = '\0';
1203 size_override++;
1204 }
1205 type_override = overrides;
1206 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001207 if (type_override && !type_override[0]) type_override = nullptr;
1208 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001209
1210 auto format = [&](const std::string &partition) {
1211 if (erase_first && needs_erase(usb, partition.c_str())) {
1212 fb_queue_erase(partition.c_str());
1213 }
1214 fb_perform_format(usb, partition.c_str(), 0, type_override, size_override);
1215 };
1216 do_for_partitions(usb, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001217 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001218 } else if(!strcmp(*argv, "signature")) {
1219 require(2);
1220 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001221 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001222 if (sz != 256) die("signature must be 256 bytes");
1223 fb_queue_download("signature", data, sz);
1224 fb_queue_command("signature", "installing signature");
1225 skip(2);
1226 } else if(!strcmp(*argv, "reboot")) {
1227 wants_reboot = 1;
1228 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001229 if (argc > 0) {
1230 if (!strcmp(*argv, "bootloader")) {
1231 wants_reboot = 0;
1232 wants_reboot_bootloader = 1;
1233 skip(1);
1234 }
1235 }
1236 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001237 } else if(!strcmp(*argv, "reboot-bootloader")) {
1238 wants_reboot_bootloader = 1;
1239 skip(1);
1240 } else if (!strcmp(*argv, "continue")) {
1241 fb_queue_command("continue", "resuming boot");
1242 skip(1);
1243 } else if(!strcmp(*argv, "boot")) {
1244 char *kname = 0;
1245 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001246 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001247 skip(1);
1248 if (argc > 0) {
1249 kname = argv[0];
1250 skip(1);
1251 }
1252 if (argc > 0) {
1253 rname = argv[0];
1254 skip(1);
1255 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001256 if (argc > 0) {
1257 sname = argv[0];
1258 skip(1);
1259 }
1260 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001261 if (data == 0) return 1;
1262 fb_queue_download("boot.img", data, sz);
1263 fb_queue_command("boot", "booting");
1264 } else if(!strcmp(*argv, "flash")) {
1265 char *pname = argv[1];
1266 char *fname = 0;
1267 require(2);
1268 if (argc > 2) {
1269 fname = argv[2];
1270 skip(3);
1271 } else {
1272 fname = find_item(pname, product);
1273 skip(2);
1274 }
1275 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001276
1277 auto flash = [&](const std::string &partition) {
1278 if (erase_first && needs_erase(usb, partition.c_str())) {
1279 fb_queue_erase(partition.c_str());
1280 }
1281 do_flash(usb, partition.c_str(), fname);
1282 };
1283 do_for_partitions(usb, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001284 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001285 char *kname = argv[2];
1286 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001287 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001288 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001289 skip(3);
1290 if (argc > 0) {
1291 rname = argv[0];
1292 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001293 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001294 if (argc > 0) {
1295 sname = argv[0];
1296 skip(1);
1297 }
1298 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001299 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001300 auto flashraw = [&](const std::string &partition) {
1301 fb_queue_flash(partition.c_str(), data, sz);
1302 };
1303 do_for_partitions(usb, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001304 } else if(!strcmp(*argv, "flashall")) {
1305 skip(1);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001306 do_flashall(usb, slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001307 wants_reboot = 1;
1308 } else if(!strcmp(*argv, "update")) {
1309 if (argc > 1) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001310 do_update(usb, argv[1], slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001311 skip(2);
1312 } else {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001313 do_update(usb, "update.zip", slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001314 skip(1);
1315 }
1316 wants_reboot = 1;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001317 } else if(!strcmp(*argv, "set_active")) {
1318 require(2);
1319 fb_set_active(argv[1]);
1320 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001321 } else if(!strcmp(*argv, "oem")) {
1322 argc = do_oem_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001323 } else if(!strcmp(*argv, "flashing") && argc == 2) {
1324 if(!strcmp(*(argv+1), "unlock") || !strcmp(*(argv+1), "lock")
1325 || !strcmp(*(argv+1), "unlock_critical")
1326 || !strcmp(*(argv+1), "lock_critical")
1327 || !strcmp(*(argv+1), "get_unlock_ability")) {
1328 argc = do_oem_command(argc, argv);
1329 } else {
1330 usage();
1331 return 1;
1332 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001333 } else {
1334 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001335 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001336 }
1337 }
1338
1339 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001340 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001341 fb_queue_erase("userdata");
Elliott Hughesfc797672015-04-07 20:12:50 -07001342 fb_perform_format(usb, "userdata", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001343
1344 std::string cache_type;
1345 if (fb_getvar(usb, "partition-type:cache", &cache_type) && !cache_type.empty()) {
1346 fprintf(stderr, "wiping cache...\n");
1347 fb_queue_erase("cache");
1348 fb_perform_format(usb, "cache", 1, nullptr, nullptr);
1349 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001350 }
1351 if (wants_reboot) {
1352 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001353 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001354 } else if (wants_reboot_bootloader) {
1355 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001356 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001357 }
1358
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001359 return fb_execute_queue(usb) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001360}