Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 |
| 12 | * the documentation and/or other materials provided with the |
| 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 |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 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 | |
| 29 | #include <stdarg.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
David Anderson | aa87dc5 | 2023-01-27 20:39:06 -0800 | [diff] [blame^] | 33 | #include <sys/stat.h> |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 34 | #include <sys/time.h> |
| 35 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 36 | #include "util.h" |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 37 | |
David Anderson | aa87dc5 | 2023-01-27 20:39:06 -0800 | [diff] [blame^] | 38 | using android::base::borrowed_fd; |
| 39 | |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 40 | static bool g_verbose = false; |
| 41 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 42 | double now() { |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 43 | struct timeval tv; |
| 44 | gettimeofday(&tv, NULL); |
| 45 | return (double)tv.tv_sec + (double)tv.tv_usec / 1000000; |
| 46 | } |
| 47 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 48 | void die(const char* fmt, ...) { |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 49 | va_list ap; |
| 50 | va_start(ap, fmt); |
Elliott Hughes | f238d87 | 2018-03-29 14:46:29 -0700 | [diff] [blame] | 51 | fprintf(stderr, "fastboot: error: "); |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 52 | vfprintf(stderr, fmt, ap); |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 53 | fprintf(stderr, "\n"); |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 54 | va_end(ap); |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 55 | exit(EXIT_FAILURE); |
| 56 | } |
| 57 | |
David Anderson | 7c84b9f | 2019-06-27 22:15:29 -0700 | [diff] [blame] | 58 | void die(const std::string& str) { |
| 59 | die("%s", str.c_str()); |
| 60 | } |
| 61 | |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 62 | void set_verbose() { |
| 63 | g_verbose = true; |
| 64 | } |
| 65 | |
| 66 | void verbose(const char* fmt, ...) { |
| 67 | if (!g_verbose) return; |
| 68 | |
| 69 | if (*fmt != '\n') { |
| 70 | va_list ap; |
| 71 | va_start(ap, fmt); |
| 72 | fprintf(stderr, "fastboot: verbose: "); |
| 73 | vfprintf(stderr, fmt, ap); |
| 74 | va_end(ap); |
| 75 | } |
| 76 | fprintf(stderr, "\n"); |
| 77 | } |
David Anderson | aa87dc5 | 2023-01-27 20:39:06 -0800 | [diff] [blame^] | 78 | |
| 79 | bool should_flash_in_userspace(const android::fs_mgr::LpMetadata& metadata, |
| 80 | const std::string& partition_name) { |
| 81 | for (const auto& partition : metadata.partitions) { |
| 82 | auto candidate = android::fs_mgr::GetPartitionName(partition); |
| 83 | if (partition.attributes & LP_PARTITION_ATTR_SLOT_SUFFIXED) { |
| 84 | // On retrofit devices, we don't know if, or whether, the A or B |
| 85 | // slot has been flashed for dynamic partitions. Instead we add |
| 86 | // both names to the list as a conservative guess. |
| 87 | if (candidate + "_a" == partition_name || candidate + "_b" == partition_name) { |
| 88 | return true; |
| 89 | } |
| 90 | } else if (candidate == partition_name) { |
| 91 | return true; |
| 92 | } |
| 93 | } |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | bool is_sparse_file(borrowed_fd fd) { |
| 98 | SparsePtr s(sparse_file_import(fd.get(), false, false), sparse_file_destroy); |
| 99 | return !!s; |
| 100 | } |
| 101 | |
| 102 | int64_t get_file_size(borrowed_fd fd) { |
| 103 | struct stat sb; |
| 104 | if (fstat(fd.get(), &sb) == -1) { |
| 105 | die("could not get file size"); |
| 106 | } |
| 107 | return sb.st_size; |
| 108 | } |