Sunday, September 7, 2008

Boot Process


Computer initialization is a process, not an event.
In general, a computer system life cycle consists of
  • Booting
  • Kernel initialization
  • Device initialization
  • Operation mode
  • Shut down or restart

Applying power

PC initialization starts when we turn on the system.
When all the input voltages from a power supply are valid, the supply generates a PowerGood (PG) logic signal.
When the motherboard timer chip receives the PG signal, the timer stops forcing a Reset signal to the CPU.
At this point the CPU starts processing.

Bootstrap

Bootstrap loader is the program that loads the kernel.
Bootstrap loader is an example of an absolute loader.
The very first operation performed by the CPU is to fetch an instruction from address FFFF:000h.
Since this address is almost at the end of the available ROM space, the instruction is always a jump command (JMP) followed by the actual BIOS ROM starting address.
By making all the CPUs start at the same point, the BIOS ROM can send program control anywhere in the particular ROM (and each ROM is usually different).
this initial search of address FFFF:000h and the subsequent redirection of the CPU is traditionally referred to as the bootstrap.
A boot manager lets to choose the first program to execute.

There are generally three things that a BIOS does in chronological order:
  • A POST(Power On Self Test): It checks whether or not the hardware connected to the system is working fine.
  • Hardware devices are then initialized.
  • It then searches for an operating system; boot sectors for different media are searched; if a valid sector is found then its executed.

Core Tests

Core test are the part of the power on self test sequence, which is the most important use of the system BIOS during initialization.
To ensure integrity a set of hardware-specific self-test routines check the major motherboard components and identifies the presence of any specialized BIOS chip in the system (driver controller BIOS, video BIOS, SCSI BIOS, and so on).
If an error is detected in the early phase of testing, a series of beeps (or beep codes) are produced. By knowing the BIOS manufacturer and the beep code we can determine the problem.
Next, the BIOS looks for the presence of a video ROM from memory location C000:000h through C780:00h. In just about all the systems, the search will reveal a video BIOS ROM on a video adaptor board plugged into an available expansion slot.
If the test is successful the control is transferred to the video BIOS which loads and initializes the video adapter.
When no external video adapter BIOS is located, the system BIOS provides an initialization routine for the motherboard's video adaptor and the cursor appears.
Now that the video system is ready , system BIOS will scan memory from C800:0000h through DF80:0000h in 2KB increment s to the search for other ROMSthat might be on the adapter card on the system.If found their contents are tested and run.

POST

BIOS then checks the memory location at 0000:0472h. This address contains a flag that determines whether the initialization is a cold start (power first applied) or a warm start (reset button or a CTRL-ALT-DEL key combination).
A value of 1234h indicates a warm starts in which the POST routine is skipped.If any other value is found at that location, a cold start is assumed, and the full POST routine will be executed.
The full POST routine checks many other high level functions on the motherboard, memory, keyboard, video adapter, floppy drive, math coprocessor, printer port, serial port, hard drive, and other sub-systems.
If the POST completes successfully, the system will respond with a single beep, else it displays an error message and the system initialization halts.

Finding the Operating System

The system now needs to load an operating system (let us consider DOS, since it is more simple).The first step is to have the BIOS search for a DOS VBS on the media.BIOS will load sector 1 (head 0 cylinder 0) from the disk's DOS VBS into the memory starting at 000:7C00h.
The BIOS loads the sector from the disk's master partition boot sector (called the master boot sector; MBS), into memory starting at 0000:7C00h, and the last two bytes of the partition are checked.
If the last two bytes of the master boot sector is not 55h and AAh, respectively, the boot sector is invalid and a "No boot device available" message is displayed, and the system initialization will halt.

The disk will search for and identify any extended partitions. Once its found the drive's original boot sector will search for a boot indicator byte marking a partition as active and bootable. If non or more than one is marked as bootable a disk error message will be displayed such as "Invalid partition table".

