Remove the device tree from one of the vboot support APIs.

This change removes the device tree from one of the vboot support APIs so
that they fit better with coreboot u-boot. Specifically this is
cros_gpio_fetch which is for getting GPIO values. Coreboot will read these and
provide the values through its coreboot tables. The device tree pointer is
global data so it doesn't need to be passed around as a parameter to be
accessible.

This change also removes the device tree from other interfaces in
cmd_vboot_twostop.c so the structure of the code is more consistent. This way
people can expect the device tree to always be the one pointed at by the
global data and not sometimes one and sometimes the other, even though as
written those will be equivalent. This change also takes the opportunity to
mark the local functions in that file static, which is most of them.

BUG=chrome-os-partner:4552
TEST=Built for x86-alex and tegra2_kaen. Installed and booted to chromeos
login on Kaen.

Change-Id: I084e774d97025d9ec71abe09c92fab8a7827892f
Signed-off-by: Gabe Black <gabeblack@google.com>
Reviewed-on: http://gerrit.chromium.org/gerrit/5232
Reviewed-by: Gabe Black <gabeblack@chromium.org>
Tested-by: Gabe Black <gabeblack@chromium.org>
diff --git a/board/chromebook-x86/chromeos/cros_gpio.c b/board/chromebook-x86/chromeos/cros_gpio.c
index 12d29b1..3d0f733 100644
--- a/board/chromebook-x86/chromeos/cros_gpio.c
+++ b/board/chromebook-x86/chromeos/cros_gpio.c
@@ -13,8 +13,7 @@
 #include <chromeos/cros_gpio.h>
 #include <common.h>
 
