Basic Input Output System


Basic Input Output System

BIOS is the system that standarizes comunication between CPU and hardware. Usually with assembly instructions we can MOV, IN or OUT data from cpu registers or memory to, for example, disk. But due to this being inefficient (compared to a printed circiut board), the BIOS has special buses to do this data movement and all the hardware that implements the BIOS can comunicate faster with this buses to load data and send instructions to the hardware.

Once we send the power signal to the AC power, it starts the Motherboard and then setting the registers of the CPU in a certain way so when CPU loop starts it will be going to load BIOS and setting memory segmentation.

The Basic Input/Output System (BIOS) is a firmware program built into the motherboard of a computer. Its primary function is to initialize and test the hardware components during the boot process and to provide a low-level interface for communicating with them.

Here are the basic steps involved in how the BIOS works:

  1. Power-on self-test (POST): The BIOS starts by performing a POST, which checks the essential hardware components such as the CPU, memory, and disk drives. If any of these components fail the test, the BIOS will halt the boot process and display an error message.
  2. BIOS initialization: After the POST is complete, the BIOS initializes the system hardware, including setting up the interrupt handlers, initializing the keyboard and mouse, and configuring the system clock.
  3. Boot loader: The BIOS then searches for a boot loader on the boot device (usually the hard drive). The boot loader is responsible for loading the operating system into memory and starting its execution.
  4. BIOS services: The BIOS provides various services to the operating system, such as reading and writing to the disk drives, handling keyboard and mouse input, and accessing system resources.

Here's an example of some assembly code that might be used in the BIOS:

ASM

org 0x7c00           ; BIOS loads boot sector to 0x7c00
jmp start ; jump to start of program
nop ; padding

; Initialize stack pointer
start:
mov ax, 0x9000 ; set stack to 0x9000
mov ss, ax
mov sp, 0xffff

; Display message
mov ah, 0x0e ; select BIOS teletype function
mov al, 'H' ; display 'H'
int 0x10 ; invoke BIOS function
mov al, 'i' ; display 'i'
int 0x10
mov al, '!' ; display '!'
int 0x10

; Infinite loop
loop:
jmp loop ; jump back to start of loop

In this example, the code initializes the stack pointer, displays a message on the screen using BIOS teletype function, and enters an infinite loop. This code is typically located at the beginning of the boot sector, which is loaded by the BIOS into memory and executed during the boot process.

BIOS Initialization (step 2)

During the BIOS initialization process, interrupt handlers are loaded into main memory through a process called interrupt vectoring.

Interrupt vectoring is a mechanism that allows the BIOS to redirect hardware interrupts to the appropriate interrupt handler routine in memory. The BIOS maintains a table called the interrupt vector table, which contains a list of memory addresses where the interrupt handlers are located.

When a hardware interrupt occurs, the BIOS reads the interrupt vector table to determine the memory address of the corresponding interrupt handler. It then redirects the interrupt to that address, and the interrupt handler routine is executed.

The interrupt vector table is usually located in a fixed location in memory, which is known as the interrupt vector base address (IVT). The IVT is typically located at memory address 0x0000:0x0000 and is 1KB in size.

During the BIOS initialization process, the BIOS sets up the IVT by loading the addresses of the appropriate interrupt handlers into the table. It also sets up the interrupt descriptor table (IDT), which is used by the operating system to handle interrupts once it has taken control of the system.

Overall, interrupt vectoring is an essential part of the BIOS initialization process, as it allows the BIOS to properly handle hardware interrupts and redirect them to the appropriate interrupt handlers in memory.

At this level the memory starts with the interruption table in the fist KB which is a list of actual pointers to the drivers of input/output hardware such as: display, keyboard, cursor, ports, and other hardware that implements the BIOS. https://en.wikipedia.org/wiki/BIOS_interrupt_call

In memory after the interruption table we have the BIOS arguments such as the keyboard buffer, Display configuration, and so on...

(check and details from: https://0xax.gitbooks.io/linux-insides/content/Booting/linux-bootstrap-1.html)

The physical address is calculated from 2 parts. i) segment address. ii) offset address. The CS(code segment register) is used to address the code segment of the memory i.e a location in the memory where the code is stored. The IP(Instruction pointer) contains the offset within the code segment of the memory. Hence CS:IP is used to point to the location (i.e to calculate the physical address)of the code in the memory.

In general, real mode's memory map is as follows:

0x00000000 - 0x000003FF - Real Mode Interrupt Vector Table

0x00000400 - 0x000004FF - BIOS Data Area

0x00000500 - 0x00007BFF - Unused

0x00007C00 - 0x00007DFF - Our Bootloader

0x00007E00 - 0x0009FFFF - Unused