When an active partition is found in the master boot sector, the DOS VBS from the bootable partition is loaded into memory and tested. If the DOS VBS cannot be read the message "Error loading operating system" will be displayed.If signature bytes are missing it shows an error.
After identifying the signature bits, the DOS VBS (now in memory) is executed as if it were a program.

Loading the OS

If no problem is detected in the disk's DOS VBS, IO.SYS is loaded and executed. Next MSDOS.SYS executed.

Establishing the environment

If a CONFIG.SYS is present, it is opened and read by IO.SYS.
The device statements are processed in the order they appear.
A SHELL statement is handled next.
If no shell statement is present the COMMAND.COM processor is loaded.
It overwrites the initialization code leftover from IO.SYS.
Under windows COMMAND.COM is loaded only if an AUTOEXEC.BAT file is present to execute the AUTOEXEC.BAT statements.
When an AUTOEXEC.BAT file is present COMMAND.COM (which has now control of the system) will loadand execute the batch file. After batch file processing is complete, the DOS Prompt will appear.
If no AUTOEXEC.BAT file is tint he root directory, COMMAND.COM will request the current DATE and TIME, and then show the dos prompt.
Now applications can be launched by using OS command.AUTOEXEC.BAT may also call a shell or start an application.

More on the Boot loader

A sector has a length of 512 bytes. It becomes a boot sector because of its location and the hex value 0xaa55 in the final two byte. The BIOS look this value when scanning a potential boot media. Generally the hard disk has its boot sector in its first sector, which is called master boot record (MBR).

There is a primary boot loader as well as a secondary boot loader.
Boot loaders may face peculiar constraints, especially in size, for instance, on the IBM PC, and compatible the first stage of the boot loaders must fit into the first 446 bytes of the MBR in order to leave room for the 64-byte partition table and two byte AA55h 'signature' which the BIOS requires for a proper boot loader.
In the second stage, boot loader load the operating system. Second stage loader include LILO( LInux LOader), GRUB (GRand Unified Bootloader),(these two are used on linux extensively); NTLDR (Windows); BSD Bootloader etc.

A Look at MS-DOS Operating System


An operating system is a resource allocator. It provides an environment for various other application software. These tasks are accomplished in the MS-DOS with three files:

  • IO.SYS
  • MSDOS.SYS
  • COMMAND.COM

IO.SYS

The IO.SYS provides various low-level routines(drivers) that interact with BIOS.
In addition to the drivers it contains a system initialization routine.
The whole content of the file (except for the system initialization routine) are kept in low memory throughout system operation.
In order for a disk to be bootable under MS-DOS (earlier versions 3.x, 4.x), IO.SYS must be the first file in the disk directory, and it must occupy at least the first available cluster on the disk. This is the disk's OS volume boot sector(VBR). However later versions eliminate this requirement.
First during the boot the IO.SYS is loaded to the memory and the control is passed to it. The the boot continues.

MSDOS.SYS

The MSDOS.SYS is listed second in the boot disk's directory and is the second file to be loaded into the memory (low-level memory and resides there throughout the system's operation) during the boot process.
It contains routines that handles OS disk and file access.

COMMAND.COM

The COMMAND.COM file serves as the MS-DOS shell and command processor.
This is the program that one interacts with at the command-line prompt.
COMMAND.COM is the third file that loads when the computer boots.
Its stored in the low level memory along with IO.SYS and MSDOS.SYS.
MS-DOS uses two types of commands in normal operation:

  • resident
  • transient

Resident commands are also called internal commands, are procedures that are coded directly into the COMMAND.COM.
So resident commands execute immediately when called from the command line.
eg: CLS, DIR etc.

Transient commands are also called external commands represent more broader and powerful group of commands.
Transient commands are not loaded with COMMAND.COM.
Instead, the commands are available as small COM or EXE file.
eg: DEBUG
Transient command must be loaded from the disk each time they are executed.
Separating the complex commands from the COMMAND.COM reduces the size.

