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