vgabios: Unify code to generate the vbe mode list.
[seabios.git] / src / bootsplash.c
index ed104159f6c1d1a94e0f2d8cf51fbdf2b0d1582b..76b72c13ab6f26404f753bbbeac915734269ef02 100644 (file)
 #include "util.h" // dprintf
 #include "jpeg.h" // splash
 #include "biosvar.h" // SET_EBDA
-
-
-/****************************************************************
- * VESA structures
- ****************************************************************/
-
-struct vesa_info
-{
-    u32 vesa_signature;
-    u16 vesa_version;
-    struct segoff_s oem_string_ptr;
-    u8 capabilities[4];
-    struct segoff_s video_mode_ptr;
-    u16 total_memory;
-    u16 oem_software_rev;
-    struct segoff_s oem_vendor_name_ptr;
-    struct segoff_s oem_product_name_ptr;
-    struct segoff_s oem_product_rev_ptr;
-    u8 reserved[222];
-    u8 oem_data[256];
-} PACKED;
-
-#define VESA_SIGNATURE 0x41534556 // VESA
-#define VBE2_SIGNATURE 0x32454256 // VBE2
-
-struct vesa_mode_info
-{
-    u16 mode_attributes;
-    u8 win_a_attributes;
-    u8 win_b_attributes;
-    u16 win_granularity;
-    u16 win_size;
-    u16 win_a_segment;
-    u16 win_b_segment;
-    u32 win_func_ptr;
-    u16 bytes_per_scanline;
-    u16 x_resolution;
-    u16 y_resolution;
-    u8 x_charsize;
-    u8 y_charsize;
-    u8 number_of_planes;
-    u8 bits_per_pixel;
-    u8 number_of_banks;
-    u8 memory_model;
-    u8 bank_size;
-    u8 number_of_image_pages;
-    u8 reserved_page;
-    u8 red_mask_size;
-    u8 red_mask_pos;
-    u8 green_mask_size;
-    u8 green_mask_pos;
-    u8 blue_mask_size;
-    u8 blue_mask_pos;
-    u8 reserved_mask_size;
-    u8 reserved_mask_pos;
-    u8 direct_color_mode_info;
-    u32 phys_base_ptr;
-    u32 offscreen_mem_offset;
-    u16 offscreen_mem_size;
-    u8 reserved[206];
-} PACKED;
+#include "paravirt.h" // romfile_find
+#include "vbe.h" // struct vbe_info
+#include "bmp.h"
 
 /****************************************************************
  * Helper functions
@@ -92,7 +34,8 @@ call16_int10(struct bregs *br)
  * VGA text / graphics console
  ****************************************************************/
 
-static void enable_vga_text_console(void)
+void
+enable_vga_console(void)
 {
     dprintf(1, "Turning on vga text mode console\n");
     struct bregs br;
@@ -106,115 +49,203 @@ static void enable_vga_text_console(void)
     printf("SeaBIOS (version %s)\n\n", VERSION);
 }
 
