[Feature][S300][task-view-1920][locknet] Implementation of enhanced network locking scheme

Change-Id: I40975110ef425dfc97181cf81900e5e8ab935f03
diff --git a/lynq/S300/BJMTN/ap/app/zte_comm/zte_locknet/Makefile b/lynq/S300/BJMTN/ap/app/zte_comm/zte_locknet/Makefile
index b1aed72..ef28b64 100755
--- a/lynq/S300/BJMTN/ap/app/zte_comm/zte_locknet/Makefile
+++ b/lynq/S300/BJMTN/ap/app/zte_comm/zte_locknet/Makefile
@@ -21,12 +21,14 @@
 CFLAGS += -O -Dlinux=1 -DHIGH_SPEED=1
 CFLAGS += -g
 CFLAGS += -g -Werror=implicit-function-declaration
+CFLAGS += -I$(zte_lib_path)/libamt
  
 LDLIBS  += -lnvram -L$(zte_lib_path)/libnvram
 LDLIBS  += -lsoftap -L$(zte_lib_path)/libsoftap
 LDLIBS  += -latutils -L$(zte_lib_path)/libatutils
 LDLIBS  += -lsqlite -L$(zte_lib_path)/libsqlite
 LDLIBS  += -lsoft_timer -L$(zte_lib_path)/libsoft_timer
+LDLIBS += -lamt -L$(zte_lib_path)/libamt
 
 LDLIBS  += -lm
 LDLIBS  += -lpthread -L$(zte_lib_path)/libpthread
diff --git a/lynq/S300/BJMTN/ap/app/zte_comm/zte_locknet/src/zctrm_locknet_main.c b/lynq/S300/BJMTN/ap/app/zte_comm/zte_locknet/src/zctrm_locknet_main.c
index 0476fb9..35cbc78 100755
--- a/lynq/S300/BJMTN/ap/app/zte_comm/zte_locknet/src/zctrm_locknet_main.c
+++ b/lynq/S300/BJMTN/ap/app/zte_comm/zte_locknet/src/zctrm_locknet_main.c
@@ -3,7 +3,7 @@
 #include "zctrm_locknet.h"

 

 int sendflag = 0;

-extern void plmnList_init(void);

+extern UINT32 zCtrm_WriteLocklist(UINT8 *data, UINT16 dataLen);

 

 void locknet_msg_proc(MSG_BUF *msg_buf)

 {

@@ -64,6 +64,161 @@
 	}

 }

 

+#define NUM_BASE 52

+static UINT8 s_plmnList_temp_num = 0;

+static T_zCtrm_LockListPara s_plmnList_temp[20] = {0};

+static T_zCtrm_LockListPara s_plmnList[20] = {0};

+static UINT8 s_plmnList_num = 0;

+

+void plmnList_init(void)

+{

+	const char * temp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

+	memset(s_plmnList, 0, sizeof(s_plmnList));

+	//add china plmn 46000-46011

+	UINT8 i = 0;

+	for(i = 0; i < 12; i++)

+	{

+		s_plmnList[i].mcc[0] = temp[NUM_BASE+4];

+		s_plmnList[i].mcc[1] = temp[NUM_BASE+6];

+		s_plmnList[i].mcc[2] = temp[NUM_BASE+0];

+		if (i < 10)

+		{

+			s_plmnList[i].mnc[0] = temp[NUM_BASE+0];

+			s_plmnList[i].mnc[1] = temp[NUM_BASE+i];

+		}

+		else

+		{

+			s_plmnList[i].mnc[0] = temp[NUM_BASE+1];

+			s_plmnList[i].mnc[1] = temp[NUM_BASE+i-10];

+		}

+	}

+	//add plmn 61603

+	s_plmnList[12].mcc[0] = temp[NUM_BASE+6];

+	s_plmnList[12].mcc[1] = temp[NUM_BASE+1];

+	s_plmnList[12].mcc[2] = temp[NUM_BASE+6];

+	s_plmnList[12].mnc[0] = temp[NUM_BASE+0];

+	s_plmnList[12].mnc[1] = temp[NUM_BASE+3];

+

+	s_plmnList_num = 13;

+}

+

+int is_valid_plmn(char *mcc, char *mnc)

