Use blkid to identify partition 46/183246/5 submit/tizen_4.0/20180704.023839
authorJaemin Ryu <jm77.ryu@samsung.com>
Wed, 4 Jul 2018 01:15:04 +0000 (10:15 +0900)
committerseolheui kim <s414.kim@samsung.com>
Wed, 4 Jul 2018 02:32:42 +0000 (11:32 +0900)
Change-Id: I16f5681b21e87a65d77b642d14f102f71f8605b8
Signed-off-by: Jaemin Ryu <jm77.ryu@samsung.com>
fota/CMakeLists.txt
fota/fota.cpp
packaging/ode.spec

index 2d2e71f..af7098f 100755 (executable)
@@ -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
index c747af7..8ea9967 100644 (file)
  *  See the License for the specific language governing permissions and
  *  limitations under the License
  */
-
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
 #include <stdlib.h>
+#include <strings.h>
+#include <blkid.h>
 
 #include <string>
 #include <iostream>
@@ -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;
index 3ba6cd9..ab1305d 100755 (executable)
@@ -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/