The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 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 Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 12 | * the documentation and/or other materials provided with the |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 13 | * 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 Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | * 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 Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 29 | #define _LARGEFILE64_SOURCE |
| 30 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 33 | #include <stdbool.h> |
| 34 | #include <stdint.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 35 | #include <string.h> |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame^] | 36 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 37 | #include <errno.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <unistd.h> |
| 40 | #include <limits.h> |
| 41 | #include <ctype.h> |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 42 | #include <getopt.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | |
| 44 | #include <sys/time.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 45 | #include <sys/types.h> |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 46 | #include <sys/stat.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 47 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | #include <bootimg.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 49 | #include <sparse/sparse.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 50 | #include <zipfile/zipfile.h> |
| 51 | |
| 52 | #include "fastboot.h" |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 53 | #include "fs.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 55 | #ifndef O_BINARY |
| 56 | #define O_BINARY 0 |
| 57 | #endif |
| 58 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 59 | #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a))) |
| 60 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 61 | char cur_product[FB_RESPONSE_SZ + 1]; |
| 62 | |
Brian Swetland | 2a63bb7 | 2009-04-28 16:05:07 -0700 | [diff] [blame] | 63 | void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline); |
| 64 | |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 65 | boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset, |
| 66 | void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset, |
| 67 | void *second, unsigned second_size, unsigned second_offset, |
| 68 | unsigned page_size, unsigned base, unsigned tags_offset, |
Brian Swetland | 2a63bb7 | 2009-04-28 16:05:07 -0700 | [diff] [blame] | 69 | unsigned *bootimg_size); |
| 70 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 71 | static usb_handle *usb = 0; |
| 72 | static const char *serial = 0; |
| 73 | static const char *product = 0; |
| 74 | static const char *cmdline = 0; |
| 75 | static int wipe_data = 0; |
| 76 | static unsigned short vendor_id = 0; |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 77 | static int long_listing = 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 78 | static int64_t sparse_limit = -1; |
| 79 | static int64_t target_sparse_limit = -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 80 | |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 81 | unsigned page_size = 2048; |
| 82 | unsigned base_addr = 0x10000000; |
| 83 | unsigned kernel_offset = 0x00008000; |
| 84 | unsigned ramdisk_offset = 0x01000000; |
| 85 | unsigned second_offset = 0x00f00000; |
| 86 | unsigned tags_offset = 0x00000100; |
| 87 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 88 | enum fb_buffer_type { |
| 89 | FB_BUFFER, |
| 90 | FB_BUFFER_SPARSE, |
| 91 | }; |
| 92 | |
| 93 | struct fastboot_buffer { |
| 94 | enum fb_buffer_type type; |
| 95 | void *data; |
| 96 | unsigned int sz; |
| 97 | }; |
| 98 | |
| 99 | static struct { |
| 100 | char img_name[13]; |
| 101 | char sig_name[13]; |
| 102 | char part_name[9]; |
| 103 | bool is_optional; |
Daniel Rosenberg | 73a4ad2 | 2014-04-29 13:54:19 -0700 | [diff] [blame] | 104 | } images[4] = { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 105 | {"boot.img", "boot.sig", "boot", false}, |
| 106 | {"recovery.img", "recovery.sig", "recovery", true}, |
| 107 | {"system.img", "system.sig", "system", false}, |
Daniel Rosenberg | 73a4ad2 | 2014-04-29 13:54:19 -0700 | [diff] [blame] | 108 | {"tos.img", "tos.sig", "tos", true}, |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 109 | }; |
Brian Swetland | 2a63bb7 | 2009-04-28 16:05:07 -0700 | [diff] [blame] | 110 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 111 | void get_my_path(char *path); |
| 112 | |
| 113 | char *find_item(const char *item, const char *product) |
| 114 | { |
| 115 | char *dir; |
| 116 | char *fn; |
| 117 | char path[PATH_MAX + 128]; |
| 118 | |
| 119 | if(!strcmp(item,"boot")) { |
| 120 | fn = "boot.img"; |
| 121 | } else if(!strcmp(item,"recovery")) { |
| 122 | fn = "recovery.img"; |
| 123 | } else if(!strcmp(item,"system")) { |
| 124 | fn = "system.img"; |
Daniel Rosenberg | 73a4ad2 | 2014-04-29 13:54:19 -0700 | [diff] [blame] | 125 | } else if(!strcmp(item,"tos")) { |
| 126 | fn = "tos.img"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 127 | } else if(!strcmp(item,"userdata")) { |
| 128 | fn = "userdata.img"; |
Jean-Baptiste Queru | d7608a4 | 2011-09-30 14:39:25 -0700 | [diff] [blame] | 129 | } else if(!strcmp(item,"cache")) { |
| 130 | fn = "cache.img"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 131 | } else if(!strcmp(item,"info")) { |
| 132 | fn = "android-info.txt"; |
| 133 | } else { |
| 134 | fprintf(stderr,"unknown partition '%s'\n", item); |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | if(product) { |
| 139 | get_my_path(path); |
| 140 | sprintf(path + strlen(path), |
| 141 | "../../../target/product/%s/%s", product, fn); |
| 142 | return strdup(path); |
| 143 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 144 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 145 | dir = getenv("ANDROID_PRODUCT_OUT"); |
| 146 | if((dir == 0) || (dir[0] == 0)) { |
| 147 | die("neither -p product specified nor ANDROID_PRODUCT_OUT set"); |
| 148 | return 0; |
| 149 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 150 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 151 | sprintf(path, "%s/%s", dir, fn); |
| 152 | return strdup(path); |
| 153 | } |
| 154 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 155 | static int64_t file_size(int fd) |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 156 | { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 157 | struct stat st; |
| 158 | int ret; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 159 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 160 | ret = fstat(fd, &st); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 161 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 162 | return ret ? -1 : st.st_size; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 165 | static void *load_fd(int fd, unsigned *_sz) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 166 | { |
| 167 | char *data; |
| 168 | int sz; |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 169 | int errno_tmp; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 170 | |
| 171 | data = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 172 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 173 | sz = file_size(fd); |
| 174 | if (sz < 0) { |
| 175 | goto oops; |
| 176 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 177 | |
| 178 | data = (char*) malloc(sz); |
| 179 | if(data == 0) goto oops; |
| 180 | |
| 181 | if(read(fd, data, sz) != sz) goto oops; |
| 182 | close(fd); |
| 183 | |
| 184 | if(_sz) *_sz = sz; |
| 185 | return data; |
| 186 | |
| 187 | oops: |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 188 | errno_tmp = errno; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | close(fd); |
| 190 | if(data != 0) free(data); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 191 | errno = errno_tmp; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 192 | return 0; |
| 193 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 194 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 195 | static void *load_file(const char *fn, unsigned *_sz) |
| 196 | { |
| 197 | int fd; |
| 198 | |
| 199 | fd = open(fn, O_RDONLY | O_BINARY); |
| 200 | if(fd < 0) return 0; |
| 201 | |
| 202 | return load_fd(fd, _sz); |
| 203 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 204 | |
JP Abgrall | a032ded | 2012-06-06 11:53:33 -0700 | [diff] [blame] | 205 | int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial) |
| 206 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 207 | if(!(vendor_id && (info->dev_vendor == vendor_id)) && |
Mike Lockwood | 09070d9 | 2009-08-05 17:04:36 -0400 | [diff] [blame] | 208 | (info->dev_vendor != 0x18d1) && // Google |
Wu, Hao | f60e863 | 2012-01-17 12:04:11 -0800 | [diff] [blame] | 209 | (info->dev_vendor != 0x8087) && // Intel |
The Android Open Source Project | f614d64 | 2009-03-18 17:39:49 -0700 | [diff] [blame] | 210 | (info->dev_vendor != 0x0451) && |
Robert CH Chou | e25ff1c | 2009-09-21 09:51:35 +0800 | [diff] [blame] | 211 | (info->dev_vendor != 0x0502) && |
Dima Zavin | 509f739 | 2010-05-14 14:48:30 -0700 | [diff] [blame] | 212 | (info->dev_vendor != 0x0fce) && // Sony Ericsson |
| 213 | (info->dev_vendor != 0x05c6) && // Qualcomm |
Mike Lockwood | 09070d9 | 2009-08-05 17:04:36 -0400 | [diff] [blame] | 214 | (info->dev_vendor != 0x22b8) && // Motorola |
Erik Gilling | 37e9e90 | 2010-01-20 17:40:05 -0800 | [diff] [blame] | 215 | (info->dev_vendor != 0x0955) && // Nvidia |
Xavier Ducrohet | af82f21 | 2010-01-21 17:39:25 -0800 | [diff] [blame] | 216 | (info->dev_vendor != 0x413c) && // DELL |
Xavier Ducrohet | 746f324 | 2012-01-13 16:03:37 -0800 | [diff] [blame] | 217 | (info->dev_vendor != 0x2314) && // INQ Mobile |
Ramanan Rajeswaran | 73c019b | 2012-02-24 13:00:34 -0800 | [diff] [blame] | 218 | (info->dev_vendor != 0x0b05) && // Asus |
Mike Lockwood | 09070d9 | 2009-08-05 17:04:36 -0400 | [diff] [blame] | 219 | (info->dev_vendor != 0x0bb4)) // HTC |
| 220 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 221 | if(info->ifc_class != 0xff) return -1; |
| 222 | if(info->ifc_subclass != 0x42) return -1; |
| 223 | if(info->ifc_protocol != 0x03) return -1; |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 224 | // require matching serial number or device path if requested |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 225 | // at the command line with the -s option. |
JP Abgrall | a032ded | 2012-06-06 11:53:33 -0700 | [diff] [blame] | 226 | if (local_serial && (strcmp(local_serial, info->serial_number) != 0 && |
| 227 | strcmp(local_serial, info->device_path) != 0)) return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 231 | int match_fastboot(usb_ifc_info *info) |
| 232 | { |
| 233 | return match_fastboot_with_serial(info, serial); |
| 234 | } |
| 235 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 236 | int list_devices_callback(usb_ifc_info *info) |
| 237 | { |
JP Abgrall | a032ded | 2012-06-06 11:53:33 -0700 | [diff] [blame] | 238 | if (match_fastboot_with_serial(info, NULL) == 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 239 | char* serial = info->serial_number; |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 240 | if (!info->writable) { |
| 241 | serial = "no permissions"; // like "adb devices" |
| 242 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 243 | if (!serial[0]) { |
| 244 | serial = "????????????"; |
| 245 | } |
Scott Anderson | 866b1bd | 2012-06-04 20:29:11 -0700 | [diff] [blame] | 246 | // output compatible with "adb devices" |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 247 | if (!long_listing) { |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 248 | printf("%s\tfastboot\n", serial); |
Scott Anderson | 866b1bd | 2012-06-04 20:29:11 -0700 | [diff] [blame] | 249 | } else if (!info->device_path) { |
| 250 | printf("%-22s fastboot\n", serial); |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 251 | } else { |
Scott Anderson | 866b1bd | 2012-06-04 20:29:11 -0700 | [diff] [blame] | 252 | printf("%-22s fastboot %s\n", serial, info->device_path); |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 253 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | usb_handle *open_device(void) |
| 260 | { |
| 261 | static usb_handle *usb = 0; |
| 262 | int announce = 1; |
| 263 | |
| 264 | if(usb) return usb; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 265 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 266 | for(;;) { |
| 267 | usb = usb_open(match_fastboot); |
| 268 | if(usb) return usb; |
| 269 | if(announce) { |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 270 | announce = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 271 | fprintf(stderr,"< waiting for device >\n"); |
| 272 | } |
| 273 | sleep(1); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | void list_devices(void) { |
| 278 | // We don't actually open a USB device here, |
| 279 | // just getting our callback called so we can |
| 280 | // list all the connected devices. |
| 281 | usb_open(list_devices_callback); |
| 282 | } |
| 283 | |
| 284 | void usage(void) |
| 285 | { |
| 286 | fprintf(stderr, |
| 287 | /* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */ |
| 288 | "usage: fastboot [ <option> ] <command>\n" |
| 289 | "\n" |
| 290 | "commands:\n" |
| 291 | " update <filename> reflash device from update.zip\n" |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame^] | 292 | " flashall flash boot, system, and if found,\n" |
| 293 | " recovery, tos\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 294 | " flash <partition> [ <filename> ] write a file to a flash partition\n" |
| 295 | " erase <partition> erase a flash partition\n" |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame^] | 296 | " format[:[<fs type>][:[<size>]] <partition> format a flash partition.\n" |
| 297 | " Can override the fs type and/or\n" |
| 298 | " size the bootloader reports.\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 299 | " getvar <variable> display a bootloader variable\n" |
| 300 | " boot <kernel> [ <ramdisk> ] download and boot kernel\n" |
| 301 | " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n" |
| 302 | " devices list all connected devices\n" |
Bruce Beare | 24ce4bc | 2010-10-14 09:43:26 -0700 | [diff] [blame] | 303 | " continue continue with autoboot\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 304 | " reboot reboot device normally\n" |
| 305 | " reboot-bootloader reboot device into bootloader\n" |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 306 | " help show this help message\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 307 | "\n" |
| 308 | "options:\n" |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 309 | " -w erase userdata and cache (and format\n" |
| 310 | " if supported by partition type)\n" |
| 311 | " -u do not first erase partition before\n" |
| 312 | " formatting\n" |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 313 | " -s <specific device> specify device serial number\n" |
| 314 | " or path to device port\n" |
| 315 | " -l with \"devices\", lists device paths\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 316 | " -p <product> specify product name\n" |
| 317 | " -c <cmdline> override kernel commandline\n" |
| 318 | " -i <vendor id> specify a custom USB vendor id\n" |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame^] | 319 | " -b <base_addr> specify a custom kernel base address.\n" |
| 320 | " default: 0x10000000\n" |
| 321 | " -n <page size> specify the nand page size.\n" |
| 322 | " default: 2048\n" |
| 323 | " -S <size>[K|M|G] automatically sparse files greater\n" |
| 324 | " than size. 0 to disable\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 325 | ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 326 | } |
| 327 | |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 328 | void *load_bootable_image(const char *kernel, const char *ramdisk, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 329 | unsigned *sz, const char *cmdline) |
| 330 | { |
| 331 | void *kdata = 0, *rdata = 0; |
| 332 | unsigned ksize = 0, rsize = 0; |
| 333 | void *bdata; |
| 334 | unsigned bsize; |
| 335 | |
| 336 | if(kernel == 0) { |
| 337 | fprintf(stderr, "no image specified\n"); |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | kdata = load_file(kernel, &ksize); |
| 342 | if(kdata == 0) { |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 343 | fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 344 | return 0; |
| 345 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 346 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 347 | /* is this actually a boot image? */ |
| 348 | if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 349 | if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 350 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 351 | if(ramdisk) { |
| 352 | fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n"); |
| 353 | return 0; |
| 354 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 355 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 356 | *sz = ksize; |
| 357 | return kdata; |
| 358 | } |
| 359 | |
| 360 | if(ramdisk) { |
| 361 | rdata = load_file(ramdisk, &rsize); |
| 362 | if(rdata == 0) { |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 363 | fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | fprintf(stderr,"creating boot image...\n"); |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 369 | bdata = mkbootimg(kdata, ksize, kernel_offset, |
| 370 | rdata, rsize, ramdisk_offset, |
| 371 | 0, 0, second_offset, |
| 372 | page_size, base_addr, tags_offset, &bsize); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 373 | if(bdata == 0) { |
| 374 | fprintf(stderr,"failed to create boot.img\n"); |
| 375 | return 0; |
| 376 | } |
| 377 | if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline); |
| 378 | fprintf(stderr,"creating boot image - %d bytes\n", bsize); |
| 379 | *sz = bsize; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 380 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 381 | return bdata; |
| 382 | } |
| 383 | |
| 384 | void *unzip_file(zipfile_t zip, const char *name, unsigned *sz) |
| 385 | { |
| 386 | void *data; |
| 387 | zipentry_t entry; |
| 388 | unsigned datasz; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 389 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 390 | entry = lookup_zipentry(zip, name); |
| 391 | if (entry == NULL) { |
| 392 | fprintf(stderr, "archive does not contain '%s'\n", name); |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | *sz = get_zipentry_size(entry); |
| 397 | |
| 398 | datasz = *sz * 1.001; |
| 399 | data = malloc(datasz); |
| 400 | |
| 401 | if(data == 0) { |
| 402 | fprintf(stderr, "failed to allocate %d bytes\n", *sz); |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | if (decompress_zipentry(entry, data, datasz)) { |
| 407 | fprintf(stderr, "failed to unzip '%s' from archive\n", name); |
| 408 | free(data); |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | return data; |
| 413 | } |
| 414 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 415 | static int unzip_to_file(zipfile_t zip, char *name) |
| 416 | { |
| 417 | int fd; |
| 418 | char *data; |
| 419 | unsigned sz; |
| 420 | |
| 421 | fd = fileno(tmpfile()); |
| 422 | if (fd < 0) { |
| 423 | return -1; |
| 424 | } |
| 425 | |
| 426 | data = unzip_file(zip, name, &sz); |
| 427 | if (data == 0) { |
| 428 | return -1; |
| 429 | } |
| 430 | |
| 431 | if (write(fd, data, sz) != sz) { |
| 432 | fd = -1; |
| 433 | } |
| 434 | |
| 435 | free(data); |
| 436 | lseek(fd, 0, SEEK_SET); |
| 437 | return fd; |
| 438 | } |
| 439 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 440 | static char *strip(char *s) |
| 441 | { |
| 442 | int n; |
| 443 | while(*s && isspace(*s)) s++; |
| 444 | n = strlen(s); |
| 445 | while(n-- > 0) { |
| 446 | if(!isspace(s[n])) break; |
| 447 | s[n] = 0; |
| 448 | } |
| 449 | return s; |
| 450 | } |
| 451 | |
| 452 | #define MAX_OPTIONS 32 |
| 453 | static int setup_requirement_line(char *name) |
| 454 | { |
| 455 | char *val[MAX_OPTIONS]; |
| 456 | const char **out; |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 457 | char *prod = NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 458 | unsigned n, count; |
| 459 | char *x; |
| 460 | int invert = 0; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 461 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 462 | if (!strncmp(name, "reject ", 7)) { |
| 463 | name += 7; |
| 464 | invert = 1; |
| 465 | } else if (!strncmp(name, "require ", 8)) { |
| 466 | name += 8; |
| 467 | invert = 0; |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 468 | } else if (!strncmp(name, "require-for-product:", 20)) { |
| 469 | // Get the product and point name past it |
| 470 | prod = name + 20; |
| 471 | name = strchr(name, ' '); |
| 472 | if (!name) return -1; |
| 473 | *name = 0; |
| 474 | name += 1; |
| 475 | invert = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | x = strchr(name, '='); |
| 479 | if (x == 0) return 0; |
| 480 | *x = 0; |
| 481 | val[0] = x + 1; |
| 482 | |
| 483 | for(count = 1; count < MAX_OPTIONS; count++) { |
| 484 | x = strchr(val[count - 1],'|'); |
| 485 | if (x == 0) break; |
| 486 | *x = 0; |
| 487 | val[count] = x + 1; |
| 488 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 489 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 490 | name = strip(name); |
| 491 | for(n = 0; n < count; n++) val[n] = strip(val[n]); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 492 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 493 | name = strip(name); |
| 494 | if (name == 0) return -1; |
| 495 | |
| 496 | /* work around an unfortunate name mismatch */ |
| 497 | if (!strcmp(name,"board")) name = "product"; |
| 498 | |
| 499 | out = malloc(sizeof(char*) * count); |
| 500 | if (out == 0) return -1; |
| 501 | |
| 502 | for(n = 0; n < count; n++) { |
| 503 | out[n] = strdup(strip(val[n])); |
Elliott Hughes | 14e28d3 | 2013-10-29 14:12:46 -0700 | [diff] [blame] | 504 | if (out[n] == 0) { |
| 505 | for(size_t i = 0; i < n; ++i) { |
| 506 | free((char*) out[i]); |
| 507 | } |
| 508 | free(out); |
| 509 | return -1; |
| 510 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 511 | } |
| 512 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 513 | fb_queue_require(prod, name, invert, n, out); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static void setup_requirements(char *data, unsigned sz) |
| 518 | { |
| 519 | char *s; |
| 520 | |
| 521 | s = data; |
| 522 | while (sz-- > 0) { |
| 523 | if(*s == '\n') { |
| 524 | *s++ = 0; |
| 525 | if (setup_requirement_line(data)) { |
| 526 | die("out of memory"); |
| 527 | } |
| 528 | data = s; |
| 529 | } else { |
| 530 | s++; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | void queue_info_dump(void) |
| 536 | { |
| 537 | fb_queue_notice("--------------------------------------------"); |
| 538 | fb_queue_display("version-bootloader", "Bootloader Version..."); |
| 539 | fb_queue_display("version-baseband", "Baseband Version....."); |
| 540 | fb_queue_display("serialno", "Serial Number........"); |
| 541 | fb_queue_notice("--------------------------------------------"); |
| 542 | } |
| 543 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 544 | static struct sparse_file **load_sparse_files(int fd, int max_size) |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 545 | { |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 546 | struct sparse_file *s; |
| 547 | int files; |
| 548 | struct sparse_file **out_s; |
| 549 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 550 | s = sparse_file_import_auto(fd, false); |
| 551 | if (!s) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 552 | die("cannot sparse read file\n"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | files = sparse_file_resparse(s, max_size, NULL, 0); |
| 556 | if (files < 0) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 557 | die("Failed to resparse\n"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | out_s = calloc(sizeof(struct sparse_file *), files + 1); |
| 561 | if (!out_s) { |
| 562 | die("Failed to allocate sparse file array\n"); |
| 563 | } |
| 564 | |
| 565 | files = sparse_file_resparse(s, max_size, out_s, files); |
| 566 | if (files < 0) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 567 | die("Failed to resparse\n"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | return out_s; |
| 571 | } |
| 572 | |
| 573 | static int64_t get_target_sparse_limit(struct usb_handle *usb) |
| 574 | { |
| 575 | int64_t limit = 0; |
| 576 | char response[FB_RESPONSE_SZ + 1]; |
| 577 | int status = fb_getvar(usb, response, "max-download-size"); |
| 578 | |
| 579 | if (!status) { |
| 580 | limit = strtoul(response, NULL, 0); |
| 581 | if (limit > 0) { |
| 582 | fprintf(stderr, "target reported max download size of %lld bytes\n", |
| 583 | limit); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | return limit; |
| 588 | } |
| 589 | |
| 590 | static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size) |
| 591 | { |
| 592 | int64_t limit; |
| 593 | |
| 594 | if (sparse_limit == 0) { |
| 595 | return 0; |
| 596 | } else if (sparse_limit > 0) { |
| 597 | limit = sparse_limit; |
| 598 | } else { |
| 599 | if (target_sparse_limit == -1) { |
| 600 | target_sparse_limit = get_target_sparse_limit(usb); |
| 601 | } |
| 602 | if (target_sparse_limit > 0) { |
| 603 | limit = target_sparse_limit; |
| 604 | } else { |
Colin Cross | 0bbfb39 | 2012-07-24 18:05:21 -0700 | [diff] [blame] | 605 | return 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
| 609 | if (size > limit) { |
| 610 | return limit; |
| 611 | } |
| 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 616 | /* Until we get lazy inode table init working in make_ext4fs, we need to |
| 617 | * erase partitions of type ext4 before flashing a filesystem so no stale |
| 618 | * inodes are left lying around. Otherwise, e2fsck gets very upset. |
| 619 | */ |
| 620 | static int needs_erase(const char *part) |
| 621 | { |
| 622 | /* The function fb_format_supported() currently returns the value |
| 623 | * we want, so just call it. |
| 624 | */ |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame^] | 625 | return fb_format_supported(usb, part, NULL); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 628 | static int load_buf_fd(usb_handle *usb, int fd, |
| 629 | struct fastboot_buffer *buf) |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 630 | { |
| 631 | int64_t sz64; |
| 632 | void *data; |
| 633 | int64_t limit; |
| 634 | |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 635 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 636 | sz64 = file_size(fd); |
| 637 | if (sz64 < 0) { |
| 638 | return -1; |
| 639 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 640 | |
| 641 | lseek(fd, 0, SEEK_SET); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 642 | limit = get_sparse_limit(usb, sz64); |
| 643 | if (limit) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 644 | struct sparse_file **s = load_sparse_files(fd, limit); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 645 | if (s == NULL) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 646 | return -1; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 647 | } |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 648 | buf->type = FB_BUFFER_SPARSE; |
| 649 | buf->data = s; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 650 | } else { |
| 651 | unsigned int sz; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 652 | data = load_fd(fd, &sz); |
| 653 | if (data == 0) return -1; |
| 654 | buf->type = FB_BUFFER; |
| 655 | buf->data = data; |
| 656 | buf->sz = sz; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 657 | } |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 658 | |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | static int load_buf(usb_handle *usb, const char *fname, |
| 663 | struct fastboot_buffer *buf) |
| 664 | { |
| 665 | int fd; |
| 666 | |
| 667 | fd = open(fname, O_RDONLY | O_BINARY); |
| 668 | if (fd < 0) { |
Daniel Rosenberg | 8228059 | 2014-04-29 13:45:05 -0700 | [diff] [blame] | 669 | return -1; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | return load_buf_fd(usb, fd, buf); |
| 673 | } |
| 674 | |
| 675 | static void flash_buf(const char *pname, struct fastboot_buffer *buf) |
| 676 | { |
| 677 | struct sparse_file **s; |
| 678 | |
| 679 | switch (buf->type) { |
| 680 | case FB_BUFFER_SPARSE: |
| 681 | s = buf->data; |
| 682 | while (*s) { |
| 683 | int64_t sz64 = sparse_file_len(*s, true, false); |
| 684 | fb_queue_flash_sparse(pname, *s++, sz64); |
| 685 | } |
| 686 | break; |
| 687 | case FB_BUFFER: |
| 688 | fb_queue_flash(pname, buf->data, buf->sz); |
| 689 | break; |
| 690 | default: |
| 691 | die("unknown buffer type: %d", buf->type); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | void do_flash(usb_handle *usb, const char *pname, const char *fname) |
| 696 | { |
| 697 | struct fastboot_buffer buf; |
| 698 | |
| 699 | if (load_buf(usb, fname, &buf)) { |
| 700 | die("cannot load '%s'", fname); |
| 701 | } |
| 702 | flash_buf(pname, &buf); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 703 | } |
| 704 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 705 | void do_update_signature(zipfile_t zip, char *fn) |
| 706 | { |
| 707 | void *data; |
| 708 | unsigned sz; |
| 709 | data = unzip_file(zip, fn, &sz); |
| 710 | if (data == 0) return; |
| 711 | fb_queue_download("signature", data, sz); |
| 712 | fb_queue_command("signature", "installing signature"); |
| 713 | } |
| 714 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 715 | void do_update(usb_handle *usb, char *fn, int erase_first) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 716 | { |
| 717 | void *zdata; |
| 718 | unsigned zsize; |
| 719 | void *data; |
| 720 | unsigned sz; |
| 721 | zipfile_t zip; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 722 | int fd; |
| 723 | int rc; |
| 724 | struct fastboot_buffer buf; |
| 725 | int i; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 726 | |
| 727 | queue_info_dump(); |
| 728 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 729 | fb_queue_query_save("product", cur_product, sizeof(cur_product)); |
| 730 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 731 | zdata = load_file(fn, &zsize); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 732 | if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 733 | |
| 734 | zip = init_zipfile(zdata, zsize); |
| 735 | if(zip == 0) die("failed to access zipdata in '%s'"); |
| 736 | |
| 737 | data = unzip_file(zip, "android-info.txt", &sz); |
| 738 | if (data == 0) { |
| 739 | char *tmp; |
| 740 | /* fallback for older zipfiles */ |
| 741 | data = unzip_file(zip, "android-product.txt", &sz); |
| 742 | if ((data == 0) || (sz < 1)) { |
| 743 | die("update package has no android-info.txt or android-product.txt"); |
| 744 | } |
| 745 | tmp = malloc(sz + 128); |
| 746 | if (tmp == 0) die("out of memory"); |
| 747 | sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data); |
| 748 | data = tmp; |
| 749 | sz = strlen(tmp); |
| 750 | } |
| 751 | |
| 752 | setup_requirements(data, sz); |
| 753 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 754 | for (i = 0; i < ARRAY_SIZE(images); i++) { |
| 755 | fd = unzip_to_file(zip, images[i].img_name); |
| 756 | if (fd < 0) { |
| 757 | if (images[i].is_optional) |
| 758 | continue; |
| 759 | die("update package missing %s", images[i].img_name); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 760 | } |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 761 | rc = load_buf_fd(usb, fd, &buf); |
| 762 | if (rc) die("cannot load %s from flash", images[i].img_name); |
| 763 | do_update_signature(zip, images[i].sig_name); |
| 764 | if (erase_first && needs_erase(images[i].part_name)) { |
| 765 | fb_queue_erase(images[i].part_name); |
| 766 | } |
| 767 | flash_buf(images[i].part_name, &buf); |
| 768 | /* not closing the fd here since the sparse code keeps the fd around |
| 769 | * but hasn't mmaped data yet. The tmpfile will get cleaned up when the |
| 770 | * program exits. |
| 771 | */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 772 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | void do_send_signature(char *fn) |
| 776 | { |
| 777 | void *data; |
| 778 | unsigned sz; |
| 779 | char *xtn; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 780 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 781 | xtn = strrchr(fn, '.'); |
| 782 | if (!xtn) return; |
| 783 | if (strcmp(xtn, ".img")) return; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 784 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 785 | strcpy(xtn,".sig"); |
| 786 | data = load_file(fn, &sz); |
| 787 | strcpy(xtn,".img"); |
| 788 | if (data == 0) return; |
| 789 | fb_queue_download("signature", data, sz); |
| 790 | fb_queue_command("signature", "installing signature"); |
| 791 | } |
| 792 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 793 | void do_flashall(usb_handle *usb, int erase_first) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 794 | { |
| 795 | char *fname; |
| 796 | void *data; |
| 797 | unsigned sz; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 798 | struct fastboot_buffer buf; |
| 799 | int i; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 800 | |
| 801 | queue_info_dump(); |
| 802 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 803 | fb_queue_query_save("product", cur_product, sizeof(cur_product)); |
| 804 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 805 | fname = find_item("info", product); |
| 806 | if (fname == 0) die("cannot find android-info.txt"); |
| 807 | data = load_file(fname, &sz); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 808 | if (data == 0) die("could not load android-info.txt: %s", strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 809 | setup_requirements(data, sz); |
| 810 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 811 | for (i = 0; i < ARRAY_SIZE(images); i++) { |
| 812 | fname = find_item(images[i].part_name, product); |
| 813 | if (load_buf(usb, fname, &buf)) { |
| 814 | if (images[i].is_optional) |
| 815 | continue; |
| 816 | die("could not load %s\n", images[i].img_name); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 817 | } |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 818 | do_send_signature(fname); |
| 819 | if (erase_first && needs_erase(images[i].part_name)) { |
| 820 | fb_queue_erase(images[i].part_name); |
| 821 | } |
| 822 | flash_buf(images[i].part_name, &buf); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 823 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | #define skip(n) do { argc -= (n); argv += (n); } while (0) |
JP Abgrall | 2d13d14 | 2011-03-01 23:35:07 -0800 | [diff] [blame] | 827 | #define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 828 | |
| 829 | int do_oem_command(int argc, char **argv) |
| 830 | { |
| 831 | int i; |
| 832 | char command[256]; |
| 833 | if (argc <= 1) return 0; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 834 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 835 | command[0] = 0; |
| 836 | while(1) { |
| 837 | strcat(command,*argv); |
| 838 | skip(1); |
| 839 | if(argc == 0) break; |
| 840 | strcat(command," "); |
| 841 | } |
| 842 | |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 843 | fb_queue_command(command,""); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 844 | return 0; |
| 845 | } |
| 846 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 847 | static int64_t parse_num(const char *arg) |
| 848 | { |
| 849 | char *endptr; |
| 850 | unsigned long long num; |
| 851 | |
| 852 | num = strtoull(arg, &endptr, 0); |
| 853 | if (endptr == arg) { |
| 854 | return -1; |
| 855 | } |
| 856 | |
| 857 | if (*endptr == 'k' || *endptr == 'K') { |
| 858 | if (num >= (-1ULL) / 1024) { |
| 859 | return -1; |
| 860 | } |
| 861 | num *= 1024LL; |
| 862 | endptr++; |
| 863 | } else if (*endptr == 'm' || *endptr == 'M') { |
| 864 | if (num >= (-1ULL) / (1024 * 1024)) { |
| 865 | return -1; |
| 866 | } |
| 867 | num *= 1024LL * 1024LL; |
| 868 | endptr++; |
| 869 | } else if (*endptr == 'g' || *endptr == 'G') { |
| 870 | if (num >= (-1ULL) / (1024 * 1024 * 1024)) { |
| 871 | return -1; |
| 872 | } |
| 873 | num *= 1024LL * 1024LL * 1024LL; |
| 874 | endptr++; |
| 875 | } |
| 876 | |
| 877 | if (*endptr != '\0') { |
| 878 | return -1; |
| 879 | } |
| 880 | |
| 881 | if (num > INT64_MAX) { |
| 882 | return -1; |
| 883 | } |
| 884 | |
| 885 | return num; |
| 886 | } |
| 887 | |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame^] | 888 | void fb_perform_format(const char *partition, int skip_if_not_supported, |
| 889 | const char *type_override, const char *size_override) |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 890 | { |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame^] | 891 | char pTypeBuff[FB_RESPONSE_SZ + 1], pSizeBuff[FB_RESPONSE_SZ + 1]; |
| 892 | char *pType = pTypeBuff; |
| 893 | char *pSize = pSizeBuff; |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 894 | unsigned int limit = INT_MAX; |
| 895 | struct fastboot_buffer buf; |
| 896 | const char *errMsg = NULL; |
| 897 | const struct fs_generator *gen; |
| 898 | uint64_t pSz; |
| 899 | int status; |
| 900 | int fd; |
| 901 | |
| 902 | if (target_sparse_limit > 0 && target_sparse_limit < limit) |
| 903 | limit = target_sparse_limit; |
| 904 | if (sparse_limit > 0 && sparse_limit < limit) |
| 905 | limit = sparse_limit; |
| 906 | |
| 907 | status = fb_getvar(usb, pType, "partition-type:%s", partition); |
| 908 | if (status) { |
| 909 | errMsg = "Can't determine partition type.\n"; |
| 910 | goto failed; |
| 911 | } |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame^] | 912 | if (type_override) { |
| 913 | if (strcmp(type_override, pType)) { |
| 914 | fprintf(stderr, |
| 915 | "Warning: %s type is %s, but %s was requested for formating.\n", |
| 916 | partition, pType, type_override); |
| 917 | } |
| 918 | pType = type_override; |
| 919 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 920 | |
| 921 | status = fb_getvar(usb, pSize, "partition-size:%s", partition); |
| 922 | if (status) { |
| 923 | errMsg = "Unable to get partition size\n"; |
| 924 | goto failed; |
| 925 | } |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame^] | 926 | if (size_override) { |
| 927 | if (strcmp(size_override, pSize)) { |
| 928 | fprintf(stderr, |
| 929 | "Warning: %s size is %s, but %s was requested for formating.\n", |
| 930 | partition, pSize, size_override); |
| 931 | } |
| 932 | pSize = size_override; |
| 933 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 934 | |
| 935 | gen = fs_get_generator(pType); |
| 936 | if (!gen) { |
| 937 | if (skip_if_not_supported) { |
| 938 | fprintf(stderr, "Erase successful, but not automatically formatting.\n"); |
| 939 | fprintf(stderr, "File system type %s not supported.\n", pType); |
| 940 | return; |
| 941 | } |
| 942 | fprintf(stderr, "Formatting is not supported for filesystem with type '%s'.\n", pType); |
| 943 | return; |
| 944 | } |
| 945 | |
| 946 | pSz = strtoll(pSize, (char **)NULL, 16); |
| 947 | |
| 948 | fd = fileno(tmpfile()); |
| 949 | if (fs_generator_generate(gen, fd, pSz)) { |
| 950 | close(fd); |
| 951 | fprintf(stderr, "Cannot generate image.\n"); |
| 952 | return; |
| 953 | } |
| 954 | |
| 955 | if (load_buf_fd(usb, fd, &buf)) { |
| 956 | fprintf(stderr, "Cannot read image.\n"); |
| 957 | close(fd); |
| 958 | return; |
| 959 | } |
| 960 | flash_buf(partition, &buf); |
| 961 | |
| 962 | return; |
| 963 | |
| 964 | |
| 965 | failed: |
| 966 | if (skip_if_not_supported) { |
| 967 | fprintf(stderr, "Erase successful, but not automatically formatting.\n"); |
| 968 | if (errMsg) |
| 969 | fprintf(stderr, "%s", errMsg); |
| 970 | } |
| 971 | fprintf(stderr,"FAILED (%s)\n", fb_get_error()); |
| 972 | } |
| 973 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 974 | int main(int argc, char **argv) |
| 975 | { |
| 976 | int wants_wipe = 0; |
| 977 | int wants_reboot = 0; |
| 978 | int wants_reboot_bootloader = 0; |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 979 | int erase_first = 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 980 | void *data; |
| 981 | unsigned sz; |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 982 | int status; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 983 | int c; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 984 | int r; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 985 | |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 986 | const struct option longopts[] = { |
| 987 | {"base", required_argument, 0, 'b'}, |
| 988 | {"kernel_offset", required_argument, 0, 'k'}, |
| 989 | {"page_size", required_argument, 0, 'n'}, |
| 990 | {"ramdisk_offset", required_argument, 0, 'r'}, |
Mohamad Ayyash | 29fd7b1 | 2014-01-14 18:27:25 -0800 | [diff] [blame] | 991 | {"tags_offset", required_argument, 0, 't'}, |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 992 | {"help", 0, 0, 'h'}, |
| 993 | {0, 0, 0, 0} |
| 994 | }; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 995 | |
| 996 | serial = getenv("ANDROID_SERIAL"); |
| 997 | |
| 998 | while (1) { |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 999 | int option_index = 0; |
Mohamad Ayyash | 29fd7b1 | 2014-01-14 18:27:25 -0800 | [diff] [blame] | 1000 | c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:h", longopts, NULL); |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1001 | if (c < 0) { |
| 1002 | break; |
| 1003 | } |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1004 | /* Alphabetical cases */ |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1005 | switch (c) { |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1006 | case 'b': |
| 1007 | base_addr = strtoul(optarg, 0, 16); |
| 1008 | break; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1009 | case 'c': |
| 1010 | cmdline = optarg; |
| 1011 | break; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1012 | case 'h': |
| 1013 | usage(); |
| 1014 | return 1; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1015 | case 'i': { |
| 1016 | char *endptr = NULL; |
| 1017 | unsigned long val; |
| 1018 | |
| 1019 | val = strtoul(optarg, &endptr, 0); |
| 1020 | if (!endptr || *endptr != '\0' || (val & ~0xffff)) |
| 1021 | die("invalid vendor id '%s'", optarg); |
| 1022 | vendor_id = (unsigned short)val; |
| 1023 | break; |
| 1024 | } |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1025 | case 'k': |
| 1026 | kernel_offset = strtoul(optarg, 0, 16); |
| 1027 | break; |
| 1028 | case 'l': |
| 1029 | long_listing = 1; |
| 1030 | break; |
| 1031 | case 'n': |
| 1032 | page_size = (unsigned)strtoul(optarg, NULL, 0); |
| 1033 | if (!page_size) die("invalid page size"); |
| 1034 | break; |
| 1035 | case 'p': |
| 1036 | product = optarg; |
| 1037 | break; |
| 1038 | case 'r': |
| 1039 | ramdisk_offset = strtoul(optarg, 0, 16); |
| 1040 | break; |
Mohamad Ayyash | 29fd7b1 | 2014-01-14 18:27:25 -0800 | [diff] [blame] | 1041 | case 't': |
| 1042 | tags_offset = strtoul(optarg, 0, 16); |
| 1043 | break; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1044 | case 's': |
| 1045 | serial = optarg; |
| 1046 | break; |
| 1047 | case 'S': |
| 1048 | sparse_limit = parse_num(optarg); |
| 1049 | if (sparse_limit < 0) { |
| 1050 | die("invalid sparse limit"); |
| 1051 | } |
| 1052 | break; |
| 1053 | case 'u': |
| 1054 | erase_first = 0; |
| 1055 | break; |
| 1056 | case 'w': |
| 1057 | wants_wipe = 1; |
| 1058 | break; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1059 | case '?': |
| 1060 | return 1; |
| 1061 | default: |
| 1062 | abort(); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | argc -= optind; |
| 1067 | argv += optind; |
| 1068 | |
| 1069 | if (argc == 0 && !wants_wipe) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1070 | usage(); |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 1071 | return 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
Colin Cross | 8fb6e06 | 2012-07-24 16:36:41 -0700 | [diff] [blame] | 1074 | if (argc > 0 && !strcmp(*argv, "devices")) { |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1075 | skip(1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1076 | list_devices(); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 1077 | return 0; |
| 1078 | } |
| 1079 | |
Colin Cross | c7b75dc | 2012-08-29 18:17:06 -0700 | [diff] [blame] | 1080 | if (argc > 0 && !strcmp(*argv, "help")) { |
| 1081 | usage(); |
| 1082 | return 0; |
| 1083 | } |
| 1084 | |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1085 | usb = open_device(); |
Elliott Hughes | 31dbed7 | 2009-10-07 15:38:53 -0700 | [diff] [blame] | 1086 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1087 | while (argc > 0) { |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1088 | if(!strcmp(*argv, "getvar")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1089 | require(2); |
| 1090 | fb_queue_display(argv[1], argv[1]); |
| 1091 | skip(2); |
| 1092 | } else if(!strcmp(*argv, "erase")) { |
| 1093 | require(2); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1094 | |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame^] | 1095 | if (fb_format_supported(usb, argv[1], NULL)) { |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1096 | fprintf(stderr, "******** Did you mean to fastboot format this partition?\n"); |
| 1097 | } |
| 1098 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1099 | fb_queue_erase(argv[1]); |
| 1100 | skip(2); |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame^] | 1101 | } else if(!strncmp(*argv, "format", strlen("format"))) { |
| 1102 | char *overrides; |
| 1103 | char *type_override = NULL; |
| 1104 | char *size_override = NULL; |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 1105 | require(2); |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame^] | 1106 | /* |
| 1107 | * Parsing for: "format[:[type][:[size]]]" |
| 1108 | * Some valid things: |
| 1109 | * - select ontly the size, and leave default fs type: |
| 1110 | * format::0x4000000 userdata |
| 1111 | * - default fs type and size: |
| 1112 | * format userdata |
| 1113 | * format:: userdata |
| 1114 | */ |
| 1115 | overrides = strchr(*argv, ':'); |
| 1116 | if (overrides) { |
| 1117 | overrides++; |
| 1118 | size_override = strchr(overrides, ':'); |
| 1119 | if (size_override) { |
| 1120 | size_override[0] = '\0'; |
| 1121 | size_override++; |
| 1122 | } |
| 1123 | type_override = overrides; |
| 1124 | } |
| 1125 | if (type_override && !type_override[0]) type_override = NULL; |
| 1126 | if (size_override && !size_override[0]) size_override = NULL; |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1127 | if (erase_first && needs_erase(argv[1])) { |
| 1128 | fb_queue_erase(argv[1]); |
| 1129 | } |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame^] | 1130 | fb_perform_format(argv[1], 0, type_override, size_override); |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 1131 | skip(2); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1132 | } else if(!strcmp(*argv, "signature")) { |
| 1133 | require(2); |
| 1134 | data = load_file(argv[1], &sz); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 1135 | if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1136 | if (sz != 256) die("signature must be 256 bytes"); |
| 1137 | fb_queue_download("signature", data, sz); |
| 1138 | fb_queue_command("signature", "installing signature"); |
| 1139 | skip(2); |
| 1140 | } else if(!strcmp(*argv, "reboot")) { |
| 1141 | wants_reboot = 1; |
| 1142 | skip(1); |
| 1143 | } else if(!strcmp(*argv, "reboot-bootloader")) { |
| 1144 | wants_reboot_bootloader = 1; |
| 1145 | skip(1); |
| 1146 | } else if (!strcmp(*argv, "continue")) { |
| 1147 | fb_queue_command("continue", "resuming boot"); |
| 1148 | skip(1); |
| 1149 | } else if(!strcmp(*argv, "boot")) { |
| 1150 | char *kname = 0; |
| 1151 | char *rname = 0; |
| 1152 | skip(1); |
| 1153 | if (argc > 0) { |
| 1154 | kname = argv[0]; |
| 1155 | skip(1); |
| 1156 | } |
| 1157 | if (argc > 0) { |
| 1158 | rname = argv[0]; |
| 1159 | skip(1); |
| 1160 | } |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1161 | data = load_bootable_image(kname, rname, &sz, cmdline); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1162 | if (data == 0) return 1; |
| 1163 | fb_queue_download("boot.img", data, sz); |
| 1164 | fb_queue_command("boot", "booting"); |
| 1165 | } else if(!strcmp(*argv, "flash")) { |
| 1166 | char *pname = argv[1]; |
| 1167 | char *fname = 0; |
| 1168 | require(2); |
| 1169 | if (argc > 2) { |
| 1170 | fname = argv[2]; |
| 1171 | skip(3); |
| 1172 | } else { |
| 1173 | fname = find_item(pname, product); |
| 1174 | skip(2); |
| 1175 | } |
| 1176 | if (fname == 0) die("cannot determine image filename for '%s'", pname); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1177 | if (erase_first && needs_erase(pname)) { |
| 1178 | fb_queue_erase(pname); |
| 1179 | } |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 1180 | do_flash(usb, pname, fname); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1181 | } else if(!strcmp(*argv, "flash:raw")) { |
| 1182 | char *pname = argv[1]; |
| 1183 | char *kname = argv[2]; |
| 1184 | char *rname = 0; |
| 1185 | require(3); |
| 1186 | if(argc > 3) { |
| 1187 | rname = argv[3]; |
| 1188 | skip(4); |
| 1189 | } else { |
| 1190 | skip(3); |
| 1191 | } |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1192 | data = load_bootable_image(kname, rname, &sz, cmdline); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1193 | if (data == 0) die("cannot load bootable image"); |
| 1194 | fb_queue_flash(pname, data, sz); |
| 1195 | } else if(!strcmp(*argv, "flashall")) { |
| 1196 | skip(1); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 1197 | do_flashall(usb, erase_first); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1198 | wants_reboot = 1; |
| 1199 | } else if(!strcmp(*argv, "update")) { |
| 1200 | if (argc > 1) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 1201 | do_update(usb, argv[1], erase_first); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1202 | skip(2); |
| 1203 | } else { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 1204 | do_update(usb, "update.zip", erase_first); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1205 | skip(1); |
| 1206 | } |
| 1207 | wants_reboot = 1; |
| 1208 | } else if(!strcmp(*argv, "oem")) { |
| 1209 | argc = do_oem_command(argc, argv); |
| 1210 | } else { |
| 1211 | usage(); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 1212 | return 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | if (wants_wipe) { |
| 1217 | fb_queue_erase("userdata"); |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame^] | 1218 | fb_perform_format("userdata", 1, NULL, NULL); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1219 | fb_queue_erase("cache"); |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame^] | 1220 | fb_perform_format("cache", 1, NULL, NULL); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1221 | } |
| 1222 | if (wants_reboot) { |
| 1223 | fb_queue_reboot(); |
| 1224 | } else if (wants_reboot_bootloader) { |
| 1225 | fb_queue_command("reboot-bootloader", "rebooting into bootloader"); |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 1226 | fb_queue_wait_for_disconnect(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1227 | } |
| 1228 | |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 1229 | if (fb_queue_is_empty()) |
| 1230 | return 0; |
| 1231 | |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 1232 | status = fb_execute_queue(usb); |
| 1233 | return (status) ? 1 : 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1234 | } |