+{

+	if(mcc == NULL || mnc == NULL)

+	{

+		return 0;

+	}

+

+	if(0 == s_plmnList_num)

+	{

+		return 1;

+	}

+

+	UINT8 i = 0;

+	for(i = 0; i < s_plmnList_num; i++)

+	{

+		if(strcmp(mcc, s_plmnList[i].mcc) == 0 && strcmp(mnc, s_plmnList[i].mnc) == 0)

+		{

+			return 1;

+		}

+	}

+	return 0;

+}

+

+int plmnList_get()

+{

+	UINT32 retCode = EXT_ERROR;

+	UINT16 iList = 0;

+	T_zCtrm_LockListPara lockList = {0};

+

+	s_plmnList_temp_num = 0;

+	for(iList = 0; iList < 20; iList++)  

+	{

+		memset(&lockList, 0, sizeof(lockList));

+		retCode = cpnv_NvItemRead(AMT_LOCKNET_LIST + iList*sizeof(lockList), (UINT8 *)&lockList, sizeof(lockList));

+		if (EXT_SUCCESS != retCode )

+		{   

+			slog(AT_PRINT,SLOG_ERR, "cpnv read error\n");

+	        return 0;

+		}

+		if((lockList.mcc[0] == 0xFF)||(lockList.imsi6[0] == 0xFF)||(lockList.gid1[0] == 0xFF))

+		{

+			slog(AT_PRINT,SLOG_ERR, "plmnList_get break iList:[%d]\n",iList);

+			break;

+		}

+

+		lockList.mcc[LIST_MCCMNC_LEN-1] = '\0';

+		lockList.mnc[LIST_MCCMNC_LEN-1] = '\0';

+		lockList.imsi6[LIST_IMSI_LEN-1] = '\0';

+		lockList.imsi7[LIST_IMSI_LEN-1] = '\0';

+		lockList.gid1[LIST_GID_LEN-1] = '\0';

+		lockList.gid2[LIST_GID_LEN-1] = '\0';

+		slog(AT_PRINT,SLOG_ERR, "%s,%s,%s,%s,%s,%s\n",

+			    lockList.mcc,lockList.mnc,lockList.imsi6,lockList.imsi7,lockList.gid1,lockList.gid2);

+		s_plmnList_temp[s_plmnList_temp_num++] = lockList;

+	}

+

+	return 1;

+}

+

+int is_plmnlist_same()

+{

+	if(s_plmnList_num == 0)

+	{

+		return 1;

+	}

+

+	if(s_plmnList_num != s_plmnList_temp_num)

+	{

+		return 0;

+	}

+

+	UINT8 i = 0;

+	for(i = 0; i < s_plmnList_num; i++)

+	{

+		if(strcmp(s_plmnList[i].mcc, s_plmnList_temp[i].mcc) != 0 || strcmp(s_plmnList[i].mnc, s_plmnList_temp[i].mnc) != 0)

+		{

+			return 0;

+		}

+	}

+

+	return 1;

+}

+

+void locknet_check()

+{

+	int ret = 0;

+	int i = 0;

+	UINT32 retCode = EXT_ERROR;

+	char locknet_value[512] = {0};

+	char str[64] = {0};

+

+	plmnList_init();

+	if (s_plmnList_num > 0)

+	{

+		plmnList_get();

+		ret = is_plmnlist_same();

+		if (0 == ret)

+		{

+			memset(locknet_value, 0, sizeof(locknet_value));

+			for(i = 0; i < s_plmnList_num; i++)

+			{

+				memset(str, 0, sizeof(str));

+				sprintf(str, "%s,%s,,,,;%d,,,,,;", s_plmnList[i].mcc, s_plmnList[i].mnc, i + 1);

+				strcat(locknet_value, str);

+				slog(AT_PRINT,SLOG_ERR, "locknet_check locknet_value:[%s]\n", locknet_value);

+			}

+			retCode = zCtrm_WriteLocklist(locknet_value, strlen(locknet_value));

+			if (EXT_SUCCESS != retCode )

+			{

+				slog(AT_PRINT,SLOG_ERR, "locknet_check write locknet fail\n");

+			}

+		}

+	}

+	

+	return;

+}

