blob: e2971f25257c9ffed51720cbc1080d4e0e26cdc6 [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>
Colin Crossf8387882012-05-24 17:18:41 -070045
Colin Crossf8387882012-05-24 17:18:41 -070046#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070047#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
Elliott Hughes253c18d2015-03-18 22:47:09 -070049#include "bootimg_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070051#include "fs.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052
Colin Crossf8387882012-05-24 17:18:41 -070053#ifndef O_BINARY
54#define O_BINARY 0
55#endif
56
Rom Lemarchand622810c2013-06-28 09:54:59 -070057#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
58
Wink Savilleb98762f2011-04-04 17:54:59 -070059char cur_product[FB_RESPONSE_SZ + 1];
60
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061static const char *serial = 0;
62static const char *product = 0;
63static const char *cmdline = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070065static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070066static int64_t sparse_limit = -1;
67static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068
Elliott Hughesfc797672015-04-07 20:12:50 -070069static unsigned page_size = 2048;
70static unsigned base_addr = 0x10000000;
71static unsigned kernel_offset = 0x00008000;
72static unsigned ramdisk_offset = 0x01000000;
73static unsigned second_offset = 0x00f00000;
74static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080075
Rom Lemarchand622810c2013-06-28 09:54:59 -070076enum fb_buffer_type {
77 FB_BUFFER,
78 FB_BUFFER_SPARSE,
79};
80
81struct fastboot_buffer {
82 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070083 void* data;
84 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070085};
86
87static struct {
88 char img_name[13];
89 char sig_name[13];
90 char part_name[9];
91 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -070092} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -070093 {"boot.img", "boot.sig", "boot", false},
94 {"recovery.img", "recovery.sig", "recovery", true},
95 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -070096 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -070097};
Brian Swetland2a63bb72009-04-28 16:05:07 -070098
Elliott Hughesfc797672015-04-07 20:12:50 -070099static char* find_item(const char* item, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700101 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102 char path[PATH_MAX + 128];
103
104 if(!strcmp(item,"boot")) {
105 fn = "boot.img";
106 } else if(!strcmp(item,"recovery")) {
107 fn = "recovery.img";
108 } else if(!strcmp(item,"system")) {
109 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700110 } else if(!strcmp(item,"vendor")) {
111 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112 } else if(!strcmp(item,"userdata")) {
113 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700114 } else if(!strcmp(item,"cache")) {
115 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116 } else if(!strcmp(item,"info")) {
117 fn = "android-info.txt";
118 } else {
119 fprintf(stderr,"unknown partition '%s'\n", item);
120 return 0;
121 }
122
123 if(product) {
124 get_my_path(path);
125 sprintf(path + strlen(path),
126 "../../../target/product/%s/%s", product, fn);
127 return strdup(path);
128 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800129
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 dir = getenv("ANDROID_PRODUCT_OUT");
131 if((dir == 0) || (dir[0] == 0)) {
132 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
133 return 0;
134 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800135
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 sprintf(path, "%s/%s", dir, fn);
137 return strdup(path);
138}
139
Elliott Hughesfc797672015-04-07 20:12:50 -0700140static int64_t get_file_size(int fd) {
141 struct stat sb;
142 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700143}
144
Elliott Hughesfc797672015-04-07 20:12:50 -0700145static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800146 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700147 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148
Elliott Hughesfc797672015-04-07 20:12:50 -0700149 *sz = get_file_size(fd);
150 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700151 goto oops;
152 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153
Elliott Hughesfc797672015-04-07 20:12:50 -0700154 data = (char*) malloc(*sz);
155 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156
Elliott Hughesfc797672015-04-07 20:12:50 -0700157 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 close(fd);
159
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 return data;
161
162oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800163 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164 close(fd);
165 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800166 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 return 0;
168}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169
Elliott Hughesfc797672015-04-07 20:12:50 -0700170static void* load_file(const char* fn, int64_t* sz) {
171 int fd = open(fn, O_RDONLY | O_BINARY);
172 if (fd == -1) return nullptr;
173 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700174}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175
Elliott Hughesfc797672015-04-07 20:12:50 -0700176static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700177 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700178 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700179 return -1;
180 }
181
182 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
183 return -1;
184 }
185
Scott Anderson13081c62012-04-06 12:39:30 -0700186 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700188 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
189 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 return 0;
191}
192
Elliott Hughesfc797672015-04-07 20:12:50 -0700193static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800194 return match_fastboot_with_serial(info, serial);
195}
196
Elliott Hughesfc797672015-04-07 20:12:50 -0700197static int list_devices_callback(usb_ifc_info* info) {
198 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700199 const char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700200 if (!info->writable) {
201 serial = "no permissions"; // like "adb devices"
202 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 if (!serial[0]) {
204 serial = "????????????";
205 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700206 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700207 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700208 printf("%s\tfastboot\n", serial);
Stephen Hines04f89532014-11-26 14:54:43 -0800209 } else if (strcmp("", info->device_path) == 0) {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700210 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700211 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700212 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700213 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214 }
215
216 return -1;
217}
218
Elliott Hughesfc797672015-04-07 20:12:50 -0700219static usb_handle* open_device() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 static usb_handle *usb = 0;
221 int announce = 1;
222
223 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800224
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 for(;;) {
226 usb = usb_open(match_fastboot);
227 if(usb) return usb;
228 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800229 announce = 0;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700230 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700232 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 }
234}
235
Elliott Hughesfc797672015-04-07 20:12:50 -0700236static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 // We don't actually open a USB device here,
238 // just getting our callback called so we can
239 // list all the connected devices.
240 usb_open(list_devices_callback);
241}
242
Elliott Hughesfc797672015-04-07 20:12:50 -0700243static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 fprintf(stderr,
245/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
246 "usage: fastboot [ <option> ] <command>\n"
247 "\n"
248 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700249 " update <filename> Reflash device from update.zip.\n"
250 " flashall Flash boot, system, vendor, and --\n"
251 " if found -- recovery.\n"
252 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
253 " flashing lock Locks the device. Prevents flashing.\n"
254 " flashing unlock Unlocks the device. Allows flashing\n"
255 " any partition except\n"
256 " bootloader-related partitions.\n"
257 " flashing lock_critical Prevents flashing bootloader-related\n"
258 " partitions.\n"
259 " flashing unlock_critical Enables flashing bootloader-related\n"
260 " partitions.\n"
261 " flashing get_unlock_ability Queries bootloader to see if the\n"
262 " device is unlocked.\n"
263 " erase <partition> Erase a flash partition.\n"
264 " format[:[<fs type>][:[<size>]] <partition>\n"
265 " Format a flash partition. Can\n"
266 " override the fs type and/or size\n"
267 " the bootloader reports.\n"
268 " getvar <variable> Display a bootloader variable.\n"
269 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
270 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
271 " Create bootimage and flash it.\n"
272 " devices [-l] List all connected devices [with\n"
273 " device paths].\n"
274 " continue Continue with autoboot.\n"
275 " reboot [bootloader] Reboot device [into bootloader].\n"
276 " reboot-bootloader Reboot device into bootloader.\n"
277 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278 "\n"
279 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700280 " -w Erase userdata and cache (and format\n"
281 " if supported by partition type).\n"
282 " -u Do not erase partition before\n"
283 " formatting.\n"
284 " -s <specific device> Specify device serial number\n"
285 " or path to device port.\n"
286 " -p <product> Specify product name.\n"
287 " -c <cmdline> Override kernel commandline.\n"
288 " -i <vendor id> Specify a custom USB vendor id.\n"
289 " -b <base_addr> Specify a custom kernel base\n"
290 " address (default: 0x10000000).\n"
291 " -n <page size> Specify the nand page size\n"
292 " (default: 2048).\n"
293 " -S <size>[K|M|G] Automatically sparse files greater\n"
294 " than 'size'. 0 to disable.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296}
297
Elliott Hughesfc797672015-04-07 20:12:50 -0700298static void* load_bootable_image(const char* kernel, const char* ramdisk,
299 const char* secondstage, int64_t* sz,
300 const char* cmdline) {
301 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 fprintf(stderr, "no image specified\n");
303 return 0;
304 }
305
Elliott Hughesfc797672015-04-07 20:12:50 -0700306 int64_t ksize;
307 void* kdata = load_file(kernel, &ksize);
308 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800309 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310 return 0;
311 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800312
Elliott Hughesfc797672015-04-07 20:12:50 -0700313 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700315 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800316
Elliott Hughesfc797672015-04-07 20:12:50 -0700317 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
319 return 0;
320 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800321
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 *sz = ksize;
323 return kdata;
324 }
325
Elliott Hughesfc797672015-04-07 20:12:50 -0700326 void* rdata = nullptr;
327 int64_t rsize = 0;
328 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700330 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800331 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332 return 0;
333 }
334 }
335
Elliott Hughesfc797672015-04-07 20:12:50 -0700336 void* sdata = nullptr;
337 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200338 if (secondstage) {
339 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700340 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200341 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
342 return 0;
343 }
344 }
345
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700347 int64_t bsize = 0;
348 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800349 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200350 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800351 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700352 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353 fprintf(stderr,"failed to create boot.img\n");
354 return 0;
355 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700356 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
357 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800359
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360 return bdata;
361}
362
Elliott Hughesfc797672015-04-07 20:12:50 -0700363static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364{
Yusuke Sato07447542015-06-25 14:39:19 -0700365 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700366 ZipEntry zip_entry;
367 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700368 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000369 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370 }
371
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700372 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800373
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700374 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700375 if (data == nullptr) {
376 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000377 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378 }
379
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700380 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
381 if (error != 0) {
382 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000384 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000386
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387 return data;
388}
389
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700390#if defined(_WIN32)
391
392// TODO: move this to somewhere it can be shared.
393
394#include <windows.h>
395
396// Windows' tmpfile(3) requires administrator rights because
397// it creates temporary files in the root directory.
398static FILE* win32_tmpfile() {
399 char temp_path[PATH_MAX];
400 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
401 if (nchars == 0 || nchars >= sizeof(temp_path)) {
402 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
403 return nullptr;
404 }
405
406 char filename[PATH_MAX];
407 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
408 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
409 return nullptr;
410 }
411
412 return fopen(filename, "w+bTD");
413}
414
415#define tmpfile win32_tmpfile
416
417#endif
418
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700419static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
420 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700421 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700422 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
423 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700424 return -1;
425 }
426
Yusuke Sato07447542015-06-25 14:39:19 -0700427 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700428 ZipEntry zip_entry;
429 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
430 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000431 return -1;
432 }
433
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700434 int fd = fileno(fp);
435 int error = ExtractEntryToFile(zip, &zip_entry, fd);
436 if (error != 0) {
437 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
438 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700439 }
440
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000441 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700442 return fd;
443}
444
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800445static char *strip(char *s)
446{
447 int n;
448 while(*s && isspace(*s)) s++;
449 n = strlen(s);
450 while(n-- > 0) {
451 if(!isspace(s[n])) break;
452 s[n] = 0;
453 }
454 return s;
455}
456
457#define MAX_OPTIONS 32
458static int setup_requirement_line(char *name)
459{
460 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700461 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 unsigned n, count;
463 char *x;
464 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800465
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466 if (!strncmp(name, "reject ", 7)) {
467 name += 7;
468 invert = 1;
469 } else if (!strncmp(name, "require ", 8)) {
470 name += 8;
471 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700472 } else if (!strncmp(name, "require-for-product:", 20)) {
473 // Get the product and point name past it
474 prod = name + 20;
475 name = strchr(name, ' ');
476 if (!name) return -1;
477 *name = 0;
478 name += 1;
479 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480 }
481
482 x = strchr(name, '=');
483 if (x == 0) return 0;
484 *x = 0;
485 val[0] = x + 1;
486
487 for(count = 1; count < MAX_OPTIONS; count++) {
488 x = strchr(val[count - 1],'|');
489 if (x == 0) break;
490 *x = 0;
491 val[count] = x + 1;
492 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800493
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494 name = strip(name);
495 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800496
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497 name = strip(name);
498 if (name == 0) return -1;
499
Elliott Hughes253c18d2015-03-18 22:47:09 -0700500 const char* var = name;
501 // Work around an unfortunate name mismatch.
502 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503
Elliott Hughes253c18d2015-03-18 22:47:09 -0700504 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 if (out == 0) return -1;
506
507 for(n = 0; n < count; n++) {
508 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700509 if (out[n] == 0) {
510 for(size_t i = 0; i < n; ++i) {
511 free((char*) out[i]);
512 }
513 free(out);
514 return -1;
515 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516 }
517
Elliott Hughes253c18d2015-03-18 22:47:09 -0700518 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519 return 0;
520}
521
Elliott Hughesfc797672015-04-07 20:12:50 -0700522static void setup_requirements(char* data, int64_t sz) {
523 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700525 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526 *s++ = 0;
527 if (setup_requirement_line(data)) {
528 die("out of memory");
529 }
530 data = s;
531 } else {
532 s++;
533 }
534 }
535}
536
Elliott Hughesfc797672015-04-07 20:12:50 -0700537static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800538 fb_queue_notice("--------------------------------------------");
539 fb_queue_display("version-bootloader", "Bootloader Version...");
540 fb_queue_display("version-baseband", "Baseband Version.....");
541 fb_queue_display("serialno", "Serial Number........");
542 fb_queue_notice("--------------------------------------------");
543}
544
Rom Lemarchand622810c2013-06-28 09:54:59 -0700545static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700546{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700547 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700548 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700549 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700550 }
551
Elliott Hughesfc797672015-04-07 20:12:50 -0700552 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700553 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700554 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700555 }
556
Elliott Hughes253c18d2015-03-18 22:47:09 -0700557 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700558 if (!out_s) {
559 die("Failed to allocate sparse file array\n");
560 }
561
562 files = sparse_file_resparse(s, max_size, out_s, files);
563 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700564 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700565 }
566
567 return out_s;
568}
569
570static int64_t get_target_sparse_limit(struct usb_handle *usb)
571{
572 int64_t limit = 0;
573 char response[FB_RESPONSE_SZ + 1];
574 int status = fb_getvar(usb, response, "max-download-size");
575
576 if (!status) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700577 limit = strtoul(response, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700578 if (limit > 0) {
Ying Wangcf86e2f2014-05-15 20:06:40 -0700579 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n",
Colin Crossf8387882012-05-24 17:18:41 -0700580 limit);
581 }
582 }
583
584 return limit;
585}
586
587static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
588{
589 int64_t limit;
590
591 if (sparse_limit == 0) {
592 return 0;
593 } else if (sparse_limit > 0) {
594 limit = sparse_limit;
595 } else {
596 if (target_sparse_limit == -1) {
597 target_sparse_limit = get_target_sparse_limit(usb);
598 }
599 if (target_sparse_limit > 0) {
600 limit = target_sparse_limit;
601 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700602 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700603 }
604 }
605
606 if (size > limit) {
607 return limit;
608 }
609
610 return 0;
611}
612
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700613/* Until we get lazy inode table init working in make_ext4fs, we need to
614 * erase partitions of type ext4 before flashing a filesystem so no stale
615 * inodes are left lying around. Otherwise, e2fsck gets very upset.
616 */
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700617static int needs_erase(usb_handle* usb, const char *part)
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700618{
619 /* The function fb_format_supported() currently returns the value
620 * we want, so just call it.
621 */
Elliott Hughesfc797672015-04-07 20:12:50 -0700622 return fb_format_supported(usb, part, nullptr);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700623}
624
Elliott Hughesfc797672015-04-07 20:12:50 -0700625static int load_buf_fd(usb_handle* usb, int fd, struct fastboot_buffer* buf) {
626 int64_t sz = get_file_size(fd);
627 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000628 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700629 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700630
Elliott Hughesfc797672015-04-07 20:12:50 -0700631 lseek64(fd, 0, SEEK_SET);
632 int64_t limit = get_sparse_limit(usb, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700633 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700634 sparse_file** s = load_sparse_files(fd, limit);
635 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700636 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700637 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700638 buf->type = FB_BUFFER_SPARSE;
639 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700640 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700641 void* data = load_fd(fd, &sz);
642 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700643 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700644 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000645 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700646 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700647
648 return 0;
649}
650
651static int load_buf(usb_handle *usb, const char *fname,
652 struct fastboot_buffer *buf)
653{
654 int fd;
655
656 fd = open(fname, O_RDONLY | O_BINARY);
657 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700658 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700659 }
660
661 return load_buf_fd(usb, fd, buf);
662}
663
664static void flash_buf(const char *pname, struct fastboot_buffer *buf)
665{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700666 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700667
668 switch (buf->type) {
669 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700670 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700671 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700672 int64_t sz = sparse_file_len(*s, true, false);
673 fb_queue_flash_sparse(pname, *s++, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700674 }
675 break;
676 case FB_BUFFER:
677 fb_queue_flash(pname, buf->data, buf->sz);
678 break;
679 default:
680 die("unknown buffer type: %d", buf->type);
681 }
682}
683
Elliott Hughesfc797672015-04-07 20:12:50 -0700684static void do_flash(usb_handle* usb, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700685 struct fastboot_buffer buf;
686
687 if (load_buf(usb, fname, &buf)) {
688 die("cannot load '%s'", fname);
689 }
690 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700691}
692
Elliott Hughesfc797672015-04-07 20:12:50 -0700693static void do_update_signature(ZipArchiveHandle zip, char* fn) {
694 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700695 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700696 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800697 fb_queue_download("signature", data, sz);
698 fb_queue_command("signature", "installing signature");
699}
700
Elliott Hughesfc797672015-04-07 20:12:50 -0700701static void do_update(usb_handle* usb, const char* filename, bool erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800702 queue_info_dump();
703
Wink Savilleb98762f2011-04-04 17:54:59 -0700704 fb_queue_query_save("product", cur_product, sizeof(cur_product));
705
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700706 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700707 int error = OpenArchive(filename, &zip);
708 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100709 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700710 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700711 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800712
Elliott Hughesfc797672015-04-07 20:12:50 -0700713 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700714 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700715 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100716 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700717 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718 }
719
Elliott Hughes253c18d2015-03-18 22:47:09 -0700720 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800721
Elliott Hughesacdbe922015-06-02 21:37:05 -0700722 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700723 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700724 if (fd == -1) {
725 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700726 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700727 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100728 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700729 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700730 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700731 fastboot_buffer buf;
732 int rc = load_buf_fd(usb, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700733 if (rc) die("cannot load %s from flash", images[i].img_name);
734 do_update_signature(zip, images[i].sig_name);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700735 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700736 fb_queue_erase(images[i].part_name);
737 }
738 flash_buf(images[i].part_name, &buf);
739 /* not closing the fd here since the sparse code keeps the fd around
740 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
741 * program exits.
742 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800743 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700744
745 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800746}
747
Elliott Hughesfc797672015-04-07 20:12:50 -0700748static void do_send_signature(char* fn) {
749 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800750 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -0700751
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800752 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800753
Elliott Hughesfc797672015-04-07 20:12:50 -0700754 strcpy(xtn, ".sig");
755
756 int64_t sz;
757 void* data = load_file(fn, &sz);
758 strcpy(xtn, ".img");
759 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800760 fb_queue_download("signature", data, sz);
761 fb_queue_command("signature", "installing signature");
762}
763
Elliott Hughesfc797672015-04-07 20:12:50 -0700764static void do_flashall(usb_handle* usb, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800765 queue_info_dump();
766
Wink Savilleb98762f2011-04-04 17:54:59 -0700767 fb_queue_query_save("product", cur_product, sizeof(cur_product));
768
Elliott Hughes253c18d2015-03-18 22:47:09 -0700769 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -0700770 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800771
Elliott Hughesfc797672015-04-07 20:12:50 -0700772 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700773 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700774 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700775
776 setup_requirements(reinterpret_cast<char*>(data), sz);
777
778 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700779 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700780 fastboot_buffer buf;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700781 if (load_buf(usb, fname, &buf)) {
782 if (images[i].is_optional)
783 continue;
784 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700785 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700786 do_send_signature(fname);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700787 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700788 fb_queue_erase(images[i].part_name);
789 }
790 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800791 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800792}
793
794#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800795#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800796
Elliott Hughesfc797672015-04-07 20:12:50 -0700797static int do_oem_command(int argc, char** argv) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800798 char command[256];
799 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800800
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800801 command[0] = 0;
802 while(1) {
803 strcat(command,*argv);
804 skip(1);
805 if(argc == 0) break;
806 strcat(command," ");
807 }
808
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800809 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800810 return 0;
811}
812
Colin Crossf8387882012-05-24 17:18:41 -0700813static int64_t parse_num(const char *arg)
814{
815 char *endptr;
816 unsigned long long num;
817
818 num = strtoull(arg, &endptr, 0);
819 if (endptr == arg) {
820 return -1;
821 }
822
823 if (*endptr == 'k' || *endptr == 'K') {
824 if (num >= (-1ULL) / 1024) {
825 return -1;
826 }
827 num *= 1024LL;
828 endptr++;
829 } else if (*endptr == 'm' || *endptr == 'M') {
830 if (num >= (-1ULL) / (1024 * 1024)) {
831 return -1;
832 }
833 num *= 1024LL * 1024LL;
834 endptr++;
835 } else if (*endptr == 'g' || *endptr == 'G') {
836 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
837 return -1;
838 }
839 num *= 1024LL * 1024LL * 1024LL;
840 endptr++;
841 }
842
843 if (*endptr != '\0') {
844 return -1;
845 }
846
847 if (num > INT64_MAX) {
848 return -1;
849 }
850
851 return num;
852}
853
Elliott Hughesfc797672015-04-07 20:12:50 -0700854static void fb_perform_format(usb_handle* usb,
855 const char *partition, int skip_if_not_supported,
856 const char *type_override, const char *size_override) {
JP Abgrall7e859742014-05-06 15:14:15 -0700857 char pTypeBuff[FB_RESPONSE_SZ + 1], pSizeBuff[FB_RESPONSE_SZ + 1];
858 char *pType = pTypeBuff;
859 char *pSize = pSizeBuff;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700860 unsigned int limit = INT_MAX;
861 struct fastboot_buffer buf;
Elliott Hughesfc797672015-04-07 20:12:50 -0700862 const char *errMsg = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700863 const struct fs_generator *gen;
864 uint64_t pSz;
865 int status;
866 int fd;
867
868 if (target_sparse_limit > 0 && target_sparse_limit < limit)
869 limit = target_sparse_limit;
870 if (sparse_limit > 0 && sparse_limit < limit)
871 limit = sparse_limit;
872
873 status = fb_getvar(usb, pType, "partition-type:%s", partition);
874 if (status) {
875 errMsg = "Can't determine partition type.\n";
876 goto failed;
877 }
JP Abgrall7e859742014-05-06 15:14:15 -0700878 if (type_override) {
879 if (strcmp(type_override, pType)) {
880 fprintf(stderr,
881 "Warning: %s type is %s, but %s was requested for formating.\n",
882 partition, pType, type_override);
883 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700884 pType = (char *)type_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700885 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700886
887 status = fb_getvar(usb, pSize, "partition-size:%s", partition);
888 if (status) {
889 errMsg = "Unable to get partition size\n";
890 goto failed;
891 }
JP Abgrall7e859742014-05-06 15:14:15 -0700892 if (size_override) {
893 if (strcmp(size_override, pSize)) {
894 fprintf(stderr,
895 "Warning: %s size is %s, but %s was requested for formating.\n",
896 partition, pSize, size_override);
897 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700898 pSize = (char *)size_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700899 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700900
901 gen = fs_get_generator(pType);
902 if (!gen) {
903 if (skip_if_not_supported) {
904 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
905 fprintf(stderr, "File system type %s not supported.\n", pType);
906 return;
907 }
908 fprintf(stderr, "Formatting is not supported for filesystem with type '%s'.\n", pType);
909 return;
910 }
911
Elliott Hughesfc797672015-04-07 20:12:50 -0700912 pSz = strtoll(pSize, (char **)nullptr, 16);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700913
914 fd = fileno(tmpfile());
915 if (fs_generator_generate(gen, fd, pSz)) {
916 close(fd);
917 fprintf(stderr, "Cannot generate image.\n");
918 return;
919 }
920
921 if (load_buf_fd(usb, fd, &buf)) {
922 fprintf(stderr, "Cannot read image.\n");
923 close(fd);
924 return;
925 }
926 flash_buf(partition, &buf);
927
928 return;
929
930
931failed:
932 if (skip_if_not_supported) {
933 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
934 if (errMsg)
935 fprintf(stderr, "%s", errMsg);
936 }
937 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
938}
939
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800940int main(int argc, char **argv)
941{
942 int wants_wipe = 0;
943 int wants_reboot = 0;
944 int wants_reboot_bootloader = 0;
Elliott Hughesfc797672015-04-07 20:12:50 -0700945 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800946 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -0700947 int64_t sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700948 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700949 int c;
Florian Bäuerle27ded482014-11-24 11:29:34 +0100950 int longindex;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800951
JP Abgrall7b8970c2013-03-07 17:06:41 -0800952 const struct option longopts[] = {
953 {"base", required_argument, 0, 'b'},
954 {"kernel_offset", required_argument, 0, 'k'},
955 {"page_size", required_argument, 0, 'n'},
956 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800957 {"tags_offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -0700958 {"help", no_argument, 0, 'h'},
959 {"unbuffered", no_argument, 0, 0},
960 {"version", no_argument, 0, 0},
JP Abgrall7b8970c2013-03-07 17:06:41 -0800961 {0, 0, 0, 0}
962 };
Colin Cross8879f982012-05-22 17:53:34 -0700963
964 serial = getenv("ANDROID_SERIAL");
965
966 while (1) {
Florian Bäuerle27ded482014-11-24 11:29:34 +0100967 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 -0700968 if (c < 0) {
969 break;
970 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800971 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -0700972 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -0700973 case 'b':
974 base_addr = strtoul(optarg, 0, 16);
975 break;
Colin Cross8879f982012-05-22 17:53:34 -0700976 case 'c':
977 cmdline = optarg;
978 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -0800979 case 'h':
980 usage();
981 return 1;
Colin Cross8879f982012-05-22 17:53:34 -0700982 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -0700983 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -0700984 unsigned long val;
985
986 val = strtoul(optarg, &endptr, 0);
987 if (!endptr || *endptr != '\0' || (val & ~0xffff))
988 die("invalid vendor id '%s'", optarg);
989 vendor_id = (unsigned short)val;
990 break;
991 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800992 case 'k':
993 kernel_offset = strtoul(optarg, 0, 16);
994 break;
995 case 'l':
996 long_listing = 1;
997 break;
998 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -0700999 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001000 if (!page_size) die("invalid page size");
1001 break;
1002 case 'p':
1003 product = optarg;
1004 break;
1005 case 'r':
1006 ramdisk_offset = strtoul(optarg, 0, 16);
1007 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001008 case 't':
1009 tags_offset = strtoul(optarg, 0, 16);
1010 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001011 case 's':
1012 serial = optarg;
1013 break;
1014 case 'S':
1015 sparse_limit = parse_num(optarg);
1016 if (sparse_limit < 0) {
1017 die("invalid sparse limit");
1018 }
1019 break;
1020 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001021 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001022 break;
1023 case 'w':
1024 wants_wipe = 1;
1025 break;
Colin Cross8879f982012-05-22 17:53:34 -07001026 case '?':
1027 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001028 case 0:
1029 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001030 setvbuf(stdout, nullptr, _IONBF, 0);
1031 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001032 } else if (strcmp("version", longopts[longindex].name) == 0) {
1033 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1034 return 0;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001035 }
1036 break;
Colin Cross8879f982012-05-22 17:53:34 -07001037 default:
1038 abort();
1039 }
1040 }
1041
1042 argc -= optind;
1043 argv += optind;
1044
1045 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001046 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001047 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001048 }
1049
Colin Cross8fb6e062012-07-24 16:36:41 -07001050 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001051 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001052 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001053 return 0;
1054 }
1055
Colin Crossc7b75dc2012-08-29 18:17:06 -07001056 if (argc > 0 && !strcmp(*argv, "help")) {
1057 usage();
1058 return 0;
1059 }
1060
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001061 usb_handle* usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001062
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001063 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -07001064 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001065 require(2);
1066 fb_queue_display(argv[1], argv[1]);
1067 skip(2);
1068 } else if(!strcmp(*argv, "erase")) {
1069 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001070
Elliott Hughesfc797672015-04-07 20:12:50 -07001071 if (fb_format_supported(usb, argv[1], nullptr)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001072 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
1073 }
1074
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001075 fb_queue_erase(argv[1]);
1076 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001077 } else if(!strncmp(*argv, "format", strlen("format"))) {
1078 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001079 char *type_override = nullptr;
1080 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001081 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001082 /*
1083 * Parsing for: "format[:[type][:[size]]]"
1084 * Some valid things:
1085 * - select ontly the size, and leave default fs type:
1086 * format::0x4000000 userdata
1087 * - default fs type and size:
1088 * format userdata
1089 * format:: userdata
1090 */
1091 overrides = strchr(*argv, ':');
1092 if (overrides) {
1093 overrides++;
1094 size_override = strchr(overrides, ':');
1095 if (size_override) {
1096 size_override[0] = '\0';
1097 size_override++;
1098 }
1099 type_override = overrides;
1100 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001101 if (type_override && !type_override[0]) type_override = nullptr;
1102 if (size_override && !size_override[0]) size_override = nullptr;
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001103 if (erase_first && needs_erase(usb, argv[1])) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001104 fb_queue_erase(argv[1]);
1105 }
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001106 fb_perform_format(usb, argv[1], 0, type_override, size_override);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001107 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001108 } else if(!strcmp(*argv, "signature")) {
1109 require(2);
1110 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001111 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001112 if (sz != 256) die("signature must be 256 bytes");
1113 fb_queue_download("signature", data, sz);
1114 fb_queue_command("signature", "installing signature");
1115 skip(2);
1116 } else if(!strcmp(*argv, "reboot")) {
1117 wants_reboot = 1;
1118 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001119 if (argc > 0) {
1120 if (!strcmp(*argv, "bootloader")) {
1121 wants_reboot = 0;
1122 wants_reboot_bootloader = 1;
1123 skip(1);
1124 }
1125 }
1126 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001127 } else if(!strcmp(*argv, "reboot-bootloader")) {
1128 wants_reboot_bootloader = 1;
1129 skip(1);
1130 } else if (!strcmp(*argv, "continue")) {
1131 fb_queue_command("continue", "resuming boot");
1132 skip(1);
1133 } else if(!strcmp(*argv, "boot")) {
1134 char *kname = 0;
1135 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001136 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001137 skip(1);
1138 if (argc > 0) {
1139 kname = argv[0];
1140 skip(1);
1141 }
1142 if (argc > 0) {
1143 rname = argv[0];
1144 skip(1);
1145 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001146 if (argc > 0) {
1147 sname = argv[0];
1148 skip(1);
1149 }
1150 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001151 if (data == 0) return 1;
1152 fb_queue_download("boot.img", data, sz);
1153 fb_queue_command("boot", "booting");
1154 } else if(!strcmp(*argv, "flash")) {
1155 char *pname = argv[1];
1156 char *fname = 0;
1157 require(2);
1158 if (argc > 2) {
1159 fname = argv[2];
1160 skip(3);
1161 } else {
1162 fname = find_item(pname, product);
1163 skip(2);
1164 }
1165 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001166 if (erase_first && needs_erase(usb, pname)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001167 fb_queue_erase(pname);
1168 }
Colin Crossf8387882012-05-24 17:18:41 -07001169 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001170 } else if(!strcmp(*argv, "flash:raw")) {
1171 char *pname = argv[1];
1172 char *kname = argv[2];
1173 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001174 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001175 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001176 skip(3);
1177 if (argc > 0) {
1178 rname = argv[0];
1179 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001180 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001181 if (argc > 0) {
1182 sname = argv[0];
1183 skip(1);
1184 }
1185 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001186 if (data == 0) die("cannot load bootable image");
1187 fb_queue_flash(pname, data, sz);
1188 } else if(!strcmp(*argv, "flashall")) {
1189 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001190 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001191 wants_reboot = 1;
1192 } else if(!strcmp(*argv, "update")) {
1193 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001194 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001195 skip(2);
1196 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001197 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001198 skip(1);
1199 }
1200 wants_reboot = 1;
1201 } else if(!strcmp(*argv, "oem")) {
1202 argc = do_oem_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001203 } else if(!strcmp(*argv, "flashing") && argc == 2) {
1204 if(!strcmp(*(argv+1), "unlock") || !strcmp(*(argv+1), "lock")
1205 || !strcmp(*(argv+1), "unlock_critical")
1206 || !strcmp(*(argv+1), "lock_critical")
1207 || !strcmp(*(argv+1), "get_unlock_ability")) {
1208 argc = do_oem_command(argc, argv);
1209 } else {
1210 usage();
1211 return 1;
1212 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001213 } else {
1214 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001215 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001216 }
1217 }
1218
1219 if (wants_wipe) {
1220 fb_queue_erase("userdata");
Elliott Hughesfc797672015-04-07 20:12:50 -07001221 fb_perform_format(usb, "userdata", 1, nullptr, nullptr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001222 fb_queue_erase("cache");
Elliott Hughesfc797672015-04-07 20:12:50 -07001223 fb_perform_format(usb, "cache", 1, nullptr, nullptr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001224 }
1225 if (wants_reboot) {
1226 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001227 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001228 } else if (wants_reboot_bootloader) {
1229 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001230 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001231 }
1232
Scott Anderson13081c62012-04-06 12:39:30 -07001233 if (fb_queue_is_empty())
1234 return 0;
1235
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001236 status = fb_execute_queue(usb);
1237 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001238}