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