1 // Initialize the VGA console and possibly show a boot splash image.
2 //
3 // Copyright (C) 2009-2010 coresystems GmbH
4 // Copyright (C) 2010 Kevin O'Connor <kevin@koconnor.net>
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "bregs.h" // struct bregs
9 #include "config.h" // CONFIG_*
10 #include "farptr.h" // FLATPTR_TO_SEG
11 #include "malloc.h" // free
12 #include "output.h" // dprintf
13 #include "romfile.h" // romfile_loadfile
14 #include "stacks.h" // call16_int
15 #include "std/vbe.h" // struct vbe_info
16 #include "string.h" // memset
17 #include "util.h" // enable_bootsplash
18
19
20 /****************************************************************
21 * Helper functions
22 ****************************************************************/
23
24 // Call int10 vga handler.
25 static void
call16_int10(struct bregs * br)26 call16_int10(struct bregs *br)
27 {
28 br->flags = F_IF;
29 start_preempt();
30 call16_int(0x10, br);
31 finish_preempt();
32 }
33
34
35 /****************************************************************
36 * VGA text / graphics console
37 ****************************************************************/
38
39 void
enable_vga_console(void)40 enable_vga_console(void)
41 {
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));
47 br.ax = 0x0003;
48 call16_int10(&br);
49
50 // Write to screen.
51 printf("SeaBIOS (version %s)\n", VERSION);
52 display_uuid();
53 }
54
55 static int
find_videomode(struct vbe_info * vesa_info,struct vbe_mode_info * mode_info,int width,int height,int bpp_req)56 find_videomode(struct vbe_info *vesa_info, struct vbe_mode_info *mode_info
57 , int width, int height, int bpp_req)
58 {
59 dprintf(3, "Finding vesa mode with dimensions %d/%d\n", width, height);
60 u16 *videomodes = SEGOFF_TO_FLATPTR(vesa_info->video_mode);
61 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;
71 br.cx = videomode;
72 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 }
79 if (mode_info->xres != width
80 || mode_info->yres != height)
81 continue;
82 u8 depth = mode_info->bits_per_pixel;
83 if (bpp_req == 0) {
84 if ((depth != 16 && depth != 24 && depth != 32)
85 || mode_info->green_size == 5)
86 continue;
87 } else {
88 if (depth != bpp_req)
89 continue;
90 }
91 return videomode;
92 }
93 }
94
95 static int BootsplashActive;
96
97 void
enable_bootsplash(void)98 enable_bootsplash(void)
99 {
100 if (!CONFIG_BOOTSPLASH)
101 return;
102 /* splash picture can be bmp or jpeg file */
103 dprintf(3, "Checking for bootsplash\n");
104 u8 type = 0; /* 0 means jpg, 1 means bmp, default is 0=jpg */
105 int filesize;
106 u8 *filedata = romfile_loadfile("bootsplash.jpg", &filesize);
107 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");
114
115 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;
118 struct vbe_info *vesa_info = malloc_tmplow(sizeof(*vesa_info));
119 struct vbe_mode_info *mode_info = malloc_tmplow(sizeof(*mode_info));
120 if (!vesa_info || !mode_info) {
121 warn_noalloc();
122 goto done;
123 }
124
125 /* Check whether we have a VESA 2.0 compliant BIOS */
126 memset(vesa_info, 0, sizeof(struct vbe_info));
127 vesa_info->signature = VBE2_SIGNATURE;
128 struct bregs br;
129 memset(&br, 0, sizeof(br));
130 br.ax = 0x4f00;
131 br.di = FLATPTR_TO_OFFSET(vesa_info);
132 br.es = FLATPTR_TO_SEG(vesa_info);
133 call16_int10(&br);
134 if (vesa_info->signature != VESA_SIGNATURE) {
135 dprintf(1,"No VBE2 found.\n");
136 goto done;
137 }
138
139 /* Print some debugging information about our card. */
140 char *vendor = SEGOFF_TO_FLATPTR(vesa_info->oem_vendor_string);
141 char *product = SEGOFF_TO_FLATPTR(vesa_info->oem_product_string);
142 dprintf(3, "VESA %d.%d\nVENDOR: %s\nPRODUCT: %s\n",
143 vesa_info->version>>8, vesa_info->version&0xff,
144 vendor, product);
145
146 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_info(bmp, &width, &height, &bpp_require);
176 }
177
178 // jpeg would use 16 or 24 bpp video mode, BMP uses 16/24/32 bpp mode.
179
180 // Try to find a graphics mode with the corresponding dimensions.
181 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);
186 goto done;
187 }
188 void *framebuffer = (void *)mode_info->phys_base;
189 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.
196 int imagesize = height * mode_info->bytes_per_scanline;
197 picture = malloc_tmphigh(imagesize);
198 if (!picture) {
199 warn_noalloc();
200 goto done;
201 }
202
203 if (type == 0) {
204 dprintf(5, "Decompressing bootsplash.jpg\n");
205 ret = jpeg_show(jpeg, picture, width, height, depth,
206 mode_info->bytes_per_scanline);
207 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 }
219 }
220
221 /* Switch to graphics mode */
222 dprintf(5, "Switching to graphics mode\n");
223 memset(&br, 0, sizeof(br));
224 br.ax = 0x4f02;
225 br.bx = videomode | VBE_MODE_LINEAR_FRAME_BUFFER;
226 call16_int10(&br);
227 if (br.ax != 0x4f) {
228 dprintf(1, "set_mode failed.\n");
229 goto done;
230 }
231
232 /* Show the picture */
233 dprintf(5, "Showing bootsplash picture\n");
234 iomemcpy(framebuffer, picture, imagesize);
235 dprintf(5, "Bootsplash copy complete\n");
236 BootsplashActive = 1;
237
238 done:
239 free(filedata);
240 free(picture);
241 free(vesa_info);
242 free(mode_info);
243 free(jpeg);
244 free(bmp);
245 return;
246 }
247
248 void
disable_bootsplash(void)249 disable_bootsplash(void)
250 {
251 if (!CONFIG_BOOTSPLASH || !BootsplashActive)
252 return;
253 BootsplashActive = 0;
254 enable_vga_console();
255 }
256