Add choice that read/write locality selection when defining nv index.

Signed-off-by:Xiaokai Wang <xiaokai.wang@intel.com>

diff --git a/src/tpm_mgmt/tpm_nvdefine.c b/src/tpm_mgmt/tpm_nvdefine.c
index e2c748f..267d2bf 100644
--- a/src/tpm_mgmt/tpm_nvdefine.c
+++ b/src/tpm_mgmt/tpm_nvdefine.c
@@ -27,6 +27,10 @@
 #include "tpm_utils.h"
 #include "tpm_nvcommon.h"
 
+static unsigned int r_loc_arg = 0;
+static unsigned int w_loc_arg = 0;
+static unsigned int r_loc_flag = 0;
+static unsigned int w_loc_flag = 0;
 static unsigned int nvindex;
 static BOOL nvindex_set;
 static unsigned int nvperm;
@@ -122,6 +126,20 @@ static int parse(const int aOpt, const char *aArg)
 			return -1;
 		break;
 
+	case 'R':
+		if (parseHexOrDecimal(aArg, &r_loc_arg, 0, UINT_MAX,
+				       "read localityValue") != 0)
+			return -1;
+		r_loc_flag = 1;
+		break;
+
+	case 'W':
+		if (parseHexOrDecimal(aArg, &w_loc_arg, 0, UINT_MAX,
+				       "write localityValue") != 0)
+			return -1;
+		w_loc_flag = 1;
+		break;
+
 	case 'f':
 		filename = aArg;
 		break;
@@ -152,6 +170,12 @@ static void help(const char* aCmd)
 		     _("PCRs to seal the NVRAM area to for reading (use multiple times)"));
 	logCmdOption("-w, --wpcrs",
 		     _("PCRs to seal the NVRAM area to for writing (use multiple times)"));
+	logCmdOption("-R, --rlv",
+		     _("read locality value:uint8.there are 5 localities:0~4.\n"
+			   "\t\tfor example,TPM_LOC_ZERO=0x1 TPM_LOC_ONE=0x2 TPM_LOC_TWO=0x4\n"
+			   "\t\tTPM_LOC_THREE=0x8 TPM_LOC_FOUR=0x10 TPM_LOC_ALL=0x1f"));
+	logCmdOption("-W, --wlv",
+		     _("write locality value:uint8.the same as read locality value."));
 	logCmdOption("-f, --filename",
 		     _("File containing PCR info for the NVRAM area"));
 
@@ -252,6 +276,8 @@ int main(int argc, char **argv)
 		{"rpcrs"           , required_argument, NULL, 'r'},
 		{"wpcrs"           , required_argument, NULL, 'w'},
 		{"filename"        , required_argument, NULL, 'f'},
+		{"rlv"             , optional_argument, NULL, 'R'},
+		{"wlv"             , optional_argument, NULL, 'W'},
 		{"pwdo"            , optional_argument, NULL, 'o'},
 		{"pwda"            , optional_argument, NULL, 'a'},
 		{"use-unicode"     ,       no_argument, NULL, 'u'},
@@ -266,7 +292,7 @@ int main(int argc, char **argv)
 	initIntlSys();
 
 	if (genericOptHandler
-		    (argc, argv, "i:s:p:o:a:r:w:f:yzu", hOpts,
+		    (argc, argv, "i:s:p:o:a:r:w:R:W:f:yzu", hOpts,
 		     sizeof(hOpts) / sizeof(struct option), parse, help) != 0)
 		goto out;
 
@@ -451,13 +477,39 @@ int main(int argc, char **argv)
 			goto out_close_obj;
 	}
 
-	if (hPcrsRead)
+	if (r_loc_arg > 0x1f) {
+		logMsg(_("wrong read locality number!\n"));
+		goto out_close;
+	}
+
+	if (contextCreateObject(hContext, TSS_OBJECT_TYPE_PCRS, initFlag,
+				&hPcrsRead) != TSS_SUCCESS)
+		goto out_close;
+
+	if (r_loc_flag == 1) {
+		if (pcrcompositeSetPcrLocality(hPcrsRead, r_loc_arg) != TSS_SUCCESS)
+			goto out_close;
+	} else {
 		if (pcrcompositeSetPcrLocality(hPcrsRead, localityValue) != TSS_SUCCESS)
 			goto out_close;
+	}
+
+	if (w_loc_arg > 0x1f) {
+		logMsg(_("wrong write locality number!\n"));
+		goto out_close;
+	}
+
+	if (contextCreateObject(hContext, TSS_OBJECT_TYPE_PCRS, initFlag,
+				&hPcrsWrite) != TSS_SUCCESS)
+		goto out_close;
 
-	if (hPcrsWrite)
+	if (w_loc_flag == 1) {
+		if (pcrcompositeSetPcrLocality(hPcrsWrite, w_loc_arg) != TSS_SUCCESS)
+			goto out_close;
+	} else {
 		if (pcrcompositeSetPcrLocality(hPcrsWrite, localityValue) != TSS_SUCCESS)
 			goto out_close;
+	}
 
 	if (NVDefineSpace(nvObject, hPcrsRead, hPcrsWrite) != TSS_SUCCESS)
 		goto out_close;
