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> |
Peter Collingbourne | cc1bf34 | 2024-02-07 16:55:49 -0800 | [diff] [blame] | 34 | #include <time.h> |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 35 | |
David Anderson | 74c7807 | 2023-03-16 21:21:28 -0700 | [diff] [blame] | 36 | #include <android-base/parseint.h> |
| 37 | #include <android-base/strings.h> |
| 38 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 39 | #include "util.h" |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 40 | |
David Anderson | aa87dc5 | 2023-01-27 20:39:06 -0800 | [diff] [blame] | 41 | using android::base::borrowed_fd; |
| 42 | |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 43 | static bool g_verbose = false; |
| 44 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 45 | double now() { |
Peter Collingbourne | cc1bf34 | 2024-02-07 16:55:49 -0800 | [diff] [blame] | 46 | struct timespec ts; |
| 47 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 48 | return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000; |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 49 | } |
| 50 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 51 | void die(const char* fmt, ...) { |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 52 | va_list ap; |
| 53 | va_start(ap, fmt); |
Elliott Hughes | f238d87 | 2018-03-29 14:46:29 -0700 | [diff] [blame] | 54 | fprintf(stderr, "fastboot: error: "); |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 55 | vfprintf(stderr, fmt, ap); |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 56 | fprintf(stderr, "\n"); |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 57 | va_end(ap); |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 58 | exit(EXIT_FAILURE); |
| 59 | } |
| 60 | |
David Anderson | 7c84b9f | 2019-06-27 22:15:29 -0700 | [diff] [blame] | 61 | void die(const std::string& str) { |
| 62 | die("%s", str.c_str()); |
| 63 | } |
| 64 | |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 65 | void set_verbose() { |
| 66 | g_verbose = true; |
| 67 | } |
| 68 | |
| 69 | void verbose(const char* fmt, ...) { |
| 70 | if (!g_verbose) return; |
| 71 | |
| 72 | if (*fmt != '\n') { |
| 73 | va_list ap; |
| 74 | va_start(ap, fmt); |
| 75 | fprintf(stderr, "fastboot: verbose: "); |
| 76 | vfprintf(stderr, fmt, ap); |
| 77 | va_end(ap); |
| 78 | } |
| 79 | fprintf(stderr, "\n"); |
| 80 | } |
David Anderson | aa87dc5 | 2023-01-27 20:39:06 -0800 | [diff] [blame] | 81 | |
| 82 | bool should_flash_in_userspace(const android::fs_mgr::LpMetadata& metadata, |
| 83 | const std::string& partition_name) { |
| 84 | for (const auto& partition : metadata.partitions) { |
| 85 | auto candidate = android::fs_mgr::GetPartitionName(partition); |
| 86 | if (partition.attributes & LP_PARTITION_ATTR_SLOT_SUFFIXED) { |
| 87 | // On retrofit devices, we don't know if, or whether, the A or B |
| 88 | // slot has been flashed for dynamic partitions. Instead we add |
| 89 | // both names to the list as a conservative guess. |
| 90 | if (candidate + "_a" == partition_name || candidate + "_b" == partition_name) { |
| 91 | return true; |
| 92 | } |
| 93 | } else if (candidate == partition_name) { |
| 94 | return true; |
| 95 | } |
| 96 | } |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | bool is_sparse_file(borrowed_fd fd) { |
| 101 | SparsePtr s(sparse_file_import(fd.get(), false, false), sparse_file_destroy); |
| 102 | return !!s; |
| 103 | } |
| 104 | |
| 105 | int64_t get_file_size(borrowed_fd fd) { |
| 106 | struct stat sb; |
| 107 | if (fstat(fd.get(), &sb) == -1) { |
| 108 | die("could not get file size"); |
| 109 | } |
| 110 | return sb.st_size; |
| 111 | } |
David Anderson | 74c7807 | 2023-03-16 21:21:28 -0700 | [diff] [blame] | 112 | |
| 113 | std::string fb_fix_numeric_var(std::string var) { |
| 114 | // Some bootloaders (angler, for example), send spurious leading whitespace. |
| 115 | var = android::base::Trim(var); |
| 116 | // Some bootloaders (hammerhead, for example) use implicit hex. |
| 117 | // This code used to use strtol with base 16. |
| 118 | if (!android::base::StartsWith(var, "0x")) var = "0x" + var; |
| 119 | return var; |
| 120 | } |