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