0x000A0000 - 0x000BFFFF - Video RAM (VRAM) Memory

0x000B0000 - 0x000B7777 - Monochrome Video Memory

0x000B8000 - 0x000BFFFF - Color Video Memory

0x000C0000 - 0x000C7FFF - Video ROM BIOS

0x000C8000 - 0x000EFFFF - BIOS Shadow Area

0x000F0000 - 0x000FFFFF - System BIOS

There is also EFI

EFI (Extensible Firmware Interface)

Function: A modern firmware interface that replaces the older BIOS (Basic Input/Output System). EFI provides a more flexible and powerful way to initialize hardware components before the operating system loads.

Benefits: Supports booting from larger hard drives, allows for advanced features like secure boot, and provides a more extensible platform for future hardware advancements.

Secure Boot

Function: A security feature that restricts the operating system that can boot on a computer. It verifies the digital signature of the boot loader and operating system kernel before allowing them to load, preventing unauthorized or potentially malicious software from taking control.

Benefits: Enhances system security by making it more difficult for malware to infect the system at the boot level.

PAE/NX (Physical Address Extension/NX bit)

PAE (Physical Address Extension): Enables a computer system to use more than 4 GB of physical memory (RAM) by extending the addressing capabilities of the CPU.

NX bit (No-Execute bit): A hardware feature that helps prevent certain types of malware attacks by marking memory pages as non-executable. This prevents code from being run in data areas, making it more difficult for malicious programs to exploit vulnerabilities.

Combined Benefits: PAE allows for larger memory usage, while NX enhances security by mitigating exploit attempts.

VT-x (Intel Virtualization Technology)

Function: A hardware virtualization technology developed by Intel that allows a single computer system to run multiple virtual machines (VMs) efficiently. VT-x provides hardware-assisted virtualization features that improve the performance of VMs by offloading certain tasks from the host operating system to the underlying hardware.

Benefits: Enables efficient virtualization for running multiple operating systems or applications on a single physical machine, improving resource utilization and flexibility.

AMD-V (AMD Virtualization)

Function: AMD's equivalent to Intel VT-x, providing hardware-assisted virtualization capabilities for running multiple VMs on a single AMD processor. It offers similar benefits to VT-x.

Benefits: Enables efficient virtualization for running multiple operating systems or applications on a single AMD physical machine.

Boot loader (step 3)

After BIOS initialization, the boot loader process for the Linux kernel typically involves the following steps:

  1. The BIOS searches for a bootable device and loads the boot loader into memory. The boot loader is typically located in the Master Boot Record (MBR) of a disk drive or in a separate boot partition.
  2. The boot loader, such as GRUB (Grand Unified Bootloader), displays a boot menu if multiple boot options are available. The user can select the kernel and the operating system to boot.
  3. The boot loader reads the configuration files, such as /boot/grub/grub.cfg, to determine the location of the kernel and the initial RAM disk (initrd).
  4. The boot loader loads the kernel image into memory and transfers control to it. The kernel is loaded as a compressed image, which is decompressed by the boot loader.
  5. The kernel initializes the hardware and sets up the memory management, file system, and other essential components.
  6. The kernel mounts the initial RAM disk, which contains a minimal file system with essential utilities and drivers needed to boot the system. The initrd may also contain additional drivers and firmware for hardware devices that are not detected by the kernel.
  7. The kernel executes the init program, which is specified in the initrd/initramfs configuration file. The init program initializes the system services and runs the startup scripts to bring up the system.
  8. The startup scripts configure the network, start the daemons, and launch the user interface or console login prompt.

Overall, the boot loader process for the Linux kernel involves several steps, from loading the kernel and initrd into memory to executing the startup scripts and launching the user interface. The boot loader and kernel work together to initialize the hardware, mount the file systems, and set up the system services needed to boot the Linux operating system.

Later on, the boot loader of the operating system may continue or overwrite those datastructures in the first addresses of memory to put there the system interruptions (system calls).

| Protected-mode kernel |

100000 +------------------------+

| I/O memory hole |

0A0000 +------------------------+

| Reserved for BIOS | Leave as much as possible unused

~ ~

| Command line | (Can also be below the X+10000 mark)

X+10000 +------------------------+

| Stack/heap | For use by the kernel real-mode code.

X+08000 +------------------------+

| Kernel setup | The kernel real-mode code.

| Kernel boot sector | The kernel legacy boot sector.

X +------------------------+

| Boot loader | <- Boot sector entry point 0x7C00

001000 +------------------------+

| Reserved for MBR/BIOS |

000800 +------------------------+

| Typically used by MBR |

000600 +------------------------+

| BIOS use only |

000000 +------------------------+