Bus Arbitration


Deciding which bus master gets to use the bus next is called bus arbitration. There are a wide variety of schemes for bus arbitration; these may involve special hardware or extremely sophisticated bus protocols.In a bus arbitration scheme, a device (or the processor) wanting to use the bus signals a bus request and the later granted the bus. After a grant the device an use the bus, later signaling to arbiter that the bus is no longer required. The arbiter can grant the bus to another device. Most multiple bus masters have a set of bus lines for performing request or grants. A bus release line is also needed if each device does not have its own request lines. Some times the signals used for the bus arbitration have physically separate lines, while in other systems the data line of the bus are used for this function (though this prevents overlapping of arbitration with transfer).

Arbitration scheme usually try to balance two factors in choosing which device to grant the bus. First ,each device has a bus priority, and the highest priority device must be serviced first. Second we should prefer that any device,
even one with low priority, never be completely locked out from the bus. This property called fairness ensures that every device that wants to use the bus is guaranteed to get it eventually. In addition to these factors more sophisticated schemes aim to reduce the time needed to arbitrate the bus.
Because arbitration time is overhead, which increases the bus time access, it should be reduced and overlapped with the bus transfers whenever possible.

Bus arbitration schemes can be divided into four broad classes:

  • Daisy chain arbitration
In this scheme the bus grant line is run through the device from highest priority to lowest priority (the priorities are determined by the position on the bus). A high priority device that desires bus access simply intercepts the bus grant signal, not allowing the lower priority device to see the signal.
Advantage: Simplicity.
Disadvantage: It cannot assure fairness- allow priority request may be locked out indefinitely-and the use of the daisy chain grant signal also limit the bus speed.

  • Centralized, parallel arbitration
These schemes use multiple request lines, and the device independently requests the bus. A centralized arbiter chooses from among the devices requesting bus access and notifies the selected device that it is now bus master.
Disadvantage: It requires a central arbiter, which may become the bottleneck for bus usage.
PCI, a standard backplane bus, uses a central arbitration scheme.

  • Distributed arbitration by self selection
These schemes also use multiple request lines, but the devices requesting bus access determine who will be granted access. Each device wanting bus access places a code indicating its identity on the bus. By examining the bus, the devices can determine highest priority device that has made a request. There is no need for a central arbiter; each device determines independently whether it is the high priority requester. This scheme however does require more lines for request signals.
The NuBus, which is the backplane bus in Apple Macintosh II, use this scheme.


  • Distributed arbitration by collision detection
In this scheme each device independently request the bus. Multiple simultaneous request results in a collision. The collision is detected and the scheme for selecting among the colliding parties is used.
Ethernet use this scheme.

Saturday, September 6, 2008

Hard disk size measurement

Manufacturers and the operating system measures hard disk capacity in a different way. That is the binary gigabyte and the decimal gigabyte.When we install a new 160 GB(manufacturer measurement) hard drive we see (operating system reports) only 149 GB.Similarly if we install a 4GB hard disk we get 3.72GB.

This difference is because of the way in which manufacturers and software developers calculate the drive capacity.Technically, hard drive capacity is calculated by multiplying the number of cylinders, sectors, and heads time 512.

Capacity = Cylinders * Heads * Sectors * 512 (bytes per sector)

So a typical drive with 7752 cylinders, 16 heads and 36 sectors, we get:
7752 * 16 * 63 * 512 = 4,000,776,192 bytes.

Hard disk manufacturers use decimal megabyte to determine the size of their hard drive.
i.e. 4,000,776,197 bytes / 1,000,000,000 = 4.0 GB

Whereas, software makers use binary megabytes to calculate the drive size.A binary gigabyte is 1,073,741,824 bytes and a binary megabyte is 1,048,576 bytes.
So we get: 4,000,776,192 bytes / 1,073,741,824 = 3.72