blob: b330f64b65e3876dd1554ab4930b89a10b189c01 [file] [log] [blame]
Alex Deymo42432912013-07-12 20:21:15 -07001// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/hardware.h"
Liam McLoughlin049d1652013-07-31 18:47:46 -07006#include "update_engine/utils.h"
Alex Deymo42432912013-07-12 20:21:15 -07007
8#include <base/logging.h>
9#include <rootdev/rootdev.h>
10
11using std::string;
12
13namespace chromeos_update_engine {
14
15const string Hardware::BootDevice() {
16 char boot_path[PATH_MAX];
17 // Resolve the boot device path fully, including dereferencing
18 // through dm-verity.
19 int ret = rootdev(boot_path, sizeof(boot_path), true, false);
20
21 if (ret < 0) {
22 LOG(ERROR) << "rootdev failed to find the root device";
23 return "";
24 }
25 LOG_IF(WARNING, ret > 0) << "rootdev found a device name with no device node";
26
27 // This local variable is used to construct the return string and is not
28 // passed around after use.
29 return boot_path;
30}
31
32const string Hardware::KernelDeviceOfBootDevice(
33 const std::string& boot_device) {
Liam McLoughlin049d1652013-07-31 18:47:46 -070034 if (boot_device.empty())
35 return boot_device;
36
37 string ubiblock_prefix("/dev/ubiblock");
38 string ret;
39 char partition_num;
40 if(utils::StringHasPrefix(boot_device, ubiblock_prefix)) {
41 // eg: /dev/ubiblock3_0 becomes /dev/mtdblock2
42 ret = "/dev/mtdblock";
43 partition_num = boot_device[ubiblock_prefix.size()];
44 } else {
45 // eg: /dev/sda3 becomes /dev/sda2
46 // eg: /dev/mmcblk0p3 becomes /dev/mmcblk0p2
47 ret = boot_device.substr(0, boot_device.size() - 1);
48 partition_num = boot_device[boot_device.size() - 1];
49 }
50
51 // Currently this assumes the partition number of the boot device is
Alex Deymo42432912013-07-12 20:21:15 -070052 // 3, 5, or 7, and changes it to 2, 4, or 6, respectively, to
53 // get the kernel device.
Liam McLoughlin049d1652013-07-31 18:47:46 -070054 if (partition_num == '3' || partition_num == '5' || partition_num == '7') {
55 ret.append(1, partition_num - 1);
Alex Deymo42432912013-07-12 20:21:15 -070056 return ret;
57 }
Liam McLoughlin049d1652013-07-31 18:47:46 -070058
Alex Deymo42432912013-07-12 20:21:15 -070059 return "";
60}
61
62} // namespace chromeos_update_engine