1dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/*
2dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
3dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * All rights reserved.
4dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
5dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Redistribution and use in source and binary forms, with or without
6dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * modification, are permitted provided that the following conditions
7dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * are met:
8dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *  * Redistributions of source code must retain the above copyright
9dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
10dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *  * Redistributions in binary form must reproduce the above copyright
11dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *    notice, this list of conditions and the following disclaimer in
12ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang *    the documentation and/or other materials provided with the
13dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *    distribution.
14dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
15dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * SUCH DAMAGE.
27dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
28dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
29f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross#define _LARGEFILE64_SOURCE
30f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
31dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdio.h>
32dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdlib.h>
33dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdarg.h>
34f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross#include <stdbool.h>
35f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross#include <stdint.h>
36dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <string.h>
37dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <errno.h>
38dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <fcntl.h>
39dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <unistd.h>
40dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <limits.h>
41dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <ctype.h>
428879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross#include <getopt.h>
43dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
44dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <sys/time.h>
45f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross#include <sys/types.h>
46f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
47dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <bootimg.h>
48f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross#include <sparse/sparse.h>
49dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <zipfile/zipfile.h>
50dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
51dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "fastboot.h"
52dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
53f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross#ifndef O_BINARY
54f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross#define O_BINARY 0
55f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross#endif
56f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
57b98762f7824c291dc5d9a9b757af434ef31d8c81Wink Savillechar cur_product[FB_RESPONSE_SZ + 1];
58b98762f7824c291dc5d9a9b757af434ef31d8c81Wink Saville
592a63bb7abf1b27a7a2e8fd5951d77f71a2f290d4Brian Swetlandvoid bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline);
602a63bb7abf1b27a7a2e8fd5951d77f71a2f290d4Brian Swetland
612a63bb7abf1b27a7a2e8fd5951d77f71a2f290d4Brian Swetlandboot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size,
622a63bb7abf1b27a7a2e8fd5951d77f71a2f290d4Brian Swetland                        void *ramdisk, unsigned ramdisk_size,
632a63bb7abf1b27a7a2e8fd5951d77f71a2f290d4Brian Swetland                        void *second, unsigned second_size,
642a63bb7abf1b27a7a2e8fd5951d77f71a2f290d4Brian Swetland                        unsigned page_size, unsigned base,
652a63bb7abf1b27a7a2e8fd5951d77f71a2f290d4Brian Swetland                        unsigned *bootimg_size);
662a63bb7abf1b27a7a2e8fd5951d77f71a2f290d4Brian Swetland
67dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic usb_handle *usb = 0;
68dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic const char *serial = 0;
69dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic const char *product = 0;
70dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic const char *cmdline = 0;
71dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int wipe_data = 0;
72dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic unsigned short vendor_id = 0;
7313081c6915220db03886b177f1a8e0b2c63467c9Scott Andersonstatic int long_listing = 0;
74f838788e6a4d57634a53eb597ee76a597feffcb5Colin Crossstatic int64_t sparse_limit = -1;
75f838788e6a4d57634a53eb597ee76a597feffcb5Colin Crossstatic int64_t target_sparse_limit = -1;
76dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
772a63bb7abf1b27a7a2e8fd5951d77f71a2f290d4Brian Swetlandstatic unsigned base_addr = 0x10000000;
782a63bb7abf1b27a7a2e8fd5951d77f71a2f290d4Brian Swetland
79dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid die(const char *fmt, ...)
80dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
81dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    va_list ap;
82dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    va_start(ap, fmt);
83dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fprintf(stderr,"error: ");
84dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vfprintf(stderr, fmt, ap);
85dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fprintf(stderr,"\n");
86dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    va_end(ap);
87dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    exit(1);
88ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang}
89dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
90dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid get_my_path(char *path);
91dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
92dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectchar *find_item(const char *item, const char *product)
93dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
94dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *dir;
95dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *fn;
96dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char path[PATH_MAX + 128];
97dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
98dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(!strcmp(item,"boot")) {
99dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fn = "boot.img";
100dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else if(!strcmp(item,"recovery")) {
101dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fn = "recovery.img";
102dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else if(!strcmp(item,"system")) {
103dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fn = "system.img";
104dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else if(!strcmp(item,"userdata")) {
105dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fn = "userdata.img";
106d7608a40d6bed0d8ca686414a5a4f44b6d4d0435Jean-Baptiste Queru    } else if(!strcmp(item,"cache")) {
107d7608a40d6bed0d8ca686414a5a4f44b6d4d0435Jean-Baptiste Queru        fn = "cache.img";
108dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else if(!strcmp(item,"info")) {
109dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fn = "android-info.txt";
110dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else {
111dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr,"unknown partition '%s'\n", item);
112dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 0;
113dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
114dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
115dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(product) {
116dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        get_my_path(path);
117dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        sprintf(path + strlen(path),
118dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                "../../../target/product/%s/%s", product, fn);
119dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return strdup(path);
120dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
121ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang
122dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    dir = getenv("ANDROID_PRODUCT_OUT");
123dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if((dir == 0) || (dir[0] == 0)) {
124dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
125dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 0;
126dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
127ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang
128dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    sprintf(path, "%s/%s", dir, fn);
129dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return strdup(path);
130dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
131dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
132dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifdef _WIN32
133dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid *load_file(const char *fn, unsigned *_sz);
134f838788e6a4d57634a53eb597ee76a597feffcb5Colin Crossint64_t file_size(const char *fn);
135dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#else
136f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross#if defined(__APPLE__) && defined(__MACH__)
137f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross#define lseek64 lseek
138f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross#define off64_t off_t
139f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross#endif
140f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
141f838788e6a4d57634a53eb597ee76a597feffcb5Colin Crossint64_t file_size(const char *fn)
142f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross{
143f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    off64_t off;
144f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    int fd;
145f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
146f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    fd = open(fn, O_RDONLY);
147f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    if (fd < 0) return -1;
148f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
149f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    off = lseek64(fd, 0, SEEK_END);
150f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    close(fd);
151f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
152f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    return off;
153f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross}
154f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
155dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid *load_file(const char *fn, unsigned *_sz)
156dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
157dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *data;
158dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int sz;
159dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int fd;
16064ba258b7a17fd5f0abd788c1b021ad45ad732b9Matt Gumbel    int errno_tmp;
161dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
162dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    data = 0;
163dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fd = open(fn, O_RDONLY);
164dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(fd < 0) return 0;
165dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
166dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    sz = lseek(fd, 0, SEEK_END);
167dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(sz < 0) goto oops;
168dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
169dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
170dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
171dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    data = (char*) malloc(sz);
172dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(data == 0) goto oops;
173dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
174dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(read(fd, data, sz) != sz) goto oops;
175dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    close(fd);
176dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
177dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(_sz) *_sz = sz;
178dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return data;
179dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
180dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectoops:
18164ba258b7a17fd5f0abd788c1b021ad45ad732b9Matt Gumbel    errno_tmp = errno;
182dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    close(fd);
183dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(data != 0) free(data);
18464ba258b7a17fd5f0abd788c1b021ad45ad732b9Matt Gumbel    errno = errno_tmp;
185dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
186dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
187dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
188dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
189dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint match_fastboot(usb_ifc_info *info)
190dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
191a032dedefe275b1d5a08b9856dfcfcb12579b4a7JP Abgrall    return match_fastboot_with_serial(info, serial);
192a032dedefe275b1d5a08b9856dfcfcb12579b4a7JP Abgrall}
193a032dedefe275b1d5a08b9856dfcfcb12579b4a7JP Abgrall
194a032dedefe275b1d5a08b9856dfcfcb12579b4a7JP Abgrallint match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
195a032dedefe275b1d5a08b9856dfcfcb12579b4a7JP Abgrall{
196dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
19709070d9e8685645f4ca8ecca62ae489472aed646Mike Lockwood       (info->dev_vendor != 0x18d1) &&  // Google
198f60e8638282d66c108f1e1dea030856312f65892Wu, Hao       (info->dev_vendor != 0x8087) &&  // Intel
199f614d64d4b4dfcd20c77ac3ccf2d9c9090a49303The Android Open Source Project       (info->dev_vendor != 0x0451) &&
200e25ff1c682ae5cc87af5e76de7e31a2702b620f3Robert CH Chou       (info->dev_vendor != 0x0502) &&
201509f7399b8f246f506307b22039e63323ec09622Dima Zavin       (info->dev_vendor != 0x0fce) &&  // Sony Ericsson
202509f7399b8f246f506307b22039e63323ec09622Dima Zavin       (info->dev_vendor != 0x05c6) &&  // Qualcomm
20309070d9e8685645f4ca8ecca62ae489472aed646Mike Lockwood       (info->dev_vendor != 0x22b8) &&  // Motorola
20437e9e904360d6a8757c958dacaab1343eacb7fa8Erik Gilling       (info->dev_vendor != 0x0955) &&  // Nvidia
205af82f21df7b87af2b378a46c8f338ebdd9c66175Xavier Ducrohet       (info->dev_vendor != 0x413c) &&  // DELL
206746f3243f6d30ab51a36dcfea76eac3ba29ac625Xavier Ducrohet       (info->dev_vendor != 0x2314) &&  // INQ Mobile
20773c019b30a7c70c50d2a854982143d5eb8c74f8cRamanan Rajeswaran       (info->dev_vendor != 0x0b05) &&  // Asus
20809070d9e8685645f4ca8ecca62ae489472aed646Mike Lockwood       (info->dev_vendor != 0x0bb4))    // HTC
20909070d9e8685645f4ca8ecca62ae489472aed646Mike Lockwood            return -1;
210dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(info->ifc_class != 0xff) return -1;
211dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(info->ifc_subclass != 0x42) return -1;
212dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(info->ifc_protocol != 0x03) return -1;
21313081c6915220db03886b177f1a8e0b2c63467c9Scott Anderson    // require matching serial number or device path if requested
214dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    // at the command line with the -s option.
215a032dedefe275b1d5a08b9856dfcfcb12579b4a7JP Abgrall    if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
216a032dedefe275b1d5a08b9856dfcfcb12579b4a7JP Abgrall                   strcmp(local_serial, info->device_path) != 0)) return -1;
217dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
218dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
219dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
220dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint list_devices_callback(usb_ifc_info *info)
221dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
222a032dedefe275b1d5a08b9856dfcfcb12579b4a7JP Abgrall    if (match_fastboot_with_serial(info, NULL) == 0) {
223dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        char* serial = info->serial_number;
224b4add9b74525210478bac702d27fdaf9cf7ab18fElliott Hughes        if (!info->writable) {
225b4add9b74525210478bac702d27fdaf9cf7ab18fElliott Hughes            serial = "no permissions"; // like "adb devices"
226b4add9b74525210478bac702d27fdaf9cf7ab18fElliott Hughes        }
227dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (!serial[0]) {
228dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            serial = "????????????";
229dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
230866b1bd5051db4f22b634df1f8a06bc1c9aa2e26Scott Anderson        // output compatible with "adb devices"
23113081c6915220db03886b177f1a8e0b2c63467c9Scott Anderson        if (!long_listing) {
23213081c6915220db03886b177f1a8e0b2c63467c9Scott Anderson            printf("%s\tfastboot\n", serial);
233866b1bd5051db4f22b634df1f8a06bc1c9aa2e26Scott Anderson        } else if (!info->device_path) {
234866b1bd5051db4f22b634df1f8a06bc1c9aa2e26Scott Anderson            printf("%-22s fastboot\n", serial);
23513081c6915220db03886b177f1a8e0b2c63467c9Scott Anderson        } else {
236866b1bd5051db4f22b634df1f8a06bc1c9aa2e26Scott Anderson            printf("%-22s fastboot %s\n", serial, info->device_path);
23713081c6915220db03886b177f1a8e0b2c63467c9Scott Anderson        }
238dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
239dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
240dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return -1;
241dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
242dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
243dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectusb_handle *open_device(void)
244dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
245dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    static usb_handle *usb = 0;
246dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int announce = 1;
247dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
248dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(usb) return usb;
249ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang
250dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    for(;;) {
251dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        usb = usb_open(match_fastboot);
252dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(usb) return usb;
253dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(announce) {
254ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang            announce = 0;
255dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            fprintf(stderr,"< waiting for device >\n");
256dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
257dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        sleep(1);
258dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
259dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
260dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
261dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid list_devices(void) {
262dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    // We don't actually open a USB device here,
263dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    // just getting our callback called so we can
264dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    // list all the connected devices.
265dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    usb_open(list_devices_callback);
266dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
267dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
268dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid usage(void)
269dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
270dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fprintf(stderr,
271dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/*           1234567890123456789012345678901234567890123456789012345678901234567890123456 */
272dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "usage: fastboot [ <option> ] <command>\n"
273dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "\n"
274dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "commands:\n"
275dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "  update <filename>                        reflash device from update.zip\n"
276dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "  flashall                                 flash boot + recovery + system\n"
277dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "  flash <partition> [ <filename> ]         write a file to a flash partition\n"
278dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "  erase <partition>                        erase a flash partition\n"
279c8ba5366da7e23ebf1cd76bcf49449b878563102Anatol Pomazau            "  format <partition>                       format a flash partition \n"
280dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "  getvar <variable>                        display a bootloader variable\n"
281dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "  boot <kernel> [ <ramdisk> ]              download and boot kernel\n"
282dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "  flash:raw boot <kernel> [ <ramdisk> ]    create bootimage and flash it\n"
283dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "  devices                                  list all connected devices\n"
28424ce4bc8d7c4706286ef0530870dea80a1a0cd11Bruce Beare            "  continue                                 continue with autoboot\n"
285dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "  reboot                                   reboot device normally\n"
286dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "  reboot-bootloader                        reboot device into bootloader\n"
287ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang            "  help                                     show this help message\n"
288dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "\n"
289dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "options:\n"
2905ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall            "  -w                                       erase userdata and cache (and format\n"
2915ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall            "                                           if supported by partition type)\n"
2925ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall            "  -u                                       do not first erase partition before\n"
2935ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall            "                                           formatting\n"
29413081c6915220db03886b177f1a8e0b2c63467c9Scott Anderson            "  -s <specific device>                     specify device serial number\n"
29513081c6915220db03886b177f1a8e0b2c63467c9Scott Anderson            "                                           or path to device port\n"
29613081c6915220db03886b177f1a8e0b2c63467c9Scott Anderson            "  -l                                       with \"devices\", lists device paths\n"
297dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "  -p <product>                             specify product name\n"
298dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "  -c <cmdline>                             override kernel commandline\n"
299dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "  -i <vendor id>                           specify a custom USB vendor id\n"
30095ec983887867a117f4ef87073495111aff4a506Dima Zavin            "  -b <base_addr>                           specify a custom kernel base address\n"
301931175a1c5f01f57781c9fcf64beade6ed5148ffDima Zavin            "  -n <page size>                           specify the nand page size. default: 2048\n"
302f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross            "  -S <size>[K|M|G]                         automatically sparse files greater than\n"
3030bbfb3976e9491b3e1a872ebdaa850ef868a24aaColin Cross            "                                           size.  0 to disable\n"
304dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        );
305dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
306dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
307931175a1c5f01f57781c9fcf64beade6ed5148ffDima Zavinvoid *load_bootable_image(unsigned page_size, const char *kernel, const char *ramdisk,
308dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                          unsigned *sz, const char *cmdline)
309dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
310dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    void *kdata = 0, *rdata = 0;
311dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned ksize = 0, rsize = 0;
312dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    void *bdata;
313dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned bsize;
314dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
315dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(kernel == 0) {
316dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr, "no image specified\n");
317dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 0;
318dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
319dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
320dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    kdata = load_file(kernel, &ksize);
321dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(kdata == 0) {
32264ba258b7a17fd5f0abd788c1b021ad45ad732b9Matt Gumbel        fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
323dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 0;
324dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
325ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang
326dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        /* is this actually a boot image? */
327dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
328dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
329ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang
330dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(ramdisk) {
331dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
332dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return 0;
333dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
334ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang
335dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        *sz = ksize;
336dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return kdata;
337dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
338dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
339dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(ramdisk) {
340dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        rdata = load_file(ramdisk, &rsize);
341dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(rdata == 0) {
34264ba258b7a17fd5f0abd788c1b021ad45ad732b9Matt Gumbel            fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
343dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return  0;
344dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
345dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
346dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
347dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fprintf(stderr,"creating boot image...\n");
348931175a1c5f01f57781c9fcf64beade6ed5148ffDima Zavin    bdata = mkbootimg(kdata, ksize, rdata, rsize, 0, 0, page_size, base_addr, &bsize);
349dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(bdata == 0) {
350dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr,"failed to create boot.img\n");
351dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 0;
352dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
353dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
354dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fprintf(stderr,"creating boot image - %d bytes\n", bsize);
355dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    *sz = bsize;
356ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang
357dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return bdata;
358dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
359dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
360dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
361dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
362dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    void *data;
363dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    zipentry_t entry;
364dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned datasz;
365ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang
366dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    entry = lookup_zipentry(zip, name);
367dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (entry == NULL) {
368dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr, "archive does not contain '%s'\n", name);
369dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 0;
370dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
371dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
372dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    *sz = get_zipentry_size(entry);
373dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
374dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    datasz = *sz * 1.001;
375dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    data = malloc(datasz);
376dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
377dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(data == 0) {
378dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr, "failed to allocate %d bytes\n", *sz);
379dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 0;
380dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
381dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
382dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (decompress_zipentry(entry, data, datasz)) {
383dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr, "failed to unzip '%s' from archive\n", name);
384dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        free(data);
385dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 0;
386dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
387dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
388dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return data;
389dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
390dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
391dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic char *strip(char *s)
392dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
393dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int n;
394dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    while(*s && isspace(*s)) s++;
395dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    n = strlen(s);
396dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    while(n-- > 0) {
397dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(!isspace(s[n])) break;
398dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        s[n] = 0;
399dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
400dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return s;
401dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
402dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
403dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define MAX_OPTIONS 32
404dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int setup_requirement_line(char *name)
405dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
406dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *val[MAX_OPTIONS];
407dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    const char **out;
408b98762f7824c291dc5d9a9b757af434ef31d8c81Wink Saville    char *prod = NULL;
409dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned n, count;
410dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *x;
411dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int invert = 0;
412ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang
413dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (!strncmp(name, "reject ", 7)) {
414dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        name += 7;
415dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        invert = 1;
416dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else if (!strncmp(name, "require ", 8)) {
417dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        name += 8;
418dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        invert = 0;
419b98762f7824c291dc5d9a9b757af434ef31d8c81Wink Saville    } else if (!strncmp(name, "require-for-product:", 20)) {
420b98762f7824c291dc5d9a9b757af434ef31d8c81Wink Saville        // Get the product and point name past it
421b98762f7824c291dc5d9a9b757af434ef31d8c81Wink Saville        prod = name + 20;
422b98762f7824c291dc5d9a9b757af434ef31d8c81Wink Saville        name = strchr(name, ' ');
423b98762f7824c291dc5d9a9b757af434ef31d8c81Wink Saville        if (!name) return -1;
424b98762f7824c291dc5d9a9b757af434ef31d8c81Wink Saville        *name = 0;
425b98762f7824c291dc5d9a9b757af434ef31d8c81Wink Saville        name += 1;
426b98762f7824c291dc5d9a9b757af434ef31d8c81Wink Saville        invert = 0;
427dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
428dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
429dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    x = strchr(name, '=');
430dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (x == 0) return 0;
431dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    *x = 0;
432dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    val[0] = x + 1;
433dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
434dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    for(count = 1; count < MAX_OPTIONS; count++) {
435dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        x = strchr(val[count - 1],'|');
436dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (x == 0) break;
437dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        *x = 0;
438dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        val[count] = x + 1;
439dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
440ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang
441dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    name = strip(name);
442dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    for(n = 0; n < count; n++) val[n] = strip(val[n]);
443ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang
444dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    name = strip(name);
445dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (name == 0) return -1;
446dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
447dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        /* work around an unfortunate name mismatch */
448dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (!strcmp(name,"board")) name = "product";
449dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
450dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    out = malloc(sizeof(char*) * count);
451dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (out == 0) return -1;
452dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
453dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    for(n = 0; n < count; n++) {
454dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        out[n] = strdup(strip(val[n]));
455dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (out[n] == 0) return -1;
456dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
457dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
458b98762f7824c291dc5d9a9b757af434ef31d8c81Wink Saville    fb_queue_require(prod, name, invert, n, out);
459dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
460dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
461dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
462dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic void setup_requirements(char *data, unsigned sz)
463dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
464dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *s;
465dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
466dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    s = data;
467dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    while (sz-- > 0) {
468dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(*s == '\n') {
469dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            *s++ = 0;
470dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if (setup_requirement_line(data)) {
471dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                die("out of memory");
472dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
473dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            data = s;
474dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        } else {
475dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            s++;
476dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
477dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
478dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
479dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
480dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid queue_info_dump(void)
481dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
482dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fb_queue_notice("--------------------------------------------");
483dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fb_queue_display("version-bootloader", "Bootloader Version...");
484dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fb_queue_display("version-baseband",   "Baseband Version.....");
485dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fb_queue_display("serialno",           "Serial Number........");
486dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fb_queue_notice("--------------------------------------------");
487dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
488dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
489f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
490f838788e6a4d57634a53eb597ee76a597feffcb5Colin Crossstruct sparse_file **load_sparse_files(const char *fname, int max_size)
491f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross{
492f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    int fd;
493f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    struct sparse_file *s;
494f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    int files;
495f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    struct sparse_file **out_s;
496f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
497f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    fd = open(fname, O_RDONLY | O_BINARY);
498f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    if (fd < 0) {
499f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        die("cannot open '%s'\n", fname);
500f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    }
501f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
502f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    s = sparse_file_import_auto(fd, false);
503f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    if (!s) {
504f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        die("cannot sparse read file '%s'\n", fname);
505f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    }
506f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
507f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    files = sparse_file_resparse(s, max_size, NULL, 0);
508f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    if (files < 0) {
509f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        die("Failed to resparse '%s'\n", fname);
510f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    }
511f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
512f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    out_s = calloc(sizeof(struct sparse_file *), files + 1);
513f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    if (!out_s) {
514f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        die("Failed to allocate sparse file array\n");
515f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    }
516f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
517f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    files = sparse_file_resparse(s, max_size, out_s, files);
518f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    if (files < 0) {
519f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        die("Failed to resparse '%s'\n", fname);
520f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    }
521f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
522f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    return out_s;
523f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross}
524f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
525f838788e6a4d57634a53eb597ee76a597feffcb5Colin Crossstatic int64_t get_target_sparse_limit(struct usb_handle *usb)
526f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross{
527f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    int64_t limit = 0;
528f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    char response[FB_RESPONSE_SZ + 1];
529f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    int status = fb_getvar(usb, response, "max-download-size");
530f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
531f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    if (!status) {
532f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        limit = strtoul(response, NULL, 0);
533f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        if (limit > 0) {
534f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross            fprintf(stderr, "target reported max download size of %lld bytes\n",
535f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross                    limit);
536f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        }
537f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    }
538f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
539f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    return limit;
540f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross}
541f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
542f838788e6a4d57634a53eb597ee76a597feffcb5Colin Crossstatic int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
543f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross{
544f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    int64_t limit;
545f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
546f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    if (sparse_limit == 0) {
547f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        return 0;
548f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    } else if (sparse_limit > 0) {
549f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        limit = sparse_limit;
550f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    } else {
551f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        if (target_sparse_limit == -1) {
552f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross            target_sparse_limit = get_target_sparse_limit(usb);
553f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        }
554f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        if (target_sparse_limit > 0) {
555f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross            limit = target_sparse_limit;
556f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        } else {
5570bbfb3976e9491b3e1a872ebdaa850ef868a24aaColin Cross            return 0;
558f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        }
559f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    }
560f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
561f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    if (size > limit) {
562f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        return limit;
563f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    }
564f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
565f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    return 0;
566f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross}
567f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
5685ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall/* Until we get lazy inode table init working in make_ext4fs, we need to
5695ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall * erase partitions of type ext4 before flashing a filesystem so no stale
5705ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall * inodes are left lying around.  Otherwise, e2fsck gets very upset.
5715ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall */
5725ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrallstatic int needs_erase(const char *part)
5735ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall{
5745ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall    /* The function fb_format_supported() currently returns the value
5755ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall     * we want, so just call it.
5765ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall     */
5775ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall     return fb_format_supported(usb, part);
5785ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall}
5795ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall
580f838788e6a4d57634a53eb597ee76a597feffcb5Colin Crossvoid do_flash(usb_handle *usb, const char *pname, const char *fname)
581f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross{
582f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    int64_t sz64;
583f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    void *data;
584f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    int64_t limit;
585f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
586f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    sz64 = file_size(fname);
587f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    limit = get_sparse_limit(usb, sz64);
588f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    if (limit) {
589f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        struct sparse_file **s = load_sparse_files(fname, limit);
590f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        if (s == NULL) {
591f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross            die("cannot sparse load '%s'\n", fname);
592f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        }
593f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        while (*s) {
594f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross            sz64 = sparse_file_len(*s, true, false);
595f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross            fb_queue_flash_sparse(pname, *s++, sz64);
596f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        }
597f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    } else {
598f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        unsigned int sz;
599f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        data = load_file(fname, &sz);
60064ba258b7a17fd5f0abd788c1b021ad45ad732b9Matt Gumbel        if (data == 0) die("cannot load '%s': %s\n", fname, strerror(errno));
601f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        fb_queue_flash(pname, data, sz);
602f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    }
603f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross}
604f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
605dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid do_update_signature(zipfile_t zip, char *fn)
606dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
607dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    void *data;
608dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned sz;
609dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    data = unzip_file(zip, fn, &sz);
610dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (data == 0) return;
611dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fb_queue_download("signature", data, sz);
612dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fb_queue_command("signature", "installing signature");
613dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
614dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
6155ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrallvoid do_update(char *fn, int erase_first)
616dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
617dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    void *zdata;
618dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned zsize;
619dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    void *data;
620dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned sz;
621dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    zipfile_t zip;
622dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
623dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    queue_info_dump();
624dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
625b98762f7824c291dc5d9a9b757af434ef31d8c81Wink Saville    fb_queue_query_save("product", cur_product, sizeof(cur_product));
626b98762f7824c291dc5d9a9b757af434ef31d8c81Wink Saville
627dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    zdata = load_file(fn, &zsize);
62864ba258b7a17fd5f0abd788c1b021ad45ad732b9Matt Gumbel    if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
629dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
630dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    zip = init_zipfile(zdata, zsize);
631dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(zip == 0) die("failed to access zipdata in '%s'");
632dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
633dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    data = unzip_file(zip, "android-info.txt", &sz);
634dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (data == 0) {
635dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        char *tmp;
636dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            /* fallback for older zipfiles */
637dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        data = unzip_file(zip, "android-product.txt", &sz);
638dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if ((data == 0) || (sz < 1)) {
639dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            die("update package has no android-info.txt or android-product.txt");
640dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
641dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        tmp = malloc(sz + 128);
642dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (tmp == 0) die("out of memory");
643dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
644dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        data = tmp;
645dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        sz = strlen(tmp);
646dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
647dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
648dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    setup_requirements(data, sz);
649dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
650dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    data = unzip_file(zip, "boot.img", &sz);
651dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (data == 0) die("update package missing boot.img");
652dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    do_update_signature(zip, "boot.sig");
6535ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall    if (erase_first && needs_erase("boot")) {
6545ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall        fb_queue_erase("boot");
6555ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall    }
656dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fb_queue_flash("boot", data, sz);
657dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
658dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    data = unzip_file(zip, "recovery.img", &sz);
659dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (data != 0) {
660dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        do_update_signature(zip, "recovery.sig");
6615ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall        if (erase_first && needs_erase("recovery")) {
6625ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall            fb_queue_erase("recovery");
6635ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall        }
664dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fb_queue_flash("recovery", data, sz);
665dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
666dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
667dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    data = unzip_file(zip, "system.img", &sz);
668dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (data == 0) die("update package missing system.img");
669dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    do_update_signature(zip, "system.sig");
6705ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall    if (erase_first && needs_erase("system")) {
6715ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall        fb_queue_erase("system");
6725ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall    }
673dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fb_queue_flash("system", data, sz);
674dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
675dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
676dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid do_send_signature(char *fn)
677dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
678dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    void *data;
679dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned sz;
680dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *xtn;
681ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang
682dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    xtn = strrchr(fn, '.');
683dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (!xtn) return;
684dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (strcmp(xtn, ".img")) return;
685ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang
686dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    strcpy(xtn,".sig");
687dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    data = load_file(fn, &sz);
688dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    strcpy(xtn,".img");
689dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (data == 0) return;
690dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fb_queue_download("signature", data, sz);
691dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fb_queue_command("signature", "installing signature");
692dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
693dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
6945ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrallvoid do_flashall(int erase_first)
695dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
696dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *fname;
697dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    void *data;
698dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned sz;
699dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
700dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    queue_info_dump();
701dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
702b98762f7824c291dc5d9a9b757af434ef31d8c81Wink Saville    fb_queue_query_save("product", cur_product, sizeof(cur_product));
703b98762f7824c291dc5d9a9b757af434ef31d8c81Wink Saville
704dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fname = find_item("info", product);
705dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (fname == 0) die("cannot find android-info.txt");
706dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    data = load_file(fname, &sz);
70764ba258b7a17fd5f0abd788c1b021ad45ad732b9Matt Gumbel    if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
708dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    setup_requirements(data, sz);
709dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
710dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fname = find_item("boot", product);
711dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    data = load_file(fname, &sz);
71264ba258b7a17fd5f0abd788c1b021ad45ad732b9Matt Gumbel    if (data == 0) die("could not load boot.img: %s", strerror(errno));
713dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    do_send_signature(fname);
7145ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall    if (erase_first && needs_erase("boot")) {
7155ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall        fb_queue_erase("boot");
7165ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall    }
717dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fb_queue_flash("boot", data, sz);
718dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
719dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fname = find_item("recovery", product);
720dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    data = load_file(fname, &sz);
721dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (data != 0) {
722dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        do_send_signature(fname);
7235ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall        if (erase_first && needs_erase("recovery")) {
7245ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall            fb_queue_erase("recovery");
7255ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall        }
726dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fb_queue_flash("recovery", data, sz);
727dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
728dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
729dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fname = find_item("system", product);
730dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    data = load_file(fname, &sz);
73164ba258b7a17fd5f0abd788c1b021ad45ad732b9Matt Gumbel    if (data == 0) die("could not load system.img: %s", strerror(errno));
732dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    do_send_signature(fname);
7335ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall    if (erase_first && needs_erase("system")) {
7345ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall        fb_queue_erase("system");
7355ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall    }
736ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang    fb_queue_flash("system", data, sz);
737dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
738dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
739dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define skip(n) do { argc -= (n); argv += (n); } while (0)
7402d13d1408bef9e26cc418e6fc2579dfd12378a3cJP Abgrall#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
741dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
742dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint do_oem_command(int argc, char **argv)
743dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
744dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int i;
745dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char command[256];
746dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (argc <= 1) return 0;
747ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang
748dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    command[0] = 0;
749dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    while(1) {
750dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        strcat(command,*argv);
751dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        skip(1);
752dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(argc == 0) break;
753dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        strcat(command," ");
754dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
755dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
756ee52055ae5eeb951bb4805bd8aa57d63718e1ce7Tsu Chiang Chuang    fb_queue_command(command,"");
757dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
758dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
759dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
760f838788e6a4d57634a53eb597ee76a597feffcb5Colin Crossstatic int64_t parse_num(const char *arg)
761f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross{
762f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    char *endptr;
763f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    unsigned long long num;
764f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
765f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    num = strtoull(arg, &endptr, 0);
766f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    if (endptr == arg) {
767f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        return -1;
768f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    }
769f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
770f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    if (*endptr == 'k' || *endptr == 'K') {
771f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        if (num >= (-1ULL) / 1024) {
772f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross            return -1;
773f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        }
774f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        num *= 1024LL;
775f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        endptr++;
776f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    } else if (*endptr == 'm' || *endptr == 'M') {
777f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        if (num >= (-1ULL) / (1024 * 1024)) {
778f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross            return -1;
779f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        }
780f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        num *= 1024LL * 1024LL;
781f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        endptr++;
782f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    } else if (*endptr == 'g' || *endptr == 'G') {
783f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
784f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross            return -1;
785f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        }
786f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        num *= 1024LL * 1024LL * 1024LL;
787f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        endptr++;
788f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    }
789f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
790f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    if (*endptr != '\0') {
791f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        return -1;
792f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    }
793f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
794f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    if (num > INT64_MAX) {
795f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        return -1;
796f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    }
797f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
798f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    return num;
799f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross}
800f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross
801dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint main(int argc, char **argv)
802dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
803dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int wants_wipe = 0;
804dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int wants_reboot = 0;
805dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int wants_reboot_bootloader = 0;
8065ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall    int erase_first = 1;
807dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    void *data;
808dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned sz;
809931175a1c5f01f57781c9fcf64beade6ed5148ffDima Zavin    unsigned page_size = 2048;
810eb31c0bdc0eac0898f01cdc1737cea94269f8f27Brian Carlstrom    int status;
8118879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross    int c;
812f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    int r;
813dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
814f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross    const struct option longopts = { 0, 0, 0, 0 };
8158879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross
8168879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross    serial = getenv("ANDROID_SERIAL");
8178879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross
8188879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross    while (1) {
8195ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall        c = getopt_long(argc, argv, "wub:n:s:S:lp:c:i:m:h", &longopts, NULL);
8208879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross        if (c < 0) {
8218879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross            break;
8228879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross        }
8238879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross
8248879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross        switch (c) {
8258879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross        case 'w':
8268879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross            wants_wipe = 1;
8278879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross            break;
8285ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall        case 'u':
8295ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall            erase_first = 0;
8305ee5d389d6cb90b3cd2856730f8ff12211bc95d2Ken Sumrall            break;
8318879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross        case 'b':
8328879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross            base_addr = strtoul(optarg, 0, 16);
8338879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross            break;
8348879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross        case 'n':
8358879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross            page_size = (unsigned)strtoul(optarg, NULL, 0);
8368879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross            if (!page_size) die("invalid page size");
8378879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross            break;
8388879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross        case 's':
8398879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross            serial = optarg;
8408879f988bac8d4cb46fb82e3d82ad69a9ed89b16Colin Cross            break;
841f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross        case 'S':
842f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross            sparse_limit = parse_num(optarg);
843f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross            if (sparse_limit < 0) {
844f838788e6a4d57634a53eb597ee76a597feffcb5Colin Cross                    die("invalid sparse limit");
845f838788e6a4d57634a53eb597ee76a597feffcb5Coli