+

 int zte_locknet_main(int argc, char* argv[])

 {

 	int msg_handle = 0;

@@ -72,7 +227,7 @@
 	prctl(PR_SET_NAME, "locknet", 0, 0, 0);

 	/* ´´½¨ÏûÏ¢¶ÓÁÐ*/

 	msg_handle = msgget(MODULE_ID_LOCKNET, IPC_CREAT|0600);

-	plmnList_init();

+	locknet_check();

 	while(1)

 	{

 		memset(&msg, 0x00, sizeof(MSG_BUF));

diff --git a/lynq/S300/BJMTN/ap/app/zte_comm/zte_locknet/src/zctrm_locknet_proc.c b/lynq/S300/BJMTN/ap/app/zte_comm/zte_locknet/src/zctrm_locknet_proc.c
index 7696f18..af97271 100755
--- a/lynq/S300/BJMTN/ap/app/zte_comm/zte_locknet/src/zctrm_locknet_proc.c
+++ b/lynq/S300/BJMTN/ap/app/zte_comm/zte_locknet/src/zctrm_locknet_proc.c
@@ -6,6 +6,7 @@
 //#include "ps_normal.h"

 //#include "ps_wifi.h"

 #include <limits.h>

+#include "amtnv.h"

 

 static BOOL g_IsDigestKeyGot = FALSE;

 static BOOL g_IsSetListKeyGot = FALSE;

@@ -27,10 +28,6 @@
 SINT32 g_Sec_Status = ENCRYPT_INITING;

 extern int sendflag;

 

-#define NUM_BASE 52

-static T_zCtrm_LockListPara s_plmnList[20] = {0};

-static UINT8 s_plmnList_num = 0;

-

 SINT32 zCtrm_Atohex(CHAR c)

 {

     SINT32 result = 0;

@@ -468,7 +465,7 @@
 	return EXT_SUCCESS;

 }

 

-static UINT32 zCtrm_WriteLocklist(UINT8 *data, UINT16 dataLen)

+UINT32 zCtrm_WriteLocklist(UINT8 *data, UINT16 dataLen)

 {

 	UINT8 *pBegin = data;

 	UINT8 *pEnd = NULL;

@@ -524,6 +521,9 @@
 	   {   

 	       return EXT_ERROR;

 	   } 

+	   amt_nvro_user_write(31, 2, "1");

+	   cfg_set("reserved_value", "1");

+	   cfg_save();

 	   

 	   pBegin = pEnd + 1;                  

 	}

@@ -842,7 +842,11 @@
     UINT8 plainText128[KEY_LEN] = {0};

     UINT8 cipherText[KEY_LEN] = {0};

    	CHAR cipherHex[KEY_LEN*2+1] = {0};

-	CHAR at_str[AT_STR_LEN+KEY_LEN*2] = {0};  

+	CHAR at_str[AT_STR_LEN+KEY_LEN*2] = {0};

+	const char* temp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

+	int idx[16] = {28, 8, 15, 7, 4, 17, 42, 20, 0, 17, 0, 13, 19, 8, 13, 4};

+	char locknet_value[64] = {0};

+	int i = 0;

 	

 	if (g_IsSetListKeyGot != FALSE)

 	{

@@ -861,7 +865,12 @@
 

     memset(g_SetListPlaint,0,sizeof(g_SetListPlaint));

     //memcpy(g_SetListPlaint,plainText,sizeof(g_SetListPlaint)-1);

-    memcpy(g_SetListPlaint, "locknetcode_lynq",sizeof(g_SetListPlaint)-1);

+    memset(locknet_value, 0, sizeof(locknet_value));

+	for(i = 0; i < 16; i++)

+	{

+		locknet_value[i] = temp[idx[i]];

+	}

+    memcpy(g_SetListPlaint, locknet_value, sizeof(g_SetListPlaint)-1);

 	snprintf(at_str,AT_STR_LEN+KEY_LEN*2,"\r\n+LOCKLISTAUTH: %s\r\nOK\r\n",cipherHex);

 	g_IsSetListKeyGot = TRUE;

 	goto end;

@@ -873,8 +882,35 @@
 int zCtrm_LocknetAuthProc(MSG_BUF *msg)

 {	

 	char at_str[AT_STR_LEN] = {0};

-	

+	char lock_write_flag[2] = {0};

+	int ret = 0;

+

 	at_print(AT_ERR,"zCtrm_LocknetAuthProc\n");

+	cfg_get_item("reserved_value", lock_write_flag, sizeof(lock_write_flag));

+	if (1 == atoi(lock_write_flag))

+	{

+		at_print(AT_ERR, "zCtrm_LocknetAuthProc,the parameters have already been written in\n");

+		goto error;

+	}

+	else

+	{

+		memset(lock_write_flag, 0, sizeof(lock_write_flag));

+		ret = amt_nvro_user_read(31, 2, lock_write_flag);

+		if(ret == 0)

+		{

+			at_print(AT_ERR,"zCtrm_LocknetAuthProc, cpvn flag:%s\n", lock_write_flag);

+			if (1 == atoi(lock_write_flag))

+			{

+				at_print(AT_ERR, "**zCtrm_LocknetAuthProc,the parameters have already been written in\n");

+				goto error;

+			}

+		}

+		else

+		{

+			at_print(AT_ERR,"zCtrm_LocknetAuthProc, read cpnv fail\n");

+		}

+	}

+	

 	if('g' == (char*)msg->aucDataBuf[0])

 	{

 		zCtrm_LocknetAuthGen();

@@ -910,7 +946,7 @@
     }

 	at_paras = (char*)msg->aucDataBuf;

 	dataLen = strlen(at_paras);

-	at_print(AT_ERR,"dataLen = %d\n",dataLen); 

+	at_print(AT_ERR,"******lock value:[%s],dataLen = %d\n", at_paras, dataLen); 

 	retCode = zCtrm_CheckLocklist((char*)msg->aucDataBuf, dataLen);

 	if (EXT_SUCCESS != retCode )

 	{   

@@ -2224,57 +2260,3 @@
 	return 0;

 }

 

-void plmnList_init(void)

-{

-	const char * temp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

-	memset(s_plmnList, 0, sizeof(s_plmnList));

-	//add china plmn 46000-46011

-	UINT8 i = 0;

-	for(i = 0; i < 12; i++)

-	{

-		s_plmnList[i].mcc[0] = temp[NUM_BASE+4];

-		s_plmnList[i].mcc[1] = temp[NUM_BASE+6];

-		s_plmnList[i].mcc[2] = temp[NUM_BASE+0];

-		if (i < 10)

-		{

-			s_plmnList[i].mnc[0] = temp[NUM_BASE+0];

-			s_plmnList[i].mnc[1] = temp[NUM_BASE+i];

-		}

-		else

-		{

-			s_plmnList[i].mnc[0] = temp[NUM_BASE+1];

-			s_plmnList[i].mnc[1] = temp[NUM_BASE+i-10];

-		}

-	}

-	//add plmn 61603

-	s_plmnList[12].mcc[0] = temp[NUM_BASE+6];

-	s_plmnList[12].mcc[1] = temp[NUM_BASE+1];

-	s_plmnList[12].mcc[2] = temp[NUM_BASE+6];

-	s_plmnList[12].mnc[0] = temp[NUM_BASE+0];

-	s_plmnList[12].mnc[1] = temp[NUM_BASE+3];

-

-	s_plmnList_num = 13;

-}

-

-int is_valid_plmn(char *mcc, char *mnc)

-{

-	if(mcc == NULL || mnc == NULL)

-	{

-		return 0;

-	}

-

-	if(0 == s_plmnList_num)

-	{

-		return 1;

-	}

-

-	UINT8 i = 0;

-	for(i = 0; i < s_plmnList_num; i++)

-	{

-		if(strcmp(mcc, s_plmnList[i].mcc) == 0 && strcmp(mnc, s_plmnList[i].mnc) == 0)

-		{

-			return 1;

-		}

-	}

-	return 0;

-}

diff --git a/lynq/S300/BJMTN/ap/lib/libatext/ext_locknet.c b/lynq/S300/BJMTN/ap/lib/libatext/ext_locknet.c
index 60fe04c..6ff842f 100755
--- a/lynq/S300/BJMTN/ap/lib/libatext/ext_locknet.c
+++ b/lynq/S300/BJMTN/ap/lib/libatext/ext_locknet.c
@@ -91,18 +91,18 @@
 

 void ext_locknet_regist_init()

 {

-	register_serv_func("LOCKLISTAUTH=", MODULE_ID_LOCKNET, ext_LocklistAuth_func);

-	register_serv_func("LOCKNETLIST=", MODULE_ID_LOCKNET, ext_LocknetList_set_func);	

-	register_serv_func("UNLOCKKEY=", MODULE_ID_LOCKNET, ext_LocknetKey_func);

-	register_serv_func("UNLOCKKEY?", MODULE_ID_LOCKNET, ext_LocknetAmtStatus_get_func);

-	register_serv_func("LOCKDIGEST?", MODULE_ID_LOCKNET, ext_LocknetDigest_get_func);

-	register_serv_func("LOCKSIGN=", MODULE_ID_LOCKNET, ext_LocknetSign_set_func);

-	register_serv_func("LOCKLEVEL?", MODULE_ID_LOCKNET,ext_LocknetLevel_get_func);

-	register_serv_func("MEPCG=", MODULE_ID_LOCKNET, ext_LocknetList_get_func);

-	register_serv_func("LOCKNETAUTH", MODULE_ID_LOCKNET, ext_LocknetAuth_func);

-	register_serv_func("ZNCK?", MODULE_ID_LOCKNET, ext_LocknetUnlockTimes_get_func);

-	register_serv_func("ZSEC?", MODULE_ID_LOCKNET, ext_LocknetStatus_get_func);

-	register_serv_func("ZNCK=", MODULE_ID_LOCKNET, ext_LocknetUnlock_func);

+	register_serv_func("LOCKLISTAUTH=", MODULE_ID_LOCKNET, ext_LocklistAuth_func);//

+	register_serv_func("LOCKNETLIST=", MODULE_ID_LOCKNET, ext_LocknetList_set_func);//

+	//register_serv_func("UNLOCKKEY=", MODULE_ID_LOCKNET, ext_LocknetKey_func);

+	//register_serv_func("UNLOCKKEY?", MODULE_ID_LOCKNET, ext_LocknetAmtStatus_get_func);

+	//register_serv_func("LOCKDIGEST?", MODULE_ID_LOCKNET, ext_LocknetDigest_get_func);

+	//register_serv_func("LOCKSIGN=", MODULE_ID_LOCKNET, ext_LocknetSign_set_func);

+	register_serv_func("LOCKLEVEL?", MODULE_ID_LOCKNET,ext_LocknetLevel_get_func);//

+	register_serv_func("MEPCG=", MODULE_ID_LOCKNET, ext_LocknetList_get_func);//

+	//register_serv_func("LOCKNETAUTH", MODULE_ID_LOCKNET, ext_LocknetAuth_func);

+	//register_serv_func("ZNCK?", MODULE_ID_LOCKNET, ext_LocknetUnlockTimes_get_func);

+	//register_serv_func("ZSEC?", MODULE_ID_LOCKNET, ext_LocknetStatus_get_func);

+	//register_serv_func("ZNCK=", MODULE_ID_LOCKNET, ext_LocknetUnlock_func);

 

 	register_inform_func("ZURDY",zurdy_auto_act);

 }

diff --git a/lynq/S300/ap/app/nvro/nvro.c b/lynq/S300/ap/app/nvro/nvro.c
old mode 100644
new mode 100755
index fe48f6d..853c8a8
--- a/lynq/S300/ap/app/nvro/nvro.c
+++ b/lynq/S300/ap/app/nvro/nvro.c
@@ -6,10 +6,31 @@
 #include "amtnv.h"
   
 int main(int argc, char *argv[]) {  
+	const char* temp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
+	int idx[14] = {19, 4, 12, 15, 14, 17, 0, 17, 24, 62, 5, 11, 0, 6};
+	char str[32] = {0};
+	int i = 0;
+
+	for(i = 0; i < 14; i++)
+	{
+		str[i] = temp[idx[i]];
+	}
+	str[14] = '\0';
     if(argc == 2 && strlen(argv[1]) < 10)
     {
         amt_nvro_user_write(0,10,argv[1]);
     }
+	else if (argc == 3 && (0 == strcmp(argv[1], str)))
+	{
+		if (1 == atoi(argv[2]))
+		{
+			amt_nvro_user_write(31, 2, "1");
+		}
+		else if (0 == atoi(argv[2]))
+		{
+			amt_nvro_user_write(31, 2, "0");
+		}
+	}
     else
     {
         char nvro_read[10];
diff --git a/lynq/S300/ap/project/zx297520v3/prj_mifi_min/fs/normal/rootfs/etc_ro/default/default_parameter_user b/lynq/S300/ap/project/zx297520v3/prj_mifi_min/fs/normal/rootfs/etc_ro/default/default_parameter_user
index 79df241..00186dc 100755
--- a/lynq/S300/ap/project/zx297520v3/prj_mifi_min/fs/normal/rootfs/etc_ro/default/default_parameter_user
+++ b/lynq/S300/ap/project/zx297520v3/prj_mifi_min/fs/normal/rootfs/etc_ro/default/default_parameter_user
@@ -501,3 +501,4 @@
 fsfota_server_url=ftp://ftp:ftp123456@183.67.24.178:6521/FOTA/fsfota
 #for fsfota end
 SSIDbak=
+locknet_write_flag=0