blob: 493d60d5c54d98b9d45344fe1b7a56f7c4f58549 [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
Kevin O'Connorccee6e82013-09-02 21:25:21 -040015#include "fw/smbios.h" // display_uuid
Kevin O'Connor41639f82013-09-14 19:37:36 -040016#include "romfile.h" // romfile_loadfile
Kevin O'Connor3df600b2013-09-14 19:28:55 -040017#include "stacks.h" // call16_int
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040018#include "string.h" // memset
Kevin O'Connorc8a3d3e2012-09-03 13:52:50 -040019
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040020
21/****************************************************************
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040022 * Helper functions
23 ****************************************************************/
24
Kevin O'Connor26411862010-07-26 23:25:39 -040025// Call int10 vga handler.
26static void
27call16_int10(struct bregs *br)
28{
29 br->flags = F_IF;
30 start_preempt();
31 call16_int(0x10, br);
32 finish_preempt();
33}
34
35
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040036/****************************************************************
37 * VGA text / graphics console
38 ****************************************************************/
Kevin O'Connor26411862010-07-26 23:25:39 -040039
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -040040void
41enable_vga_console(void)
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040042{
43 dprintf(1, "Turning on vga text mode console\n");
44 struct bregs br;
45
46 /* Enable VGA text mode */
47 memset(&br, 0, sizeof(br));
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040048 br.ax = 0x0003;
Kevin O'Connor26411862010-07-26 23:25:39 -040049 call16_int10(&br);
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040050
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040051 // Write to screen.
Kevin O'Connor57174c72012-12-21 22:52:50 -050052 printf("SeaBIOS (version %s)\n", VERSION);
Kevin O'Connor8a0a9722013-01-21 01:53:31 -050053 display_uuid();
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040054}
55
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040056static int
Julian Pidancet69e941c2011-12-19 05:07:59 +000057find_videomode(struct vbe_info *vesa_info, struct vbe_mode_info *mode_info
Wayne Xia5042ca52011-07-08 11:02:09 +080058 , int width, int height, int bpp_req)
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040059{
60 dprintf(3, "Finding vesa mode with dimensions %d/%d\n", width, height);
Julian Pidancet69e941c2011-12-19 05:07:59 +000061 u16 *videomodes = SEGOFF_TO_FLATPTR(vesa_info->video_mode);
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040062 for (;; videomodes++) {
63 u16 videomode = *videomodes;
64 if (videomode == 0xffff) {
65 dprintf(1, "Unable to find vesa video mode dimensions %d/%d\n"
66 , width, height);
67 return -1;
68 }
69 struct bregs br;
70 memset(&br, 0, sizeof(br));
71 br.ax = 0x4f01;
Kevin O'Connorc8a3d3e2012-09-03 13:52:50 -040072 br.cx = videomode;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040073 br.di = FLATPTR_TO_OFFSET(mode_info);
74 br.es = FLATPTR_TO_SEG(mode_info);
75 call16_int10(&br);
76 if (br.ax != 0x4f) {
77 dprintf(1, "get_mode failed.\n");
78 continue;
79 }
Julian Pidancet69e941c2011-12-19 05:07:59 +000080 if (mode_info->xres != width
81 || mode_info->yres != height)
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040082 continue;
83 u8 depth = mode_info->bits_per_pixel;
Wayne Xia5042ca52011-07-08 11:02:09 +080084 if (bpp_req == 0) {
85 if (depth != 16 && depth != 24 && depth != 32)
86 continue;
87 } else {
88 if (depth != bpp_req)
89 continue;
90 }
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040091 return videomode;
92 }
93}
94
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -040095static int BootsplashActive;
Kevin O'Connor227dc3e2010-07-26 23:02:26 -040096
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -040097void
98enable_bootsplash(void)
99{
Kevin O'Connorc8e4e882010-07-30 18:53:10 -0400100 if (!CONFIG_BOOTSPLASH)
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400101 return;
Wayne Xia5042ca52011-07-08 11:02:09 +0800102 /* splash picture can be bmp or jpeg file */
Kevin O'Connoreb6dc782010-08-03 20:58:43 -0400103 dprintf(3, "Checking for bootsplash\n");
Wayne Xia5042ca52011-07-08 11:02:09 +0800104 u8 type = 0; /* 0 means jpg, 1 means bmp, default is 0=jpg */
Kevin O'Connorf9b09302010-12-24 11:15:26 -0500105 int filesize;
106 u8 *filedata = romfile_loadfile("bootsplash.jpg", &filesize);
Wayne Xia5042ca52011-07-08 11:02:09 +0800107 if (!filedata) {
108 filedata = romfile_loadfile("bootsplash.bmp", &filesize);
109 if (!filedata)
110 return;
111 type = 1;
112 }
113 dprintf(3, "start showing bootsplash\n");
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400114
Wayne Xia5042ca52011-07-08 11:02:09 +0800115 u8 *picture = NULL; /* data buff used to be flushed to the video buf */
116 struct jpeg_decdata *jpeg = NULL;
117 struct bmp_decdata *bmp = NULL;
Julian Pidancet69e941c2011-12-19 05:07:59 +0000118 struct vbe_info *vesa_info = malloc_tmplow(sizeof(*vesa_info));
119 struct vbe_mode_info *mode_info = malloc_tmplow(sizeof(*mode_info));
Wayne Xia5042ca52011-07-08 11:02:09 +0800120 if (!vesa_info || !mode_info) {
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400121 warn_noalloc();
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400122 goto done;
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400123 }
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400124
125 /* Check whether we have a VESA 2.0 compliant BIOS */
Julian Pidancet69e941c2011-12-19 05:07:59 +0000126 memset(vesa_info, 0, sizeof(struct vbe_info));
127 vesa_info->signature = VBE2_SIGNATURE;
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400128 struct bregs br;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400129 memset(&br, 0, sizeof(br));
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400130 br.ax = 0x4f00;
131 br.di = FLATPTR_TO_OFFSET(vesa_info);
132 br.es = FLATPTR_TO_SEG(vesa_info);
Kevin O'Connor26411862010-07-26 23:25:39 -0400133 call16_int10(&br);
Julian Pidancet69e941c2011-12-19 05:07:59 +0000134 if (vesa_info->signature != VESA_SIGNATURE) {
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400135 dprintf(1,"No VBE2 found.\n");
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400136 goto done;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400137 }
138
139 /* Print some debugging information about our card. */
Julian Pidancet69e941c2011-12-19 05:07:59 +0000140 char *vendor = SEGOFF_TO_FLATPTR(vesa_info->oem_vendor_string);
141 char *product = SEGOFF_TO_FLATPTR(vesa_info->oem_product_string);
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400142 dprintf(3, "VESA %d.%d\nVENDOR: %s\nPRODUCT: %s\n",
Julian Pidancet69e941c2011-12-19 05:07:59 +0000143 vesa_info->version>>8, vesa_info->version&0xff,
Kevin O'Connora576c9c2010-07-26 22:43:18 -0400144 vendor, product);
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400145
Wayne Xia5042ca52011-07-08 11:02:09 +0800146 int ret, width, height;
147 int bpp_require = 0;
148 if (type == 0) {
149 jpeg = jpeg_alloc();
150 if (!jpeg) {
151 warn_noalloc();
152 goto done;
153 }
154 /* Parse jpeg and get image size. */
155 dprintf(5, "Decoding bootsplash.jpg\n");
156 ret = jpeg_decode(jpeg, filedata);
157 if (ret) {
158 dprintf(1, "jpeg_decode failed with return code %d...\n", ret);
159 goto done;
160 }
161 jpeg_get_size(jpeg, &width, &height);
162 } else {
163 bmp = bmp_alloc();
164 if (!bmp) {
165 warn_noalloc();
166 goto done;
167 }
168 /* Parse bmp and get image size. */
169 dprintf(5, "Decoding bootsplash.bmp\n");
170 ret = bmp_decode(bmp, filedata, filesize);
171 if (ret) {
172 dprintf(1, "bmp_decode failed with return code %d...\n", ret);
173 goto done;
174 }
175 bmp_get_size(bmp, &width, &height);
176 bpp_require = 24;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400177 }
Wayne Xia5042ca52011-07-08 11:02:09 +0800178 /* jpeg would use 16 or 24 bpp video mode, BMP use 24bpp mode only */
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400179
180 // Try to find a graphics mode with the corresponding dimensions.
Wayne Xia5042ca52011-07-08 11:02:09 +0800181 int videomode = find_videomode(vesa_info, mode_info, width, height,
182 bpp_require);
183 if (videomode < 0) {
184 dprintf(1, "failed to find a videomode with %dx%d %dbpp (0=any).\n",
185 width, height, bpp_require);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400186 goto done;
Wayne Xia5042ca52011-07-08 11:02:09 +0800187 }
Julian Pidancet69e941c2011-12-19 05:07:59 +0000188 void *framebuffer = (void *)mode_info->phys_base;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400189 int depth = mode_info->bits_per_pixel;
190 dprintf(3, "mode: %04x\n", videomode);
191 dprintf(3, "framebuffer: %p\n", framebuffer);
192 dprintf(3, "bytes per scanline: %d\n", mode_info->bytes_per_scanline);
193 dprintf(3, "bits per pixel: %d\n", depth);
194
195 // Allocate space for image and decompress it.
Wayne Xia5042ca52011-07-08 11:02:09 +0800196 int imagesize = height * mode_info->bytes_per_scanline;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400197 picture = malloc_tmphigh(imagesize);
198 if (!picture) {
199 warn_noalloc();
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400200 goto done;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400201 }
Wayne Xia5042ca52011-07-08 11:02:09 +0800202
203 if (type == 0) {
204 dprintf(5, "Decompressing bootsplash.jpg\n");
Wayne Xia8031efa2011-07-08 11:03:16 +0800205 ret = jpeg_show(jpeg, picture, width, height, depth,
206 mode_info->bytes_per_scanline);
Wayne Xia5042ca52011-07-08 11:02:09 +0800207 if (ret) {
208 dprintf(1, "jpeg_show failed with return code %d...\n", ret);
209 goto done;
210 }
211 } else {
212 dprintf(5, "Decompressing bootsplash.bmp\n");
213 ret = bmp_show(bmp, picture, width, height, depth,
214 mode_info->bytes_per_scanline);
215 if (ret) {
216 dprintf(1, "bmp_show failed with return code %d...\n", ret);
217 goto done;
218 }
Kevin O'Connorbbc47222010-07-30 13:07:08 -0400219 }
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400220
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400221 /* Switch to graphics mode */
Kevin O'Connoreb6dc782010-08-03 20:58:43 -0400222 dprintf(5, "Switching to graphics mode\n");
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400223 memset(&br, 0, sizeof(br));
224 br.ax = 0x4f02;
Kevin O'Connorc8a3d3e2012-09-03 13:52:50 -0400225 br.bx = videomode | VBE_MODE_LINEAR_FRAME_BUFFER;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400226 call16_int10(&br);
227 if (br.ax != 0x4f) {
228 dprintf(1, "set_mode failed.\n");
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400229 goto done;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400230 }
231
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400232 /* Show the picture */
Wayne Xia5042ca52011-07-08 11:02:09 +0800233 dprintf(5, "Showing bootsplash picture\n");
Kevin O'Connor6dc76f42010-07-26 23:16:12 -0400234 iomemcpy(framebuffer, picture, imagesize);
Kevin O'Connoreb6dc782010-08-03 20:58:43 -0400235 dprintf(5, "Bootsplash copy complete\n");
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400236 BootsplashActive = 1;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400237
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400238done:
Kevin O'Connorb2b9d4a2010-07-30 13:25:21 -0400239 free(filedata);
Kevin O'Connor6dc76f42010-07-26 23:16:12 -0400240 free(picture);
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400241 free(vesa_info);
242 free(mode_info);
Kevin O'Connorb2b9d4a2010-07-30 13:25:21 -0400243 free(jpeg);
Wayne Xia5042ca52011-07-08 11:02:09 +0800244 free(bmp);
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400245 return;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400246}
247
248void
249disable_bootsplash(void)
250{
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400251 if (!CONFIG_BOOTSPLASH || !BootsplashActive)
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400252 return;
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400253 BootsplashActive = 0;
254 enable_vga_console();
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400255}