14e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park/** Mobicore Driver Registry.
24e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park *
34e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * Implements the MobiCore driver registry which maintains trustlets.
44e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park *
54e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * @file
64e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * @ingroup MCD_MCDIMPL_DAEMON_REG
74e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * <!-- Copyright Giesecke & Devrient GmbH 2009 - 2012 -->
87b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim *
94e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * Redistribution and use in source and binary forms, with or without
104e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * modification, are permitted provided that the following conditions
114e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * are met:
124e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * 1. Redistributions of source code must retain the above copyright
134e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park *    notice, this list of conditions and the following disclaimer.
144e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * 2. Redistributions in binary form must reproduce the above copyright
154e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park *    notice, this list of conditions and the following disclaimer in the
164e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park *    documentation and/or other materials provided with the distribution.
174e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * 3. The name of the author may not be used to endorse or promote
184e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park *    products derived from this software without specific prior
194e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park *    written permission.
204e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park *
214e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
224e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
234e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
254e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
274e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
284e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
294e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
304e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
314e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
324e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park */
334e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
344e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park#include "MobiCoreRegistry.h"
354e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park#include <stdlib.h>
364e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park#include <dirent.h>
379081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim#include <stdio.h>
384e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park#include <sys/stat.h>
394e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park#include <assert.h>
404e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park#include <string>
414e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park#include <cstring>
424e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park#include <cstddef>
434e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park#include "mcLoadFormat.h"
444e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park#include "mcSpid.h"
454e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park#include "mcVersionHelper.h"
464e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
474e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park#include "log.h"
484e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
494e9e8c9c0169b40318386436d762c3d73cf4c328DongJin ParkMC_CHECK_DATA_OBJECT_VERSION(MCLF, 2, 0);
504e9e8c9c0169b40318386436d762c3d73cf4c328DongJin ParkMC_CHECK_DATA_OBJECT_VERSION(CONTAINER, 2, 0);
514e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
524e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park// Asserts expression at compile-time (to be used within a function body).
534e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park#define ASSERT_STATIC(e) do { enum { assert_static__ = 1 / (e) }; } while (0)
544e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
554e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Parkusing namespace std;
564e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
574e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Parkstatic const string MC_REGISTRY_DEFAULT_PATH = "/data/app/mcRegistry";
584e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Parkstatic const string AUTH_TOKEN_FILE_NAME = "00000000.authtokcont";
594e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Parkstatic const string ROOT_FILE_NAME = "00000000.rootcont";
604e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Parkstatic const string SP_CONT_FILE_EXT = ".spcont";
614e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Parkstatic const string TL_CONT_FILE_EXT = ".tlcont";
624e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Parkstatic const string TL_BIN_FILE_EXT = ".tlbin";
634e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Parkstatic const string DATA_CONT_FILE_EXT = ".datacont";
644e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
654e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Parkstatic const string ENV_MC_REGISTRY_PATH = "MC_REGISTRY_PATH";
664e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Parkstatic const string ENV_MC_REGISTRY_FALLBACK_PATH = "MC_REGISTRY_FALLBACK_PATH";
674e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Parkstatic const string ENV_MC_AUTH_TOKEN_PATH = "MC_AUTH_TOKEN_PATH";
684e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
694e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Parkstatic const string getRegistryPath();
704e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Parkstatic const string getAuthTokenFilePath();
714e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Parkstatic const string getRootContFilePath();
724e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Parkstatic const string getSpDataPath(mcSpid_t spid);
734e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Parkstatic const string getSpContFilePath(mcSpid_t spid);
747b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kimstatic const string getTlContFilePath(const mcUuid_t *uuid);
757b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kimstatic const string getTlDataPath(const mcUuid_t *uuid);
767b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kimstatic const string getTlDataFilePath(const mcUuid_t *uuid, mcPid_t pid);
777b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kimstatic const string getTlBinFilePath(const mcUuid_t *uuid);
784e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
794e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Parkstatic const string uint32ToString(mcSpid_t spid);
807b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kimstatic const string byteArrayToString(const void *bytes, size_t elems);
817b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kimstatic bool doesDirExist(const char *path);
824e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
834e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
844e9e8c9c0169b40318386436d762c3d73cf4c328DongJin ParkmcResult_t mcRegistryStoreAuthToken(
857b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const mcSoAuthTokenCont_t *so
867b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim)
877b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
884e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (NULL == so) {
894e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry store So.Soc failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
904e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
914e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
924e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (CONT_TYPE_SOC != so->coSoc.type) {
934e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry store So.Soc failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
944e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
954e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
967b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const string &authTokenFilePath = getAuthTokenFilePath();
974e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    LOG_I("store AuthToken: %s", authTokenFilePath.c_str());
989081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
999081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    FILE *fs = fopen(authTokenFilePath.c_str(), "wb");
1004e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (!fs) {
1014e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry store So.Soc failed: %d", MC_DRV_ERR_INVALID_DEVICE_FILE);
1024e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_DEVICE_FILE;
1034e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
1049081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, 0, SEEK_SET);
1059081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fwrite((char *)so, 1, sizeof(mcSoAuthTokenCont_t), fs);
1069081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fflush(fs);
1079081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fclose(fs);
1089081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
1094e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return MC_DRV_OK;
1104e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
1114e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
1124e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
1134e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
1144e9e8c9c0169b40318386436d762c3d73cf4c328DongJin ParkmcResult_t mcRegistryReadAuthToken(
1157b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    mcSoAuthTokenCont_t *so
1167b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim)
1177b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
1184e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (NULL == so) {
1194e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry read So.Soc failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
1204e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
1214e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
1227b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const string &authTokenFilePath = getAuthTokenFilePath();
1234e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    LOG_I("read AuthToken: %s", authTokenFilePath.c_str());
1249081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
1259081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    FILE *fs = fopen(authTokenFilePath.c_str(), "rb");
1264e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (!fs) {
1274e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry read So.Soc failed: %d", MC_DRV_ERR_INVALID_DEVICE_FILE);
1284e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_DEVICE_FILE;
1294e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
1309081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, 0, SEEK_END);
1319081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    int32_t filesize = ftell(fs);
1324e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (sizeof(mcSoAuthTokenCont_t) != filesize) {
1339081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        fclose(fs);
1344e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry read So.Soc failed: %d", MC_DRV_ERR_OUT_OF_RESOURCES);
1354e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_OUT_OF_RESOURCES;
1364e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
1379081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, 0, SEEK_SET);
1389081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fread((char *)so, 1, sizeof(mcSoAuthTokenCont_t), fs);
1399081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fclose(fs);
1409081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
1414e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return MC_DRV_OK;
1424e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
1434e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
1444e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
1454e9e8c9c0169b40318386436d762c3d73cf4c328DongJin ParkmcResult_t mcRegistryDeleteAuthToken(
1467b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    void
1477b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim)
1487b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
1494e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    remove(getAuthTokenFilePath().c_str());
1504e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    // @TODO: is return to check ?
1514e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return MC_DRV_OK;
1524e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
1534e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
1544e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
1554e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
1564e9e8c9c0169b40318386436d762c3d73cf4c328DongJin ParkmcResult_t mcRegistryStoreRoot(
1577b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const mcSoRootCont_t *so
1587b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim)
1597b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
1604e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (NULL == so) {
1614e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry store So.Root failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
1624e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
1634e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
1647b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    if (CONT_TYPE_ROOT != so->cont.type) {
1654e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry store So.Root failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
1664e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
1674e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
1687b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const string &rootContFilePath = getRootContFilePath();
1694e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    LOG_I("store Root: %s", rootContFilePath.c_str());
1709081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
1719081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    FILE *fs = fopen(rootContFilePath.c_str(), "wb");
1724e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (!fs) {
1734e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry store So.Root failed: %d", MC_DRV_ERR_INVALID_DEVICE_FILE);
1744e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_DEVICE_FILE;
1754e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
1769081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, 0, SEEK_SET);
1779081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fwrite((char *)so, 1, sizeof(mcSoRootCont_t), fs);
1789081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fflush(fs);
1799081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fclose(fs);
1809081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
1814e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return MC_DRV_OK;
1824e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
1834e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
1844e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
1854e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
1864e9e8c9c0169b40318386436d762c3d73cf4c328DongJin ParkmcResult_t mcRegistryReadRoot(
1877b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    mcSoRootCont_t *so
1887b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim)
1897b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
1904e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (NULL == so) {
1914e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry read So.Root failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
1924e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
1934e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
1947b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const string &rootContFilePath = getRootContFilePath();
1954e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    LOG_I("read Root: %s", rootContFilePath.c_str());
1969081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
1979081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    FILE *fs = fopen(rootContFilePath.c_str(), "rb");
1984e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (!fs) {
1994e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry read So.Root failed: %d", MC_DRV_ERR_INVALID_DEVICE_FILE);
2004e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_DEVICE_FILE;
2014e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
2029081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, 0, SEEK_END);
2039081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    int32_t filesize = ftell(fs);
2044e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (sizeof(mcSoRootCont_t) != filesize) {
2059081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        fclose(fs);
2064e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry read So.Root failed: %d", MC_DRV_ERR_OUT_OF_RESOURCES);
2074e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_OUT_OF_RESOURCES;
2084e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
2099081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, 0, SEEK_SET);
2109081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fread((char *)so, 1, sizeof(mcSoRootCont_t), fs);
2119081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fclose(fs);
2129081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
2134e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return MC_DRV_OK;
2144e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
2154e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
2164e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
2174e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
2184e9e8c9c0169b40318386436d762c3d73cf4c328DongJin ParkmcResult_t mcRegistryStoreSp(
2197b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    mcSpid_t            spid,
2207b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const mcSoSpCont_t  *so
2217b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim)
2227b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
2234e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if ((0 == spid) || (NULL == so)) {
2244e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry store So.Sp(SpId) failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
2254e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
2264e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
2277b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    if (CONT_TYPE_SP != so->cont.type) {
2284e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry store So.Sp(SpId) failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
2294e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
2304e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
2317b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const string &spContFilePath = getSpContFilePath(spid);
2324e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    LOG_I("store SP: %s", spContFilePath.c_str());
2339081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
2349081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    FILE *fs = fopen(spContFilePath.c_str(), "wb");
2354e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (!fs) {
2364e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry store So.Sp(SpId) failed: %d", MC_DRV_ERR_INVALID_DEVICE_FILE);
2374e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_DEVICE_FILE;
2384e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
2399081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, 0, SEEK_SET);
2409081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fwrite((char *)so, 1, sizeof(mcSoSpCont_t), fs);
2419081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fflush(fs);
2429081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fclose(fs);
2439081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
2444e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return MC_DRV_OK;
2454e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
2464e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
2474e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
2484e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
2494e9e8c9c0169b40318386436d762c3d73cf4c328DongJin ParkmcResult_t mcRegistryReadSp(
2507b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    mcSpid_t        spid,
2517b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    mcSoSpCont_t    *so
2527b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim)
2537b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
2544e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if ((0 == spid) || (NULL == so)) {
2554e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry read So.Sp(SpId) failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
2564e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
2574e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
2587b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const string &spContFilePath = getSpContFilePath(spid);
2594e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    LOG_I("read SP: %s", spContFilePath.c_str());
2609081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
2619081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    FILE *fs = fopen(spContFilePath.c_str(), "rb");
2624e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (!fs) {
2634e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry read So.Sp(SpId) failed: %d", MC_DRV_ERR_INVALID_DEVICE_FILE);
2644e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_DEVICE_FILE;
2654e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
2669081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, 0, SEEK_END);
2679081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    int32_t filesize = ftell(fs);
2684e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (sizeof(mcSoSpCont_t) != filesize) {
2699081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        fclose(fs);
2704e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry read So.Sp(SpId) failed: %d", MC_DRV_ERR_OUT_OF_RESOURCES);
2714e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_OUT_OF_RESOURCES;
2724e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
2739081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, 0, SEEK_SET);
2749081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fread((char *)so, 1, sizeof(mcSoSpCont_t), fs);
2759081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fclose(fs);
2769081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
2774e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return MC_DRV_OK;
2784e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
2794e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
2804e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
2814e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
2824e9e8c9c0169b40318386436d762c3d73cf4c328DongJin ParkmcResult_t mcRegistryStoreTrustletCon(
2837b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const mcUuid_t      *uuid,
2847b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const mcSoTltCont_t *so
2857b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim)
2867b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
2874e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if ((NULL == uuid) || (NULL == so)) {
2884e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry store So.TrustletCont(uuid) failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
2894e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
2904e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
2917b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    if (CONT_TYPE_TLCON != so->cont.type) {
2924e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry store So.TrustletCont(uuid) failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
2934e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
2944e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
2957b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const string &tlContFilePath = getTlContFilePath(uuid);
2964e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    LOG_I("store TLc: %s", tlContFilePath.c_str());
2979081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
2989081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    FILE *fs = fopen(tlContFilePath.c_str(), "wb");
2994e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (!fs) {
3004e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry store So.TrustletCont(uuid) failed: %d", MC_DRV_ERR_INVALID_DEVICE_FILE);
3014e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_DEVICE_FILE;
3024e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
3039081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, 0, SEEK_SET);
3049081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fwrite((char *)so, 1, sizeof(mcSoTltCont_t), fs);
3059081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fflush(fs);
3069081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fclose(fs);
3079081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
3084e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return MC_DRV_OK;
3094e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
3104e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
3114e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
3124e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
3134e9e8c9c0169b40318386436d762c3d73cf4c328DongJin ParkmcResult_t mcRegistryReadTrustletCon(
3147b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const mcUuid_t  *uuid,
3157b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    mcSoTltCont_t   *so
3167b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim)
3177b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
3184e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if ((NULL == uuid) || (NULL == so)) {
3194e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry read So.TrustletCont(uuid) failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
3204e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
3214e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
3227b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const string &tlContFilePath = getTlContFilePath(uuid);
3234e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    LOG_I("read TLc: %s", tlContFilePath.c_str());
3249081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
3259081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    FILE *fs = fopen(tlContFilePath.c_str(), "rb");
3264e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (!fs) {
3274e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry read So.TrustletCont(uuid) failed: %d", MC_DRV_ERR_INVALID_DEVICE_FILE);
3284e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_DEVICE_FILE;
3294e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
3309081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, 0, SEEK_END);
3319081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    int32_t filesize = ftell(fs);
3324e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (sizeof(mcSoTltCont_t) != filesize) {
3339081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        fclose(fs);
3347b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        LOG_E("mcRegistry read So.TrustletCont(uuid) failed: %d. Size=%i, expected=%i", MC_DRV_ERR_OUT_OF_RESOURCES, filesize, sizeof(mcSoTltCont_t));
3354e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_OUT_OF_RESOURCES;
3364e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
3379081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, 0, SEEK_SET);
3389081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fread((char *)so, 1, sizeof(mcSoTltCont_t), fs);
3399081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fclose(fs);
3409081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
3414e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return MC_DRV_OK;
3424e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
3434e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
3444e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
3454e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
3464e9e8c9c0169b40318386436d762c3d73cf4c328DongJin ParkmcResult_t mcRegistryStoreData(
3477b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const mcSoDataCont_t *so
3487b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim)
3497b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
3504e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (NULL == so) {
3514e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry store So.Data failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
3524e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
3534e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
3544e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    string pathname, filename;
3554e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
3564e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    switch (so->cont.type) {
3574e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    case CONT_TYPE_SPDATA:
3584e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("SPDATA not supported");
3594e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
3604e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        break;
3614e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    case CONT_TYPE_TLDATA:
3624e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        pathname = getTlDataPath(&so->cont.uuid);
3634e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        filename = getTlDataFilePath(&so->cont.uuid, so->cont.pid);
3644e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        break;
3654e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    default:
3664e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry store So.Data(cid/pid) failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
3674e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
3684e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
3694e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    mkdir(pathname.c_str(), 0777);
3704e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
3714e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    LOG_I("store DT: %s", filename.c_str());
3729081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
3739081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    FILE *fs = fopen(filename.c_str(), "wb");
3744e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (!fs) {
3754e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry store So.Data(cid/pid) failed: %d", MC_DRV_ERR_INVALID_DEVICE_FILE);
3764e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_DEVICE_FILE;
3774e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
3789081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, 0, SEEK_SET);
3799081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fwrite((char *)so, 1, MC_SO_SIZE(so->soHeader.plainLen, so->soHeader.encryptedLen), fs);
3809081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fflush(fs);
3819081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fclose(fs);
3829081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
3834e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return MC_DRV_OK;
3844e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
3854e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
3864e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
3874e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
3884e9e8c9c0169b40318386436d762c3d73cf4c328DongJin ParkmcResult_t mcRegistryReadData(
3897b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    uint32_t        context,
3907b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const mcCid_t   *cid,
3917b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    mcPid_t         pid,
3927b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    mcSoDataCont_t  *so,
3937b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    uint32_t        maxLen
3947b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim)
3957b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
3964e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if ((NULL == cid) || (NULL == so)) {
3974e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry read So.Data failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
3984e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
3994e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
4004e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    string filename;
4014e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    switch (context) {
4024e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    case 0:
4034e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("SPDATA not supported");
4044e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
4054e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        break;
4064e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    case 1:
4074e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        filename = getTlDataFilePath(&so->cont.uuid, so->cont.pid);
4084e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        break;
4094e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    default:
4104e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry read So.Data(cid/pid) failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
4114e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
4124e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
4134e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    LOG_I("read DT: %s", filename.c_str());
4149081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
4159081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    FILE *fs = fopen(filename.c_str(), "rb");
4164e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (!fs) {
4174e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry read So.Data(cid/pid) failed: %d", MC_DRV_ERR_INVALID_DEVICE_FILE);
4184e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_DEVICE_FILE;
4194e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
4209081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, 0, SEEK_END);
4219081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    uint32_t filesize = ftell(fs);
4224e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (maxLen < filesize) {
4239081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        fclose(fs);
4244e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry read So.Data(cid/pid) failed: %d", MC_DRV_ERR_OUT_OF_RESOURCES);
4254e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_OUT_OF_RESOURCES;
4264e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
4279081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, 0, SEEK_SET);
4287b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    char *p = (char *) so;
4299081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fread(p, 1, sizeof(mcSoHeader_t), fs);
4304e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    p += sizeof(mcSoHeader_t);
4319081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fread(p, 1, MC_SO_SIZE(so->soHeader.plainLen, so->soHeader.encryptedLen) - sizeof(mcSoHeader_t), fs);
4329081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fclose(fs);
4339081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
4344e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return MC_DRV_OK;
4354e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
4364e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
4374e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
4384e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
4394e9e8c9c0169b40318386436d762c3d73cf4c328DongJin ParkmcResult_t mcRegistryCleanupTrustlet(
4407b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const mcUuid_t *uuid
4417b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim)
4427b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
4434e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    DIR            *dp;
4444e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    struct dirent  *de;
4454e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    int             e;
4464e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
4474e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (NULL == uuid) {
4484e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry cleanupTrustlet(uuid) failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
4494e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
4504e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
4514e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    string pathname = getTlDataPath(uuid);
4524e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (NULL != (dp = opendir(pathname.c_str()))) {
4534e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        while (NULL != (de = readdir(dp))) {
4544e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            if (de->d_name[0] != '.') {
4554e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                string dname = pathname + "/" + string (de->d_name);
4564e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                LOG_I("delete DT: %s", dname.c_str());
4574e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                if (0 != (e = remove(dname.c_str()))) {
4584e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                    LOG_E("remove UUID-data %s failed! error: %d", dname.c_str(), e);
4594e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                    return MC_DRV_ERR_UNKNOWN;
4604e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                }
4614e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            }
4624e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        }
4634e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_I("delete dir: %s", pathname.c_str());
4644e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        if (0 != (e = rmdir(pathname.c_str()))) {
4654e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            LOG_E("remove UUID-dir failed! errno: %d", e);
4664e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            return MC_DRV_ERR_UNKNOWN;
4674e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        }
4684e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
4694e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    string tlBinFilePath = getTlBinFilePath(uuid);
4704e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    LOG_I("delete Tlb: %s", tlBinFilePath.c_str());
4714e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (0 != (e = remove(tlBinFilePath.c_str()))) {
4724e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("remove Tlb failed! errno: %d", e);
4734e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//        return MC_DRV_ERR_UNKNOWN;     // a trustlet-binary must not be present ! (registered but not usable)
4744e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
4754e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    string tlContFilePath = getTlContFilePath(uuid);
4764e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    LOG_I("delete Tlc: %s", tlContFilePath.c_str());
4774e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (0 != (e = remove(tlContFilePath.c_str()))) {
4784e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("remove Tlc failed! errno: %d", e);
4794e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_UNKNOWN;
4804e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
4814e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return MC_DRV_OK;
4824e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
4834e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
4844e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
4854e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
4864e9e8c9c0169b40318386436d762c3d73cf4c328DongJin ParkmcResult_t mcRegistryCleanupSp(
4877b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    mcSpid_t spid
4887b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim)
4897b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
4904e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    mcResult_t      ret;
4914e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    mcSoSpCont_t    data;
4924e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    uint32_t        i;
4934e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    DIR            *dp;
4944e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    struct dirent  *de;
4954e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    int             e;
4964e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
4974e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (0 == spid) {
4984e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistry cleanupSP(SpId) failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
4994e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_INVALID_PARAMETER;
5004e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
5014e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    ret = mcRegistryReadSp(spid, &data);
5024e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (MC_DRV_OK != ret) {
5034e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("read SP->UUID aborted! Return code: %d", ret);
5044e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return ret;
5054e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
5064e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    for (i = 0; (i < MC_CONT_CHILDREN_COUNT) && (ret == MC_DRV_OK); i++) {
5077b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        if (0 != strncmp((const char *) & (data.cont.children[i]), (const char *)&MC_UUID_FREE, sizeof(mcUuid_t))) {
5084e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            ret = mcRegistryCleanupTrustlet(&(data.cont.children[i]));
5094e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        }
5104e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
5114e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (MC_DRV_OK != ret) {
5124e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("delete SP->UUID failed! Return code: %d", ret);
5134e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return ret;
5144e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
5154e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    string pathname = getSpDataPath(spid);
5164e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
5174e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (NULL != (dp = opendir(pathname.c_str()))) {
5184e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        while (NULL != (de = readdir(dp))) {
5194e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            if (de->d_name[0] != '.') {
5204e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                string dname = pathname + "/" + string (de->d_name);
5214e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                LOG_I("delete DT: %s", dname.c_str());
5224e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                if (0 != (e = remove(dname.c_str()))) {
5234e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                    LOG_E("remove SPID-data %s failed! error: %d", dname.c_str(), e);
5244e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                    return MC_DRV_ERR_UNKNOWN;
5254e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                }
5264e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            }
5274e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        }
5284e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_I("delete dir: %s", pathname.c_str());
5294e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        if (0 != (e = rmdir(pathname.c_str()))) {
5304e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            LOG_E("remove SPID-dir failed! error: %d", e);
5314e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            return MC_DRV_ERR_UNKNOWN;
5324e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        }
5334e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
5344e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    string spContFilePath = getSpContFilePath(spid);
5354e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    LOG_I("delete Sp: %s", spContFilePath.c_str());
5364e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (0 != (e = remove(spContFilePath.c_str()))) {
5374e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("remove SP failed! error: %d", e);
5384e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_UNKNOWN;
5394e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
5404e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return MC_DRV_OK;
5414e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
5424e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
5434e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
5444e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
5457b143edf281bed18c8ebd0733465f3af5af327ebJungtae KimmcResult_t mcRegistryCleanupRoot(void)
5467b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
5474e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    mcResult_t ret;
5484e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    mcSoRootCont_t data;
5494e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    uint32_t i;
5504e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    int e;
5514e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
5524e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    ret = mcRegistryReadRoot(&data);
5534e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (MC_DRV_OK != ret) {
5544e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("read Root aborted! Return code: %d", ret);
5554e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return ret;
5564e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
5574e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    for (i = 0; (i < MC_CONT_CHILDREN_COUNT) && (ret == MC_DRV_OK); i++) {
5584e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        mcSpid_t spid = data.cont.children[i];
5594e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        if (spid != MC_SPID_FREE) {
5604e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            ret = mcRegistryCleanupSp(spid);
5614e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            if (MC_DRV_OK != ret) {
5624e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                LOG_E("Cleanup SP failed! Return code: %d", ret);
5634e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                return ret;
5644e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            }
5654e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        }
5664e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
5674e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
5684e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    string rootContFilePath = getRootContFilePath();
5694e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    LOG_I("Delete root: %s", rootContFilePath.c_str());
5704e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (0 != (e = remove(rootContFilePath.c_str()))) {
5714e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("Delete root failed! error: %d", e);
5724e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return MC_DRV_ERR_UNKNOWN;
5734e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
5744e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return MC_DRV_OK;
5754e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
5764e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
5774e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
5784e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
5794e9e8c9c0169b40318386436d762c3d73cf4c328DongJin ParkregObject_t *mcRegistryGetServiceBlob(
5807b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const mcUuid_t *uuid
5817b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim)
5827b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
5837b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    regObject_t *regobj = NULL;
5844e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
5854e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    // Ensure that a UUID is provided.
5864e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (NULL == uuid) {
5879081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        LOG_E("No UUID given");
5884e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return NULL;
5894e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
5904e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
5914e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    // Open service blob file.
5924e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    string tlBinFilePath = getTlBinFilePath(uuid);
5939081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    LOG_I(" Loading %s", tlBinFilePath.c_str());
5944e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
5959081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    FILE *fs = fopen(tlBinFilePath.c_str(), "rb");
5964e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (!fs) {
5979081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        LOG_E("Cannot open %s", tlBinFilePath.c_str());
5984e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return NULL;
5994e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
6004e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
6014e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    // Determine and check service blob size.
6029081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, 0, SEEK_END);
6039081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    int32_t tlSize = ftell(fs);
6049081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, 0, SEEK_SET);
6054e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (MAX_TL_SIZE < tlSize) {
6064e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistryGetServiceBlob() failed: service blob too big: %d", tlSize);
6074e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return NULL;
6084e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
6094e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
6104e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    // Check TL magic value.
6119081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, offsetof(mclfIntro_t, magic), SEEK_SET);
6124e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    uint32_t magic;
6137b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    fread((char *)&magic, 1, sizeof(magic), fs);
6144e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (magic != MC_SERVICE_HEADER_MAGIC_BE) {
6159081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        fclose(fs);
6164e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistryGetServiceBlob() failed: wrong header magic value: %d", magic);
6174e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return NULL;
6184e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
6194e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
6204e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    // Check header version.
6219081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, offsetof(mclfIntro_t, version), SEEK_SET);
6224e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    uint32_t version;
6237b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    fread((char *)&version, 1, sizeof(version), fs);
6244e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
6257b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    char *msg;
6264e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (!checkVersionOkDataObjectMCLF(version, &msg)) {
6279081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        fclose(fs);
6289081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        LOG_E("%s", msg);
6299081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        return NULL;
6304e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
6314e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
6324e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    // Get service type.
6337b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    fseek(fs, offsetof(mclfHeaderV2_t, serviceType), SEEK_SET);
6344e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    serviceType_t serviceType;
6357b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    fread((char *)&serviceType, 1, sizeof(serviceType), fs);
6369081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fseek(fs, 0, SEEK_SET);
6374e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
6389081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim#ifndef NDEBUG
6399081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    {
6407b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        const char *service_types[] = {
6417b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim            "illegal", "Driver", "Trustlet", "System Trustlet"
6429081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        };
6437b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        int serviceType_safe = serviceType > SERVICE_TYPE_SYSTEM_TRUSTLET ? SERVICE_TYPE_ILLEGAL : serviceType;
6449081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        LOG_I(" Service is a %s (service type %d)", service_types[serviceType_safe], serviceType);
6459081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    }
6469081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim#endif
6474e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
6484e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    // If loadable driver or system trustlet.
6494e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (SERVICE_TYPE_DRIVER == serviceType || SERVICE_TYPE_SYSTEM_TRUSTLET == serviceType) {
6504e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        // Take trustlet blob 'as is'.
6517b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        if (NULL == (regobj = (regObject_t *) (malloc(sizeof(regObject_t) + tlSize)))) {
6529081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim            fclose(fs);
6534e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            LOG_E("mcRegistryGetServiceBlob() failed: Out of memory");
6544e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            return NULL;
6554e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        }
6564e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        regobj->len = tlSize;
6579081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        fread((char *)regobj->value, 1, tlSize, fs);
6589081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        fclose(fs);
6597b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        // If user trustlet.
6604e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    } else if (SERVICE_TYPE_SP_TRUSTLET == serviceType) {
6614e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        // Take trustlet blob and append root, sp, and tl container.
6624e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        size_t regObjValueSize = tlSize + sizeof(mcSoContainerPath_t);
6634e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
6644e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        // Prepare registry object.
6657b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        if (NULL == (regobj = (regObject_t *) malloc(sizeof(regObject_t) + regObjValueSize))) {
6669081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim            fclose(fs);
6674e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            LOG_E("mcRegistryGetServiceBlob() failed: Out of memory");
6684e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            return NULL;
6694e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        }
6704e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        regobj->len = regObjValueSize;
6714e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
6724e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        // Read and fill in trustlet blob at beginning.
6737b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        fread((char *)regobj->value, 1, tlSize, fs);
6749081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        fclose(fs);
6754e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
6764e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        // Goto end of allocated space and fill in tl container, sp container,
6774e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        // and root container from back to front. Final registry object value
6784e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        // looks like this:
6797b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        //
6804e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        //    +---------------------------+-----------+---------+---------+
6814e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        //    | TL-Header TL-Code TL-Data | Root Cont | SP Cont | TL Cont |
6824e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        //    +---------------------------+-----------+-------------------+
6834e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        //    /------ Trustlet BLOB ------/
6844e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        //
6854e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        //    /------------------ regobj->header.len ---------------------/
6864e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
6877b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        uint8_t *p = regobj->value + regobj->len;
6884e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        mcResult_t ret;
6894e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        do {
6907b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim            char *msg;
6914e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
6924e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            // Fill in TL container.
6934e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            p -= sizeof(mcSoTltCont_t);
6947b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim            mcSoTltCont_t *soTlt = (mcSoTltCont_t *)p;
6954e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            if (MC_DRV_OK != (ret = mcRegistryReadTrustletCon(uuid, soTlt))) {
6964e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                break;
6974e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            }
6987b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim            mcTltCont_t *tltCont = &soTlt->cont;
6994e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            if (!checkVersionOkDataObjectCONTAINER(tltCont->version, &msg)) {
7004e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                LOG_E("Tlt container %s", msg);
7014e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                ret = MC_DRV_ERR_CONTAINER_VERSION;
7024e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                break;
7034e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            }
7044e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
7054e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            // Fill in SP container.
7064e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            mcSpid_t spid = tltCont->parent;
7074e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            p -= sizeof(mcSoSpCont_t);
7087b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim            mcSoSpCont_t *soSp = (mcSoSpCont_t *)p;
7094e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            if (MC_DRV_OK != (ret = mcRegistryReadSp(spid, soSp))) {
7104e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                break;
7114e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            }
7127b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim            mcSpCont_t *spCont = &soSp->cont;
7134e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            if (!checkVersionOkDataObjectCONTAINER(spCont->version, &msg)) {
7144e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                LOG_E("SP container %s", msg);
7154e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                ret = MC_DRV_ERR_CONTAINER_VERSION;
7164e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                break;
7174e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            }
7184e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
7194e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            // Fill in root container.
7204e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            p -= sizeof(mcSoRootCont_t);
7217b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim            mcSoRootCont_t *soRoot = (mcSoRootCont_t *)p;
7224e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            if (MC_DRV_OK != (ret = mcRegistryReadRoot(soRoot))) {
7234e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                break;
7244e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            }
7257b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim            mcRootCont_t *rootCont = &soRoot->cont;
7264e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            if (!checkVersionOkDataObjectCONTAINER(rootCont->version, &msg)) {
7274e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                LOG_E("Root container %s", msg);
7284e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                ret = MC_DRV_ERR_CONTAINER_VERSION;
7294e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park                break;
7304e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            }
7314e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
7324e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            // Ensure order of elements in registry object value.
7337b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim            assert(p - tlSize - sizeof(regObject_t) == (uint8_t *)regobj);
7344e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        } while (false);
7354e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
7364e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        if (MC_DRV_OK != ret) {
7374e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            LOG_E("mcRegistryGetServiceBlob() failed: Error code: %d", ret);
7384e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            free(regobj);
7394e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            return NULL;
7404e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        }
7417b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        // Any other service type.
7424e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    } else {
7439081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        fclose(fs);
7444e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_E("mcRegistryGetServiceBlob() failed: Unsupported service type %u", serviceType);
7454e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
7464e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
7474e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return regobj;
7484e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
7494e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
7504e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
7514e9e8c9c0169b40318386436d762c3d73cf4c328DongJin ParkregObject_t *mcRegistryGetDriverBlob(
7527b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const char *driverFilename
7537b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim)
7547b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
7557b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    regObject_t *regobj = NULL;
7569081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
7579081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    // Open service blob file.
7587b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    FILE *fs = fopen(driverFilename, "rb");
7597b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    if (!fs) {
7607b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        LOG_E("mcRegistryGetDriverBlob() failed: cannot open %s", driverFilename);
7617b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        return NULL;
7627b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    }
7637b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim
7647b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    // Determine and check service blob size.
7657b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    fseek(fs, 0, SEEK_END);
7667b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    int32_t tlSize = ftell(fs);
7677b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    fseek(fs, 0, SEEK_SET);
7687b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    if (MAX_TL_SIZE < tlSize) {
7697b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        LOG_E("mcRegistryGetDriverBlob() failed: service blob too big: %d", tlSize);
7707b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        fclose(fs);
7717b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        return NULL;
7727b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    }
7737b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim
7747b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    // Check TL magic value.
7757b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    fseek(fs, offsetof(mclfIntro_t, magic), SEEK_SET);
7767b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    uint32_t magic;
7777b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    fread((char *)&magic, 1, sizeof(magic), fs);
7787b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    if (magic != MC_SERVICE_HEADER_MAGIC_BE) {
7797b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        LOG_E("mcRegistryGetDriverBlob() failed: wrong header magic value: %d", magic);
7807b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        fclose(fs);
7817b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        return NULL;
7827b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    }
7837b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim
7847b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    // Check header version.
7857b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    fseek(fs, offsetof(mclfIntro_t, version), SEEK_SET);
7867b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    uint32_t version;
7877b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    fread((char *)&version, 1, sizeof(version), fs);
7887b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim
7897b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    char *msg;
7904e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (!checkVersionOkDataObjectMCLF(version, &msg)) {
7919081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        LOG_E("%s", msg);
7929081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        fclose(fs);
7939081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        return NULL;
7944e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
7957b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim
7967b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    // Get service type.
7977b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    fseek(fs, offsetof(mclfHeaderV2_t, serviceType), SEEK_SET);
7987b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    serviceType_t serviceType;
7997b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    fread((char *)&serviceType, 1, sizeof(serviceType), fs);
8007b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    fseek(fs, 0, SEEK_SET);
8017b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim
8027b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    LOG_I("mcRegistryGetDriverBlob() Service is of type: %d", serviceType);
8037b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim
8047b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    // If loadable driver or system trustlet.
8057b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    if (SERVICE_TYPE_DRIVER == serviceType) {
8067b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        // Take trustlet blob 'as is'.
8077b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        if (NULL == (regobj = (regObject_t *) (malloc(sizeof(regObject_t) + tlSize)))) {
8087b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim            LOG_E("mcRegistryGetDriverBlob() failed: Out of memory");
8097b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim            fclose(fs);
8107b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim            return NULL;
8117b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        }
8127b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        regobj->len = tlSize;
8137b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        fread((char *)regobj->value, 1, tlSize, fs);
8147b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        // Otherwise we are not interested
8157b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    } else {
8167b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        LOG_E("mcRegistryGetServiceBlob() failed: Unsupported service type %u", serviceType);
8177b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    }
8189081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
8199081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim    fclose(fs);
8209081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim
8217b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    return regobj;
8224e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
8234e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
8244e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
8257b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kimstatic const string getRegistryPath()
8267b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
8277b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const char *path;
8284e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    string registryPath;
8294e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
8304e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    // First, attempt to use regular registry environment variable.
8314e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    path = getenv(ENV_MC_REGISTRY_PATH.c_str());
8324e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (doesDirExist(path)) {
8334e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_I("getRegistryPath(): Using MC_REGISTRY_PATH %s", path);
8344e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        registryPath = path;
8354e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    } else {
8364e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        // Second, attempt to use fallback registry environment variable.
8374e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        path = getenv(ENV_MC_REGISTRY_FALLBACK_PATH.c_str());
8384e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        if (doesDirExist(path)) {
8394e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            LOG_I("getRegistryPath(): Using MC_REGISTRY_FALLBACK_PATH %s", path);
8404e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park            registryPath = path;
8414e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        }
8424e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
8434e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
8444e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    // As a last resort, use the default registry path.
8454e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (registryPath.length() == 0) {
8464e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        registryPath = MC_REGISTRY_DEFAULT_PATH;
8479081ca65cb7959b6a06ba44823f84a6afa8bca2fJihyun Kim        LOG_I(" Using default registry path %s", registryPath.c_str());
8484e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
8494e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
8504e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    assert(registryPath.length() != 0);
8517b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim
8524e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return registryPath;
8534e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
8544e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
8554e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
8567b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kimstatic const string getAuthTokenFilePath()
8577b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
8587b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    const char *path;
8594e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    string authTokenPath;
8604e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
8614e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    // First, attempt to use regular auth token path environment variable.
8624e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    path = getenv(ENV_MC_AUTH_TOKEN_PATH.c_str());
8634e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (doesDirExist(path)) {
8644e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_I("getAuthTokenFilePath(): Using MC_AUTH_TOKEN_PATH %s", path);
8654e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        authTokenPath = path;
8664e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    } else {
8674e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        authTokenPath = getRegistryPath();
8684e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        LOG_I("getAuthTokenFilePath(): Using path %s", authTokenPath.c_str());
8694e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
8704e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
8714e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return authTokenPath + "/" + AUTH_TOKEN_FILE_NAME;
8724e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
8734e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
8744e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
8757b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kimstatic const string getRootContFilePath()
8767b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
8774e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return getRegistryPath() + "/" + ROOT_FILE_NAME;
8784e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
8794e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
8804e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
8817b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kimstatic const string getSpDataPath(mcSpid_t spid)
8827b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
8834e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return getRegistryPath() + "/" + uint32ToString(spid);
8844e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
8854e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
8864e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
8877b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kimstatic const string getSpContFilePath(mcSpid_t spid)
8887b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
8894e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return getRegistryPath() + "/" + uint32ToString(spid) + SP_CONT_FILE_EXT;
8904e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
8914e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
8924e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
8937b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kimstatic const string getTlContFilePath(const mcUuid_t *uuid)
8947b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
8954e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return getRegistryPath() + "/" + byteArrayToString(uuid, sizeof(*uuid)) + TL_CONT_FILE_EXT;
8964e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
8974e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
8984e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
8997b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kimstatic const string getTlDataPath(const mcUuid_t *uuid)
9007b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
9014e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return getRegistryPath() + "/" + byteArrayToString(uuid, sizeof(*uuid));
9024e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
9034e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
9044e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
9057b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kimstatic const string getTlDataFilePath(const mcUuid_t *uuid, mcPid_t pid)
9067b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
9074e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return getTlDataPath(uuid) + "/" + uint32ToString(pid.data) + DATA_CONT_FILE_EXT;
9084e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
9094e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
9104e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
9117b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kimstatic const string getTlBinFilePath(const mcUuid_t *uuid)
9127b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
9134e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return getRegistryPath() + "/" + byteArrayToString(uuid, sizeof(*uuid)) + TL_BIN_FILE_EXT;
9144e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
9154e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
9164e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
9177b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kimstatic const string byteArrayToString(const void *bytes, size_t elems)
9187b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
9194e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    char hx[elems * 2 + 1];
9204e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
9214e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    for (size_t i = 0; i < elems; i++) {
9227b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        sprintf(&hx[i * 2], "%02x", ((uint8_t *)bytes)[i]);
9237b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    }
9244e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return string(hx);
9254e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
9264e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
9274e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
9284e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Parkstatic const string uint32ToString(
9297b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    uint32_t value
9307b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim)
9317b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
9324e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    char hx[sizeof(uint32_t) * 2 + 1];
9334e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    uint32_t i;
9344e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
9354e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    for (i = 0; i < (2 * sizeof(value)); i++) {
9367b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        hx[i] = (value >> (28 - (i * 4))) & 0x0F;
9377b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        if (hx[i] > 9) {
9387b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim            hx[i] = (hx[i] - 9) | 0x40;
9397b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        } else {
9407b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim            hx[i] |= 0x30;
9417b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim        }
9427b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim    }
9434e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    hx[i] = '\0';
9444e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return string(hx);
9454e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
9464e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
9474e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park//------------------------------------------------------------------------------
9487b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kimstatic bool doesDirExist(const char *path)
9497b143edf281bed18c8ebd0733465f3af5af327ebJungtae Kim{
9504e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    struct stat ss;
9514e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    if (path != NULL && stat(path, &ss) == 0 && S_ISDIR(ss.st_mode)) {
9524e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park        return true;
9534e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    }
9544e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park    return false;
9554e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park}
9564e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
9574e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park
9584e9e8c9c0169b40318386436d762c3d73cf4c328DongJin Park/** @} */
959