lib: add common write_tables() implementation

In order to de-duplicate common patterns implement one write_tables()
function. The new write_tables() replaces all the architecture-specific
ones that were largely copied. The callbacks are put in place to
handle any per-architecture requirements.

Change-Id: Id3d7abdce5b30f5557ccfe1dacff3c58c59f5e2b
Signed-off-by: Aaron Durbin <adurbin@chromium.org>
Reviewed-on: https://review.coreboot.org/14436
Tested-by: build bot (Jenkins)
Tested-by: Raptor Engineering Automated Test Stand <noreply@raptorengineeringinc.com>
Reviewed-by: Patrick Georgi <pgeorgi@google.com>
diff --git a/src/lib/coreboot_table.c b/src/lib/coreboot_table.c
index 583f609..58c6f48 100644
--- a/src/lib/coreboot_table.c
+++ b/src/lib/coreboot_table.c
@@ -459,29 +459,12 @@
 	return (uintptr_t)lb_table_fini(head) - entry;
 }
 
-unsigned long write_coreboot_table(
-	unsigned long low_table_start, unsigned long low_table_end,
-	unsigned long rom_table_start __unused, unsigned long rom_table_end)
+static uintptr_t write_coreboot_table(uintptr_t rom_table_end)
 {
 	struct lb_header *head;
 
-	arch_write_tables(rom_table_end);
-
-	if (low_table_start || low_table_end) {
-		printk(BIOS_DEBUG, "Writing table forward entry at 0x%08lx\n",
-				low_table_end);
-		head = lb_table_init(low_table_end);
-		lb_forward(head, (struct lb_header*)rom_table_end);
-
-		low_table_end = (unsigned long) lb_table_fini(head);
-		printk(BIOS_DEBUG, "Table forward entry ends at 0x%08lx.\n",
-			low_table_end);
-		low_table_end = ALIGN(low_table_end, 4096);
-		printk(BIOS_DEBUG, "... aligned to 0x%08lx\n", low_table_end);
-	}
-
 	printk(BIOS_DEBUG, "Writing coreboot table at 0x%08lx\n",
-		rom_table_end);
+		(long)rom_table_end);
 
 	head = lb_table_init(rom_table_end);
 
@@ -505,13 +488,6 @@
 	/* Initialize the memory map at boot time. */
 	bootmem_init();
 
-	if (low_table_start || low_table_end) {
-		uint64_t size = low_table_end - low_table_start;
-		/* Record the mptable and the the lb_table.
-		 * (This will be adjusted later)  */
-		bootmem_add_range(low_table_start, size, LB_MEM_TABLE);
-	}
-
 	/* No other memory areas can be added after the memory table has been
 	 * committed as the entries won't show up in the serialize mem table. */
 	bootmem_write_memory_table(lb_memory(head));
@@ -575,9 +551,38 @@
 	/* Add all cbmem entries into the coreboot tables. */
 	cbmem_add_records_to_cbtable(head);
 
-	/* Print CBMEM sections */
-	cbmem_list();
-
 	/* Remember where my valid memory ranges are */
 	return lb_table_fini(head);
 }
+
+void write_tables(void)
+{
+	uintptr_t cbtable_start;
+	uintptr_t cbtable_end;
+	size_t cbtable_size;
+	const size_t max_table_size = CONFIG_COREBOOT_TABLE_SIZE;
+
+	cbtable_start = (uintptr_t)cbmem_add(CBMEM_ID_CBTABLE, max_table_size);
+
+	if (!cbtable_start) {
+		printk(BIOS_ERR, "Could not add CBMEM for coreboot table.\n");
+		return;
+	}
+
+	/* Add architecture specific tables. */
+	arch_write_tables(cbtable_start);
+
+	/* Write the coreboot table. */
+	cbtable_end = write_coreboot_table(cbtable_start);
+	cbtable_size = cbtable_end - cbtable_start;
+
+	if (cbtable_size > max_table_size) {
+		printk(BIOS_ERR, "%s: coreboot table didn't fit (%zx/%zx)\n",
+			__func__, cbtable_size, max_table_size);
+	}
+
+	printk(BIOS_DEBUG, "coreboot table: %zd bytes.\n", cbtable_size);
+
+	/* Print CBMEM sections */
+	cbmem_list();
+}