blob: ded54a5b983c1ae88702dd2461fc8053cffdcd6b [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>
Mark Wachsler157b0012013-10-02 09:35:38 -040034#include <sys/time.h>
35
Aaron Wisnerdb511202018-06-26 15:38:35 -050036#include "util.h"
Mark Wachsler157b0012013-10-02 09:35:38 -040037
David Andersonaa87dc52023-01-27 20:39:06 -080038using android::base::borrowed_fd;
39
Elliott Hughes855cdf82018-04-02 14:24:03 -070040static bool g_verbose = false;
41
Elliott Hughes5620d222018-03-28 08:20:00 -070042double now() {
Mark Wachsler157b0012013-10-02 09:35:38 -040043 struct timeval tv;
44 gettimeofday(&tv, NULL);
45 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
46}
47
Elliott Hughes5620d222018-03-28 08:20:00 -070048void die(const char* fmt, ...) {
Mark Wachsler157b0012013-10-02 09:35:38 -040049 va_list ap;
50 va_start(ap, fmt);
Elliott Hughesf238d872018-03-29 14:46:29 -070051 fprintf(stderr, "fastboot: error: ");
Mark Wachsler157b0012013-10-02 09:35:38 -040052 vfprintf(stderr, fmt, ap);
Elliott Hughes855cdf82018-04-02 14:24:03 -070053 fprintf(stderr, "\n");
Mark Wachsler157b0012013-10-02 09:35:38 -040054 va_end(ap);
Elliott Hughes5620d222018-03-28 08:20:00 -070055 exit(EXIT_FAILURE);
56}
57
David Anderson7c84b9f2019-06-27 22:15:29 -070058void die(const std::string& str) {
59 die("%s", str.c_str());
60}
61
Elliott Hughes855cdf82018-04-02 14:24:03 -070062void set_verbose() {
63 g_verbose = true;
64}
65
66void 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 Andersonaa87dc52023-01-27 20:39:06 -080078
79bool 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
97bool is_sparse_file(borrowed_fd fd) {
98 SparsePtr s(sparse_file_import(fd.get(), false, false), sparse_file_destroy);
99 return !!s;
100}
101
102int64_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}