-int cros_gpio_fetch(enum cros_gpio_index index, const void *fdt,
-		cros_gpio_t *gpio)
+int cros_gpio_fetch(enum cros_gpio_index index, cros_gpio_t *gpio)
 {
 	printf("cros_gpio_fetch used but not implemented.\n");
 	return 0;
diff --git a/board/nvidia/chromeos/cros_gpio.c b/board/nvidia/chromeos/cros_gpio.c
index 4c6c198..10c8a40 100644
--- a/board/nvidia/chromeos/cros_gpio.c
+++ b/board/nvidia/chromeos/cros_gpio.c
@@ -14,13 +14,15 @@
 #include <fdt_decode.h>
 #include <asm/arch/gpio.h>
 #include <asm/arch/tegra2.h>
+#include <asm/global_data.h>
 #include <chromeos/common.h>
 #include <chromeos/cros_gpio.h>
 
 #define PREFIX "cros_gpio: "
 
-int cros_gpio_fetch(enum cros_gpio_index index, const void *fdt,
-		cros_gpio_t *gpio)
+DECLARE_GLOBAL_DATA_PTR;
+
+int cros_gpio_fetch(enum cros_gpio_index index, cros_gpio_t *gpio)
 {
 	const char const *port[CROS_GPIO_MAX_GPIO] = {
 		"gpio_port_write_protect_switch",
@@ -45,7 +47,7 @@
 
 	gpio->index = index;
 
-	gpio->port = fdt_decode_get_config_int(fdt, port[index], -1);
+	gpio->port = fdt_decode_get_config_int(gd->blob, port[index], -1);
 	if (gpio->port == -1) {
 		VBDEBUG(PREFIX "failed to decode gpio port\n");
 		return -1;
@@ -53,7 +55,8 @@
 
 	gpio_direction_input(gpio->port);
 
-	gpio->polarity = fdt_decode_get_config_int(fdt, polarity[index], -1);
+	gpio->polarity =
+		fdt_decode_get_config_int(gd->blob, polarity[index], -1);
 	if (gpio->polarity == -1) {
 		VBDEBUG(PREFIX "failed to decode gpio polarity\n");
 		return -1;
diff --git a/common/cmd_vboot_test.c b/common/cmd_vboot_test.c
index 5475461..8521a62 100644
--- a/common/cmd_vboot_test.c
+++ b/common/cmd_vboot_test.c
@@ -27,8 +27,6 @@
 #define TEST_FW_START		0xc1000
 #define DEFAULT_TEST_FW_LENGTH	0x1000
 
-DECLARE_GLOBAL_DATA_PTR;
-
 static int do_vboot_test_fwrw(cmd_tbl_t *cmdtp,
 		int flag, int argc, char * const argv[])
 {
@@ -155,12 +153,11 @@
 static int do_vboot_test_gpio(cmd_tbl_t *cmdtp,
 		int flag, int argc, char * const argv[])
 {
-	void *fdt_ptr = (void *)gd->blob;
 	cros_gpio_t gpio;
 	int i;
 
 	for (i = 0; i < CROS_GPIO_MAX_GPIO; i++) {
-		if (cros_gpio_fetch(i, fdt_ptr, &gpio)) {
+		if (cros_gpio_fetch(i, &gpio)) {
 			VbExDebug("Failed to fetch GPIO, %d!\n", i);
 			return 1;
 		}
diff --git a/common/cmd_vboot_twostop.c b/common/cmd_vboot_twostop.c
index 1e72663..64a8ad2 100644
--- a/common/cmd_vboot_twostop.c
+++ b/common/cmd_vboot_twostop.c
@@ -101,7 +101,8 @@
 #define VB_SELECT_COMMAND_LINE	0xfd
 
 #ifdef VBOOT_DEBUG
-const char *str_selection(uint32_t selection)
+static const char *
+str_selection(uint32_t selection)
 {
 	static const char const *str[] = {
 		"VB_SELECT_FIRMWARE_RECOVERY",
@@ -121,10 +122,9 @@
 }
 #endif /* VBOOT_DEBUG */
 
-int twostop_init_cparams(struct twostop_fmap *fmap,
-		void *gbb,
-		void *vb_shared_data,
-		VbCommonParams *cparams)
+static int
+twostop_init_cparams(struct twostop_fmap *fmap, void *gbb,
+		     void *vb_shared_data, VbCommonParams *cparams)
 {
 	cparams->gbb_data = gbb;
 	cparams->gbb_size = fmap->readonly.gbb.length;
@@ -146,7 +146,8 @@
 }
 
 #ifdef CONFIG_OF_CONTROL
-static uintptr_t get_current_sp(void)
+static uintptr_t
+get_current_sp(void)
 {
 	uintptr_t addr;
 
@@ -155,14 +156,14 @@
 }
 #endif
 
-static void wipe_unused_memory(const void const *fdt,
-		crossystem_data_t *cdata, VbCommonParams *cparams)
+static void
+wipe_unused_memory(crossystem_data_t *cdata, VbCommonParams *cparams)
 {
 #ifdef CONFIG_OF_CONTROL
 	memory_wipe_t wipe;
 	struct fdt_memory config;
 
-	if (fdt_decode_memory(fdt, &config))
+	if (fdt_decode_memory(gd->blob, &config))
 		VbExError(PREFIX "FDT decode memory section error\n");
 
 	memory_wipe_init(&wipe, config.start, config.end);
@@ -188,16 +189,15 @@
 #endif
 }
 
-VbError_t twostop_init_vboot_library(const void const *fdt,
-		firmware_storage_t *file,
-		void *gbb, uint32_t gbb_offset,
-		crossystem_data_t *cdata,
-		VbCommonParams *cparams)
+static VbError_t
+twostop_init_vboot_library(firmware_storage_t *file, void *gbb,
+			   uint32_t gbb_offset, crossystem_data_t *cdata,
+			   VbCommonParams *cparams)
 {
 	VbError_t err;
 	VbInitParams iparams;
 
-	if (fdt_decode_chromeos_config_has_prop(fdt, "twostop-optional")) {
+	if (fdt_decode_chromeos_config_has_prop(gd->blob, "twostop-optional")) {
 		VBDEBUG(PREFIX "twostop-optional\n");
 		iparams.flags = VB_INIT_FLAG_RO_NORMAL_SUPPORT;
 	} else {
@@ -221,7 +221,7 @@
 	VBDEBUG(PREFIX "iparams.out_flags: %08x\n", iparams.out_flags);
 
 	if (iparams.out_flags & VB_INIT_OUT_CLEAR_RAM)
-		wipe_unused_memory(fdt, cdata, cparams);
+		wipe_unused_memory(cdata, cparams);
 
 	/* Load required information of GBB */
 	if (iparams.out_flags & VB_INIT_OUT_ENABLE_DISPLAY)
@@ -234,11 +234,10 @@
 	return VBERROR_SUCCESS;
 }
 
-uint32_t twostop_make_selection(struct twostop_fmap *fmap,
-		firmware_storage_t *file,
-		VbCommonParams *cparams,
-		void **fw_blob_ptr,
-		uint32_t *fw_size_ptr)
+static uint32_t
+twostop_make_selection(struct twostop_fmap *fmap, firmware_storage_t *file,
+		       VbCommonParams *cparams, void **fw_blob_ptr,
+		       uint32_t *fw_size_ptr)
 {
 	uint32_t selection = VB_SELECT_ERROR;
 	VbError_t err;
@@ -324,13 +323,12 @@
 	return selection;
 }
 
-uint32_t twostop_select_and_set_main_firmware(const void const *fdt,
-		struct twostop_fmap *fmap,
-		firmware_storage_t *file,
-		void *gbb,
-		crossystem_data_t *cdata,
-		void *vb_shared_data,
-		int *boot_mode, void **fw_blob_ptr, uint32_t *fw_size_ptr)
+static uint32_t
+twostop_select_and_set_main_firmware(struct twostop_fmap *fmap,
+				     firmware_storage_t *file,
+				     void *gbb, crossystem_data_t *cdata,
+				     void *vb_shared_data, int *boot_mode,
+				     void **fw_blob_ptr, uint32_t *fw_size_ptr)
 {
 	uint32_t selection;
 	uint32_t id_offset = 0, id_length = 0;
@@ -343,8 +341,8 @@
 		return VB_SELECT_ERROR;
 	}
 
-	if (twostop_init_vboot_library(fdt, file,
-				gbb, fmap->readonly.gbb.offset, cdata, &cparams)
+	if (twostop_init_vboot_library(file, gbb, fmap->readonly.gbb.offset,
+				       cdata, &cparams)
 			!= VBERROR_SUCCESS) {
 		VBDEBUG(PREFIX "failed to init vboot library\n");
 		return VB_SELECT_ERROR;
@@ -405,7 +403,8 @@
 	return selection;
 }
 
-uint32_t twostop_jump(crossystem_data_t *cdata, void *fw_blob, uint32_t fw_size)
+static uint32_t
+twostop_jump(crossystem_data_t *cdata, void *fw_blob, uint32_t fw_size)
 {
 	VBDEBUG(PREFIX "jump to readwrite main firmware at %#x, size %#x\n",
 			CONFIG_SYS_TEXT_BASE, fw_size);
@@ -429,21 +428,18 @@
 	return VB_SELECT_ERROR;
 }
 
-int twostop_init(const void const *fdt,
-		struct twostop_fmap *fmap,
-		firmware_storage_t *file,
-		void *gbb,
-		crossystem_data_t *cdata,
-		void *vb_shared_data)
+static int
+twostop_init(struct twostop_fmap *fmap, firmware_storage_t *file,
+	     void *gbb, crossystem_data_t *cdata, void *vb_shared_data)
 {
 	cros_gpio_t wpsw, recsw, devsw;
 	GoogleBinaryBlockHeader *gbbh = (GoogleBinaryBlockHeader *)gbb;
 	uint8_t hardware_id[ID_LEN], readonly_firmware_id[ID_LEN];
 	int ret = -1;
 
-	if (cros_gpio_fetch(CROS_GPIO_WPSW, fdt, &wpsw) ||
-			cros_gpio_fetch(CROS_GPIO_RECSW, fdt, &recsw) ||
-			cros_gpio_fetch(CROS_GPIO_DEVSW, fdt, &devsw)) {
+	if (cros_gpio_fetch(CROS_GPIO_WPSW, &wpsw) ||
+			cros_gpio_fetch(CROS_GPIO_RECSW, &recsw) ||
+			cros_gpio_fetch(CROS_GPIO_DEVSW, &devsw)) {
 		VBDEBUG(PREFIX "failed to fetch gpio\n");
 		return -1;
 	}
@@ -451,7 +447,7 @@
 	cros_gpio_dump(&recsw);
 	cros_gpio_dump(&devsw);
 
-	if (fdt_decode_twostop_fmap(fdt, fmap)) {
+	if (fdt_decode_twostop_fmap(gd->blob, fmap)) {
 		VBDEBUG(PREFIX "failed to decode fmap\n");
 		return -1;
 	}
@@ -508,10 +504,9 @@
 	return ret;
 }
 
-uint32_t twostop_main_firmware(struct twostop_fmap *fmap,
-		void *gbb,
-		crossystem_data_t *cdata,
-		void *vb_shared_data)
+static uint32_t
+twostop_main_firmware(struct twostop_fmap *fmap, void *gbb,
+		      crossystem_data_t *cdata, void *vb_shared_data)
 {
 	VbError_t err;
 	VbSelectAndLoadKernelParams kparams;
@@ -561,7 +556,8 @@
 	return VB_SELECT_ERROR;
 }
 
-uint32_t twostop_boot(const void const *fdt)
+static uint32_t
+twostop_boot(void)
 {
 	struct twostop_fmap fmap;
 	firmware_storage_t file;
@@ -573,12 +569,12 @@
 	uint32_t selection;
 	int boot_mode = FIRMWARE_TYPE_NORMAL;
 
-	if (twostop_init(fdt, &fmap, &file, gbb, cdata, vb_shared_data)) {
+	if (twostop_init(&fmap, &file, gbb, cdata, vb_shared_data)) {
 		VBDEBUG(PREFIX "failed to init twostop boot\n");
 		return VB_SELECT_ERROR;
 	}
 
-	selection = twostop_select_and_set_main_firmware(fdt, &fmap, &file,
+	selection = twostop_select_and_set_main_firmware(&fmap, &file,
 			gbb, cdata, vb_shared_data,
 			&boot_mode, &fw_blob, &fw_size);
 	VBDEBUG(PREFIX "selection of bootstub: %s\n", str_selection(selection));
@@ -617,7 +613,8 @@
 	return VB_SELECT_COMMAND_LINE;
 }
 
-uint32_t twostop_readwrite_main_firmware(const void const *fdt)
+static uint32_t
+twostop_readwrite_main_firmware(void)
 {
 	struct twostop_fmap fmap;
 	crossystem_data_t *cdata = (crossystem_data_t *)CROSSYSTEM_DATA_ADDRESS;
@@ -636,7 +633,7 @@
 		return VB_SELECT_ERROR;
 	}
 
-	if (fdt_decode_twostop_fmap(fdt, &fmap)) {
+	if (fdt_decode_twostop_fmap(gd->blob, &fmap)) {
 		VBDEBUG(PREFIX "failed to decode fmap\n");
 		return VB_SELECT_ERROR;
 	}
@@ -659,9 +656,9 @@
 	return twostop_main_firmware(&fmap, gbb, cdata, vb_shared_data);
 }
 
-int do_vboot_twostop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+static int
+do_vboot_twostop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-	const void const *fdt = gd->blob;
 	uint32_t selection;
 
 	/*
@@ -681,9 +678,9 @@
 	 * readwrite firmware code path.
 	 */
 	if (is_processor_reset())
-		selection = twostop_boot(fdt);
+		selection = twostop_boot();
 	else
-		selection = twostop_readwrite_main_firmware(fdt);
+		selection = twostop_readwrite_main_firmware();
 
 	VBDEBUG(PREFIX "selection of main firmware: %s\n",
 			str_selection(selection));
diff --git a/include/chromeos/cros_gpio.h b/include/chromeos/cros_gpio.h
index 2502d69..dd01634 100644
--- a/include/chromeos/cros_gpio.h
+++ b/include/chromeos/cros_gpio.h
@@ -35,8 +35,7 @@
 	int value;
 } cros_gpio_t;
 
-int cros_gpio_fetch(enum cros_gpio_index index, const void *fdt,
-		cros_gpio_t *gpio);
+int cros_gpio_fetch(enum cros_gpio_index index, cros_gpio_t *gpio);
 
 int cros_gpio_dump(cros_gpio_t *gpio);
 
diff --git a/lib/vbexport/misc.c b/lib/vbexport/misc.c
index cab7a4c..9a608ab 100644
--- a/lib/vbexport/misc.c
+++ b/lib/vbexport/misc.c
@@ -15,15 +15,12 @@
 
 #define PREFIX			"misc: "
 
-DECLARE_GLOBAL_DATA_PTR;
-
 uint32_t VbExIsShutdownRequested(void)
 {
-	void *fdt_ptr = (void *)gd->blob;
 	cros_gpio_t lidsw, pwrsw;
 
-	if (cros_gpio_fetch(CROS_GPIO_LIDSW, fdt_ptr, &lidsw) ||
-			cros_gpio_fetch(CROS_GPIO_PWRSW, fdt_ptr, &pwrsw)) {
+	if (cros_gpio_fetch(CROS_GPIO_LIDSW, &lidsw) ||
+			cros_gpio_fetch(CROS_GPIO_PWRSW, &pwrsw)) {
 		VBDEBUG(PREFIX "Failed to fetch GPIO!\n");
 		/* still return 0, No-Shutdown-Requested */
 		return 0;
diff --git a/lib/vboot/global_data.c b/lib/vboot/global_data.c
index 0070d3f..1442096 100644
--- a/lib/vboot/global_data.c
+++ b/lib/vboot/global_data.c
@@ -29,7 +29,6 @@
 
 int init_vboot_global(vb_global_t *global, firmware_storage_t *file)
 {
-	void *fdt_ptr = (void *)gd->blob;
 	cros_gpio_t wpsw, recsw, devsw;
 	struct twostop_fmap fmap;
 	uint8_t frid[ID_LEN];
@@ -41,14 +40,14 @@
 			VBGLOBAL_SIGNATURE_SIZE);
 
 	/* Gets GPIO status */
-	if (cros_gpio_fetch(CROS_GPIO_WPSW, fdt_ptr, &wpsw) ||
-			cros_gpio_fetch(CROS_GPIO_RECSW, fdt_ptr, &recsw) ||
-			cros_gpio_fetch(CROS_GPIO_DEVSW, fdt_ptr, &devsw)) {
+	if (cros_gpio_fetch(CROS_GPIO_WPSW, &wpsw) ||
+			cros_gpio_fetch(CROS_GPIO_RECSW, &recsw) ||
+			cros_gpio_fetch(CROS_GPIO_DEVSW, &devsw)) {
 		VBDEBUG(PREFIX "Failed to fetch GPIO!\n");
 		return 1;
         }
 
-	if (fdt_decode_twostop_fmap(fdt_ptr, &fmap)) {
+	if (fdt_decode_twostop_fmap(gd->blob, &fmap)) {
 		VBDEBUG(PREFIX "Failed to load fmap config from fdt!\n");
 		return 1;
 	}