vboot: make VbExDisplayScreen call vboot_draw_screen

This change makes VbExDisplayScreen call vboot_draw_screen, which will
draw screens using cbgfx. screens.c will be populated in CL:303074.

BUG=none
BRANCH=tot
TEST=Tested on Samus

Change-Id: Ic98a3d8b2829a1cc3ac142eb9cb52c3d76fc78cd
Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/303181
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Reviewed-by: Duncan Laurie <dlaurie@chromium.org>
diff --git a/src/vboot/Makefile.inc b/src/vboot/Makefile.inc
index 2f47610..eedb081 100644
--- a/src/vboot/Makefile.inc
+++ b/src/vboot/Makefile.inc
@@ -21,6 +21,7 @@
 depthcharge-$(CONFIG_FASTBOOT_MODE) += fastboot.c
 depthcharge-y += boot_policy.c
 depthcharge-y += firmware_id.c
+depthcharge-y += screens.c
 
 # Where "main" lives.
 readonly-y += ro_main.c
diff --git a/src/vboot/callbacks/display.c b/src/vboot/callbacks/display.c
index efa0691..536c430 100644
--- a/src/vboot/callbacks/display.c
+++ b/src/vboot/callbacks/display.c
@@ -31,15 +31,14 @@
 #include "drivers/video/display.h"
 #include "vboot/firmware_id.h"
 #include "vboot/util/commonparams.h"
+#include "vboot/screens.h"
 
 VbError_t VbExDisplayInit(uint32_t *width, uint32_t *height)
 {
-	if (display_init())
+	const struct rgb_color rgb = { .red = 0, .green = 0, .blue = 0 };
+	if (clear_screen(&rgb))
 		return VBERROR_UNKNOWN;
 
-	video_init();
-	video_console_cursor_enable(0);
-
 	if (gbb_copy_in_bmp_block())
 		return VBERROR_UNKNOWN;
 
@@ -49,6 +48,7 @@
 	} else {
 		*width = *height = 0;
 	}
+
 	return VBERROR_SUCCESS;
 }
 
@@ -88,6 +88,9 @@
 {
 	const char *msg = NULL;
 
+	if (vboot_draw_screen(screen_type) == CBGFX_SUCCESS)
+		return VBERROR_SUCCESS;
+
 	/*
 	 * Show the debug messages for development. It is a backup method
 	 * when GBB does not contain a full set of bitmaps.
diff --git a/src/vboot/screens.c b/src/vboot/screens.c
new file mode 100644
index 0000000..c3c17fd
--- /dev/null
+++ b/src/vboot/screens.c
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but without any warranty; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <libpayload.h>
+#include <vboot_api.h>
+#include <vboot/screens.h>
+#include "drivers/video/display.h"
+
+static char initialized = 0;
+
+static VbError_t draw_screen(uint32_t screen_type, uint32_t locale)
+{
+	VbError_t rv = VBERROR_SUCCESS;
+
+	switch (screen_type) {
+	case VB_SCREEN_BLANK:
+	case VB_SCREEN_DEVELOPER_WARNING:
+	case VB_SCREEN_RECOVERY_REMOVE:
+	case VB_SCREEN_RECOVERY_NO_GOOD:
+	case VB_SCREEN_RECOVERY_INSERT:
+	case VB_SCREEN_RECOVERY_TO_DEV:
+	case VB_SCREEN_DEVELOPER_TO_NORM:
+	case VB_SCREEN_WAIT:
+	case VB_SCREEN_TO_NORM_CONFIRMED:
+	default:
+		printf("Not a valid screen type: 0x%x\n", screen_type);
+		return VBERROR_INVALID_SCREEN_INDEX;
+	}
+
+	return rv;
+}
+
+static VbError_t vboot_init_display(void)
+{
+	if (display_init())
+		return VBERROR_UNKNOWN;
+
+	/* initialize video console */
+	video_init();
+	video_console_clear();
+	video_console_cursor_enable(0);
+	initialized = 1;
+
+	return VBERROR_SUCCESS;
+}
+
+int vboot_draw_screen(uint32_t screen)
+{
+	static uint32_t current_screen = VB_SCREEN_BLANK;
+	static uint32_t current_locale = 0;
+	/* TODO: Read locale from nvram */
+	uint32_t locale = 0;
+	VbError_t rv;
+
+	printf("%s: screen=0x%x locale=%d\n", __func__, screen, locale);
+
+	if (!initialized) {
+		if (vboot_init_display())
+			return VBERROR_UNKNOWN;
+	}
+
+	/* If requested screen is the same as the current one, we're done. */
+	if (screen == current_screen && locale == current_locale)
+		return VBERROR_SUCCESS;
+
+	/* If the screen is blank, turn off the backlight; else turn it on. */
+	backlight_update(VB_SCREEN_BLANK == screen ? 0 : 1);
+
+	/* TODO: draw only locale dependent part if current_screen == screen */
+	rv = draw_screen(screen, locale);
+	if (rv)
+		return rv;
+
+	current_screen = screen;
+	current_locale = locale;
+
+	return VBERROR_SUCCESS;
+}
diff --git a/src/vboot/screens.h b/src/vboot/screens.h
new file mode 100644
index 0000000..7f64f2d
--- /dev/null
+++ b/src/vboot/screens.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but without any warranty; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#ifndef __VBOOT_SCREENS_H__
+#define __VBOOT_SCREENS_H__
+
+#include <stdint.h>
+
+int vboot_draw_screen(uint32_t screen);
+
+#endif /* __VBOOT_SCREENS_H__ */