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