-void enable_vga_console(void)
+static int
+find_videomode(struct vbe_info *vesa_info, struct vbe_mode_info *mode_info
+               , int width, int height, int bpp_req)
 {
-    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) {
+    dprintf(3, "Finding vesa mode with dimensions %d/%d\n", width, height);
+    u16 *videomodes = SEGOFF_TO_FLATPTR(vesa_info->video_mode);
+    for (;; videomodes++) {
+        u16 videomode = *videomodes;
+        if (videomode == 0xffff) {
+            dprintf(1, "Unable to find vesa video mode dimensions %d/%d\n"
+                    , width, height);
+            return -1;
+        }
+        struct bregs br;
+        memset(&br, 0, sizeof(br));
+        br.ax = 0x4f01;
+        br.cx = (1 << 14) | videomode;
+        br.di = FLATPTR_TO_OFFSET(mode_info);
+        br.es = FLATPTR_TO_SEG(mode_info);
+        call16_int10(&br);
+        if (br.ax != 0x4f) {
+            dprintf(1, "get_mode failed.\n");
+            continue;
+        }
+        if (mode_info->xres != width
+            || mode_info->yres != height)
+            continue;
+        u8 depth = mode_info->bits_per_pixel;
+        if (bpp_req == 0) {
+            if (depth != 16 && depth != 24 && depth != 32)
+                continue;
+        } else {
+            if (depth != bpp_req)
+                continue;
+        }
+        return videomode;
+    }
+}
+
+static int BootsplashActive;
+
+void
+enable_bootsplash(void)
+{
+    if (!CONFIG_BOOTSPLASH)
+        return;
+    /* splash picture can be bmp or jpeg file */
+    dprintf(3, "Checking for bootsplash\n");
+    u8 type = 0; /* 0 means jpg, 1 means bmp, default is 0=jpg */
+    int filesize;
+    u8 *filedata = romfile_loadfile("bootsplash.jpg", &filesize);
+    if (!filedata) {
+        filedata = romfile_loadfile("bootsplash.bmp", &filesize);
+        if (!filedata)
+            return;
+        type = 1;
+    }
+    dprintf(3, "start showing bootsplash\n");
+
+    u8 *picture = NULL; /* data buff used to be flushed to the video buf */
+    struct jpeg_decdata *jpeg = NULL;
+    struct bmp_decdata *bmp = NULL;
+    struct vbe_info *vesa_info = malloc_tmplow(sizeof(*vesa_info));
+    struct vbe_mode_info *mode_info = malloc_tmplow(sizeof(*mode_info));
+    if (!vesa_info || !mode_info) {
         warn_noalloc();
-        goto gotext;
+        goto done;
     }
 
     /* Check whether we have a VESA 2.0 compliant BIOS */
-    memset(vesa_info, 0, sizeof(struct vesa_info));
-    vesa_info->vesa_signature = VBE2_SIGNATURE;
+    memset(vesa_info, 0, sizeof(struct vbe_info));
+    vesa_info->signature = VBE2_SIGNATURE;
     struct bregs br;
     memset(&br, 0, sizeof(br));
     br.ax = 0x4f00;
     br.di = FLATPTR_TO_OFFSET(vesa_info);
     br.es = FLATPTR_TO_SEG(vesa_info);
     call16_int10(&br);
-    if (vesa_info->vesa_signature != VESA_SIGNATURE) {
+    if (vesa_info->signature != VESA_SIGNATURE) {
         dprintf(1,"No VBE2 found.\n");
-        goto gotext;
+        goto done;
     }
 
     /* 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,
+    char *vendor = SEGOFF_TO_FLATPTR(vesa_info->oem_vendor_string);
+    char *product = SEGOFF_TO_FLATPTR(vesa_info->oem_product_string);
+    dprintf(3, "VESA %d.%d\nVENDOR: %s\nPRODUCT: %s\n",
+            vesa_info->version>>8, vesa_info->version&0xff,
             vendor, product);
 
-    /* Get information about our graphics mode, like the
-     * framebuffer start address
-     */
-    memset(&br, 0, sizeof(br));
-    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);
-    call16_int10(&br);
-    if (br.ax != 0x4f) {
-        dprintf(1, "get_mode failed.\n");
-        goto gotext;
+    int ret, width, height;
+    int bpp_require = 0;
+    if (type == 0) {
+        jpeg = jpeg_alloc();
+        if (!jpeg) {
+            warn_noalloc();
+            goto done;
+        }
+        /* Parse jpeg and get image size. */
+        dprintf(5, "Decoding bootsplash.jpg\n");
+        ret = jpeg_decode(jpeg, filedata);
+        if (ret) {
+            dprintf(1, "jpeg_decode failed with return code %d...\n", ret);
+            goto done;
+        }
+        jpeg_get_size(jpeg, &width, &height);
+    } else {
+        bmp = bmp_alloc();
+        if (!bmp) {
+            warn_noalloc();
+            goto done;
+        }
+        /* Parse bmp and get image size. */
+        dprintf(5, "Decoding bootsplash.bmp\n");
+        ret = bmp_decode(bmp, filedata, filesize);
+        if (ret) {
+            dprintf(1, "bmp_decode failed with return code %d...\n", ret);
+            goto done;
+        }
+        bmp_get_size(bmp, &width, &height);
+        bpp_require = 24;
+    }
+    /* jpeg would use 16 or 24 bpp video mode, BMP use 24bpp mode only */
+
+    // Try to find a graphics mode with the corresponding dimensions.
+    int videomode = find_videomode(vesa_info, mode_info, width, height,
+                                       bpp_require);
+    if (videomode < 0) {
+        dprintf(1, "failed to find a videomode with %dx%d %dbpp (0=any).\n",
+                    width, height, bpp_require);
+        goto done;
+    }
+    void *framebuffer = (void *)mode_info->phys_base;
+    int depth = mode_info->bits_per_pixel;
+    dprintf(3, "mode: %04x\n", videomode);
+    dprintf(3, "framebuffer: %p\n", framebuffer);
+    dprintf(3, "bytes per scanline: %d\n", mode_info->bytes_per_scanline);
+    dprintf(3, "bits per pixel: %d\n", depth);
+
+    // Allocate space for image and decompress it.
+    int imagesize = height * mode_info->bytes_per_scanline;
+    picture = malloc_tmphigh(imagesize);
+    if (!picture) {
+        warn_noalloc();
+        goto done;
+    }
+
+    if (type == 0) {
+        dprintf(5, "Decompressing bootsplash.jpg\n");
+        ret = jpeg_show(jpeg, picture, width, height, depth,
+                            mode_info->bytes_per_scanline);
+        if (ret) {
+            dprintf(1, "jpeg_show failed with return code %d...\n", ret);
+            goto done;
+        }
+    } else {
+        dprintf(5, "Decompressing bootsplash.bmp\n");
+        ret = bmp_show(bmp, picture, width, height, depth,
+                           mode_info->bytes_per_scanline);
+        if (ret) {
+            dprintf(1, "bmp_show failed with return code %d...\n", ret);
+            goto done;
+        }
     }
-    unsigned char *framebuffer = (unsigned char *) (mode_info->phys_base_ptr);
 
     /* Switch to graphics mode */
+    dprintf(5, "Switching to graphics mode\n");
     memset(&br, 0, sizeof(br));
     br.ax = 0x4f02;
-    br.bx = (1 << 14) | CONFIG_BOOTSPLASH_VESA_MODE;
+    br.bx = (1 << 14) | videomode;
     call16_int10(&br);
     if (br.ax != 0x4f) {
         dprintf(1, "set_mode failed.\n");
-        goto gotext;
-    }
-
-    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);
-
-    /* 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, "jpeg_decode failed with return code %d...\n", ret);
-        goto gotext;
+        goto done;
     }
 
     /* Show the picture */
+    dprintf(5, "Showing bootsplash picture\n");
     iomemcpy(framebuffer, picture, imagesize);
-    SET_EBDA(bootsplash_active, 1);
+    dprintf(5, "Bootsplash copy complete\n");
+    BootsplashActive = 1;
 
-cleanup:
-    free(jpeg);
+done:
+    free(filedata);
     free(picture);
     free(vesa_info);
     free(mode_info);
-    free(decdata);
+    free(jpeg);
+    free(bmp);
     return;
-gotext:
-    enable_vga_text_console();
-    goto cleanup;
 }
 
 void
 disable_bootsplash(void)
 {
-    if (! CONFIG_BOOTSPLASH || !GET_EBDA(bootsplash_active))
+    if (!CONFIG_BOOTSPLASH || !BootsplashActive)
         return;
-    SET_EBDA(bootsplash_active, 0);
-    enable_vga_text_console();
+    BootsplashActive = 0;
+    enable_vga_console();
 }