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> |
Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 37 | #include <stdint.h> |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <sys/stat.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | #include <sys/time.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 43 | #include <sys/types.h> |
Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 44 | #include <unistd.h> |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 45 | #include <functional> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 46 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 47 | #include <base/parseint.h> |
Elliott Hughes | 77c0e66 | 2015-11-02 15:51:12 -0800 | [diff] [blame] | 48 | #include <base/strings.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 49 | #include <sparse/sparse.h> |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 50 | #include <ziparchive/zip_archive.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 52 | #include <base/strings.h> |
| 53 | #include <base/parseint.h> |
| 54 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 55 | #include "bootimg_utils.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 56 | #include "fastboot.h" |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 57 | #include "fs.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 59 | #ifndef O_BINARY |
| 60 | #define O_BINARY 0 |
| 61 | #endif |
| 62 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 63 | #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a))) |
| 64 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 65 | char cur_product[FB_RESPONSE_SZ + 1]; |
| 66 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 67 | static const char *serial = 0; |
| 68 | static const char *product = 0; |
| 69 | static const char *cmdline = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 70 | static unsigned short vendor_id = 0; |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 71 | static int long_listing = 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 72 | static int64_t sparse_limit = -1; |
| 73 | static int64_t target_sparse_limit = -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 74 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 75 | static unsigned page_size = 2048; |
| 76 | static unsigned base_addr = 0x10000000; |
| 77 | static unsigned kernel_offset = 0x00008000; |
| 78 | static unsigned ramdisk_offset = 0x01000000; |
| 79 | static unsigned second_offset = 0x00f00000; |
| 80 | static unsigned tags_offset = 0x00000100; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 81 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 82 | enum fb_buffer_type { |
| 83 | FB_BUFFER, |
| 84 | FB_BUFFER_SPARSE, |
| 85 | }; |
| 86 | |
| 87 | struct fastboot_buffer { |
| 88 | enum fb_buffer_type type; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 89 | void* data; |
| 90 | int64_t sz; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static struct { |
| 94 | char img_name[13]; |
| 95 | char sig_name[13]; |
| 96 | char part_name[9]; |
| 97 | bool is_optional; |
Daniel Rosenberg | f530c93 | 2014-05-28 14:10:01 -0700 | [diff] [blame] | 98 | } images[] = { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 99 | {"boot.img", "boot.sig", "boot", false}, |
| 100 | {"recovery.img", "recovery.sig", "recovery", true}, |
| 101 | {"system.img", "system.sig", "system", false}, |
Daniel Rosenberg | f530c93 | 2014-05-28 14:10:01 -0700 | [diff] [blame] | 102 | {"vendor.img", "vendor.sig", "vendor", true}, |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 103 | }; |
Brian Swetland | 2a63bb7 | 2009-04-28 16:05:07 -0700 | [diff] [blame] | 104 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 105 | static char* find_item(const char* item, const char* product) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 106 | char *dir; |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 107 | const char *fn; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 108 | char path[PATH_MAX + 128]; |
| 109 | |
| 110 | if(!strcmp(item,"boot")) { |
| 111 | fn = "boot.img"; |
| 112 | } else if(!strcmp(item,"recovery")) { |
| 113 | fn = "recovery.img"; |
| 114 | } else if(!strcmp(item,"system")) { |
| 115 | fn = "system.img"; |
Daniel Rosenberg | f530c93 | 2014-05-28 14:10:01 -0700 | [diff] [blame] | 116 | } else if(!strcmp(item,"vendor")) { |
| 117 | fn = "vendor.img"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 118 | } else if(!strcmp(item,"userdata")) { |
| 119 | fn = "userdata.img"; |
Jean-Baptiste Queru | d7608a4 | 2011-09-30 14:39:25 -0700 | [diff] [blame] | 120 | } else if(!strcmp(item,"cache")) { |
| 121 | fn = "cache.img"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 122 | } else if(!strcmp(item,"info")) { |
| 123 | fn = "android-info.txt"; |
| 124 | } else { |
| 125 | fprintf(stderr,"unknown partition '%s'\n", item); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | if(product) { |
| 130 | get_my_path(path); |
| 131 | sprintf(path + strlen(path), |
| 132 | "../../../target/product/%s/%s", product, fn); |
| 133 | return strdup(path); |
| 134 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 135 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 136 | dir = getenv("ANDROID_PRODUCT_OUT"); |
| 137 | if((dir == 0) || (dir[0] == 0)) { |
| 138 | die("neither -p product specified nor ANDROID_PRODUCT_OUT set"); |
| 139 | return 0; |
| 140 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 141 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 142 | sprintf(path, "%s/%s", dir, fn); |
| 143 | return strdup(path); |
| 144 | } |
| 145 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 146 | static int64_t get_file_size(int fd) { |
| 147 | struct stat sb; |
| 148 | return fstat(fd, &sb) == -1 ? -1 : sb.st_size; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 151 | static void* load_fd(int fd, int64_t* sz) { |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 152 | int errno_tmp; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 153 | char* data = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 154 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 155 | *sz = get_file_size(fd); |
| 156 | if (*sz < 0) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 157 | goto oops; |
| 158 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 160 | data = (char*) malloc(*sz); |
| 161 | if (data == nullptr) goto oops; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 162 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 163 | if(read(fd, data, *sz) != *sz) goto oops; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 164 | close(fd); |
| 165 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 166 | return data; |
| 167 | |
| 168 | oops: |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 169 | errno_tmp = errno; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 170 | close(fd); |
| 171 | if(data != 0) free(data); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 172 | errno = errno_tmp; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 173 | return 0; |
| 174 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 175 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 176 | static void* load_file(const char* fn, int64_t* sz) { |
| 177 | int fd = open(fn, O_RDONLY | O_BINARY); |
| 178 | if (fd == -1) return nullptr; |
| 179 | return load_fd(fd, sz); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 180 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 181 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 182 | static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) { |
Elliott Hughes | e1746fd | 2015-08-10 15:30:54 -0700 | [diff] [blame] | 183 | // Require a matching vendor id if the user specified one with -i. |
Elliott Hughes | b46964f | 2015-08-19 17:49:45 -0700 | [diff] [blame] | 184 | if (vendor_id != 0 && info->dev_vendor != vendor_id) { |
Elliott Hughes | e1746fd | 2015-08-10 15:30:54 -0700 | [diff] [blame] | 185 | return -1; |
| 186 | } |
| 187 | |
| 188 | if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) { |
| 189 | return -1; |
| 190 | } |
| 191 | |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 192 | // require matching serial number or device path if requested |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 193 | // at the command line with the -s option. |
JP Abgrall | a032ded | 2012-06-06 11:53:33 -0700 | [diff] [blame] | 194 | if (local_serial && (strcmp(local_serial, info->serial_number) != 0 && |
| 195 | strcmp(local_serial, info->device_path) != 0)) return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 199 | static int match_fastboot(usb_ifc_info* info) { |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 200 | return match_fastboot_with_serial(info, serial); |
| 201 | } |
| 202 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 203 | static int list_devices_callback(usb_ifc_info* info) { |
| 204 | if (match_fastboot_with_serial(info, nullptr) == 0) { |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 205 | const char* serial = info->serial_number; |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 206 | if (!info->writable) { |
| 207 | serial = "no permissions"; // like "adb devices" |
| 208 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 209 | if (!serial[0]) { |
| 210 | serial = "????????????"; |
| 211 | } |
Scott Anderson | 866b1bd | 2012-06-04 20:29:11 -0700 | [diff] [blame] | 212 | // output compatible with "adb devices" |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 213 | if (!long_listing) { |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 214 | printf("%s\tfastboot\n", serial); |
Stephen Hines | 04f8953 | 2014-11-26 14:54:43 -0800 | [diff] [blame] | 215 | } else if (strcmp("", info->device_path) == 0) { |
Scott Anderson | 866b1bd | 2012-06-04 20:29:11 -0700 | [diff] [blame] | 216 | printf("%-22s fastboot\n", serial); |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 217 | } else { |
Scott Anderson | 866b1bd | 2012-06-04 20:29:11 -0700 | [diff] [blame] | 218 | printf("%-22s fastboot %s\n", serial, info->device_path); |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 219 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | return -1; |
| 223 | } |
| 224 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 225 | static usb_handle* open_device() { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 226 | static usb_handle *usb = 0; |
| 227 | int announce = 1; |
| 228 | |
| 229 | if(usb) return usb; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 230 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 231 | for(;;) { |
| 232 | usb = usb_open(match_fastboot); |
| 233 | if(usb) return usb; |
| 234 | if(announce) { |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 235 | announce = 0; |
Elliott Hughes | b46964f | 2015-08-19 17:49:45 -0700 | [diff] [blame] | 236 | fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 237 | } |
Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 238 | usleep(1000); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 242 | static void list_devices() { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 243 | // We don't actually open a USB device here, |
| 244 | // just getting our callback called so we can |
| 245 | // list all the connected devices. |
| 246 | usb_open(list_devices_callback); |
| 247 | } |
| 248 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 249 | static void usage() { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 250 | fprintf(stderr, |
| 251 | /* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */ |
| 252 | "usage: fastboot [ <option> ] <command>\n" |
| 253 | "\n" |
| 254 | "commands:\n" |
Elliott Hughes | 08df533 | 2015-06-10 11:58:14 -0700 | [diff] [blame] | 255 | " update <filename> Reflash device from update.zip.\n" |
| 256 | " flashall Flash boot, system, vendor, and --\n" |
| 257 | " if found -- recovery.\n" |
| 258 | " flash <partition> [ <filename> ] Write a file to a flash partition.\n" |
| 259 | " flashing lock Locks the device. Prevents flashing.\n" |
| 260 | " flashing unlock Unlocks the device. Allows flashing\n" |
| 261 | " any partition except\n" |
| 262 | " bootloader-related partitions.\n" |
| 263 | " flashing lock_critical Prevents flashing bootloader-related\n" |
| 264 | " partitions.\n" |
| 265 | " flashing unlock_critical Enables flashing bootloader-related\n" |
| 266 | " partitions.\n" |
| 267 | " flashing get_unlock_ability Queries bootloader to see if the\n" |
| 268 | " device is unlocked.\n" |
| 269 | " erase <partition> Erase a flash partition.\n" |
| 270 | " format[:[<fs type>][:[<size>]] <partition>\n" |
| 271 | " Format a flash partition. Can\n" |
| 272 | " override the fs type and/or size\n" |
| 273 | " the bootloader reports.\n" |
| 274 | " getvar <variable> Display a bootloader variable.\n" |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 275 | " set_active <suffix> Sets the active slot. If slots are\n" |
| 276 | " not supported, this does nothing.\n" |
Elliott Hughes | 08df533 | 2015-06-10 11:58:14 -0700 | [diff] [blame] | 277 | " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n" |
| 278 | " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n" |
| 279 | " Create bootimage and flash it.\n" |
| 280 | " devices [-l] List all connected devices [with\n" |
| 281 | " device paths].\n" |
| 282 | " continue Continue with autoboot.\n" |
| 283 | " reboot [bootloader] Reboot device [into bootloader].\n" |
| 284 | " reboot-bootloader Reboot device into bootloader.\n" |
| 285 | " help Show this help message.\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 286 | "\n" |
| 287 | "options:\n" |
Elliott Hughes | 08df533 | 2015-06-10 11:58:14 -0700 | [diff] [blame] | 288 | " -w Erase userdata and cache (and format\n" |
| 289 | " if supported by partition type).\n" |
| 290 | " -u Do not erase partition before\n" |
| 291 | " formatting.\n" |
| 292 | " -s <specific device> Specify device serial number\n" |
| 293 | " or path to device port.\n" |
| 294 | " -p <product> Specify product name.\n" |
| 295 | " -c <cmdline> Override kernel commandline.\n" |
| 296 | " -i <vendor id> Specify a custom USB vendor id.\n" |
| 297 | " -b <base_addr> Specify a custom kernel base\n" |
| 298 | " address (default: 0x10000000).\n" |
| 299 | " -n <page size> Specify the nand page size\n" |
| 300 | " (default: 2048).\n" |
| 301 | " -S <size>[K|M|G] Automatically sparse files greater\n" |
| 302 | " than 'size'. 0 to disable.\n" |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 303 | " --slot <suffix> Specify slot suffix to be used if the\n" |
| 304 | " device supports slots. This will be\n" |
| 305 | " added to all partition names that use\n" |
| 306 | " slots. 'all' can be given to refer\n" |
| 307 | " to all slots. If this is not given,\n" |
| 308 | " slotted partitions will default to\n" |
| 309 | " the current active slot.\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 310 | ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 313 | static void* load_bootable_image(const char* kernel, const char* ramdisk, |
| 314 | const char* secondstage, int64_t* sz, |
| 315 | const char* cmdline) { |
| 316 | if (kernel == nullptr) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 317 | fprintf(stderr, "no image specified\n"); |
| 318 | return 0; |
| 319 | } |
| 320 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 321 | int64_t ksize; |
| 322 | void* kdata = load_file(kernel, &ksize); |
| 323 | if (kdata == nullptr) { |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 324 | 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] | 325 | return 0; |
| 326 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 327 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 328 | // Is this actually a boot image? |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 329 | if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 330 | if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 331 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 332 | if (ramdisk) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 333 | fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n"); |
| 334 | return 0; |
| 335 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 336 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 337 | *sz = ksize; |
| 338 | return kdata; |
| 339 | } |
| 340 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 341 | void* rdata = nullptr; |
| 342 | int64_t rsize = 0; |
| 343 | if (ramdisk) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 344 | rdata = load_file(ramdisk, &rsize); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 345 | if (rdata == nullptr) { |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 346 | 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] | 347 | return 0; |
| 348 | } |
| 349 | } |
| 350 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 351 | void* sdata = nullptr; |
| 352 | int64_t ssize = 0; |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 353 | if (secondstage) { |
| 354 | sdata = load_file(secondstage, &ssize); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 355 | if (sdata == nullptr) { |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 356 | fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno)); |
| 357 | return 0; |
| 358 | } |
| 359 | } |
| 360 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 361 | fprintf(stderr,"creating boot image...\n"); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 362 | int64_t bsize = 0; |
| 363 | void* bdata = mkbootimg(kdata, ksize, kernel_offset, |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 364 | rdata, rsize, ramdisk_offset, |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 365 | sdata, ssize, second_offset, |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 366 | page_size, base_addr, tags_offset, &bsize); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 367 | if (bdata == nullptr) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 368 | fprintf(stderr,"failed to create boot.img\n"); |
| 369 | return 0; |
| 370 | } |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 371 | if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline); |
| 372 | fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 373 | *sz = bsize; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 374 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 375 | return bdata; |
| 376 | } |
| 377 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 378 | static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 379 | { |
Yusuke Sato | 0744754 | 2015-06-25 14:39:19 -0700 | [diff] [blame] | 380 | ZipString zip_entry_name(entry_name); |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 381 | ZipEntry zip_entry; |
| 382 | if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) { |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 383 | fprintf(stderr, "archive does not contain '%s'\n", entry_name); |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 384 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 387 | *sz = zip_entry.uncompressed_length; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 388 | |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 389 | uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length)); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 390 | if (data == nullptr) { |
| 391 | fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name); |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 392 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 395 | int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length); |
| 396 | if (error != 0) { |
| 397 | fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 398 | free(data); |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 399 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 400 | } |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 401 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 402 | return data; |
| 403 | } |
| 404 | |
Elliott Hughes | a26fbee | 2015-06-03 15:22:11 -0700 | [diff] [blame] | 405 | #if defined(_WIN32) |
| 406 | |
| 407 | // TODO: move this to somewhere it can be shared. |
| 408 | |
| 409 | #include <windows.h> |
| 410 | |
| 411 | // Windows' tmpfile(3) requires administrator rights because |
| 412 | // it creates temporary files in the root directory. |
| 413 | static FILE* win32_tmpfile() { |
| 414 | char temp_path[PATH_MAX]; |
| 415 | DWORD nchars = GetTempPath(sizeof(temp_path), temp_path); |
| 416 | if (nchars == 0 || nchars >= sizeof(temp_path)) { |
| 417 | fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError()); |
| 418 | return nullptr; |
| 419 | } |
| 420 | |
| 421 | char filename[PATH_MAX]; |
| 422 | if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) { |
| 423 | fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError()); |
| 424 | return nullptr; |
| 425 | } |
| 426 | |
| 427 | return fopen(filename, "w+bTD"); |
| 428 | } |
| 429 | |
| 430 | #define tmpfile win32_tmpfile |
| 431 | |
| 432 | #endif |
| 433 | |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 434 | static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) { |
| 435 | FILE* fp = tmpfile(); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 436 | if (fp == nullptr) { |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 437 | fprintf(stderr, "failed to create temporary file for '%s': %s\n", |
| 438 | entry_name, strerror(errno)); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 439 | return -1; |
| 440 | } |
| 441 | |
Yusuke Sato | 0744754 | 2015-06-25 14:39:19 -0700 | [diff] [blame] | 442 | ZipString zip_entry_name(entry_name); |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 443 | ZipEntry zip_entry; |
| 444 | if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) { |
| 445 | fprintf(stderr, "archive does not contain '%s'\n", entry_name); |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 446 | return -1; |
| 447 | } |
| 448 | |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 449 | int fd = fileno(fp); |
| 450 | int error = ExtractEntryToFile(zip, &zip_entry, fd); |
| 451 | if (error != 0) { |
| 452 | fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error)); |
| 453 | return -1; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 456 | lseek(fd, 0, SEEK_SET); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 457 | return fd; |
| 458 | } |
| 459 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 460 | static char *strip(char *s) |
| 461 | { |
| 462 | int n; |
| 463 | while(*s && isspace(*s)) s++; |
| 464 | n = strlen(s); |
| 465 | while(n-- > 0) { |
| 466 | if(!isspace(s[n])) break; |
| 467 | s[n] = 0; |
| 468 | } |
| 469 | return s; |
| 470 | } |
| 471 | |
| 472 | #define MAX_OPTIONS 32 |
| 473 | static int setup_requirement_line(char *name) |
| 474 | { |
| 475 | char *val[MAX_OPTIONS]; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 476 | char *prod = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 477 | unsigned n, count; |
| 478 | char *x; |
| 479 | int invert = 0; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 480 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 481 | if (!strncmp(name, "reject ", 7)) { |
| 482 | name += 7; |
| 483 | invert = 1; |
| 484 | } else if (!strncmp(name, "require ", 8)) { |
| 485 | name += 8; |
| 486 | invert = 0; |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 487 | } else if (!strncmp(name, "require-for-product:", 20)) { |
| 488 | // Get the product and point name past it |
| 489 | prod = name + 20; |
| 490 | name = strchr(name, ' '); |
| 491 | if (!name) return -1; |
| 492 | *name = 0; |
| 493 | name += 1; |
| 494 | invert = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | x = strchr(name, '='); |
| 498 | if (x == 0) return 0; |
| 499 | *x = 0; |
| 500 | val[0] = x + 1; |
| 501 | |
| 502 | for(count = 1; count < MAX_OPTIONS; count++) { |
| 503 | x = strchr(val[count - 1],'|'); |
| 504 | if (x == 0) break; |
| 505 | *x = 0; |
| 506 | val[count] = x + 1; |
| 507 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 508 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 509 | name = strip(name); |
| 510 | for(n = 0; n < count; n++) val[n] = strip(val[n]); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 511 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 512 | name = strip(name); |
| 513 | if (name == 0) return -1; |
| 514 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 515 | const char* var = name; |
| 516 | // Work around an unfortunate name mismatch. |
| 517 | if (!strcmp(name,"board")) var = "product"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 518 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 519 | const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 520 | if (out == 0) return -1; |
| 521 | |
| 522 | for(n = 0; n < count; n++) { |
| 523 | out[n] = strdup(strip(val[n])); |
Elliott Hughes | 14e28d3 | 2013-10-29 14:12:46 -0700 | [diff] [blame] | 524 | if (out[n] == 0) { |
| 525 | for(size_t i = 0; i < n; ++i) { |
| 526 | free((char*) out[i]); |
| 527 | } |
| 528 | free(out); |
| 529 | return -1; |
| 530 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 533 | fb_queue_require(prod, var, invert, n, out); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 534 | return 0; |
| 535 | } |
| 536 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 537 | static void setup_requirements(char* data, int64_t sz) { |
| 538 | char* s = data; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 539 | while (sz-- > 0) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 540 | if (*s == '\n') { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 541 | *s++ = 0; |
| 542 | if (setup_requirement_line(data)) { |
| 543 | die("out of memory"); |
| 544 | } |
| 545 | data = s; |
| 546 | } else { |
| 547 | s++; |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 552 | static void queue_info_dump() { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 553 | fb_queue_notice("--------------------------------------------"); |
| 554 | fb_queue_display("version-bootloader", "Bootloader Version..."); |
| 555 | fb_queue_display("version-baseband", "Baseband Version....."); |
| 556 | fb_queue_display("serialno", "Serial Number........"); |
| 557 | fb_queue_notice("--------------------------------------------"); |
| 558 | } |
| 559 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 560 | static struct sparse_file **load_sparse_files(int fd, int max_size) |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 561 | { |
Mohamad Ayyash | 80cc1f6 | 2015-03-31 12:09:29 -0700 | [diff] [blame] | 562 | struct sparse_file* s = sparse_file_import_auto(fd, false, true); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 563 | if (!s) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 564 | die("cannot sparse read file\n"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 567 | int files = sparse_file_resparse(s, max_size, nullptr, 0); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 568 | if (files < 0) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 569 | die("Failed to resparse\n"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 572 | sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1)); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 573 | if (!out_s) { |
| 574 | die("Failed to allocate sparse file array\n"); |
| 575 | } |
| 576 | |
| 577 | files = sparse_file_resparse(s, max_size, out_s, files); |
| 578 | if (files < 0) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 579 | die("Failed to resparse\n"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | return out_s; |
| 583 | } |
| 584 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 585 | static int64_t get_target_sparse_limit(usb_handle* usb) { |
| 586 | std::string max_download_size; |
| 587 | if (!fb_getvar(usb, "max-download-size", &max_download_size)) { |
Elliott Hughes | 3ab0586 | 2015-11-02 09:01:53 -0800 | [diff] [blame] | 588 | fprintf(stderr, "target didn't report max-download-size\n"); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 589 | return 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Elliott Hughes | 77c0e66 | 2015-11-02 15:51:12 -0800 | [diff] [blame] | 592 | // Some bootloaders (angler, for example) send spurious whitespace too. |
| 593 | max_download_size = android::base::Trim(max_download_size); |
| 594 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 595 | uint64_t limit; |
| 596 | if (!android::base::ParseUint(max_download_size.c_str(), &limit)) { |
Elliott Hughes | 3ab0586 | 2015-11-02 09:01:53 -0800 | [diff] [blame] | 597 | fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str()); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 598 | return 0; |
| 599 | } |
| 600 | if (limit > 0) { |
| 601 | fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit); |
| 602 | } |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 603 | return limit; |
| 604 | } |
| 605 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 606 | static int64_t get_sparse_limit(usb_handle* usb, int64_t size) { |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 607 | int64_t limit; |
| 608 | |
| 609 | if (sparse_limit == 0) { |
| 610 | return 0; |
| 611 | } else if (sparse_limit > 0) { |
| 612 | limit = sparse_limit; |
| 613 | } else { |
| 614 | if (target_sparse_limit == -1) { |
| 615 | target_sparse_limit = get_target_sparse_limit(usb); |
| 616 | } |
| 617 | if (target_sparse_limit > 0) { |
| 618 | limit = target_sparse_limit; |
| 619 | } else { |
Colin Cross | 0bbfb39 | 2012-07-24 18:05:21 -0700 | [diff] [blame] | 620 | return 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
| 624 | if (size > limit) { |
| 625 | return limit; |
| 626 | } |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 631 | // Until we get lazy inode table init working in make_ext4fs, we need to |
| 632 | // erase partitions of type ext4 before flashing a filesystem so no stale |
| 633 | // inodes are left lying around. Otherwise, e2fsck gets very upset. |
Elliott Hughes | 8ab9a32 | 2015-11-02 14:05:57 -0800 | [diff] [blame] | 634 | static bool needs_erase(usb_handle* usb, const char* partition) { |
| 635 | std::string partition_type; |
| 636 | if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) { |
| 637 | return false; |
| 638 | } |
| 639 | return partition_type == "ext4"; |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 642 | static int load_buf_fd(usb_handle* usb, int fd, struct fastboot_buffer* buf) { |
| 643 | int64_t sz = get_file_size(fd); |
| 644 | if (sz == -1) { |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 645 | return -1; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 646 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 647 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 648 | lseek64(fd, 0, SEEK_SET); |
| 649 | int64_t limit = get_sparse_limit(usb, sz); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 650 | if (limit) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 651 | sparse_file** s = load_sparse_files(fd, limit); |
| 652 | if (s == nullptr) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 653 | return -1; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 654 | } |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 655 | buf->type = FB_BUFFER_SPARSE; |
| 656 | buf->data = s; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 657 | } else { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 658 | void* data = load_fd(fd, &sz); |
| 659 | if (data == nullptr) return -1; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 660 | buf->type = FB_BUFFER; |
Sasha Levitskiy | 782111b | 2014-05-05 19:43:15 -0700 | [diff] [blame] | 661 | buf->data = data; |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 662 | buf->sz = sz; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 663 | } |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 664 | |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | static int load_buf(usb_handle *usb, const char *fname, |
| 669 | struct fastboot_buffer *buf) |
| 670 | { |
| 671 | int fd; |
| 672 | |
| 673 | fd = open(fname, O_RDONLY | O_BINARY); |
| 674 | if (fd < 0) { |
Daniel Rosenberg | 8228059 | 2014-04-29 13:45:05 -0700 | [diff] [blame] | 675 | return -1; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | return load_buf_fd(usb, fd, buf); |
| 679 | } |
| 680 | |
| 681 | static void flash_buf(const char *pname, struct fastboot_buffer *buf) |
| 682 | { |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 683 | sparse_file** s; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 684 | |
| 685 | switch (buf->type) { |
| 686 | case FB_BUFFER_SPARSE: |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 687 | s = reinterpret_cast<sparse_file**>(buf->data); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 688 | while (*s) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 689 | int64_t sz = sparse_file_len(*s, true, false); |
| 690 | fb_queue_flash_sparse(pname, *s++, sz); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 691 | } |
| 692 | break; |
| 693 | case FB_BUFFER: |
| 694 | fb_queue_flash(pname, buf->data, buf->sz); |
| 695 | break; |
| 696 | default: |
| 697 | die("unknown buffer type: %d", buf->type); |
| 698 | } |
| 699 | } |
| 700 | |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 701 | static std::vector<std::string> get_suffixes(usb_handle* usb) { |
| 702 | std::vector<std::string> suffixes; |
| 703 | std::string suffix_list; |
| 704 | if (!fb_getvar(usb, "slot-suffixes", &suffix_list)) { |
| 705 | die("Could not get suffixes.\n"); |
| 706 | } |
| 707 | return android::base::Split(suffix_list, ","); |
| 708 | } |
| 709 | |
| 710 | static std::string verify_slot(usb_handle* usb, const char *slot) { |
| 711 | if (strcmp(slot, "all") == 0) { |
| 712 | return "all"; |
| 713 | } |
| 714 | std::vector<std::string> suffixes = get_suffixes(usb); |
| 715 | for (const std::string &suffix : suffixes) { |
| 716 | if (suffix == slot) |
| 717 | return slot; |
| 718 | } |
| 719 | fprintf(stderr, "Slot %s does not exist. supported slots are:", slot); |
| 720 | for (const std::string &suffix : suffixes) { |
| 721 | fprintf(stderr, "%s\n", suffix.c_str()); |
| 722 | } |
| 723 | exit(1); |
| 724 | } |
| 725 | |
| 726 | static void do_for_partition(usb_handle* usb, const char *part, const char *slot, std::function<void(const std::string&)> func, bool force_slot) { |
| 727 | std::string partition_slot; |
| 728 | std::string current_slot; |
| 729 | |
| 730 | if (!fb_getvar(usb, std::string("partition-slot:")+part, &partition_slot)) { |
| 731 | /* If partition-slot is not supported, the answer is no. */ |
| 732 | partition_slot = ""; |
| 733 | } |
| 734 | if (partition_slot == "1") { |
| 735 | if (!slot || slot[0] == 0) { |
| 736 | if (!fb_getvar(usb, "current-slot", ¤t_slot)) { |
| 737 | die("Failed to identify current slot.\n"); |
| 738 | } |
| 739 | func(std::string(part) + '-' + current_slot); |
| 740 | } else { |
| 741 | func(std::string(part) + '-' + slot); |
| 742 | } |
| 743 | } else { |
| 744 | if (force_slot && slot && slot[0]) { |
| 745 | fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n", part, slot); |
| 746 | } |
| 747 | func(part); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | /* This function will find the real partition name given a base name, and a slot. If slot is NULL or empty, |
| 752 | * it will use the current slot. If slot is "all", it will return a list of all possible partition names. |
| 753 | * If force_slot is true, it will fail if a slot is specified, and the given partition does not support slots. |
| 754 | */ |
| 755 | static void do_for_partitions(usb_handle* usb, const char *part, const char *slot, std::function<void(const std::string&)> func, bool force_slot) { |
| 756 | std::string partition_slot; |
| 757 | |
| 758 | if (slot && strcmp(slot, "all") == 0) { |
| 759 | if (!fb_getvar(usb, std::string("partition-slot:") + part, &partition_slot)) { |
| 760 | die("Could not check if partition %s has slot.", part); |
| 761 | } |
| 762 | if (partition_slot == "1") { |
| 763 | std::vector<std::string> suffixes = get_suffixes(usb); |
| 764 | for (std::string &suffix : suffixes) { |
| 765 | do_for_partition(usb, part, suffix.c_str(), func, force_slot); |
| 766 | } |
| 767 | } else { |
| 768 | do_for_partition(usb, part, "", func, force_slot); |
| 769 | } |
| 770 | } else { |
| 771 | do_for_partition(usb, part, slot, func, force_slot); |
| 772 | } |
| 773 | } |
| 774 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 775 | static void do_flash(usb_handle* usb, const char* pname, const char* fname) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 776 | struct fastboot_buffer buf; |
| 777 | |
| 778 | if (load_buf(usb, fname, &buf)) { |
| 779 | die("cannot load '%s'", fname); |
| 780 | } |
| 781 | flash_buf(pname, &buf); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 784 | static void do_update_signature(ZipArchiveHandle zip, char* fn) { |
| 785 | int64_t sz; |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 786 | void* data = unzip_file(zip, fn, &sz); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 787 | if (data == nullptr) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 788 | fb_queue_download("signature", data, sz); |
| 789 | fb_queue_command("signature", "installing signature"); |
| 790 | } |
| 791 | |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 792 | static void do_update(usb_handle* usb, const char* filename, const char* slot_override, bool erase_first) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 793 | queue_info_dump(); |
| 794 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 795 | fb_queue_query_save("product", cur_product, sizeof(cur_product)); |
| 796 | |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 797 | ZipArchiveHandle zip; |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 798 | int error = OpenArchive(filename, &zip); |
| 799 | if (error != 0) { |
Narayan Kamath | f6e9ffb | 2015-05-11 16:59:46 +0100 | [diff] [blame] | 800 | CloseArchive(zip); |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 801 | die("failed to open zip file '%s': %s", filename, ErrorCodeString(error)); |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 802 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 803 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 804 | int64_t sz; |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 805 | void* data = unzip_file(zip, "android-info.txt", &sz); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 806 | if (data == nullptr) { |
Narayan Kamath | f6e9ffb | 2015-05-11 16:59:46 +0100 | [diff] [blame] | 807 | CloseArchive(zip); |
Elliott Hughes | 7c6d884 | 2015-03-19 10:30:53 -0700 | [diff] [blame] | 808 | die("update package '%s' has no android-info.txt", filename); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 809 | } |
| 810 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 811 | setup_requirements(reinterpret_cast<char*>(data), sz); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 812 | |
Elliott Hughes | acdbe92 | 2015-06-02 21:37:05 -0700 | [diff] [blame] | 813 | for (size_t i = 0; i < ARRAY_SIZE(images); ++i) { |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 814 | int fd = unzip_to_file(zip, images[i].img_name); |
Elliott Hughes | acdbe92 | 2015-06-02 21:37:05 -0700 | [diff] [blame] | 815 | if (fd == -1) { |
| 816 | if (images[i].is_optional) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 817 | continue; |
Elliott Hughes | acdbe92 | 2015-06-02 21:37:05 -0700 | [diff] [blame] | 818 | } |
Narayan Kamath | f6e9ffb | 2015-05-11 16:59:46 +0100 | [diff] [blame] | 819 | CloseArchive(zip); |
Elliott Hughes | acdbe92 | 2015-06-02 21:37:05 -0700 | [diff] [blame] | 820 | exit(1); // unzip_to_file already explained why. |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 821 | } |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 822 | fastboot_buffer buf; |
| 823 | int rc = load_buf_fd(usb, fd, &buf); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 824 | if (rc) die("cannot load %s from flash", images[i].img_name); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 825 | |
| 826 | auto update = [&](const std::string &partition) { |
| 827 | do_update_signature(zip, images[i].sig_name); |
| 828 | if (erase_first && needs_erase(usb, partition.c_str())) { |
| 829 | fb_queue_erase(partition.c_str()); |
| 830 | } |
| 831 | flash_buf(partition.c_str(), &buf); |
| 832 | /* not closing the fd here since the sparse code keeps the fd around |
| 833 | * but hasn't mmaped data yet. The tmpfile will get cleaned up when the |
| 834 | * program exits. |
| 835 | */ |
| 836 | }; |
| 837 | do_for_partitions(usb, images[i].part_name, slot_override, update, false); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 838 | } |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 839 | |
| 840 | CloseArchive(zip); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 841 | } |
| 842 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 843 | static void do_send_signature(char* fn) { |
| 844 | char* xtn = strrchr(fn, '.'); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 845 | if (!xtn) return; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 846 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 847 | if (strcmp(xtn, ".img")) return; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 848 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 849 | strcpy(xtn, ".sig"); |
| 850 | |
| 851 | int64_t sz; |
| 852 | void* data = load_file(fn, &sz); |
| 853 | strcpy(xtn, ".img"); |
| 854 | if (data == nullptr) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 855 | fb_queue_download("signature", data, sz); |
| 856 | fb_queue_command("signature", "installing signature"); |
| 857 | } |
| 858 | |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 859 | static void do_flashall(usb_handle* usb, const char *slot_override, int erase_first) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 860 | queue_info_dump(); |
| 861 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 862 | fb_queue_query_save("product", cur_product, sizeof(cur_product)); |
| 863 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 864 | char* fname = find_item("info", product); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 865 | if (fname == nullptr) die("cannot find android-info.txt"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 866 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 867 | int64_t sz; |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 868 | void* data = load_file(fname, &sz); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 869 | if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno)); |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 870 | |
| 871 | setup_requirements(reinterpret_cast<char*>(data), sz); |
| 872 | |
| 873 | for (size_t i = 0; i < ARRAY_SIZE(images); i++) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 874 | fname = find_item(images[i].part_name, product); |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 875 | fastboot_buffer buf; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 876 | if (load_buf(usb, fname, &buf)) { |
| 877 | if (images[i].is_optional) |
| 878 | continue; |
| 879 | die("could not load %s\n", images[i].img_name); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 880 | } |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 881 | |
| 882 | auto flashall = [&](const std::string &partition) { |
| 883 | do_send_signature(fname); |
| 884 | if (erase_first && needs_erase(usb, partition.c_str())) { |
| 885 | fb_queue_erase(partition.c_str()); |
| 886 | } |
| 887 | flash_buf(partition.c_str(), &buf); |
| 888 | }; |
| 889 | do_for_partitions(usb, images[i].part_name, slot_override, flashall, false); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 890 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | #define skip(n) do { argc -= (n); argv += (n); } while (0) |
JP Abgrall | 2d13d14 | 2011-03-01 23:35:07 -0800 | [diff] [blame] | 894 | #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] | 895 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 896 | static int do_oem_command(int argc, char** argv) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 897 | char command[256]; |
| 898 | if (argc <= 1) return 0; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 899 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 900 | command[0] = 0; |
| 901 | while(1) { |
| 902 | strcat(command,*argv); |
| 903 | skip(1); |
| 904 | if(argc == 0) break; |
| 905 | strcat(command," "); |
| 906 | } |
| 907 | |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 908 | fb_queue_command(command,""); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 909 | return 0; |
| 910 | } |
| 911 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 912 | static int64_t parse_num(const char *arg) |
| 913 | { |
| 914 | char *endptr; |
| 915 | unsigned long long num; |
| 916 | |
| 917 | num = strtoull(arg, &endptr, 0); |
| 918 | if (endptr == arg) { |
| 919 | return -1; |
| 920 | } |
| 921 | |
| 922 | if (*endptr == 'k' || *endptr == 'K') { |
| 923 | if (num >= (-1ULL) / 1024) { |
| 924 | return -1; |
| 925 | } |
| 926 | num *= 1024LL; |
| 927 | endptr++; |
| 928 | } else if (*endptr == 'm' || *endptr == 'M') { |
| 929 | if (num >= (-1ULL) / (1024 * 1024)) { |
| 930 | return -1; |
| 931 | } |
| 932 | num *= 1024LL * 1024LL; |
| 933 | endptr++; |
| 934 | } else if (*endptr == 'g' || *endptr == 'G') { |
| 935 | if (num >= (-1ULL) / (1024 * 1024 * 1024)) { |
| 936 | return -1; |
| 937 | } |
| 938 | num *= 1024LL * 1024LL * 1024LL; |
| 939 | endptr++; |
| 940 | } |
| 941 | |
| 942 | if (*endptr != '\0') { |
| 943 | return -1; |
| 944 | } |
| 945 | |
| 946 | if (num > INT64_MAX) { |
| 947 | return -1; |
| 948 | } |
| 949 | |
| 950 | return num; |
| 951 | } |
| 952 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 953 | static void fb_perform_format(usb_handle* usb, |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 954 | const char* partition, int skip_if_not_supported, |
| 955 | const char* type_override, const char* size_override) { |
| 956 | std::string partition_type, partition_size; |
| 957 | |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 958 | struct fastboot_buffer buf; |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 959 | const char* errMsg = nullptr; |
| 960 | const struct fs_generator* gen = nullptr; |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 961 | int fd; |
| 962 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 963 | unsigned int limit = INT_MAX; |
| 964 | if (target_sparse_limit > 0 && target_sparse_limit < limit) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 965 | limit = target_sparse_limit; |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 966 | } |
| 967 | if (sparse_limit > 0 && sparse_limit < limit) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 968 | limit = sparse_limit; |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 969 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 970 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 971 | if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 972 | errMsg = "Can't determine partition type.\n"; |
| 973 | goto failed; |
| 974 | } |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 975 | if (type_override) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 976 | if (partition_type != type_override) { |
| 977 | fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n", |
| 978 | partition, partition_type.c_str(), type_override); |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 979 | } |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 980 | partition_type = type_override; |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 981 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 982 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 983 | if (!fb_getvar(usb, std::string("partition-size:") + partition, &partition_size)) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 984 | errMsg = "Unable to get partition size\n"; |
| 985 | goto failed; |
| 986 | } |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 987 | if (size_override) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 988 | if (partition_size != size_override) { |
| 989 | fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n", |
| 990 | partition, partition_size.c_str(), size_override); |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 991 | } |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 992 | partition_size = size_override; |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 993 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 994 | |
Elliott Hughes | 8ab9a32 | 2015-11-02 14:05:57 -0800 | [diff] [blame] | 995 | gen = fs_get_generator(partition_type); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 996 | if (!gen) { |
| 997 | if (skip_if_not_supported) { |
| 998 | fprintf(stderr, "Erase successful, but not automatically formatting.\n"); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 999 | fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str()); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1000 | return; |
| 1001 | } |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1002 | fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n", |
| 1003 | partition_type.c_str()); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1004 | return; |
| 1005 | } |
| 1006 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1007 | int64_t size; |
| 1008 | if (!android::base::ParseInt(partition_size.c_str(), &size)) { |
| 1009 | fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str()); |
| 1010 | return; |
| 1011 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1012 | |
| 1013 | fd = fileno(tmpfile()); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1014 | if (fs_generator_generate(gen, fd, size)) { |
| 1015 | fprintf(stderr, "Cannot generate image: %s\n", strerror(errno)); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1016 | close(fd); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1017 | return; |
| 1018 | } |
| 1019 | |
| 1020 | if (load_buf_fd(usb, fd, &buf)) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1021 | fprintf(stderr, "Cannot read image: %s\n", strerror(errno)); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1022 | close(fd); |
| 1023 | return; |
| 1024 | } |
| 1025 | flash_buf(partition, &buf); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1026 | return; |
| 1027 | |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1028 | failed: |
| 1029 | if (skip_if_not_supported) { |
| 1030 | fprintf(stderr, "Erase successful, but not automatically formatting.\n"); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1031 | if (errMsg) fprintf(stderr, "%s", errMsg); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1032 | } |
| 1033 | fprintf(stderr,"FAILED (%s)\n", fb_get_error()); |
| 1034 | } |
| 1035 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1036 | int main(int argc, char **argv) |
| 1037 | { |
| 1038 | int wants_wipe = 0; |
| 1039 | int wants_reboot = 0; |
| 1040 | int wants_reboot_bootloader = 0; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1041 | bool erase_first = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1042 | void *data; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1043 | int64_t sz; |
Florian Bäuerle | 27ded48 | 2014-11-24 11:29:34 +0100 | [diff] [blame] | 1044 | int longindex; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 1045 | std::string slot_override; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1046 | |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1047 | const struct option longopts[] = { |
| 1048 | {"base", required_argument, 0, 'b'}, |
| 1049 | {"kernel_offset", required_argument, 0, 'k'}, |
| 1050 | {"page_size", required_argument, 0, 'n'}, |
| 1051 | {"ramdisk_offset", required_argument, 0, 'r'}, |
Mohamad Ayyash | 29fd7b1 | 2014-01-14 18:27:25 -0800 | [diff] [blame] | 1052 | {"tags_offset", required_argument, 0, 't'}, |
Elliott Hughes | 379646b | 2015-06-02 13:50:00 -0700 | [diff] [blame] | 1053 | {"help", no_argument, 0, 'h'}, |
| 1054 | {"unbuffered", no_argument, 0, 0}, |
| 1055 | {"version", no_argument, 0, 0}, |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 1056 | {"slot", required_argument, 0, 0}, |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1057 | {0, 0, 0, 0} |
| 1058 | }; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1059 | |
| 1060 | serial = getenv("ANDROID_SERIAL"); |
| 1061 | |
| 1062 | while (1) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1063 | int c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:h", longopts, &longindex); |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1064 | if (c < 0) { |
| 1065 | break; |
| 1066 | } |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1067 | /* Alphabetical cases */ |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1068 | switch (c) { |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1069 | case 'b': |
| 1070 | base_addr = strtoul(optarg, 0, 16); |
| 1071 | break; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1072 | case 'c': |
| 1073 | cmdline = optarg; |
| 1074 | break; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1075 | case 'h': |
| 1076 | usage(); |
| 1077 | return 1; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1078 | case 'i': { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1079 | char *endptr = nullptr; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1080 | unsigned long val; |
| 1081 | |
| 1082 | val = strtoul(optarg, &endptr, 0); |
| 1083 | if (!endptr || *endptr != '\0' || (val & ~0xffff)) |
| 1084 | die("invalid vendor id '%s'", optarg); |
| 1085 | vendor_id = (unsigned short)val; |
| 1086 | break; |
| 1087 | } |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1088 | case 'k': |
| 1089 | kernel_offset = strtoul(optarg, 0, 16); |
| 1090 | break; |
| 1091 | case 'l': |
| 1092 | long_listing = 1; |
| 1093 | break; |
| 1094 | case 'n': |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1095 | page_size = (unsigned)strtoul(optarg, nullptr, 0); |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1096 | if (!page_size) die("invalid page size"); |
| 1097 | break; |
| 1098 | case 'p': |
| 1099 | product = optarg; |
| 1100 | break; |
| 1101 | case 'r': |
| 1102 | ramdisk_offset = strtoul(optarg, 0, 16); |
| 1103 | break; |
Mohamad Ayyash | 29fd7b1 | 2014-01-14 18:27:25 -0800 | [diff] [blame] | 1104 | case 't': |
| 1105 | tags_offset = strtoul(optarg, 0, 16); |
| 1106 | break; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1107 | case 's': |
| 1108 | serial = optarg; |
| 1109 | break; |
| 1110 | case 'S': |
| 1111 | sparse_limit = parse_num(optarg); |
| 1112 | if (sparse_limit < 0) { |
| 1113 | die("invalid sparse limit"); |
| 1114 | } |
| 1115 | break; |
| 1116 | case 'u': |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1117 | erase_first = false; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1118 | break; |
| 1119 | case 'w': |
| 1120 | wants_wipe = 1; |
| 1121 | break; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1122 | case '?': |
| 1123 | return 1; |
Florian Bäuerle | 27ded48 | 2014-11-24 11:29:34 +0100 | [diff] [blame] | 1124 | case 0: |
| 1125 | if (strcmp("unbuffered", longopts[longindex].name) == 0) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1126 | setvbuf(stdout, nullptr, _IONBF, 0); |
| 1127 | setvbuf(stderr, nullptr, _IONBF, 0); |
Elliott Hughes | 379646b | 2015-06-02 13:50:00 -0700 | [diff] [blame] | 1128 | } else if (strcmp("version", longopts[longindex].name) == 0) { |
| 1129 | fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION); |
| 1130 | return 0; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 1131 | } else if (strcmp("slot", longopts[longindex].name) == 0) { |
| 1132 | slot_override = std::string(optarg); |
Florian Bäuerle | 27ded48 | 2014-11-24 11:29:34 +0100 | [diff] [blame] | 1133 | } |
| 1134 | break; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1135 | default: |
| 1136 | abort(); |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | argc -= optind; |
| 1141 | argv += optind; |
| 1142 | |
| 1143 | if (argc == 0 && !wants_wipe) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1144 | usage(); |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 1145 | return 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1146 | } |
| 1147 | |
Colin Cross | 8fb6e06 | 2012-07-24 16:36:41 -0700 | [diff] [blame] | 1148 | if (argc > 0 && !strcmp(*argv, "devices")) { |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1149 | skip(1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1150 | list_devices(); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 1151 | return 0; |
| 1152 | } |
| 1153 | |
Colin Cross | c7b75dc | 2012-08-29 18:17:06 -0700 | [diff] [blame] | 1154 | if (argc > 0 && !strcmp(*argv, "help")) { |
| 1155 | usage(); |
| 1156 | return 0; |
| 1157 | } |
| 1158 | |
Elliott Hughes | c0ce65f | 2015-06-02 13:34:07 -0700 | [diff] [blame] | 1159 | usb_handle* usb = open_device(); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 1160 | if (slot_override != "") |
| 1161 | slot_override = verify_slot(usb, slot_override.c_str()); |
Elliott Hughes | 31dbed7 | 2009-10-07 15:38:53 -0700 | [diff] [blame] | 1162 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1163 | while (argc > 0) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1164 | if (!strcmp(*argv, "getvar")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1165 | require(2); |
| 1166 | fb_queue_display(argv[1], argv[1]); |
| 1167 | skip(2); |
| 1168 | } else if(!strcmp(*argv, "erase")) { |
| 1169 | require(2); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1170 | |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 1171 | auto erase = [&](const std::string &partition) { |
Elliott Hughes | 8ab9a32 | 2015-11-02 14:05:57 -0800 | [diff] [blame] | 1172 | std::string partition_type; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 1173 | if (fb_getvar(usb, std::string("partition-type:") + argv[1], &partition_type) && |
| 1174 | fs_get_generator(partition_type) != nullptr) { |
| 1175 | fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n", |
| 1176 | partition_type.c_str()); |
| 1177 | } |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1178 | |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 1179 | fb_queue_erase(partition.c_str()); |
| 1180 | }; |
| 1181 | do_for_partitions(usb, argv[1], slot_override.c_str(), erase, true); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1182 | skip(2); |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1183 | } else if(!strncmp(*argv, "format", strlen("format"))) { |
| 1184 | char *overrides; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1185 | char *type_override = nullptr; |
| 1186 | char *size_override = nullptr; |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 1187 | require(2); |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1188 | /* |
| 1189 | * Parsing for: "format[:[type][:[size]]]" |
| 1190 | * Some valid things: |
| 1191 | * - select ontly the size, and leave default fs type: |
| 1192 | * format::0x4000000 userdata |
| 1193 | * - default fs type and size: |
| 1194 | * format userdata |
| 1195 | * format:: userdata |
| 1196 | */ |
| 1197 | overrides = strchr(*argv, ':'); |
| 1198 | if (overrides) { |
| 1199 | overrides++; |
| 1200 | size_override = strchr(overrides, ':'); |
| 1201 | if (size_override) { |
| 1202 | size_override[0] = '\0'; |
| 1203 | size_override++; |
| 1204 | } |
| 1205 | type_override = overrides; |
| 1206 | } |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1207 | if (type_override && !type_override[0]) type_override = nullptr; |
| 1208 | if (size_override && !size_override[0]) size_override = nullptr; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 1209 | |
| 1210 | auto format = [&](const std::string &partition) { |
| 1211 | if (erase_first && needs_erase(usb, partition.c_str())) { |
| 1212 | fb_queue_erase(partition.c_str()); |
| 1213 | } |
| 1214 | fb_perform_format(usb, partition.c_str(), 0, type_override, size_override); |
| 1215 | }; |
| 1216 | do_for_partitions(usb, argv[1], slot_override.c_str(), format, true); |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 1217 | skip(2); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1218 | } else if(!strcmp(*argv, "signature")) { |
| 1219 | require(2); |
| 1220 | data = load_file(argv[1], &sz); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1221 | if (data == nullptr) 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] | 1222 | if (sz != 256) die("signature must be 256 bytes"); |
| 1223 | fb_queue_download("signature", data, sz); |
| 1224 | fb_queue_command("signature", "installing signature"); |
| 1225 | skip(2); |
| 1226 | } else if(!strcmp(*argv, "reboot")) { |
| 1227 | wants_reboot = 1; |
| 1228 | skip(1); |
Elliott Hughes | ca85df0 | 2015-02-25 10:02:00 -0800 | [diff] [blame] | 1229 | if (argc > 0) { |
| 1230 | if (!strcmp(*argv, "bootloader")) { |
| 1231 | wants_reboot = 0; |
| 1232 | wants_reboot_bootloader = 1; |
| 1233 | skip(1); |
| 1234 | } |
| 1235 | } |
| 1236 | require(0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1237 | } else if(!strcmp(*argv, "reboot-bootloader")) { |
| 1238 | wants_reboot_bootloader = 1; |
| 1239 | skip(1); |
| 1240 | } else if (!strcmp(*argv, "continue")) { |
| 1241 | fb_queue_command("continue", "resuming boot"); |
| 1242 | skip(1); |
| 1243 | } else if(!strcmp(*argv, "boot")) { |
| 1244 | char *kname = 0; |
| 1245 | char *rname = 0; |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 1246 | char *sname = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1247 | skip(1); |
| 1248 | if (argc > 0) { |
| 1249 | kname = argv[0]; |
| 1250 | skip(1); |
| 1251 | } |
| 1252 | if (argc > 0) { |
| 1253 | rname = argv[0]; |
| 1254 | skip(1); |
| 1255 | } |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 1256 | if (argc > 0) { |
| 1257 | sname = argv[0]; |
| 1258 | skip(1); |
| 1259 | } |
| 1260 | data = load_bootable_image(kname, rname, sname, &sz, cmdline); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1261 | if (data == 0) return 1; |
| 1262 | fb_queue_download("boot.img", data, sz); |
| 1263 | fb_queue_command("boot", "booting"); |
| 1264 | } else if(!strcmp(*argv, "flash")) { |
| 1265 | char *pname = argv[1]; |
| 1266 | char *fname = 0; |
| 1267 | require(2); |
| 1268 | if (argc > 2) { |
| 1269 | fname = argv[2]; |
| 1270 | skip(3); |
| 1271 | } else { |
| 1272 | fname = find_item(pname, product); |
| 1273 | skip(2); |
| 1274 | } |
| 1275 | if (fname == 0) die("cannot determine image filename for '%s'", pname); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 1276 | |
| 1277 | auto flash = [&](const std::string &partition) { |
| 1278 | if (erase_first && needs_erase(usb, partition.c_str())) { |
| 1279 | fb_queue_erase(partition.c_str()); |
| 1280 | } |
| 1281 | do_flash(usb, partition.c_str(), fname); |
| 1282 | }; |
| 1283 | do_for_partitions(usb, pname, slot_override.c_str(), flash, true); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1284 | } else if(!strcmp(*argv, "flash:raw")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1285 | char *kname = argv[2]; |
| 1286 | char *rname = 0; |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 1287 | char *sname = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1288 | require(3); |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 1289 | skip(3); |
| 1290 | if (argc > 0) { |
| 1291 | rname = argv[0]; |
| 1292 | skip(1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1293 | } |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 1294 | if (argc > 0) { |
| 1295 | sname = argv[0]; |
| 1296 | skip(1); |
| 1297 | } |
| 1298 | data = load_bootable_image(kname, rname, sname, &sz, cmdline); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1299 | if (data == 0) die("cannot load bootable image"); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 1300 | auto flashraw = [&](const std::string &partition) { |
| 1301 | fb_queue_flash(partition.c_str(), data, sz); |
| 1302 | }; |
| 1303 | do_for_partitions(usb, argv[1], slot_override.c_str(), flashraw, true); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1304 | } else if(!strcmp(*argv, "flashall")) { |
| 1305 | skip(1); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 1306 | do_flashall(usb, slot_override.c_str(), erase_first); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1307 | wants_reboot = 1; |
| 1308 | } else if(!strcmp(*argv, "update")) { |
| 1309 | if (argc > 1) { |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 1310 | do_update(usb, argv[1], slot_override.c_str(), erase_first); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1311 | skip(2); |
| 1312 | } else { |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 1313 | do_update(usb, "update.zip", slot_override.c_str(), erase_first); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1314 | skip(1); |
| 1315 | } |
| 1316 | wants_reboot = 1; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame^] | 1317 | } else if(!strcmp(*argv, "set_active")) { |
| 1318 | require(2); |
| 1319 | fb_set_active(argv[1]); |
| 1320 | skip(2); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1321 | } else if(!strcmp(*argv, "oem")) { |
| 1322 | argc = do_oem_command(argc, argv); |
Badhri Jagan Sridharan | bf11095 | 2015-05-15 16:43:47 -0700 | [diff] [blame] | 1323 | } else if(!strcmp(*argv, "flashing") && argc == 2) { |
| 1324 | if(!strcmp(*(argv+1), "unlock") || !strcmp(*(argv+1), "lock") |
| 1325 | || !strcmp(*(argv+1), "unlock_critical") |
| 1326 | || !strcmp(*(argv+1), "lock_critical") |
| 1327 | || !strcmp(*(argv+1), "get_unlock_ability")) { |
| 1328 | argc = do_oem_command(argc, argv); |
| 1329 | } else { |
| 1330 | usage(); |
| 1331 | return 1; |
| 1332 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1333 | } else { |
| 1334 | usage(); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 1335 | return 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | if (wants_wipe) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1340 | fprintf(stderr, "wiping userdata...\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1341 | fb_queue_erase("userdata"); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1342 | fb_perform_format(usb, "userdata", 1, nullptr, nullptr); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1343 | |
| 1344 | std::string cache_type; |
| 1345 | if (fb_getvar(usb, "partition-type:cache", &cache_type) && !cache_type.empty()) { |
| 1346 | fprintf(stderr, "wiping cache...\n"); |
| 1347 | fb_queue_erase("cache"); |
| 1348 | fb_perform_format(usb, "cache", 1, nullptr, nullptr); |
| 1349 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1350 | } |
| 1351 | if (wants_reboot) { |
| 1352 | fb_queue_reboot(); |
Mark Wachsler | ec25e7b | 2014-06-24 11:04:54 -0400 | [diff] [blame] | 1353 | fb_queue_wait_for_disconnect(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1354 | } else if (wants_reboot_bootloader) { |
| 1355 | fb_queue_command("reboot-bootloader", "rebooting into bootloader"); |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 1356 | fb_queue_wait_for_disconnect(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1357 | } |
| 1358 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1359 | return fb_execute_queue(usb) ? EXIT_FAILURE : EXIT_SUCCESS; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1360 | } |