Cleanup bootsplash vesa signature detection.
[seabios.git] / src / bootsplash.c
index 6302133e7e5ddd6d99ce94b0c42a6f9eb5294614..ed104159f6c1d1a94e0f2d8cf51fbdf2b0d1582b 100644 (file)
@@ -1,6 +1,7 @@
 // Option rom scanning code.
 //
 // Copyright (C) 2009-2010  coresystems GmbH
+// Copyright (C) 2010  Kevin O'Connor <kevin@koconnor.net>
 //
 // This file may be distributed under the terms of the GNU LGPLv3 license.
 
@@ -9,10 +10,8 @@
 #include "config.h" // CONFIG_*
 #include "util.h" // dprintf
 #include "jpeg.h" // splash
+#include "biosvar.h" // SET_EBDA
 
-/****************************************************************
- * Definitions
- ****************************************************************/
 
 /****************************************************************
  * VESA structures
@@ -20,7 +19,7 @@
 
 struct vesa_info
 {
-    u8 vesa_signature[4];
+    u32 vesa_signature;
     u16 vesa_version;
     struct segoff_s oem_string_ptr;
     u8 capabilities[4];
@@ -34,6 +33,9 @@ struct vesa_info
     u8 oem_data[256];
 } PACKED;
 
+#define VESA_SIGNATURE 0x41534556 // VESA
+#define VBE2_SIGNATURE 0x32454256 // VBE2
+
 struct vesa_mode_info
 {
     u16 mode_attributes;
@@ -75,9 +77,21 @@ struct vesa_mode_info
  * Helper functions
  ****************************************************************/
 
+// Call int10 vga handler.
+static void
+call16_int10(struct bregs *br)
+{
+    br->flags = F_IF;
+    start_preempt();
+    call16_int(0x10, br);
+    finish_preempt();
+}
+
+
 /****************************************************************
  * VGA text / graphics console
  ****************************************************************/
