blob: 78023a59451cb8aaebd11f263f9ea3b4f2cafcea [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
Julian Pidancet69e941c2011-12-19 05:07:59 +000013#include "vbe.h" // struct vbe_info
Kevin O'Connorc8a3d3e2012-09-03 13:52:50 -040014#include "bmp.h" // bmp_alloc
15
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040016
17/****************************************************************
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040018 * Helper functions
19 ****************************************************************/
20
Kevin O'Connor26411862010-07-26 23:25:39 -040021// Call int10 vga handler.
22static void
23call16_int10(struct bregs *br)
24{
25 br->flags = F_IF;
26 start_preempt();
27 call16_int(0x10, br);
28 finish_preempt();
29}
30
31
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040032/****************************************************************
33 * VGA text / graphics console
34 ****************************************************************/
Kevin O'Connor26411862010-07-26 23:25:39 -040035
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -040036void
37enable_vga_console(void)
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040038{
39 dprintf(1, "Turning on vga text mode console\n");
40 struct bregs br;
41
42 /* Enable VGA text mode */
43 memset(&br, 0, sizeof(br));
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040044 br.ax = 0x0003;
Kevin O'Connor26411862010-07-26 23:25:39 -040045 call16_int10(&br);
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040046
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040047 // Write to screen.
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -040048 printf("SeaBIOS (version %s)\n\n", VERSION);
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040049}
50
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040051static int
Julian Pidancet69e941c2011-12-19 05:07:59 +000052find_videomode(struct vbe_info *vesa_info, struct vbe_mode_info *mode_info
Wayne Xia5042ca52011-07-08 11:02:09 +080053 , int width, int height, int bpp_req)
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040054{
55 dprintf(3, "Finding vesa mode with dimensions %d/%d\n", width, height);
Julian Pidancet69e941c2011-12-19 05:07:59 +000056 u16 *videomodes = SEGOFF_TO_FLATPTR(vesa_info->video_mode);
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040057 for (;; videomodes++) {
58 u16 videomode = *videomodes;
59 if (videomode == 0xffff) {
60 dprintf(1, "Unable to find vesa video mode dimensions %d/%d\n"
61 , width, height);
62 return -1;
63 }
64 struct bregs br;
65 memset(&br, 0, sizeof(br));
66 br.ax = 0x4f01;
Kevin O'Connorc8a3d3e2012-09-03 13:52:50 -040067 br.cx = videomode;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040068 br.di = FLATPTR_TO_OFFSET(mode_info);
69 br.es = FLATPTR_TO_SEG(mode_info);
70 call16_int10(&br);
71 if (br.ax != 0x4f) {
72 dprintf(1, "get_mode failed.\n");
73 continue;
74 }
Julian Pidancet69e941c2011-12-19 05:07:59 +000075 if (mode_info->xres != width
76 || mode_info->yres != height)
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040077 continue;
78 u8 depth = mode_info->bits_per_pixel;
Wayne Xia5042ca52011-07-08 11:02:09 +080079 if (bpp_req == 0) {
80 if (depth != 16 && depth != 24 && depth != 32)
81 continue;
82 } else {
83 if (depth != bpp_req)
84 continue;
85 }
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040086 return videomode;
87 }
88}
89
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -040090static int BootsplashActive;
Kevin O'Connor227dc3e2010-07-26 23:02:26 -040091
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -040092void
93enable_bootsplash(void)
94{
Kevin O'Connorc8e4e882010-07-30 18:53:10 -040095 if (!CONFIG_BOOTSPLASH)
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -040096 return;
Wayne Xia5042ca52011-07-08 11:02:09 +080097 /* splash picture can be bmp or jpeg file */
Kevin O'Connoreb6dc782010-08-03 20:58:43 -040098 dprintf(3, "Checking for bootsplash\n");
Wayne Xia5042ca52011-07-08 11:02:09 +080099 u8 type = 0; /* 0 means jpg, 1 means bmp, default is 0=jpg */
Kevin O'Connorf9b09302010-12-24 11:15:26 -0500100 int filesize;
101 u8 *filedata = romfile_loadfile("bootsplash.jpg", &filesize);
Wayne Xia5042ca52011-07-08 11:02:09 +0800102 if (!filedata) {
103 filedata = romfile_loadfile("bootsplash.bmp", &filesize);
104 if (!filedata)
105 return;
106 type = 1;
107 }
108 dprintf(3, "start showing bootsplash\n");
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400109
Wayne Xia5042ca52011-07-08 11:02:09 +0800110 u8 *picture = NULL; /* data buff used to be flushed to the video buf */
111 struct jpeg_decdata *jpeg = NULL;
112 struct bmp_decdata *bmp = NULL;
Julian Pidancet69e941c2011-12-19 05:07:59 +0000113 struct vbe_info *vesa_info = malloc_tmplow(sizeof(*vesa_info));
114 struct vbe_mode_info *mode_info = malloc_tmplow(sizeof(*mode_info));
Wayne Xia5042ca52011-07-08 11:02:09 +0800115 if (!vesa_info || !mode_info) {
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400116 warn_noalloc();
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400117 goto done;
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400118 }
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400119
120 /* Check whether we have a VESA 2.0 compliant BIOS */
Julian Pidancet69e941c2011-12-19 05:07:59 +0000121 memset(vesa_info, 0, sizeof(struct vbe_info));
122 vesa_info->signature = VBE2_SIGNATURE;
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400123 struct bregs br;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400124 memset(&br, 0, sizeof(br));
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400125 br.ax = 0x4f00;
126 br.di = FLATPTR_TO_OFFSET(vesa_info);
127 br.es = FLATPTR_TO_SEG(vesa_info);
Kevin O'Connor26411862010-07-26 23:25:39 -0400128 call16_int10(&br);
Julian Pidancet69e941c2011-12-19 05:07:59 +0000129 if (vesa_info->signature != VESA_SIGNATURE) {
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400130 dprintf(1,"No VBE2 found.\n");
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400131 goto done;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400132 }
133
134 /* Print some debugging information about our card. */
Julian Pidancet69e941c2011-12-19 05:07:59 +0000135 char *vendor = SEGOFF_TO_FLATPTR(vesa_info->oem_vendor_string);
136 char *product = SEGOFF_TO_FLATPTR(vesa_info->oem_product_string);
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400137 dprintf(3, "VESA %d.%d\nVENDOR: %s\nPRODUCT: %s\n",
Julian Pidancet69e941c2011-12-19 05:07:59 +0000138 vesa_info->version>>8, vesa_info->version&0xff,
Kevin O'Connora576c9c2010-07-26 22:43:18 -0400139 vendor, product);
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400140
Wayne Xia5042ca52011-07-08 11:02:09 +0800141 int ret, width, height;
142 int bpp_require = 0;
143 if (type == 0) {
144 jpeg = jpeg_alloc();
145 if (!jpeg) {
146 warn_noalloc();
147 goto done;
148 }
149 /* Parse jpeg and get image size. */
150 dprintf(5, "Decoding bootsplash.jpg\n");
151 ret = jpeg_decode(jpeg, filedata);
152 if (ret) {
153 dprintf(1, "jpeg_decode failed with return code %d...\n", ret);
154 goto done;
155 }
156 jpeg_get_size(jpeg, &width, &height);
157 } else {
158 bmp = bmp_alloc();
159 if (!bmp) {
160 warn_noalloc();
161 goto done;
162 }
163 /* Parse bmp and get image size. */
164 dprintf(5, "Decoding bootsplash.bmp\n");
165 ret = bmp_decode(bmp, filedata, filesize);
166 if (ret) {
167 dprintf(1, "bmp_decode failed with return code %d...\n", ret);
168 goto done;
169 }
170 bmp_get_size(bmp, &width, &height);
171 bpp_require = 24;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400172 }
Wayne Xia5042ca52011-07-08 11:02:09 +0800173 /* jpeg would use 16 or 24 bpp video mode, BMP use 24bpp mode only */
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400174
175 // Try to find a graphics mode with the corresponding dimensions.
Wayne Xia5042ca52011-07-08 11:02:09 +0800176 int videomode = find_videomode(vesa_info, mode_info, width, height,
177 bpp_require);
178 if (videomode < 0) {
179 dprintf(1, "failed to find a videomode with %dx%d %dbpp (0=any).\n",
180 width, height, bpp_require);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400181 goto done;
Wayne Xia5042ca52011-07-08 11:02:09 +0800182 }
Julian Pidancet69e941c2011-12-19 05:07:59 +0000183 void *framebuffer = (void *)mode_info->phys_base;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400184 int depth = mode_info->bits_per_pixel;
185 dprintf(3, "mode: %04x\n", videomode);
186 dprintf(3, "framebuffer: %p\n", framebuffer);
187 dprintf(3, "bytes per scanline: %d\n", mode_info->bytes_per_scanline);
188 dprintf(3, "bits per pixel: %d\n", depth);
189
190 // Allocate space for image and decompress it.
Wayne Xia5042ca52011-07-08 11:02:09 +0800191 int imagesize = height * mode_info->bytes_per_scanline;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400192 picture = malloc_tmphigh(imagesize);
193 if (!picture) {
194 warn_noalloc();
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400195 goto done;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400196 }
Wayne Xia5042ca52011-07-08 11:02:09 +0800197
198 if (type == 0) {
199 dprintf(5, "Decompressing bootsplash.jpg\n");
Wayne Xia8031efa2011-07-08 11:03:16 +0800200 ret = jpeg_show(jpeg, picture, width, height, depth,
201 mode_info->bytes_per_scanline);
Wayne Xia5042ca52011-07-08 11:02:09 +0800202 if (ret) {
203 dprintf(1, "jpeg_show failed with return code %d...\n", ret);
204 goto done;
205 }
206 } else {
207 dprintf(5, "Decompressing bootsplash.bmp\n");
208 ret = bmp_show(bmp, picture, width, height, depth,
209 mode_info->bytes_per_scanline);
210 if (ret) {
211 dprintf(1, "bmp_show failed with return code %d...\n", ret);
212 goto done;
213 }
Kevin O'Connorbbc47222010-07-30 13:07:08 -0400214 }
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400215
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400216 /* Switch to graphics mode */
Kevin O'Connoreb6dc782010-08-03 20:58:43 -0400217 dprintf(5, "Switching to graphics mode\n");
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400218 memset(&br, 0, sizeof(br));
219 br.ax = 0x4f02;
Kevin O'Connorc8a3d3e2012-09-03 13:52:50 -0400220 br.bx = videomode | VBE_MODE_LINEAR_FRAME_BUFFER;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400221 call16_int10(&br);
222 if (br.ax != 0x4f) {
223 dprintf(1, "set_mode failed.\n");
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400224 goto done;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400225 }
226
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400227 /* Show the picture */
Wayne Xia5042ca52011-07-08 11:02:09 +0800228 dprintf(5, "Showing bootsplash picture\n");
Kevin O'Connor6dc76f42010-07-26 23:16:12 -0400229 iomemcpy(framebuffer, picture, imagesize);
Kevin O'Connoreb6dc782010-08-03 20:58:43 -0400230 dprintf(5, "Bootsplash copy complete\n");
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400231 BootsplashActive = 1;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400232
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400233done:
Kevin O'Connorb2b9d4a2010-07-30 13:25:21 -0400234 free(filedata);
Kevin O'Connor6dc76f42010-07-26 23:16:12 -0400235 free(picture);
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400236 free(vesa_info);
237 free(mode_info);
Kevin O'Connorb2b9d4a2010-07-30 13:25:21 -0400238 free(jpeg);
Wayne Xia5042ca52011-07-08 11:02:09 +0800239 free(bmp);
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400240 return;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400241}
242
243void
244disable_bootsplash(void)
245{
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400246 if (!CONFIG_BOOTSPLASH || !BootsplashActive)
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400247 return;
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400248 BootsplashActive = 0;
249 enable_vga_console();
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400250}