blob: 7d26c6fb699b4ad5ab25d5d4e79dde5d5e81d3ec [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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031#include <stdio.h>
32#include <stdlib.h>
Colin Crossf8387882012-05-24 17:18:41 -070033#include <stdbool.h>
34#include <stdint.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035#include <string.h>
36#include <errno.h>
37#include <fcntl.h>
38#include <unistd.h>
39#include <limits.h>
40#include <ctype.h>
Colin Cross8879f982012-05-22 17:53:34 -070041#include <getopt.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
43#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070044#include <sys/types.h>
Rom Lemarchand622810c2013-06-28 09:54:59 -070045#include <sys/stat.h>
Colin Crossf8387882012-05-24 17:18:41 -070046
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047#include <bootimg.h>
Colin Crossf8387882012-05-24 17:18:41 -070048#include <sparse/sparse.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049#include <zipfile/zipfile.h>
50
51#include "fastboot.h"
52
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
Brian Swetland2a63bb72009-04-28 16:05:07 -070061void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline);
62
JP Abgrall7b8970c2013-03-07 17:06:41 -080063boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset,
64 void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset,
65 void *second, unsigned second_size, unsigned second_offset,
66 unsigned page_size, unsigned base, unsigned tags_offset,
Brian Swetland2a63bb72009-04-28 16:05:07 -070067 unsigned *bootimg_size);
68
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069static usb_handle *usb = 0;
70static const char *serial = 0;
71static const char *product = 0;
72static const char *cmdline = 0;
73static int wipe_data = 0;
74static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070075static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070076static int64_t sparse_limit = -1;
77static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078
JP Abgrall7b8970c2013-03-07 17:06:41 -080079unsigned page_size = 2048;
80unsigned base_addr = 0x10000000;
81unsigned kernel_offset = 0x00008000;
82unsigned ramdisk_offset = 0x01000000;
83unsigned second_offset = 0x00f00000;
84unsigned tags_offset = 0x00000100;
85
Rom Lemarchand622810c2013-06-28 09:54:59 -070086enum fb_buffer_type {
87 FB_BUFFER,
88 FB_BUFFER_SPARSE,
89};
90
91struct fastboot_buffer {
92 enum fb_buffer_type type;
93 void *data;
94 unsigned int sz;
95};
96
97static struct {
98 char img_name[13];
99 char sig_name[13];
100 char part_name[9];
101 bool is_optional;
102} images[3] = {
103 {"boot.img", "boot.sig", "boot", false},
104 {"recovery.img", "recovery.sig", "recovery", true},
105 {"system.img", "system.sig", "system", false},
106};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700107
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108void get_my_path(char *path);
109
110char *find_item(const char *item, const char *product)
111{
112 char *dir;
113 char *fn;
114 char path[PATH_MAX + 128];
115
116 if(!strcmp(item,"boot")) {
117 fn = "boot.img";
118 } else if(!strcmp(item,"recovery")) {
119 fn = "recovery.img";
120 } else if(!strcmp(item,"system")) {
121 fn = "system.img";
122 } else if(!strcmp(item,"userdata")) {
123 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700124 } else if(!strcmp(item,"cache")) {
125 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 } else if(!strcmp(item,"info")) {
127 fn = "android-info.txt";
128 } else {
129 fprintf(stderr,"unknown partition '%s'\n", item);
130 return 0;
131 }
132
133 if(product) {
134 get_my_path(path);
135 sprintf(path + strlen(path),
136 "../../../target/product/%s/%s", product, fn);
137 return strdup(path);
138 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800139
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 dir = getenv("ANDROID_PRODUCT_OUT");
141 if((dir == 0) || (dir[0] == 0)) {
142 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
143 return 0;
144 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800145
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 sprintf(path, "%s/%s", dir, fn);
147 return strdup(path);
148}
149
Rom Lemarchand622810c2013-06-28 09:54:59 -0700150static int64_t file_size(int fd)
Colin Crossf8387882012-05-24 17:18:41 -0700151{
Rom Lemarchand622810c2013-06-28 09:54:59 -0700152 struct stat st;
153 int ret;
Colin Crossf8387882012-05-24 17:18:41 -0700154
Rom Lemarchand622810c2013-06-28 09:54:59 -0700155 ret = fstat(fd, &st);
Colin Crossf8387882012-05-24 17:18:41 -0700156
Rom Lemarchand622810c2013-06-28 09:54:59 -0700157 return ret ? -1 : st.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700158}
159
Rom Lemarchand622810c2013-06-28 09:54:59 -0700160static void *load_fd(int fd, unsigned *_sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161{
162 char *data;
163 int sz;
Matt Gumbel64ba2582011-12-08 11:59:38 -0800164 int errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165
166 data = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167
Rom Lemarchand622810c2013-06-28 09:54:59 -0700168 sz = file_size(fd);
169 if (sz < 0) {
170 goto oops;
171 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172
173 data = (char*) malloc(sz);
174 if(data == 0) goto oops;
175
176 if(read(fd, data, sz) != sz) goto oops;
177 close(fd);
178
179 if(_sz) *_sz = sz;
180 return data;
181
182oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800183 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184 close(fd);
185 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800186 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 return 0;
188}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189
Rom Lemarchand622810c2013-06-28 09:54:59 -0700190static void *load_file(const char *fn, unsigned *_sz)
191{
192 int fd;
193
194 fd = open(fn, O_RDONLY | O_BINARY);
195 if(fd < 0) return 0;
196
197 return load_fd(fd, _sz);
198}
199
JP Abgralla032ded2012-06-06 11:53:33 -0700200int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
201{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400203 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800204 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700205 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800206 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700207 (info->dev_vendor != 0x0fce) && // Sony Ericsson
208 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400209 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800210 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800211 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800212 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800213 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400214 (info->dev_vendor != 0x0bb4)) // HTC
215 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216 if(info->ifc_class != 0xff) return -1;
217 if(info->ifc_subclass != 0x42) return -1;
218 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700219 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700221 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
222 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 return 0;
224}
225
JP Abgrall7b8970c2013-03-07 17:06:41 -0800226int match_fastboot(usb_ifc_info *info)
227{
228 return match_fastboot_with_serial(info, serial);
229}
230
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231int list_devices_callback(usb_ifc_info *info)
232{
JP Abgralla032ded2012-06-06 11:53:33 -0700233 if (match_fastboot_with_serial(info, NULL) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700235 if (!info->writable) {
236 serial = "no permissions"; // like "adb devices"
237 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 if (!serial[0]) {
239 serial = "????????????";
240 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700241 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700242 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700243 printf("%s\tfastboot\n", serial);
Scott Anderson866b1bd2012-06-04 20:29:11 -0700244 } else if (!info->device_path) {
245 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700246 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700247 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700248 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 }
250
251 return -1;
252}
253
254usb_handle *open_device(void)
255{
256 static usb_handle *usb = 0;
257 int announce = 1;
258
259 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800260
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261 for(;;) {
262 usb = usb_open(match_fastboot);
263 if(usb) return usb;
264 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800265 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266 fprintf(stderr,"< waiting for device >\n");
267 }
268 sleep(1);
269 }
270}
271
272void list_devices(void) {
273 // We don't actually open a USB device here,
274 // just getting our callback called so we can
275 // list all the connected devices.
276 usb_open(list_devices_callback);
277}
278
279void usage(void)
280{
281 fprintf(stderr,
282/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
283 "usage: fastboot [ <option> ] <command>\n"
284 "\n"
285 "commands:\n"
286 " update <filename> reflash device from update.zip\n"
287 " flashall flash boot + recovery + system\n"
288 " flash <partition> [ <filename> ] write a file to a flash partition\n"
289 " erase <partition> erase a flash partition\n"
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800290 " format <partition> format a flash partition \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291 " getvar <variable> display a bootloader variable\n"
292 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
293 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
294 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700295 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 " reboot reboot device normally\n"
297 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800298 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 "\n"
300 "options:\n"
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700301 " -w erase userdata and cache (and format\n"
302 " if supported by partition type)\n"
303 " -u do not first erase partition before\n"
304 " formatting\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700305 " -s <specific device> specify device serial number\n"
306 " or path to device port\n"
307 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 " -p <product> specify product name\n"
309 " -c <cmdline> override kernel commandline\n"
310 " -i <vendor id> specify a custom USB vendor id\n"
JP Abgrall7b8970c2013-03-07 17:06:41 -0800311 " -b <base_addr> specify a custom kernel base address. default: 0x10000000\n"
Dima Zavin931175a2010-02-12 20:26:33 -0800312 " -n <page size> specify the nand page size. default: 2048\n"
Colin Crossf8387882012-05-24 17:18:41 -0700313 " -S <size>[K|M|G] automatically sparse files greater than\n"
Colin Cross0bbfb392012-07-24 18:05:21 -0700314 " size. 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316}
317
JP Abgrall7b8970c2013-03-07 17:06:41 -0800318void *load_bootable_image(const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319 unsigned *sz, const char *cmdline)
320{
321 void *kdata = 0, *rdata = 0;
322 unsigned ksize = 0, rsize = 0;
323 void *bdata;
324 unsigned bsize;
325
326 if(kernel == 0) {
327 fprintf(stderr, "no image specified\n");
328 return 0;
329 }
330
331 kdata = load_file(kernel, &ksize);
332 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800333 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 return 0;
335 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800336
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337 /* is this actually a boot image? */
338 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
339 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800340
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 if(ramdisk) {
342 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
343 return 0;
344 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800345
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346 *sz = ksize;
347 return kdata;
348 }
349
350 if(ramdisk) {
351 rdata = load_file(ramdisk, &rsize);
352 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800353 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354 return 0;
355 }
356 }
357
358 fprintf(stderr,"creating boot image...\n");
JP Abgrall7b8970c2013-03-07 17:06:41 -0800359 bdata = mkbootimg(kdata, ksize, kernel_offset,
360 rdata, rsize, ramdisk_offset,
361 0, 0, second_offset,
362 page_size, base_addr, tags_offset, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 if(bdata == 0) {
364 fprintf(stderr,"failed to create boot.img\n");
365 return 0;
366 }
367 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
368 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
369 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800370
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371 return bdata;
372}
373
374void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
375{
376 void *data;
377 zipentry_t entry;
378 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800379
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380 entry = lookup_zipentry(zip, name);
381 if (entry == NULL) {
382 fprintf(stderr, "archive does not contain '%s'\n", name);
383 return 0;
384 }
385
386 *sz = get_zipentry_size(entry);
387
388 datasz = *sz * 1.001;
389 data = malloc(datasz);
390
391 if(data == 0) {
392 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
393 return 0;
394 }
395
396 if (decompress_zipentry(entry, data, datasz)) {
397 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
398 free(data);
399 return 0;
400 }
401
402 return data;
403}
404
Rom Lemarchand622810c2013-06-28 09:54:59 -0700405static int unzip_to_file(zipfile_t zip, char *name)
406{
407 int fd;
408 char *data;
409 unsigned sz;
410
411 fd = fileno(tmpfile());
412 if (fd < 0) {
413 return -1;
414 }
415
416 data = unzip_file(zip, name, &sz);
417 if (data == 0) {
418 return -1;
419 }
420
421 if (write(fd, data, sz) != sz) {
422 fd = -1;
423 }
424
425 free(data);
426 lseek(fd, 0, SEEK_SET);
427 return fd;
428}
429
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430static char *strip(char *s)
431{
432 int n;
433 while(*s && isspace(*s)) s++;
434 n = strlen(s);
435 while(n-- > 0) {
436 if(!isspace(s[n])) break;
437 s[n] = 0;
438 }
439 return s;
440}
441
442#define MAX_OPTIONS 32
443static int setup_requirement_line(char *name)
444{
445 char *val[MAX_OPTIONS];
446 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700447 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 unsigned n, count;
449 char *x;
450 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800451
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452 if (!strncmp(name, "reject ", 7)) {
453 name += 7;
454 invert = 1;
455 } else if (!strncmp(name, "require ", 8)) {
456 name += 8;
457 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700458 } else if (!strncmp(name, "require-for-product:", 20)) {
459 // Get the product and point name past it
460 prod = name + 20;
461 name = strchr(name, ' ');
462 if (!name) return -1;
463 *name = 0;
464 name += 1;
465 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466 }
467
468 x = strchr(name, '=');
469 if (x == 0) return 0;
470 *x = 0;
471 val[0] = x + 1;
472
473 for(count = 1; count < MAX_OPTIONS; count++) {
474 x = strchr(val[count - 1],'|');
475 if (x == 0) break;
476 *x = 0;
477 val[count] = x + 1;
478 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800479
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480 name = strip(name);
481 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800482
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483 name = strip(name);
484 if (name == 0) return -1;
485
486 /* work around an unfortunate name mismatch */
487 if (!strcmp(name,"board")) name = "product";
488
489 out = malloc(sizeof(char*) * count);
490 if (out == 0) return -1;
491
492 for(n = 0; n < count; n++) {
493 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700494 if (out[n] == 0) {
495 for(size_t i = 0; i < n; ++i) {
496 free((char*) out[i]);
497 }
498 free(out);
499 return -1;
500 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 }
502
Wink Savilleb98762f2011-04-04 17:54:59 -0700503 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 return 0;
505}
506
507static void setup_requirements(char *data, unsigned sz)
508{
509 char *s;
510
511 s = data;
512 while (sz-- > 0) {
513 if(*s == '\n') {
514 *s++ = 0;
515 if (setup_requirement_line(data)) {
516 die("out of memory");
517 }
518 data = s;
519 } else {
520 s++;
521 }
522 }
523}
524
525void queue_info_dump(void)
526{
527 fb_queue_notice("--------------------------------------------");
528 fb_queue_display("version-bootloader", "Bootloader Version...");
529 fb_queue_display("version-baseband", "Baseband Version.....");
530 fb_queue_display("serialno", "Serial Number........");
531 fb_queue_notice("--------------------------------------------");
532}
533
Rom Lemarchand622810c2013-06-28 09:54:59 -0700534static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700535{
Colin Crossf8387882012-05-24 17:18:41 -0700536 struct sparse_file *s;
537 int files;
538 struct sparse_file **out_s;
539
Colin Crossf8387882012-05-24 17:18:41 -0700540 s = sparse_file_import_auto(fd, false);
541 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700542 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700543 }
544
545 files = sparse_file_resparse(s, max_size, NULL, 0);
546 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700547 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700548 }
549
550 out_s = calloc(sizeof(struct sparse_file *), files + 1);
551 if (!out_s) {
552 die("Failed to allocate sparse file array\n");
553 }
554
555 files = sparse_file_resparse(s, max_size, out_s, files);
556 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700557 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700558 }
559
560 return out_s;
561}
562
563static int64_t get_target_sparse_limit(struct usb_handle *usb)
564{
565 int64_t limit = 0;
566 char response[FB_RESPONSE_SZ + 1];
567 int status = fb_getvar(usb, response, "max-download-size");
568
569 if (!status) {
570 limit = strtoul(response, NULL, 0);
571 if (limit > 0) {
572 fprintf(stderr, "target reported max download size of %lld bytes\n",
573 limit);
574 }
575 }
576
577 return limit;
578}
579
580static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
581{
582 int64_t limit;
583
584 if (sparse_limit == 0) {
585 return 0;
586 } else if (sparse_limit > 0) {
587 limit = sparse_limit;
588 } else {
589 if (target_sparse_limit == -1) {
590 target_sparse_limit = get_target_sparse_limit(usb);
591 }
592 if (target_sparse_limit > 0) {
593 limit = target_sparse_limit;
594 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700595 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700596 }
597 }
598
599 if (size > limit) {
600 return limit;
601 }
602
603 return 0;
604}
605
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700606/* Until we get lazy inode table init working in make_ext4fs, we need to
607 * erase partitions of type ext4 before flashing a filesystem so no stale
608 * inodes are left lying around. Otherwise, e2fsck gets very upset.
609 */
610static int needs_erase(const char *part)
611{
612 /* The function fb_format_supported() currently returns the value
613 * we want, so just call it.
614 */
615 return fb_format_supported(usb, part);
616}
617
Rom Lemarchand622810c2013-06-28 09:54:59 -0700618static int load_buf_fd(usb_handle *usb, int fd,
619 struct fastboot_buffer *buf)
Colin Crossf8387882012-05-24 17:18:41 -0700620{
621 int64_t sz64;
622 void *data;
623 int64_t limit;
624
Rom Lemarchand622810c2013-06-28 09:54:59 -0700625 sz64 = file_size(fd);
626 if (sz64 < 0) {
627 return -1;
628 }
Colin Crossf8387882012-05-24 17:18:41 -0700629 limit = get_sparse_limit(usb, sz64);
630 if (limit) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700631 struct sparse_file **s = load_sparse_files(fd, limit);
Colin Crossf8387882012-05-24 17:18:41 -0700632 if (s == NULL) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700633 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700634 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700635 buf->type = FB_BUFFER_SPARSE;
636 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700637 } else {
638 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700639 data = load_fd(fd, &sz);
640 if (data == 0) return -1;
641 buf->type = FB_BUFFER;
642 buf->data = data;
643 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700644 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700645
646 return 0;
647}
648
649static int load_buf(usb_handle *usb, const char *fname,
650 struct fastboot_buffer *buf)
651{
652 int fd;
653
654 fd = open(fname, O_RDONLY | O_BINARY);
655 if (fd < 0) {
656 die("cannot open '%s'\n", fname);
657 }
658
659 return load_buf_fd(usb, fd, buf);
660}
661
662static void flash_buf(const char *pname, struct fastboot_buffer *buf)
663{
664 struct sparse_file **s;
665
666 switch (buf->type) {
667 case FB_BUFFER_SPARSE:
668 s = buf->data;
669 while (*s) {
670 int64_t sz64 = sparse_file_len(*s, true, false);
671 fb_queue_flash_sparse(pname, *s++, sz64);
672 }
673 break;
674 case FB_BUFFER:
675 fb_queue_flash(pname, buf->data, buf->sz);
676 break;
677 default:
678 die("unknown buffer type: %d", buf->type);
679 }
680}
681
682void do_flash(usb_handle *usb, const char *pname, const char *fname)
683{
684 struct fastboot_buffer buf;
685
686 if (load_buf(usb, fname, &buf)) {
687 die("cannot load '%s'", fname);
688 }
689 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700690}
691
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692void do_update_signature(zipfile_t zip, char *fn)
693{
694 void *data;
695 unsigned sz;
696 data = unzip_file(zip, fn, &sz);
697 if (data == 0) return;
698 fb_queue_download("signature", data, sz);
699 fb_queue_command("signature", "installing signature");
700}
701
Rom Lemarchand622810c2013-06-28 09:54:59 -0700702void do_update(usb_handle *usb, char *fn, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703{
704 void *zdata;
705 unsigned zsize;
706 void *data;
707 unsigned sz;
708 zipfile_t zip;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700709 int fd;
710 int rc;
711 struct fastboot_buffer buf;
712 int i;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800713
714 queue_info_dump();
715
Wink Savilleb98762f2011-04-04 17:54:59 -0700716 fb_queue_query_save("product", cur_product, sizeof(cur_product));
717
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718 zdata = load_file(fn, &zsize);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800719 if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800720
721 zip = init_zipfile(zdata, zsize);
722 if(zip == 0) die("failed to access zipdata in '%s'");
723
724 data = unzip_file(zip, "android-info.txt", &sz);
725 if (data == 0) {
726 char *tmp;
727 /* fallback for older zipfiles */
728 data = unzip_file(zip, "android-product.txt", &sz);
729 if ((data == 0) || (sz < 1)) {
730 die("update package has no android-info.txt or android-product.txt");
731 }
732 tmp = malloc(sz + 128);
733 if (tmp == 0) die("out of memory");
734 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
735 data = tmp;
736 sz = strlen(tmp);
737 }
738
739 setup_requirements(data, sz);
740
Rom Lemarchand622810c2013-06-28 09:54:59 -0700741 for (i = 0; i < ARRAY_SIZE(images); i++) {
742 fd = unzip_to_file(zip, images[i].img_name);
743 if (fd < 0) {
744 if (images[i].is_optional)
745 continue;
746 die("update package missing %s", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700747 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700748 rc = load_buf_fd(usb, fd, &buf);
749 if (rc) die("cannot load %s from flash", images[i].img_name);
750 do_update_signature(zip, images[i].sig_name);
751 if (erase_first && needs_erase(images[i].part_name)) {
752 fb_queue_erase(images[i].part_name);
753 }
754 flash_buf(images[i].part_name, &buf);
755 /* not closing the fd here since the sparse code keeps the fd around
756 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
757 * program exits.
758 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800759 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800760}
761
762void do_send_signature(char *fn)
763{
764 void *data;
765 unsigned sz;
766 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800767
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800768 xtn = strrchr(fn, '.');
769 if (!xtn) return;
770 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800771
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800772 strcpy(xtn,".sig");
773 data = load_file(fn, &sz);
774 strcpy(xtn,".img");
775 if (data == 0) return;
776 fb_queue_download("signature", data, sz);
777 fb_queue_command("signature", "installing signature");
778}
779
Rom Lemarchand622810c2013-06-28 09:54:59 -0700780void do_flashall(usb_handle *usb, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800781{
782 char *fname;
783 void *data;
784 unsigned sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700785 struct fastboot_buffer buf;
786 int i;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800787
788 queue_info_dump();
789
Wink Savilleb98762f2011-04-04 17:54:59 -0700790 fb_queue_query_save("product", cur_product, sizeof(cur_product));
791
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800792 fname = find_item("info", product);
793 if (fname == 0) die("cannot find android-info.txt");
794 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800795 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800796 setup_requirements(data, sz);
797
Rom Lemarchand622810c2013-06-28 09:54:59 -0700798 for (i = 0; i < ARRAY_SIZE(images); i++) {
799 fname = find_item(images[i].part_name, product);
800 if (load_buf(usb, fname, &buf)) {
801 if (images[i].is_optional)
802 continue;
803 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700804 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700805 do_send_signature(fname);
806 if (erase_first && needs_erase(images[i].part_name)) {
807 fb_queue_erase(images[i].part_name);
808 }
809 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800810 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800811}
812
813#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800814#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800815
816int do_oem_command(int argc, char **argv)
817{
818 int i;
819 char command[256];
820 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800821
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800822 command[0] = 0;
823 while(1) {
824 strcat(command,*argv);
825 skip(1);
826 if(argc == 0) break;
827 strcat(command," ");
828 }
829
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800830 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800831 return 0;
832}
833
Colin Crossf8387882012-05-24 17:18:41 -0700834static int64_t parse_num(const char *arg)
835{
836 char *endptr;
837 unsigned long long num;
838
839 num = strtoull(arg, &endptr, 0);
840 if (endptr == arg) {
841 return -1;
842 }
843
844 if (*endptr == 'k' || *endptr == 'K') {
845 if (num >= (-1ULL) / 1024) {
846 return -1;
847 }
848 num *= 1024LL;
849 endptr++;
850 } else if (*endptr == 'm' || *endptr == 'M') {
851 if (num >= (-1ULL) / (1024 * 1024)) {
852 return -1;
853 }
854 num *= 1024LL * 1024LL;
855 endptr++;
856 } else if (*endptr == 'g' || *endptr == 'G') {
857 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
858 return -1;
859 }
860 num *= 1024LL * 1024LL * 1024LL;
861 endptr++;
862 }
863
864 if (*endptr != '\0') {
865 return -1;
866 }
867
868 if (num > INT64_MAX) {
869 return -1;
870 }
871
872 return num;
873}
874
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800875int main(int argc, char **argv)
876{
877 int wants_wipe = 0;
878 int wants_reboot = 0;
879 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700880 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800881 void *data;
882 unsigned sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700883 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700884 int c;
Colin Crossf8387882012-05-24 17:18:41 -0700885 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800886
JP Abgrall7b8970c2013-03-07 17:06:41 -0800887 const struct option longopts[] = {
888 {"base", required_argument, 0, 'b'},
889 {"kernel_offset", required_argument, 0, 'k'},
890 {"page_size", required_argument, 0, 'n'},
891 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyashbdf513c2014-01-14 18:27:25 -0800892 {"tags_offset", required_argument, 0, 't'},
JP Abgrall7b8970c2013-03-07 17:06:41 -0800893 {"help", 0, 0, 'h'},
894 {0, 0, 0, 0}
895 };
Colin Cross8879f982012-05-22 17:53:34 -0700896
897 serial = getenv("ANDROID_SERIAL");
898
899 while (1) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800900 int option_index = 0;
Mohamad Ayyashbdf513c2014-01-14 18:27:25 -0800901 c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:h", longopts, NULL);
Colin Cross8879f982012-05-22 17:53:34 -0700902 if (c < 0) {
903 break;
904 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800905 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -0700906 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -0700907 case 'b':
908 base_addr = strtoul(optarg, 0, 16);
909 break;
Colin Cross8879f982012-05-22 17:53:34 -0700910 case 'c':
911 cmdline = optarg;
912 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -0800913 case 'h':
914 usage();
915 return 1;
Colin Cross8879f982012-05-22 17:53:34 -0700916 case 'i': {
917 char *endptr = NULL;
918 unsigned long val;
919
920 val = strtoul(optarg, &endptr, 0);
921 if (!endptr || *endptr != '\0' || (val & ~0xffff))
922 die("invalid vendor id '%s'", optarg);
923 vendor_id = (unsigned short)val;
924 break;
925 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800926 case 'k':
927 kernel_offset = strtoul(optarg, 0, 16);
928 break;
929 case 'l':
930 long_listing = 1;
931 break;
932 case 'n':
933 page_size = (unsigned)strtoul(optarg, NULL, 0);
934 if (!page_size) die("invalid page size");
935 break;
936 case 'p':
937 product = optarg;
938 break;
939 case 'r':
940 ramdisk_offset = strtoul(optarg, 0, 16);
941 break;
Mohamad Ayyashbdf513c2014-01-14 18:27:25 -0800942 case 't':
943 tags_offset = strtoul(optarg, 0, 16);
944 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -0800945 case 's':
946 serial = optarg;
947 break;
948 case 'S':
949 sparse_limit = parse_num(optarg);
950 if (sparse_limit < 0) {
951 die("invalid sparse limit");
952 }
953 break;
954 case 'u':
955 erase_first = 0;
956 break;
957 case 'w':
958 wants_wipe = 1;
959 break;
Colin Cross8879f982012-05-22 17:53:34 -0700960 case '?':
961 return 1;
962 default:
963 abort();
964 }
965 }
966
967 argc -= optind;
968 argv += optind;
969
970 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800971 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700972 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800973 }
974
Colin Cross8fb6e062012-07-24 16:36:41 -0700975 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -0700976 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800977 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800978 return 0;
979 }
980
Colin Crossc7b75dc2012-08-29 18:17:06 -0700981 if (argc > 0 && !strcmp(*argv, "help")) {
982 usage();
983 return 0;
984 }
985
Colin Cross8879f982012-05-22 17:53:34 -0700986 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -0700987
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800988 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -0700989 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800990 require(2);
991 fb_queue_display(argv[1], argv[1]);
992 skip(2);
993 } else if(!strcmp(*argv, "erase")) {
994 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700995
996 if (fb_format_supported(usb, argv[1])) {
997 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
998 }
999
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001000 fb_queue_erase(argv[1]);
1001 skip(2);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001002 } else if(!strcmp(*argv, "format")) {
1003 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001004 if (erase_first && needs_erase(argv[1])) {
1005 fb_queue_erase(argv[1]);
1006 }
JP Abgrall30ae5802012-05-07 20:25:24 -07001007 fb_queue_format(argv[1], 0);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001008 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001009 } else if(!strcmp(*argv, "signature")) {
1010 require(2);
1011 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -08001012 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001013 if (sz != 256) die("signature must be 256 bytes");
1014 fb_queue_download("signature", data, sz);
1015 fb_queue_command("signature", "installing signature");
1016 skip(2);
1017 } else if(!strcmp(*argv, "reboot")) {
1018 wants_reboot = 1;
1019 skip(1);
1020 } else if(!strcmp(*argv, "reboot-bootloader")) {
1021 wants_reboot_bootloader = 1;
1022 skip(1);
1023 } else if (!strcmp(*argv, "continue")) {
1024 fb_queue_command("continue", "resuming boot");
1025 skip(1);
1026 } else if(!strcmp(*argv, "boot")) {
1027 char *kname = 0;
1028 char *rname = 0;
1029 skip(1);
1030 if (argc > 0) {
1031 kname = argv[0];
1032 skip(1);
1033 }
1034 if (argc > 0) {
1035 rname = argv[0];
1036 skip(1);
1037 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001038 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001039 if (data == 0) return 1;
1040 fb_queue_download("boot.img", data, sz);
1041 fb_queue_command("boot", "booting");
1042 } else if(!strcmp(*argv, "flash")) {
1043 char *pname = argv[1];
1044 char *fname = 0;
1045 require(2);
1046 if (argc > 2) {
1047 fname = argv[2];
1048 skip(3);
1049 } else {
1050 fname = find_item(pname, product);
1051 skip(2);
1052 }
1053 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001054 if (erase_first && needs_erase(pname)) {
1055 fb_queue_erase(pname);
1056 }
Colin Crossf8387882012-05-24 17:18:41 -07001057 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001058 } else if(!strcmp(*argv, "flash:raw")) {
1059 char *pname = argv[1];
1060 char *kname = argv[2];
1061 char *rname = 0;
1062 require(3);
1063 if(argc > 3) {
1064 rname = argv[3];
1065 skip(4);
1066 } else {
1067 skip(3);
1068 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001069 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001070 if (data == 0) die("cannot load bootable image");
1071 fb_queue_flash(pname, data, sz);
1072 } else if(!strcmp(*argv, "flashall")) {
1073 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001074 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001075 wants_reboot = 1;
1076 } else if(!strcmp(*argv, "update")) {
1077 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001078 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001079 skip(2);
1080 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001081 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001082 skip(1);
1083 }
1084 wants_reboot = 1;
1085 } else if(!strcmp(*argv, "oem")) {
1086 argc = do_oem_command(argc, argv);
1087 } else {
1088 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001089 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001090 }
1091 }
1092
1093 if (wants_wipe) {
1094 fb_queue_erase("userdata");
JP Abgrall30ae5802012-05-07 20:25:24 -07001095 fb_queue_format("userdata", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001096 fb_queue_erase("cache");
JP Abgrall30ae5802012-05-07 20:25:24 -07001097 fb_queue_format("cache", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001098 }
1099 if (wants_reboot) {
1100 fb_queue_reboot();
1101 } else if (wants_reboot_bootloader) {
1102 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001103 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001104 }
1105
Scott Anderson13081c62012-04-06 12:39:30 -07001106 if (fb_queue_is_empty())
1107 return 0;
1108
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001109 status = fb_execute_queue(usb);
1110 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001111}