From 034d68da6e57de32f81277f8058b6c7962ce5172 Mon Sep 17 00:00:00 2001 From: Jaemin Ryu Date: Wed, 4 Jul 2018 10:15:04 +0900 Subject: [PATCH] Use blkid to identify partition Change-Id: I16f5681b21e87a65d77b642d14f102f71f8605b8 Signed-off-by: Jaemin Ryu --- fota/CMakeLists.txt | 2 +- fota/fota.cpp | 89 +++++++++++++++++++++++++++++++++++++++++++++++++---- packaging/ode.spec | 1 + 3 files changed, 85 insertions(+), 7 deletions(-) diff --git a/fota/CMakeLists.txt b/fota/CMakeLists.txt index 2d2e71f..af7098f 100755 --- a/fota/CMakeLists.txt +++ b/fota/CMakeLists.txt @@ -16,7 +16,7 @@ SET(PROJECT_NAME "ode-fota") -PKG_CHECK_MODULES(FOTA_DEPS REQUIRED klay-static) +PKG_CHECK_MODULES(FOTA_DEPS REQUIRED klay-static blkid) SET(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/fota.cpp ${ODE_SERVER}/upgrade-support.cpp diff --git a/fota/fota.cpp b/fota/fota.cpp index c747af7..8ea9967 100644 --- a/fota/fota.cpp +++ b/fota/fota.cpp @@ -13,8 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License */ - +#include +#include +#include +#include #include +#include +#include #include #include @@ -29,6 +34,77 @@ audit::LogSink *SINK = nullptr; namespace ode { +struct AbstractDevice { + AbstractDevice() {} + virtual ~AbstractDevice() {} + virtual std::string findNode(const std::string &name) = 0; +}; + +struct MmcDevice : public AbstractDevice { +public: + MmcDevice(int id) : device("/dev/mmcblk" + std::to_string(id)), deviceHandle(-1) + { + deviceHandle = ::open(device.c_str(), O_RDONLY); + if (deviceHandle == -1) + throw std::runtime_error("Invalid device: " + device); + } + + virtual ~MmcDevice() + { + if (deviceHandle != -1) + ::close(deviceHandle); + } + + virtual std::string findNode(const std::string &name) + { + blkid_partlist partlist; + blkid_probe probe; + int partno = 0; + + probe = ::blkid_new_probe(); + if (!probe) { + throw std::runtime_error("Failed to call blkid_new_probe"); + } + + if (::blkid_probe_set_device(probe, deviceHandle, 0, 0) < 0) { + ::blkid_free_probe(probe); + throw std::runtime_error("Failed to set prove device: " + device); + } + + partlist = ::blkid_probe_get_partitions(probe); + if (!partlist) { + ::blkid_free_probe(probe); + throw std::runtime_error("Failed to get partition list in device: " + device); + } + + int num = ::blkid_partlist_numof_partitions(partlist); + for (int i = 1; i <= num; i++) { + blkid_partition par = ::blkid_partlist_get_partition(partlist, i); + + const char *n = ::blkid_partition_get_name(par); + if (!n) + break; + + if (::strcasecmp(n, name.c_str()) == 0) { + partno = ::blkid_partition_get_partno(par); + break; + } + } + + ::blkid_free_probe(probe); + + if (partno <= 0) { + throw std::runtime_error("Failed to get partition number with " + name); + } + + return device + "p" + std::to_string(partno); + } + +private: + std::string device; + int deviceHandle; +}; + // dummy implementation ProgressBar::ProgressBar(UpdateFunc const&) : updateValue(0) {} ProgressBar::~ProgressBar() {} @@ -44,8 +120,6 @@ namespace { const std::string MOUNT = "mount"; const std::string REMOVE = "remove"; -const std::string DEV_PATH = ode::findDevPath(); - void usage() { std::cout << @@ -68,15 +142,18 @@ int main(int argc, char* argv[]) return EXIT_FAILURE; } + MmcDevice dev(0); + std::string devpath = dev.findNode("user"); + if (MOUNT == argv[1]) { - auto masterKey = UpgradeSupport::loadMasterKey(DEV_PATH); + auto masterKey = UpgradeSupport::loadMasterKey(devpath); - DMCryptEngine dmcrypt(DEV_PATH, INTERNAL_PATH, ProgressBar([](int){})); + DMCryptEngine dmcrypt(devpath, INTERNAL_PATH, ProgressBar([](int){})); // mount options are ignored by mount() dmcrypt.mount(masterKey, 0); } else if (REMOVE == argv[1]) { - UpgradeSupport::removeMasterKey(DEV_PATH); + UpgradeSupport::removeMasterKey(devpath); } else { usage(); return EXIT_FAILURE; diff --git a/packaging/ode.spec b/packaging/ode.spec index 3ba6cd9..ab1305d 100755 --- a/packaging/ode.spec +++ b/packaging/ode.spec @@ -18,6 +18,7 @@ BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(cynara-client) BuildRequires: pkgconfig(libcrypto) BuildRequires: pkgconfig(libsmack) +BuildRequires: pkgconfig(blkid) Requires: cryptsetup %global key_storage_plugin_dir %{_libdir}/ode-key-storage-plugin/ -- 2.7.4