| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2012 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/common/utils.h" | 
| Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 18 |  | 
| Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 19 | #include <stdint.h> | 
|  | 20 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 21 | #include <dirent.h> | 
| Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 22 | #include <elf.h> | 
| Alex Deymo | 6f20dd4 | 2015-08-18 16:42:46 -0700 | [diff] [blame] | 23 | #include <endian.h> | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 24 | #include <errno.h> | 
| Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 25 | #include <fcntl.h> | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 26 | #include <stdio.h> | 
|  | 27 | #include <stdlib.h> | 
|  | 28 | #include <string.h> | 
| Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 29 | #include <sys/mount.h> | 
|  | 30 | #include <sys/resource.h> | 
|  | 31 | #include <sys/stat.h> | 
|  | 32 | #include <sys/types.h> | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 33 | #include <unistd.h> | 
| Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 34 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 35 | #include <algorithm> | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 36 | #include <utility> | 
| Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 37 | #include <vector> | 
| Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 38 |  | 
| Alex Vakulenko | 4906c1c | 2014-08-21 13:17:44 -0700 | [diff] [blame] | 39 | #include <base/callback.h> | 
| Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 40 | #include <base/files/file_path.h> | 
| Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 41 | #include <base/files/file_util.h> | 
| Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 42 | #include <base/files/scoped_file.h> | 
| Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 43 | #include <base/format_macros.h> | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 44 | #include <base/location.h> | 
| Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 45 | #include <base/logging.h> | 
| Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 46 | #include <base/posix/eintr_wrapper.h> | 
| Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 47 | #include <base/rand_util.h> | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 48 | #include <base/strings/string_number_conversions.h> | 
|  | 49 | #include <base/strings/string_split.h> | 
|  | 50 | #include <base/strings/string_util.h> | 
|  | 51 | #include <base/strings/stringprintf.h> | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 52 | #include <brillo/data_encoding.h> | 
| Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 53 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 54 | #include "update_engine/common/clock_interface.h" | 
|  | 55 | #include "update_engine/common/constants.h" | 
| Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 56 | #include "update_engine/common/platform_constants.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 57 | #include "update_engine/common/prefs_interface.h" | 
|  | 58 | #include "update_engine/common/subprocess.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 59 | #include "update_engine/payload_consumer/file_descriptor.h" | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 60 |  | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 61 | using base::Time; | 
| Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 62 | using base::TimeDelta; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 63 | using std::min; | 
| Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 64 | using std::pair; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 65 | using std::string; | 
|  | 66 | using std::vector; | 
|  | 67 |  | 
|  | 68 | namespace chromeos_update_engine { | 
|  | 69 |  | 
| Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 70 | namespace { | 
|  | 71 |  | 
|  | 72 | // The following constants control how UnmountFilesystem should retry if | 
|  | 73 | // umount() fails with an errno EBUSY, i.e. retry 5 times over the course of | 
|  | 74 | // one second. | 
|  | 75 | const int kUnmountMaxNumOfRetries = 5; | 
|  | 76 | const int kUnmountRetryIntervalInMicroseconds = 200 * 1000;  // 200 ms | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 77 |  | 
|  | 78 | // Number of bytes to read from a file to attempt to detect its contents. Used | 
|  | 79 | // in GetFileFormat. | 
|  | 80 | const int kGetFileFormatMaxHeaderSize = 32; | 
|  | 81 |  | 
| Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 82 | // The path to the kernel's boot_id. | 
|  | 83 | const char kBootIdPath[] = "/proc/sys/kernel/random/boot_id"; | 
|  | 84 |  | 
| Alex Deymo | 3295102 | 2016-08-10 17:02:49 -0700 | [diff] [blame] | 85 | // A pointer to a null-terminated string containing the root directory where all | 
|  | 86 | // the temporary files should be created. If null, the system default is used | 
|  | 87 | // instead. | 
|  | 88 | const char* root_temp_dir = nullptr; | 
|  | 89 |  | 
| Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 90 | // Return true if |disk_name| is an MTD or a UBI device. Note that this test is | 
|  | 91 | // simply based on the name of the device. | 
|  | 92 | bool IsMtdDeviceName(const string& disk_name) { | 
| Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 93 | return base::StartsWith(disk_name, "/dev/ubi", | 
|  | 94 | base::CompareCase::SENSITIVE) || | 
|  | 95 | base::StartsWith(disk_name, "/dev/mtd", base::CompareCase::SENSITIVE); | 
| Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
|  | 98 | // Return the device name for the corresponding partition on a NAND device. | 
|  | 99 | // WARNING: This function returns device names that are not mountable. | 
|  | 100 | string MakeNandPartitionName(int partition_num) { | 
|  | 101 | switch (partition_num) { | 
|  | 102 | case 2: | 
|  | 103 | case 4: | 
|  | 104 | case 6: { | 
|  | 105 | return base::StringPrintf("/dev/mtd%d", partition_num); | 
|  | 106 | } | 
|  | 107 | default: { | 
|  | 108 | return base::StringPrintf("/dev/ubi%d_0", partition_num); | 
|  | 109 | } | 
|  | 110 | } | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | // Return the device name for the corresponding partition on a NAND device that | 
|  | 114 | // may be mountable (but may not be writable). | 
|  | 115 | string MakeNandPartitionNameForMount(int partition_num) { | 
|  | 116 | switch (partition_num) { | 
|  | 117 | case 2: | 
|  | 118 | case 4: | 
|  | 119 | case 6: { | 
|  | 120 | return base::StringPrintf("/dev/mtd%d", partition_num); | 
|  | 121 | } | 
|  | 122 | case 3: | 
|  | 123 | case 5: | 
|  | 124 | case 7: { | 
|  | 125 | return base::StringPrintf("/dev/ubiblock%d_0", partition_num); | 
|  | 126 | } | 
|  | 127 | default: { | 
|  | 128 | return base::StringPrintf("/dev/ubi%d_0", partition_num); | 
|  | 129 | } | 
|  | 130 | } | 
|  | 131 | } | 
|  | 132 |  | 
| Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 133 | // If |path| is absolute, or explicit relative to the current working directory, | 
|  | 134 | // leaves it as is. Otherwise, uses the system's temp directory, as defined by | 
|  | 135 | // base::GetTempDir() and prepends it to |path|. On success stores the full | 
|  | 136 | // temporary path in |template_path| and returns true. | 
|  | 137 | bool GetTempName(const string& path, base::FilePath* template_path) { | 
| Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 138 | if (path[0] == '/' || | 
|  | 139 | base::StartsWith(path, "./", base::CompareCase::SENSITIVE) || | 
|  | 140 | base::StartsWith(path, "../", base::CompareCase::SENSITIVE)) { | 
| Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 141 | *template_path = base::FilePath(path); | 
|  | 142 | return true; | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | base::FilePath temp_dir; | 
| Alex Deymo | 3295102 | 2016-08-10 17:02:49 -0700 | [diff] [blame] | 146 | if (root_temp_dir) { | 
|  | 147 | temp_dir = base::FilePath(root_temp_dir); | 
|  | 148 | } else { | 
| Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 149 | #ifdef __ANDROID__ | 
| Alex Deymo | 3295102 | 2016-08-10 17:02:49 -0700 | [diff] [blame] | 150 | temp_dir = base::FilePath(constants::kNonVolatileDirectory).Append("tmp"); | 
|  | 151 | #else | 
|  | 152 | TEST_AND_RETURN_FALSE(base::GetTempDir(&temp_dir)); | 
|  | 153 | #endif  // __ANDROID__ | 
|  | 154 | } | 
| Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 155 | if (!base::PathExists(temp_dir)) | 
|  | 156 | TEST_AND_RETURN_FALSE(base::CreateDirectory(temp_dir)); | 
| Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 157 | *template_path = temp_dir.Append(path); | 
|  | 158 | return true; | 
|  | 159 | } | 
|  | 160 |  | 
| Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 161 | }  // namespace | 
|  | 162 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 163 | namespace utils { | 
|  | 164 |  | 
| Alex Deymo | 3295102 | 2016-08-10 17:02:49 -0700 | [diff] [blame] | 165 | void SetRootTempDir(const char* new_root_temp_dir) { | 
|  | 166 | root_temp_dir = new_root_temp_dir; | 
|  | 167 | } | 
|  | 168 |  | 
| J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 169 | string ParseECVersion(string input_line) { | 
| Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 170 | base::TrimWhitespaceASCII(input_line, base::TRIM_ALL, &input_line); | 
| Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 171 |  | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 172 | // At this point we want to convert the format key=value pair from mosys to | 
| Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 173 | // a vector of key value pairs. | 
| Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 174 | vector<pair<string, string>> kv_pairs; | 
| J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 175 | if (base::SplitStringIntoKeyValuePairs(input_line, '=', ' ', &kv_pairs)) { | 
| Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 176 | for (const pair<string, string>& kv_pair : kv_pairs) { | 
| Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 177 | // Finally match against the fw_verion which may have quotes. | 
| Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 178 | if (kv_pair.first == "fw_version") { | 
| Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 179 | string output; | 
|  | 180 | // Trim any quotes. | 
| Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 181 | base::TrimString(kv_pair.second, "\"", &output); | 
| Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 182 | return output; | 
|  | 183 | } | 
|  | 184 | } | 
|  | 185 | } | 
|  | 186 | LOG(ERROR) << "Unable to parse fwid from ec info."; | 
|  | 187 | return ""; | 
|  | 188 | } | 
|  | 189 |  | 
| Alex Deymo | 335516c | 2016-11-28 13:37:06 -0800 | [diff] [blame] | 190 | bool WriteFile(const char* path, const void* data, size_t data_len) { | 
|  | 191 | int fd = HANDLE_EINTR(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600)); | 
|  | 192 | TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); | 
|  | 193 | ScopedFdCloser fd_closer(&fd); | 
|  | 194 | return WriteAll(fd, data, data_len); | 
| Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 195 | } | 
|  | 196 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 197 | bool ReadAll( | 
|  | 198 | int fd, void* buf, size_t count, size_t* out_bytes_read, bool* eof) { | 
|  | 199 | char* c_buf = static_cast<char*>(buf); | 
|  | 200 | size_t bytes_read = 0; | 
|  | 201 | *eof = false; | 
|  | 202 | while (bytes_read < count) { | 
|  | 203 | ssize_t rc = HANDLE_EINTR(read(fd, c_buf + bytes_read, count - bytes_read)); | 
|  | 204 | if (rc < 0) { | 
|  | 205 | // EAGAIN and EWOULDBLOCK are normal return values when there's no more | 
|  | 206 | // input and we are in non-blocking mode. | 
|  | 207 | if (errno != EWOULDBLOCK && errno != EAGAIN) { | 
|  | 208 | PLOG(ERROR) << "Error reading fd " << fd; | 
|  | 209 | *out_bytes_read = bytes_read; | 
|  | 210 | return false; | 
|  | 211 | } | 
|  | 212 | break; | 
|  | 213 | } else if (rc == 0) { | 
|  | 214 | // A value of 0 means that we reached EOF and there is nothing else to | 
|  | 215 | // read from this fd. | 
|  | 216 | *eof = true; | 
|  | 217 | break; | 
|  | 218 | } else { | 
|  | 219 | bytes_read += rc; | 
|  | 220 | } | 
|  | 221 | } | 
|  | 222 | *out_bytes_read = bytes_read; | 
|  | 223 | return true; | 
|  | 224 | } | 
|  | 225 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 226 | bool WriteAll(int fd, const void* buf, size_t count) { | 
| Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 227 | const char* c_buf = static_cast<const char*>(buf); | 
|  | 228 | ssize_t bytes_written = 0; | 
|  | 229 | while (bytes_written < static_cast<ssize_t>(count)) { | 
|  | 230 | ssize_t rc = write(fd, c_buf + bytes_written, count - bytes_written); | 
|  | 231 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); | 
|  | 232 | bytes_written += rc; | 
|  | 233 | } | 
|  | 234 | return true; | 
|  | 235 | } | 
|  | 236 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 237 | bool PWriteAll(int fd, const void* buf, size_t count, off_t offset) { | 
|  | 238 | const char* c_buf = static_cast<const char*>(buf); | 
| Gilad Arnold | 780db21 | 2012-07-11 13:12:49 -0700 | [diff] [blame] | 239 | size_t bytes_written = 0; | 
|  | 240 | int num_attempts = 0; | 
|  | 241 | while (bytes_written < count) { | 
|  | 242 | num_attempts++; | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 243 | ssize_t rc = pwrite(fd, c_buf + bytes_written, count - bytes_written, | 
|  | 244 | offset + bytes_written); | 
| Gilad Arnold | 780db21 | 2012-07-11 13:12:49 -0700 | [diff] [blame] | 245 | // TODO(garnold) for debugging failure in chromium-os:31077; to be removed. | 
|  | 246 | if (rc < 0) { | 
|  | 247 | PLOG(ERROR) << "pwrite error; num_attempts=" << num_attempts | 
|  | 248 | << " bytes_written=" << bytes_written | 
|  | 249 | << " count=" << count << " offset=" << offset; | 
|  | 250 | } | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 251 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); | 
|  | 252 | bytes_written += rc; | 
|  | 253 | } | 
|  | 254 | return true; | 
|  | 255 | } | 
|  | 256 |  | 
| Chih-Hung Hsieh | 5c6bb1d | 2016-07-27 13:33:15 -0700 | [diff] [blame] | 257 | bool WriteAll(const FileDescriptorPtr& fd, const void* buf, size_t count) { | 
| Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 258 | const char* c_buf = static_cast<const char*>(buf); | 
|  | 259 | ssize_t bytes_written = 0; | 
|  | 260 | while (bytes_written < static_cast<ssize_t>(count)) { | 
|  | 261 | ssize_t rc = fd->Write(c_buf + bytes_written, count - bytes_written); | 
|  | 262 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); | 
|  | 263 | bytes_written += rc; | 
|  | 264 | } | 
|  | 265 | return true; | 
|  | 266 | } | 
|  | 267 |  | 
| Chih-Hung Hsieh | 5c6bb1d | 2016-07-27 13:33:15 -0700 | [diff] [blame] | 268 | bool PWriteAll(const FileDescriptorPtr& fd, | 
| Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 269 | const void* buf, | 
|  | 270 | size_t count, | 
|  | 271 | off_t offset) { | 
| Allie Wood | 0c8cf7e | 2015-04-23 19:24:49 -0700 | [diff] [blame] | 272 | TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(offset, SEEK_SET) != | 
|  | 273 | static_cast<off_t>(-1)); | 
| Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 274 | return WriteAll(fd, buf, count); | 
|  | 275 | } | 
|  | 276 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 277 | bool PReadAll(int fd, void* buf, size_t count, off_t offset, | 
|  | 278 | ssize_t* out_bytes_read) { | 
|  | 279 | char* c_buf = static_cast<char*>(buf); | 
|  | 280 | ssize_t bytes_read = 0; | 
|  | 281 | while (bytes_read < static_cast<ssize_t>(count)) { | 
|  | 282 | ssize_t rc = pread(fd, c_buf + bytes_read, count - bytes_read, | 
|  | 283 | offset + bytes_read); | 
|  | 284 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); | 
|  | 285 | if (rc == 0) { | 
|  | 286 | break; | 
|  | 287 | } | 
|  | 288 | bytes_read += rc; | 
|  | 289 | } | 
|  | 290 | *out_bytes_read = bytes_read; | 
|  | 291 | return true; | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 292 | } | 
|  | 293 |  | 
| Chih-Hung Hsieh | 5c6bb1d | 2016-07-27 13:33:15 -0700 | [diff] [blame] | 294 | bool PReadAll(const FileDescriptorPtr& fd, void* buf, size_t count, off_t offset, | 
| Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 295 | ssize_t* out_bytes_read) { | 
| Allie Wood | 0c8cf7e | 2015-04-23 19:24:49 -0700 | [diff] [blame] | 296 | TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(offset, SEEK_SET) != | 
|  | 297 | static_cast<off_t>(-1)); | 
| Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 298 | char* c_buf = static_cast<char*>(buf); | 
|  | 299 | ssize_t bytes_read = 0; | 
|  | 300 | while (bytes_read < static_cast<ssize_t>(count)) { | 
|  | 301 | ssize_t rc = fd->Read(c_buf + bytes_read, count - bytes_read); | 
|  | 302 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); | 
|  | 303 | if (rc == 0) { | 
|  | 304 | break; | 
|  | 305 | } | 
|  | 306 | bytes_read += rc; | 
|  | 307 | } | 
|  | 308 | *out_bytes_read = bytes_read; | 
|  | 309 | return true; | 
|  | 310 | } | 
|  | 311 |  | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 312 | // Append |nbytes| of content from |buf| to the vector pointed to by either | 
|  | 313 | // |vec_p| or |str_p|. | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 314 | static void AppendBytes(const uint8_t* buf, size_t nbytes, | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 315 | brillo::Blob* vec_p) { | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 316 | CHECK(buf); | 
|  | 317 | CHECK(vec_p); | 
|  | 318 | vec_p->insert(vec_p->end(), buf, buf + nbytes); | 
|  | 319 | } | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 320 | static void AppendBytes(const uint8_t* buf, size_t nbytes, | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 321 | string* str_p) { | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 322 | CHECK(buf); | 
|  | 323 | CHECK(str_p); | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 324 | str_p->append(buf, buf + nbytes); | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 325 | } | 
|  | 326 |  | 
|  | 327 | // Reads from an open file |fp|, appending the read content to the container | 
|  | 328 | // pointer to by |out_p|.  Returns true upon successful reading all of the | 
| Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 329 | // file's content, false otherwise. If |size| is not -1, reads up to |size| | 
|  | 330 | // bytes. | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 331 | template <class T> | 
| Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 332 | static bool Read(FILE* fp, off_t size, T* out_p) { | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 333 | CHECK(fp); | 
| Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 334 | CHECK(size == -1 || size >= 0); | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 335 | uint8_t buf[1024]; | 
| Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 336 | while (size == -1 || size > 0) { | 
|  | 337 | off_t bytes_to_read = sizeof(buf); | 
|  | 338 | if (size > 0 && bytes_to_read > size) { | 
|  | 339 | bytes_to_read = size; | 
|  | 340 | } | 
|  | 341 | size_t nbytes = fread(buf, 1, bytes_to_read, fp); | 
|  | 342 | if (!nbytes) { | 
|  | 343 | break; | 
|  | 344 | } | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 345 | AppendBytes(buf, nbytes, out_p); | 
| Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 346 | if (size != -1) { | 
|  | 347 | CHECK(size >= static_cast<off_t>(nbytes)); | 
|  | 348 | size -= nbytes; | 
|  | 349 | } | 
|  | 350 | } | 
|  | 351 | if (ferror(fp)) { | 
|  | 352 | return false; | 
|  | 353 | } | 
|  | 354 | return size == 0 || feof(fp); | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 355 | } | 
|  | 356 |  | 
| Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 357 | // Opens a file |path| for reading and appends its the contents to a container | 
|  | 358 | // |out_p|. Starts reading the file from |offset|. If |offset| is beyond the end | 
|  | 359 | // of the file, returns success. If |size| is not -1, reads up to |size| bytes. | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 360 | template <class T> | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 361 | static bool ReadFileChunkAndAppend(const string& path, | 
| Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 362 | off_t offset, | 
|  | 363 | off_t size, | 
|  | 364 | T* out_p) { | 
|  | 365 | CHECK_GE(offset, 0); | 
|  | 366 | CHECK(size == -1 || size >= 0); | 
| Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 367 | base::ScopedFILE fp(fopen(path.c_str(), "r")); | 
| Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 368 | if (!fp.get()) | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 369 | return false; | 
| Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 370 | if (offset) { | 
|  | 371 | // Return success without appending any data if a chunk beyond the end of | 
|  | 372 | // the file is requested. | 
|  | 373 | if (offset >= FileSize(path)) { | 
|  | 374 | return true; | 
|  | 375 | } | 
|  | 376 | TEST_AND_RETURN_FALSE_ERRNO(fseek(fp.get(), offset, SEEK_SET) == 0); | 
|  | 377 | } | 
|  | 378 | return Read(fp.get(), size, out_p); | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 379 | } | 
|  | 380 |  | 
| Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 381 | // TODO(deymo): This is only used in unittest, but requires the private | 
|  | 382 | // Read<string>() defined here. Expose Read<string>() or move to base/ version. | 
|  | 383 | bool ReadPipe(const string& cmd, string* out_p) { | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 384 | FILE* fp = popen(cmd.c_str(), "r"); | 
|  | 385 | if (!fp) | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 386 | return false; | 
| Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 387 | bool success = Read(fp, -1, out_p); | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 388 | return (success && pclose(fp) >= 0); | 
|  | 389 | } | 
|  | 390 |  | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 391 | bool ReadFile(const string& path, brillo::Blob* out_p) { | 
| Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 392 | return ReadFileChunkAndAppend(path, 0, -1, out_p); | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 393 | } | 
|  | 394 |  | 
| Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 395 | bool ReadFile(const string& path, string* out_p) { | 
|  | 396 | return ReadFileChunkAndAppend(path, 0, -1, out_p); | 
| Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 397 | } | 
|  | 398 |  | 
| Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 399 | bool ReadFileChunk(const string& path, off_t offset, off_t size, | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 400 | brillo::Blob* out_p) { | 
| Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 401 | return ReadFileChunkAndAppend(path, offset, size, out_p); | 
|  | 402 | } | 
|  | 403 |  | 
| Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 404 | off_t BlockDevSize(int fd) { | 
|  | 405 | uint64_t dev_size; | 
|  | 406 | int rc = ioctl(fd, BLKGETSIZE64, &dev_size); | 
|  | 407 | if (rc == -1) { | 
|  | 408 | dev_size = -1; | 
|  | 409 | PLOG(ERROR) << "Error running ioctl(BLKGETSIZE64) on " << fd; | 
|  | 410 | } | 
|  | 411 | return dev_size; | 
|  | 412 | } | 
|  | 413 |  | 
| Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 414 | off_t FileSize(int fd) { | 
| Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 415 | struct stat stbuf; | 
| Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 416 | int rc = fstat(fd, &stbuf); | 
| Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 417 | CHECK_EQ(rc, 0); | 
| Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 418 | if (rc < 0) { | 
|  | 419 | PLOG(ERROR) << "Error stat-ing " << fd; | 
| Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 420 | return rc; | 
| Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 421 | } | 
|  | 422 | if (S_ISREG(stbuf.st_mode)) | 
|  | 423 | return stbuf.st_size; | 
|  | 424 | if (S_ISBLK(stbuf.st_mode)) | 
|  | 425 | return BlockDevSize(fd); | 
|  | 426 | LOG(ERROR) << "Couldn't determine the type of " << fd; | 
|  | 427 | return -1; | 
|  | 428 | } | 
|  | 429 |  | 
|  | 430 | off_t FileSize(const string& path) { | 
|  | 431 | int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); | 
|  | 432 | if (fd == -1) { | 
|  | 433 | PLOG(ERROR) << "Error opening " << path; | 
|  | 434 | return fd; | 
|  | 435 | } | 
|  | 436 | off_t size = FileSize(fd); | 
|  | 437 | if (size == -1) | 
|  | 438 | PLOG(ERROR) << "Error getting file size of " << path; | 
|  | 439 | close(fd); | 
|  | 440 | return size; | 
| Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 441 | } | 
|  | 442 |  | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 443 | void HexDumpArray(const uint8_t* const arr, const size_t length) { | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 444 | LOG(INFO) << "Logging array of length: " << length; | 
|  | 445 | const unsigned int bytes_per_line = 16; | 
| Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 446 | for (uint32_t i = 0; i < length; i += bytes_per_line) { | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 447 | const unsigned int bytes_remaining = length - i; | 
|  | 448 | const unsigned int bytes_per_this_line = min(bytes_per_line, | 
|  | 449 | bytes_remaining); | 
|  | 450 | char header[100]; | 
|  | 451 | int r = snprintf(header, sizeof(header), "0x%08x : ", i); | 
|  | 452 | TEST_AND_RETURN(r == 13); | 
|  | 453 | string line = header; | 
|  | 454 | for (unsigned int j = 0; j < bytes_per_this_line; j++) { | 
|  | 455 | char buf[20]; | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 456 | uint8_t c = arr[i + j]; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 457 | r = snprintf(buf, sizeof(buf), "%02x ", static_cast<unsigned int>(c)); | 
|  | 458 | TEST_AND_RETURN(r == 3); | 
|  | 459 | line += buf; | 
|  | 460 | } | 
|  | 461 | LOG(INFO) << line; | 
|  | 462 | } | 
|  | 463 | } | 
|  | 464 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 465 | bool SplitPartitionName(const string& partition_name, | 
|  | 466 | string* out_disk_name, | 
| Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 467 | int* out_partition_num) { | 
| Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 468 | if (!base::StartsWith(partition_name, "/dev/", | 
|  | 469 | base::CompareCase::SENSITIVE)) { | 
| Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 470 | LOG(ERROR) << "Invalid partition device name: " << partition_name; | 
|  | 471 | return false; | 
| Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 472 | } | 
| Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 473 |  | 
| Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 474 | size_t last_nondigit_pos = partition_name.find_last_not_of("0123456789"); | 
|  | 475 | if (last_nondigit_pos == string::npos || | 
|  | 476 | (last_nondigit_pos + 1) == partition_name.size()) { | 
|  | 477 | LOG(ERROR) << "Unable to parse partition device name: " << partition_name; | 
| Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 478 | return false; | 
|  | 479 | } | 
|  | 480 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 481 | size_t partition_name_len = string::npos; | 
| Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 482 | if (partition_name[last_nondigit_pos] == '_') { | 
|  | 483 | // NAND block devices have weird naming which could be something | 
|  | 484 | // like "/dev/ubiblock2_0". We discard "_0" in such a case. | 
|  | 485 | size_t prev_nondigit_pos = | 
|  | 486 | partition_name.find_last_not_of("0123456789", last_nondigit_pos - 1); | 
|  | 487 | if (prev_nondigit_pos == string::npos || | 
|  | 488 | (prev_nondigit_pos + 1) == last_nondigit_pos) { | 
|  | 489 | LOG(ERROR) << "Unable to parse partition device name: " << partition_name; | 
|  | 490 | return false; | 
|  | 491 | } | 
|  | 492 |  | 
|  | 493 | partition_name_len = last_nondigit_pos - prev_nondigit_pos; | 
|  | 494 | last_nondigit_pos = prev_nondigit_pos; | 
| Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 495 | } | 
|  | 496 |  | 
|  | 497 | if (out_disk_name) { | 
|  | 498 | // Special case for MMC devices which have the following naming scheme: | 
|  | 499 | // mmcblk0p2 | 
| Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 500 | size_t disk_name_len = last_nondigit_pos; | 
|  | 501 | if (partition_name[last_nondigit_pos] != 'p' || | 
|  | 502 | last_nondigit_pos == 0 || | 
|  | 503 | !isdigit(partition_name[last_nondigit_pos - 1])) { | 
|  | 504 | disk_name_len++; | 
|  | 505 | } | 
|  | 506 | *out_disk_name = partition_name.substr(0, disk_name_len); | 
| Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 507 | } | 
|  | 508 |  | 
|  | 509 | if (out_partition_num) { | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 510 | string partition_str = partition_name.substr(last_nondigit_pos + 1, | 
|  | 511 | partition_name_len); | 
| Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 512 | *out_partition_num = atoi(partition_str.c_str()); | 
|  | 513 | } | 
|  | 514 | return true; | 
|  | 515 | } | 
|  | 516 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 517 | string MakePartitionName(const string& disk_name, int partition_num) { | 
| Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 518 | if (partition_num < 1) { | 
|  | 519 | LOG(ERROR) << "Invalid partition number: " << partition_num; | 
|  | 520 | return string(); | 
|  | 521 | } | 
|  | 522 |  | 
| Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 523 | if (!base::StartsWith(disk_name, "/dev/", base::CompareCase::SENSITIVE)) { | 
| Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 524 | LOG(ERROR) << "Invalid disk name: " << disk_name; | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 525 | return string(); | 
| Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 526 | } | 
|  | 527 |  | 
| Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 528 | if (IsMtdDeviceName(disk_name)) { | 
|  | 529 | // Special case for UBI block devices. | 
|  | 530 | //   1. ubiblock is not writable, we need to use plain "ubi". | 
|  | 531 | //   2. There is a "_0" suffix. | 
|  | 532 | return MakeNandPartitionName(partition_num); | 
| Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 533 | } | 
|  | 534 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 535 | string partition_name = disk_name; | 
| Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 536 | if (isdigit(partition_name.back())) { | 
|  | 537 | // Special case for devices with names ending with a digit. | 
|  | 538 | // Add "p" to separate the disk name from partition number, | 
|  | 539 | // e.g. "/dev/loop0p2" | 
| Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 540 | partition_name += 'p'; | 
|  | 541 | } | 
|  | 542 |  | 
| Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 543 | partition_name += std::to_string(partition_num); | 
|  | 544 |  | 
| Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 545 | return partition_name; | 
| Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 546 | } | 
|  | 547 |  | 
| Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 548 | string MakePartitionNameForMount(const string& part_name) { | 
|  | 549 | if (IsMtdDeviceName(part_name)) { | 
|  | 550 | int partition_num; | 
|  | 551 | if (!SplitPartitionName(part_name, nullptr, &partition_num)) { | 
|  | 552 | return ""; | 
|  | 553 | } | 
|  | 554 | return MakeNandPartitionNameForMount(partition_num); | 
|  | 555 | } | 
|  | 556 | return part_name; | 
|  | 557 | } | 
|  | 558 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 559 | string ErrnoNumberAsString(int err) { | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 560 | char buf[100]; | 
|  | 561 | buf[0] = '\0'; | 
|  | 562 | return strerror_r(err, buf, sizeof(buf)); | 
|  | 563 | } | 
|  | 564 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 565 | bool FileExists(const char* path) { | 
|  | 566 | struct stat stbuf; | 
|  | 567 | return 0 == lstat(path, &stbuf); | 
|  | 568 | } | 
|  | 569 |  | 
| Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 570 | bool IsSymlink(const char* path) { | 
|  | 571 | struct stat stbuf; | 
|  | 572 | return lstat(path, &stbuf) == 0 && S_ISLNK(stbuf.st_mode) != 0; | 
|  | 573 | } | 
|  | 574 |  | 
| Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 575 | bool TryAttachingUbiVolume(int volume_num, int timeout) { | 
|  | 576 | const string volume_path = base::StringPrintf("/dev/ubi%d_0", volume_num); | 
|  | 577 | if (FileExists(volume_path.c_str())) { | 
|  | 578 | return true; | 
|  | 579 | } | 
|  | 580 |  | 
|  | 581 | int exit_code; | 
|  | 582 | vector<string> cmd = { | 
|  | 583 | "ubiattach", | 
|  | 584 | "-m", | 
|  | 585 | base::StringPrintf("%d", volume_num), | 
|  | 586 | "-d", | 
|  | 587 | base::StringPrintf("%d", volume_num) | 
|  | 588 | }; | 
|  | 589 | TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &exit_code, nullptr)); | 
|  | 590 | TEST_AND_RETURN_FALSE(exit_code == 0); | 
|  | 591 |  | 
|  | 592 | cmd = { | 
|  | 593 | "ubiblock", | 
|  | 594 | "--create", | 
|  | 595 | volume_path | 
|  | 596 | }; | 
|  | 597 | TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &exit_code, nullptr)); | 
|  | 598 | TEST_AND_RETURN_FALSE(exit_code == 0); | 
|  | 599 |  | 
|  | 600 | while (timeout > 0 && !FileExists(volume_path.c_str())) { | 
|  | 601 | sleep(1); | 
|  | 602 | timeout--; | 
|  | 603 | } | 
|  | 604 |  | 
|  | 605 | return FileExists(volume_path.c_str()); | 
|  | 606 | } | 
|  | 607 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 608 | bool MakeTempFile(const string& base_filename_template, | 
|  | 609 | string* filename, | 
| Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 610 | int* fd) { | 
| Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 611 | base::FilePath filename_template; | 
|  | 612 | TEST_AND_RETURN_FALSE( | 
|  | 613 | GetTempName(base_filename_template, &filename_template)); | 
| Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 614 | DCHECK(filename || fd); | 
| Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 615 | vector<char> buf(filename_template.value().size() + 1); | 
|  | 616 | memcpy(buf.data(), filename_template.value().data(), | 
|  | 617 | filename_template.value().size()); | 
|  | 618 | buf[filename_template.value().size()] = '\0'; | 
| Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 619 |  | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 620 | int mkstemp_fd = mkstemp(buf.data()); | 
| Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 621 | TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0); | 
|  | 622 | if (filename) { | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 623 | *filename = buf.data(); | 
| Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 624 | } | 
|  | 625 | if (fd) { | 
|  | 626 | *fd = mkstemp_fd; | 
|  | 627 | } else { | 
|  | 628 | close(mkstemp_fd); | 
|  | 629 | } | 
|  | 630 | return true; | 
|  | 631 | } | 
|  | 632 |  | 
| Alex Deymo | 5fb356c | 2016-03-25 18:48:22 -0700 | [diff] [blame] | 633 | bool SetBlockDeviceReadOnly(const string& device, bool read_only) { | 
|  | 634 | int fd = HANDLE_EINTR(open(device.c_str(), O_RDONLY | O_CLOEXEC)); | 
|  | 635 | if (fd < 0) { | 
|  | 636 | PLOG(ERROR) << "Opening block device " << device; | 
|  | 637 | return false; | 
|  | 638 | } | 
|  | 639 | ScopedFdCloser fd_closer(&fd); | 
|  | 640 | // We take no action if not needed. | 
|  | 641 | int read_only_flag; | 
|  | 642 | int expected_flag = read_only ? 1 : 0; | 
|  | 643 | int rc = ioctl(fd, BLKROGET, &read_only_flag); | 
|  | 644 | // In case of failure reading the setting we will try to set it anyway. | 
|  | 645 | if (rc == 0 && read_only_flag == expected_flag) | 
|  | 646 | return true; | 
|  | 647 |  | 
|  | 648 | rc = ioctl(fd, BLKROSET, &expected_flag); | 
|  | 649 | if (rc != 0) { | 
|  | 650 | PLOG(ERROR) << "Marking block device " << device << " as read_only=" | 
|  | 651 | << expected_flag; | 
|  | 652 | return false; | 
|  | 653 | } | 
|  | 654 | return true; | 
|  | 655 | } | 
|  | 656 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 657 | bool MountFilesystem(const string& device, | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 658 | const string& mountpoint, | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 659 | unsigned long mountflags,  // NOLINT(runtime/int) | 
| Alex Deymo | 14dbd33 | 2016-03-01 18:55:54 -0800 | [diff] [blame] | 660 | const string& type, | 
|  | 661 | const string& fs_mount_options) { | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 662 | vector<const char*> fstypes; | 
|  | 663 | if (type.empty()) { | 
|  | 664 | fstypes = {"ext2", "ext3", "ext4", "squashfs"}; | 
|  | 665 | } else { | 
|  | 666 | fstypes = {type.c_str()}; | 
|  | 667 | } | 
| Alex Deymo | 4c82df3 | 2014-11-10 22:25:57 -0800 | [diff] [blame] | 668 | for (const char* fstype : fstypes) { | 
|  | 669 | int rc = mount(device.c_str(), mountpoint.c_str(), fstype, mountflags, | 
| Alex Deymo | 14dbd33 | 2016-03-01 18:55:54 -0800 | [diff] [blame] | 670 | fs_mount_options.c_str()); | 
| Alex Deymo | 4c82df3 | 2014-11-10 22:25:57 -0800 | [diff] [blame] | 671 | if (rc == 0) | 
|  | 672 | return true; | 
|  | 673 |  | 
|  | 674 | PLOG(WARNING) << "Unable to mount destination device " << device | 
|  | 675 | << " on " << mountpoint << " as " << fstype; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 676 | } | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 677 | if (!type.empty()) { | 
|  | 678 | LOG(ERROR) << "Unable to mount " << device << " with any supported type"; | 
|  | 679 | } | 
| Alex Deymo | 4c82df3 | 2014-11-10 22:25:57 -0800 | [diff] [blame] | 680 | return false; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 681 | } | 
|  | 682 |  | 
|  | 683 | bool UnmountFilesystem(const string& mountpoint) { | 
| Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 684 | int num_retries = 1; | 
|  | 685 | for (;; ++num_retries) { | 
| Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 686 | if (umount(mountpoint.c_str()) == 0) | 
| Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 687 | return true; | 
|  | 688 | if (errno != EBUSY || num_retries >= kUnmountMaxNumOfRetries) | 
| Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 689 | break; | 
| Alex Deymo | 8d2bbe3 | 2015-06-23 16:28:59 -0700 | [diff] [blame] | 690 | usleep(kUnmountRetryIntervalInMicroseconds); | 
| Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 691 | } | 
| Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 692 | if (errno == EINVAL) { | 
|  | 693 | LOG(INFO) << "Not a mountpoint: " << mountpoint; | 
|  | 694 | return false; | 
|  | 695 | } | 
|  | 696 | PLOG(WARNING) << "Error unmounting " << mountpoint << " after " << num_retries | 
|  | 697 | << " attempts. Lazy unmounting instead, error was"; | 
|  | 698 | if (umount2(mountpoint.c_str(), MNT_DETACH) != 0) { | 
|  | 699 | PLOG(ERROR) << "Lazy unmount failed"; | 
|  | 700 | return false; | 
|  | 701 | } | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 702 | return true; | 
|  | 703 | } | 
|  | 704 |  | 
| Alex Deymo | f411650 | 2017-03-22 17:00:31 -0700 | [diff] [blame] | 705 | bool IsMountpoint(const std::string& mountpoint) { | 
|  | 706 | struct stat stdir, stparent; | 
|  | 707 |  | 
|  | 708 | // Check whether the passed mountpoint is a directory and the /.. is in the | 
|  | 709 | // same device or not. If mountpoint/.. is in a different device it means that | 
|  | 710 | // there is a filesystem mounted there. If it is not, but they both point to | 
|  | 711 | // the same inode it basically is the special case of /.. pointing to /. This | 
|  | 712 | // test doesn't play well with bind mount but that's out of the scope of what | 
|  | 713 | // we want to detect here. | 
|  | 714 | if (lstat(mountpoint.c_str(), &stdir) != 0) { | 
|  | 715 | PLOG(ERROR) << "Error stat'ing " << mountpoint; | 
|  | 716 | return false; | 
|  | 717 | } | 
|  | 718 | if (!S_ISDIR(stdir.st_mode)) | 
|  | 719 | return false; | 
|  | 720 |  | 
|  | 721 | base::FilePath parent(mountpoint); | 
|  | 722 | parent = parent.Append(".."); | 
|  | 723 | if (lstat(parent.value().c_str(), &stparent) != 0) { | 
|  | 724 | PLOG(ERROR) << "Error stat'ing " << parent.value(); | 
|  | 725 | return false; | 
|  | 726 | } | 
|  | 727 | return S_ISDIR(stparent.st_mode) && | 
|  | 728 | (stparent.st_dev != stdir.st_dev || stparent.st_ino == stdir.st_ino); | 
|  | 729 | } | 
|  | 730 |  | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 731 | // Tries to parse the header of an ELF file to obtain a human-readable | 
|  | 732 | // description of it on the |output| string. | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 733 | static bool GetFileFormatELF(const uint8_t* buffer, size_t size, | 
|  | 734 | string* output) { | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 735 | // 0x00: EI_MAG - ELF magic header, 4 bytes. | 
| Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 736 | if (size < SELFMAG || memcmp(buffer, ELFMAG, SELFMAG) != 0) | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 737 | return false; | 
|  | 738 | *output = "ELF"; | 
|  | 739 |  | 
|  | 740 | // 0x04: EI_CLASS, 1 byte. | 
| Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 741 | if (size < EI_CLASS + 1) | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 742 | return true; | 
| Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 743 | switch (buffer[EI_CLASS]) { | 
|  | 744 | case ELFCLASS32: | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 745 | *output += " 32-bit"; | 
|  | 746 | break; | 
| Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 747 | case ELFCLASS64: | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 748 | *output += " 64-bit"; | 
|  | 749 | break; | 
|  | 750 | default: | 
|  | 751 | *output += " ?-bit"; | 
|  | 752 | } | 
|  | 753 |  | 
|  | 754 | // 0x05: EI_DATA, endianness, 1 byte. | 
| Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 755 | if (size < EI_DATA + 1) | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 756 | return true; | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 757 | uint8_t ei_data = buffer[EI_DATA]; | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 758 | switch (ei_data) { | 
| Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 759 | case ELFDATA2LSB: | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 760 | *output += " little-endian"; | 
|  | 761 | break; | 
| Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 762 | case ELFDATA2MSB: | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 763 | *output += " big-endian"; | 
|  | 764 | break; | 
|  | 765 | default: | 
|  | 766 | *output += " ?-endian"; | 
|  | 767 | // Don't parse anything after the 0x10 offset if endianness is unknown. | 
|  | 768 | return true; | 
|  | 769 | } | 
|  | 770 |  | 
| Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 771 | const Elf32_Ehdr* hdr = reinterpret_cast<const Elf32_Ehdr*>(buffer); | 
|  | 772 | // 0x12: e_machine, 2 byte endianness based on ei_data. The position (0x12) | 
|  | 773 | // and size is the same for both 32 and 64 bits. | 
|  | 774 | if (size < offsetof(Elf32_Ehdr, e_machine) + sizeof(hdr->e_machine)) | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 775 | return true; | 
| Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 776 | uint16_t e_machine; | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 777 | // Fix endianess regardless of the host endianess. | 
| Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 778 | if (ei_data == ELFDATA2LSB) | 
|  | 779 | e_machine = le16toh(hdr->e_machine); | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 780 | else | 
| Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 781 | e_machine = be16toh(hdr->e_machine); | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 782 |  | 
|  | 783 | switch (e_machine) { | 
| Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 784 | case EM_386: | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 785 | *output += " x86"; | 
|  | 786 | break; | 
| Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 787 | case EM_MIPS: | 
|  | 788 | *output += " mips"; | 
|  | 789 | break; | 
|  | 790 | case EM_ARM: | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 791 | *output += " arm"; | 
|  | 792 | break; | 
| Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 793 | case EM_X86_64: | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 794 | *output += " x86-64"; | 
|  | 795 | break; | 
|  | 796 | default: | 
|  | 797 | *output += " unknown-arch"; | 
|  | 798 | } | 
|  | 799 | return true; | 
|  | 800 | } | 
|  | 801 |  | 
|  | 802 | string GetFileFormat(const string& path) { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 803 | brillo::Blob buffer; | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 804 | if (!ReadFileChunkAndAppend(path, 0, kGetFileFormatMaxHeaderSize, &buffer)) | 
|  | 805 | return "File not found."; | 
|  | 806 |  | 
|  | 807 | string result; | 
|  | 808 | if (GetFileFormatELF(buffer.data(), buffer.size(), &result)) | 
|  | 809 | return result; | 
|  | 810 |  | 
|  | 811 | return "data"; | 
|  | 812 | } | 
|  | 813 |  | 
| Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 814 | int FuzzInt(int value, unsigned int range) { | 
|  | 815 | int min = value - range / 2; | 
|  | 816 | int max = value + range - range / 2; | 
|  | 817 | return base::RandInt(min, max); | 
|  | 818 | } | 
|  | 819 |  | 
| Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 820 | string FormatSecs(unsigned secs) { | 
| Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 821 | return FormatTimeDelta(TimeDelta::FromSeconds(secs)); | 
| Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 822 | } | 
|  | 823 |  | 
| Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 824 | string FormatTimeDelta(TimeDelta delta) { | 
| David Zeuthen | 973449e | 2014-08-18 16:18:23 -0400 | [diff] [blame] | 825 | string str; | 
|  | 826 |  | 
|  | 827 | // Handle negative durations by prefixing with a minus. | 
|  | 828 | if (delta.ToInternalValue() < 0) { | 
|  | 829 | delta *= -1; | 
|  | 830 | str = "-"; | 
|  | 831 | } | 
|  | 832 |  | 
| Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 833 | // Canonicalize into days, hours, minutes, seconds and microseconds. | 
|  | 834 | unsigned days = delta.InDays(); | 
| Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 835 | delta -= TimeDelta::FromDays(days); | 
| Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 836 | unsigned hours = delta.InHours(); | 
| Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 837 | delta -= TimeDelta::FromHours(hours); | 
| Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 838 | unsigned mins = delta.InMinutes(); | 
| Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 839 | delta -= TimeDelta::FromMinutes(mins); | 
| Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 840 | unsigned secs = delta.InSeconds(); | 
| Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 841 | delta -= TimeDelta::FromSeconds(secs); | 
| Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 842 | unsigned usecs = delta.InMicroseconds(); | 
| Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 843 |  | 
| Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 844 | if (days) | 
|  | 845 | base::StringAppendF(&str, "%ud", days); | 
|  | 846 | if (days || hours) | 
| Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 847 | base::StringAppendF(&str, "%uh", hours); | 
| Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 848 | if (days || hours || mins) | 
| Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 849 | base::StringAppendF(&str, "%um", mins); | 
| Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 850 | base::StringAppendF(&str, "%u", secs); | 
|  | 851 | if (usecs) { | 
|  | 852 | int width = 6; | 
|  | 853 | while ((usecs / 10) * 10 == usecs) { | 
|  | 854 | usecs /= 10; | 
|  | 855 | width--; | 
|  | 856 | } | 
|  | 857 | base::StringAppendF(&str, ".%0*u", width, usecs); | 
|  | 858 | } | 
|  | 859 | base::StringAppendF(&str, "s"); | 
| Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 860 | return str; | 
|  | 861 | } | 
|  | 862 |  | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 863 | string ToString(const Time utc_time) { | 
|  | 864 | Time::Exploded exp_time; | 
|  | 865 | utc_time.UTCExplode(&exp_time); | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 866 | return base::StringPrintf("%d/%d/%d %d:%02d:%02d GMT", | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 867 | exp_time.month, | 
|  | 868 | exp_time.day_of_month, | 
|  | 869 | exp_time.year, | 
|  | 870 | exp_time.hour, | 
|  | 871 | exp_time.minute, | 
|  | 872 | exp_time.second); | 
|  | 873 | } | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 874 |  | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 875 | string ToString(bool b) { | 
|  | 876 | return (b ? "true" : "false"); | 
|  | 877 | } | 
|  | 878 |  | 
| Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 879 | string ToString(DownloadSource source) { | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 880 | switch (source) { | 
|  | 881 | case kDownloadSourceHttpsServer: return "HttpsServer"; | 
|  | 882 | case kDownloadSourceHttpServer:  return "HttpServer"; | 
| David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 883 | case kDownloadSourceHttpPeer:    return "HttpPeer"; | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 884 | case kNumDownloadSources:        return "Unknown"; | 
|  | 885 | // Don't add a default case to let the compiler warn about newly added | 
|  | 886 | // download sources which should be added here. | 
|  | 887 | } | 
|  | 888 |  | 
|  | 889 | return "Unknown"; | 
|  | 890 | } | 
|  | 891 |  | 
| Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 892 | string ToString(PayloadType payload_type) { | 
|  | 893 | switch (payload_type) { | 
|  | 894 | case kPayloadTypeDelta:      return "Delta"; | 
|  | 895 | case kPayloadTypeFull:       return "Full"; | 
|  | 896 | case kPayloadTypeForcedFull: return "ForcedFull"; | 
|  | 897 | case kNumPayloadTypes:       return "Unknown"; | 
|  | 898 | // Don't add a default case to let the compiler warn about newly added | 
|  | 899 | // payload types which should be added here. | 
|  | 900 | } | 
|  | 901 |  | 
|  | 902 | return "Unknown"; | 
|  | 903 | } | 
|  | 904 |  | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 905 | ErrorCode GetBaseErrorCode(ErrorCode code) { | 
| Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 906 | // Ignore the higher order bits in the code by applying the mask as | 
|  | 907 | // we want the enumerations to be in the small contiguous range | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 908 | // with values less than ErrorCode::kUmaReportedMax. | 
|  | 909 | ErrorCode base_code = static_cast<ErrorCode>( | 
|  | 910 | static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags)); | 
| Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 911 |  | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 912 | // Make additional adjustments required for UMA and error classification. | 
|  | 913 | // TODO(jaysri): Move this logic to UeErrorCode.cc when we fix | 
|  | 914 | // chromium-os:34369. | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 915 | if (base_code >= ErrorCode::kOmahaRequestHTTPResponseBase) { | 
| Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 916 | // Since we want to keep the enums to a small value, aggregate all HTTP | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 917 | // errors into this one bucket for UMA and error classification purposes. | 
| Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 918 | LOG(INFO) << "Converting error code " << base_code | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 919 | << " to ErrorCode::kOmahaErrorInHTTPResponse"; | 
|  | 920 | base_code = ErrorCode::kOmahaErrorInHTTPResponse; | 
| Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 921 | } | 
|  | 922 |  | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 923 | return base_code; | 
|  | 924 | } | 
|  | 925 |  | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 926 | Time TimeFromStructTimespec(struct timespec *ts) { | 
| Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 927 | int64_t us = static_cast<int64_t>(ts->tv_sec) * Time::kMicrosecondsPerSecond + | 
|  | 928 | static_cast<int64_t>(ts->tv_nsec) / Time::kNanosecondsPerMicrosecond; | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 929 | return Time::UnixEpoch() + TimeDelta::FromMicroseconds(us); | 
|  | 930 | } | 
|  | 931 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 932 | string StringVectorToString(const vector<string> &vec_str) { | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 933 | string str = "["; | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 934 | for (vector<string>::const_iterator i = vec_str.begin(); | 
|  | 935 | i != vec_str.end(); ++i) { | 
|  | 936 | if (i != vec_str.begin()) | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 937 | str += ", "; | 
|  | 938 | str += '"'; | 
|  | 939 | str += *i; | 
|  | 940 | str += '"'; | 
|  | 941 | } | 
|  | 942 | str += "]"; | 
|  | 943 | return str; | 
|  | 944 | } | 
|  | 945 |  | 
| Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 946 | // The P2P file id should be the same for devices running new version and old | 
|  | 947 | // version so that they can share it with each other. The hash in the response | 
|  | 948 | // was base64 encoded, but now that we switched to use "hash_sha256" field which | 
|  | 949 | // is hex encoded, we have to convert them back to base64 for P2P. However, the | 
|  | 950 | // base64 encoded hash was base64 encoded here again historically for some | 
|  | 951 | // reason, so we keep the same behavior here. | 
|  | 952 | string CalculateP2PFileId(const brillo::Blob& payload_hash, | 
|  | 953 | size_t payload_size) { | 
|  | 954 | string encoded_hash = brillo::data_encoding::Base64Encode( | 
|  | 955 | brillo::data_encoding::Base64Encode(payload_hash)); | 
| Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 956 | return base::StringPrintf("cros_update_size_%" PRIuS "_hash_%s", | 
| Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 957 | payload_size, | 
|  | 958 | encoded_hash.c_str()); | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 959 | } | 
|  | 960 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 961 | bool DecodeAndStoreBase64String(const string& base64_encoded, | 
| David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 962 | base::FilePath *out_path) { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 963 | brillo::Blob contents; | 
| David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 964 |  | 
|  | 965 | out_path->clear(); | 
|  | 966 |  | 
|  | 967 | if (base64_encoded.size() == 0) { | 
|  | 968 | LOG(ERROR) << "Can't decode empty string."; | 
|  | 969 | return false; | 
|  | 970 | } | 
|  | 971 |  | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 972 | if (!brillo::data_encoding::Base64Decode(base64_encoded, &contents) || | 
| David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 973 | contents.size() == 0) { | 
|  | 974 | LOG(ERROR) << "Error decoding base64."; | 
|  | 975 | return false; | 
|  | 976 | } | 
|  | 977 |  | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 978 | FILE *file = base::CreateAndOpenTemporaryFile(out_path); | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 979 | if (file == nullptr) { | 
| David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 980 | LOG(ERROR) << "Error creating temporary file."; | 
|  | 981 | return false; | 
|  | 982 | } | 
|  | 983 |  | 
| Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 984 | if (fwrite(contents.data(), 1, contents.size(), file) != contents.size()) { | 
| David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 985 | PLOG(ERROR) << "Error writing to temporary file."; | 
|  | 986 | if (fclose(file) != 0) | 
|  | 987 | PLOG(ERROR) << "Error closing temporary file."; | 
|  | 988 | if (unlink(out_path->value().c_str()) != 0) | 
|  | 989 | PLOG(ERROR) << "Error unlinking temporary file."; | 
|  | 990 | out_path->clear(); | 
|  | 991 | return false; | 
|  | 992 | } | 
|  | 993 |  | 
|  | 994 | if (fclose(file) != 0) { | 
|  | 995 | PLOG(ERROR) << "Error closing temporary file."; | 
|  | 996 | out_path->clear(); | 
|  | 997 | return false; | 
|  | 998 | } | 
|  | 999 |  | 
|  | 1000 | return true; | 
|  | 1001 | } | 
|  | 1002 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1003 | bool ConvertToOmahaInstallDate(Time time, int *out_num_days) { | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1004 | time_t unix_time = time.ToTimeT(); | 
|  | 1005 | // Output of: date +"%s" --date="Jan 1, 2007 0:00 PST". | 
|  | 1006 | const time_t kOmahaEpoch = 1167638400; | 
|  | 1007 | const int64_t kNumSecondsPerWeek = 7*24*3600; | 
|  | 1008 | const int64_t kNumDaysPerWeek = 7; | 
|  | 1009 |  | 
|  | 1010 | time_t omaha_time = unix_time - kOmahaEpoch; | 
|  | 1011 |  | 
|  | 1012 | if (omaha_time < 0) | 
|  | 1013 | return false; | 
|  | 1014 |  | 
|  | 1015 | // Note, as per the comment in utils.h we are deliberately not | 
|  | 1016 | // handling DST correctly. | 
|  | 1017 |  | 
|  | 1018 | int64_t num_weeks_since_omaha_epoch = omaha_time / kNumSecondsPerWeek; | 
|  | 1019 | *out_num_days = num_weeks_since_omaha_epoch * kNumDaysPerWeek; | 
|  | 1020 |  | 
|  | 1021 | return true; | 
|  | 1022 | } | 
|  | 1023 |  | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1024 | bool GetMinorVersion(const brillo::KeyValueStore& store, | 
| Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 1025 | uint32_t* minor_version) { | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1026 | string result; | 
| Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 1027 | if (store.GetString("PAYLOAD_MINOR_VERSION", &result)) { | 
| Allie Wood | 425aa97 | 2015-02-17 14:47:17 -0800 | [diff] [blame] | 1028 | if (!base::StringToUint(result, minor_version)) { | 
|  | 1029 | LOG(ERROR) << "StringToUint failed when parsing delta minor version."; | 
|  | 1030 | return false; | 
|  | 1031 | } | 
| Allie Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 1032 | return true; | 
|  | 1033 | } | 
|  | 1034 | return false; | 
|  | 1035 | } | 
|  | 1036 |  | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1037 | bool ReadExtents(const string& path, const vector<Extent>& extents, | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1038 | brillo::Blob* out_data, ssize_t out_data_size, | 
| Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 1039 | size_t block_size) { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1040 | brillo::Blob data(out_data_size); | 
| Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 1041 | ssize_t bytes_read = 0; | 
|  | 1042 | int fd = open(path.c_str(), O_RDONLY); | 
|  | 1043 | TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); | 
|  | 1044 | ScopedFdCloser fd_closer(&fd); | 
|  | 1045 |  | 
| Gilad Arnold | 41e3474 | 2015-05-11 11:31:50 -0700 | [diff] [blame] | 1046 | for (const Extent& extent : extents) { | 
| Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 1047 | ssize_t bytes_read_this_iteration = 0; | 
|  | 1048 | ssize_t bytes = extent.num_blocks() * block_size; | 
|  | 1049 | TEST_AND_RETURN_FALSE(bytes_read + bytes <= out_data_size); | 
|  | 1050 | TEST_AND_RETURN_FALSE(utils::PReadAll(fd, | 
|  | 1051 | &data[bytes_read], | 
|  | 1052 | bytes, | 
|  | 1053 | extent.start_block() * block_size, | 
|  | 1054 | &bytes_read_this_iteration)); | 
|  | 1055 | TEST_AND_RETURN_FALSE(bytes_read_this_iteration == bytes); | 
|  | 1056 | bytes_read += bytes_read_this_iteration; | 
|  | 1057 | } | 
|  | 1058 | TEST_AND_RETURN_FALSE(out_data_size == bytes_read); | 
|  | 1059 | *out_data = data; | 
|  | 1060 | return true; | 
|  | 1061 | } | 
|  | 1062 |  | 
| Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 1063 | bool GetBootId(string* boot_id) { | 
|  | 1064 | TEST_AND_RETURN_FALSE( | 
|  | 1065 | base::ReadFileToString(base::FilePath(kBootIdPath), boot_id)); | 
|  | 1066 | base::TrimWhitespaceASCII(*boot_id, base::TRIM_TRAILING, boot_id); | 
|  | 1067 | return true; | 
|  | 1068 | } | 
|  | 1069 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1070 | }  // namespace utils | 
|  | 1071 |  | 
|  | 1072 | }  // namespace chromeos_update_engine |