probe: Improve probe_frid to support both ARM and X86

To unify probing logic, we want to provide a new 'fdt_get_frid' as
equivalent call to `acpi_get_frid`. The `probe_frid` then can
support both X86 and ARM platforms.

BUG=chromium:982692
TEST=emerge-$BOARD mosys

Change-Id: I1d9b86441a1c470946db1c27a72b4e66a305ce7b
Reviewed-on: https://chromium-review.googlesource.com/1697964
Tested-by: Hung-Te Lin <hungte@chromium.org>
Commit-Ready: Hung-Te Lin <hungte@chromium.org>
Legacy-Commit-Queue: Commit Bot <commit-bot@chromium.org>
Reviewed-by: Hsin-Yi Wang <hsinyi@chromium.org>
diff --git a/include/lib/fdt.h b/include/lib/fdt.h
index 6f63ae1..192f266 100644
--- a/include/lib/fdt.h
+++ b/include/lib/fdt.h
@@ -58,6 +58,16 @@
  */
 extern int fdt_get_sku_id(void);
 
+/*
+ * fdt_get_frid - retrieve FRID and store in a newly allocated buffer
+ *
+ * @buf:	buffer to store read only firmware ID
+ *
+ * returns length of read only firmware id to indicate success
+ * returns <0 to indicate error
+ */
+extern int fdt_get_frid(char **buf);
+
 enum vbnv_storage_media {
 	VBNV_STORAGE_UNKNOWN	= -1,
 	VBNV_STORAGE_CROS_EC,	/* ChromeOS EC */
diff --git a/lib/file/fdt.c b/lib/file/fdt.c
index 72d6135..5706fbd 100644
--- a/lib/file/fdt.c
+++ b/lib/file/fdt.c
@@ -36,10 +36,12 @@
 #include <string.h>
 #include <unistd.h>
 
+#include "lib/chromeos.h"
 #include "lib/fdt.h"
 #include "lib/file.h"
 #include "lib/string_builder.h"
 
+#include "mosys/alloc.h"
 #include "mosys/globals.h"
 #include "mosys/log.h"
 
@@ -49,6 +51,7 @@
 
 #define FDT_ROOT		"/proc/device-tree/"
 #define FDT_VBNV_STORAGE_PATH	"firmware/chromeos/nonvolatile-context-storage"
+#define FDT_FRID_PATH		"firmware/chromeos/readonly-firmware-version"
 /* FIXME: assume coreboot for now */
 #define FDT_RAM_CODE_PATH	"firmware/coreboot/ram-code"
 #define FDT_BOARD_ID_PATH	"firmware/coreboot/board-id"
@@ -148,6 +151,23 @@
 	return sku_id;
 }
 
+int fdt_get_frid(char **buf)
+{
+	int len = CHROMEOS_FRID_MAXLEN;
+	char *frid = mosys_malloc(len);
+
+	len = fdt_read_node(FDT_FRID_PATH, frid, len - 1);
+	if (len < 0) {
+		lprintf(LOG_ERR, "%s: failed to read frid from %s\n",
+			__func__, FDT_FRID_PATH);
+		free(frid);
+	} else {
+		frid[len] = '\0';
+		*buf = frid;
+	}
+	return len;
+}
+
 static enum vbnv_storage_media fdt_get_vbnv_storage(void)
 {
 	char buf[8];
diff --git a/lib/misc/probe.c b/lib/misc/probe.c
index 3a5ff58..7407f26 100644
--- a/lib/misc/probe.c
+++ b/lib/misc/probe.c
@@ -45,6 +45,7 @@
 #include "mosys/platform.h"
 
 #include "lib/acpi.h"
+#include "lib/fdt.h"
 #include "lib/file.h"
 #include "lib/probe.h"
 #include "lib/smbios.h"
@@ -54,6 +55,16 @@
 #define LINE_MAX	512
 #endif
 
+
+static int plat_get_frid(char **buf)
+{
+#ifdef CONFIG_PLATFORM_ARCH_X86
+	return acpi_get_frid(buf);
+#else
+	return fdt_get_frid(buf);
+#endif
+}
+
 int probe_frid(const char *frids[])
 {
 	int ret = 0;
@@ -63,7 +74,7 @@
 	if (!id) {
 		char *raw_frid = NULL, *tmp;
 
-		if (acpi_get_frid(&raw_frid) < 0)
+		if (plat_get_frid(&raw_frid) < 0)
 			goto probe_frid_done;
 
 		/* FRID begins with platform name, followed by a dot, followed