blob: cf7913c8ab9a755b167c1f814b470dd7fe4bac9e [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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.com3defe6a2009-12-04 20:57:17 +000016
17#include "update_engine/utils.h"
Darin Petkovf74eb652010-08-04 12:08:38 -070018
Ben Chan9abb7632014-08-07 00:10:53 -070019#include <stdint.h>
20
adlr@google.com3defe6a2009-12-04 20:57:17 +000021#include <dirent.h>
Alex Deymoc1711e22014-08-08 13:16:23 -070022#include <elf.h>
Alex Deymo6f20dd42015-08-18 16:42:46 -070023#include <endian.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000024#include <errno.h>
Alex Deymo192393b2014-11-10 15:58:38 -080025#include <ext2fs/ext2fs.h>
Andrew de los Reyes970bb282009-12-09 16:34:04 -080026#include <fcntl.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Alex Deymoc4acdf42014-05-28 21:07:10 -070030#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.com3defe6a2009-12-04 20:57:17 +000035#include <unistd.h>
Darin Petkovf74eb652010-08-04 12:08:38 -070036
adlr@google.com3defe6a2009-12-04 20:57:17 +000037#include <algorithm>
Alex Vakulenkod2779df2014-06-16 13:19:00 -070038#include <utility>
Chris Sosac1972482013-04-30 22:31:10 -070039#include <vector>
Darin Petkovf74eb652010-08-04 12:08:38 -070040
Alex Vakulenko4906c1c2014-08-21 13:17:44 -070041#include <base/callback.h>
Ben Chan736fcb52014-05-21 18:28:22 -070042#include <base/files/file_path.h>
Alex Deymo192393b2014-11-10 15:58:38 -080043#include <base/files/file_util.h>
Ben Chan736fcb52014-05-21 18:28:22 -070044#include <base/files/scoped_file.h>
Alex Deymoc00c98a2015-03-17 17:38:00 -070045#include <base/format_macros.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070046#include <base/location.h>
Mike Frysinger8155d082012-04-06 15:23:18 -040047#include <base/logging.h>
Chris Sosafc661a12013-02-26 14:43:21 -080048#include <base/posix/eintr_wrapper.h>
Will Drewry8f71da82010-08-30 14:07:11 -050049#include <base/rand_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070050#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 Vakulenko981a9fb2015-02-09 12:51:24 -080054#include <chromeos/data_encoding.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070055#include <chromeos/message_loops/message_loop.h>
Will Drewry8f71da82010-08-30 14:07:11 -050056
David Zeuthen33bae492014-02-25 16:16:18 -080057#include "update_engine/clock_interface.h"
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070058#include "update_engine/constants.h"
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080059#include "update_engine/file_descriptor.h"
Andrew de los Reyes970bb282009-12-09 16:34:04 -080060#include "update_engine/file_writer.h"
Darin Petkov33d30642010-08-04 10:18:57 -070061#include "update_engine/omaha_request_params.h"
David Zeuthen33bae492014-02-25 16:16:18 -080062#include "update_engine/prefs_interface.h"
Darin Petkov296889c2010-07-23 16:20:54 -070063#include "update_engine/subprocess.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -080064#include "update_engine/system_state.h"
65#include "update_engine/update_attempter.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000066
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070067using base::Time;
Gilad Arnold8e3f1262013-01-08 14:59:54 -080068using base::TimeDelta;
adlr@google.com3defe6a2009-12-04 20:57:17 +000069using std::min;
Chris Sosac1972482013-04-30 22:31:10 -070070using std::pair;
adlr@google.com3defe6a2009-12-04 20:57:17 +000071using std::string;
72using std::vector;
73
74namespace chromeos_update_engine {
75
Ben Chan77a1eba2012-10-07 22:54:55 -070076namespace {
77
78// The following constants control how UnmountFilesystem should retry if
79// umount() fails with an errno EBUSY, i.e. retry 5 times over the course of
80// one second.
81const int kUnmountMaxNumOfRetries = 5;
82const int kUnmountRetryIntervalInMicroseconds = 200 * 1000; // 200 ms
Alex Deymo032e7722014-03-25 17:53:56 -070083
84// Number of bytes to read from a file to attempt to detect its contents. Used
85// in GetFileFormat.
86const int kGetFileFormatMaxHeaderSize = 32;
87
Alex Deymodd132f32015-09-14 19:12:07 -070088// The path to the kernel's boot_id.
89const char kBootIdPath[] = "/proc/sys/kernel/random/boot_id";
90
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080091// Return true if |disk_name| is an MTD or a UBI device. Note that this test is
92// simply based on the name of the device.
93bool IsMtdDeviceName(const string& disk_name) {
Alex Vakulenko6a9d3492015-06-15 12:53:22 -070094 return base::StartsWithASCII(disk_name, "/dev/ubi", true) ||
95 base::StartsWithASCII(disk_name, "/dev/mtd", true);
Nam T. Nguyena78b28c2015-03-06 22:30:12 -080096}
97
98// Return the device name for the corresponding partition on a NAND device.
99// WARNING: This function returns device names that are not mountable.
100string MakeNandPartitionName(int partition_num) {
101 switch (partition_num) {
102 case 2:
103 case 4:
104 case 6: {
105 return base::StringPrintf("/dev/mtd%d", partition_num);
106 }
107 default: {
108 return base::StringPrintf("/dev/ubi%d_0", partition_num);
109 }
110 }
111}
112
113// Return the device name for the corresponding partition on a NAND device that
114// may be mountable (but may not be writable).
115string MakeNandPartitionNameForMount(int partition_num) {
116 switch (partition_num) {
117 case 2:
118 case 4:
119 case 6: {
120 return base::StringPrintf("/dev/mtd%d", partition_num);
121 }
122 case 3:
123 case 5:
124 case 7: {
125 return base::StringPrintf("/dev/ubiblock%d_0", partition_num);
126 }
127 default: {
128 return base::StringPrintf("/dev/ubi%d_0", partition_num);
129 }
130 }
131}
132
Ben Chan77a1eba2012-10-07 22:54:55 -0700133} // namespace
134
adlr@google.com3defe6a2009-12-04 20:57:17 +0000135namespace utils {
136
Chris Sosa4f8ee272012-11-30 13:01:54 -0800137// Cgroup container is created in update-engine's upstart script located at
138// /etc/init/update-engine.conf.
139static const char kCGroupDir[] = "/sys/fs/cgroup/cpu/update-engine";
140
J. Richard Barnette63137e52013-10-28 10:57:29 -0700141string ParseECVersion(string input_line) {
Ben Chan736fcb52014-05-21 18:28:22 -0700142 base::TrimWhitespaceASCII(input_line, base::TRIM_ALL, &input_line);
Chris Sosac1972482013-04-30 22:31:10 -0700143
Alex Vakulenko75039d72014-03-25 12:36:28 -0700144 // At this point we want to convert the format key=value pair from mosys to
Chris Sosac1972482013-04-30 22:31:10 -0700145 // a vector of key value pairs.
Ben Chanf9cb98c2014-09-21 18:31:30 -0700146 vector<pair<string, string>> kv_pairs;
J. Richard Barnette63137e52013-10-28 10:57:29 -0700147 if (base::SplitStringIntoKeyValuePairs(input_line, '=', ' ', &kv_pairs)) {
Alex Deymo020600d2014-11-05 21:05:55 -0800148 for (const pair<string, string>& kv_pair : kv_pairs) {
Chris Sosac1972482013-04-30 22:31:10 -0700149 // Finally match against the fw_verion which may have quotes.
Alex Deymo020600d2014-11-05 21:05:55 -0800150 if (kv_pair.first == "fw_version") {
Chris Sosac1972482013-04-30 22:31:10 -0700151 string output;
152 // Trim any quotes.
Alex Deymo020600d2014-11-05 21:05:55 -0800153 base::TrimString(kv_pair.second, "\"", &output);
Chris Sosac1972482013-04-30 22:31:10 -0700154 return output;
155 }
156 }
157 }
158 LOG(ERROR) << "Unable to parse fwid from ec info.";
159 return "";
160}
161
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800162bool WriteFile(const char* path, const void* data, int data_len) {
Andrew de los Reyes970bb282009-12-09 16:34:04 -0800163 DirectFileWriter writer;
164 TEST_AND_RETURN_FALSE_ERRNO(0 == writer.Open(path,
165 O_WRONLY | O_CREAT | O_TRUNC,
Chris Masone4dc2ada2010-09-23 12:43:03 -0700166 0600));
Andrew de los Reyes970bb282009-12-09 16:34:04 -0800167 ScopedFileWriterCloser closer(&writer);
Don Garrette410e0f2011-11-10 15:39:01 -0800168 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(data, data_len));
Andrew de los Reyes970bb282009-12-09 16:34:04 -0800169 return true;
170}
171
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700172bool WriteAll(int fd, const void* buf, size_t count) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700173 const char* c_buf = static_cast<const char*>(buf);
174 ssize_t bytes_written = 0;
175 while (bytes_written < static_cast<ssize_t>(count)) {
176 ssize_t rc = write(fd, c_buf + bytes_written, count - bytes_written);
177 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
178 bytes_written += rc;
179 }
180 return true;
181}
182
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700183bool PWriteAll(int fd, const void* buf, size_t count, off_t offset) {
184 const char* c_buf = static_cast<const char*>(buf);
Gilad Arnold780db212012-07-11 13:12:49 -0700185 size_t bytes_written = 0;
186 int num_attempts = 0;
187 while (bytes_written < count) {
188 num_attempts++;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700189 ssize_t rc = pwrite(fd, c_buf + bytes_written, count - bytes_written,
190 offset + bytes_written);
Gilad Arnold780db212012-07-11 13:12:49 -0700191 // TODO(garnold) for debugging failure in chromium-os:31077; to be removed.
192 if (rc < 0) {
193 PLOG(ERROR) << "pwrite error; num_attempts=" << num_attempts
194 << " bytes_written=" << bytes_written
195 << " count=" << count << " offset=" << offset;
196 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700197 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
198 bytes_written += rc;
199 }
200 return true;
201}
202
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800203bool WriteAll(FileDescriptorPtr fd, const void* buf, size_t count) {
204 const char* c_buf = static_cast<const char*>(buf);
205 ssize_t bytes_written = 0;
206 while (bytes_written < static_cast<ssize_t>(count)) {
207 ssize_t rc = fd->Write(c_buf + bytes_written, count - bytes_written);
208 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
209 bytes_written += rc;
210 }
211 return true;
212}
213
214bool PWriteAll(FileDescriptorPtr fd,
215 const void* buf,
216 size_t count,
217 off_t offset) {
Allie Wood0c8cf7e2015-04-23 19:24:49 -0700218 TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(offset, SEEK_SET) !=
219 static_cast<off_t>(-1));
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800220 return WriteAll(fd, buf, count);
221}
222
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700223bool PReadAll(int fd, void* buf, size_t count, off_t offset,
224 ssize_t* out_bytes_read) {
225 char* c_buf = static_cast<char*>(buf);
226 ssize_t bytes_read = 0;
227 while (bytes_read < static_cast<ssize_t>(count)) {
228 ssize_t rc = pread(fd, c_buf + bytes_read, count - bytes_read,
229 offset + bytes_read);
230 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
231 if (rc == 0) {
232 break;
233 }
234 bytes_read += rc;
235 }
236 *out_bytes_read = bytes_read;
237 return true;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700238}
239
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800240bool PReadAll(FileDescriptorPtr fd, void* buf, size_t count, off_t offset,
241 ssize_t* out_bytes_read) {
Allie Wood0c8cf7e2015-04-23 19:24:49 -0700242 TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(offset, SEEK_SET) !=
243 static_cast<off_t>(-1));
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800244 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 = fd->Read(c_buf + bytes_read, count - bytes_read);
248 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
249 if (rc == 0) {
250 break;
251 }
252 bytes_read += rc;
253 }
254 *out_bytes_read = bytes_read;
255 return true;
256}
257
Gilad Arnold19a45f02012-07-19 12:36:10 -0700258// Append |nbytes| of content from |buf| to the vector pointed to by either
259// |vec_p| or |str_p|.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800260static void AppendBytes(const uint8_t* buf, size_t nbytes,
261 chromeos::Blob* vec_p) {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700262 CHECK(buf);
263 CHECK(vec_p);
264 vec_p->insert(vec_p->end(), buf, buf + nbytes);
265}
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800266static void AppendBytes(const uint8_t* buf, size_t nbytes,
Alex Deymof329b932014-10-30 01:37:48 -0700267 string* str_p) {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700268 CHECK(buf);
269 CHECK(str_p);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800270 str_p->append(buf, buf + nbytes);
Gilad Arnold19a45f02012-07-19 12:36:10 -0700271}
272
273// Reads from an open file |fp|, appending the read content to the container
274// pointer to by |out_p|. Returns true upon successful reading all of the
Darin Petkov8e447e02013-04-16 16:23:50 +0200275// file's content, false otherwise. If |size| is not -1, reads up to |size|
276// bytes.
Gilad Arnold19a45f02012-07-19 12:36:10 -0700277template <class T>
Darin Petkov8e447e02013-04-16 16:23:50 +0200278static bool Read(FILE* fp, off_t size, T* out_p) {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700279 CHECK(fp);
Darin Petkov8e447e02013-04-16 16:23:50 +0200280 CHECK(size == -1 || size >= 0);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800281 uint8_t buf[1024];
Darin Petkov8e447e02013-04-16 16:23:50 +0200282 while (size == -1 || size > 0) {
283 off_t bytes_to_read = sizeof(buf);
284 if (size > 0 && bytes_to_read > size) {
285 bytes_to_read = size;
286 }
287 size_t nbytes = fread(buf, 1, bytes_to_read, fp);
288 if (!nbytes) {
289 break;
290 }
Gilad Arnold19a45f02012-07-19 12:36:10 -0700291 AppendBytes(buf, nbytes, out_p);
Darin Petkov8e447e02013-04-16 16:23:50 +0200292 if (size != -1) {
293 CHECK(size >= static_cast<off_t>(nbytes));
294 size -= nbytes;
295 }
296 }
297 if (ferror(fp)) {
298 return false;
299 }
300 return size == 0 || feof(fp);
Gilad Arnold19a45f02012-07-19 12:36:10 -0700301}
302
Darin Petkov8e447e02013-04-16 16:23:50 +0200303// Opens a file |path| for reading and appends its the contents to a container
304// |out_p|. Starts reading the file from |offset|. If |offset| is beyond the end
305// of the file, returns success. If |size| is not -1, reads up to |size| bytes.
Gilad Arnold19a45f02012-07-19 12:36:10 -0700306template <class T>
Alex Deymof329b932014-10-30 01:37:48 -0700307static bool ReadFileChunkAndAppend(const string& path,
Darin Petkov8e447e02013-04-16 16:23:50 +0200308 off_t offset,
309 off_t size,
310 T* out_p) {
311 CHECK_GE(offset, 0);
312 CHECK(size == -1 || size >= 0);
Ben Chan736fcb52014-05-21 18:28:22 -0700313 base::ScopedFILE fp(fopen(path.c_str(), "r"));
Darin Petkov8e447e02013-04-16 16:23:50 +0200314 if (!fp.get())
adlr@google.com3defe6a2009-12-04 20:57:17 +0000315 return false;
Darin Petkov8e447e02013-04-16 16:23:50 +0200316 if (offset) {
317 // Return success without appending any data if a chunk beyond the end of
318 // the file is requested.
319 if (offset >= FileSize(path)) {
320 return true;
321 }
322 TEST_AND_RETURN_FALSE_ERRNO(fseek(fp.get(), offset, SEEK_SET) == 0);
323 }
324 return Read(fp.get(), size, out_p);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000325}
326
Alex Deymo10875d92014-11-10 21:52:57 -0800327// TODO(deymo): This is only used in unittest, but requires the private
328// Read<string>() defined here. Expose Read<string>() or move to base/ version.
329bool ReadPipe(const string& cmd, string* out_p) {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700330 FILE* fp = popen(cmd.c_str(), "r");
331 if (!fp)
adlr@google.com3defe6a2009-12-04 20:57:17 +0000332 return false;
Darin Petkov8e447e02013-04-16 16:23:50 +0200333 bool success = Read(fp, -1, out_p);
Gilad Arnold19a45f02012-07-19 12:36:10 -0700334 return (success && pclose(fp) >= 0);
335}
336
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800337bool ReadFile(const string& path, chromeos::Blob* out_p) {
Darin Petkov8e447e02013-04-16 16:23:50 +0200338 return ReadFileChunkAndAppend(path, 0, -1, out_p);
Gilad Arnold19a45f02012-07-19 12:36:10 -0700339}
340
Darin Petkov8e447e02013-04-16 16:23:50 +0200341bool ReadFile(const string& path, string* out_p) {
342 return ReadFileChunkAndAppend(path, 0, -1, out_p);
Gilad Arnold19a45f02012-07-19 12:36:10 -0700343}
344
Darin Petkov8e447e02013-04-16 16:23:50 +0200345bool ReadFileChunk(const string& path, off_t offset, off_t size,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800346 chromeos::Blob* out_p) {
Darin Petkov8e447e02013-04-16 16:23:50 +0200347 return ReadFileChunkAndAppend(path, offset, size, out_p);
348}
349
Gabe Blackb92cd2e2014-09-08 02:47:41 -0700350off_t BlockDevSize(int fd) {
351 uint64_t dev_size;
352 int rc = ioctl(fd, BLKGETSIZE64, &dev_size);
353 if (rc == -1) {
354 dev_size = -1;
355 PLOG(ERROR) << "Error running ioctl(BLKGETSIZE64) on " << fd;
356 }
357 return dev_size;
358}
359
360off_t BlockDevSize(const string& path) {
361 int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
362 if (fd == -1) {
363 PLOG(ERROR) << "Error opening " << path;
364 return fd;
365 }
366
367 off_t dev_size = BlockDevSize(fd);
368 if (dev_size == -1)
369 PLOG(ERROR) << "Error getting block device size on " << path;
370
371 close(fd);
372 return dev_size;
373}
374
375off_t FileSize(int fd) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700376 struct stat stbuf;
Gabe Blackb92cd2e2014-09-08 02:47:41 -0700377 int rc = fstat(fd, &stbuf);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700378 CHECK_EQ(rc, 0);
Gabe Blackb92cd2e2014-09-08 02:47:41 -0700379 if (rc < 0) {
380 PLOG(ERROR) << "Error stat-ing " << fd;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700381 return rc;
Gabe Blackb92cd2e2014-09-08 02:47:41 -0700382 }
383 if (S_ISREG(stbuf.st_mode))
384 return stbuf.st_size;
385 if (S_ISBLK(stbuf.st_mode))
386 return BlockDevSize(fd);
387 LOG(ERROR) << "Couldn't determine the type of " << fd;
388 return -1;
389}
390
391off_t FileSize(const string& path) {
392 int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
393 if (fd == -1) {
394 PLOG(ERROR) << "Error opening " << path;
395 return fd;
396 }
397 off_t size = FileSize(fd);
398 if (size == -1)
399 PLOG(ERROR) << "Error getting file size of " << path;
400 close(fd);
401 return size;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700402}
403
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800404void HexDumpArray(const uint8_t* const arr, const size_t length) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000405 LOG(INFO) << "Logging array of length: " << length;
406 const unsigned int bytes_per_line = 16;
Andrew de los Reyes08c4e272010-04-15 14:02:17 -0700407 for (uint32_t i = 0; i < length; i += bytes_per_line) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000408 const unsigned int bytes_remaining = length - i;
409 const unsigned int bytes_per_this_line = min(bytes_per_line,
410 bytes_remaining);
411 char header[100];
412 int r = snprintf(header, sizeof(header), "0x%08x : ", i);
413 TEST_AND_RETURN(r == 13);
414 string line = header;
415 for (unsigned int j = 0; j < bytes_per_this_line; j++) {
416 char buf[20];
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800417 uint8_t c = arr[i + j];
adlr@google.com3defe6a2009-12-04 20:57:17 +0000418 r = snprintf(buf, sizeof(buf), "%02x ", static_cast<unsigned int>(c));
419 TEST_AND_RETURN(r == 3);
420 line += buf;
421 }
422 LOG(INFO) << line;
423 }
424}
425
Alex Deymof329b932014-10-30 01:37:48 -0700426bool SplitPartitionName(const string& partition_name,
427 string* out_disk_name,
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800428 int* out_partition_num) {
Alex Vakulenko6a9d3492015-06-15 12:53:22 -0700429 if (!base::StartsWithASCII(partition_name, "/dev/", true)) {
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800430 LOG(ERROR) << "Invalid partition device name: " << partition_name;
431 return false;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700432 }
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800433
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700434 size_t last_nondigit_pos = partition_name.find_last_not_of("0123456789");
435 if (last_nondigit_pos == string::npos ||
436 (last_nondigit_pos + 1) == partition_name.size()) {
437 LOG(ERROR) << "Unable to parse partition device name: " << partition_name;
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800438 return false;
439 }
440
Alex Deymof329b932014-10-30 01:37:48 -0700441 size_t partition_name_len = string::npos;
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700442 if (partition_name[last_nondigit_pos] == '_') {
443 // NAND block devices have weird naming which could be something
444 // like "/dev/ubiblock2_0". We discard "_0" in such a case.
445 size_t prev_nondigit_pos =
446 partition_name.find_last_not_of("0123456789", last_nondigit_pos - 1);
447 if (prev_nondigit_pos == string::npos ||
448 (prev_nondigit_pos + 1) == last_nondigit_pos) {
449 LOG(ERROR) << "Unable to parse partition device name: " << partition_name;
450 return false;
451 }
452
453 partition_name_len = last_nondigit_pos - prev_nondigit_pos;
454 last_nondigit_pos = prev_nondigit_pos;
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800455 }
456
457 if (out_disk_name) {
458 // Special case for MMC devices which have the following naming scheme:
459 // mmcblk0p2
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700460 size_t disk_name_len = last_nondigit_pos;
461 if (partition_name[last_nondigit_pos] != 'p' ||
462 last_nondigit_pos == 0 ||
463 !isdigit(partition_name[last_nondigit_pos - 1])) {
464 disk_name_len++;
465 }
466 *out_disk_name = partition_name.substr(0, disk_name_len);
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800467 }
468
469 if (out_partition_num) {
Alex Deymof329b932014-10-30 01:37:48 -0700470 string partition_str = partition_name.substr(last_nondigit_pos + 1,
471 partition_name_len);
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800472 *out_partition_num = atoi(partition_str.c_str());
473 }
474 return true;
475}
476
Alex Deymof329b932014-10-30 01:37:48 -0700477string MakePartitionName(const string& disk_name, int partition_num) {
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800478 if (partition_num < 1) {
479 LOG(ERROR) << "Invalid partition number: " << partition_num;
480 return string();
481 }
482
Alex Vakulenko6a9d3492015-06-15 12:53:22 -0700483 if (!base::StartsWithASCII(disk_name, "/dev/", true)) {
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800484 LOG(ERROR) << "Invalid disk name: " << disk_name;
Alex Deymof329b932014-10-30 01:37:48 -0700485 return string();
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800486 }
487
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800488 if (IsMtdDeviceName(disk_name)) {
489 // Special case for UBI block devices.
490 // 1. ubiblock is not writable, we need to use plain "ubi".
491 // 2. There is a "_0" suffix.
492 return MakeNandPartitionName(partition_num);
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800493 }
494
Alex Deymof329b932014-10-30 01:37:48 -0700495 string partition_name = disk_name;
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700496 if (isdigit(partition_name.back())) {
497 // Special case for devices with names ending with a digit.
498 // Add "p" to separate the disk name from partition number,
499 // e.g. "/dev/loop0p2"
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800500 partition_name += 'p';
501 }
502
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700503 partition_name += std::to_string(partition_num);
504
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800505 return partition_name;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700506}
507
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800508string MakePartitionNameForMount(const string& part_name) {
509 if (IsMtdDeviceName(part_name)) {
510 int partition_num;
511 if (!SplitPartitionName(part_name, nullptr, &partition_num)) {
512 return "";
513 }
514 return MakeNandPartitionNameForMount(partition_num);
515 }
516 return part_name;
517}
518
Alex Deymof329b932014-10-30 01:37:48 -0700519string ErrnoNumberAsString(int err) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000520 char buf[100];
521 buf[0] = '\0';
522 return strerror_r(err, buf, sizeof(buf));
523}
524
adlr@google.com3defe6a2009-12-04 20:57:17 +0000525bool FileExists(const char* path) {
526 struct stat stbuf;
527 return 0 == lstat(path, &stbuf);
528}
529
Darin Petkov30291ed2010-11-12 10:23:06 -0800530bool IsSymlink(const char* path) {
531 struct stat stbuf;
532 return lstat(path, &stbuf) == 0 && S_ISLNK(stbuf.st_mode) != 0;
533}
534
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800535bool TryAttachingUbiVolume(int volume_num, int timeout) {
536 const string volume_path = base::StringPrintf("/dev/ubi%d_0", volume_num);
537 if (FileExists(volume_path.c_str())) {
538 return true;
539 }
540
541 int exit_code;
542 vector<string> cmd = {
543 "ubiattach",
544 "-m",
545 base::StringPrintf("%d", volume_num),
546 "-d",
547 base::StringPrintf("%d", volume_num)
548 };
549 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &exit_code, nullptr));
550 TEST_AND_RETURN_FALSE(exit_code == 0);
551
552 cmd = {
553 "ubiblock",
554 "--create",
555 volume_path
556 };
557 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &exit_code, nullptr));
558 TEST_AND_RETURN_FALSE(exit_code == 0);
559
560 while (timeout > 0 && !FileExists(volume_path.c_str())) {
561 sleep(1);
562 timeout--;
563 }
564
565 return FileExists(volume_path.c_str());
566}
567
Gilad Arnoldd04f8e22014-01-09 13:13:40 -0800568// If |path| is absolute, or explicit relative to the current working directory,
569// leaves it as is. Otherwise, if TMPDIR is defined in the environment and is
570// non-empty, prepends it to |path|. Otherwise, prepends /tmp. Returns the
571// resulting path.
572static const string PrependTmpdir(const string& path) {
Alex Vakulenko6a9d3492015-06-15 12:53:22 -0700573 if (path[0] == '/' || base::StartsWithASCII(path, "./", true) ||
574 base::StartsWithASCII(path, "../", true))
Gilad Arnoldd04f8e22014-01-09 13:13:40 -0800575 return path;
576
577 const char *tmpdir = getenv("TMPDIR");
578 const string prefix = (tmpdir && *tmpdir ? tmpdir : "/tmp");
579 return prefix + "/" + path;
580}
581
Alex Deymof329b932014-10-30 01:37:48 -0700582bool MakeTempFile(const string& base_filename_template,
583 string* filename,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700584 int* fd) {
Gilad Arnoldd04f8e22014-01-09 13:13:40 -0800585 const string filename_template = PrependTmpdir(base_filename_template);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700586 DCHECK(filename || fd);
587 vector<char> buf(filename_template.size() + 1);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800588 memcpy(buf.data(), filename_template.data(), filename_template.size());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700589 buf[filename_template.size()] = '\0';
Darin Petkov296889c2010-07-23 16:20:54 -0700590
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800591 int mkstemp_fd = mkstemp(buf.data());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700592 TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0);
593 if (filename) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800594 *filename = buf.data();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700595 }
596 if (fd) {
597 *fd = mkstemp_fd;
598 } else {
599 close(mkstemp_fd);
600 }
601 return true;
602}
603
Alex Deymof329b932014-10-30 01:37:48 -0700604bool MakeTempDirectory(const string& base_dirname_template,
605 string* dirname) {
Gilad Arnoldd04f8e22014-01-09 13:13:40 -0800606 const string dirname_template = PrependTmpdir(base_dirname_template);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700607 DCHECK(dirname);
608 vector<char> buf(dirname_template.size() + 1);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800609 memcpy(buf.data(), dirname_template.data(), dirname_template.size());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700610 buf[dirname_template.size()] = '\0';
Darin Petkov296889c2010-07-23 16:20:54 -0700611
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800612 char* return_code = mkdtemp(buf.data());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700613 TEST_AND_RETURN_FALSE_ERRNO(return_code != nullptr);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800614 *dirname = buf.data();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700615 return true;
616}
617
adlr@google.com3defe6a2009-12-04 20:57:17 +0000618bool MountFilesystem(const string& device,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700619 const string& mountpoint,
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700620 unsigned long mountflags) { // NOLINT(runtime/int)
Alex Deymo4c82df32014-11-10 22:25:57 -0800621 // TODO(sosa): Remove "ext3" once crbug.com/208022 is resolved.
622 const vector<const char*> fstypes{"ext2", "ext3", "squashfs"};
623 for (const char* fstype : fstypes) {
624 int rc = mount(device.c_str(), mountpoint.c_str(), fstype, mountflags,
625 nullptr);
626 if (rc == 0)
627 return true;
628
629 PLOG(WARNING) << "Unable to mount destination device " << device
630 << " on " << mountpoint << " as " << fstype;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000631 }
Alex Deymo4c82df32014-11-10 22:25:57 -0800632 LOG(ERROR) << "Unable to mount " << device << " with any supported type";
633 return false;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000634}
635
636bool UnmountFilesystem(const string& mountpoint) {
Ben Chan77a1eba2012-10-07 22:54:55 -0700637 for (int num_retries = 0; ; ++num_retries) {
638 if (umount(mountpoint.c_str()) == 0)
639 break;
640
641 TEST_AND_RETURN_FALSE_ERRNO(errno == EBUSY &&
642 num_retries < kUnmountMaxNumOfRetries);
Alex Deymo8d2bbe32015-06-23 16:28:59 -0700643 usleep(kUnmountRetryIntervalInMicroseconds);
Ben Chan77a1eba2012-10-07 22:54:55 -0700644 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000645 return true;
646}
647
Alex Deymof329b932014-10-30 01:37:48 -0700648bool GetFilesystemSize(const string& device,
Darin Petkovd3f8c892010-10-12 21:38:45 -0700649 int* out_block_count,
650 int* out_block_size) {
651 int fd = HANDLE_EINTR(open(device.c_str(), O_RDONLY));
Alex Deymoe7141c62014-10-17 14:03:01 -0700652 TEST_AND_RETURN_FALSE_ERRNO(fd >= 0);
Darin Petkovd3f8c892010-10-12 21:38:45 -0700653 ScopedFdCloser fd_closer(&fd);
654 return GetFilesystemSizeFromFD(fd, out_block_count, out_block_size);
655}
656
657bool GetFilesystemSizeFromFD(int fd,
658 int* out_block_count,
659 int* out_block_size) {
660 TEST_AND_RETURN_FALSE(fd >= 0);
661
Alex Deymo192393b2014-11-10 15:58:38 -0800662 // Determine the filesystem size by directly reading the block count and
663 // block size information from the superblock. Supported FS are ext3 and
664 // squashfs.
665
666 // Read from the fd only once and detect in memory. The first 2 KiB is enough
667 // to read the ext2 superblock (located at offset 1024) and the squashfs
668 // superblock (located at offset 0).
669 const ssize_t kBufferSize = 2048;
670
671 uint8_t buffer[kBufferSize];
672 if (HANDLE_EINTR(pread(fd, buffer, kBufferSize, 0)) != kBufferSize) {
673 PLOG(ERROR) << "Unable to read the file system header:";
Darin Petkovd3f8c892010-10-12 21:38:45 -0700674 return false;
675 }
Alex Deymo192393b2014-11-10 15:58:38 -0800676
677 if (GetSquashfs4Size(buffer, kBufferSize, out_block_count, out_block_size))
678 return true;
679 if (GetExt3Size(buffer, kBufferSize, out_block_count, out_block_size))
680 return true;
681
682 LOG(ERROR) << "Unable to determine file system type.";
683 return false;
684}
685
686bool GetExt3Size(const uint8_t* buffer, size_t buffer_size,
687 int* out_block_count,
688 int* out_block_size) {
689 // See include/linux/ext2_fs.h for more details on the structure. We obtain
690 // ext2 constants from ext2fs/ext2fs.h header but we don't link with the
691 // library.
692 if (buffer_size < SUPERBLOCK_OFFSET + SUPERBLOCK_SIZE)
693 return false;
694
695 const uint8_t* superblock = buffer + SUPERBLOCK_OFFSET;
696
697 // ext3_fs.h: ext3_super_block.s_blocks_count
698 uint32_t block_count =
699 *reinterpret_cast<const uint32_t*>(superblock + 1 * sizeof(int32_t));
700
701 // ext3_fs.h: ext3_super_block.s_log_block_size
702 uint32_t log_block_size =
703 *reinterpret_cast<const uint32_t*>(superblock + 6 * sizeof(int32_t));
704
705 // ext3_fs.h: ext3_super_block.s_magic
706 uint16_t magic =
707 *reinterpret_cast<const uint16_t*>(superblock + 14 * sizeof(int32_t));
708
Darin Petkovd3f8c892010-10-12 21:38:45 -0700709 block_count = le32toh(block_count);
Alex Deymo192393b2014-11-10 15:58:38 -0800710 log_block_size = le32toh(log_block_size) + EXT2_MIN_BLOCK_LOG_SIZE;
Darin Petkovd3f8c892010-10-12 21:38:45 -0700711 magic = le16toh(magic);
712
713 // Sanity check the parameters.
Alex Deymo192393b2014-11-10 15:58:38 -0800714 TEST_AND_RETURN_FALSE(magic == EXT2_SUPER_MAGIC);
715 TEST_AND_RETURN_FALSE(log_block_size >= EXT2_MIN_BLOCK_LOG_SIZE &&
716 log_block_size <= EXT2_MAX_BLOCK_LOG_SIZE);
Darin Petkovd3f8c892010-10-12 21:38:45 -0700717 TEST_AND_RETURN_FALSE(block_count > 0);
718
Alex Deymo192393b2014-11-10 15:58:38 -0800719 if (out_block_count)
Darin Petkovd3f8c892010-10-12 21:38:45 -0700720 *out_block_count = block_count;
Alex Deymo192393b2014-11-10 15:58:38 -0800721 if (out_block_size)
722 *out_block_size = 1 << log_block_size;
723 return true;
724}
725
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800726bool GetSquashfs4Size(const uint8_t* buffer, size_t buffer_size,
Alex Deymo192393b2014-11-10 15:58:38 -0800727 int* out_block_count,
728 int* out_block_size) {
729 // See fs/squashfs/squashfs_fs.h for format details. We only support
730 // Squashfs 4.x little endian.
731
732 // sizeof(struct squashfs_super_block)
733 const size_t kSquashfsSuperBlockSize = 96;
734 if (buffer_size < kSquashfsSuperBlockSize)
735 return false;
736
737 // Check magic, squashfs_fs.h: SQUASHFS_MAGIC
738 if (memcmp(buffer, "hsqs", 4) != 0)
739 return false; // Only little endian is supported.
740
741 // squashfs_fs.h: struct squashfs_super_block.s_major
742 uint16_t s_major = *reinterpret_cast<const uint16_t*>(
743 buffer + 5 * sizeof(uint32_t) + 4 * sizeof(uint16_t));
744
745 if (s_major != 4) {
746 LOG(ERROR) << "Found unsupported squashfs major version " << s_major;
747 return false;
Darin Petkovd3f8c892010-10-12 21:38:45 -0700748 }
Alex Deymo192393b2014-11-10 15:58:38 -0800749
750 // squashfs_fs.h: struct squashfs_super_block.bytes_used
751 uint64_t bytes_used = *reinterpret_cast<const int64_t*>(
752 buffer + 5 * sizeof(uint32_t) + 6 * sizeof(uint16_t) + sizeof(uint64_t));
753
754 const int block_size = 4096;
755
756 // The squashfs' bytes_used doesn't need to be aligned with the block boundary
757 // so we round up to the nearest blocksize.
758 if (out_block_count)
759 *out_block_count = (bytes_used + block_size - 1) / block_size;
760 if (out_block_size)
Darin Petkovd3f8c892010-10-12 21:38:45 -0700761 *out_block_size = block_size;
Darin Petkovd3f8c892010-10-12 21:38:45 -0700762 return true;
763}
764
Alex Deymo60ca1a72015-06-18 18:19:15 -0700765bool IsExtFilesystem(const string& device) {
Alex Deymode942f32015-03-13 11:57:15 -0700766 chromeos::Blob header;
767 // The first 2 KiB is enough to read the ext2 superblock (located at offset
768 // 1024).
769 if (!ReadFileChunk(device, 0, 2048, &header))
770 return false;
771 return GetExt3Size(header.data(), header.size(), nullptr, nullptr);
772}
773
Alex Deymo60ca1a72015-06-18 18:19:15 -0700774bool IsSquashfsFilesystem(const string& device) {
Alex Deymode942f32015-03-13 11:57:15 -0700775 chromeos::Blob header;
776 // The first 96 is enough to read the squashfs superblock.
777 const ssize_t kSquashfsSuperBlockSize = 96;
778 if (!ReadFileChunk(device, 0, kSquashfsSuperBlockSize, &header))
779 return false;
780 return GetSquashfs4Size(header.data(), header.size(), nullptr, nullptr);
781}
782
Alex Deymo032e7722014-03-25 17:53:56 -0700783// Tries to parse the header of an ELF file to obtain a human-readable
784// description of it on the |output| string.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800785static bool GetFileFormatELF(const uint8_t* buffer, size_t size,
786 string* output) {
Alex Deymo032e7722014-03-25 17:53:56 -0700787 // 0x00: EI_MAG - ELF magic header, 4 bytes.
Alex Deymoc1711e22014-08-08 13:16:23 -0700788 if (size < SELFMAG || memcmp(buffer, ELFMAG, SELFMAG) != 0)
Alex Deymo032e7722014-03-25 17:53:56 -0700789 return false;
790 *output = "ELF";
791
792 // 0x04: EI_CLASS, 1 byte.
Alex Deymoc1711e22014-08-08 13:16:23 -0700793 if (size < EI_CLASS + 1)
Alex Deymo032e7722014-03-25 17:53:56 -0700794 return true;
Alex Deymoc1711e22014-08-08 13:16:23 -0700795 switch (buffer[EI_CLASS]) {
796 case ELFCLASS32:
Alex Deymo032e7722014-03-25 17:53:56 -0700797 *output += " 32-bit";
798 break;
Alex Deymoc1711e22014-08-08 13:16:23 -0700799 case ELFCLASS64:
Alex Deymo032e7722014-03-25 17:53:56 -0700800 *output += " 64-bit";
801 break;
802 default:
803 *output += " ?-bit";
804 }
805
806 // 0x05: EI_DATA, endianness, 1 byte.
Alex Deymoc1711e22014-08-08 13:16:23 -0700807 if (size < EI_DATA + 1)
Alex Deymo032e7722014-03-25 17:53:56 -0700808 return true;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800809 uint8_t ei_data = buffer[EI_DATA];
Alex Deymo032e7722014-03-25 17:53:56 -0700810 switch (ei_data) {
Alex Deymoc1711e22014-08-08 13:16:23 -0700811 case ELFDATA2LSB:
Alex Deymo032e7722014-03-25 17:53:56 -0700812 *output += " little-endian";
813 break;
Alex Deymoc1711e22014-08-08 13:16:23 -0700814 case ELFDATA2MSB:
Alex Deymo032e7722014-03-25 17:53:56 -0700815 *output += " big-endian";
816 break;
817 default:
818 *output += " ?-endian";
819 // Don't parse anything after the 0x10 offset if endianness is unknown.
820 return true;
821 }
822
Alex Deymoc1711e22014-08-08 13:16:23 -0700823 const Elf32_Ehdr* hdr = reinterpret_cast<const Elf32_Ehdr*>(buffer);
824 // 0x12: e_machine, 2 byte endianness based on ei_data. The position (0x12)
825 // and size is the same for both 32 and 64 bits.
826 if (size < offsetof(Elf32_Ehdr, e_machine) + sizeof(hdr->e_machine))
Alex Deymo032e7722014-03-25 17:53:56 -0700827 return true;
Alex Deymoc1711e22014-08-08 13:16:23 -0700828 uint16_t e_machine;
Alex Deymo032e7722014-03-25 17:53:56 -0700829 // Fix endianess regardless of the host endianess.
Alex Deymoc1711e22014-08-08 13:16:23 -0700830 if (ei_data == ELFDATA2LSB)
831 e_machine = le16toh(hdr->e_machine);
Alex Deymo032e7722014-03-25 17:53:56 -0700832 else
Alex Deymoc1711e22014-08-08 13:16:23 -0700833 e_machine = be16toh(hdr->e_machine);
Alex Deymo032e7722014-03-25 17:53:56 -0700834
835 switch (e_machine) {
Alex Deymoc1711e22014-08-08 13:16:23 -0700836 case EM_386:
Alex Deymo032e7722014-03-25 17:53:56 -0700837 *output += " x86";
838 break;
Alex Deymoc1711e22014-08-08 13:16:23 -0700839 case EM_MIPS:
840 *output += " mips";
841 break;
842 case EM_ARM:
Alex Deymo032e7722014-03-25 17:53:56 -0700843 *output += " arm";
844 break;
Alex Deymoc1711e22014-08-08 13:16:23 -0700845 case EM_X86_64:
Alex Deymo032e7722014-03-25 17:53:56 -0700846 *output += " x86-64";
847 break;
848 default:
849 *output += " unknown-arch";
850 }
851 return true;
852}
853
854string GetFileFormat(const string& path) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800855 chromeos::Blob buffer;
Alex Deymo032e7722014-03-25 17:53:56 -0700856 if (!ReadFileChunkAndAppend(path, 0, kGetFileFormatMaxHeaderSize, &buffer))
857 return "File not found.";
858
859 string result;
860 if (GetFileFormatELF(buffer.data(), buffer.size(), &result))
861 return result;
862
863 return "data";
864}
865
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800866namespace {
867// Do the actual trigger. We do it as a main-loop callback to (try to) get a
868// consistent stack trace.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700869void TriggerCrashReporterUpload() {
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800870 pid_t pid = fork();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700871 CHECK_GE(pid, 0) << "fork failed"; // fork() failed. Something is very wrong.
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800872 if (pid == 0) {
873 // We are the child. Crash.
874 abort(); // never returns
875 }
876 // We are the parent. Wait for child to terminate.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700877 pid_t result = waitpid(pid, nullptr, 0);
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800878 LOG_IF(ERROR, result < 0) << "waitpid() failed";
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800879}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700880} // namespace
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800881
882void ScheduleCrashReporterUpload() {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700883 chromeos::MessageLoop::current()->PostTask(
884 FROM_HERE,
885 base::Bind(&TriggerCrashReporterUpload));
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800886}
887
Chris Sosa4f8ee272012-11-30 13:01:54 -0800888bool SetCpuShares(CpuShares shares) {
889 string string_shares = base::IntToString(static_cast<int>(shares));
890 string cpu_shares_file = string(utils::kCGroupDir) + "/cpu.shares";
891 LOG(INFO) << "Setting cgroup cpu shares to " << string_shares;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700892 if (utils::WriteFile(cpu_shares_file.c_str(), string_shares.c_str(),
893 string_shares.size())) {
Chris Sosa4f8ee272012-11-30 13:01:54 -0800894 return true;
895 } else {
896 LOG(ERROR) << "Failed to change cgroup cpu shares to "<< string_shares
897 << " using " << cpu_shares_file;
898 return false;
899 }
Darin Petkovc6c135c2010-08-11 13:36:18 -0700900}
901
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700902int FuzzInt(int value, unsigned int range) {
903 int min = value - range / 2;
904 int max = value + range - range / 2;
905 return base::RandInt(min, max);
906}
907
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700908string FormatSecs(unsigned secs) {
Gilad Arnold8e3f1262013-01-08 14:59:54 -0800909 return FormatTimeDelta(TimeDelta::FromSeconds(secs));
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700910}
911
Gilad Arnold8e3f1262013-01-08 14:59:54 -0800912string FormatTimeDelta(TimeDelta delta) {
David Zeuthen973449e2014-08-18 16:18:23 -0400913 string str;
914
915 // Handle negative durations by prefixing with a minus.
916 if (delta.ToInternalValue() < 0) {
917 delta *= -1;
918 str = "-";
919 }
920
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700921 // Canonicalize into days, hours, minutes, seconds and microseconds.
922 unsigned days = delta.InDays();
Gilad Arnold8e3f1262013-01-08 14:59:54 -0800923 delta -= TimeDelta::FromDays(days);
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700924 unsigned hours = delta.InHours();
Gilad Arnold8e3f1262013-01-08 14:59:54 -0800925 delta -= TimeDelta::FromHours(hours);
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700926 unsigned mins = delta.InMinutes();
Gilad Arnold8e3f1262013-01-08 14:59:54 -0800927 delta -= TimeDelta::FromMinutes(mins);
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700928 unsigned secs = delta.InSeconds();
Gilad Arnold8e3f1262013-01-08 14:59:54 -0800929 delta -= TimeDelta::FromSeconds(secs);
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700930 unsigned usecs = delta.InMicroseconds();
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800931
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700932 if (days)
933 base::StringAppendF(&str, "%ud", days);
934 if (days || hours)
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800935 base::StringAppendF(&str, "%uh", hours);
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700936 if (days || hours || mins)
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800937 base::StringAppendF(&str, "%um", mins);
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700938 base::StringAppendF(&str, "%u", secs);
939 if (usecs) {
940 int width = 6;
941 while ((usecs / 10) * 10 == usecs) {
942 usecs /= 10;
943 width--;
944 }
945 base::StringAppendF(&str, ".%0*u", width, usecs);
946 }
947 base::StringAppendF(&str, "s");
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800948 return str;
949}
950
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700951string ToString(const Time utc_time) {
952 Time::Exploded exp_time;
953 utc_time.UTCExplode(&exp_time);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700954 return base::StringPrintf("%d/%d/%d %d:%02d:%02d GMT",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700955 exp_time.month,
956 exp_time.day_of_month,
957 exp_time.year,
958 exp_time.hour,
959 exp_time.minute,
960 exp_time.second);
961}
adlr@google.com3defe6a2009-12-04 20:57:17 +0000962
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700963string ToString(bool b) {
964 return (b ? "true" : "false");
965}
966
Alex Deymo1c656c42013-06-28 11:02:14 -0700967string ToString(DownloadSource source) {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700968 switch (source) {
969 case kDownloadSourceHttpsServer: return "HttpsServer";
970 case kDownloadSourceHttpServer: return "HttpServer";
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700971 case kDownloadSourceHttpPeer: return "HttpPeer";
Jay Srinivasan19409b72013-04-12 19:23:36 -0700972 case kNumDownloadSources: return "Unknown";
973 // Don't add a default case to let the compiler warn about newly added
974 // download sources which should be added here.
975 }
976
977 return "Unknown";
978}
979
Alex Deymo1c656c42013-06-28 11:02:14 -0700980string ToString(PayloadType payload_type) {
981 switch (payload_type) {
982 case kPayloadTypeDelta: return "Delta";
983 case kPayloadTypeFull: return "Full";
984 case kPayloadTypeForcedFull: return "ForcedFull";
985 case kNumPayloadTypes: return "Unknown";
986 // Don't add a default case to let the compiler warn about newly added
987 // payload types which should be added here.
988 }
989
990 return "Unknown";
991}
992
David Zeuthena99981f2013-04-29 13:42:47 -0700993ErrorCode GetBaseErrorCode(ErrorCode code) {
Jay Srinivasanf0572052012-10-23 18:12:56 -0700994 // Ignore the higher order bits in the code by applying the mask as
995 // we want the enumerations to be in the small contiguous range
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700996 // with values less than ErrorCode::kUmaReportedMax.
997 ErrorCode base_code = static_cast<ErrorCode>(
998 static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags));
Jay Srinivasanf0572052012-10-23 18:12:56 -0700999
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -08001000 // Make additional adjustments required for UMA and error classification.
1001 // TODO(jaysri): Move this logic to UeErrorCode.cc when we fix
1002 // chromium-os:34369.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001003 if (base_code >= ErrorCode::kOmahaRequestHTTPResponseBase) {
Jay Srinivasanf0572052012-10-23 18:12:56 -07001004 // Since we want to keep the enums to a small value, aggregate all HTTP
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -08001005 // errors into this one bucket for UMA and error classification purposes.
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001006 LOG(INFO) << "Converting error code " << base_code
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001007 << " to ErrorCode::kOmahaErrorInHTTPResponse";
1008 base_code = ErrorCode::kOmahaErrorInHTTPResponse;
Jay Srinivasanf0572052012-10-23 18:12:56 -07001009 }
1010
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -08001011 return base_code;
1012}
1013
David Zeuthen33bae492014-02-25 16:16:18 -08001014metrics::AttemptResult GetAttemptResult(ErrorCode code) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001015 ErrorCode base_code = static_cast<ErrorCode>(
1016 static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags));
David Zeuthen33bae492014-02-25 16:16:18 -08001017
1018 switch (base_code) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001019 case ErrorCode::kSuccess:
David Zeuthen33bae492014-02-25 16:16:18 -08001020 return metrics::AttemptResult::kUpdateSucceeded;
1021
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001022 case ErrorCode::kDownloadTransferError:
David Zeuthen33bae492014-02-25 16:16:18 -08001023 return metrics::AttemptResult::kPayloadDownloadError;
1024
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001025 case ErrorCode::kDownloadInvalidMetadataSize:
1026 case ErrorCode::kDownloadInvalidMetadataMagicString:
1027 case ErrorCode::kDownloadMetadataSignatureError:
1028 case ErrorCode::kDownloadMetadataSignatureVerificationError:
1029 case ErrorCode::kPayloadMismatchedType:
1030 case ErrorCode::kUnsupportedMajorPayloadVersion:
1031 case ErrorCode::kUnsupportedMinorPayloadVersion:
1032 case ErrorCode::kDownloadNewPartitionInfoError:
1033 case ErrorCode::kDownloadSignatureMissingInManifest:
1034 case ErrorCode::kDownloadManifestParseError:
1035 case ErrorCode::kDownloadOperationHashMissingError:
David Zeuthen33bae492014-02-25 16:16:18 -08001036 return metrics::AttemptResult::kMetadataMalformed;
1037
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001038 case ErrorCode::kDownloadOperationHashMismatch:
1039 case ErrorCode::kDownloadOperationHashVerificationError:
David Zeuthen33bae492014-02-25 16:16:18 -08001040 return metrics::AttemptResult::kOperationMalformed;
1041
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001042 case ErrorCode::kDownloadOperationExecutionError:
1043 case ErrorCode::kInstallDeviceOpenError:
1044 case ErrorCode::kKernelDeviceOpenError:
1045 case ErrorCode::kDownloadWriteError:
1046 case ErrorCode::kFilesystemCopierError:
Allie Woodeb9e6d82015-04-17 13:55:30 -07001047 case ErrorCode::kFilesystemVerifierError:
David Zeuthen33bae492014-02-25 16:16:18 -08001048 return metrics::AttemptResult::kOperationExecutionError;
1049
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001050 case ErrorCode::kDownloadMetadataSignatureMismatch:
David Zeuthen33bae492014-02-25 16:16:18 -08001051 return metrics::AttemptResult::kMetadataVerificationFailed;
1052
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001053 case ErrorCode::kPayloadSizeMismatchError:
1054 case ErrorCode::kPayloadHashMismatchError:
1055 case ErrorCode::kDownloadPayloadVerificationError:
1056 case ErrorCode::kSignedDeltaPayloadExpectedError:
1057 case ErrorCode::kDownloadPayloadPubKeyVerificationError:
David Zeuthen33bae492014-02-25 16:16:18 -08001058 return metrics::AttemptResult::kPayloadVerificationFailed;
1059
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001060 case ErrorCode::kNewRootfsVerificationError:
1061 case ErrorCode::kNewKernelVerificationError:
David Zeuthen33bae492014-02-25 16:16:18 -08001062 return metrics::AttemptResult::kVerificationFailed;
1063
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001064 case ErrorCode::kPostinstallRunnerError:
1065 case ErrorCode::kPostinstallBootedFromFirmwareB:
1066 case ErrorCode::kPostinstallFirmwareRONotUpdatable:
David Zeuthen33bae492014-02-25 16:16:18 -08001067 return metrics::AttemptResult::kPostInstallFailed;
1068
1069 // We should never get these errors in the update-attempt stage so
1070 // return internal error if this happens.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001071 case ErrorCode::kError:
1072 case ErrorCode::kOmahaRequestXMLParseError:
1073 case ErrorCode::kOmahaRequestError:
1074 case ErrorCode::kOmahaResponseHandlerError:
1075 case ErrorCode::kDownloadStateInitializationError:
1076 case ErrorCode::kOmahaRequestEmptyResponseError:
1077 case ErrorCode::kDownloadInvalidMetadataSignature:
1078 case ErrorCode::kOmahaResponseInvalid:
1079 case ErrorCode::kOmahaUpdateIgnoredPerPolicy:
1080 case ErrorCode::kOmahaUpdateDeferredPerPolicy:
1081 case ErrorCode::kOmahaErrorInHTTPResponse:
1082 case ErrorCode::kDownloadMetadataSignatureMissingError:
1083 case ErrorCode::kOmahaUpdateDeferredForBackoff:
1084 case ErrorCode::kPostinstallPowerwashError:
1085 case ErrorCode::kUpdateCanceledByChannelChange:
David Zeuthenf3e28012014-08-26 18:23:52 -04001086 case ErrorCode::kOmahaRequestXMLHasEntityDecl:
David Zeuthen33bae492014-02-25 16:16:18 -08001087 return metrics::AttemptResult::kInternalError;
1088
1089 // Special flags. These can't happen (we mask them out above) but
1090 // the compiler doesn't know that. Just break out so we can warn and
1091 // return |kInternalError|.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001092 case ErrorCode::kUmaReportedMax:
1093 case ErrorCode::kOmahaRequestHTTPResponseBase:
1094 case ErrorCode::kDevModeFlag:
1095 case ErrorCode::kResumedFlag:
1096 case ErrorCode::kTestImageFlag:
1097 case ErrorCode::kTestOmahaUrlFlag:
1098 case ErrorCode::kSpecialFlags:
David Zeuthen33bae492014-02-25 16:16:18 -08001099 break;
1100 }
1101
1102 LOG(ERROR) << "Unexpected error code " << base_code;
1103 return metrics::AttemptResult::kInternalError;
1104}
1105
1106
1107metrics::DownloadErrorCode GetDownloadErrorCode(ErrorCode code) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001108 ErrorCode base_code = static_cast<ErrorCode>(
1109 static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags));
David Zeuthen33bae492014-02-25 16:16:18 -08001110
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001111 if (base_code >= ErrorCode::kOmahaRequestHTTPResponseBase) {
1112 int http_status =
1113 static_cast<int>(base_code) -
1114 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase);
David Zeuthen33bae492014-02-25 16:16:18 -08001115 if (http_status >= 200 && http_status <= 599) {
1116 return static_cast<metrics::DownloadErrorCode>(
1117 static_cast<int>(metrics::DownloadErrorCode::kHttpStatus200) +
1118 http_status - 200);
1119 } else if (http_status == 0) {
1120 // The code is using HTTP Status 0 for "Unable to get http
1121 // response code."
1122 return metrics::DownloadErrorCode::kDownloadError;
1123 }
1124 LOG(WARNING) << "Unexpected HTTP status code " << http_status;
1125 return metrics::DownloadErrorCode::kHttpStatusOther;
1126 }
1127
1128 switch (base_code) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001129 // Unfortunately, ErrorCode::kDownloadTransferError is returned for a wide
David Zeuthen33bae492014-02-25 16:16:18 -08001130 // variety of errors (proxy errors, host not reachable, timeouts etc.).
1131 //
1132 // For now just map that to kDownloading. See http://crbug.com/355745
1133 // for how we plan to add more detail in the future.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001134 case ErrorCode::kDownloadTransferError:
David Zeuthen33bae492014-02-25 16:16:18 -08001135 return metrics::DownloadErrorCode::kDownloadError;
1136
1137 // All of these error codes are not related to downloading so break
1138 // out so we can warn and return InputMalformed.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001139 case ErrorCode::kSuccess:
1140 case ErrorCode::kError:
1141 case ErrorCode::kOmahaRequestError:
1142 case ErrorCode::kOmahaResponseHandlerError:
1143 case ErrorCode::kFilesystemCopierError:
1144 case ErrorCode::kPostinstallRunnerError:
1145 case ErrorCode::kPayloadMismatchedType:
1146 case ErrorCode::kInstallDeviceOpenError:
1147 case ErrorCode::kKernelDeviceOpenError:
1148 case ErrorCode::kPayloadHashMismatchError:
1149 case ErrorCode::kPayloadSizeMismatchError:
1150 case ErrorCode::kDownloadPayloadVerificationError:
1151 case ErrorCode::kDownloadNewPartitionInfoError:
1152 case ErrorCode::kDownloadWriteError:
1153 case ErrorCode::kNewRootfsVerificationError:
1154 case ErrorCode::kNewKernelVerificationError:
1155 case ErrorCode::kSignedDeltaPayloadExpectedError:
1156 case ErrorCode::kDownloadPayloadPubKeyVerificationError:
1157 case ErrorCode::kPostinstallBootedFromFirmwareB:
1158 case ErrorCode::kDownloadStateInitializationError:
1159 case ErrorCode::kDownloadInvalidMetadataMagicString:
1160 case ErrorCode::kDownloadSignatureMissingInManifest:
1161 case ErrorCode::kDownloadManifestParseError:
1162 case ErrorCode::kDownloadMetadataSignatureError:
1163 case ErrorCode::kDownloadMetadataSignatureVerificationError:
1164 case ErrorCode::kDownloadMetadataSignatureMismatch:
1165 case ErrorCode::kDownloadOperationHashVerificationError:
1166 case ErrorCode::kDownloadOperationExecutionError:
1167 case ErrorCode::kDownloadOperationHashMismatch:
1168 case ErrorCode::kOmahaRequestEmptyResponseError:
1169 case ErrorCode::kOmahaRequestXMLParseError:
1170 case ErrorCode::kDownloadInvalidMetadataSize:
1171 case ErrorCode::kDownloadInvalidMetadataSignature:
1172 case ErrorCode::kOmahaResponseInvalid:
1173 case ErrorCode::kOmahaUpdateIgnoredPerPolicy:
1174 case ErrorCode::kOmahaUpdateDeferredPerPolicy:
1175 case ErrorCode::kOmahaErrorInHTTPResponse:
1176 case ErrorCode::kDownloadOperationHashMissingError:
1177 case ErrorCode::kDownloadMetadataSignatureMissingError:
1178 case ErrorCode::kOmahaUpdateDeferredForBackoff:
1179 case ErrorCode::kPostinstallPowerwashError:
1180 case ErrorCode::kUpdateCanceledByChannelChange:
1181 case ErrorCode::kPostinstallFirmwareRONotUpdatable:
1182 case ErrorCode::kUnsupportedMajorPayloadVersion:
1183 case ErrorCode::kUnsupportedMinorPayloadVersion:
David Zeuthenf3e28012014-08-26 18:23:52 -04001184 case ErrorCode::kOmahaRequestXMLHasEntityDecl:
Allie Woodeb9e6d82015-04-17 13:55:30 -07001185 case ErrorCode::kFilesystemVerifierError:
David Zeuthen33bae492014-02-25 16:16:18 -08001186 break;
1187
1188 // Special flags. These can't happen (we mask them out above) but
1189 // the compiler doesn't know that. Just break out so we can warn and
1190 // return |kInputMalformed|.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001191 case ErrorCode::kUmaReportedMax:
1192 case ErrorCode::kOmahaRequestHTTPResponseBase:
1193 case ErrorCode::kDevModeFlag:
1194 case ErrorCode::kResumedFlag:
1195 case ErrorCode::kTestImageFlag:
1196 case ErrorCode::kTestOmahaUrlFlag:
1197 case ErrorCode::kSpecialFlags:
David Zeuthen33bae492014-02-25 16:16:18 -08001198 LOG(ERROR) << "Unexpected error code " << base_code;
1199 break;
1200 }
1201
1202 return metrics::DownloadErrorCode::kInputMalformed;
1203}
1204
David Zeuthenb281f072014-04-02 10:20:19 -07001205metrics::ConnectionType GetConnectionType(
1206 NetworkConnectionType type,
1207 NetworkTethering tethering) {
1208 switch (type) {
Alex Deymo75eac7e2015-07-29 13:39:14 -07001209 case NetworkConnectionType::kUnknown:
David Zeuthenb281f072014-04-02 10:20:19 -07001210 return metrics::ConnectionType::kUnknown;
1211
Alex Deymo75eac7e2015-07-29 13:39:14 -07001212 case NetworkConnectionType::kEthernet:
David Zeuthenb281f072014-04-02 10:20:19 -07001213 if (tethering == NetworkTethering::kConfirmed)
1214 return metrics::ConnectionType::kTetheredEthernet;
1215 else
1216 return metrics::ConnectionType::kEthernet;
1217
Alex Deymo75eac7e2015-07-29 13:39:14 -07001218 case NetworkConnectionType::kWifi:
David Zeuthenb281f072014-04-02 10:20:19 -07001219 if (tethering == NetworkTethering::kConfirmed)
1220 return metrics::ConnectionType::kTetheredWifi;
1221 else
1222 return metrics::ConnectionType::kWifi;
1223
Alex Deymo75eac7e2015-07-29 13:39:14 -07001224 case NetworkConnectionType::kWimax:
David Zeuthenb281f072014-04-02 10:20:19 -07001225 return metrics::ConnectionType::kWimax;
1226
Alex Deymo75eac7e2015-07-29 13:39:14 -07001227 case NetworkConnectionType::kBluetooth:
David Zeuthenb281f072014-04-02 10:20:19 -07001228 return metrics::ConnectionType::kBluetooth;
1229
Alex Deymo75eac7e2015-07-29 13:39:14 -07001230 case NetworkConnectionType::kCellular:
David Zeuthenb281f072014-04-02 10:20:19 -07001231 return metrics::ConnectionType::kCellular;
1232 }
1233
Alex Deymo75eac7e2015-07-29 13:39:14 -07001234 LOG(ERROR) << "Unexpected network connection type: type="
1235 << static_cast<int>(type)
David Zeuthenb281f072014-04-02 10:20:19 -07001236 << ", tethering=" << static_cast<int>(tethering);
1237
1238 return metrics::ConnectionType::kUnknown;
1239}
1240
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001241// Returns a printable version of the various flags denoted in the higher order
1242// bits of the given code. Returns an empty string if none of those bits are
1243// set.
1244string GetFlagNames(uint32_t code) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001245 uint32_t flags = (static_cast<uint32_t>(code) &
1246 static_cast<uint32_t>(ErrorCode::kSpecialFlags));
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001247 string flag_names;
1248 string separator = "";
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001249 for (size_t i = 0; i < sizeof(flags) * 8; i++) {
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001250 uint32_t flag = flags & (1 << i);
1251 if (flag) {
David Zeuthena99981f2013-04-29 13:42:47 -07001252 flag_names += separator + CodeToString(static_cast<ErrorCode>(flag));
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001253 separator = ", ";
1254 }
1255 }
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -08001256
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001257 return flag_names;
Jay Srinivasanf0572052012-10-23 18:12:56 -07001258}
1259
David Zeuthena99981f2013-04-29 13:42:47 -07001260void SendErrorCodeToUma(SystemState* system_state, ErrorCode code) {
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001261 if (!system_state)
1262 return;
1263
David Zeuthena99981f2013-04-29 13:42:47 -07001264 ErrorCode uma_error_code = GetBaseErrorCode(code);
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001265
1266 // If the code doesn't have flags computed already, compute them now based on
1267 // the state of the current update attempt.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001268 uint32_t flags =
1269 static_cast<int>(code) & static_cast<int>(ErrorCode::kSpecialFlags);
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001270 if (!flags)
1271 flags = system_state->update_attempter()->GetErrorCodeFlags();
1272
1273 // Determine the UMA bucket depending on the flags. But, ignore the resumed
1274 // flag, as it's perfectly normal for production devices to resume their
1275 // downloads and so we want to record those cases also in NormalErrorCodes
1276 // bucket.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001277 string metric =
1278 flags & ~static_cast<uint32_t>(ErrorCode::kResumedFlag) ?
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001279 "Installer.DevModeErrorCodes" : "Installer.NormalErrorCodes";
1280
1281 LOG(INFO) << "Sending error code " << uma_error_code
1282 << " (" << CodeToString(uma_error_code) << ")"
1283 << " to UMA metric: " << metric
1284 << ". Flags = " << (flags ? GetFlagNames(flags) : "None");
1285
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001286 system_state->metrics_lib()->SendEnumToUMA(
1287 metric, static_cast<int>(uma_error_code),
1288 static_cast<int>(ErrorCode::kUmaReportedMax));
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001289}
1290
David Zeuthena99981f2013-04-29 13:42:47 -07001291string CodeToString(ErrorCode code) {
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001292 // If the given code has both parts (i.e. the error code part and the flags
1293 // part) then strip off the flags part since the switch statement below
1294 // has case statements only for the base error code or a single flag but
1295 // doesn't support any combinations of those.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001296 if ((static_cast<int>(code) & static_cast<int>(ErrorCode::kSpecialFlags)) &&
1297 (static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags)))
1298 code = static_cast<ErrorCode>(
1299 static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags));
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001300 switch (code) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001301 case ErrorCode::kSuccess: return "ErrorCode::kSuccess";
1302 case ErrorCode::kError: return "ErrorCode::kError";
1303 case ErrorCode::kOmahaRequestError: return "ErrorCode::kOmahaRequestError";
1304 case ErrorCode::kOmahaResponseHandlerError:
1305 return "ErrorCode::kOmahaResponseHandlerError";
1306 case ErrorCode::kFilesystemCopierError:
1307 return "ErrorCode::kFilesystemCopierError";
1308 case ErrorCode::kPostinstallRunnerError:
1309 return "ErrorCode::kPostinstallRunnerError";
1310 case ErrorCode::kPayloadMismatchedType:
1311 return "ErrorCode::kPayloadMismatchedType";
1312 case ErrorCode::kInstallDeviceOpenError:
1313 return "ErrorCode::kInstallDeviceOpenError";
1314 case ErrorCode::kKernelDeviceOpenError:
1315 return "ErrorCode::kKernelDeviceOpenError";
1316 case ErrorCode::kDownloadTransferError:
1317 return "ErrorCode::kDownloadTransferError";
1318 case ErrorCode::kPayloadHashMismatchError:
1319 return "ErrorCode::kPayloadHashMismatchError";
1320 case ErrorCode::kPayloadSizeMismatchError:
1321 return "ErrorCode::kPayloadSizeMismatchError";
1322 case ErrorCode::kDownloadPayloadVerificationError:
1323 return "ErrorCode::kDownloadPayloadVerificationError";
1324 case ErrorCode::kDownloadNewPartitionInfoError:
1325 return "ErrorCode::kDownloadNewPartitionInfoError";
1326 case ErrorCode::kDownloadWriteError:
1327 return "ErrorCode::kDownloadWriteError";
1328 case ErrorCode::kNewRootfsVerificationError:
1329 return "ErrorCode::kNewRootfsVerificationError";
1330 case ErrorCode::kNewKernelVerificationError:
1331 return "ErrorCode::kNewKernelVerificationError";
1332 case ErrorCode::kSignedDeltaPayloadExpectedError:
1333 return "ErrorCode::kSignedDeltaPayloadExpectedError";
1334 case ErrorCode::kDownloadPayloadPubKeyVerificationError:
1335 return "ErrorCode::kDownloadPayloadPubKeyVerificationError";
1336 case ErrorCode::kPostinstallBootedFromFirmwareB:
1337 return "ErrorCode::kPostinstallBootedFromFirmwareB";
1338 case ErrorCode::kDownloadStateInitializationError:
1339 return "ErrorCode::kDownloadStateInitializationError";
1340 case ErrorCode::kDownloadInvalidMetadataMagicString:
1341 return "ErrorCode::kDownloadInvalidMetadataMagicString";
1342 case ErrorCode::kDownloadSignatureMissingInManifest:
1343 return "ErrorCode::kDownloadSignatureMissingInManifest";
1344 case ErrorCode::kDownloadManifestParseError:
1345 return "ErrorCode::kDownloadManifestParseError";
1346 case ErrorCode::kDownloadMetadataSignatureError:
1347 return "ErrorCode::kDownloadMetadataSignatureError";
1348 case ErrorCode::kDownloadMetadataSignatureVerificationError:
1349 return "ErrorCode::kDownloadMetadataSignatureVerificationError";
1350 case ErrorCode::kDownloadMetadataSignatureMismatch:
1351 return "ErrorCode::kDownloadMetadataSignatureMismatch";
1352 case ErrorCode::kDownloadOperationHashVerificationError:
1353 return "ErrorCode::kDownloadOperationHashVerificationError";
1354 case ErrorCode::kDownloadOperationExecutionError:
1355 return "ErrorCode::kDownloadOperationExecutionError";
1356 case ErrorCode::kDownloadOperationHashMismatch:
1357 return "ErrorCode::kDownloadOperationHashMismatch";
1358 case ErrorCode::kOmahaRequestEmptyResponseError:
1359 return "ErrorCode::kOmahaRequestEmptyResponseError";
1360 case ErrorCode::kOmahaRequestXMLParseError:
1361 return "ErrorCode::kOmahaRequestXMLParseError";
1362 case ErrorCode::kDownloadInvalidMetadataSize:
1363 return "ErrorCode::kDownloadInvalidMetadataSize";
1364 case ErrorCode::kDownloadInvalidMetadataSignature:
1365 return "ErrorCode::kDownloadInvalidMetadataSignature";
1366 case ErrorCode::kOmahaResponseInvalid:
1367 return "ErrorCode::kOmahaResponseInvalid";
1368 case ErrorCode::kOmahaUpdateIgnoredPerPolicy:
1369 return "ErrorCode::kOmahaUpdateIgnoredPerPolicy";
1370 case ErrorCode::kOmahaUpdateDeferredPerPolicy:
1371 return "ErrorCode::kOmahaUpdateDeferredPerPolicy";
1372 case ErrorCode::kOmahaErrorInHTTPResponse:
1373 return "ErrorCode::kOmahaErrorInHTTPResponse";
1374 case ErrorCode::kDownloadOperationHashMissingError:
1375 return "ErrorCode::kDownloadOperationHashMissingError";
1376 case ErrorCode::kDownloadMetadataSignatureMissingError:
1377 return "ErrorCode::kDownloadMetadataSignatureMissingError";
1378 case ErrorCode::kOmahaUpdateDeferredForBackoff:
1379 return "ErrorCode::kOmahaUpdateDeferredForBackoff";
1380 case ErrorCode::kPostinstallPowerwashError:
1381 return "ErrorCode::kPostinstallPowerwashError";
1382 case ErrorCode::kUpdateCanceledByChannelChange:
1383 return "ErrorCode::kUpdateCanceledByChannelChange";
1384 case ErrorCode::kUmaReportedMax:
1385 return "ErrorCode::kUmaReportedMax";
1386 case ErrorCode::kOmahaRequestHTTPResponseBase:
1387 return "ErrorCode::kOmahaRequestHTTPResponseBase";
1388 case ErrorCode::kResumedFlag:
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001389 return "Resumed";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001390 case ErrorCode::kDevModeFlag:
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001391 return "DevMode";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001392 case ErrorCode::kTestImageFlag:
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001393 return "TestImage";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001394 case ErrorCode::kTestOmahaUrlFlag:
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001395 return "TestOmahaUrl";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001396 case ErrorCode::kSpecialFlags:
1397 return "ErrorCode::kSpecialFlags";
1398 case ErrorCode::kPostinstallFirmwareRONotUpdatable:
1399 return "ErrorCode::kPostinstallFirmwareRONotUpdatable";
1400 case ErrorCode::kUnsupportedMajorPayloadVersion:
1401 return "ErrorCode::kUnsupportedMajorPayloadVersion";
1402 case ErrorCode::kUnsupportedMinorPayloadVersion:
1403 return "ErrorCode::kUnsupportedMinorPayloadVersion";
David Zeuthenf3e28012014-08-26 18:23:52 -04001404 case ErrorCode::kOmahaRequestXMLHasEntityDecl:
1405 return "ErrorCode::kOmahaRequestXMLHasEntityDecl";
Allie Woodeb9e6d82015-04-17 13:55:30 -07001406 case ErrorCode::kFilesystemVerifierError:
1407 return "ErrorCode::kFilesystemVerifierError";
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001408 // Don't add a default case to let the compiler warn about newly added
1409 // error codes which should be added here.
1410 }
1411
1412 return "Unknown error: " + base::UintToString(static_cast<unsigned>(code));
1413}
Jay Srinivasanf0572052012-10-23 18:12:56 -07001414
Gilad Arnold30dedd82013-07-03 06:19:09 -07001415bool CreatePowerwashMarkerFile(const char* file_path) {
1416 const char* marker_file = file_path ? file_path : kPowerwashMarkerFile;
1417 bool result = utils::WriteFile(marker_file,
Jay Srinivasan1c0fe792013-03-28 16:45:25 -07001418 kPowerwashCommand,
1419 strlen(kPowerwashCommand));
Gilad Arnold30dedd82013-07-03 06:19:09 -07001420 if (result) {
1421 LOG(INFO) << "Created " << marker_file << " to powerwash on next reboot";
1422 } else {
1423 PLOG(ERROR) << "Error in creating powerwash marker file: " << marker_file;
1424 }
Jay Srinivasan1c0fe792013-03-28 16:45:25 -07001425
1426 return result;
1427}
1428
Gilad Arnold30dedd82013-07-03 06:19:09 -07001429bool DeletePowerwashMarkerFile(const char* file_path) {
1430 const char* marker_file = file_path ? file_path : kPowerwashMarkerFile;
Alex Vakulenko75039d72014-03-25 12:36:28 -07001431 const base::FilePath kPowerwashMarkerPath(marker_file);
1432 bool result = base::DeleteFile(kPowerwashMarkerPath, false);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -07001433
1434 if (result)
1435 LOG(INFO) << "Successfully deleted the powerwash marker file : "
Gilad Arnold30dedd82013-07-03 06:19:09 -07001436 << marker_file;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -07001437 else
1438 PLOG(ERROR) << "Could not delete the powerwash marker file : "
Gilad Arnold30dedd82013-07-03 06:19:09 -07001439 << marker_file;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -07001440
1441 return result;
1442}
1443
David Zeuthen27a48bc2013-08-06 12:06:29 -07001444Time TimeFromStructTimespec(struct timespec *ts) {
Ben Chan9abb7632014-08-07 00:10:53 -07001445 int64_t us = static_cast<int64_t>(ts->tv_sec) * Time::kMicrosecondsPerSecond +
1446 static_cast<int64_t>(ts->tv_nsec) / Time::kNanosecondsPerMicrosecond;
David Zeuthen27a48bc2013-08-06 12:06:29 -07001447 return Time::UnixEpoch() + TimeDelta::FromMicroseconds(us);
1448}
1449
Alex Deymof329b932014-10-30 01:37:48 -07001450string StringVectorToString(const vector<string> &vec_str) {
David Zeuthen27a48bc2013-08-06 12:06:29 -07001451 string str = "[";
Alex Deymof329b932014-10-30 01:37:48 -07001452 for (vector<string>::const_iterator i = vec_str.begin();
1453 i != vec_str.end(); ++i) {
1454 if (i != vec_str.begin())
David Zeuthen27a48bc2013-08-06 12:06:29 -07001455 str += ", ";
1456 str += '"';
1457 str += *i;
1458 str += '"';
1459 }
1460 str += "]";
1461 return str;
1462}
1463
David Zeuthen8f191b22013-08-06 12:27:50 -07001464string CalculateP2PFileId(const string& payload_hash, size_t payload_size) {
Alex Vakulenko981a9fb2015-02-09 12:51:24 -08001465 string encoded_hash = chromeos::data_encoding::Base64Encode(payload_hash);
Alex Deymoc00c98a2015-03-17 17:38:00 -07001466 return base::StringPrintf("cros_update_size_%" PRIuS "_hash_%s",
Alex Vakulenko981a9fb2015-02-09 12:51:24 -08001467 payload_size,
1468 encoded_hash.c_str());
David Zeuthen8f191b22013-08-06 12:27:50 -07001469}
1470
Alex Deymof329b932014-10-30 01:37:48 -07001471bool DecodeAndStoreBase64String(const string& base64_encoded,
David Zeuthene7f89172013-10-31 10:21:04 -07001472 base::FilePath *out_path) {
Alex Vakulenko981a9fb2015-02-09 12:51:24 -08001473 chromeos::Blob contents;
David Zeuthene7f89172013-10-31 10:21:04 -07001474
1475 out_path->clear();
1476
1477 if (base64_encoded.size() == 0) {
1478 LOG(ERROR) << "Can't decode empty string.";
1479 return false;
1480 }
1481
Alex Vakulenko981a9fb2015-02-09 12:51:24 -08001482 if (!chromeos::data_encoding::Base64Decode(base64_encoded, &contents) ||
David Zeuthene7f89172013-10-31 10:21:04 -07001483 contents.size() == 0) {
1484 LOG(ERROR) << "Error decoding base64.";
1485 return false;
1486 }
1487
Alex Vakulenko75039d72014-03-25 12:36:28 -07001488 FILE *file = base::CreateAndOpenTemporaryFile(out_path);
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001489 if (file == nullptr) {
David Zeuthene7f89172013-10-31 10:21:04 -07001490 LOG(ERROR) << "Error creating temporary file.";
1491 return false;
1492 }
1493
Alex Vakulenko981a9fb2015-02-09 12:51:24 -08001494 if (fwrite(contents.data(), 1, contents.size(), file) != contents.size()) {
David Zeuthene7f89172013-10-31 10:21:04 -07001495 PLOG(ERROR) << "Error writing to temporary file.";
1496 if (fclose(file) != 0)
1497 PLOG(ERROR) << "Error closing temporary file.";
1498 if (unlink(out_path->value().c_str()) != 0)
1499 PLOG(ERROR) << "Error unlinking temporary file.";
1500 out_path->clear();
1501 return false;
1502 }
1503
1504 if (fclose(file) != 0) {
1505 PLOG(ERROR) << "Error closing temporary file.";
1506 out_path->clear();
1507 return false;
1508 }
1509
1510 return true;
1511}
1512
Alex Deymof329b932014-10-30 01:37:48 -07001513bool ConvertToOmahaInstallDate(Time time, int *out_num_days) {
David Zeuthen639aa362014-02-03 16:23:44 -08001514 time_t unix_time = time.ToTimeT();
1515 // Output of: date +"%s" --date="Jan 1, 2007 0:00 PST".
1516 const time_t kOmahaEpoch = 1167638400;
1517 const int64_t kNumSecondsPerWeek = 7*24*3600;
1518 const int64_t kNumDaysPerWeek = 7;
1519
1520 time_t omaha_time = unix_time - kOmahaEpoch;
1521
1522 if (omaha_time < 0)
1523 return false;
1524
1525 // Note, as per the comment in utils.h we are deliberately not
1526 // handling DST correctly.
1527
1528 int64_t num_weeks_since_omaha_epoch = omaha_time / kNumSecondsPerWeek;
1529 *out_num_days = num_weeks_since_omaha_epoch * kNumDaysPerWeek;
1530
1531 return true;
1532}
1533
David Zeuthen33bae492014-02-25 16:16:18 -08001534bool WallclockDurationHelper(SystemState* system_state,
Alex Deymof329b932014-10-30 01:37:48 -07001535 const string& state_variable_key,
1536 TimeDelta* out_duration) {
David Zeuthen33bae492014-02-25 16:16:18 -08001537 bool ret = false;
1538
Alex Deymof329b932014-10-30 01:37:48 -07001539 Time now = system_state->clock()->GetWallclockTime();
David Zeuthen33bae492014-02-25 16:16:18 -08001540 int64_t stored_value;
1541 if (system_state->prefs()->GetInt64(state_variable_key, &stored_value)) {
Alex Deymof329b932014-10-30 01:37:48 -07001542 Time stored_time = Time::FromInternalValue(stored_value);
David Zeuthen33bae492014-02-25 16:16:18 -08001543 if (stored_time > now) {
1544 LOG(ERROR) << "Stored time-stamp used for " << state_variable_key
1545 << " is in the future.";
1546 } else {
1547 *out_duration = now - stored_time;
1548 ret = true;
1549 }
1550 }
1551
1552 if (!system_state->prefs()->SetInt64(state_variable_key,
1553 now.ToInternalValue())) {
1554 LOG(ERROR) << "Error storing time-stamp in " << state_variable_key;
1555 }
1556
1557 return ret;
1558}
1559
1560bool MonotonicDurationHelper(SystemState* system_state,
1561 int64_t* storage,
Alex Deymof329b932014-10-30 01:37:48 -07001562 TimeDelta* out_duration) {
David Zeuthen33bae492014-02-25 16:16:18 -08001563 bool ret = false;
1564
Alex Deymof329b932014-10-30 01:37:48 -07001565 Time now = system_state->clock()->GetMonotonicTime();
David Zeuthen33bae492014-02-25 16:16:18 -08001566 if (*storage != 0) {
Alex Deymof329b932014-10-30 01:37:48 -07001567 Time stored_time = Time::FromInternalValue(*storage);
David Zeuthen33bae492014-02-25 16:16:18 -08001568 *out_duration = now - stored_time;
1569 ret = true;
1570 }
1571 *storage = now.ToInternalValue();
1572
1573 return ret;
1574}
1575
Alex Deymob42b98d2015-07-06 17:42:38 -07001576bool GetMinorVersion(const chromeos::KeyValueStore& store,
1577 uint32_t* minor_version) {
Alex Deymo60ca1a72015-06-18 18:19:15 -07001578 string result;
Alex Deymob42b98d2015-07-06 17:42:38 -07001579 if (store.GetString("PAYLOAD_MINOR_VERSION", &result)) {
Allie Wood425aa972015-02-17 14:47:17 -08001580 if (!base::StringToUint(result, minor_version)) {
1581 LOG(ERROR) << "StringToUint failed when parsing delta minor version.";
1582 return false;
1583 }
Allie Wood78750a42015-02-11 15:42:11 -08001584 return true;
1585 }
1586 return false;
1587}
1588
Alex Deymo60ca1a72015-06-18 18:19:15 -07001589bool ReadExtents(const string& path, const vector<Extent>& extents,
Allie Wood56873452015-03-27 17:48:40 -07001590 chromeos::Blob* out_data, ssize_t out_data_size,
1591 size_t block_size) {
1592 chromeos::Blob data(out_data_size);
1593 ssize_t bytes_read = 0;
1594 int fd = open(path.c_str(), O_RDONLY);
1595 TEST_AND_RETURN_FALSE_ERRNO(fd >= 0);
1596 ScopedFdCloser fd_closer(&fd);
1597
Gilad Arnold41e34742015-05-11 11:31:50 -07001598 for (const Extent& extent : extents) {
Allie Wood56873452015-03-27 17:48:40 -07001599 ssize_t bytes_read_this_iteration = 0;
1600 ssize_t bytes = extent.num_blocks() * block_size;
1601 TEST_AND_RETURN_FALSE(bytes_read + bytes <= out_data_size);
1602 TEST_AND_RETURN_FALSE(utils::PReadAll(fd,
1603 &data[bytes_read],
1604 bytes,
1605 extent.start_block() * block_size,
1606 &bytes_read_this_iteration));
1607 TEST_AND_RETURN_FALSE(bytes_read_this_iteration == bytes);
1608 bytes_read += bytes_read_this_iteration;
1609 }
1610 TEST_AND_RETURN_FALSE(out_data_size == bytes_read);
1611 *out_data = data;
1612 return true;
1613}
1614
Alex Deymodd132f32015-09-14 19:12:07 -07001615bool GetBootId(string* boot_id) {
1616 TEST_AND_RETURN_FALSE(
1617 base::ReadFileToString(base::FilePath(kBootIdPath), boot_id));
1618 base::TrimWhitespaceASCII(*boot_id, base::TRIM_TRAILING, boot_id);
1619 return true;
1620}
1621
adlr@google.com3defe6a2009-12-04 20:57:17 +00001622} // namespace utils
1623
1624} // namespace chromeos_update_engine