a5bd9f6
From 839b333ad80db4f39a97b7aedc927147794e576b Mon Sep 17 00:00:00 2001
a5bd9f6
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
a5bd9f6
Date: Sun, 21 Apr 2013 13:02:10 +0200
a5bd9f6
Subject: [PATCH 332/364] 	Support coreboot framebuffer.
a5bd9f6
a5bd9f6
	* grub-core/video/i386/coreboot/cbfb.c: New file.
a5bd9f6
---
a5bd9f6
 ChangeLog                               |   6 ++
a5bd9f6
 grub-core/Makefile.core.def             |   6 ++
a5bd9f6
 grub-core/commands/i386/coreboot/cbls.c |  16 ++-
a5bd9f6
 grub-core/video/i386/coreboot/cbfb.c    | 180 ++++++++++++++++++++++++++++++++
a5bd9f6
 include/grub/i386/coreboot/lbio.h       |  36 +++++--
a5bd9f6
 5 files changed, 236 insertions(+), 8 deletions(-)
a5bd9f6
 create mode 100644 grub-core/video/i386/coreboot/cbfb.c
a5bd9f6
a5bd9f6
diff --git a/ChangeLog b/ChangeLog
a5bd9f6
index 2a4264c..6be583e 100644
a5bd9f6
--- a/ChangeLog
a5bd9f6
+++ b/ChangeLog
a5bd9f6
@@ -1,3 +1,9 @@
a5bd9f6
+2013-04-21  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
+
a5bd9f6
+	Support coreboot framebuffer.
a5bd9f6
+
a5bd9f6
+	* grub-core/video/i386/coreboot/cbfb.c: New file.
a5bd9f6
+
a5bd9f6
 2013-04-20  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
 
a5bd9f6
 	* grub-core/kern/mm.c (grub_mm_init_region): Fix condition for
a5bd9f6
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
a5bd9f6
index 1f04afb..7269609 100644
a5bd9f6
--- a/grub-core/Makefile.core.def
a5bd9f6
+++ b/grub-core/Makefile.core.def
a5bd9f6
@@ -1797,6 +1797,12 @@ module = {
a5bd9f6
 };
a5bd9f6
 
