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