Make TF UUID RFC 4122 compliant

RFC4122 defines that fields are stored in network order (big endian),
but TF-A stores them in machine order (little endian by default in TF-A).
We cannot change the future UUIDs that are already generated, but we can store
all the bytes using arrays and modify fiptool to generate the UUIDs with
the correct byte order.

Change-Id: I97be2d3168d91f4dee7ccfafc533ea55ff33e46f
Signed-off-by: Roberto Vargas <roberto.vargas@arm.com>
diff --git a/tools/fiptool/fiptool.c b/tools/fiptool/fiptool.c
index e4348ee..0d4f929 100644
--- a/tools/fiptool/fiptool.c
+++ b/tools/fiptool/fiptool.c
@@ -51,7 +51,7 @@
 
 static image_desc_t *image_desc_head;
 static size_t nr_image_descs;
-static uuid_t uuid_null = { 0 };
+static const uuid_t uuid_null;
 static int verbose;
 
 static void vlog(int prio, const char *msg, va_list ap)
@@ -241,14 +241,15 @@
 {
 	assert(len >= (_UUID_STR_LEN + 1));
 
-	snprintf(s, len, "%08X-%04X-%04X-%04X-%04X%04X%04X",
-	    u->time_low,
-	    u->time_mid,
-	    u->time_hi_and_version,
-	    ((uint16_t)u->clock_seq_hi_and_reserved << 8) | u->clock_seq_low,
-	    ((uint16_t)u->node[0] << 8) | u->node[1],
-	    ((uint16_t)u->node[2] << 8) | u->node[3],
-	    ((uint16_t)u->node[4] << 8) | u->node[5]);
+	snprintf(s, len,
+	    "%02X%02X%02X%02X-%02X%02X-%02X%02X-%04X-%04X%04X%04X",
+	    u->time_low[0], u->time_low[1], u->time_low[2], u->time_low[3],
+	    u->time_mid[0], u->time_mid[1],
+	    u->time_hi_and_version[0], u->time_hi_and_version[1],
+	    (u->clock_seq_hi_and_reserved << 8) | u->clock_seq_low,
+	    (u->node[0] << 8) | u->node[1],
+	    (u->node[2] << 8) | u->node[3],
+	    (u->node[4] << 8) | u->node[5]);
 }
 
 static void uuid_from_str(uuid_t *u, const char *s)
@@ -261,10 +262,14 @@
 		log_errx("Invalid UUID: %s", s);
 
 	n = sscanf(s,
-	    "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
-	    &u->time_low, &u->time_mid, &u->time_hi_and_version,
-	    &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0],
-	    &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]);
+	    "%2hhx%2hhx%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
+	    &u->time_low[0], &u->time_low[1], &u->time_low[2], &u->time_low[3],
+	    &u->time_mid[0], &u->time_mid[1],
+	    &u->time_hi_and_version[0], &u->time_hi_and_version[1],
+	    &u->clock_seq_hi_and_reserved, &u->clock_seq_low,
+	    &u->node[0], &u->node[1],
+	    &u->node[2], &u->node[3],
+	    &u->node[4], &u->node[5]);
 	/*
 	 * Given the format specifier above, we expect 11 items to be scanned
 	 * for a properly formatted UUID.
@@ -697,7 +702,7 @@
 		case 'b': {
 			char name[_UUID_STR_LEN + 1];
 			char filename[PATH_MAX] = { 0 };
-			uuid_t uuid = { 0 };
+			uuid_t uuid = uuid_null;
 			image_desc_t *desc;
 
 			parse_blob_opt(optarg, &uuid,
@@ -794,7 +799,7 @@
 		case 'b': {
 			char name[_UUID_STR_LEN + 1];
 			char filename[PATH_MAX] = { 0 };
-			uuid_t uuid = { 0 };
+			uuid_t uuid = uuid_null;
 			image_desc_t *desc;
 
 			parse_blob_opt(optarg, &uuid,
@@ -902,7 +907,7 @@
 		case 'b': {
 			char name[_UUID_STR_LEN + 1];
 			char filename[PATH_MAX] = { 0 };
-			uuid_t uuid = { 0 };
+			uuid_t uuid = uuid_null;
 			image_desc_t *desc;
 
 			parse_blob_opt(optarg, &uuid,
@@ -1041,7 +1046,7 @@
 			break;
 		case 'b': {
 			char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
-			uuid_t uuid = { 0 };
+			uuid_t uuid = uuid_null;
 			image_desc_t *desc;
 
 			parse_blob_opt(optarg, &uuid,
diff --git a/tools/fiptool/tbbr_config.c b/tools/fiptool/tbbr_config.c
index c7df243..86b8581 100644
--- a/tools/fiptool/tbbr_config.c
+++ b/tools/fiptool/tbbr_config.c
@@ -153,7 +153,7 @@
 	},
 	{
 		.name = NULL,
-		.uuid = { 0 },
+		.uuid = { {0} },
 		.cmdline_name = NULL,
 	}
 };