blob: bd91102fbeeab467c3ed85c2a0e04c80aaf538c3 [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'Connorc8a3d3e2012-09-03 13:52:50 -040016
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'Connor57174c72012-12-21 22:52:50 -050049 printf("SeaBIOS (version %s)\n", VERSION);
Kevin O'Connor8a0a9722013-01-21 01:53:31 -050050 display_uuid();
Kevin O'Connorafbed1b2010-06-28 07:34:53 -040051}
52
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040053static int
Julian Pidancet69e941c2011-12-19 05:07:59 +000054find_videomode(struct vbe_info *vesa_info, struct vbe_mode_info *mode_info
Wayne Xia5042ca52011-07-08 11:02:09 +080055 , int width, int height, int bpp_req)
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040056{
57 dprintf(3, "Finding vesa mode with dimensions %d/%d\n", width, height);
Julian Pidancet69e941c2011-12-19 05:07:59 +000058 u16 *videomodes = SEGOFF_TO_FLATPTR(vesa_info->video_mode);
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040059 for (;; videomodes++) {
60 u16 videomode = *videomodes;
61 if (videomode == 0xffff) {
62 dprintf(1, "Unable to find vesa video mode dimensions %d/%d\n"
63 , width, height);
64 return -1;
65 }
66 struct bregs br;
67 memset(&br, 0, sizeof(br));
68 br.ax = 0x4f01;
Kevin O'Connorc8a3d3e2012-09-03 13:52:50 -040069 br.cx = videomode;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040070 br.di = FLATPTR_TO_OFFSET(mode_info);
71 br.es = FLATPTR_TO_SEG(mode_info);
72 call16_int10(&br);
73 if (br.ax != 0x4f) {
74 dprintf(1, "get_mode failed.\n");
75 continue;
76 }
Julian Pidancet69e941c2011-12-19 05:07:59 +000077 if (mode_info->xres != width
78 || mode_info->yres != height)
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040079 continue;
80 u8 depth = mode_info->bits_per_pixel;
Wayne Xia5042ca52011-07-08 11:02:09 +080081 if (bpp_req == 0) {
82 if (depth != 16 && depth != 24 && depth != 32)
83 continue;
84 } else {
85 if (depth != bpp_req)
86 continue;
87 }
Kevin O'Connor8d85eb12010-07-30 14:07:03 -040088 return videomode;
89 }
90}
91
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -040092static int BootsplashActive;
Kevin O'Connor227dc3e2010-07-26 23:02:26 -040093
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -040094void
95enable_bootsplash(void)
96{
Kevin O'Connorc8e4e882010-07-30 18:53:10 -040097 if (!CONFIG_BOOTSPLASH)
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -040098 return;
Wayne Xia5042ca52011-07-08 11:02:09 +080099 /* splash picture can be bmp or jpeg file */
Kevin O'Connoreb6dc782010-08-03 20:58:43 -0400100 dprintf(3, "Checking for bootsplash\n");
Wayne Xia5042ca52011-07-08 11:02:09 +0800101 u8 type = 0; /* 0 means jpg, 1 means bmp, default is 0=jpg */
Kevin O'Connorf9b09302010-12-24 11:15:26 -0500102 int filesize;
103 u8 *filedata = romfile_loadfile("bootsplash.jpg", &filesize);
Wayne Xia5042ca52011-07-08 11:02:09 +0800104 if (!filedata) {
105 filedata = romfile_loadfile("bootsplash.bmp", &filesize);
106 if (!filedata)
107 return;
108 type = 1;
109 }
110 dprintf(3, "start showing bootsplash\n");
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400111
Wayne Xia5042ca52011-07-08 11:02:09 +0800112 u8 *picture = NULL; /* data buff used to be flushed to the video buf */
113 struct jpeg_decdata *jpeg = NULL;
114 struct bmp_decdata *bmp = NULL;
Julian Pidancet69e941c2011-12-19 05:07:59 +0000115 struct vbe_info *vesa_info = malloc_tmplow(sizeof(*vesa_info));
116 struct vbe_mode_info *mode_info = malloc_tmplow(sizeof(*mode_info));
Wayne Xia5042ca52011-07-08 11:02:09 +0800117 if (!vesa_info || !mode_info) {
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400118 warn_noalloc();
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400119 goto done;
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400120 }
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400121
122 /* Check whether we have a VESA 2.0 compliant BIOS */
Julian Pidancet69e941c2011-12-19 05:07:59 +0000123 memset(vesa_info, 0, sizeof(struct vbe_info));
124 vesa_info->signature = VBE2_SIGNATURE;
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400125 struct bregs br;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400126 memset(&br, 0, sizeof(br));
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400127 br.ax = 0x4f00;
128 br.di = FLATPTR_TO_OFFSET(vesa_info);
129 br.es = FLATPTR_TO_SEG(vesa_info);
Kevin O'Connor26411862010-07-26 23:25:39 -0400130 call16_int10(&br);
Julian Pidancet69e941c2011-12-19 05:07:59 +0000131 if (vesa_info->signature != VESA_SIGNATURE) {
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400132 dprintf(1,"No VBE2 found.\n");
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400133 goto done;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400134 }
135
136 /* Print some debugging information about our card. */
Julian Pidancet69e941c2011-12-19 05:07:59 +0000137 char *vendor = SEGOFF_TO_FLATPTR(vesa_info->oem_vendor_string);
138 char *product = SEGOFF_TO_FLATPTR(vesa_info->oem_product_string);
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400139 dprintf(3, "VESA %d.%d\nVENDOR: %s\nPRODUCT: %s\n",
Julian Pidancet69e941c2011-12-19 05:07:59 +0000140 vesa_info->version>>8, vesa_info->version&0xff,
Kevin O'Connora576c9c2010-07-26 22:43:18 -0400141 vendor, product);
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400142
Wayne Xia5042ca52011-07-08 11:02:09 +0800143 int ret, width, height;
144 int bpp_require = 0;
145 if (type == 0) {
146 jpeg = jpeg_alloc();
147 if (!jpeg) {
148 warn_noalloc();
149 goto done;
150 }
151 /* Parse jpeg and get image size. */
152 dprintf(5, "Decoding bootsplash.jpg\n");
153 ret = jpeg_decode(jpeg, filedata);
154 if (ret) {
155 dprintf(1, "jpeg_decode failed with return code %d...\n", ret);
156 goto done;
157 }
158 jpeg_get_size(jpeg, &width, &height);
159 } else {
160 bmp = bmp_alloc();
161 if (!bmp) {
162 warn_noalloc();
163 goto done;
164 }
165 /* Parse bmp and get image size. */
166 dprintf(5, "Decoding bootsplash.bmp\n");
167 ret = bmp_decode(bmp, filedata, filesize);
168 if (ret) {
169 dprintf(1, "bmp_decode failed with return code %d...\n", ret);
170 goto done;
171 }
172 bmp_get_size(bmp, &width, &height);
173 bpp_require = 24;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400174 }
Wayne Xia5042ca52011-07-08 11:02:09 +0800175 /* jpeg would use 16 or 24 bpp video mode, BMP use 24bpp mode only */
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400176
177 // Try to find a graphics mode with the corresponding dimensions.
Wayne Xia5042ca52011-07-08 11:02:09 +0800178 int videomode = find_videomode(vesa_info, mode_info, width, height,
179 bpp_require);
180 if (videomode < 0) {
181 dprintf(1, "failed to find a videomode with %dx%d %dbpp (0=any).\n",
182 width, height, bpp_require);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400183 goto done;
Wayne Xia5042ca52011-07-08 11:02:09 +0800184 }
Julian Pidancet69e941c2011-12-19 05:07:59 +0000185 void *framebuffer = (void *)mode_info->phys_base;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400186 int depth = mode_info->bits_per_pixel;
187 dprintf(3, "mode: %04x\n", videomode);
188 dprintf(3, "framebuffer: %p\n", framebuffer);
189 dprintf(3, "bytes per scanline: %d\n", mode_info->bytes_per_scanline);
190 dprintf(3, "bits per pixel: %d\n", depth);
191
192 // Allocate space for image and decompress it.
Wayne Xia5042ca52011-07-08 11:02:09 +0800193 int imagesize = height * mode_info->bytes_per_scanline;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400194 picture = malloc_tmphigh(imagesize);
195 if (!picture) {
196 warn_noalloc();
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400197 goto done;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400198 }
Wayne Xia5042ca52011-07-08 11:02:09 +0800199
200 if (type == 0) {
201 dprintf(5, "Decompressing bootsplash.jpg\n");
Wayne Xia8031efa2011-07-08 11:03:16 +0800202 ret = jpeg_show(jpeg, picture, width, height, depth,
203 mode_info->bytes_per_scanline);
Wayne Xia5042ca52011-07-08 11:02:09 +0800204 if (ret) {
205 dprintf(1, "jpeg_show failed with return code %d...\n", ret);
206 goto done;
207 }
208 } else {
209 dprintf(5, "Decompressing bootsplash.bmp\n");
210 ret = bmp_show(bmp, picture, width, height, depth,
211 mode_info->bytes_per_scanline);
212 if (ret) {
213 dprintf(1, "bmp_show failed with return code %d...\n", ret);
214 goto done;
215 }
Kevin O'Connorbbc47222010-07-30 13:07:08 -0400216 }
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400217
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400218 /* Switch to graphics mode */
Kevin O'Connoreb6dc782010-08-03 20:58:43 -0400219 dprintf(5, "Switching to graphics mode\n");
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400220 memset(&br, 0, sizeof(br));
221 br.ax = 0x4f02;
Kevin O'Connorc8a3d3e2012-09-03 13:52:50 -0400222 br.bx = videomode | VBE_MODE_LINEAR_FRAME_BUFFER;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400223 call16_int10(&br);
224 if (br.ax != 0x4f) {
225 dprintf(1, "set_mode failed.\n");
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400226 goto done;
Kevin O'Connor8d85eb12010-07-30 14:07:03 -0400227 }
228
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400229 /* Show the picture */
Wayne Xia5042ca52011-07-08 11:02:09 +0800230 dprintf(5, "Showing bootsplash picture\n");
Kevin O'Connor6dc76f42010-07-26 23:16:12 -0400231 iomemcpy(framebuffer, picture, imagesize);
Kevin O'Connoreb6dc782010-08-03 20:58:43 -0400232 dprintf(5, "Bootsplash copy complete\n");
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400233 BootsplashActive = 1;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400234
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400235done:
Kevin O'Connorb2b9d4a2010-07-30 13:25:21 -0400236 free(filedata);
Kevin O'Connor6dc76f42010-07-26 23:16:12 -0400237 free(picture);
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400238 free(vesa_info);
239 free(mode_info);
Kevin O'Connorb2b9d4a2010-07-30 13:25:21 -0400240 free(jpeg);
Wayne Xia5042ca52011-07-08 11:02:09 +0800241 free(bmp);
Kevin O'Connor227dc3e2010-07-26 23:02:26 -0400242 return;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400243}
244
245void
246disable_bootsplash(void)
247{
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400248 if (!CONFIG_BOOTSPLASH || !BootsplashActive)
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400249 return;
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400250 BootsplashActive = 0;
251 enable_vga_console();
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400252}