Default bootsplash on (for coreboot users).
[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     void *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 static int
110 find_videomode(struct vesa_info *vesa_info, struct vesa_mode_info *mode_info
111                , int width, int height)
112 {
113     dprintf(3, "Finding vesa mode with dimensions %d/%d\n", width, height);
114     u16 *videomodes = SEGOFF_TO_FLATPTR(vesa_info->video_mode_ptr);
115     for (;; videomodes++) {
116         u16 videomode = *videomodes;
117         if (videomode == 0xffff) {
118             dprintf(1, "Unable to find vesa video mode dimensions %d/%d\n"
119                     , width, height);
120             return -1;
121         }
122         struct bregs br;
123         memset(&br, 0, sizeof(br));
124         br.ax = 0x4f01;
125         br.cx = (1 << 14) | videomode;
126         br.di = FLATPTR_TO_OFFSET(mode_info);
127         br.es = FLATPTR_TO_SEG(mode_info);
128         call16_int10(&br);
129         if (br.ax != 0x4f) {
130             dprintf(1, "get_mode failed.\n");
131             continue;
132         }
133         if (mode_info->x_resolution != width
134             || mode_info->y_resolution != height)
135             continue;
136         u8 depth = mode_info->bits_per_pixel;
137         if (depth != 16 && depth != 24 && depth != 32)
138             continue;
139         return videomode;
140     }
141 }
142
143 void enable_vga_console(void)
144 {
145     struct vesa_info *vesa_info = NULL;
146     struct vesa_mode_info *mode_info = NULL;
147     struct jpeg_decdata *jpeg = NULL;
148     u8 *filedata = NULL, *picture = NULL;
149
150     /* Needs coreboot support for CBFS */
151     if (!CONFIG_BOOTSPLASH || !CONFIG_COREBOOT)
152         goto gotext;
153     struct cbfs_file *file = cbfs_finddatafile("bootsplash.jpg");
154     if (!file)
155         goto gotext;
156     int filesize = cbfs_datasize(file);
157
158     filedata = malloc_tmphigh(filesize);
159     vesa_info = malloc_tmplow(sizeof(*vesa_info));
160     mode_info = malloc_tmplow(sizeof(*mode_info));
161     jpeg = jpeg_alloc();
162     if (!filedata || !jpeg || !vesa_info || !mode_info) {
163         warn_noalloc();
164         goto gotext;
165     }
166
167     /* Check whether we have a VESA 2.0 compliant BIOS */
168     memset(vesa_info, 0, sizeof(struct vesa_info));
169     vesa_info->vesa_signature = VBE2_SIGNATURE;
170     struct bregs br;
171     memset(&br, 0, sizeof(br));
172     br.ax = 0x4f00;
173     br.di = FLATPTR_TO_OFFSET(vesa_info);
174     br.es = FLATPTR_TO_SEG(vesa_info);
175     call16_int10(&br);
176     if (vesa_info->vesa_signature != VESA_SIGNATURE) {
177         dprintf(1,"No VBE2 found.\n");
178         goto gotext;
179     }
180
181     /* Print some debugging information about our card. */
182     char *vendor = SEGOFF_TO_FLATPTR(vesa_info->oem_vendor_name_ptr);
183     char *product = SEGOFF_TO_FLATPTR(vesa_info->oem_product_name_ptr);
184     dprintf(3, "VESA %d.%d\nVENDOR: %s\nPRODUCT: %s\n",
185             vesa_info->vesa_version>>8, vesa_info->vesa_version&0xff,
186             vendor, product);
187
188     // Parse jpeg and get image size.
189     cbfs_copyfile(file, filedata, filesize);
190     int ret = jpeg_decode(jpeg, filedata);
191     if (ret) {
192         dprintf(1, "jpeg_decode failed with return code %d...\n", ret);
193         goto gotext;
194     }
195     int width, height;
196     jpeg_get_size(jpeg, &width, &height);
197
198     // Try to find a graphics mode with the corresponding dimensions.
199     int videomode = find_videomode(vesa_info, mode_info, width, height);
200     if (videomode < 0)
201         goto gotext;
202     void *framebuffer = mode_info->phys_base_ptr;
203     int depth = mode_info->bits_per_pixel;
204     dprintf(3, "mode: %04x\n", videomode);
205     dprintf(3, "framebuffer: %p\n", framebuffer);
206     dprintf(3, "bytes per scanline: %d\n", mode_info->bytes_per_scanline);
207     dprintf(3, "bits per pixel: %d\n", depth);
208
209     // Allocate space for image and decompress it.
210     int imagesize = width * height * (depth / 8);
211     picture = malloc_tmphigh(imagesize);
212     if (!picture) {
213         warn_noalloc();
214         goto gotext;
215     }
216     ret = jpeg_show(jpeg, picture, width, height, depth);
217     if (ret) {
218         dprintf(1, "jpeg_show failed with return code %d...\n", ret);
219         goto gotext;
220     }
221
222     /* Switch to graphics mode */
223     memset(&br, 0, sizeof(br));
224     br.ax = 0x4f02;
225     br.bx = (1 << 14) | videomode;
226     call16_int10(&br);
227     if (br.ax != 0x4f) {
228         dprintf(1, "set_mode failed.\n");
229         goto gotext;
230     }
231
232     /* Show the picture */
233     iomemcpy(framebuffer, picture, imagesize);
234     SET_EBDA(bootsplash_active, 1);
235
236 cleanup:
237     free(filedata);
238     free(picture);
239     free(vesa_info);
240     free(mode_info);
241     free(jpeg);
242     return;
243 gotext:
244     enable_vga_text_console();
245     goto cleanup;
246 }
247
248 void
249 disable_bootsplash(void)
250 {
251     if (!CONFIG_BOOTSPLASH  || !CONFIG_COREBOOT || !GET_EBDA(bootsplash_active))
252         return;
253     SET_EBDA(bootsplash_active, 0);
254     enable_vga_text_console();
255 }