Cleanup bootsplash vesa signature detection.
[seabios.git] / src / bootsplash.c
1 // Option rom scanning code.
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 "farptr.h" // FLATPTR_TO_SEG
10 #include "config.h" // CONFIG_*
11 #include "util.h" // dprintf
12 #include "jpeg.h" // splash
13 #include "biosvar.h" // SET_EBDA
14
15
16 /****************************************************************
17  * VESA structures
18  ****************************************************************/
19
20 struct vesa_info
21 {
22     u32 vesa_signature;
23     u16 vesa_version;
24     struct segoff_s oem_string_ptr;
25     u8 capabilities[4];
26     struct segoff_s video_mode_ptr;
27     u16 total_memory;
28     u16 oem_software_rev;
29     struct segoff_s oem_vendor_name_ptr;
30     struct segoff_s oem_product_name_ptr;
31     struct segoff_s oem_product_rev_ptr;
32     u8 reserved[222];
33     u8 oem_data[256];
34 } PACKED;
35
36 #define VESA_SIGNATURE 0x41534556 // VESA
37 #define VBE2_SIGNATURE 0x32454256 // VBE2
38
39 struct vesa_mode_info
40 {
41     u16 mode_attributes;
42     u8 win_a_attributes;
43     u8 win_b_attributes;
44     u16 win_granularity;
45     u16 win_size;
46     u16 win_a_segment;
47     u16 win_b_segment;
48     u32 win_func_ptr;
49     u16 bytes_per_scanline;
50     u16 x_resolution;
51     u16 y_resolution;
52     u8 x_charsize;
53     u8 y_charsize;
54     u8 number_of_planes;
55     u8 bits_per_pixel;
56     u8 number_of_banks;
57     u8 memory_model;
58     u8 bank_size;
59     u8 number_of_image_pages;
60     u8 reserved_page;
61     u8 red_mask_size;
62     u8 red_mask_pos;
63     u8 green_mask_size;
64     u8 green_mask_pos;
65     u8 blue_mask_size;
66     u8 blue_mask_pos;
67     u8 reserved_mask_size;
68     u8 reserved_mask_pos;
69     u8 direct_color_mode_info;
70     u32 phys_base_ptr;
71     u32 offscreen_mem_offset;
72     u16 offscreen_mem_size;
73     u8 reserved[206];
74 } PACKED;
75
76 /****************************************************************
77  * Helper functions
78  ****************************************************************/
79
80 // Call int10 vga handler.
81 static void
82 call16_int10(struct bregs *br)
83 {
84     br->flags = F_IF;
85     start_preempt();
86     call16_int(0x10, br);
87     finish_preempt();
88 }
89
90
91 /****************************************************************
92  * VGA text / graphics console
93  ****************************************************************/
94
95 static void enable_vga_text_console(void)
96 {
97     dprintf(1, "Turning on vga text mode console\n");
98     struct bregs br;
99
100     /* Enable VGA text mode */
101     memset(&br, 0, sizeof(br));
102     br.ax = 0x0003;
103     call16_int10(&br);
104
105     // Write to screen.
106     printf("SeaBIOS (version %s)\n\n", VERSION);
107 }
108
109 void enable_vga_console(void)
110 {
111     struct vesa_info *vesa_info = NULL;
112     struct vesa_mode_info *mode_info = NULL;
113     struct jpeg_decdata *decdata = NULL;
114     u8 *jpeg = NULL, *picture = NULL;
115
116     /* Needs coreboot support for CBFS */
117     if (!CONFIG_BOOTSPLASH || !CONFIG_COREBOOT)
118         goto gotext;
119     struct cbfs_file *file = cbfs_finddatafile("bootsplash.jpg");
120     if (!file)
121         goto gotext;
122     int filesize = cbfs_datasize(file);
123
124     int imagesize = (CONFIG_BOOTSPLASH_X * CONFIG_BOOTSPLASH_Y *
125                      (CONFIG_BOOTSPLASH_DEPTH / 8));
126     jpeg = malloc_tmphigh(filesize);
127     picture = malloc_tmphigh(imagesize);
128     vesa_info = malloc_tmplow(sizeof(*vesa_info));
129     mode_info = malloc_tmplow(sizeof(*mode_info));
130     decdata = malloc_tmphigh(sizeof(*decdata));
131     if (!jpeg || !picture || !vesa_info || !mode_info || !decdata) {
132         warn_noalloc();
133         goto gotext;
134     }
135
136     /* Check whether we have a VESA 2.0 compliant BIOS */
137     memset(vesa_info, 0, sizeof(struct vesa_info));
138     vesa_info->vesa_signature = VBE2_SIGNATURE;
139     struct bregs br;
140     memset(&br, 0, sizeof(br));
141     br.ax = 0x4f00;
142     br.di = FLATPTR_TO_OFFSET(vesa_info);
143     br.es = FLATPTR_TO_SEG(vesa_info);
144     call16_int10(&br);
145     if (vesa_info->vesa_signature != VESA_SIGNATURE) {
146         dprintf(1,"No VBE2 found.\n");
147         goto gotext;
148     }
149
150     /* Print some debugging information about our card. */
151     char *vendor = SEGOFF_TO_FLATPTR(vesa_info->oem_vendor_name_ptr);
152     char *product = SEGOFF_TO_FLATPTR(vesa_info->oem_product_name_ptr);
153     dprintf(8, "VESA %d.%d\nVENDOR: %s\nPRODUCT: %s\n",
154             vesa_info->vesa_version>>8, vesa_info->vesa_version&0xff,
155             vendor, product);
156
157     /* Get information about our graphics mode, like the
158      * framebuffer start address
159      */
160     memset(&br, 0, sizeof(br));
161     br.ax = 0x4f01;
162     br.cx = (1 << 14) | CONFIG_BOOTSPLASH_VESA_MODE;
163     br.di = FLATPTR_TO_OFFSET(mode_info);
164     br.es = FLATPTR_TO_SEG(mode_info);
165     call16_int10(&br);
166     if (br.ax != 0x4f) {
167         dprintf(1, "get_mode failed.\n");
168         goto gotext;
169     }
170     unsigned char *framebuffer = (unsigned char *) (mode_info->phys_base_ptr);
171
172     /* Switch to graphics mode */
173     memset(&br, 0, sizeof(br));
174     br.ax = 0x4f02;
175     br.bx = (1 << 14) | CONFIG_BOOTSPLASH_VESA_MODE;
176     call16_int10(&br);
177     if (br.ax != 0x4f) {
178         dprintf(1, "set_mode failed.\n");
179         goto gotext;
180     }
181
182     dprintf(8, "framebuffer: %x\n", (u32)framebuffer);
183     dprintf(8, "bytes per scanline: %d\n", mode_info->bytes_per_scanline);
184     dprintf(8, "bits per pixel: %d\n", mode_info->bits_per_pixel);
185
186     /* Decompress jpeg */
187     dprintf(8, "Copying boot splash screen...\n");
188     cbfs_copyfile(file, jpeg, filesize);
189     dprintf(8, "Decompressing boot splash screen...\n");
190     int ret = jpeg_decode(jpeg, picture, CONFIG_BOOTSPLASH_X,
191                           CONFIG_BOOTSPLASH_Y, CONFIG_BOOTSPLASH_DEPTH, decdata);
192     if (ret) {
193         dprintf(1, "jpeg_decode failed with return code %d...\n", ret);
194         goto gotext;
195     }
196
197     /* Show the picture */
198     iomemcpy(framebuffer, picture, imagesize);
199     SET_EBDA(bootsplash_active, 1);
200
201 cleanup:
202     free(jpeg);
203     free(picture);
204     free(vesa_info);
205     free(mode_info);
206     free(decdata);
207     return;
208 gotext:
209     enable_vga_text_console();
210     goto cleanup;
211 }
212
213 void
214 disable_bootsplash(void)
215 {
216     if (! CONFIG_BOOTSPLASH || !GET_EBDA(bootsplash_active))
217         return;
218     SET_EBDA(bootsplash_active, 0);
219     enable_vga_text_console();
220 }