How do I set a graphics mode

From OSDev.wiki
Jump to navigation Jump to search

When a PC first boots up, it is set to a standard, known VGA text mode. But at some point you will want to start drawing pixels to the screen instead of text. This requires you to switch from text mode to a graphics mode. In 16-bit mode, you can easily use the BIOS to switch between different graphics modes by calling different functions of interrupt 0x10. Once you enter protected mode, however, things get a bit more difficult.

Using the UEFI

For EFI, the standard was Universal Graphics Adapter, UGA. It had serious shortcomings (like not able to report framebuffer address), so in UEFI it was replaced by Graphics Output Protocol (GOP).

Using the BIOS

Each video card you find in a PC comes with a piece of code that allows it to be initialized, the video BIOS. This bios is expected to run in Real Mode, and as long as you are, you can access all the functions directly by calling int 0x10 with the right values in the registers. If your kernel is based on a tutorial, you can probably not do this, because for practically all tutorials, you are in protected mode, and not in real mode.

Many video cards have two interfaces, one VGA interface for low resolutions, and the VESA VBE interface for higher resolutions. Alternatively, you can write your own code to directly deal with the graphics hardware.

In Protected Mode or Long Mode, the BIOS interface is not directly accessible. A number of approaches can be used to still use it. They all work by setting or simulating real mode and calling the interrupt that way.

Methods for drawing via the BIOS

  • Drop back to real mode, then call the BIOS. The easiest solution, but also the most ugly. Be careful to restore everything to real-mode values when you do.
  • Use Virtual 8086 Mode. The most common good solution for protected mode. This way you can run the BIOS from a virtual machine style environment, thus maintaining control over the process. Virtual 8086 mode is however not available from Long Mode.
  • Write/use an emulator. The slowest and most intensive approach, but has the advantage that it will work under any condition, including long mode and other platforms.

Writing specific drivers

The alternative approach is to not use the BIOS at all. In this case, you must write your own drivers to interact with the hardware.

The simplest way to get graphics modes without using BIOS would be to develop or reuse an existing VGA driver. This gives you all low resolution modes on practically all hardware, and is easier to do than setting up a virtual machine of any sort.

However, if you want to support high resolutions, you must write a driver for each graphics card that you want your OS to support. Only recommended if you have more than one life to waste. Note that there is no official documentation available for a lot of video cards, and that you need to resort to second-hand information (which tends to be incomplete) and existing driver implementations. If you want hardware acceleration, this is the only way to go.