smbios: Track the end of the string table

Add a new member to the context struct which tracks the end of the string
table. This allows us to avoid recalculating this at the end.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
diff --git a/lib/smbios.c b/lib/smbios.c
index 43628d6..a727352 100644
--- a/lib/smbios.c
+++ b/lib/smbios.c
@@ -24,11 +24,15 @@
  * @dev:	sysinfo device to use (NULL if none)
  * @eos:	end-of-string pointer for the table being processed. This is set
  *		up when we start processing a table
+ * @next_ptr:	pointer to the start of the next string to be added. When the
+ *		table is nopt empty, this points to the byte after the \0 of the
+ *		previous string.
  */
 struct smbios_ctx {
 	ofnode node;
 	struct udevice *dev;
 	char *eos;
+	char *next_ptr;
 };
 
 /**
@@ -77,6 +81,7 @@
 			strcpy(p, str);
 			p += strlen(str);
 			*p++ = '\0';
+			ctx->next_ptr = p;
 			*p++ = '\0';
 
 			return i;
@@ -113,6 +118,7 @@
 static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
 {
 	ctx->eos = eos;
+	ctx->next_ptr = eos;
 }
 
 /**
@@ -120,21 +126,13 @@
  *
  * This computes the size of the string area including the string terminator.
  *
- * @start:	string area start address
+ * @ctx:	SMBIOS context
  * @return:	string area size
  */
-static int smbios_string_table_len(char *start)
+static int smbios_string_table_len(const struct smbios_ctx *ctx)
 {
-	char *p = start;
-	int i, len = 0;
-
-	while (*p) {
-		i = strlen(p) + 1;
-		p += i;
-		len += i;
-	}
-
-	return len + 1;
+	/* Allow for the final \0 after all strings */
+	return (ctx->next_ptr + 1) - ctx->eos;
 }
 
 static int smbios_write_type0(ulong *current, int handle,
@@ -170,7 +168,7 @@
 	t->ec_major_release = 0xff;
 	t->ec_minor_release = 0xff;
 
-	len = t->length + smbios_string_table_len(t->eos);
+	len = t->length + smbios_string_table_len(ctx);
 	*current += len;
 	unmap_sysmem(t);
 
@@ -200,7 +198,7 @@
 	t->sku_number = smbios_add_prop(ctx, "sku");
 	t->family = smbios_add_prop(ctx, "family");
 
-	len = t->length + smbios_string_table_len(t->eos);
+	len = t->length + smbios_string_table_len(ctx);
 	*current += len;
 	unmap_sysmem(t);
 
@@ -223,7 +221,7 @@
 	t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
 	t->board_type = SMBIOS_BOARD_MOTHERBOARD;
 
-	len = t->length + smbios_string_table_len(t->eos);
+	len = t->length + smbios_string_table_len(ctx);
 	*current += len;
 	unmap_sysmem(t);
 
@@ -247,7 +245,7 @@
 	t->thermal_state = SMBIOS_STATE_SAFE;
 	t->security_status = SMBIOS_SECURITY_NONE;
 
-	len = t->length + smbios_string_table_len(t->eos);
+	len = t->length + smbios_string_table_len(ctx);
 	*current += len;
 	unmap_sysmem(t);
 
@@ -306,7 +304,7 @@
 	t->l3_cache_handle = 0xffff;
 	t->processor_family2 = t->processor_family;
 
-	len = t->length + smbios_string_table_len(t->eos);
+	len = t->length + smbios_string_table_len(ctx);
 	*current += len;
 	unmap_sysmem(t);