blob: 165c98d0629ed04fd8089c210d2599a1b47c4d61 [file] [log] [blame]
Kevin O'Connor865bfed2013-11-30 13:04:45 -05001// Initialize the VGA console and possibly show a boot splash image.
Kevin O'Connorafbed1b2010-06-28 07:34:53 -04002//
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
Kevin O'Connor2d2fa312013-09-14 21:55:26 -04008#include "bregs.h" // struct bregs
9#include "config.h" // CONFIG_*
10#include "farptr.h" // FLATPTR_TO_SEG
Kevin O'Connor9dea5902013-09-14 20:23:54 -040011#include "malloc.h" // free
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040012#include "output.h" // dprintf
Kevin O'Connor41639f82013-09-14 19:37:36 -040013#include "romfile.h" // romfile_loadfile
Kevin O'Connor3df600b2013-09-14 19:28:55 -040014#include "stacks.h" // call16_int
Kevin O'Connor2e57c812013-09-14 22:29:32 -040015#include "std/vbe.h" // struct vbe_info
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040016#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040017#include "util.h" // enable_bootsplash
Kevin O'Connorc8a3d3e2012-09-03 13:52:50 -040018
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040019
20/****************************************************************
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040021 * Helper functions
22 ****************************************************************/
23
Kevin O'Connor26411862010-07-26 23:25:39 -040024// Call int10 vga handler.
25static void
26call16_int10(struct bregs *br)
27{
28 br->flags = F_IF;
29 start_preempt();
30 call16_int(0x10, br);
31 finish_preempt();
32}
33
34
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040035/****************************************************************
36 * VGA text / graphics console
37 ****************************************************************/
Kevin O'Connor26411862010-07-26 23:25:39 -040038
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -040039void
40enable_vga_console(void)
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040041{
42 dprintf(1, "Turning on vga text mode console\n");
43 struct bregs br;
44
45 /* Enable VGA text mode */
46 memset(&br, 0, sizeof(br));
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040047 br.ax = 0x0003;
Kevin O'Connor26411862010-07-26 23:25:39 -040048 call16_int10(&br);
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040049
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040050 // Write to screen.
Kevin O'Connor57174c72012-12-21 22:52:50 -050051 printf("SeaBIOS (version %s)\n", VERSION);
Kevin O'Connor8a0a9722013-01-21 01:53:31 -050052 display_uuid();
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040053}
54
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040055static int
Julian Pidancet69e941c2011-12-19 05:07:59 +000056find_videomode(struct vbe_info *vesa_info, struct vbe_mode_info *mode_info
Wayne Xia5042ca52011-07-08 11:02:09 +080057 , int width, int height, int bpp_req)
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040058{
59 dprintf(3, "Finding vesa mode with dimensions %d/%d\n", width, height);
Julian Pidancet69e941c2011-12-19 05:07:59 +000060 u16 *videomodes = SEGOFF_TO_FLATPTR(vesa_info->video_mode);
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040061 for (;; videomodes++) {
62 u16 videomode = *videomodes;
63 if (videomode == 0xffff) {
64 dprintf(1, "Unable to find vesa video mode dimensions %d/%d\n"
65 , width, height);
66 return -1;
67 }
68 struct bregs br;
69 memset(&br, 0, sizeof(br));
70 br.ax = 0x4f01;
Kevin O'Connorc8a3d3e2012-09-03 13:52:50 -040071 br.cx = videomode;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040072 br.di = FLATPTR_TO_OFFSET(mode_info);
73 br.es = FLATPTR_TO_SEG(mode_info);
74 call16_int10(&br);
75 if (br.ax != 0x4f) {
76 dprintf(1, "get_mode failed.\n");
77 continue;
78 }
Julian Pidancet69e941c2011-12-19 05:07:59 +000079 if (mode_info->xres != width
80 || mode_info->yres != height)
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040081 continue;
82 u8 depth = mode_info->bits_per_pixel;
Wayne Xia5042ca52011-07-08 11:02:09 +080083 if (bpp_req == 0) {
Zheng Baod97c2002016-05-20 15:26:32 +000084 if ((depth != 16 && depth != 24 && depth != 32)
85 || mode_info->green_size == 5)
Wayne Xia5042ca52011-07-08 11:02:09 +080086 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}