Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1 | // 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 McLoughlin | 049d165 | 2013-07-31 18:47:46 -0700 | [diff] [blame^] | 6 | #include "update_engine/utils.h" |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 7 | |
| 8 | #include <base/logging.h> |
| 9 | #include <rootdev/rootdev.h> |
| 10 | |
| 11 | using std::string; |
| 12 | |
| 13 | namespace chromeos_update_engine { |
| 14 | |
| 15 | const 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 | |
| 32 | const string Hardware::KernelDeviceOfBootDevice( |
| 33 | const std::string& boot_device) { |
Liam McLoughlin | 049d165 | 2013-07-31 18:47:46 -0700 | [diff] [blame^] | 34 | 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 Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 52 | // 3, 5, or 7, and changes it to 2, 4, or 6, respectively, to |
| 53 | // get the kernel device. |
Liam McLoughlin | 049d165 | 2013-07-31 18:47:46 -0700 | [diff] [blame^] | 54 | if (partition_num == '3' || partition_num == '5' || partition_num == '7') { |
| 55 | ret.append(1, partition_num - 1); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 56 | return ret; |
| 57 | } |
Liam McLoughlin | 049d165 | 2013-07-31 18:47:46 -0700 | [diff] [blame^] | 58 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 59 | return ""; |
| 60 | } |
| 61 | |
| 62 | } // namespace chromeos_update_engine |