+
 static void enable_vga_text_console(void)
 {
     dprintf(1, "Turning on vga text mode console\n");
@@ -85,70 +99,57 @@ static void enable_vga_text_console(void)
 
     /* Enable VGA text mode */
     memset(&br, 0, sizeof(br));
-    br.flags = F_IF;
     br.ax = 0x0003;
-    start_preempt();
-    call16_int(0x10, &br);
-    finish_preempt();
-
-    if (CONFIG_BOOTSPLASH) {
-        /* Switch back to start of the framebuffer
-         * (disable "double buffering")
-         */
-        memset(&br, 0, sizeof(br));
-        br.flags = F_IF;
-        br.ax = 0x4f07;
-        br.bl = 0x02;
-        br.ecx = 0;
-
-        start_preempt();
-        call16_int(0x10, &br);
-        finish_preempt();
-    }
+    call16_int10(&br);
 
     // Write to screen.
-    printf("Starting SeaBIOS (version %s)\n\n", VERSION);
+    printf("SeaBIOS (version %s)\n\n", VERSION);
 }
 
 void enable_vga_console(void)
 {
-    /* Needs coreboot support for CBFS */
-    if (!CONFIG_BOOTSPLASH || !CONFIG_COREBOOT) {
-        enable_vga_text_console();
-        return;
-    }
-
-    struct bregs br;
-    struct vesa_info *vesa_info;
-    struct vesa_mode_info *mode_info;
-    struct jpeg_decdata *decdata;
+    struct vesa_info *vesa_info = NULL;
+    struct vesa_mode_info *mode_info = NULL;
+    struct jpeg_decdata *decdata = NULL;
+    u8 *jpeg = NULL, *picture = NULL;
 
+    /* Needs coreboot support for CBFS */
+    if (!CONFIG_BOOTSPLASH || !CONFIG_COREBOOT)
+        goto gotext;
+    struct cbfs_file *file = cbfs_finddatafile("bootsplash.jpg");
+    if (!file)
+        goto gotext;
+    int filesize = cbfs_datasize(file);
+
+    int imagesize = (CONFIG_BOOTSPLASH_X * CONFIG_BOOTSPLASH_Y *
+                     (CONFIG_BOOTSPLASH_DEPTH / 8));
+    jpeg = malloc_tmphigh(filesize);
+    picture = malloc_tmphigh(imagesize);
     vesa_info = malloc_tmplow(sizeof(*vesa_info));
     mode_info = malloc_tmplow(sizeof(*mode_info));
     decdata = malloc_tmphigh(sizeof(*decdata));
+    if (!jpeg || !picture || !vesa_info || !mode_info || !decdata) {
+        warn_noalloc();
+        goto gotext;
+    }
 
     /* Check whether we have a VESA 2.0 compliant BIOS */
     memset(vesa_info, 0, sizeof(struct vesa_info));
-    memcpy(vesa_info, "VBE2", 4);
-
+    vesa_info->vesa_signature = VBE2_SIGNATURE;
+    struct bregs br;
     memset(&br, 0, sizeof(br));
-    br.flags = F_IF;
     br.ax = 0x4f00;
     br.di = FLATPTR_TO_OFFSET(vesa_info);
     br.es = FLATPTR_TO_SEG(vesa_info);
-    start_preempt();
-    call16_int(0x10, &br);
-    finish_preempt();
-
-    if(strcmp("VESA", (char *)vesa_info) != 0) {
+    call16_int10(&br);
+    if (vesa_info->vesa_signature != VESA_SIGNATURE) {
         dprintf(1,"No VBE2 found.\n");
-        goto cleanup;
+        goto gotext;
     }
 
     /* Print some debugging information about our card. */
     char *vendor = SEGOFF_TO_FLATPTR(vesa_info->oem_vendor_name_ptr);
     char *product = SEGOFF_TO_FLATPTR(vesa_info->oem_product_name_ptr);
-
     dprintf(8, "VESA %d.%d\nVENDOR: %s\nPRODUCT: %s\n",
             vesa_info->vesa_version>>8, vesa_info->vesa_version&0xff,
             vendor, product);
@@ -157,100 +158,63 @@ void enable_vga_console(void)
      * framebuffer start address
      */
     memset(&br, 0, sizeof(br));
-    br.flags = F_IF;
     br.ax = 0x4f01;
     br.cx = (1 << 14) | CONFIG_BOOTSPLASH_VESA_MODE;
     br.di = FLATPTR_TO_OFFSET(mode_info);
     br.es = FLATPTR_TO_SEG(mode_info);
-    start_preempt();
-    call16_int(0x10, &br);
-    finish_preempt();
+    call16_int10(&br);
     if (br.ax != 0x4f) {
         dprintf(1, "get_mode failed.\n");
-        enable_vga_text_console();
-        goto cleanup;
+        goto gotext;
     }
     unsigned char *framebuffer = (unsigned char *) (mode_info->phys_base_ptr);
 
     /* Switch to graphics mode */
     memset(&br, 0, sizeof(br));
-    br.flags = F_IF;
     br.ax = 0x4f02;
     br.bx = (1 << 14) | CONFIG_BOOTSPLASH_VESA_MODE;
-    start_preempt();
-    call16_int(0x10, &br);
-    finish_preempt();
+    call16_int10(&br);
     if (br.ax != 0x4f) {
         dprintf(1, "set_mode failed.\n");
-        enable_vga_text_console();
-        goto cleanup;
+        goto gotext;
     }
 
-    /* Switching Intel IGD to 1MB video memory will break this. Who cares. */
-    int imagesize = CONFIG_BOOTSPLASH_X * CONFIG_BOOTSPLASH_Y *
-                        (CONFIG_BOOTSPLASH_DEPTH / 8);
-
-    /* We use "double buffering" to make things look nicer */
-    framebuffer += imagesize;
-
     dprintf(8, "framebuffer: %x\n", (u32)framebuffer);
     dprintf(8, "bytes per scanline: %d\n", mode_info->bytes_per_scanline);
     dprintf(8, "bits per pixel: %d\n", mode_info->bits_per_pixel);
 
-    /* Look for bootsplash.jpg in CBFS and decompress it... */
-    int ret = 0;
-    unsigned char *jpeg = NULL;
-
-    struct cbfs_file *file = cbfs_finddatafile("bootsplash.jpg");
-    int filesize = 0;
-
-    if (file) {
-        filesize = cbfs_datasize(file);
-        jpeg = malloc_tmphigh(filesize);
-    } else {
-        dprintf(1, "Could not find boot splash screen \"bootsplash.jpg\"\n");
-    }
-    if(jpeg) {
-        dprintf(8, "Copying boot splash screen...\n");
-        cbfs_copyfile(file, jpeg, filesize);
-        dprintf(8, "Decompressing boot splash screen...\n");
-        ret = jpeg_decode(jpeg, framebuffer, CONFIG_BOOTSPLASH_X,
+    /* Decompress jpeg */
+    dprintf(8, "Copying boot splash screen...\n");
+    cbfs_copyfile(file, jpeg, filesize);
+    dprintf(8, "Decompressing boot splash screen...\n");
+    int ret = jpeg_decode(jpeg, picture, CONFIG_BOOTSPLASH_X,
                           CONFIG_BOOTSPLASH_Y, CONFIG_BOOTSPLASH_DEPTH, decdata);
-        if (ret)
-            dprintf(1, "Failed with return code %d...\n", ret);
-    } else {
-        ret = -1;
-    }
-    free(jpeg);
     if (ret) {
-        enable_vga_text_console();
-        goto cleanup;
+        dprintf(1, "jpeg_decode failed with return code %d...\n", ret);
+        goto gotext;
     }
 
     /* Show the picture */
-    memset(&br, 0, sizeof(br));
-    br.flags = F_IF;
-    br.ax = 0x4f07;
-    br.bl = 0x02;
-    br.ecx = imagesize;
-    start_preempt();
-    call16_int(0x10, &br);
-    finish_preempt();
-    if (br.ax != 0x4f) {
-        dprintf(1, "display_start failed (ax=%04x).\n", br.ax);
-        enable_vga_text_console();
-    }
+    iomemcpy(framebuffer, picture, imagesize);
+    SET_EBDA(bootsplash_active, 1);
 
 cleanup:
-    free (vesa_info);
-    free (mode_info);
-    free (decdata);
+    free(jpeg);
+    free(picture);
+    free(vesa_info);
+    free(mode_info);
+    free(decdata);
+    return;
+gotext:
+    enable_vga_text_console();
+    goto cleanup;
 }
 
 void
 disable_bootsplash(void)
 {
-    if (! CONFIG_BOOTSPLASH)
+    if (! CONFIG_BOOTSPLASH || !GET_EBDA(bootsplash_active))
         return;
+    SET_EBDA(bootsplash_active, 0);
     enable_vga_text_console();
 }