a5bd9f6
 module = {
a5bd9f6
+  name = coreboot_fb;
a5bd9f6
+  common = video/i386/coreboot/cbfb.c;
a5bd9f6
+  enable = i386_coreboot;
a5bd9f6
+};
a5bd9f6
+
a5bd9f6
+module = {
a5bd9f6
   name = sdl;
a5bd9f6
   emu = video/emu/sdl.c;
a5bd9f6
   enable = emu;
a5bd9f6
diff --git a/grub-core/commands/i386/coreboot/cbls.c b/grub-core/commands/i386/coreboot/cbls.c
a5bd9f6
index 151f9e8..a3542f3 100644
a5bd9f6
--- a/grub-core/commands/i386/coreboot/cbls.c
a5bd9f6
+++ b/grub-core/commands/i386/coreboot/cbls.c
a5bd9f6
@@ -50,7 +50,7 @@ static const char *descs[] = {
a5bd9f6
   [0xd] = "assembler",
a5bd9f6
   [0xf] = "serial",
a5bd9f6
   [GRUB_LINUXBIOS_MEMBER_CONSOLE] = "console",
a5bd9f6
-  [0x12] = "framebuffer",
a5bd9f6
+  [GRUB_LINUXBIOS_MEMBER_FRAMEBUFFER] = "framebuffer",
a5bd9f6
   [0x13] = "GPIO",
a5bd9f6
   [0x15] = "VDAT",
a5bd9f6
   [GRUB_LINUXBIOS_MEMBER_TIMESTAMPS] = "timestamps (`coreboot_boottime' to list)",
a5bd9f6
@@ -77,6 +77,20 @@ iterate_linuxbios_table (grub_linuxbios_table_item_t table_item,
a5bd9f6
 
a5bd9f6
   switch (table_item->tag)
a5bd9f6
     {
a5bd9f6
+    case GRUB_LINUXBIOS_MEMBER_FRAMEBUFFER:
a5bd9f6
+      {
a5bd9f6
+	struct grub_linuxbios_table_framebuffer *fb;
a5bd9f6
+	fb = (struct grub_linuxbios_table_framebuffer *) (table_item + 1);
a5bd9f6
+
a5bd9f6
+	grub_printf (": %dx%dx%d pitch=%d lfb=0x%llx %d/%d/%d/%d %d/%d/%d/%d",
a5bd9f6
+		     fb->width, fb->height,
a5bd9f6
+		     fb->bpp, fb->pitch, fb->lfb,
a5bd9f6
+		     fb->red_mask_size, fb->green_mask_size,
a5bd9f6
+		     fb->blue_mask_size, fb->reserved_mask_size,
a5bd9f6
+		     fb->red_field_pos, fb->green_field_pos,
a5bd9f6
+		     fb->blue_field_pos, fb->reserved_field_pos);
a5bd9f6
+	break;
a5bd9f6
+      }
a5bd9f6
     case GRUB_LINUXBIOS_MEMBER_MAINBOARD:
a5bd9f6
       {
a5bd9f6
 	struct grub_linuxbios_mainboard *mb;
a5bd9f6
diff --git a/grub-core/video/i386/coreboot/cbfb.c b/grub-core/video/i386/coreboot/cbfb.c
a5bd9f6
new file mode 100644
a5bd9f6
index 0000000..000efdb
a5bd9f6
--- /dev/null
a5bd9f6
+++ b/grub-core/video/i386/coreboot/cbfb.c
a5bd9f6
@@ -0,0 +1,180 @@
a5bd9f6
+/*
a5bd9f6
+ *  GRUB  --  GRand Unified Bootloader
a5bd9f6
+ *  Copyright (C) 2005,2006,2007,2008,2009  Free Software Foundation, Inc.
a5bd9f6
+ *
a5bd9f6
+ *  GRUB is free software: you can redistribute it and/or modify
a5bd9f6
+ *  it under the terms of the GNU General Public License as published by
a5bd9f6
+ *  the Free Software Foundation, either version 3 of the License, or
a5bd9f6
+ *  (at your option) any later version.
a5bd9f6
+ *
a5bd9f6
+ *  GRUB is distributed in the hope that it will be useful,
a5bd9f6
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
a5bd9f6
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a5bd9f6
+ *  GNU General Public License for more details.
a5bd9f6
+ *
a5bd9f6
+ *  You should have received a copy of the GNU General Public License
a5bd9f6
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
a5bd9f6
+ */
a5bd9f6
+
a5bd9f6
+#define grub_video_render_target grub_video_fbrender_target
a5bd9f6
+
a5bd9f6
+#include <grub/err.h>
a5bd9f6
+#include <grub/types.h>
a5bd9f6
+#include <grub/dl.h>
a5bd9f6
+#include <grub/misc.h>
a5bd9f6
+#include <grub/mm.h>
a5bd9f6
+#include <grub/video.h>
a5bd9f6
+#include <grub/video_fb.h>
a5bd9f6
+#include <grub/machine/lbio.h>
a5bd9f6
+
a5bd9f6
+GRUB_MOD_LICENSE ("GPLv3+");
a5bd9f6
+
a5bd9f6
+static struct grub_linuxbios_table_framebuffer *cbfb;
a5bd9f6
+
a5bd9f6
+static struct
a5bd9f6
+{
a5bd9f6
+  struct grub_video_mode_info mode_info;
a5bd9f6
+  grub_uint8_t *ptr;
a5bd9f6
+} framebuffer;
a5bd9f6
+
a5bd9f6
+static grub_err_t
a5bd9f6
+grub_video_cbfb_init (void)
a5bd9f6
+{
a5bd9f6
+  grub_memset (&framebuffer, 0, sizeof(framebuffer));
a5bd9f6
+
a5bd9f6
+  return grub_video_fb_init ();
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+static grub_err_t
a5bd9f6
+grub_video_cbfb_fill_mode_info (struct grub_video_mode_info *out)
a5bd9f6
+{
a5bd9f6
+  grub_memset (out, 0, sizeof (*out));
a5bd9f6
+
a5bd9f6
+  out->width = cbfb->width;
a5bd9f6
+  out->height = cbfb->height;
a5bd9f6
+  out->pitch = cbfb->pitch;
a5bd9f6
+
a5bd9f6
+  out->red_field_pos = cbfb->red_field_pos;
a5bd9f6
+  out->red_mask_size = cbfb->red_mask_size;
a5bd9f6
+  out->green_field_pos = cbfb->green_field_pos;
a5bd9f6
+  out->green_mask_size = cbfb->green_mask_size;
a5bd9f6
+  out->blue_field_pos = cbfb->blue_field_pos;
a5bd9f6
+  out->blue_mask_size = cbfb->blue_mask_size;
a5bd9f6
+  out->reserved_field_pos = cbfb->reserved_field_pos;
a5bd9f6
+  out->reserved_mask_size = cbfb->reserved_mask_size;
a5bd9f6
+
a5bd9f6
+  out->mode_type = GRUB_VIDEO_MODE_TYPE_RGB;
a5bd9f6
+  out->bpp = cbfb->bpp;
a5bd9f6
+  out->bytes_per_pixel = (cbfb->bpp + 7) / 8;
a5bd9f6
+  out->number_of_colors = 256;
a5bd9f6
+
a5bd9f6
+  out->blit_format = grub_video_get_blit_format (out);
a5bd9f6
+  return GRUB_ERR_NONE;
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+static grub_err_t
a5bd9f6
+grub_video_cbfb_setup (unsigned int width, unsigned int height,
a5bd9f6
+			   unsigned int mode_type __attribute__ ((unused)),
a5bd9f6
+			   unsigned int mode_mask __attribute__ ((unused)))
a5bd9f6
+{
a5bd9f6
+  grub_err_t err;
a5bd9f6
+
a5bd9f6
+  if (!cbfb)
a5bd9f6
+    return grub_error (GRUB_ERR_IO, "Couldn't find display device.");
a5bd9f6
+
a5bd9f6
+  if (!((width == cbfb->width && height == cbfb->height)
a5bd9f6
+	|| (width == 0 && height == 0)))
a5bd9f6
+    return grub_error (GRUB_ERR_IO, "can't set mode %dx%d", width, height);
a5bd9f6
+
a5bd9f6
+  err = grub_video_cbfb_fill_mode_info (&framebuffer.mode_info);
a5bd9f6
+  if (err)
a5bd9f6
+    {
a5bd9f6
+      grub_dprintf ("video", "CBFB: couldn't fill mode info\n");
a5bd9f6
+      return err;
a5bd9f6
+    }
a5bd9f6
+
a5bd9f6
+  framebuffer.ptr = (void *) (grub_addr_t) cbfb->lfb;
a5bd9f6
+
a5bd9f6
+  grub_dprintf ("video", "CBFB: initialising FB @ %p %dx%dx%d\n",
a5bd9f6
+		framebuffer.ptr, framebuffer.mode_info.width,
a5bd9f6
+		framebuffer.mode_info.height, framebuffer.mode_info.bpp);
a5bd9f6
+
a5bd9f6
+  err = grub_video_fb_setup (mode_type, mode_mask,
a5bd9f6
+			     &framebuffer.mode_info,
a5bd9f6
+			     framebuffer.ptr, NULL, NULL);
a5bd9f6
+  if (err)
a5bd9f6
+    return err;
a5bd9f6
+
a5bd9f6
+  grub_video_fb_set_palette (0, GRUB_VIDEO_FBSTD_NUMCOLORS,
a5bd9f6
+			     grub_video_fbstd_colors);
a5bd9f6
+    
a5bd9f6
+  return err;
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+static grub_err_t
a5bd9f6
+grub_video_cbfb_get_info_and_fini (struct grub_video_mode_info *mode_info,
a5bd9f6
+				  void **framebuf)
a5bd9f6
+{
a5bd9f6
+  grub_memcpy (mode_info, &(framebuffer.mode_info), sizeof (*mode_info));
a5bd9f6
+  *framebuf = (char *) framebuffer.ptr;
a5bd9f6
+
a5bd9f6
+  grub_video_fb_fini ();
a5bd9f6
+
a5bd9f6
+  return GRUB_ERR_NONE;
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+static struct grub_video_adapter grub_video_cbfb_adapter =
a5bd9f6
+  {
a5bd9f6
+    .name = "Coreboot video driver",
a5bd9f6
+
a5bd9f6
+    .prio = GRUB_VIDEO_ADAPTER_PRIO_FIRMWARE_DIRTY,
a5bd9f6
+
a5bd9f6
+    .init = grub_video_cbfb_init,
a5bd9f6
+    .fini = grub_video_fb_fini,
a5bd9f6
+    .setup = grub_video_cbfb_setup,
a5bd9f6
+    .get_info = grub_video_fb_get_info,
a5bd9f6
+    .get_info_and_fini = grub_video_cbfb_get_info_and_fini,
a5bd9f6
+    .set_palette = grub_video_fb_set_palette,
a5bd9f6
+    .get_palette = grub_video_fb_get_palette,
a5bd9f6
+    .set_viewport = grub_video_fb_set_viewport,
a5bd9f6
+    .get_viewport = grub_video_fb_get_viewport,
a5bd9f6
+    .map_color = grub_video_fb_map_color,
a5bd9f6
+    .map_rgb = grub_video_fb_map_rgb,
a5bd9f6
+    .map_rgba = grub_video_fb_map_rgba,
a5bd9f6
+    .unmap_color = grub_video_fb_unmap_color,
a5bd9f6
+    .fill_rect = grub_video_fb_fill_rect,
a5bd9f6
+    .blit_bitmap = grub_video_fb_blit_bitmap,
a5bd9f6
+    .blit_render_target = grub_video_fb_blit_render_target,
a5bd9f6
+    .scroll = grub_video_fb_scroll,
a5bd9f6
+    .swap_buffers = grub_video_fb_swap_buffers,
a5bd9f6
+    .create_render_target = grub_video_fb_create_render_target,
a5bd9f6
+    .delete_render_target = grub_video_fb_delete_render_target,
a5bd9f6
+    .set_active_render_target = grub_video_fb_set_active_render_target,
a5bd9f6
+    .get_active_render_target = grub_video_fb_get_active_render_target,
a5bd9f6
+
a5bd9f6
+    .next = 0
a5bd9f6
+  };
a5bd9f6
+
a5bd9f6
+static int
a5bd9f6
+iterate_linuxbios_table (grub_linuxbios_table_item_t table_item,
a5bd9f6
+			 void *data __attribute__ ((unused)))
a5bd9f6
+{
a5bd9f6
+  if (table_item->tag != GRUB_LINUXBIOS_MEMBER_FRAMEBUFFER)
a5bd9f6
+    return 0;
a5bd9f6
+  cbfb = (struct grub_linuxbios_table_framebuffer *) (table_item + 1);
a5bd9f6
+  return 1;
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+GRUB_MOD_INIT(coreboot_fb)
a5bd9f6
+{
a5bd9f6
+  grub_linuxbios_table_iterate (iterate_linuxbios_table, 0);
a5bd9f6
+
a5bd9f6
+  if (cbfb)
a5bd9f6
+    grub_video_register (&grub_video_cbfb_adapter);
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+GRUB_MOD_FINI(coreboot_fb)
a5bd9f6
+{
a5bd9f6
+  if (cbfb)
a5bd9f6
+    grub_video_unregister (&grub_video_cbfb_adapter);
a5bd9f6
+}
a5bd9f6
diff --git a/include/grub/i386/coreboot/lbio.h b/include/grub/i386/coreboot/lbio.h
a5bd9f6
index b4150f4..9a93046 100644
a5bd9f6
--- a/include/grub/i386/coreboot/lbio.h
a5bd9f6
+++ b/include/grub/i386/coreboot/lbio.h
a5bd9f6
@@ -54,18 +54,40 @@ struct grub_linuxbios_mainboard
a5bd9f6
 
a5bd9f6
 struct grub_linuxbios_table_item
a5bd9f6
 {
a5bd9f6
-#define GRUB_LINUXBIOS_MEMBER_UNUSED		0x00
a5bd9f6
-#define GRUB_LINUXBIOS_MEMBER_MEMORY		0x01
a5bd9f6
-#define GRUB_LINUXBIOS_MEMBER_MAINBOARD		0x03
a5bd9f6
-#define GRUB_LINUXBIOS_MEMBER_CONSOLE           0x10
a5bd9f6
-#define GRUB_LINUXBIOS_MEMBER_LINK              0x11
a5bd9f6
-#define GRUB_LINUXBIOS_MEMBER_TIMESTAMPS        0x16
a5bd9f6
-#define GRUB_LINUXBIOS_MEMBER_CBMEMC            0x17
a5bd9f6
   grub_uint32_t tag;
a5bd9f6
   grub_uint32_t size;
a5bd9f6
 };
a5bd9f6
 typedef struct grub_linuxbios_table_item *grub_linuxbios_table_item_t;
a5bd9f6
 
a5bd9f6
+enum
a5bd9f6
+  {
a5bd9f6
+    GRUB_LINUXBIOS_MEMBER_UNUSED      = 0x00,
a5bd9f6
+    GRUB_LINUXBIOS_MEMBER_MEMORY      = 0x01,
a5bd9f6
+    GRUB_LINUXBIOS_MEMBER_MAINBOARD   = 0x03,
a5bd9f6
+    GRUB_LINUXBIOS_MEMBER_CONSOLE     = 0x10,
a5bd9f6
+    GRUB_LINUXBIOS_MEMBER_LINK        = 0x11,
a5bd9f6
+    GRUB_LINUXBIOS_MEMBER_FRAMEBUFFER = 0x12,
a5bd9f6
+    GRUB_LINUXBIOS_MEMBER_TIMESTAMPS  = 0x16,
a5bd9f6
+    GRUB_LINUXBIOS_MEMBER_CBMEMC      = 0x17
a5bd9f6
+  };
a5bd9f6
+
a5bd9f6
+struct grub_linuxbios_table_framebuffer {
a5bd9f6
+  grub_uint64_t lfb;
a5bd9f6
+  grub_uint32_t width;
a5bd9f6
+  grub_uint32_t height;
a5bd9f6
+  grub_uint32_t pitch;
a5bd9f6
+  grub_uint8_t bpp;
a5bd9f6
+
a5bd9f6
+  grub_uint8_t red_field_pos;
a5bd9f6
+  grub_uint8_t red_mask_size;
a5bd9f6
+  grub_uint8_t green_field_pos;
a5bd9f6
+  grub_uint8_t green_mask_size;
a5bd9f6
+  grub_uint8_t blue_field_pos;
a5bd9f6
+  grub_uint8_t blue_mask_size;
a5bd9f6
+  grub_uint8_t reserved_field_pos;
a5bd9f6
+  grub_uint8_t reserved_mask_size;
a5bd9f6
+};
a5bd9f6
+
a5bd9f6
 struct grub_linuxbios_mem_region
a5bd9f6
 {
a5bd9f6
   grub_uint64_t addr;
a5bd9f6
-- 
a5bd9f6
1.8.1.4
a5bd9f6