blob: 76b72c13ab6f26404f753bbbeac915734269ef02 [file] [log] [blame]
Kevin O'Connorafbed1b2010-06-28 07:34:53 -04001// Option rom scanning code.
2//
3// Copyright (C) 2009-2010 coresystems GmbH
Kevin O'Connor6dc76f42010-07-26 23:16:12 -04004// Copyright (C) 2010 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorafbed1b2010-06-28 07:34:53 -04005//
6// This file may be distributed under the terms of the GNU LGPLv3 license.
7
8#include "bregs.h" // struct bregs
9#include "farptr.h" // FLATPTR_TO_SEG
10#include "config.h" // CONFIG_*
11#include "util.h" // dprintf
12#include "jpeg.h" // splash
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -040013#include "biosvar.h" // SET_EBDA
Kevin O'Connorc8e4e882010-07-30 18:53:10 -040014#include "paravirt.h" // romfile_find
Julian Pidancet69e941c2011-12-19 05:07:59 +000015#include "vbe.h" // struct vbe_info
Wayne Xia5042ca52011-07-08 11:02:09 +080016#include "bmp.h"
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040017
18/****************************************************************
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040019 * Helper functions
20 ****************************************************************/
21
Kevin O'Connor26411862010-07-26 23:25:39 -040022// Call int10 vga handler.
23static void
24call16_int10(struct bregs *br)
25{
26 br->flags = F_IF;
27 start_preempt();
28 call16_int(0x10, br);
29 finish_preempt();
30}
31
32
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040033/****************************************************************
34 * VGA text / graphics console
35 ****************************************************************/
Kevin O'Connor26411862010-07-26 23:25:39 -040036
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -040037void
38enable_vga_console(void)
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040039{
40 dprintf(1, "Turning on vga text mode console\n");
41 struct bregs br;
42
43 /* Enable VGA text mode */
44 memset(&br, 0, sizeof(br));
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040045 br.ax = 0x0003;
Kevin O'Connor26411862010-07-26 23:25:39 -040046 call16_int10(&br);
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040047
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040048 // Write to screen.
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -040049 printf("SeaBIOS (version %s)\n\n", VERSION);
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040050}
51
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040052static int
Julian Pidancet69e941c2011-12-19 05:07:59 +000053find_videomode(struct vbe_info *vesa_info, struct vbe_mode_info *mode_info
Wayne Xia5042ca52011-07-08 11:02:09 +080054 , int width, int height, int bpp_req)
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040055{
56 dprintf(3, "Finding vesa mode with dimensions %d/%d\n", width, height);
Julian Pidancet69e941c2011-12-19 05:07:59 +000057 u16 *videomodes = SEGOFF_TO_FLATPTR(vesa_info->video_mode);
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040058 for (;; videomodes++) {
59 u16 videomode = *videomodes;
60 if (videomode == 0xffff) {
61 dprintf(1, "Unable to find vesa video mode dimensions %d/%d\n"
62 , width, height);
63 return -1;
64 }
65 struct bregs br;
66 memset(&br, 0, sizeof(br));
67 br.ax = 0x4f01;
68 br.cx = (1 << 14) | videomode;
69 br.di = FLATPTR_TO_OFFSET(mode_info);
70 br.es = FLATPTR_TO_SEG(mode_info);
71 call16_int10(&br);
72 if (br.ax != 0x4f) {
73 dprintf(1, "get_mode failed.\n");
74 continue;
75 }
Julian Pidancet69e941c2011-12-19 05:07:59 +000076 if (mode_info->xres != width
77 || mode_info->yres != height)
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040078 continue;
79 u8 depth = mode_info->bits_per_pixel;
Wayne Xia5042ca52011-07-08 11:02:09 +080080 if (bpp_req == 0) {
81 if (depth != 16 && depth != 24 && depth != 32)
82 continue;
83 } else {
84 if (depth != bpp_req)
85 continue;
86 }
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040087 return videomode;
88 }
89}
90
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -040091static int BootsplashActive;
Kevin O'Connor227dc3e2010-07-26 23:02:26 -040092
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -040093void
94enable_bootsplash(void)
95{
Kevin O'Connorc8e4e882010-07-30 18:53:10 -040096 if (!CONFIG_BOOTSPLASH)
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -040097 return;
Wayne Xia5042ca52011-07-08 11:02:09 +080098 /* splash picture can be bmp or jpeg file */
Kevin O'Connoreb6dc782010-08-03 20:58:43 -040099 dprintf(3, "Checking for bootsplash\n");
Wayne Xia5042ca52011-07-08 11:02:09 +0800100 u8 type = 0; /* 0 means jpg, 1 means bmp, default is 0=jpg */
Kevin O'Connorf9b09302010-12-24 11:15:26 -0500101 int filesize;
102 u8 *filedata = romfile_loadfile("bootsplash.jpg", &filesize);
Wayne Xia5042ca52011-07-08 11:02:09 +0800103 if (!filedata) {
104 filedata = romfile_loadfile("bootsplash.bmp", &filesize);
105 if (!filedata)
106 return;
107 type = 1;
108 }
109 dprintf(3, "start showing bootsplash\n");
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400110
Wayne Xia5042ca52011-07-08 11:02:09 +0800111 u8 *picture = NULL; /* data buff used to be flushed to the video buf */
112 struct jpeg_decdata *jpeg = NULL;
113 struct bmp_decdata *bmp = NULL;
Julian Pidancet69e941c2011-12-19 05:07:59 +0000114 struct vbe_info *vesa_info = malloc_tmplow(sizeof(*vesa_info));
115 struct vbe_mode_info *mode_info = malloc_tmplow(sizeof(*mode_info));
Wayne Xia5042ca52011-07-08 11:02:09 +0800116 if (!vesa_info || !mode_info) {
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400117 warn_noalloc();
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400118 goto done;
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400119 }
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400120
121 /* Check whether we have a VESA 2.0 compliant BIOS */
Julian Pidancet69e941c2011-12-19 05:07:59 +0000122 memset(vesa_info, 0, sizeof(struct vbe_info));
123 vesa_info->signature = VBE2_SIGNATURE;
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400124 struct bregs br;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400125 memset(&br, 0, sizeof(br));
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400126 br.ax = 0x4f00;
127 br.di = FLATPTR_TO_OFFSET(vesa_info);
128 br.es = FLATPTR_TO_SEG(vesa_info);
Kevin O'Connor26411862010-07-26 23:25:39 -0400129 call16_int10(&br);
Julian Pidancet69e941c2011-12-19 05:07:59 +0000130 if (vesa_info->signature != VESA_SIGNATURE) {
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400131 dprintf(1,"No VBE2 found.\n");
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400132 goto done;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400133 }
134
135 /* Print some debugging information about our card. */
Julian Pidancet69e941c2011-12-19 05:07:59 +0000136 char *vendor = SEGOFF_TO_FLATPTR(vesa_info->oem_vendor_string);
137 char *product = SEGOFF_TO_FLATPTR(vesa_info->oem_product_string);
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400138 dprintf(3, "VESA %d.%d\nVENDOR: %s\nPRODUCT: %s\n",
Julian Pidancet69e941c2011-12-19 05:07:59 +0000139 vesa_info->version>>8, vesa_info->version&0xff,
Kevin O'Connora576c9c2010-07-26 22:43:18 -0400140 vendor, product);
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400141
Wayne Xia5042ca52011-07-08 11:02:09 +0800142 int ret, width, height;
143 int bpp_require = 0;
144 if (type == 0) {
145 jpeg = jpeg_alloc();
146 if (!jpeg) {
147 warn_noalloc();
148 goto done;
149 }
150 /* Parse jpeg and get image size. */
151 dprintf(5, "Decoding bootsplash.jpg\n");
152 ret = jpeg_decode(jpeg, filedata);
153 if (ret) {
154 dprintf(1, "jpeg_decode failed with return code %d...\n", ret);
155 goto done;
156 }
157 jpeg_get_size(jpeg, &width, &height);
158 } else {
159 bmp = bmp_alloc();
160 if (!bmp) {
161 warn_noalloc();
162 goto done;
163 }
164 /* Parse bmp and get image size. */
165 dprintf(5, "Decoding bootsplash.bmp\n");
166 ret = bmp_decode(bmp, filedata, filesize);
167 if (ret) {
168 dprintf(1, "bmp_decode failed with return code %d...\n", ret);
169 goto done;
170 }
171 bmp_get_size(bmp, &width, &height);
172 bpp_require = 24;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400173 }
Wayne Xia5042ca52011-07-08 11:02:09 +0800174 /* jpeg would use 16 or 24 bpp video mode, BMP use 24bpp mode only */
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400175
176 // Try to find a graphics mode with the corresponding dimensions.
Wayne Xia5042ca52011-07-08 11:02:09 +0800177 int videomode = find_videomode(vesa_info, mode_info, width, height,
178 bpp_require);
179 if (videomode < 0) {
180 dprintf(1, "failed to find a videomode with %dx%d %dbpp (0=any).\n",
181 width, height, bpp_require);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400182 goto done;
Wayne Xia5042ca52011-07-08 11:02:09 +0800183 }
Julian Pidancet69e941c2011-12-19 05:07:59 +0000184 void *framebuffer = (void *)mode_info->phys_base;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400185 int depth = mode_info->bits_per_pixel;
186 dprintf(3, "mode: %04x\n", videomode);
187 dprintf(3, "framebuffer: %p\n", framebuffer);
188 dprintf(3, "bytes per scanline: %d\n", mode_info->bytes_per_scanline);
189 dprintf(3, "bits per pixel: %d\n", depth);
190
191 // Allocate space for image and decompress it.
Wayne Xia5042ca52011-07-08 11:02:09 +0800192 int imagesize = height * mode_info->bytes_per_scanline;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400193 picture = malloc_tmphigh(imagesize);
194 if (!picture) {
195 warn_noalloc();
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400196 goto done;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400197 }
Wayne Xia5042ca52011-07-08 11:02:09 +0800198
199 if (type == 0) {
200 dprintf(5, "Decompressing bootsplash.jpg\n");
Wayne Xia8031efa2011-07-08 11:03:16 +0800201 ret = jpeg_show(jpeg, picture, width, height, depth,
202 mode_info->bytes_per_scanline);
Wayne Xia5042ca52011-07-08 11:02:09 +0800203 if (ret) {
204 dprintf(1, "jpeg_show failed with return code %d...\n", ret);
205 goto done;
206 }
207 } else {
208 dprintf(5, "Decompressing bootsplash.bmp\n");
209 ret = bmp_show(bmp, picture, width, height, depth,
210 mode_info->bytes_per_scanline);
211 if (ret) {
212 dprintf(1, "bmp_show failed with return code %d...\n", ret);
213 goto done;
214 }
Kevin O'Connorbbc47222010-07-30 13:07:08 -0400215 }
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400216
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400217 /* Switch to graphics mode */
Kevin O'Connoreb6dc782010-08-03 20:58:43 -0400218 dprintf(5, "Switching to graphics mode\n");
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400219 memset(&br, 0, sizeof(br));
220 br.ax = 0x4f02;
221 br.bx = (1 << 14) | videomode;
222 call16_int10(&br);
223 if (br.ax != 0x4f) {
224 dprintf(1, "set_mode failed.\n");
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400225 goto done;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400226 }
227
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400228 /* Show the picture */
Wayne Xia5042ca52011-07-08 11:02:09 +0800229 dprintf(5, "Showing bootsplash picture\n");
Kevin O'Connor6dc76f42010-07-26 23:16:12 -0400230 iomemcpy(framebuffer, picture, imagesize);
Kevin O'Connoreb6dc782010-08-03 20:58:43 -0400231 dprintf(5, "Bootsplash copy complete\n");
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400232 BootsplashActive = 1;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400233
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400234done:
Kevin O'Connorb2b9d4a2010-07-30 13:25:21 -0400235 free(filedata);
Kevin O'Connor6dc76f42010-07-26 23:16:12 -0400236 free(picture);
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400237 free(vesa_info);
238 free(mode_info);
Kevin O'Connorb2b9d4a2010-07-30 13:25:21 -0400239 free(jpeg);
Wayne Xia5042ca52011-07-08 11:02:09 +0800240 free(bmp);
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400241 return;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400242}
243
244void
245disable_bootsplash(void)
246{
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400247 if (!CONFIG_BOOTSPLASH || !BootsplashActive)
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400248 return;
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400249 BootsplashActive = 0;
250 enable_vga_console();
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400251}