• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
33 #include <sys/stat.h>
34 #include <sys/time.h>
35 
36 #include <android-base/parseint.h>
37 #include <android-base/strings.h>
38 
39 #include "util.h"
40 
41 using android::base::borrowed_fd;
42 
43 static bool g_verbose = false;
44 
now()45 double now() {
46     struct timeval tv;
47     gettimeofday(&tv, NULL);
48     return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
49 }
50 
die(const char * fmt,...)51 void die(const char* fmt, ...) {
52     va_list ap;
53     va_start(ap, fmt);
54     fprintf(stderr, "fastboot: error: ");
55     vfprintf(stderr, fmt, ap);
56     fprintf(stderr, "\n");
57     va_end(ap);
58     exit(EXIT_FAILURE);
59 }
60 
die(const std::string & str)61 void die(const std::string& str) {
62     die("%s", str.c_str());
63 }
64 
set_verbose()65 void set_verbose() {
66     g_verbose = true;
67 }
68 
verbose(const char * fmt,...)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 }
81 
should_flash_in_userspace(const android::fs_mgr::LpMetadata & metadata,const std::string & partition_name)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 
is_sparse_file(borrowed_fd fd)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 
get_file_size(borrowed_fd fd)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 }
112 
fb_fix_numeric_var(std::string var)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 }
121