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