Merge "Merge remote-tracking branch 'quic/caf/google/tbase-300' into b2"
diff --git a/MobiCoreDriverLib/Android.mk b/MobiCoreDriverLib/Android.mk
index 89696ad..3d9c506 100755
--- a/MobiCoreDriverLib/Android.mk
+++ b/MobiCoreDriverLib/Android.mk
@@ -15,6 +15,7 @@
 LOCAL_SHARED_LIBRARIES += $(GLOBAL_LIBRARIES)
 
 LOCAL_CFLAGS := -fvisibility=hidden -fvisibility-inlines-hidden
+LOCAL_CFLAGS += -Wno-missing-field-initializers
 LOCAL_CFLAGS += -include buildTag.h
 LOCAL_CFLAGS += -DLOG_TAG=\"McClient\"
 LOCAL_CFLAGS += -DTBASE_API_LEVEL=3
@@ -56,6 +57,8 @@
 LOCAL_CFLAGS += -include buildTag.h
 LOCAL_CFLAGS += -DLOG_TAG=\"McDaemon\"
 LOCAL_CFLAGS += -DTBASE_API_LEVEL=3
+#remove some warnings
+LOCAL_CFLAGS += -Wno-missing-field-initializers -Wno-format
 LOCAL_C_INCLUDES += $(GLOBAL_INCLUDES)
 LOCAL_SHARED_LIBRARIES += $(GLOBAL_LIBRARIES) libMcClient
 
@@ -118,4 +121,4 @@
 # Import logwrapper
 include $(LOG_WRAPPER)/Android.mk
 
-include $(BUILD_SHARED_LIBRARY)
\ No newline at end of file
+include $(BUILD_SHARED_LIBRARY)
diff --git a/MobiCoreDriverLib/ClientLib/GP/tee_client_api.cpp b/MobiCoreDriverLib/ClientLib/GP/tee_client_api.cpp
index e9e53bc..1475e06 100755
--- a/MobiCoreDriverLib/ClientLib/GP/tee_client_api.cpp
+++ b/MobiCoreDriverLib/ClientLib/GP/tee_client_api.cpp
@@ -234,7 +234,7 @@
     }
 
     //Copy version indicator field
-    strcpy(tci->header, "TCIGP000");
+    memcpy(tci->header, "TCIGP000", sizeof(tci->header));
 
     // Fill in invalid values for secure world to overwrite
     tci->returnStatus = 0;
diff --git a/MobiCoreDriverLib/Daemon/MobiCoreDriverDaemon.cpp b/MobiCoreDriverLib/Daemon/MobiCoreDriverDaemon.cpp
index d20ff8a..dc7e16e 100755
--- a/MobiCoreDriverLib/Daemon/MobiCoreDriverDaemon.cpp
+++ b/MobiCoreDriverLib/Daemon/MobiCoreDriverDaemon.cpp
@@ -156,25 +156,38 @@
     LOG_I("Looking for suitable tokens");
 
     mcSoAuthTokenCont_t authtoken;
+    mcSoAuthTokenCont_t authtokenbackup;
     mcSoRootCont_t rootcont;
     uint32_t sosize;
     uint8_t *p = NULL;
 
