blob: b964a36c912836d2d7e18347344937ee8e855f21 [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>
37#include <stdbool.h>
38#include <stdint.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070044#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070045#include <unistd.h>
Colin Crossf8387882012-05-24 17:18:41 -070046
Colin Crossf8387882012-05-24 17:18:41 -070047#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070048#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
Elliott Hughes253c18d2015-03-18 22:47:09 -070050#include "bootimg_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070052#include "fs.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
Colin Crossf8387882012-05-24 17:18:41 -070054#ifndef O_BINARY
55#define O_BINARY 0
56#endif
57
Rom Lemarchand622810c2013-06-28 09:54:59 -070058#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
59
Wink Savilleb98762f2011-04-04 17:54:59 -070060char cur_product[FB_RESPONSE_SZ + 1];
61
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062static const char *serial = 0;
63static const char *product = 0;
64static const char *cmdline = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070066static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070067static int64_t sparse_limit = -1;
68static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
JP Abgrall7b8970c2013-03-07 17:06:41 -080070unsigned page_size = 2048;
71unsigned base_addr = 0x10000000;
72unsigned kernel_offset = 0x00008000;
73unsigned ramdisk_offset = 0x01000000;
74unsigned second_offset = 0x00f00000;
75unsigned tags_offset = 0x00000100;
76
Rom Lemarchand622810c2013-06-28 09:54:59 -070077enum fb_buffer_type {
78 FB_BUFFER,
79 FB_BUFFER_SPARSE,
80};
81
82struct fastboot_buffer {
83 enum fb_buffer_type type;
84 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +000085 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070086};
87
88static struct {
89 char img_name[13];
90 char sig_name[13];
91 char part_name[9];
92 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -070093} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -070094 {"boot.img", "boot.sig", "boot", false},
95 {"recovery.img", "recovery.sig", "recovery", true},
96 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -070097 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -070098};
Brian Swetland2a63bb72009-04-28 16:05:07 -070099
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100char *find_item(const char *item, const char *product)
101{
102 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700103 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 char path[PATH_MAX + 128];
105
106 if(!strcmp(item,"boot")) {
107 fn = "boot.img";
108 } else if(!strcmp(item,"recovery")) {
109 fn = "recovery.img";
110 } else if(!strcmp(item,"system")) {
111 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700112 } else if(!strcmp(item,"vendor")) {
113 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114 } else if(!strcmp(item,"userdata")) {
115 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700116 } else if(!strcmp(item,"cache")) {
117 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118 } else if(!strcmp(item,"info")) {
119 fn = "android-info.txt";
120 } else {
121 fprintf(stderr,"unknown partition '%s'\n", item);
122 return 0;
123 }
124
125 if(product) {
126 get_my_path(path);
127 sprintf(path + strlen(path),
128 "../../../target/product/%s/%s", product, fn);
129 return strdup(path);
130 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800131
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 dir = getenv("ANDROID_PRODUCT_OUT");
133 if((dir == 0) || (dir[0] == 0)) {
134 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
135 return 0;
136 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800137
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 sprintf(path, "%s/%s", dir, fn);
139 return strdup(path);
140}
141
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000142static int64_t file_size(int fd)
Colin Crossf8387882012-05-24 17:18:41 -0700143{
Rom Lemarchand622810c2013-06-28 09:54:59 -0700144 struct stat st;
145 int ret;
Colin Crossf8387882012-05-24 17:18:41 -0700146
Rom Lemarchand622810c2013-06-28 09:54:59 -0700147 ret = fstat(fd, &st);
Colin Crossf8387882012-05-24 17:18:41 -0700148
Rom Lemarchand622810c2013-06-28 09:54:59 -0700149 return ret ? -1 : st.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700150}
151
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000152static void *load_fd(int fd, unsigned *_sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000154 char *data;
155 int sz;
Matt Gumbel64ba2582011-12-08 11:59:38 -0800156 int errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000158 data = 0;
159
160 sz = file_size(fd);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700161 if (sz < 0) {
162 goto oops;
163 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164
165 data = (char*) malloc(sz);
166 if(data == 0) goto oops;
167
168 if(read(fd, data, sz) != sz) goto oops;
169 close(fd);
170
171 if(_sz) *_sz = sz;
172 return data;
173
174oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800175 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 close(fd);
177 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800178 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179 return 0;
180}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000182static void *load_file(const char *fn, unsigned *_sz)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700183{
184 int fd;
185
186 fd = open(fn, O_RDONLY | O_BINARY);
187 if(fd < 0) return 0;
188
189 return load_fd(fd, _sz);
190}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
JP Abgralla032ded2012-06-06 11:53:33 -0700192int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
193{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400195 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800196 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700197 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800198 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700199 (info->dev_vendor != 0x0fce) && // Sony Ericsson
200 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400201 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800202 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800203 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800204 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800205 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400206 (info->dev_vendor != 0x0bb4)) // HTC
207 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 if(info->ifc_class != 0xff) return -1;
209 if(info->ifc_subclass != 0x42) return -1;
210 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700211 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700213 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
214 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215 return 0;
216}
217
JP Abgrall7b8970c2013-03-07 17:06:41 -0800218int match_fastboot(usb_ifc_info *info)
219{
220 return match_fastboot_with_serial(info, serial);
221}
222
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223int list_devices_callback(usb_ifc_info *info)
224{
JP Abgralla032ded2012-06-06 11:53:33 -0700225 if (match_fastboot_with_serial(info, NULL) == 0) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700226 const char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700227 if (!info->writable) {
228 serial = "no permissions"; // like "adb devices"
229 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230 if (!serial[0]) {
231 serial = "????????????";
232 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700233 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700234 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700235 printf("%s\tfastboot\n", serial);
Stephen Hines04f89532014-11-26 14:54:43 -0800236 } else if (strcmp("", info->device_path) == 0) {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700237 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700238 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700239 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700240 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241 }
242
243 return -1;
244}
245
246usb_handle *open_device(void)
247{
248 static usb_handle *usb = 0;
249 int announce = 1;
250
251 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800252
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 for(;;) {
254 usb = usb_open(match_fastboot);
255 if(usb) return usb;
256 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800257 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258 fprintf(stderr,"< waiting for device >\n");
259 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700260 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261 }
262}
263
264void list_devices(void) {
265 // We don't actually open a USB device here,
266 // just getting our callback called so we can
267 // list all the connected devices.
268 usb_open(list_devices_callback);
269}
270
271void usage(void)
272{
273 fprintf(stderr,
274/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
275 "usage: fastboot [ <option> ] <command>\n"
276 "\n"
277 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700278 " update <filename> Reflash device from update.zip.\n"
279 " flashall Flash boot, system, vendor, and --\n"
280 " if found -- recovery.\n"
281 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
282 " flashing lock Locks the device. Prevents flashing.\n"
283 " flashing unlock Unlocks the device. Allows flashing\n"
284 " any partition except\n"
285 " bootloader-related partitions.\n"
286 " flashing lock_critical Prevents flashing bootloader-related\n"
287 " partitions.\n"
288 " flashing unlock_critical Enables flashing bootloader-related\n"
289 " partitions.\n"
290 " flashing get_unlock_ability Queries bootloader to see if the\n"
291 " device is unlocked.\n"
292 " erase <partition> Erase a flash partition.\n"
293 " format[:[<fs type>][:[<size>]] <partition>\n"
294 " Format a flash partition. Can\n"
295 " override the fs type and/or size\n"
296 " the bootloader reports.\n"
297 " getvar <variable> Display a bootloader variable.\n"
298 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
299 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
300 " Create bootimage and flash it.\n"
301 " devices [-l] List all connected devices [with\n"
302 " device paths].\n"
303 " continue Continue with autoboot.\n"
304 " reboot [bootloader] Reboot device [into bootloader].\n"
305 " reboot-bootloader Reboot device into bootloader.\n"
306 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 "\n"
308 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700309 " -w Erase userdata and cache (and format\n"
310 " if supported by partition type).\n"
311 " -u Do not erase partition before\n"
312 " formatting.\n"
313 " -s <specific device> Specify device serial number\n"
314 " or path to device port.\n"
315 " -p <product> Specify product name.\n"
316 " -c <cmdline> Override kernel commandline.\n"
317 " -i <vendor id> Specify a custom USB vendor id.\n"
318 " -b <base_addr> Specify a custom kernel base\n"
319 " address (default: 0x10000000).\n"
320 " -n <page size> Specify the nand page size\n"
321 " (default: 2048).\n"
322 " -S <size>[K|M|G] Automatically sparse files greater\n"
323 " than 'size'. 0 to disable.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325}
326
JP Abgrall7b8970c2013-03-07 17:06:41 -0800327void *load_bootable_image(const char *kernel, const char *ramdisk,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200328 const char *secondstage, unsigned *sz,
329 const char *cmdline)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330{
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200331 void *kdata = 0, *rdata = 0, *sdata = 0;
332 unsigned ksize = 0, rsize = 0, ssize = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 void *bdata;
334 unsigned bsize;
335
336 if(kernel == 0) {
337 fprintf(stderr, "no image specified\n");
338 return 0;
339 }
340
341 kdata = load_file(kernel, &ksize);
342 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800343 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 return 0;
345 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800346
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 /* is this actually a boot image? */
348 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
349 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800350
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351 if(ramdisk) {
352 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
353 return 0;
354 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800355
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356 *sz = ksize;
357 return kdata;
358 }
359
360 if(ramdisk) {
361 rdata = load_file(ramdisk, &rsize);
362 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800363 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364 return 0;
365 }
366 }
367
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200368 if (secondstage) {
369 sdata = load_file(secondstage, &ssize);
370 if(sdata == 0) {
371 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
372 return 0;
373 }
374 }
375
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376 fprintf(stderr,"creating boot image...\n");
JP Abgrall7b8970c2013-03-07 17:06:41 -0800377 bdata = mkbootimg(kdata, ksize, kernel_offset,
378 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200379 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800380 page_size, base_addr, tags_offset, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381 if(bdata == 0) {
382 fprintf(stderr,"failed to create boot.img\n");
383 return 0;
384 }
385 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
386 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
387 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800388
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389 return bdata;
390}
391
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700392static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, unsigned* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393{
Yusuke Sato07447542015-06-25 14:39:19 -0700394 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700395 ZipEntry zip_entry;
396 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700397 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000398 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399 }
400
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700401 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700403 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
404 if (data == NULL) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700405 fprintf(stderr, "failed to allocate %u bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000406 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407 }
408
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700409 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
410 if (error != 0) {
411 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000413 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000415
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416 return data;
417}
418
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700419#if defined(_WIN32)
420
421// TODO: move this to somewhere it can be shared.
422
423#include <windows.h>
424
425// Windows' tmpfile(3) requires administrator rights because
426// it creates temporary files in the root directory.
427static FILE* win32_tmpfile() {
428 char temp_path[PATH_MAX];
429 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
430 if (nchars == 0 || nchars >= sizeof(temp_path)) {
431 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
432 return nullptr;
433 }
434
435 char filename[PATH_MAX];
436 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
437 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
438 return nullptr;
439 }
440
441 return fopen(filename, "w+bTD");
442}
443
444#define tmpfile win32_tmpfile
445
446#endif
447
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700448static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
449 FILE* fp = tmpfile();
450 if (fp == NULL) {
451 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
452 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700453 return -1;
454 }
455
Yusuke Sato07447542015-06-25 14:39:19 -0700456 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700457 ZipEntry zip_entry;
458 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
459 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000460 return -1;
461 }
462
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700463 int fd = fileno(fp);
464 int error = ExtractEntryToFile(zip, &zip_entry, fd);
465 if (error != 0) {
466 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
467 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700468 }
469
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000470 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700471 return fd;
472}
473
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474static char *strip(char *s)
475{
476 int n;
477 while(*s && isspace(*s)) s++;
478 n = strlen(s);
479 while(n-- > 0) {
480 if(!isspace(s[n])) break;
481 s[n] = 0;
482 }
483 return s;
484}
485
486#define MAX_OPTIONS 32
487static int setup_requirement_line(char *name)
488{
489 char *val[MAX_OPTIONS];
Wink Savilleb98762f2011-04-04 17:54:59 -0700490 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491 unsigned n, count;
492 char *x;
493 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800494
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 if (!strncmp(name, "reject ", 7)) {
496 name += 7;
497 invert = 1;
498 } else if (!strncmp(name, "require ", 8)) {
499 name += 8;
500 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700501 } else if (!strncmp(name, "require-for-product:", 20)) {
502 // Get the product and point name past it
503 prod = name + 20;
504 name = strchr(name, ' ');
505 if (!name) return -1;
506 *name = 0;
507 name += 1;
508 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 }
510
511 x = strchr(name, '=');
512 if (x == 0) return 0;
513 *x = 0;
514 val[0] = x + 1;
515
516 for(count = 1; count < MAX_OPTIONS; count++) {
517 x = strchr(val[count - 1],'|');
518 if (x == 0) break;
519 *x = 0;
520 val[count] = x + 1;
521 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800522
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523 name = strip(name);
524 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800525
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526 name = strip(name);
527 if (name == 0) return -1;
528
Elliott Hughes253c18d2015-03-18 22:47:09 -0700529 const char* var = name;
530 // Work around an unfortunate name mismatch.
531 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532
Elliott Hughes253c18d2015-03-18 22:47:09 -0700533 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800534 if (out == 0) return -1;
535
536 for(n = 0; n < count; n++) {
537 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700538 if (out[n] == 0) {
539 for(size_t i = 0; i < n; ++i) {
540 free((char*) out[i]);
541 }
542 free(out);
543 return -1;
544 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800545 }
546
Elliott Hughes253c18d2015-03-18 22:47:09 -0700547 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800548 return 0;
549}
550
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000551static void setup_requirements(char *data, unsigned sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800552{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000553 char *s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800554
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000555 s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800556 while (sz-- > 0) {
557 if(*s == '\n') {
558 *s++ = 0;
559 if (setup_requirement_line(data)) {
560 die("out of memory");
561 }
562 data = s;
563 } else {
564 s++;
565 }
566 }
567}
568
569void queue_info_dump(void)
570{
571 fb_queue_notice("--------------------------------------------");
572 fb_queue_display("version-bootloader", "Bootloader Version...");
573 fb_queue_display("version-baseband", "Baseband Version.....");
574 fb_queue_display("serialno", "Serial Number........");
575 fb_queue_notice("--------------------------------------------");
576}
577
Rom Lemarchand622810c2013-06-28 09:54:59 -0700578static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700579{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700580 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700581 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700582 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700583 }
584
Elliott Hughes253c18d2015-03-18 22:47:09 -0700585 int files = sparse_file_resparse(s, max_size, NULL, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700586 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700587 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700588 }
589
Elliott Hughes253c18d2015-03-18 22:47:09 -0700590 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700591 if (!out_s) {
592 die("Failed to allocate sparse file array\n");
593 }
594
595 files = sparse_file_resparse(s, max_size, out_s, files);
596 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700597 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700598 }
599
600 return out_s;
601}
602
603static int64_t get_target_sparse_limit(struct usb_handle *usb)
604{
605 int64_t limit = 0;
606 char response[FB_RESPONSE_SZ + 1];
607 int status = fb_getvar(usb, response, "max-download-size");
608
609 if (!status) {
610 limit = strtoul(response, NULL, 0);
611 if (limit > 0) {
Ying Wangcf86e2f2014-05-15 20:06:40 -0700612 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n",
Colin Crossf8387882012-05-24 17:18:41 -0700613 limit);
614 }
615 }
616
617 return limit;
618}
619
620static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
621{
622 int64_t limit;
623
624 if (sparse_limit == 0) {
625 return 0;
626 } else if (sparse_limit > 0) {
627 limit = sparse_limit;
628 } else {
629 if (target_sparse_limit == -1) {
630 target_sparse_limit = get_target_sparse_limit(usb);
631 }
632 if (target_sparse_limit > 0) {
633 limit = target_sparse_limit;
634 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700635 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700636 }
637 }
638
639 if (size > limit) {
640 return limit;
641 }
642
643 return 0;
644}
645
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700646/* Until we get lazy inode table init working in make_ext4fs, we need to
647 * erase partitions of type ext4 before flashing a filesystem so no stale
648 * inodes are left lying around. Otherwise, e2fsck gets very upset.
649 */
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700650static int needs_erase(usb_handle* usb, const char *part)
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700651{
652 /* The function fb_format_supported() currently returns the value
653 * we want, so just call it.
654 */
JP Abgrall7e859742014-05-06 15:14:15 -0700655 return fb_format_supported(usb, part, NULL);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700656}
657
Rom Lemarchand622810c2013-06-28 09:54:59 -0700658static int load_buf_fd(usb_handle *usb, int fd,
659 struct fastboot_buffer *buf)
Colin Crossf8387882012-05-24 17:18:41 -0700660{
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700661 int64_t sz64;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000662 void *data;
663 int64_t limit;
Colin Crossf8387882012-05-24 17:18:41 -0700664
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000665
666 sz64 = file_size(fd);
667 if (sz64 < 0) {
668 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700669 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700670
671 lseek(fd, 0, SEEK_SET);
Colin Crossf8387882012-05-24 17:18:41 -0700672 limit = get_sparse_limit(usb, sz64);
673 if (limit) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700674 struct sparse_file **s = load_sparse_files(fd, limit);
Colin Crossf8387882012-05-24 17:18:41 -0700675 if (s == NULL) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700676 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700677 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700678 buf->type = FB_BUFFER_SPARSE;
679 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700680 } else {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000681 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700682 data = load_fd(fd, &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000683 if (data == 0) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700684 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700685 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000686 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700687 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700688
689 return 0;
690}
691
692static int load_buf(usb_handle *usb, const char *fname,
693 struct fastboot_buffer *buf)
694{
695 int fd;
696
697 fd = open(fname, O_RDONLY | O_BINARY);
698 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700699 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700700 }
701
702 return load_buf_fd(usb, fd, buf);
703}
704
705static void flash_buf(const char *pname, struct fastboot_buffer *buf)
706{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700707 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700708
709 switch (buf->type) {
710 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700711 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700712 while (*s) {
713 int64_t sz64 = sparse_file_len(*s, true, false);
714 fb_queue_flash_sparse(pname, *s++, sz64);
715 }
716 break;
717 case FB_BUFFER:
718 fb_queue_flash(pname, buf->data, buf->sz);
719 break;
720 default:
721 die("unknown buffer type: %d", buf->type);
722 }
723}
724
725void do_flash(usb_handle *usb, const char *pname, const char *fname)
726{
727 struct fastboot_buffer buf;
728
729 if (load_buf(usb, fname, &buf)) {
730 die("cannot load '%s'", fname);
731 }
732 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700733}
734
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700735void do_update_signature(ZipArchiveHandle zip, char *fn)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800736{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000737 unsigned sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700738 void* data = unzip_file(zip, fn, &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000739 if (data == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800740 fb_queue_download("signature", data, sz);
741 fb_queue_command("signature", "installing signature");
742}
743
Elliott Hughes253c18d2015-03-18 22:47:09 -0700744void do_update(usb_handle *usb, const char *filename, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800745{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800746 queue_info_dump();
747
Wink Savilleb98762f2011-04-04 17:54:59 -0700748 fb_queue_query_save("product", cur_product, sizeof(cur_product));
749
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700750 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700751 int error = OpenArchive(filename, &zip);
752 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100753 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700754 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700755 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800756
Elliott Hughes253c18d2015-03-18 22:47:09 -0700757 unsigned sz;
758 void* data = unzip_file(zip, "android-info.txt", &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000759 if (data == 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100760 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700761 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800762 }
763
Elliott Hughes253c18d2015-03-18 22:47:09 -0700764 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800765
Elliott Hughesacdbe922015-06-02 21:37:05 -0700766 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700767 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700768 if (fd == -1) {
769 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700770 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700771 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100772 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700773 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700774 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700775 fastboot_buffer buf;
776 int rc = load_buf_fd(usb, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700777 if (rc) die("cannot load %s from flash", images[i].img_name);
778 do_update_signature(zip, images[i].sig_name);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700779 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700780 fb_queue_erase(images[i].part_name);
781 }
782 flash_buf(images[i].part_name, &buf);
783 /* not closing the fd here since the sparse code keeps the fd around
784 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
785 * program exits.
786 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800787 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700788
789 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800790}
791
792void do_send_signature(char *fn)
793{
794 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000795 unsigned sz;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800796 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800797
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800798 xtn = strrchr(fn, '.');
799 if (!xtn) return;
800 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800801
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800802 strcpy(xtn,".sig");
803 data = load_file(fn, &sz);
804 strcpy(xtn,".img");
805 if (data == 0) return;
806 fb_queue_download("signature", data, sz);
807 fb_queue_command("signature", "installing signature");
808}
809
Rom Lemarchand622810c2013-06-28 09:54:59 -0700810void do_flashall(usb_handle *usb, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800811{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800812 queue_info_dump();
813
Wink Savilleb98762f2011-04-04 17:54:59 -0700814 fb_queue_query_save("product", cur_product, sizeof(cur_product));
815
Elliott Hughes253c18d2015-03-18 22:47:09 -0700816 char* fname = find_item("info", product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800817 if (fname == 0) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800818
Elliott Hughes253c18d2015-03-18 22:47:09 -0700819 unsigned sz;
820 void* data = load_file(fname, &sz);
821 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
822
823 setup_requirements(reinterpret_cast<char*>(data), sz);
824
825 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700826 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700827 fastboot_buffer buf;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700828 if (load_buf(usb, fname, &buf)) {
829 if (images[i].is_optional)
830 continue;
831 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700832 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700833 do_send_signature(fname);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700834 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700835 fb_queue_erase(images[i].part_name);
836 }
837 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800838 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800839}
840
841#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800842#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800843
844int do_oem_command(int argc, char **argv)
845{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800846 char command[256];
847 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800848
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800849 command[0] = 0;
850 while(1) {
851 strcat(command,*argv);
852 skip(1);
853 if(argc == 0) break;
854 strcat(command," ");
855 }
856
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800857 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800858 return 0;
859}
860
Colin Crossf8387882012-05-24 17:18:41 -0700861static int64_t parse_num(const char *arg)
862{
863 char *endptr;
864 unsigned long long num;
865
866 num = strtoull(arg, &endptr, 0);
867 if (endptr == arg) {
868 return -1;
869 }
870
871 if (*endptr == 'k' || *endptr == 'K') {
872 if (num >= (-1ULL) / 1024) {
873 return -1;
874 }
875 num *= 1024LL;
876 endptr++;
877 } else if (*endptr == 'm' || *endptr == 'M') {
878 if (num >= (-1ULL) / (1024 * 1024)) {
879 return -1;
880 }
881 num *= 1024LL * 1024LL;
882 endptr++;
883 } else if (*endptr == 'g' || *endptr == 'G') {
884 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
885 return -1;
886 }
887 num *= 1024LL * 1024LL * 1024LL;
888 endptr++;
889 }
890
891 if (*endptr != '\0') {
892 return -1;
893 }
894
895 if (num > INT64_MAX) {
896 return -1;
897 }
898
899 return num;
900}
901
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700902void fb_perform_format(usb_handle* usb,
903 const char *partition, int skip_if_not_supported,
JP Abgrall7e859742014-05-06 15:14:15 -0700904 const char *type_override, const char *size_override)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700905{
JP Abgrall7e859742014-05-06 15:14:15 -0700906 char pTypeBuff[FB_RESPONSE_SZ + 1], pSizeBuff[FB_RESPONSE_SZ + 1];
907 char *pType = pTypeBuff;
908 char *pSize = pSizeBuff;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700909 unsigned int limit = INT_MAX;
910 struct fastboot_buffer buf;
911 const char *errMsg = NULL;
912 const struct fs_generator *gen;
913 uint64_t pSz;
914 int status;
915 int fd;
916
917 if (target_sparse_limit > 0 && target_sparse_limit < limit)
918 limit = target_sparse_limit;
919 if (sparse_limit > 0 && sparse_limit < limit)
920 limit = sparse_limit;
921
922 status = fb_getvar(usb, pType, "partition-type:%s", partition);
923 if (status) {
924 errMsg = "Can't determine partition type.\n";
925 goto failed;
926 }
JP Abgrall7e859742014-05-06 15:14:15 -0700927 if (type_override) {
928 if (strcmp(type_override, pType)) {
929 fprintf(stderr,
930 "Warning: %s type is %s, but %s was requested for formating.\n",
931 partition, pType, type_override);
932 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700933 pType = (char *)type_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700934 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700935
936 status = fb_getvar(usb, pSize, "partition-size:%s", partition);
937 if (status) {
938 errMsg = "Unable to get partition size\n";
939 goto failed;
940 }
JP Abgrall7e859742014-05-06 15:14:15 -0700941 if (size_override) {
942 if (strcmp(size_override, pSize)) {
943 fprintf(stderr,
944 "Warning: %s size is %s, but %s was requested for formating.\n",
945 partition, pSize, size_override);
946 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700947 pSize = (char *)size_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700948 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700949
950 gen = fs_get_generator(pType);
951 if (!gen) {
952 if (skip_if_not_supported) {
953 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
954 fprintf(stderr, "File system type %s not supported.\n", pType);
955 return;
956 }
957 fprintf(stderr, "Formatting is not supported for filesystem with type '%s'.\n", pType);
958 return;
959 }
960
961 pSz = strtoll(pSize, (char **)NULL, 16);
962
963 fd = fileno(tmpfile());
964 if (fs_generator_generate(gen, fd, pSz)) {
965 close(fd);
966 fprintf(stderr, "Cannot generate image.\n");
967 return;
968 }
969
970 if (load_buf_fd(usb, fd, &buf)) {
971 fprintf(stderr, "Cannot read image.\n");
972 close(fd);
973 return;
974 }
975 flash_buf(partition, &buf);
976
977 return;
978
979
980failed:
981 if (skip_if_not_supported) {
982 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
983 if (errMsg)
984 fprintf(stderr, "%s", errMsg);
985 }
986 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
987}
988
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800989int main(int argc, char **argv)
990{
991 int wants_wipe = 0;
992 int wants_reboot = 0;
993 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700994 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800995 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000996 unsigned sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700997 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700998 int c;
Florian Bäuerle27ded482014-11-24 11:29:34 +0100999 int longindex;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001000
JP Abgrall7b8970c2013-03-07 17:06:41 -08001001 const struct option longopts[] = {
1002 {"base", required_argument, 0, 'b'},
1003 {"kernel_offset", required_argument, 0, 'k'},
1004 {"page_size", required_argument, 0, 'n'},
1005 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001006 {"tags_offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001007 {"help", no_argument, 0, 'h'},
1008 {"unbuffered", no_argument, 0, 0},
1009 {"version", no_argument, 0, 0},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001010 {0, 0, 0, 0}
1011 };
Colin Cross8879f982012-05-22 17:53:34 -07001012
1013 serial = getenv("ANDROID_SERIAL");
1014
1015 while (1) {
Florian Bäuerle27ded482014-11-24 11:29:34 +01001016 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 -07001017 if (c < 0) {
1018 break;
1019 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001020 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001021 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -07001022 case 'b':
1023 base_addr = strtoul(optarg, 0, 16);
1024 break;
Colin Cross8879f982012-05-22 17:53:34 -07001025 case 'c':
1026 cmdline = optarg;
1027 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001028 case 'h':
1029 usage();
1030 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001031 case 'i': {
1032 char *endptr = NULL;
1033 unsigned long val;
1034
1035 val = strtoul(optarg, &endptr, 0);
1036 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1037 die("invalid vendor id '%s'", optarg);
1038 vendor_id = (unsigned short)val;
1039 break;
1040 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001041 case 'k':
1042 kernel_offset = strtoul(optarg, 0, 16);
1043 break;
1044 case 'l':
1045 long_listing = 1;
1046 break;
1047 case 'n':
1048 page_size = (unsigned)strtoul(optarg, NULL, 0);
1049 if (!page_size) die("invalid page size");
1050 break;
1051 case 'p':
1052 product = optarg;
1053 break;
1054 case 'r':
1055 ramdisk_offset = strtoul(optarg, 0, 16);
1056 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001057 case 't':
1058 tags_offset = strtoul(optarg, 0, 16);
1059 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001060 case 's':
1061 serial = optarg;
1062 break;
1063 case 'S':
1064 sparse_limit = parse_num(optarg);
1065 if (sparse_limit < 0) {
1066 die("invalid sparse limit");
1067 }
1068 break;
1069 case 'u':
1070 erase_first = 0;
1071 break;
1072 case 'w':
1073 wants_wipe = 1;
1074 break;
Colin Cross8879f982012-05-22 17:53:34 -07001075 case '?':
1076 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001077 case 0:
1078 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
1079 setvbuf(stdout, NULL, _IONBF, 0);
1080 setvbuf(stderr, NULL, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001081 } else if (strcmp("version", longopts[longindex].name) == 0) {
1082 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1083 return 0;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001084 }
1085 break;
Colin Cross8879f982012-05-22 17:53:34 -07001086 default:
1087 abort();
1088 }
1089 }
1090
1091 argc -= optind;
1092 argv += optind;
1093
1094 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001095 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001096 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001097 }
1098
Colin Cross8fb6e062012-07-24 16:36:41 -07001099 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001100 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001101 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001102 return 0;
1103 }
1104
Colin Crossc7b75dc2012-08-29 18:17:06 -07001105 if (argc > 0 && !strcmp(*argv, "help")) {
1106 usage();
1107 return 0;
1108 }
1109
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001110 usb_handle* usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001111
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001112 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -07001113 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001114 require(2);
1115 fb_queue_display(argv[1], argv[1]);
1116 skip(2);
1117 } else if(!strcmp(*argv, "erase")) {
1118 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001119
JP Abgrall7e859742014-05-06 15:14:15 -07001120 if (fb_format_supported(usb, argv[1], NULL)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001121 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
1122 }
1123
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001124 fb_queue_erase(argv[1]);
1125 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001126 } else if(!strncmp(*argv, "format", strlen("format"))) {
1127 char *overrides;
1128 char *type_override = NULL;
1129 char *size_override = NULL;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001130 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001131 /*
1132 * Parsing for: "format[:[type][:[size]]]"
1133 * Some valid things:
1134 * - select ontly the size, and leave default fs type:
1135 * format::0x4000000 userdata
1136 * - default fs type and size:
1137 * format userdata
1138 * format:: userdata
1139 */
1140 overrides = strchr(*argv, ':');
1141 if (overrides) {
1142 overrides++;
1143 size_override = strchr(overrides, ':');
1144 if (size_override) {
1145 size_override[0] = '\0';
1146 size_override++;
1147 }
1148 type_override = overrides;
1149 }
1150 if (type_override && !type_override[0]) type_override = NULL;
1151 if (size_override && !size_override[0]) size_override = NULL;
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001152 if (erase_first && needs_erase(usb, argv[1])) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001153 fb_queue_erase(argv[1]);
1154 }
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001155 fb_perform_format(usb, argv[1], 0, type_override, size_override);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001156 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001157 } else if(!strcmp(*argv, "signature")) {
1158 require(2);
1159 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -08001160 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001161 if (sz != 256) die("signature must be 256 bytes");
1162 fb_queue_download("signature", data, sz);
1163 fb_queue_command("signature", "installing signature");
1164 skip(2);
1165 } else if(!strcmp(*argv, "reboot")) {
1166 wants_reboot = 1;
1167 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001168 if (argc > 0) {
1169 if (!strcmp(*argv, "bootloader")) {
1170 wants_reboot = 0;
1171 wants_reboot_bootloader = 1;
1172 skip(1);
1173 }
1174 }
1175 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001176 } else if(!strcmp(*argv, "reboot-bootloader")) {
1177 wants_reboot_bootloader = 1;
1178 skip(1);
1179 } else if (!strcmp(*argv, "continue")) {
1180 fb_queue_command("continue", "resuming boot");
1181 skip(1);
1182 } else if(!strcmp(*argv, "boot")) {
1183 char *kname = 0;
1184 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001185 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001186 skip(1);
1187 if (argc > 0) {
1188 kname = argv[0];
1189 skip(1);
1190 }
1191 if (argc > 0) {
1192 rname = argv[0];
1193 skip(1);
1194 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001195 if (argc > 0) {
1196 sname = argv[0];
1197 skip(1);
1198 }
1199 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001200 if (data == 0) return 1;
1201 fb_queue_download("boot.img", data, sz);
1202 fb_queue_command("boot", "booting");
1203 } else if(!strcmp(*argv, "flash")) {
1204 char *pname = argv[1];
1205 char *fname = 0;
1206 require(2);
1207 if (argc > 2) {
1208 fname = argv[2];
1209 skip(3);
1210 } else {
1211 fname = find_item(pname, product);
1212 skip(2);
1213 }
1214 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001215 if (erase_first && needs_erase(usb, pname)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001216 fb_queue_erase(pname);
1217 }
Colin Crossf8387882012-05-24 17:18:41 -07001218 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001219 } else if(!strcmp(*argv, "flash:raw")) {
1220 char *pname = argv[1];
1221 char *kname = argv[2];
1222 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001223 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001224 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001225 skip(3);
1226 if (argc > 0) {
1227 rname = argv[0];
1228 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001229 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001230 if (argc > 0) {
1231 sname = argv[0];
1232 skip(1);
1233 }
1234 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001235 if (data == 0) die("cannot load bootable image");
1236 fb_queue_flash(pname, data, sz);
1237 } else if(!strcmp(*argv, "flashall")) {
1238 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001239 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001240 wants_reboot = 1;
1241 } else if(!strcmp(*argv, "update")) {
1242 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001243 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001244 skip(2);
1245 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001246 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001247 skip(1);
1248 }
1249 wants_reboot = 1;
1250 } else if(!strcmp(*argv, "oem")) {
1251 argc = do_oem_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001252 } else if(!strcmp(*argv, "flashing") && argc == 2) {
1253 if(!strcmp(*(argv+1), "unlock") || !strcmp(*(argv+1), "lock")
1254 || !strcmp(*(argv+1), "unlock_critical")
1255 || !strcmp(*(argv+1), "lock_critical")
1256 || !strcmp(*(argv+1), "get_unlock_ability")) {
1257 argc = do_oem_command(argc, argv);
1258 } else {
1259 usage();
1260 return 1;
1261 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001262 } else {
1263 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001264 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001265 }
1266 }
1267
1268 if (wants_wipe) {
1269 fb_queue_erase("userdata");
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001270 fb_perform_format(usb, "userdata", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001271 fb_queue_erase("cache");
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001272 fb_perform_format(usb, "cache", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001273 }
1274 if (wants_reboot) {
1275 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001276 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001277 } else if (wants_reboot_bootloader) {
1278 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001279 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001280 }
1281
Scott Anderson13081c62012-04-06 12:39:30 -07001282 if (fb_queue_is_empty())
1283 return 0;
1284
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001285 status = fb_execute_queue(usb);
1286 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001287}