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