BIOS: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
No edit summary
(Expanded stubbed article)
Line 1: Line 1:
BIOS (Basic Input/Output System) was created to offer generalized low-level services to early PC system programmers.
BIOS (Basic Input/Output System) was intended to offer low-level services to system programmers, like changing the video mode, accessing disks, asking system properties (amount of RAM, etc). In [[Real Mode]], these services can be accessed through interrupts (10h for video, 13h for disks, 15h for miscellaneous stuff). You may get the complete list of BIOS interrupts in [http://www.ctyme.com/rbrown.htm Ralf Browns Interrupt List].
The basic aims were: to hide (as much as possible) variations in PC models and hardware from the OS and applications,
and to make OS and application development easier (because the BIOS services handled most of the hardware level interface).


These BIOS services are still used (especially during bootup), and are often named "BIOS functions".
Also note that some of the bios detection/state result is stored in the [http://www.nondot.org/sabre/os/files/Booting/BIOS_SEG.txt Bios Data Area]. All the "persistent" state of the BIOS is kept in the [[CMOS]] chip.
In [[Real Mode]], they can be easily accessed through [[software interrupts]], using [[Assembly]] language.


==BIOS functions==
Unfortunately, in [[Protected mode]], most of BIOS services become unavailable and trying to call them nonetheless will result in exceptions or unreliable responses because of the different way '''segment''' values are handled. Some newer services however (like SMBios, PCI and PnP, or VBE) offer an interface that is compatible with 32bits operations.


To access a BIOS function, you generally set the AH [[CPU register]] (or AX, or EAX) to a particular value, and then do
If you still need the other services in [[Protected mode]], see [[Virtual 8086 Mode]].
an INT opcode. The value in AH (or AX, or EAX), combined with the particular interrupt number selected requests a specific
BIOS function. (Other CPU registers hold any "arguments" to the function, and often the return values from the function, also.)

It is simplest to create a listing of BIOS functions by specifying the interrupt number, and the value
of AH (or AX, or EAX) that selects the function. It is also easiest to refer to particular BIOS functions this way in discussions. For example,
INT 0x13, AH=0 is a BIOS function that resets hard disks or floppy disks.

Note: the INT and AH values are always listed in hexadecimal notation. Accidentally using a decimal value in an INT opcode
is a very common source of errors when using BIOS functions.

To an extent, the BIOS functions are organized by interrupt number:
* INT 0x10 = Video display functions (including VESA/VBE)
* INT 0x13 = mass storage (disk, floppy) access
* INT 0x15 = memory size functions
* INT 0x16 = keyboard functions

The exhaustive list of BIOS functions is available from [[RBIL]].

Unfortunately, the PC industry has never been good about maintaining standards. So each PC manufacturer and each BIOS
manufacturer randomly made up new BIOS functions. It is also possible to "hook" any of these interrupts, and insert extra
functions that mimic BIOS functions. Early PC hardware and software manufacturers did this often. So there ended up being literally
thousands of BIOS functions (or mimics). The RBIL list is enormous, and is mostly filled with functions that only
work when combined with some completely obsolete computer, BIOS, or piece of hardware or software.

===Common functions===

Unfortunately, RBIL does not clearly indicate which BIOS functions are "generic" (in some sense). That is, the ones that are always available,
and that everyone uses. Partially this is because the "standard" BIOS functions grew over time, so if you go back far enough in time you can
usually find a computer that does not support almost any specific BIOS function.
But there is definitely a set that is commonly used in most current OSes.

* INT 0x10 ??


(see [[ATA_in_x86_RealMode_(BIOS)|ATA using BIOS]] for more detail on these BIOS function calls)
* INT 0x13, AH = 0 -- reset floppy/hard disk
* INT 0x13, AH = 2 -- read floppy/hard disk in CHS mode
* INT 0x13, AH = 3 -- write floppy/hard disk in CHS mode
* INT 0x13, AH = 0x41 -- test existence of INT 13 extensions
* INT 0x13, AH = 0x42 -- read hard disk in LBA mode
* INT 0x13, AH = 0x43 -- write hard disk in LBA mode


(see [[Detecting Memory (x86)]] for more detail on these BIOS function calls)
* INT 0x12 -- get low memory size
* INT 0x15, EAX = 0xE820 -- get complete memory map
* INT 0x15, AX = 0xE801 -- get contiguous memory size
* INT 0x15, AX = 0xE881 -- get contiguous memory size
* INT 0x15, AH = 0x88 -- get contiguous memory size



* INT 0x16, AH = 0 -- read keyboard scancode (blocking)
* INT 0x16, AH = 1 -- read keyboard scancode (non-blocking)


===ASM notes===

Each BIOS function (as described in RBIL) has a specific set of "result" registers. Beyond those listed registers,
the BIOS functions are supposed to perfectly preserve all the other register values. Early versions of Bochs (below 2.3)
had a small problem with this. The lower halves of all the 32bit extended registers (ie. EBX, ECX) were preserved properly, but
the upper words of some of the registers got trashed.

The BIOS functions themselves should never crash. On any error they will:
* almost always set the carry flag (test with JC),
* sometimes return "ah = 0x86 (unsupported function)",
* sometimes return "ah = 0x80 (invalid command)"
* or (for seriously buggy BIOSes) return with nothing changed.

Try to always test these error returns, because in many circumstances the BIOS functions might appear to be returning valid
(but very wrong) data -- rather than an error code.


==BIOS in Protected Mode==

Unfortunately, in [[Protected mode]], almost all BIOS functions become unavailable, and trying to call them nonetheless will result in exceptions or
unreliable responses (because of the different way '''segment''' values are handled). Some newer services however (such as SMBios, PCI, PnP, or VBE)
offer an interface that is compatible with 32bit Protected Mode.

If you must use Real Mode BIOS functions after the CPU has been switched into Protected Mode, then see [[Virtual 8086 Mode]],
or perhaps exit Protected Mode, and momentarily return to [[Real Mode]]. Both methods have associated problems, so try to get everything you
need from the BIOS before you ever enter Protected Mode.


==Additional Information from the BIOS==

Most of the useful information you get from the BIOS will come from calling BIOS functions. However, there is a small amount
of additional information that can be aquired.

Some of the BIOS detection/state result is stored in the [[Memory Map (x86)#BDA|Bios Data Area]].

Additional information is kept in the [[CMOS]] chip.


[[Category:X86]]
[[Category:X86]]

Revision as of 07:04, 16 May 2008

BIOS (Basic Input/Output System) was created to offer generalized low-level services to early PC system programmers. The basic aims were: to hide (as much as possible) variations in PC models and hardware from the OS and applications, and to make OS and application development easier (because the BIOS services handled most of the hardware level interface).

These BIOS services are still used (especially during bootup), and are often named "BIOS functions". In Real Mode, they can be easily accessed through software interrupts, using Assembly language.

BIOS functions

To access a BIOS function, you generally set the AH CPU register (or AX, or EAX) to a particular value, and then do an INT opcode. The value in AH (or AX, or EAX), combined with the particular interrupt number selected requests a specific BIOS function. (Other CPU registers hold any "arguments" to the function, and often the return values from the function, also.)

It is simplest to create a listing of BIOS functions by specifying the interrupt number, and the value of AH (or AX, or EAX) that selects the function. It is also easiest to refer to particular BIOS functions this way in discussions. For example, INT 0x13, AH=0 is a BIOS function that resets hard disks or floppy disks.

Note: the INT and AH values are always listed in hexadecimal notation. Accidentally using a decimal value in an INT opcode is a very common source of errors when using BIOS functions.

To an extent, the BIOS functions are organized by interrupt number:

  • INT 0x10 = Video display functions (including VESA/VBE)
  • INT 0x13 = mass storage (disk, floppy) access
  • INT 0x15 = memory size functions
  • INT 0x16 = keyboard functions

The exhaustive list of BIOS functions is available from RBIL.

Unfortunately, the PC industry has never been good about maintaining standards. So each PC manufacturer and each BIOS manufacturer randomly made up new BIOS functions. It is also possible to "hook" any of these interrupts, and insert extra functions that mimic BIOS functions. Early PC hardware and software manufacturers did this often. So there ended up being literally thousands of BIOS functions (or mimics). The RBIL list is enormous, and is mostly filled with functions that only work when combined with some completely obsolete computer, BIOS, or piece of hardware or software.

Common functions

Unfortunately, RBIL does not clearly indicate which BIOS functions are "generic" (in some sense). That is, the ones that are always available, and that everyone uses. Partially this is because the "standard" BIOS functions grew over time, so if you go back far enough in time you can usually find a computer that does not support almost any specific BIOS function. But there is definitely a set that is commonly used in most current OSes.

  • INT 0x10 ??


(see ATA using BIOS for more detail on these BIOS function calls)

  • INT 0x13, AH = 0 -- reset floppy/hard disk
  • INT 0x13, AH = 2 -- read floppy/hard disk in CHS mode
  • INT 0x13, AH = 3 -- write floppy/hard disk in CHS mode
  • INT 0x13, AH = 0x41 -- test existence of INT 13 extensions
  • INT 0x13, AH = 0x42 -- read hard disk in LBA mode
  • INT 0x13, AH = 0x43 -- write hard disk in LBA mode


(see Detecting Memory (x86) for more detail on these BIOS function calls)

  • INT 0x12 -- get low memory size
  • INT 0x15, EAX = 0xE820 -- get complete memory map
  • INT 0x15, AX = 0xE801 -- get contiguous memory size
  • INT 0x15, AX = 0xE881 -- get contiguous memory size
  • INT 0x15, AH = 0x88 -- get contiguous memory size


  • INT 0x16, AH = 0 -- read keyboard scancode (blocking)
  • INT 0x16, AH = 1 -- read keyboard scancode (non-blocking)


ASM notes

Each BIOS function (as described in RBIL) has a specific set of "result" registers. Beyond those listed registers, the BIOS functions are supposed to perfectly preserve all the other register values. Early versions of Bochs (below 2.3) had a small problem with this. The lower halves of all the 32bit extended registers (ie. EBX, ECX) were preserved properly, but the upper words of some of the registers got trashed.

The BIOS functions themselves should never crash. On any error they will:

  • almost always set the carry flag (test with JC),
  • sometimes return "ah = 0x86 (unsupported function)",
  • sometimes return "ah = 0x80 (invalid command)"
  • or (for seriously buggy BIOSes) return with nothing changed.

Try to always test these error returns, because in many circumstances the BIOS functions might appear to be returning valid (but very wrong) data -- rather than an error code.


BIOS in Protected Mode

Unfortunately, in Protected mode, almost all BIOS functions become unavailable, and trying to call them nonetheless will result in exceptions or unreliable responses (because of the different way segment values are handled). Some newer services however (such as SMBios, PCI, PnP, or VBE) offer an interface that is compatible with 32bit Protected Mode.

If you must use Real Mode BIOS functions after the CPU has been switched into Protected Mode, then see Virtual 8086 Mode, or perhaps exit Protected Mode, and momentarily return to Real Mode. Both methods have associated problems, so try to get everything you need from the BIOS before you ever enter Protected Mode.


Additional Information from the BIOS

Most of the useful information you get from the BIOS will come from calling BIOS functions. However, there is a small amount of additional information that can be aquired.

Some of the BIOS detection/state result is stored in the Bios Data Area.

Additional information is kept in the CMOS chip.