blob: 5966aead6a1b7b2429be1a238a8e6f6160ec1965 [file] [log] [blame]
Mark Wachsler157b0012013-10-02 09:35:38 -04001/*
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 Andersonaa87dc52023-01-27 20:39:06 -080033#include <sys/stat.h>
Peter Collingbournecc1bf342024-02-07 16:55:49 -080034#include <time.h>
Mark Wachsler157b0012013-10-02 09:35:38 -040035
David Anderson74c78072023-03-16 21:21:28 -070036#include <android-base/parseint.h>
37#include <android-base/strings.h>
38
Aaron Wisnerdb511202018-06-26 15:38:35 -050039#include "util.h"
Mark Wachsler157b0012013-10-02 09:35:38 -040040
David Andersonaa87dc52023-01-27 20:39:06 -080041using android::base::borrowed_fd;
42
Elliott Hughes855cdf82018-04-02 14:24:03 -070043static bool g_verbose = false;
44
Elliott Hughes5620d222018-03-28 08:20:00 -070045double now() {
Peter Collingbournecc1bf342024-02-07 16:55:49 -080046 struct timespec ts;
47 clock_gettime(CLOCK_MONOTONIC, &ts);
48 return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000;
Mark Wachsler157b0012013-10-02 09:35:38 -040049}
50
Elliott Hughes5620d222018-03-28 08:20:00 -070051void die(const char* fmt, ...) {
Mark Wachsler157b0012013-10-02 09:35:38 -040052 va_list ap;
53 va_start(ap, fmt);
Elliott Hughesf238d872018-03-29 14:46:29 -070054 fprintf(stderr, "fastboot: error: ");
Mark Wachsler157b0012013-10-02 09:35:38 -040055 vfprintf(stderr, fmt, ap);
Elliott Hughes855cdf82018-04-02 14:24:03 -070056 fprintf(stderr, "\n");
Mark Wachsler157b0012013-10-02 09:35:38 -040057 va_end(ap);
Elliott Hughes5620d222018-03-28 08:20:00 -070058 exit(EXIT_FAILURE);
59}
60
David Anderson7c84b9f2019-06-27 22:15:29 -070061void die(const std::string& str) {
62 die("%s", str.c_str());
63}
64
Elliott Hughes855cdf82018-04-02 14:24:03 -070065void set_verbose() {
66 g_verbose = true;
67}
68
69void 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 Andersonaa87dc52023-01-27 20:39:06 -080081
82bool 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
100bool is_sparse_file(borrowed_fd fd) {
101 SparsePtr s(sparse_file_import(fd.get(), false, false), sparse_file_destroy);
102 return !!s;
103}
104
105int64_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 Anderson74c78072023-03-16 21:21:28 -0700112
113std::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}