+    // Search order:  1. authtoken 2. authtoken backup 3. root container
+    sosize = 0;
     mcResult_t ret = mcRegistryReadAuthToken(&authtoken);
     if (ret != MC_DRV_OK) {
-        LOG_I("Failed to read AuthToken (ret=%u). Trying Root Container", ret);
+        LOG_I("Failed to read AuthToken (ret=%u). Trying AuthToken backup", ret);
 
-        sosize = sizeof(rootcont);
-        ret = mcRegistryReadRoot(&rootcont, &sosize);
+        ret = mcRegistryReadAuthTokenBackup(&authtokenbackup);
         if (ret != MC_DRV_OK) {
-            LOG_I("Failed to read Root Cont, (ret=%u)", ret);
-            LOG_W("Device endorsements not supported!");
-            sosize = 0;
+            LOG_I("Failed to read AuthToken backup (ret=%u). Trying Root Cont", ret);
+
+            sosize = sizeof(rootcont);
+            ret = mcRegistryReadRoot(&rootcont, &sosize);
+            if (ret != MC_DRV_OK) {
+                LOG_I("Failed to read Root Cont, (ret=%u).", ret);
+                LOG_W("Device endorsements not supported!");
+                sosize = 0;
+            } else {
+                LOG_I("Found Root Cont.");
+                p = (uint8_t *) &rootcont;
+            }
+
+        } else {
+            LOG_I("Found AuthToken backup.");
+            p = (uint8_t *) &authtokenbackup;
+            sosize = sizeof(authtokenbackup);
         }
-        else {
-            LOG_I("Found Root Cont.");
-            p = (uint8_t *) &rootcont;
-        }
+        
     } else {
         LOG_I("Found AuthToken.");
         p = (uint8_t *) &authtoken;
@@ -1361,26 +1374,13 @@
 
         /* ignore terminal has been closed signal */
         signal(SIGHUP, SIG_IGN);
-
-        int i = fork();
-        if (i < 0) {
-            exit(1);
-        }
-        // Parent
-        else if (i > 0) {
-            exit(0);
+        
+         /* become a daemon */
+        if (daemon(0, 0) < 0) {
+            fprintf(stderr, "Fork failed, exiting.\n");
+            return 1;
         }
 
-        // obtain a new process group */
-        setsid();
-        /* close all descriptors */
-        for (i = getdtablesize(); i >= 0; --i) {
-            close(i);
-        }
-        // STDIN, STDOUT and STDERR should all point to /dev/null */
-        i = open("/dev/null", O_RDWR);
-        dup(i);
-        dup(i);
         /* ignore tty signals */
         signal(SIGTSTP, SIG_IGN);
         signal(SIGTTOU, SIG_IGN);
diff --git a/MobiCoreDriverLib/Registry/PrivateRegistry.cpp b/MobiCoreDriverLib/Registry/PrivateRegistry.cpp
index 0b2293a..ba938b8 100755
--- a/MobiCoreDriverLib/Registry/PrivateRegistry.cpp
+++ b/MobiCoreDriverLib/Registry/PrivateRegistry.cpp
@@ -74,6 +74,7 @@
 #define MC_REGISTRY_DEFAULT_PATH "/system/app/mcRegistry"
 #define MC_REGISTRY_FALLBACK_PATH "/data/app/mcRegistry"
 #define AUTH_TOKEN_FILE_NAME "00000000.authtokcont"
+#define AUTH_TOKEN_FILE_NAME_BACKUP_SUFFIX ".backup"
 #define ENV_MC_AUTH_TOKEN_PATH "MC_AUTH_TOKEN_PATH"
 #define ROOT_FILE_NAME "00000000.rootcont"
 #define SP_CONT_FILE_EXT ".spcont"
@@ -180,6 +181,12 @@
 }
 
 //------------------------------------------------------------------------------
+static string getAuthTokenFilePathBackup()
+{
+    return getAuthTokenFilePath() + AUTH_TOKEN_FILE_NAME_BACKUP_SUFFIX;
+}
+
+//------------------------------------------------------------------------------
 static string getRootContFilePath()
 {
     return getRegistryPath() + "/" + ROOT_FILE_NAME;
@@ -294,10 +301,45 @@
 }
 
 //------------------------------------------------------------------------------
+mcResult_t mcRegistryReadAuthTokenBackup(mcSoAuthTokenCont_t *so)
+{
+    if (NULL == so) {
+        LOG_E("mcRegistry read So.Soc failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
+        return MC_DRV_ERR_INVALID_PARAMETER;
+    }
+    const string &authTokenFilePath = getAuthTokenFilePathBackup();
+    LOG_I("read AuthToken: %s", authTokenFilePath.c_str());
+
+    FILE *fs = fopen(authTokenFilePath.c_str(), "rb");
+    if (!fs) {
+        LOG_W("mcRegistry read So.Soc failed: %d", MC_DRV_ERR_INVALID_DEVICE_FILE);
+        return MC_DRV_ERR_INVALID_DEVICE_FILE;
+    }
+    fseek(fs, 0, SEEK_END);
+    int32_t filesize = ftell(fs);
+    if (sizeof(mcSoAuthTokenCont_t) != filesize) {
+        fclose(fs);
+        LOG_W("mcRegistry read So.Soc failed: %d", MC_DRV_ERR_OUT_OF_RESOURCES);
+        return MC_DRV_ERR_OUT_OF_RESOURCES;
+    }
+    fseek(fs, 0, SEEK_SET);
+    if (fread((char *)so, 1, sizeof(mcSoAuthTokenCont_t), fs) !=
+                sizeof(mcSoAuthTokenCont_t))
+    {
+        fclose(fs);
+        LOG_W("mcRegistry read So.Soc failed: %d", MC_DRV_ERR_INVALID_PARAMETER);
+        return MC_DRV_ERR_INVALID_PARAMETER;
+    }
+    fclose(fs);
+
+    return MC_DRV_OK;
+}
+
+//------------------------------------------------------------------------------
 mcResult_t mcRegistryDeleteAuthToken(void)
 {
-    if (remove(getAuthTokenFilePath().c_str())) {
-        LOG_ERRNO("Delete Auth token file!");
+    if (rename(getAuthTokenFilePath().c_str(), getAuthTokenFilePathBackup().c_str())) {
+        LOG_ERRNO("Rename Auth token file!");
         return MC_DRV_ERR_UNKNOWN;
     } else
         return MC_DRV_OK;
diff --git a/MobiCoreDriverLib/Registry/PrivateRegistry.h b/MobiCoreDriverLib/Registry/PrivateRegistry.h
index 9404939..25be116 100755
--- a/MobiCoreDriverLib/Registry/PrivateRegistry.h
+++ b/MobiCoreDriverLib/Registry/PrivateRegistry.h
@@ -71,6 +71,12 @@
      */
     mcResult_t mcRegistryReadAuthToken(mcSoAuthTokenCont_t *so);
 
+    /** Reads an authentication token backup from registry.
+     * @param[out] so Authentication token secure object.
+     * @return MC_DRV_OK if successful, otherwise error code.
+     */
+    mcResult_t mcRegistryReadAuthTokenBackup(mcSoAuthTokenCont_t *so);
+
     /** Deletes the authentication token secure object from the registry.
      * @return MC_DRV_OK if successful, otherwise error code.
      */
diff --git a/MobiCoreDriverLib/buildTag.h b/MobiCoreDriverLib/buildTag.h
index bb90ab7..05cb483 100755
--- a/MobiCoreDriverLib/buildTag.h
+++ b/MobiCoreDriverLib/buildTag.h
@@ -26,4 +26,4 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 #define MOBICORE_COMPONENT_BUILD_TAG \
-		"*** t-base-300-QC-8x26-Android-V001 ###"
+		"*** t-base-300-QC-8974-Android-V005 ###"
diff --git a/rootpa/Code/Android/app/jni/Common/Android.mk b/rootpa/Code/Android/app/jni/Common/Android.mk
index ceee2ac..c2b0560 100755
--- a/rootpa/Code/Android/app/jni/Common/Android.mk
+++ b/rootpa/Code/Android/app/jni/Common/Android.mk
@@ -66,6 +66,7 @@
 LOCAL_C_INCLUDES +=  $(MOBICOREDRIVER_DIR_INC2)
 LOCAL_C_INCLUDES +=  external/curl/include
 LOCAL_C_INCLUDES +=  external/libxml2/include
+LOCAL_C_INCLUDES +=  external/icu/icu4c/source/common
 LOCAL_C_INCLUDES +=  external/icu4c/common
 LOCAL_C_INCLUDES +=  $(LOCAL_PATH)/../../../../Common
 LOCAL_C_INCLUDES +=  $(LOCAL_PATH)/../../../../Common/include
@@ -79,4 +80,6 @@
 
 LOCAL_MODULE_TAGS := debug eng optional
 
+LOCAL_32_BIT_ONLY := true
+
 include $(BUILD_STATIC_LIBRARY)