diff options
author | Bill Richardson <wfrichar@chromium.org> | 2013-02-28 13:53:09 -0800 |
---|---|---|
committer | ChromeBot <chrome-bot@google.com> | 2013-03-04 11:09:59 -0800 |
commit | 81a0b3de707b4e00958faf36f41b8f3d7bac6808 (patch) | |
tree | 44156bef6c22241980fc1b417046b468f1697661 /cgpt | |
parent | c7c6e5d2f7076fe512b692411f0d05d4bf76d91f (diff) | |
download | vboot-81a0b3de707b4e00958faf36f41b8f3d7bac6808.tar.gz |
Move CgptManager from vboot_reference to installer
The C++ wrapper around various vboot_reference functions doesn't belong in
the vboot repo itself. Put it in the installer repo instead.
BUG=chromium-os:39228
BRANCH=none
TEST=auto
CQ-DEPEND=CL:44441, CL:44443
Refactoring only, no new code. Everything should continue to work as before.
Change-Id: I15ba416987e38905825fedcc87d7b75ebdc4fd1f
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/44442
Reviewed-by: Jay Srinivasan <jaysri@chromium.org>
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Diffstat (limited to 'cgpt')
-rw-r--r-- | cgpt/CgptManager.cc | 429 | ||||
-rw-r--r-- | cgpt/CgptManager.h | 172 |
2 files changed, 0 insertions, 601 deletions
diff --git a/cgpt/CgptManager.cc b/cgpt/CgptManager.cc deleted file mode 100644 index a1a018a3..00000000 --- a/cgpt/CgptManager.cc +++ /dev/null @@ -1,429 +0,0 @@ -// Copyright (c) 2012 The Chromium OS Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include <string.h> - -#include "CgptManager.h" - -extern "C" { -#include "cgpt_params.h" -} - -using std::string; - -// We don't use these variables for the libcgpt version. -const char* progname = ""; -const char* command = ""; -void (*uuid_generator)(uint8_t* buffer) = NULL; - - -// This file implements the C++ wrapper methods over the C cgpt methods. - -CgptManager::CgptManager(): - is_initialized_(false) { -} - -CgptManager::~CgptManager() { -} - -CgptErrorCode CgptManager::Initialize(const string& device_name) { - device_name_ = device_name; - is_initialized_ = true; - return kCgptSuccess; -} - -CgptErrorCode CgptManager::ClearAll() { - if (!is_initialized_) - return kCgptNotInitialized; - - CgptCreateParams params; - memset(¶ms, 0, sizeof(params)); - - params.drive_name = const_cast<char *>(device_name_.c_str()); - params.zap = 0; - - int retval = cgpt_create(¶ms); - if (retval != CGPT_OK) - return kCgptUnknownError; - - return kCgptSuccess; -} - -CgptErrorCode CgptManager::AddPartition(const string& label, - const Guid& partition_type_guid, - const Guid& unique_id, - uint64_t beginning_offset, - uint64_t num_sectors) { - if (!is_initialized_) - return kCgptNotInitialized; - - CgptAddParams params; - memset(¶ms, 0, sizeof(params)); - - params.drive_name = const_cast<char *>(device_name_.c_str()); - params.label = const_cast<char *>(label.c_str()); - - params.type_guid = partition_type_guid; - params.set_type = 1; - - params.begin = beginning_offset; - params.set_begin = 1; - - params.size = num_sectors; - params.set_size = 1; - - if (!IsZero(&unique_id)) { - params.unique_guid = unique_id; - params.set_unique = 1; - } - - int retval = cgpt_add(¶ms); - if (retval != CGPT_OK) - return kCgptUnknownError; - - return kCgptSuccess; -} - -CgptErrorCode CgptManager::GetNumNonEmptyPartitions(uint8_t* num_partitions) const { - if (!is_initialized_) - return kCgptNotInitialized; - - if (!num_partitions) - return kCgptInvalidArgument; - - CgptShowParams params; - memset(¶ms, 0, sizeof(params)); - - params.drive_name = const_cast<char *>(device_name_.c_str()); - int retval = cgpt_get_num_non_empty_partitions(¶ms); - - if (retval != CGPT_OK) - return kCgptUnknownError; - - *num_partitions = params.num_partitions; - return kCgptSuccess; -} - -CgptErrorCode CgptManager::SetPmbr(uint32_t boot_partition_number, - const string& boot_file_name, - bool should_create_legacy_partition) { - if (!is_initialized_) - return kCgptNotInitialized; - - CgptBootParams params; - memset(¶ms, 0, sizeof(params)); - - params.drive_name = const_cast<char *>(device_name_.c_str()); - if (!boot_file_name.empty()) - params.bootfile = const_cast<char *>(boot_file_name.c_str()); - - params.partition = boot_partition_number; - params.create_pmbr = should_create_legacy_partition; - - int retval = cgpt_boot(¶ms); - if (retval != CGPT_OK) - return kCgptUnknownError; - - return kCgptSuccess; -} - -CgptErrorCode CgptManager::GetPmbrBootPartitionNumber( - uint32_t* boot_partition) const { - if (!is_initialized_) - return kCgptNotInitialized; - - if (!boot_partition) - return kCgptInvalidArgument; - - CgptBootParams params; - memset(¶ms, 0, sizeof(params)); - - params.drive_name = const_cast<char *>(device_name_.c_str()); - - int retval = cgpt_get_boot_partition_number(¶ms); - if (retval != CGPT_OK) - return kCgptUnknownError; - - *boot_partition = params.partition; - return kCgptSuccess; -} - -CgptErrorCode CgptManager::SetSuccessful( - uint32_t partition_number, - bool is_successful) { - if (!is_initialized_) - return kCgptNotInitialized; - - CgptAddParams params; - memset(¶ms, 0, sizeof(params)); - - params.drive_name = const_cast<char *>(device_name_.c_str()); - params.partition = partition_number; - - params.successful = is_successful; - params.set_successful = true; - - int retval = cgpt_set_attributes(¶ms); - if (retval != CGPT_OK) - return kCgptUnknownError; - - return kCgptSuccess; -} - -CgptErrorCode CgptManager::GetSuccessful(uint32_t partition_number, - bool* is_successful) const { - if (!is_initialized_) - return kCgptNotInitialized; - - if (!is_successful) - return kCgptInvalidArgument; - - CgptAddParams params; - memset(¶ms, 0, sizeof(params)); - - params.drive_name = const_cast<char *>(device_name_.c_str()); - params.partition = partition_number; - - int retval = cgpt_get_partition_details(¶ms); - if (retval != CGPT_OK) - return kCgptUnknownError; - - *is_successful = params.successful; - return kCgptSuccess; -} - -CgptErrorCode CgptManager::SetNumTriesLeft(uint32_t partition_number, - int numTries) { - if (!is_initialized_) - return kCgptNotInitialized; - - CgptAddParams params; - memset(¶ms, 0, sizeof(params)); - - params.drive_name = const_cast<char *>(device_name_.c_str()); - params.partition = partition_number; - - params.tries = numTries; - params.set_tries = true; - - int retval = cgpt_set_attributes(¶ms); - if (retval != CGPT_OK) - return kCgptUnknownError; - - return kCgptSuccess; -} - -CgptErrorCode CgptManager::GetNumTriesLeft(uint32_t partition_number, - int* numTries) const { - if (!is_initialized_) - return kCgptNotInitialized; - - if (!numTries) - return kCgptInvalidArgument; - - CgptAddParams params; - memset(¶ms, 0, sizeof(params)); - - params.drive_name = const_cast<char *>(device_name_.c_str()); - params.partition = partition_number; - - int retval = cgpt_get_partition_details(¶ms); - if (retval != CGPT_OK) - return kCgptUnknownError; - - *numTries = params.tries; - return kCgptSuccess; -} - -CgptErrorCode CgptManager::SetPriority(uint32_t partition_number, - uint8_t priority) { - if (!is_initialized_) - return kCgptNotInitialized; - - CgptAddParams params; - memset(¶ms, 0, sizeof(params)); - - params.drive_name = const_cast<char *>(device_name_.c_str()); - params.partition = partition_number; - - params.priority = priority; - params.set_priority = true; - - int retval = cgpt_set_attributes(¶ms); - if (retval != CGPT_OK) - return kCgptUnknownError; - - return kCgptSuccess; -} - -CgptErrorCode CgptManager::GetPriority(uint32_t partition_number, - uint8_t* priority) const { - if (!is_initialized_) - return kCgptNotInitialized; - - if (!priority) - return kCgptInvalidArgument; - - CgptAddParams params; - memset(¶ms, 0, sizeof(params)); - - params.drive_name = const_cast<char *>(device_name_.c_str()); - params.partition = partition_number; - - int retval = cgpt_get_partition_details(¶ms); - if (retval != CGPT_OK) - return kCgptUnknownError; - - *priority = params.priority; - return kCgptSuccess; -} - -CgptErrorCode CgptManager::GetBeginningOffset(uint32_t partition_number, - uint64_t* offset) const { - if (!is_initialized_) - return kCgptNotInitialized; - - if (!offset) - return kCgptInvalidArgument; - - CgptAddParams params; - memset(¶ms, 0, sizeof(params)); - - params.drive_name = const_cast<char *>(device_name_.c_str()); - params.partition = partition_number; - - int retval = cgpt_get_partition_details(¶ms); - if (retval != CGPT_OK) - return kCgptUnknownError; - - *offset = params.begin; - return kCgptSuccess; -} - -CgptErrorCode CgptManager::GetNumSectors(uint32_t partition_number, - uint64_t* num_sectors) const { - if (!is_initialized_) - return kCgptNotInitialized; - - if (!num_sectors) - return kCgptInvalidArgument; - - CgptAddParams params; - memset(¶ms, 0, sizeof(params)); - - params.drive_name = const_cast<char *>(device_name_.c_str()); - params.partition = partition_number; - - int retval = cgpt_get_partition_details(¶ms); - if (retval != CGPT_OK) - return kCgptUnknownError; - - *num_sectors = params.size; - return kCgptSuccess; -} - -CgptErrorCode CgptManager::GetPartitionTypeId(uint32_t partition_number, - Guid* type_id) const { - if (!is_initialized_) - return kCgptNotInitialized; - - if (!type_id) - return kCgptInvalidArgument; - - CgptAddParams params; - memset(¶ms, 0, sizeof(params)); - - params.drive_name = const_cast<char *>(device_name_.c_str()); - params.partition = partition_number; - - int retval = cgpt_get_partition_details(¶ms); - if (retval != CGPT_OK) - return kCgptUnknownError; - - *type_id = params.type_guid; - return kCgptSuccess; -} - -CgptErrorCode CgptManager::GetPartitionUniqueId(uint32_t partition_number, - Guid* unique_id) const { - if (!is_initialized_) - return kCgptNotInitialized; - - if (!unique_id) - return kCgptInvalidArgument; - - CgptAddParams params; - memset(¶ms, 0, sizeof(params)); - - params.drive_name = const_cast<char *>(device_name_.c_str()); - params.partition = partition_number; - - int retval = cgpt_get_partition_details(¶ms); - if (retval != CGPT_OK) - return kCgptUnknownError; - - *unique_id = params.unique_guid; - return kCgptSuccess; -} - -CgptErrorCode CgptManager::GetPartitionNumberByUniqueId( - const Guid& unique_id, - uint32_t* partition_number) const { - if (!is_initialized_) - return kCgptNotInitialized; - - if (!partition_number) - return kCgptInvalidArgument; - - CgptAddParams params; - memset(¶ms, 0, sizeof(params)); - - params.drive_name = const_cast<char *>(device_name_.c_str()); - params.unique_guid = unique_id; - params.set_unique = 1; - - int retval = cgpt_get_partition_details(¶ms); - if (retval != CGPT_OK) - return kCgptUnknownError; - - *partition_number = params.partition; - return kCgptSuccess; -} - -CgptErrorCode CgptManager::SetHighestPriority(uint32_t partition_number, - uint8_t highest_priority) { - if (!is_initialized_) - return kCgptNotInitialized; - - CgptPrioritizeParams params; - memset(¶ms, 0, sizeof(params)); - - params.drive_name = const_cast<char *>(device_name_.c_str()); - params.set_partition = partition_number; - params.max_priority = highest_priority; - - int retval = cgpt_prioritize(¶ms); - if (retval != CGPT_OK) - return kCgptUnknownError; - - return kCgptSuccess; -} - -CgptErrorCode CgptManager::SetHighestPriority(uint32_t partition_number) { - // The internal implementation in cgpt_prioritize automatically computes the - // right priority number if we supply 0 for the highest_priority argument. - return SetHighestPriority(partition_number, 0); -} - -CgptErrorCode CgptManager::Validate() { - if (!is_initialized_) - return kCgptNotInitialized; - - uint8_t num_partitions; - - // GetNumNonEmptyPartitions does the check for GptSanityCheck. - // so call it (ignore the num_partitions result) and just return - // its success/failure result. - return GetNumNonEmptyPartitions(&num_partitions); -} diff --git a/cgpt/CgptManager.h b/cgpt/CgptManager.h deleted file mode 100644 index 2dbca6f2..00000000 --- a/cgpt/CgptManager.h +++ /dev/null @@ -1,172 +0,0 @@ -// Copyright (c) 2012 The Chromium OS Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef VBOOT_REFERENCE_CGPT_CGPTMANAGER_H_ -#define VBOOT_REFERENCE_CGPT_CGPTMANAGER_H_ - -#include <string> -#include "gpt.h" - -// This file defines a simple C++ wrapper class interface for the cgpt methods. - -// These are the possible error codes that can be returned by the CgptManager. -typedef enum CgptErrorCode -{ - kCgptSuccess = 0, - kCgptNotInitialized = 1, - kCgptUnknownError = 2, - kCgptInvalidArgument = 3, -} CgptErrorCode; - - -// CgptManager exposes methods to manipulate the Guid Partition Table as needed -// for ChromeOS scenarios. -class CgptManager { - public: - // Default constructor. The Initialize method must be called before - // any other method can be called on this class. - CgptManager(); - - // Destructor. Automatically closes any opened device. - ~CgptManager(); - - // Opens the given device_name (e.g. "/dev/sdc") and initializes - // with the Guid Partition Table of that device. This is the first method - // that should be called on this class. Otherwise those methods will - // return kCgptNotInitialized. - // Returns kCgptSuccess or an appropriate error code. - // This device is automatically closed when this object is destructed. - CgptErrorCode Initialize(const std::string& device_name); - - // Clears all the existing contents of the GPT and PMBR on the current - // device. - CgptErrorCode ClearAll(); - - // Adds a new partition at the end of the existing partitions - // with the new label, type, unique Id, offset and size. - // Returns kCgptSuccess or an appropriate error code. - CgptErrorCode AddPartition(const std::string& label, - const Guid& partition_type_guid, - const Guid& unique_id, - uint64_t beginning_offset, - uint64_t num_sectors); - - // Populates num_partitions parameter with the number of partitions - // that are currently on this device and not empty. - // Returns kCgptSuccess or an appropriate error code. - CgptErrorCode GetNumNonEmptyPartitions(uint8_t* num_partitions) const; - - // Sets the Protective Master Boot Record on this device with the given - // boot_partition number after populating the MBR with the contents of the - // given boot_file_name. It also creates a legacy partition if - // should_create_legacy_partition is true. - // Note: Strictly speaking, the PMBR is not part of the GPT, but it is - // included here for ease of use. - CgptErrorCode SetPmbr(uint32_t boot_partition_number, - const std::string& boot_file_name, - bool should_create_legacy_partition); - - // Populates boot_partition with the partition number that's set to - // boot in the PMBR. - // Returns kCgptSuccess or an appropriate error code. - CgptErrorCode GetPmbrBootPartitionNumber(uint32_t* boot_partition) const; - - // Sets the "successful" attribute of the given kernelPartition to 0 or 1 - // based on the value of is_successful being true (1) or false(0) - // Returns kCgptSuccess or an appropriate error code. - CgptErrorCode SetSuccessful(uint32_t partition_number, bool is_successful); - - // Populates is_successful to true if the successful attribute in the - // given kernelPartition is non-zero, or to false if it's zero. - // Returns kCgptSuccess or an appropriate error code. - CgptErrorCode GetSuccessful(uint32_t partition_number, - bool* is_successful) const; - - // Sets the "NumTriesLeft" attribute of the given kernelPartition to - // the given num_tries_left value. - // Returns kCgptSuccess or an appropriate error code. - CgptErrorCode SetNumTriesLeft(uint32_t partition_number, - int num_tries_left); - - // Populates the num_tries_left parameter with the value of the - // NumTriesLeft attribute of the given kernelPartition. - // Returns kCgptSuccess or an appropriate error code. - CgptErrorCode GetNumTriesLeft(uint32_t partition_number, - int* num_tries_left) const; - - // Sets the "Priority" attribute of the given kernelPartition to - // the given priority value. - // Returns kCgptSuccess or an appropriate error code. - CgptErrorCode SetPriority(uint32_t partition_number, - uint8_t priority); - - // Populates the priority parameter with the value of the Priority - // attribute of the given kernelPartition. - // Returns kCgptSuccess or an appropriate error code. - CgptErrorCode GetPriority(uint32_t partition_number, - uint8_t* priority) const; - - // Populates the offset parameter with the beginning offset of the - // given partition. - // Returns kCgptSuccess or an appropriate error code. - CgptErrorCode GetBeginningOffset(uint32_t partition_number, - uint64_t* offset) const; - - // Populates the number of sectors in the given partition. - // Returns kCgptSuccess or an appropriate error code. - CgptErrorCode GetNumSectors(uint32_t partition_number, - uint64_t* num_sectors) const; - - // Populates the type_id parameter with the partition type id - // (these are the standard ids for kernel, rootfs, etc.) - // of the partition corresponding to the given partition_number. - // Returns kCgptSuccess or an appropriate error code. - CgptErrorCode GetPartitionTypeId(uint32_t partition_number, - Guid* type_id) const; - - // Populates the unique_id parameter with the Guid that uniquely identifies - // the given partition_number. - // Returns kCgptSuccess or an appropriate error code. - CgptErrorCode GetPartitionUniqueId(uint32_t partition_number, - Guid* unique_id) const; - - // Populates the partition_number parameter with the partition number of - // the partition which is uniquely identified by the given unique_id. - // Returns kCgptSuccess or an appropriate error code. - CgptErrorCode GetPartitionNumberByUniqueId( - const Guid& unique_id, - uint32_t* partition_number) const; - - // Sets the "Priority" attribute of given kernelPartition to the value - // specified in higestPriority parameter. In addition, also reduces the - // priorities of all the other kernel partitions, if necessary, to ensure - // no other partition has a higher priority. It does preserve the relative - // ordering among the remaining partitions and doesn't touch the partitions - // whose priorities are zero. - // Returns kCgptSuccess or an appropriate error code. - CgptErrorCode SetHighestPriority(uint32_t partition_number, - uint8_t highest_priority); - - // Same as SetHighestPriority above but works without having to explicitly - // give a value for highest_priority. The internal implementation figures - // out the best highest number that needs to be given depending on the - // existing priorities. - // Returns kCgptSuccess or an appropriate error code. - CgptErrorCode SetHighestPriority(uint32_t partition_number); - - // Runs the sanity checks on the CGPT and MBR and - // Returns kCgptSuccess if everything is valid or an appropriate error code - // if there's anything invalid or if there's any error encountered during - // the validation. - CgptErrorCode Validate(); - - private: - std::string device_name_; - bool is_initialized_; - - CgptManager(const CgptManager &); - void operator=(const CgptManager &); -}; - -#endif // VBOOT_REFERENCE_CGPT_CGPTMANAGER_H_ |