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> |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 25 | #include <ext2fs/ext2fs.h> |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 26 | #include <fcntl.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 30 | #include <sys/mount.h> |
| 31 | #include <sys/resource.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/wait.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 35 | #include <unistd.h> |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 36 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 38 | #include <utility> |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 39 | #include <vector> |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 40 | |
Alex Vakulenko | 4906c1c | 2014-08-21 13:17:44 -0700 | [diff] [blame] | 41 | #include <base/callback.h> |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 42 | #include <base/files/file_path.h> |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 43 | #include <base/files/file_util.h> |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 44 | #include <base/files/scoped_file.h> |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 45 | #include <base/format_macros.h> |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 46 | #include <base/location.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 47 | #include <base/logging.h> |
Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 48 | #include <base/posix/eintr_wrapper.h> |
Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 49 | #include <base/rand_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 50 | #include <base/strings/string_number_conversions.h> |
| 51 | #include <base/strings/string_split.h> |
| 52 | #include <base/strings/string_util.h> |
| 53 | #include <base/strings/stringprintf.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 54 | #include <brillo/data_encoding.h> |
| 55 | #include <brillo/message_loops/message_loop.h> |
Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 56 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 57 | #include "update_engine/common/clock_interface.h" |
| 58 | #include "update_engine/common/constants.h" |
Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 59 | #include "update_engine/common/platform_constants.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 60 | #include "update_engine/common/prefs_interface.h" |
| 61 | #include "update_engine/common/subprocess.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 62 | #include "update_engine/payload_consumer/file_descriptor.h" |
| 63 | #include "update_engine/payload_consumer/file_writer.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 64 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 65 | using base::Time; |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 66 | using base::TimeDelta; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 67 | using std::min; |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 68 | using std::pair; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 69 | using std::string; |
| 70 | using std::vector; |
| 71 | |
| 72 | namespace chromeos_update_engine { |
| 73 | |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 74 | namespace { |
| 75 | |
| 76 | // The following constants control how UnmountFilesystem should retry if |
| 77 | // umount() fails with an errno EBUSY, i.e. retry 5 times over the course of |
| 78 | // one second. |
| 79 | const int kUnmountMaxNumOfRetries = 5; |
| 80 | const int kUnmountRetryIntervalInMicroseconds = 200 * 1000; // 200 ms |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 81 | |
| 82 | // Number of bytes to read from a file to attempt to detect its contents. Used |
| 83 | // in GetFileFormat. |
| 84 | const int kGetFileFormatMaxHeaderSize = 32; |
| 85 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 86 | // The path to the kernel's boot_id. |
| 87 | const char kBootIdPath[] = "/proc/sys/kernel/random/boot_id"; |
| 88 | |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 89 | // Return true if |disk_name| is an MTD or a UBI device. Note that this test is |
| 90 | // simply based on the name of the device. |
| 91 | bool IsMtdDeviceName(const string& disk_name) { |
Alex Vakulenko | 6a9d349 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 92 | return base::StartsWithASCII(disk_name, "/dev/ubi", true) || |
| 93 | base::StartsWithASCII(disk_name, "/dev/mtd", true); |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // Return the device name for the corresponding partition on a NAND device. |
| 97 | // WARNING: This function returns device names that are not mountable. |
| 98 | string MakeNandPartitionName(int partition_num) { |
| 99 | switch (partition_num) { |
| 100 | case 2: |
| 101 | case 4: |
| 102 | case 6: { |
| 103 | return base::StringPrintf("/dev/mtd%d", partition_num); |
| 104 | } |
| 105 | default: { |
| 106 | return base::StringPrintf("/dev/ubi%d_0", partition_num); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Return the device name for the corresponding partition on a NAND device that |
| 112 | // may be mountable (but may not be writable). |
| 113 | string MakeNandPartitionNameForMount(int partition_num) { |
| 114 | switch (partition_num) { |
| 115 | case 2: |
| 116 | case 4: |
| 117 | case 6: { |
| 118 | return base::StringPrintf("/dev/mtd%d", partition_num); |
| 119 | } |
| 120 | case 3: |
| 121 | case 5: |
| 122 | case 7: { |
| 123 | return base::StringPrintf("/dev/ubiblock%d_0", partition_num); |
| 124 | } |
| 125 | default: { |
| 126 | return base::StringPrintf("/dev/ubi%d_0", partition_num); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 131 | // If |path| is absolute, or explicit relative to the current working directory, |
| 132 | // leaves it as is. Otherwise, uses the system's temp directory, as defined by |
| 133 | // base::GetTempDir() and prepends it to |path|. On success stores the full |
| 134 | // temporary path in |template_path| and returns true. |
| 135 | bool GetTempName(const string& path, base::FilePath* template_path) { |
| 136 | if (path[0] == '/' || base::StartsWithASCII(path, "./", true) || |
| 137 | base::StartsWithASCII(path, "../", true)) { |
| 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 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 158 | // Cgroup container is created in update-engine's upstart script located at |
| 159 | // /etc/init/update-engine.conf. |
| 160 | static const char kCGroupDir[] = "/sys/fs/cgroup/cpu/update-engine"; |
| 161 | |
J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 162 | string ParseECVersion(string input_line) { |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 163 | base::TrimWhitespaceASCII(input_line, base::TRIM_ALL, &input_line); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 164 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 165 | // 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] | 166 | // a vector of key value pairs. |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 167 | vector<pair<string, string>> kv_pairs; |
J. Richard Barnette | 63137e5 | 2013-10-28 10:57:29 -0700 | [diff] [blame] | 168 | if (base::SplitStringIntoKeyValuePairs(input_line, '=', ' ', &kv_pairs)) { |
Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 169 | for (const pair<string, string>& kv_pair : kv_pairs) { |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 170 | // Finally match against the fw_verion which may have quotes. |
Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 171 | if (kv_pair.first == "fw_version") { |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 172 | string output; |
| 173 | // Trim any quotes. |
Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 174 | base::TrimString(kv_pair.second, "\"", &output); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 175 | return output; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | LOG(ERROR) << "Unable to parse fwid from ec info."; |
| 180 | return ""; |
| 181 | } |
| 182 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 183 | 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] | 184 | DirectFileWriter writer; |
| 185 | TEST_AND_RETURN_FALSE_ERRNO(0 == writer.Open(path, |
| 186 | O_WRONLY | O_CREAT | O_TRUNC, |
Chris Masone | 4dc2ada | 2010-09-23 12:43:03 -0700 | [diff] [blame] | 187 | 0600)); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 188 | ScopedFileWriterCloser closer(&writer); |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 189 | TEST_AND_RETURN_FALSE_ERRNO(writer.Write(data, data_len)); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 193 | bool WriteAll(int fd, const void* buf, size_t count) { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 194 | const char* c_buf = static_cast<const char*>(buf); |
| 195 | ssize_t bytes_written = 0; |
| 196 | while (bytes_written < static_cast<ssize_t>(count)) { |
| 197 | ssize_t rc = write(fd, c_buf + bytes_written, count - bytes_written); |
| 198 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 199 | bytes_written += rc; |
| 200 | } |
| 201 | return true; |
| 202 | } |
| 203 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 204 | bool PWriteAll(int fd, const void* buf, size_t count, off_t offset) { |
| 205 | const char* c_buf = static_cast<const char*>(buf); |
Gilad Arnold | 780db21 | 2012-07-11 13:12:49 -0700 | [diff] [blame] | 206 | size_t bytes_written = 0; |
| 207 | int num_attempts = 0; |
| 208 | while (bytes_written < count) { |
| 209 | num_attempts++; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 210 | ssize_t rc = pwrite(fd, c_buf + bytes_written, count - bytes_written, |
| 211 | offset + bytes_written); |
Gilad Arnold | 780db21 | 2012-07-11 13:12:49 -0700 | [diff] [blame] | 212 | // TODO(garnold) for debugging failure in chromium-os:31077; to be removed. |
| 213 | if (rc < 0) { |
| 214 | PLOG(ERROR) << "pwrite error; num_attempts=" << num_attempts |
| 215 | << " bytes_written=" << bytes_written |
| 216 | << " count=" << count << " offset=" << offset; |
| 217 | } |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 218 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 219 | bytes_written += rc; |
| 220 | } |
| 221 | return true; |
| 222 | } |
| 223 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 224 | bool WriteAll(FileDescriptorPtr fd, const void* buf, size_t count) { |
| 225 | const char* c_buf = static_cast<const char*>(buf); |
| 226 | ssize_t bytes_written = 0; |
| 227 | while (bytes_written < static_cast<ssize_t>(count)) { |
| 228 | ssize_t rc = fd->Write(c_buf + bytes_written, count - bytes_written); |
| 229 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 230 | bytes_written += rc; |
| 231 | } |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | bool PWriteAll(FileDescriptorPtr fd, |
| 236 | const void* buf, |
| 237 | size_t count, |
| 238 | off_t offset) { |
Allie Wood | 0c8cf7e | 2015-04-23 19:24:49 -0700 | [diff] [blame] | 239 | TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(offset, SEEK_SET) != |
| 240 | static_cast<off_t>(-1)); |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 241 | return WriteAll(fd, buf, count); |
| 242 | } |
| 243 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 244 | bool PReadAll(int fd, void* buf, size_t count, off_t offset, |
| 245 | ssize_t* out_bytes_read) { |
| 246 | char* c_buf = static_cast<char*>(buf); |
| 247 | ssize_t bytes_read = 0; |
| 248 | while (bytes_read < static_cast<ssize_t>(count)) { |
| 249 | ssize_t rc = pread(fd, c_buf + bytes_read, count - bytes_read, |
| 250 | offset + bytes_read); |
| 251 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 252 | if (rc == 0) { |
| 253 | break; |
| 254 | } |
| 255 | bytes_read += rc; |
| 256 | } |
| 257 | *out_bytes_read = bytes_read; |
| 258 | return true; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 261 | bool PReadAll(FileDescriptorPtr fd, void* buf, size_t count, off_t offset, |
| 262 | ssize_t* out_bytes_read) { |
Allie Wood | 0c8cf7e | 2015-04-23 19:24:49 -0700 | [diff] [blame] | 263 | TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(offset, SEEK_SET) != |
| 264 | static_cast<off_t>(-1)); |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 265 | char* c_buf = static_cast<char*>(buf); |
| 266 | ssize_t bytes_read = 0; |
| 267 | while (bytes_read < static_cast<ssize_t>(count)) { |
| 268 | ssize_t rc = fd->Read(c_buf + bytes_read, count - bytes_read); |
| 269 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 270 | if (rc == 0) { |
| 271 | break; |
| 272 | } |
| 273 | bytes_read += rc; |
| 274 | } |
| 275 | *out_bytes_read = bytes_read; |
| 276 | return true; |
| 277 | } |
| 278 | |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 279 | // Append |nbytes| of content from |buf| to the vector pointed to by either |
| 280 | // |vec_p| or |str_p|. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 281 | static void AppendBytes(const uint8_t* buf, size_t nbytes, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 282 | brillo::Blob* vec_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 283 | CHECK(buf); |
| 284 | CHECK(vec_p); |
| 285 | vec_p->insert(vec_p->end(), buf, buf + nbytes); |
| 286 | } |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 287 | static void AppendBytes(const uint8_t* buf, size_t nbytes, |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 288 | string* str_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 289 | CHECK(buf); |
| 290 | CHECK(str_p); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 291 | str_p->append(buf, buf + nbytes); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // Reads from an open file |fp|, appending the read content to the container |
| 295 | // 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] | 296 | // file's content, false otherwise. If |size| is not -1, reads up to |size| |
| 297 | // bytes. |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 298 | template <class T> |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 299 | static bool Read(FILE* fp, off_t size, T* out_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 300 | CHECK(fp); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 301 | CHECK(size == -1 || size >= 0); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 302 | uint8_t buf[1024]; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 303 | while (size == -1 || size > 0) { |
| 304 | off_t bytes_to_read = sizeof(buf); |
| 305 | if (size > 0 && bytes_to_read > size) { |
| 306 | bytes_to_read = size; |
| 307 | } |
| 308 | size_t nbytes = fread(buf, 1, bytes_to_read, fp); |
| 309 | if (!nbytes) { |
| 310 | break; |
| 311 | } |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 312 | AppendBytes(buf, nbytes, out_p); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 313 | if (size != -1) { |
| 314 | CHECK(size >= static_cast<off_t>(nbytes)); |
| 315 | size -= nbytes; |
| 316 | } |
| 317 | } |
| 318 | if (ferror(fp)) { |
| 319 | return false; |
| 320 | } |
| 321 | return size == 0 || feof(fp); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 324 | // Opens a file |path| for reading and appends its the contents to a container |
| 325 | // |out_p|. Starts reading the file from |offset|. If |offset| is beyond the end |
| 326 | // 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] | 327 | template <class T> |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 328 | static bool ReadFileChunkAndAppend(const string& path, |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 329 | off_t offset, |
| 330 | off_t size, |
| 331 | T* out_p) { |
| 332 | CHECK_GE(offset, 0); |
| 333 | CHECK(size == -1 || size >= 0); |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 334 | base::ScopedFILE fp(fopen(path.c_str(), "r")); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 335 | if (!fp.get()) |
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 | if (offset) { |
| 338 | // Return success without appending any data if a chunk beyond the end of |
| 339 | // the file is requested. |
| 340 | if (offset >= FileSize(path)) { |
| 341 | return true; |
| 342 | } |
| 343 | TEST_AND_RETURN_FALSE_ERRNO(fseek(fp.get(), offset, SEEK_SET) == 0); |
| 344 | } |
| 345 | return Read(fp.get(), size, out_p); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 348 | // TODO(deymo): This is only used in unittest, but requires the private |
| 349 | // Read<string>() defined here. Expose Read<string>() or move to base/ version. |
| 350 | bool ReadPipe(const string& cmd, string* out_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 351 | FILE* fp = popen(cmd.c_str(), "r"); |
| 352 | if (!fp) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 353 | return false; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 354 | bool success = Read(fp, -1, out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 355 | return (success && pclose(fp) >= 0); |
| 356 | } |
| 357 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 358 | bool ReadFile(const string& path, brillo::Blob* out_p) { |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 359 | return ReadFileChunkAndAppend(path, 0, -1, out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 362 | bool ReadFile(const string& path, string* out_p) { |
| 363 | return ReadFileChunkAndAppend(path, 0, -1, out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 366 | bool ReadFileChunk(const string& path, off_t offset, off_t size, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 367 | brillo::Blob* out_p) { |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 368 | return ReadFileChunkAndAppend(path, offset, size, out_p); |
| 369 | } |
| 370 | |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 371 | off_t BlockDevSize(int fd) { |
| 372 | uint64_t dev_size; |
| 373 | int rc = ioctl(fd, BLKGETSIZE64, &dev_size); |
| 374 | if (rc == -1) { |
| 375 | dev_size = -1; |
| 376 | PLOG(ERROR) << "Error running ioctl(BLKGETSIZE64) on " << fd; |
| 377 | } |
| 378 | return dev_size; |
| 379 | } |
| 380 | |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 381 | off_t FileSize(int fd) { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 382 | struct stat stbuf; |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 383 | int rc = fstat(fd, &stbuf); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 384 | CHECK_EQ(rc, 0); |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 385 | if (rc < 0) { |
| 386 | PLOG(ERROR) << "Error stat-ing " << fd; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 387 | return rc; |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 388 | } |
| 389 | if (S_ISREG(stbuf.st_mode)) |
| 390 | return stbuf.st_size; |
| 391 | if (S_ISBLK(stbuf.st_mode)) |
| 392 | return BlockDevSize(fd); |
| 393 | LOG(ERROR) << "Couldn't determine the type of " << fd; |
| 394 | return -1; |
| 395 | } |
| 396 | |
| 397 | off_t FileSize(const string& path) { |
| 398 | int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); |
| 399 | if (fd == -1) { |
| 400 | PLOG(ERROR) << "Error opening " << path; |
| 401 | return fd; |
| 402 | } |
| 403 | off_t size = FileSize(fd); |
| 404 | if (size == -1) |
| 405 | PLOG(ERROR) << "Error getting file size of " << path; |
| 406 | close(fd); |
| 407 | return size; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 410 | void HexDumpArray(const uint8_t* const arr, const size_t length) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 411 | LOG(INFO) << "Logging array of length: " << length; |
| 412 | const unsigned int bytes_per_line = 16; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 413 | for (uint32_t i = 0; i < length; i += bytes_per_line) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 414 | const unsigned int bytes_remaining = length - i; |
| 415 | const unsigned int bytes_per_this_line = min(bytes_per_line, |
| 416 | bytes_remaining); |
| 417 | char header[100]; |
| 418 | int r = snprintf(header, sizeof(header), "0x%08x : ", i); |
| 419 | TEST_AND_RETURN(r == 13); |
| 420 | string line = header; |
| 421 | for (unsigned int j = 0; j < bytes_per_this_line; j++) { |
| 422 | char buf[20]; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 423 | uint8_t c = arr[i + j]; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 424 | r = snprintf(buf, sizeof(buf), "%02x ", static_cast<unsigned int>(c)); |
| 425 | TEST_AND_RETURN(r == 3); |
| 426 | line += buf; |
| 427 | } |
| 428 | LOG(INFO) << line; |
| 429 | } |
| 430 | } |
| 431 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 432 | bool SplitPartitionName(const string& partition_name, |
| 433 | string* out_disk_name, |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 434 | int* out_partition_num) { |
Alex Vakulenko | 6a9d349 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 435 | if (!base::StartsWithASCII(partition_name, "/dev/", true)) { |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 436 | LOG(ERROR) << "Invalid partition device name: " << partition_name; |
| 437 | return false; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 438 | } |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 439 | |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 440 | size_t last_nondigit_pos = partition_name.find_last_not_of("0123456789"); |
| 441 | if (last_nondigit_pos == string::npos || |
| 442 | (last_nondigit_pos + 1) == partition_name.size()) { |
| 443 | LOG(ERROR) << "Unable to parse partition device name: " << partition_name; |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 444 | return false; |
| 445 | } |
| 446 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 447 | size_t partition_name_len = string::npos; |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 448 | if (partition_name[last_nondigit_pos] == '_') { |
| 449 | // NAND block devices have weird naming which could be something |
| 450 | // like "/dev/ubiblock2_0". We discard "_0" in such a case. |
| 451 | size_t prev_nondigit_pos = |
| 452 | partition_name.find_last_not_of("0123456789", last_nondigit_pos - 1); |
| 453 | if (prev_nondigit_pos == string::npos || |
| 454 | (prev_nondigit_pos + 1) == last_nondigit_pos) { |
| 455 | LOG(ERROR) << "Unable to parse partition device name: " << partition_name; |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | partition_name_len = last_nondigit_pos - prev_nondigit_pos; |
| 460 | last_nondigit_pos = prev_nondigit_pos; |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | if (out_disk_name) { |
| 464 | // Special case for MMC devices which have the following naming scheme: |
| 465 | // mmcblk0p2 |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 466 | size_t disk_name_len = last_nondigit_pos; |
| 467 | if (partition_name[last_nondigit_pos] != 'p' || |
| 468 | last_nondigit_pos == 0 || |
| 469 | !isdigit(partition_name[last_nondigit_pos - 1])) { |
| 470 | disk_name_len++; |
| 471 | } |
| 472 | *out_disk_name = partition_name.substr(0, disk_name_len); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | if (out_partition_num) { |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 476 | string partition_str = partition_name.substr(last_nondigit_pos + 1, |
| 477 | partition_name_len); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 478 | *out_partition_num = atoi(partition_str.c_str()); |
| 479 | } |
| 480 | return true; |
| 481 | } |
| 482 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 483 | string MakePartitionName(const string& disk_name, int partition_num) { |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 484 | if (partition_num < 1) { |
| 485 | LOG(ERROR) << "Invalid partition number: " << partition_num; |
| 486 | return string(); |
| 487 | } |
| 488 | |
Alex Vakulenko | 6a9d349 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 489 | if (!base::StartsWithASCII(disk_name, "/dev/", true)) { |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 490 | LOG(ERROR) << "Invalid disk name: " << disk_name; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 491 | return string(); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 492 | } |
| 493 | |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 494 | if (IsMtdDeviceName(disk_name)) { |
| 495 | // Special case for UBI block devices. |
| 496 | // 1. ubiblock is not writable, we need to use plain "ubi". |
| 497 | // 2. There is a "_0" suffix. |
| 498 | return MakeNandPartitionName(partition_num); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 501 | string partition_name = disk_name; |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 502 | if (isdigit(partition_name.back())) { |
| 503 | // Special case for devices with names ending with a digit. |
| 504 | // Add "p" to separate the disk name from partition number, |
| 505 | // e.g. "/dev/loop0p2" |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 506 | partition_name += 'p'; |
| 507 | } |
| 508 | |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 509 | partition_name += std::to_string(partition_num); |
| 510 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 511 | return partition_name; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 514 | string MakePartitionNameForMount(const string& part_name) { |
| 515 | if (IsMtdDeviceName(part_name)) { |
| 516 | int partition_num; |
| 517 | if (!SplitPartitionName(part_name, nullptr, &partition_num)) { |
| 518 | return ""; |
| 519 | } |
| 520 | return MakeNandPartitionNameForMount(partition_num); |
| 521 | } |
| 522 | return part_name; |
| 523 | } |
| 524 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 525 | string ErrnoNumberAsString(int err) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 526 | char buf[100]; |
| 527 | buf[0] = '\0'; |
| 528 | return strerror_r(err, buf, sizeof(buf)); |
| 529 | } |
| 530 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 531 | bool FileExists(const char* path) { |
| 532 | struct stat stbuf; |
| 533 | return 0 == lstat(path, &stbuf); |
| 534 | } |
| 535 | |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 536 | bool IsSymlink(const char* path) { |
| 537 | struct stat stbuf; |
| 538 | return lstat(path, &stbuf) == 0 && S_ISLNK(stbuf.st_mode) != 0; |
| 539 | } |
| 540 | |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 541 | bool TryAttachingUbiVolume(int volume_num, int timeout) { |
| 542 | const string volume_path = base::StringPrintf("/dev/ubi%d_0", volume_num); |
| 543 | if (FileExists(volume_path.c_str())) { |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | int exit_code; |
| 548 | vector<string> cmd = { |
| 549 | "ubiattach", |
| 550 | "-m", |
| 551 | base::StringPrintf("%d", volume_num), |
| 552 | "-d", |
| 553 | base::StringPrintf("%d", volume_num) |
| 554 | }; |
| 555 | TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &exit_code, nullptr)); |
| 556 | TEST_AND_RETURN_FALSE(exit_code == 0); |
| 557 | |
| 558 | cmd = { |
| 559 | "ubiblock", |
| 560 | "--create", |
| 561 | volume_path |
| 562 | }; |
| 563 | TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &exit_code, nullptr)); |
| 564 | TEST_AND_RETURN_FALSE(exit_code == 0); |
| 565 | |
| 566 | while (timeout > 0 && !FileExists(volume_path.c_str())) { |
| 567 | sleep(1); |
| 568 | timeout--; |
| 569 | } |
| 570 | |
| 571 | return FileExists(volume_path.c_str()); |
| 572 | } |
| 573 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 574 | bool MakeTempFile(const string& base_filename_template, |
| 575 | string* filename, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 576 | int* fd) { |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 577 | base::FilePath filename_template; |
| 578 | TEST_AND_RETURN_FALSE( |
| 579 | GetTempName(base_filename_template, &filename_template)); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 580 | DCHECK(filename || fd); |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 581 | vector<char> buf(filename_template.value().size() + 1); |
| 582 | memcpy(buf.data(), filename_template.value().data(), |
| 583 | filename_template.value().size()); |
| 584 | buf[filename_template.value().size()] = '\0'; |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 585 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 586 | int mkstemp_fd = mkstemp(buf.data()); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 587 | TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0); |
| 588 | if (filename) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 589 | *filename = buf.data(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 590 | } |
| 591 | if (fd) { |
| 592 | *fd = mkstemp_fd; |
| 593 | } else { |
| 594 | close(mkstemp_fd); |
| 595 | } |
| 596 | return true; |
| 597 | } |
| 598 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 599 | bool MakeTempDirectory(const string& base_dirname_template, |
| 600 | string* dirname) { |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 601 | base::FilePath dirname_template; |
| 602 | TEST_AND_RETURN_FALSE(GetTempName(base_dirname_template, &dirname_template)); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 603 | DCHECK(dirname); |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 604 | vector<char> buf(dirname_template.value().size() + 1); |
| 605 | memcpy(buf.data(), dirname_template.value().data(), |
| 606 | dirname_template.value().size()); |
| 607 | buf[dirname_template.value().size()] = '\0'; |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 608 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 609 | char* return_code = mkdtemp(buf.data()); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 610 | TEST_AND_RETURN_FALSE_ERRNO(return_code != nullptr); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 611 | *dirname = buf.data(); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 612 | return true; |
| 613 | } |
| 614 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 615 | bool MountFilesystem(const string& device, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 616 | const string& mountpoint, |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 617 | unsigned long mountflags) { // NOLINT(runtime/int) |
Alex Deymo | 4c82df3 | 2014-11-10 22:25:57 -0800 | [diff] [blame] | 618 | // TODO(sosa): Remove "ext3" once crbug.com/208022 is resolved. |
Alex Deymo | 5fc5e10 | 2015-09-18 10:36:21 -0700 | [diff] [blame] | 619 | const vector<const char*> fstypes{"ext2", "ext3", "ext4", "squashfs"}; |
Alex Deymo | 4c82df3 | 2014-11-10 22:25:57 -0800 | [diff] [blame] | 620 | for (const char* fstype : fstypes) { |
| 621 | int rc = mount(device.c_str(), mountpoint.c_str(), fstype, mountflags, |
| 622 | nullptr); |
| 623 | if (rc == 0) |
| 624 | return true; |
| 625 | |
| 626 | PLOG(WARNING) << "Unable to mount destination device " << device |
| 627 | << " on " << mountpoint << " as " << fstype; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 628 | } |
Alex Deymo | 4c82df3 | 2014-11-10 22:25:57 -0800 | [diff] [blame] | 629 | LOG(ERROR) << "Unable to mount " << device << " with any supported type"; |
| 630 | return false; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | bool UnmountFilesystem(const string& mountpoint) { |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 634 | for (int num_retries = 0; ; ++num_retries) { |
| 635 | if (umount(mountpoint.c_str()) == 0) |
| 636 | break; |
| 637 | |
| 638 | TEST_AND_RETURN_FALSE_ERRNO(errno == EBUSY && |
| 639 | num_retries < kUnmountMaxNumOfRetries); |
Alex Deymo | 8d2bbe3 | 2015-06-23 16:28:59 -0700 | [diff] [blame] | 640 | usleep(kUnmountRetryIntervalInMicroseconds); |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 641 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 642 | return true; |
| 643 | } |
| 644 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 645 | bool GetFilesystemSize(const string& device, |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 646 | int* out_block_count, |
| 647 | int* out_block_size) { |
| 648 | int fd = HANDLE_EINTR(open(device.c_str(), O_RDONLY)); |
Alex Deymo | e7141c6 | 2014-10-17 14:03:01 -0700 | [diff] [blame] | 649 | TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 650 | ScopedFdCloser fd_closer(&fd); |
| 651 | return GetFilesystemSizeFromFD(fd, out_block_count, out_block_size); |
| 652 | } |
| 653 | |
| 654 | bool GetFilesystemSizeFromFD(int fd, |
| 655 | int* out_block_count, |
| 656 | int* out_block_size) { |
| 657 | TEST_AND_RETURN_FALSE(fd >= 0); |
| 658 | |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 659 | // Determine the filesystem size by directly reading the block count and |
| 660 | // block size information from the superblock. Supported FS are ext3 and |
| 661 | // squashfs. |
| 662 | |
| 663 | // Read from the fd only once and detect in memory. The first 2 KiB is enough |
| 664 | // to read the ext2 superblock (located at offset 1024) and the squashfs |
| 665 | // superblock (located at offset 0). |
| 666 | const ssize_t kBufferSize = 2048; |
| 667 | |
| 668 | uint8_t buffer[kBufferSize]; |
| 669 | if (HANDLE_EINTR(pread(fd, buffer, kBufferSize, 0)) != kBufferSize) { |
| 670 | PLOG(ERROR) << "Unable to read the file system header:"; |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 671 | return false; |
| 672 | } |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 673 | |
| 674 | if (GetSquashfs4Size(buffer, kBufferSize, out_block_count, out_block_size)) |
| 675 | return true; |
| 676 | if (GetExt3Size(buffer, kBufferSize, out_block_count, out_block_size)) |
| 677 | return true; |
| 678 | |
| 679 | LOG(ERROR) << "Unable to determine file system type."; |
| 680 | return false; |
| 681 | } |
| 682 | |
| 683 | bool GetExt3Size(const uint8_t* buffer, size_t buffer_size, |
| 684 | int* out_block_count, |
| 685 | int* out_block_size) { |
| 686 | // See include/linux/ext2_fs.h for more details on the structure. We obtain |
| 687 | // ext2 constants from ext2fs/ext2fs.h header but we don't link with the |
| 688 | // library. |
| 689 | if (buffer_size < SUPERBLOCK_OFFSET + SUPERBLOCK_SIZE) |
| 690 | return false; |
| 691 | |
| 692 | const uint8_t* superblock = buffer + SUPERBLOCK_OFFSET; |
| 693 | |
| 694 | // ext3_fs.h: ext3_super_block.s_blocks_count |
| 695 | uint32_t block_count = |
| 696 | *reinterpret_cast<const uint32_t*>(superblock + 1 * sizeof(int32_t)); |
| 697 | |
| 698 | // ext3_fs.h: ext3_super_block.s_log_block_size |
| 699 | uint32_t log_block_size = |
| 700 | *reinterpret_cast<const uint32_t*>(superblock + 6 * sizeof(int32_t)); |
| 701 | |
| 702 | // ext3_fs.h: ext3_super_block.s_magic |
| 703 | uint16_t magic = |
| 704 | *reinterpret_cast<const uint16_t*>(superblock + 14 * sizeof(int32_t)); |
| 705 | |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 706 | block_count = le32toh(block_count); |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 707 | log_block_size = le32toh(log_block_size) + EXT2_MIN_BLOCK_LOG_SIZE; |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 708 | magic = le16toh(magic); |
| 709 | |
| 710 | // Sanity check the parameters. |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 711 | TEST_AND_RETURN_FALSE(magic == EXT2_SUPER_MAGIC); |
| 712 | TEST_AND_RETURN_FALSE(log_block_size >= EXT2_MIN_BLOCK_LOG_SIZE && |
| 713 | log_block_size <= EXT2_MAX_BLOCK_LOG_SIZE); |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 714 | TEST_AND_RETURN_FALSE(block_count > 0); |
| 715 | |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 716 | if (out_block_count) |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 717 | *out_block_count = block_count; |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 718 | if (out_block_size) |
| 719 | *out_block_size = 1 << log_block_size; |
| 720 | return true; |
| 721 | } |
| 722 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 723 | bool GetSquashfs4Size(const uint8_t* buffer, size_t buffer_size, |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 724 | int* out_block_count, |
| 725 | int* out_block_size) { |
| 726 | // See fs/squashfs/squashfs_fs.h for format details. We only support |
| 727 | // Squashfs 4.x little endian. |
| 728 | |
| 729 | // sizeof(struct squashfs_super_block) |
| 730 | const size_t kSquashfsSuperBlockSize = 96; |
| 731 | if (buffer_size < kSquashfsSuperBlockSize) |
| 732 | return false; |
| 733 | |
| 734 | // Check magic, squashfs_fs.h: SQUASHFS_MAGIC |
| 735 | if (memcmp(buffer, "hsqs", 4) != 0) |
| 736 | return false; // Only little endian is supported. |
| 737 | |
| 738 | // squashfs_fs.h: struct squashfs_super_block.s_major |
| 739 | uint16_t s_major = *reinterpret_cast<const uint16_t*>( |
| 740 | buffer + 5 * sizeof(uint32_t) + 4 * sizeof(uint16_t)); |
| 741 | |
| 742 | if (s_major != 4) { |
| 743 | LOG(ERROR) << "Found unsupported squashfs major version " << s_major; |
| 744 | return false; |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 745 | } |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 746 | |
| 747 | // squashfs_fs.h: struct squashfs_super_block.bytes_used |
| 748 | uint64_t bytes_used = *reinterpret_cast<const int64_t*>( |
| 749 | buffer + 5 * sizeof(uint32_t) + 6 * sizeof(uint16_t) + sizeof(uint64_t)); |
| 750 | |
| 751 | const int block_size = 4096; |
| 752 | |
| 753 | // The squashfs' bytes_used doesn't need to be aligned with the block boundary |
| 754 | // so we round up to the nearest blocksize. |
| 755 | if (out_block_count) |
| 756 | *out_block_count = (bytes_used + block_size - 1) / block_size; |
| 757 | if (out_block_size) |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 758 | *out_block_size = block_size; |
Darin Petkov | d3f8c89 | 2010-10-12 21:38:45 -0700 | [diff] [blame] | 759 | return true; |
| 760 | } |
| 761 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 762 | bool IsExtFilesystem(const string& device) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 763 | brillo::Blob header; |
Alex Deymo | de942f3 | 2015-03-13 11:57:15 -0700 | [diff] [blame] | 764 | // The first 2 KiB is enough to read the ext2 superblock (located at offset |
| 765 | // 1024). |
| 766 | if (!ReadFileChunk(device, 0, 2048, &header)) |
| 767 | return false; |
| 768 | return GetExt3Size(header.data(), header.size(), nullptr, nullptr); |
| 769 | } |
| 770 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 771 | bool IsSquashfsFilesystem(const string& device) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 772 | brillo::Blob header; |
Alex Deymo | de942f3 | 2015-03-13 11:57:15 -0700 | [diff] [blame] | 773 | // The first 96 is enough to read the squashfs superblock. |
| 774 | const ssize_t kSquashfsSuperBlockSize = 96; |
| 775 | if (!ReadFileChunk(device, 0, kSquashfsSuperBlockSize, &header)) |
| 776 | return false; |
| 777 | return GetSquashfs4Size(header.data(), header.size(), nullptr, nullptr); |
| 778 | } |
| 779 | |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 780 | // Tries to parse the header of an ELF file to obtain a human-readable |
| 781 | // description of it on the |output| string. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 782 | static bool GetFileFormatELF(const uint8_t* buffer, size_t size, |
| 783 | string* output) { |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 784 | // 0x00: EI_MAG - ELF magic header, 4 bytes. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 785 | if (size < SELFMAG || memcmp(buffer, ELFMAG, SELFMAG) != 0) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 786 | return false; |
| 787 | *output = "ELF"; |
| 788 | |
| 789 | // 0x04: EI_CLASS, 1 byte. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 790 | if (size < EI_CLASS + 1) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 791 | return true; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 792 | switch (buffer[EI_CLASS]) { |
| 793 | case ELFCLASS32: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 794 | *output += " 32-bit"; |
| 795 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 796 | case ELFCLASS64: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 797 | *output += " 64-bit"; |
| 798 | break; |
| 799 | default: |
| 800 | *output += " ?-bit"; |
| 801 | } |
| 802 | |
| 803 | // 0x05: EI_DATA, endianness, 1 byte. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 804 | if (size < EI_DATA + 1) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 805 | return true; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 806 | uint8_t ei_data = buffer[EI_DATA]; |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 807 | switch (ei_data) { |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 808 | case ELFDATA2LSB: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 809 | *output += " little-endian"; |
| 810 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 811 | case ELFDATA2MSB: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 812 | *output += " big-endian"; |
| 813 | break; |
| 814 | default: |
| 815 | *output += " ?-endian"; |
| 816 | // Don't parse anything after the 0x10 offset if endianness is unknown. |
| 817 | return true; |
| 818 | } |
| 819 | |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 820 | const Elf32_Ehdr* hdr = reinterpret_cast<const Elf32_Ehdr*>(buffer); |
| 821 | // 0x12: e_machine, 2 byte endianness based on ei_data. The position (0x12) |
| 822 | // and size is the same for both 32 and 64 bits. |
| 823 | if (size < offsetof(Elf32_Ehdr, e_machine) + sizeof(hdr->e_machine)) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 824 | return true; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 825 | uint16_t e_machine; |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 826 | // Fix endianess regardless of the host endianess. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 827 | if (ei_data == ELFDATA2LSB) |
| 828 | e_machine = le16toh(hdr->e_machine); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 829 | else |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 830 | e_machine = be16toh(hdr->e_machine); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 831 | |
| 832 | switch (e_machine) { |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 833 | case EM_386: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 834 | *output += " x86"; |
| 835 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 836 | case EM_MIPS: |
| 837 | *output += " mips"; |
| 838 | break; |
| 839 | case EM_ARM: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 840 | *output += " arm"; |
| 841 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 842 | case EM_X86_64: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 843 | *output += " x86-64"; |
| 844 | break; |
| 845 | default: |
| 846 | *output += " unknown-arch"; |
| 847 | } |
| 848 | return true; |
| 849 | } |
| 850 | |
| 851 | string GetFileFormat(const string& path) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 852 | brillo::Blob buffer; |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 853 | if (!ReadFileChunkAndAppend(path, 0, kGetFileFormatMaxHeaderSize, &buffer)) |
| 854 | return "File not found."; |
| 855 | |
| 856 | string result; |
| 857 | if (GetFileFormatELF(buffer.data(), buffer.size(), &result)) |
| 858 | return result; |
| 859 | |
| 860 | return "data"; |
| 861 | } |
| 862 | |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 863 | namespace { |
| 864 | // Do the actual trigger. We do it as a main-loop callback to (try to) get a |
| 865 | // consistent stack trace. |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 866 | void TriggerCrashReporterUpload() { |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 867 | pid_t pid = fork(); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 868 | CHECK_GE(pid, 0) << "fork failed"; // fork() failed. Something is very wrong. |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 869 | if (pid == 0) { |
| 870 | // We are the child. Crash. |
| 871 | abort(); // never returns |
| 872 | } |
| 873 | // We are the parent. Wait for child to terminate. |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 874 | pid_t result = waitpid(pid, nullptr, 0); |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 875 | LOG_IF(ERROR, result < 0) << "waitpid() failed"; |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 876 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 877 | } // namespace |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 878 | |
| 879 | void ScheduleCrashReporterUpload() { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 880 | brillo::MessageLoop::current()->PostTask( |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 881 | FROM_HERE, |
| 882 | base::Bind(&TriggerCrashReporterUpload)); |
Andrew de los Reyes | 712b3ac | 2011-01-07 13:47:52 -0800 | [diff] [blame] | 883 | } |
| 884 | |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 885 | bool SetCpuShares(CpuShares shares) { |
| 886 | string string_shares = base::IntToString(static_cast<int>(shares)); |
| 887 | string cpu_shares_file = string(utils::kCGroupDir) + "/cpu.shares"; |
| 888 | LOG(INFO) << "Setting cgroup cpu shares to " << string_shares; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 889 | if (utils::WriteFile(cpu_shares_file.c_str(), string_shares.c_str(), |
| 890 | string_shares.size())) { |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 891 | return true; |
| 892 | } else { |
| 893 | LOG(ERROR) << "Failed to change cgroup cpu shares to "<< string_shares |
| 894 | << " using " << cpu_shares_file; |
| 895 | return false; |
| 896 | } |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 899 | int FuzzInt(int value, unsigned int range) { |
| 900 | int min = value - range / 2; |
| 901 | int max = value + range - range / 2; |
| 902 | return base::RandInt(min, max); |
| 903 | } |
| 904 | |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 905 | string FormatSecs(unsigned secs) { |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 906 | return FormatTimeDelta(TimeDelta::FromSeconds(secs)); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 909 | string FormatTimeDelta(TimeDelta delta) { |
David Zeuthen | 973449e | 2014-08-18 16:18:23 -0400 | [diff] [blame] | 910 | string str; |
| 911 | |
| 912 | // Handle negative durations by prefixing with a minus. |
| 913 | if (delta.ToInternalValue() < 0) { |
| 914 | delta *= -1; |
| 915 | str = "-"; |
| 916 | } |
| 917 | |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 918 | // Canonicalize into days, hours, minutes, seconds and microseconds. |
| 919 | unsigned days = delta.InDays(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 920 | delta -= TimeDelta::FromDays(days); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 921 | unsigned hours = delta.InHours(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 922 | delta -= TimeDelta::FromHours(hours); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 923 | unsigned mins = delta.InMinutes(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 924 | delta -= TimeDelta::FromMinutes(mins); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 925 | unsigned secs = delta.InSeconds(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 926 | delta -= TimeDelta::FromSeconds(secs); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 927 | unsigned usecs = delta.InMicroseconds(); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 928 | |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 929 | if (days) |
| 930 | base::StringAppendF(&str, "%ud", days); |
| 931 | if (days || hours) |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 932 | base::StringAppendF(&str, "%uh", hours); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 933 | if (days || hours || mins) |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 934 | base::StringAppendF(&str, "%um", mins); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 935 | base::StringAppendF(&str, "%u", secs); |
| 936 | if (usecs) { |
| 937 | int width = 6; |
| 938 | while ((usecs / 10) * 10 == usecs) { |
| 939 | usecs /= 10; |
| 940 | width--; |
| 941 | } |
| 942 | base::StringAppendF(&str, ".%0*u", width, usecs); |
| 943 | } |
| 944 | base::StringAppendF(&str, "s"); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 945 | return str; |
| 946 | } |
| 947 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 948 | string ToString(const Time utc_time) { |
| 949 | Time::Exploded exp_time; |
| 950 | utc_time.UTCExplode(&exp_time); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 951 | return base::StringPrintf("%d/%d/%d %d:%02d:%02d GMT", |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 952 | exp_time.month, |
| 953 | exp_time.day_of_month, |
| 954 | exp_time.year, |
| 955 | exp_time.hour, |
| 956 | exp_time.minute, |
| 957 | exp_time.second); |
| 958 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 959 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 960 | string ToString(bool b) { |
| 961 | return (b ? "true" : "false"); |
| 962 | } |
| 963 | |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 964 | string ToString(DownloadSource source) { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 965 | switch (source) { |
| 966 | case kDownloadSourceHttpsServer: return "HttpsServer"; |
| 967 | case kDownloadSourceHttpServer: return "HttpServer"; |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 968 | case kDownloadSourceHttpPeer: return "HttpPeer"; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 969 | case kNumDownloadSources: return "Unknown"; |
| 970 | // Don't add a default case to let the compiler warn about newly added |
| 971 | // download sources which should be added here. |
| 972 | } |
| 973 | |
| 974 | return "Unknown"; |
| 975 | } |
| 976 | |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 977 | string ToString(PayloadType payload_type) { |
| 978 | switch (payload_type) { |
| 979 | case kPayloadTypeDelta: return "Delta"; |
| 980 | case kPayloadTypeFull: return "Full"; |
| 981 | case kPayloadTypeForcedFull: return "ForcedFull"; |
| 982 | case kNumPayloadTypes: return "Unknown"; |
| 983 | // Don't add a default case to let the compiler warn about newly added |
| 984 | // payload types which should be added here. |
| 985 | } |
| 986 | |
| 987 | return "Unknown"; |
| 988 | } |
| 989 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 990 | ErrorCode GetBaseErrorCode(ErrorCode code) { |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 991 | // Ignore the higher order bits in the code by applying the mask as |
| 992 | // we want the enumerations to be in the small contiguous range |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 993 | // with values less than ErrorCode::kUmaReportedMax. |
| 994 | ErrorCode base_code = static_cast<ErrorCode>( |
| 995 | static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags)); |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 996 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 997 | // Make additional adjustments required for UMA and error classification. |
| 998 | // TODO(jaysri): Move this logic to UeErrorCode.cc when we fix |
| 999 | // chromium-os:34369. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1000 | if (base_code >= ErrorCode::kOmahaRequestHTTPResponseBase) { |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 1001 | // 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] | 1002 | // errors into this one bucket for UMA and error classification purposes. |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1003 | LOG(INFO) << "Converting error code " << base_code |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1004 | << " to ErrorCode::kOmahaErrorInHTTPResponse"; |
| 1005 | base_code = ErrorCode::kOmahaErrorInHTTPResponse; |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 1008 | return base_code; |
| 1009 | } |
| 1010 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1011 | string CodeToString(ErrorCode code) { |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1012 | // If the given code has both parts (i.e. the error code part and the flags |
| 1013 | // part) then strip off the flags part since the switch statement below |
| 1014 | // has case statements only for the base error code or a single flag but |
| 1015 | // doesn't support any combinations of those. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1016 | if ((static_cast<int>(code) & static_cast<int>(ErrorCode::kSpecialFlags)) && |
| 1017 | (static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags))) |
| 1018 | code = static_cast<ErrorCode>( |
| 1019 | static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags)); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1020 | switch (code) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1021 | case ErrorCode::kSuccess: return "ErrorCode::kSuccess"; |
| 1022 | case ErrorCode::kError: return "ErrorCode::kError"; |
| 1023 | case ErrorCode::kOmahaRequestError: return "ErrorCode::kOmahaRequestError"; |
| 1024 | case ErrorCode::kOmahaResponseHandlerError: |
| 1025 | return "ErrorCode::kOmahaResponseHandlerError"; |
| 1026 | case ErrorCode::kFilesystemCopierError: |
| 1027 | return "ErrorCode::kFilesystemCopierError"; |
| 1028 | case ErrorCode::kPostinstallRunnerError: |
| 1029 | return "ErrorCode::kPostinstallRunnerError"; |
| 1030 | case ErrorCode::kPayloadMismatchedType: |
| 1031 | return "ErrorCode::kPayloadMismatchedType"; |
| 1032 | case ErrorCode::kInstallDeviceOpenError: |
| 1033 | return "ErrorCode::kInstallDeviceOpenError"; |
| 1034 | case ErrorCode::kKernelDeviceOpenError: |
| 1035 | return "ErrorCode::kKernelDeviceOpenError"; |
| 1036 | case ErrorCode::kDownloadTransferError: |
| 1037 | return "ErrorCode::kDownloadTransferError"; |
| 1038 | case ErrorCode::kPayloadHashMismatchError: |
| 1039 | return "ErrorCode::kPayloadHashMismatchError"; |
| 1040 | case ErrorCode::kPayloadSizeMismatchError: |
| 1041 | return "ErrorCode::kPayloadSizeMismatchError"; |
| 1042 | case ErrorCode::kDownloadPayloadVerificationError: |
| 1043 | return "ErrorCode::kDownloadPayloadVerificationError"; |
| 1044 | case ErrorCode::kDownloadNewPartitionInfoError: |
| 1045 | return "ErrorCode::kDownloadNewPartitionInfoError"; |
| 1046 | case ErrorCode::kDownloadWriteError: |
| 1047 | return "ErrorCode::kDownloadWriteError"; |
| 1048 | case ErrorCode::kNewRootfsVerificationError: |
| 1049 | return "ErrorCode::kNewRootfsVerificationError"; |
| 1050 | case ErrorCode::kNewKernelVerificationError: |
| 1051 | return "ErrorCode::kNewKernelVerificationError"; |
| 1052 | case ErrorCode::kSignedDeltaPayloadExpectedError: |
| 1053 | return "ErrorCode::kSignedDeltaPayloadExpectedError"; |
| 1054 | case ErrorCode::kDownloadPayloadPubKeyVerificationError: |
| 1055 | return "ErrorCode::kDownloadPayloadPubKeyVerificationError"; |
| 1056 | case ErrorCode::kPostinstallBootedFromFirmwareB: |
| 1057 | return "ErrorCode::kPostinstallBootedFromFirmwareB"; |
| 1058 | case ErrorCode::kDownloadStateInitializationError: |
| 1059 | return "ErrorCode::kDownloadStateInitializationError"; |
| 1060 | case ErrorCode::kDownloadInvalidMetadataMagicString: |
| 1061 | return "ErrorCode::kDownloadInvalidMetadataMagicString"; |
| 1062 | case ErrorCode::kDownloadSignatureMissingInManifest: |
| 1063 | return "ErrorCode::kDownloadSignatureMissingInManifest"; |
| 1064 | case ErrorCode::kDownloadManifestParseError: |
| 1065 | return "ErrorCode::kDownloadManifestParseError"; |
| 1066 | case ErrorCode::kDownloadMetadataSignatureError: |
| 1067 | return "ErrorCode::kDownloadMetadataSignatureError"; |
| 1068 | case ErrorCode::kDownloadMetadataSignatureVerificationError: |
| 1069 | return "ErrorCode::kDownloadMetadataSignatureVerificationError"; |
| 1070 | case ErrorCode::kDownloadMetadataSignatureMismatch: |
| 1071 | return "ErrorCode::kDownloadMetadataSignatureMismatch"; |
| 1072 | case ErrorCode::kDownloadOperationHashVerificationError: |
| 1073 | return "ErrorCode::kDownloadOperationHashVerificationError"; |
| 1074 | case ErrorCode::kDownloadOperationExecutionError: |
| 1075 | return "ErrorCode::kDownloadOperationExecutionError"; |
| 1076 | case ErrorCode::kDownloadOperationHashMismatch: |
| 1077 | return "ErrorCode::kDownloadOperationHashMismatch"; |
| 1078 | case ErrorCode::kOmahaRequestEmptyResponseError: |
| 1079 | return "ErrorCode::kOmahaRequestEmptyResponseError"; |
| 1080 | case ErrorCode::kOmahaRequestXMLParseError: |
| 1081 | return "ErrorCode::kOmahaRequestXMLParseError"; |
| 1082 | case ErrorCode::kDownloadInvalidMetadataSize: |
| 1083 | return "ErrorCode::kDownloadInvalidMetadataSize"; |
| 1084 | case ErrorCode::kDownloadInvalidMetadataSignature: |
| 1085 | return "ErrorCode::kDownloadInvalidMetadataSignature"; |
| 1086 | case ErrorCode::kOmahaResponseInvalid: |
| 1087 | return "ErrorCode::kOmahaResponseInvalid"; |
| 1088 | case ErrorCode::kOmahaUpdateIgnoredPerPolicy: |
| 1089 | return "ErrorCode::kOmahaUpdateIgnoredPerPolicy"; |
| 1090 | case ErrorCode::kOmahaUpdateDeferredPerPolicy: |
| 1091 | return "ErrorCode::kOmahaUpdateDeferredPerPolicy"; |
| 1092 | case ErrorCode::kOmahaErrorInHTTPResponse: |
| 1093 | return "ErrorCode::kOmahaErrorInHTTPResponse"; |
| 1094 | case ErrorCode::kDownloadOperationHashMissingError: |
| 1095 | return "ErrorCode::kDownloadOperationHashMissingError"; |
| 1096 | case ErrorCode::kDownloadMetadataSignatureMissingError: |
| 1097 | return "ErrorCode::kDownloadMetadataSignatureMissingError"; |
| 1098 | case ErrorCode::kOmahaUpdateDeferredForBackoff: |
| 1099 | return "ErrorCode::kOmahaUpdateDeferredForBackoff"; |
| 1100 | case ErrorCode::kPostinstallPowerwashError: |
| 1101 | return "ErrorCode::kPostinstallPowerwashError"; |
| 1102 | case ErrorCode::kUpdateCanceledByChannelChange: |
| 1103 | return "ErrorCode::kUpdateCanceledByChannelChange"; |
| 1104 | case ErrorCode::kUmaReportedMax: |
| 1105 | return "ErrorCode::kUmaReportedMax"; |
| 1106 | case ErrorCode::kOmahaRequestHTTPResponseBase: |
| 1107 | return "ErrorCode::kOmahaRequestHTTPResponseBase"; |
| 1108 | case ErrorCode::kResumedFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1109 | return "Resumed"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1110 | case ErrorCode::kDevModeFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1111 | return "DevMode"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1112 | case ErrorCode::kTestImageFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1113 | return "TestImage"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1114 | case ErrorCode::kTestOmahaUrlFlag: |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1115 | return "TestOmahaUrl"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1116 | case ErrorCode::kSpecialFlags: |
| 1117 | return "ErrorCode::kSpecialFlags"; |
| 1118 | case ErrorCode::kPostinstallFirmwareRONotUpdatable: |
| 1119 | return "ErrorCode::kPostinstallFirmwareRONotUpdatable"; |
| 1120 | case ErrorCode::kUnsupportedMajorPayloadVersion: |
| 1121 | return "ErrorCode::kUnsupportedMajorPayloadVersion"; |
| 1122 | case ErrorCode::kUnsupportedMinorPayloadVersion: |
| 1123 | return "ErrorCode::kUnsupportedMinorPayloadVersion"; |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 1124 | case ErrorCode::kOmahaRequestXMLHasEntityDecl: |
| 1125 | return "ErrorCode::kOmahaRequestXMLHasEntityDecl"; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 1126 | case ErrorCode::kFilesystemVerifierError: |
| 1127 | return "ErrorCode::kFilesystemVerifierError"; |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 1128 | // Don't add a default case to let the compiler warn about newly added |
| 1129 | // error codes which should be added here. |
| 1130 | } |
| 1131 | |
| 1132 | return "Unknown error: " + base::UintToString(static_cast<unsigned>(code)); |
| 1133 | } |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 1134 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1135 | bool CreatePowerwashMarkerFile(const char* file_path) { |
| 1136 | const char* marker_file = file_path ? file_path : kPowerwashMarkerFile; |
| 1137 | bool result = utils::WriteFile(marker_file, |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1138 | kPowerwashCommand, |
| 1139 | strlen(kPowerwashCommand)); |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1140 | if (result) { |
| 1141 | LOG(INFO) << "Created " << marker_file << " to powerwash on next reboot"; |
| 1142 | } else { |
| 1143 | PLOG(ERROR) << "Error in creating powerwash marker file: " << marker_file; |
| 1144 | } |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1145 | |
| 1146 | return result; |
| 1147 | } |
| 1148 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1149 | bool DeletePowerwashMarkerFile(const char* file_path) { |
| 1150 | const char* marker_file = file_path ? file_path : kPowerwashMarkerFile; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 1151 | const base::FilePath kPowerwashMarkerPath(marker_file); |
| 1152 | bool result = base::DeleteFile(kPowerwashMarkerPath, false); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1153 | |
| 1154 | if (result) |
| 1155 | LOG(INFO) << "Successfully deleted the powerwash marker file : " |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1156 | << marker_file; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1157 | else |
| 1158 | PLOG(ERROR) << "Could not delete the powerwash marker file : " |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 1159 | << marker_file; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 1160 | |
| 1161 | return result; |
| 1162 | } |
| 1163 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 1164 | Time TimeFromStructTimespec(struct timespec *ts) { |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 1165 | int64_t us = static_cast<int64_t>(ts->tv_sec) * Time::kMicrosecondsPerSecond + |
| 1166 | static_cast<int64_t>(ts->tv_nsec) / Time::kNanosecondsPerMicrosecond; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 1167 | return Time::UnixEpoch() + TimeDelta::FromMicroseconds(us); |
| 1168 | } |
| 1169 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1170 | string StringVectorToString(const vector<string> &vec_str) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 1171 | string str = "["; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1172 | for (vector<string>::const_iterator i = vec_str.begin(); |
| 1173 | i != vec_str.end(); ++i) { |
| 1174 | if (i != vec_str.begin()) |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 1175 | str += ", "; |
| 1176 | str += '"'; |
| 1177 | str += *i; |
| 1178 | str += '"'; |
| 1179 | } |
| 1180 | str += "]"; |
| 1181 | return str; |
| 1182 | } |
| 1183 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1184 | string CalculateP2PFileId(const string& payload_hash, size_t payload_size) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1185 | string encoded_hash = brillo::data_encoding::Base64Encode(payload_hash); |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 1186 | return base::StringPrintf("cros_update_size_%" PRIuS "_hash_%s", |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 1187 | payload_size, |
| 1188 | encoded_hash.c_str()); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1191 | bool DecodeAndStoreBase64String(const string& base64_encoded, |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1192 | base::FilePath *out_path) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1193 | brillo::Blob contents; |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1194 | |
| 1195 | out_path->clear(); |
| 1196 | |
| 1197 | if (base64_encoded.size() == 0) { |
| 1198 | LOG(ERROR) << "Can't decode empty string."; |
| 1199 | return false; |
| 1200 | } |
| 1201 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1202 | if (!brillo::data_encoding::Base64Decode(base64_encoded, &contents) || |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1203 | contents.size() == 0) { |
| 1204 | LOG(ERROR) << "Error decoding base64."; |
| 1205 | return false; |
| 1206 | } |
| 1207 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 1208 | FILE *file = base::CreateAndOpenTemporaryFile(out_path); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1209 | if (file == nullptr) { |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1210 | LOG(ERROR) << "Error creating temporary file."; |
| 1211 | return false; |
| 1212 | } |
| 1213 | |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 1214 | if (fwrite(contents.data(), 1, contents.size(), file) != contents.size()) { |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1215 | PLOG(ERROR) << "Error writing to temporary file."; |
| 1216 | if (fclose(file) != 0) |
| 1217 | PLOG(ERROR) << "Error closing temporary file."; |
| 1218 | if (unlink(out_path->value().c_str()) != 0) |
| 1219 | PLOG(ERROR) << "Error unlinking temporary file."; |
| 1220 | out_path->clear(); |
| 1221 | return false; |
| 1222 | } |
| 1223 | |
| 1224 | if (fclose(file) != 0) { |
| 1225 | PLOG(ERROR) << "Error closing temporary file."; |
| 1226 | out_path->clear(); |
| 1227 | return false; |
| 1228 | } |
| 1229 | |
| 1230 | return true; |
| 1231 | } |
| 1232 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1233 | bool ConvertToOmahaInstallDate(Time time, int *out_num_days) { |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1234 | time_t unix_time = time.ToTimeT(); |
| 1235 | // Output of: date +"%s" --date="Jan 1, 2007 0:00 PST". |
| 1236 | const time_t kOmahaEpoch = 1167638400; |
| 1237 | const int64_t kNumSecondsPerWeek = 7*24*3600; |
| 1238 | const int64_t kNumDaysPerWeek = 7; |
| 1239 | |
| 1240 | time_t omaha_time = unix_time - kOmahaEpoch; |
| 1241 | |
| 1242 | if (omaha_time < 0) |
| 1243 | return false; |
| 1244 | |
| 1245 | // Note, as per the comment in utils.h we are deliberately not |
| 1246 | // handling DST correctly. |
| 1247 | |
| 1248 | int64_t num_weeks_since_omaha_epoch = omaha_time / kNumSecondsPerWeek; |
| 1249 | *out_num_days = num_weeks_since_omaha_epoch * kNumDaysPerWeek; |
| 1250 | |
| 1251 | return true; |
| 1252 | } |
| 1253 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1254 | bool GetMinorVersion(const brillo::KeyValueStore& store, |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 1255 | uint32_t* minor_version) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1256 | string result; |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 1257 | if (store.GetString("PAYLOAD_MINOR_VERSION", &result)) { |
Allie Wood | 425aa97 | 2015-02-17 14:47:17 -0800 | [diff] [blame] | 1258 | if (!base::StringToUint(result, minor_version)) { |
| 1259 | LOG(ERROR) << "StringToUint failed when parsing delta minor version."; |
| 1260 | return false; |
| 1261 | } |
Allie Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 1262 | return true; |
| 1263 | } |
| 1264 | return false; |
| 1265 | } |
| 1266 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1267 | bool ReadExtents(const string& path, const vector<Extent>& extents, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1268 | brillo::Blob* out_data, ssize_t out_data_size, |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 1269 | size_t block_size) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1270 | brillo::Blob data(out_data_size); |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 1271 | ssize_t bytes_read = 0; |
| 1272 | int fd = open(path.c_str(), O_RDONLY); |
| 1273 | TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); |
| 1274 | ScopedFdCloser fd_closer(&fd); |
| 1275 | |
Gilad Arnold | 41e3474 | 2015-05-11 11:31:50 -0700 | [diff] [blame] | 1276 | for (const Extent& extent : extents) { |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 1277 | ssize_t bytes_read_this_iteration = 0; |
| 1278 | ssize_t bytes = extent.num_blocks() * block_size; |
| 1279 | TEST_AND_RETURN_FALSE(bytes_read + bytes <= out_data_size); |
| 1280 | TEST_AND_RETURN_FALSE(utils::PReadAll(fd, |
| 1281 | &data[bytes_read], |
| 1282 | bytes, |
| 1283 | extent.start_block() * block_size, |
| 1284 | &bytes_read_this_iteration)); |
| 1285 | TEST_AND_RETURN_FALSE(bytes_read_this_iteration == bytes); |
| 1286 | bytes_read += bytes_read_this_iteration; |
| 1287 | } |
| 1288 | TEST_AND_RETURN_FALSE(out_data_size == bytes_read); |
| 1289 | *out_data = data; |
| 1290 | return true; |
| 1291 | } |
| 1292 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 1293 | bool GetBootId(string* boot_id) { |
| 1294 | TEST_AND_RETURN_FALSE( |
| 1295 | base::ReadFileToString(base::FilePath(kBootIdPath), boot_id)); |
| 1296 | base::TrimWhitespaceASCII(*boot_id, base::TRIM_TRAILING, boot_id); |
| 1297 | return true; |
| 1298 | } |
| 1299 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1300 | } // namespace utils |
| 1301 | |
| 1302 | } // namespace chromeos_update_engine |