#include <algorithm>
#include <boost/filesystem.hpp>
#include <config.h>
+
+namespace fs = boost::filesystem;
/*-----------------------------------------------------------------------------
* Globals
*-----------------------------------------------------------------------------*/
bool TABinaryManager::initTA(const string &uuid) {
LOGD(SIM_DAEMON, "Entry");
- pthread_rwlock_wrlock(&binaryMapLock);
+ string tapath;
+ const char * paths[] = { TEE_TASTORE_ROOT, TEE_EMBEDDED_TASTORE_ROOT, NULL };
+
+ for (int i = 0; paths[i] != NULL; ++i) {
+ if (*paths[i] == '\0') continue; // ignore empty paths
+ string path_to_file = paths[i] + uuid;
+ boost::system::error_code ec;
+ if (fs::exists(path_to_file, ec)) {
+ tapath = paths[i];
+ break;
+ }
+ }
+
+ if (tapath.empty()) {
+ LOGE(SIM_DAEMON, "Cannot find TA: %s", uuid.c_str());
+ return false;
+ }
+
StructBinaryInfo info;
bool res = false;
- if (boost::filesystem::exists(TEE_TASTORE_ROOT + uuid)) {
- pthread_mutex_lock(&taLock);
- try {
- if (unpackBinary(uuid, info)) {
- binaryMap[uuid] = info;
- res = true;
- }
- } catch (...) {
- res = false;
+ pthread_rwlock_wrlock(&binaryMapLock);
+ pthread_mutex_lock(&taLock);
+ try {
+ if (unpackBinary(uuid, tapath, info)) {
+ binaryMap[uuid] = info;
+ res = true;
}
- pthread_mutex_unlock(&taLock);
- } else {
- LOGD(SIM_DAEMON, "TA not exist %s", (TEE_TASTORE_ROOT + uuid).c_str());
+ } catch (...) {
+ res = false;
}
+ pthread_mutex_unlock(&taLock);
pthread_rwlock_unlock(&binaryMapLock);
return res;
}
myfile.close();
}
- boost::filesystem::path decName = info.imagePath + "_dec";
+ fs::path decName = info.imagePath + "_dec";
// hash of Keydata is not required.
string dec_command = "openssl enc " + cipher + " -d -nopad -nosalt -K " + secret
+ " -in " + info.imagePath + " -out " + decName.string() +
}
boost::system::error_code ec;
- boost::filesystem::remove(boost::filesystem::path(info.imagePath), ec);
+ fs::remove(fs::path(info.imagePath), ec);
if (ec) {
LOGE(SIM_DAEMON, "Post decryption failed: unlink %s : %s", info.imagePath.c_str(),
ec.message());
}
- boost::filesystem::rename(decName, boost::filesystem::path(info.imagePath), ec);
+ fs::rename(decName, fs::path(info.imagePath), ec);
if (ec) {
LOGE(SIM_DAEMON, "Post decryption failed: rename %s -> %s : %s",
decName.string().c_str(), info.imagePath.c_str(),
ec.message());
}
- boost::filesystem::remove(boost::filesystem::path(keyhashFilename), ec);
+ fs::remove(fs::path(keyhashFilename), ec);
if (ec) {
LOGE(SIM_DAEMON, "Post decryption failed: unlink %s : %s", keyhashFilename.c_str(),
ec.message());
* It is very important to check for return value from this function.
*/
-bool TABinaryManager::unpackBinary(const string &uuid, StructBinaryInfo& info) {
+bool TABinaryManager::unpackBinary(const string &uuid, const string& tapath, StructBinaryInfo& info) {
TAUnpack* unpacker = TAUnpack::getInstance();
bool ret = false;
- if (0 == unpacker->unpackTA(TEE_TASTORE_ROOT, uuid)) {
+ if (0 == unpacker->unpackTA(tapath, uuid)) {
LOGD(SIM_DAEMON, "Unpacked, filling info");
// 1. Set binary info
- info.path = string(TEE_TASTORE_ROOT) + uuid;
+ info.path = tapath + uuid;
info.extractpath = string(TEE_EXTRACT_ROOT) + uuid + "-ext/";
info.imagePath = info.extractpath + uuid + ".image";
info.manifestPath = info.extractpath + uuid + ".manifest";
#include <iostream>
#include <fstream>
#include <memory>
+#include <boost/filesystem/operations.hpp>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
+#include <config.h>
#include <log.h>
using namespace std;
+namespace fs = boost::filesystem;
/*-----------------------------------------------------------------------------
* Globals
TAPackageHeaderV2 packageHeader;
memset(&packageHeader, 0, sizeof(TAPackageHeaderV2));
- // Create directory for TA extracted files
+ // Re-create directory for TA extracted files
string extract_dir_path = TEE_EXTRACT_ROOT + uuid + "-ext/";
- struct stat info;
- if (stat(extract_dir_path.c_str(), &info) != 0) {
- if (0 != mkdir(extract_dir_path.c_str(), 0777)) {
- LOGE(SIM_DAEMON, "mkdir failed %s %s", extract_dir_path.c_str(), strerror(errno));
- return -1;
- }
+ boost::system::error_code ec;
+ fs::remove_all(extract_dir_path, ec);
+ if (ec != 0){
+ LOGE(SIM_DAEMON, "remove_all failed %s %s (trying to continue)", extract_dir_path.c_str(), ec.message().c_str());
+ }
+ fs::create_directory(extract_dir_path, ec);
+ if (ec != 0){
+ LOGE(SIM_DAEMON, "create_directory failed %s %s", extract_dir_path.c_str(), ec.message().c_str());
+ return -1;
}
// Open TA package file