Thursday, 23 February 2012

Default x86 Port Address Assignments



Default x86 Port Address Assignments
Address RangeFirst QWORDSecond QWORDThird QWORDFourth QWORD
0x000-0x00FDMA Controller Channels 0-3
0x010-0x01FSystem Use
0x020-0x02FInterrupt Controller 1System Use
0x030-0x03FSystem Use
0x040-0x04FSystem TimersSystem Use
0x050-0x05FSystem Use
0x060-0x06FKeyboard/PS2 Moude (Port 0x60)
Speaker (0x61)
Keyboard/PS2 Mouse (0x64)System Use
0x070-0x07FRTC/CMOS/NMI (0x70, 0x71)DMA Controller Channels 0-3
0x080-0x08FDMA Page Register 0-2 (0x81 - 0x83)DMA Page Register 3 (0x87)DMA Page Register 4-6 (0x89-0x8B)DMA Page Register 7 (0x8F)
0x090-0x09FSystem Use
0x0A0-0x0AFInterrupt Controller 2 (0xA0-0xA1)System Use
0x0B0-0x0BFSystem Use
0x0C0-0x0CFDMA Controller Channels 4-7 (0x0C0-0x0DF), bytes 1-16
0x0D0-0x0DFDMA Controller Channels 4-7 (0x0C0-0x0DF), bytes 16-32
0x0E0-0x0EFSystem Use
0x0F0-0x0FFFloating Point Unit (FPU/NPU/Mah Copprocessor)
0x100-0x10FSystem Use
0x110-0x11FSystem Use
0x120-0x12FSystem Use
0x130-0x13FSCSI Host Adapter (0x130-0x14F), bytes 1-16
0x140-0x14FSCSI Host Adapter (0x130-0x14F), bytes 17-32SCSI Host Adapter (0x140-0x15F), bytes 1-16
0x150-0x15FSCSI Host Adapter (0x140-0x15F), bytes 17-32
0x160-0x16FSystem UseQuaternary IDE Controller, master slave
0x170-0x17FSecondary IDE Controller, Master driveSystem Use
0x180-0x18FSystem Use
0x190-0x19FSystem Use
0x1A0-0x1AFSystem Use
0x1B0-0x1BFSystem Use
0x1C0-0x1CFSystem Use
0x1D0-0x1DFSystem Use
0x1E0-0x1EFSystem UseTertiary IDE Controller, master slave
0x1F0-0x1FFPrimary IDE Controller, master slaveSystem Use
0x200-0x20FJoystick PortSystem Use
0x210-0x21FSystem Use
0x220-0x22F
Sound Card
Non-NE2000 Network CardSystem Use
0x230-0x23FSCSI Host Adapter (0x220-0x23F), bytes 17-32)
0x240-0x24F
Sound Card
Non-NE2000 Network CardSystem Use
NE2000 Network Card (0x240-0x25F) Bytes 1-16
0x250-0x25FNE2000 Network Card (0x240-0x25F) Bytes 17-32
0x260-0x26F
Sound Card
Non-NE2000 Network CardSystem Use
NE2000 Network Card (0x240-0x27F) Bytes 1-16
0x270-0x27F
System UsePlug and Play System DevicesLPT2 - Second Parallel Port
System UseLPT3 - Third Parallel Port (Monochrome Systems)
NE2000 Network Card (0x260-0x27F) Bytes 17-32
0x280-0x28F
Sound Card
Non NE2000 Network CardSystem Use
NE2000 Network Card (0x280-0x29F) Bytes 1-16
0x290-0x29FNE2000 Network Card (0x280-0x29F) Bytes 17-32
0x2A0-0x2AF
Non NE2000 Network CardSystem Use
NE2000 Network Card (0x280-0x29F) Bytes 1-16
0x2B0-0x2BFNE2000 Network Card (0x280-0x29F) Bytes 17-32
0x2C0-0x2CFSystem Use
0x2D0-0x2DFSystem Use
0x2E0-0x2EFSystem UseCOM4 - Fourth Serial Port
0x2F0-0x2FFSystem UseCOM2 - Second Serial Port
0x300-0x30F
Sound Card / MIDI PortSystem Use
Non NE2000 Network CardSystem Use
NE2000 Network Card (0x300-0x31F) Bytes 1-16
0x310-0x31FNE2000 Network Card (0x300-0x32F) Bytes 17-32
0x320-0x32F
Sound Card / MIDI Port (0x330, 0x331)System Use
NE2000 Network Card (0x300-0x31F) Bytes 17-32
SCSI Host Adapter (0x330-0x34F) Bytes 1-16
0x330-0x33F
Sound Card / MIDI PortSystem Use
Non NE2000 Network CardSystem Use
NE2000 Network Card (0x300-0x31F) Bytes 1-16
0x340-0x34F
SCSI Host Adapter (0x330-0x34F) Bytes 17-32
SCSI Host Adapter (0x340-0x35F) Bytes 1-16
Non NE2000 Network CardSystem Use
NE2000 Network Card (0x340-0x35F) Bytes 1-16
0x350-0x35F
SCSI Host Adapter (0x340-0x35F) Bytes 17-32
NE2000 Network Card (0x300-0x31F) Bytes 1-16
0x360-0x36F
Tape Accelerator Card (0x360)System UseQuaternary IDE Controller (Slave Drive)(0x36E-0x36F)
Non NE2000 Network CardSystem Use
NE2000 Network Card (0x300-0x31F) Bytes 1-16
0x370-0x37F
Tape Accelerator Card (0x370)Secondary IDE Controller (Slave Drive)LPT1 - First Parallel Port (Color systems)
System UseLPT2 - Second Parallel Port (Monochrome Systems)
NE2000 Network Card (0x360-0x37F) Bytes 1-16
0x380-0x38FSystem UseSound Card (FM Synthesizer)System Use
0x390-0x39FSystem Use
0x3A0-0x3AFSystem Use
0x3B0-0x3BFVGA/Monochrome VideoLPT1 - First Parallel Port (Monochrome Systems)
0x3C0-0x3CFVGA/CGA Video
0x3D0-0x3DFVGA/CGA Video
0x3E0-0x3EF
Tape Accelerator Card (0x370)System UseCOM3 - Third Serial Port
System UseTertiary IDE Controller (Slave Drive)(0x3EE-0x3EF)
0x3F0-0x3FF
Floppy Disk ControllerCOM1 - First Serial Port
Tape Accelerator Card (0x3F0)Primary IDE Controller (Slave Drive)(0x3F6-0x3F7)System Use

Tuesday, 21 February 2012

HeLLo World Operating System Part 2

                                                  Bootloader
So finally in this section we are going to build a small bootloader that will say helloworld on console.

When a computer  starts up ( obviously by pressing the power button), the first thing that occurs is it  send a signal to motherboard which in turn starts the power supply. After supplying the correct amount of power to each device, it send a signal called "Power OK" to BIOS which resides on motherboard.
Once the BIOS receive the "Power OK" signal, it starts the booting process by first initializing a process called POST (Power On Self Test). POST first check that every device has right amount of power and then it check whether the memory is not corrupted. Then it initialize each devices and finally it gives control to BIOS for further booting.
Now the final process of booting begins. For this the BIOS first find 512 bytes of image called MBR (Master Boot Record) or Bootsector from the floppy disk or hard disk which is used for booting. The priority of boot devices is set by the user in BIOS setting. The normal priority is floppy disk first, then hard disk.
Once BIOS finds the bootsector it loads the image in memory and execute it. If a valid bootsector is not found, BIOS check for next drive in boot sequence until it find valid bootsector. If BIOS fails to get valid bootsector, generally it stops the execution and gives an error message "Disk boot failure".
It is bootsectors responsibility to load the operating system in memory and execute it.


bootloader

Master Boot Record

A device is "bootable" if it carries a boot sector with the byte sequence 0x55, 0xAA{signature} in bytes 511 and 512 respectively. When the BIOS finds such a boot sector, it is loaded into memory at a specific location; this is usually 0x0000:0x7c00 (segment 0, address 0x7c00). However, some BIOS' load to 0x7c0:0x0000 (segment 0x07c0, offset 0), which resolves to the same physical address, but can be surprising.

On a hard drive, the so-called Master Boot Record (MBR) holds executable code at offset 0x0000 - 0x01bd, followed by table entries for the four primary partitions, using sixteen bytes per entry (0x01be - 0x01fd), and the two-byte signature (0x01fe - 0x01ff).
The layout of the table entries is as follows:
Offset Size (bytes) Description
0x00 1 Boot Indicator (0x80=bootable, 0x00=not bootable)
0x01 1 Starting Head Number
0x02 2 Starting Cylinder Number (10 bits) and Sector (6 bits)
0x04 1 Descriptor (Type of partition/filesystem)
0x05 1 Ending Head Number
0x06 2 Ending Cylinder and Sector numbers
0x08 4 Starting Sector (relative to begining of disk)
0x0C 4 Number of Sectors in partition 







bootloader.asm

[BITS 16]             ;Tells the assembler that its a 16 bit code
[ORG 0x7C00]     ;Origin, tell the assembler that where the code will
    ;be in memory after it is been loaded

MOV SI,String            ;Store string pointer to SI
CALL PrintString     ;Call print string procedure
JMP $             ;Infinite loop, hang it here.


PrintCharacter:            ;Procedure to print character on screen
                          ;Assume that ASCII value is in register AL
MOV AH, 0x0E    ;Tell BIOS that we need to print one charater on screen.
MOV BH, 0x00   ;Page no.
MOV BL, 0x07    ;Text attribute 0x07 is lightgrey font on black background

INT 0x10                 ;Call video interrupt
RET                 ;Return to calling procedure



PrintString:        ;Procedure to print string on screen
                       ;Assume that string starting pointer is in register SI

next_character: ;Lable to fetch next character from string
MOV AL, [SI] ;Get a byte from string and store in AL register
INC SI          ;Increment SI pointer
OR AL, AL         ;Check if value in AL is zero (end of string)
JZ exit_function       ;If end then return
CALL PrintCharacter  ;Else print the character which is in AL register
JMP next_character  ;Fetch next character from string
exit_function:                   ;End label
RET                            ;Return from procedure


;Data
String db 'Hello World', 0 ;HelloWorld string ending with 0

TIMES 510 - ($ - $$) db 0 ;Fill the rest of sector with 0
DW 0xAA55 ;Add boot signature at the end of bootloader

[BITS 16]: Our code starts with [BITS 16] which is an assembler directive. This will tell assembler that our code is a 16 bit code.
[ORG 0x7C00]: Then we have used [ORG 0x7C00] which tell assembler to assemble the instructions from Origin 0x7C00. BIOS loads bootloader at physical address 0x7C00 hence we have assemble our bootloader starting from that location.
JMP $: JMP at location $ means jumping to the same location. Thus this nothing but an infinite loop. We just want to hang our code here.
TIMES 510 - ($ - $$) db 0: As bootloader is always of length 512 bytes, our code does not fit in this size as its small. We need to use rest of memory and hence we clear it out using TIMES directive. $ stands for start of instruction and $$ stands for start of program. Thus ($ - $$) means length of our code.
DW 0xAA55: This is boot signature. This tells the BIOS that this is a valid bootloader. If bios does not get 0x55 and 0xAA at the end of the bootloader than it will treat bootloader as invalid. Thus we provide this two bytes at the end of our bootloader.

Save the above file and run following command if you are on linux machine:

1:nasm bootloader.asm -f bin -o boot.bin
2:sudo dd if=boot.bin bs=512 of=/dev/fd0
3:sudo qemu -fda /dev/fd0



well in following code
MOV AH, 0x0E
MOV BH, 0x00
MOV BL, 0x07
predefined interrupt table is used like OEh is used to print character on tty mode.

I am also providing interrupt table so you can easily refer to it from here:


Interrupt table

A list of common BIOS interrupts can be found below. Note that some BIOSes (particularly old ones) will not support all of these interrupts.
Interrupt vector Description
​00h​ CPU: Executed after an attempt to divide by zero or when the quotient does not fit in the destination
​01h​ CPU: Executed after every instruction while the trace flag is set
​02h​ CPU: NMI, used e.g. by POST for memory errors
​03h​ CPU: The lowest non-reserved interrupt, it is used exclusively for debugging, and the ​INT 03​ handler is always implemented by a debugging program
​04h​ CPU: Numeric Overflow. Usually caused by the ​INTO​ instruction when the overflow flag is set.
​05h​ Executed when Shift-Print screen is pressed, as well as when the ​BOUND​ instruction detects a bound failure.
​06h​ CPU: Called when the Undefined Opcode (invalid instruction) exception occurs. Usually installed by the operating system.
​07h​ CPU: Called when an attempt was made to execute a floating-point instruction and no numeric coprocessor was available.
​08h​ IRQ0: Implemented by the system timing component; called 18.2 times per second (once every 55 ms) by the PIC
​09h​ IRQ1: Called after every key press and release (as well as during the time when a key is being held)
​0Bh​ IRQ3: Called by serial ports 2 and 4 (COM2/4) when in need of attention
​0Ch​ IRQ4: Called by serial ports 1 and 3 (COM1/3) when in need of attention
​0Dh​ IRQ5: Called by hard disk controller (PC/XT) or 2nd parallel port LPT2 (AT) when in need of attention
​0Eh​ IRQ6: Called by floppy disk controller when in need of attention
​0Fh​ IRQ7: Called by 1st parallel port LPT1 (printer) when in need of attention
​10h​ Video Services - installed by the BIOS or operating system; called by software programs
​AH​ Description
​00h​ Set Video Mode
​01h​ Set Cursor Shape
​02h​ Set Cursor Position
​03h​ Get Cursor Position And Shape
​04h​ Get Light Pen Position
​05h​ Set Display Page
​06h​ Clear/Scroll Screen Up
​07h​ Clear/Scroll Screen Down
​08h​ Read Character and Attribute at Cursor
​09h​ Write Character and Attribute at Cursor
​0Ah​ Write Character at Cursor
​0Bh​ Set Border Color
​0Ch​ Write Graphics Pixel
​0Dh​ Read Graphics Pixel
​0Eh​ Write Character in TTY Mode
​0Fh​ Get Video Mode
​13h​ Write String
​11h​ Installed by the BIOS; returns equipment list
​12h​ Installed by the BIOS or operating system; returns Conventional Memory Size
​13h​ Low Level Disk Services; installed by the BIOS or operating system; called by software programs
​AH​ Description
​00h​ Reset Disk Drives
​01h​ Check Drive Status
​02h​ Read Sectors From Drive
​03h​ Write Sectors To Drive
​04h​ Verify Sectors On Drive
​05h​ Format Track On Drive
​08h​ Get Drive Parameters
​09h​ Init Fixed Drive Parameters
​0Ch​ Seek To Specified Track
​0Dh​ Reset Fixed Disk Controller
​15h​ Get Drive Type
​16h​ Get Floppy Drive Media Change Status
​14h​ Routines for communicating via the serial port. Used by software programs.
​AH​ Description
​00h​ Serial Port Initialization
​01h​ Transmit Character
​02h​ Receive Character
​03h​ Status
​15h​ Miscellaneous (System services support routines)
​AH​ ​AL​ ​AX​ Description
​00h​ Turn on cassette drive motor
​01h​ Turn off cassette drive motor
​02h​ Read data blocks from cassette
​03h​ Write data blocks to cassette
​4Fh​ Keyboard Intercept
​83h​ Event Wait
​84h​ Read Joystick
​85h​ Sysreq Key Callout
​86h​ Wait
​87h​ Move Block
​88h​ Get Extended Memory Size
​C0h​ Get System Parameters
​C1h​ Get Extended BIOS Data Area Segment
​C2h​ Pointing Device Functions
​E8h​ ​01h​ ​E801h​ Get Extended Memory Size (Newer function, since 1994). Gives results for memory size above 64 Mb.
​E8h​ ​20h​ ​E820h​ Query System Address Map. The information returned from e820 supersedes what is returned from the older AX=E801h and AH=88h interfaces.
​16h​ Implemented by the BIOS or operating system. Provides routines to be called by software programs which communicate with the keyboard.
​AH​ Description
​00h​ Read Character
​01h​ Read Input Status
​02h​ Read Keyboard Shift Status
​10h​ Read Character Extended
​11h​ Read Input Status Extended
​12h​ Read Keyboard Shift Status Extended
​17h​ Print Services - used by software programs to communicate with the printer
​AH​ Description
​00h​ Print Character to Printer
​01h​ Initialize Printer
​02h​ Check Printer Status
​18h​ Execute Cassette BASIC: True IBM computers contain BASIC in the ROM to be interpreted and executed by this routine in the event of a boot failure (called by the BIOS)
​19h​ After POST this interrupt is used by BIOS to load the operating system.
​1Ah​ Real Time Clock Services - called by software programs to communicate with the RTC
​AH​ Description
​00h​ Read RTC
​01h​ Set RTC
​02h​ Read RTC Time
​03h​ Set RTC Time
​04h​ Read RTC Date
​05h​ Set RTC Date
​06h​ Set RTC Alarm
​07h​ Reset RTC Alarm
​1Bh​ Installed by the operating system; automatically called by ​INT 9​ when Ctrl-Break has been pressed
​1Ch​ Called automatically by ​INT 08​; available for use by software programs when a routine needs to be executed regularly
​1Dh​ Not to be called; simply a pointer to the VPT (Video Parameter Table), which contains data on video modes
​1Eh​ Not to be called; simply a pointer to the DPT (Diskette Parameter Table), containing a variety of information concerning the diskette drives
​1Fh​ Not to be called; simply a pointer to the VGCT (Video Graphics Character Table), which contains the data for ASCII characters ​80h​ to ​FFh​
​41h​ Address pointer: FDPT = Fixed Disk Parameter Table (1st hard drive)
​46h​ Address pointer: FDPT = Fixed Disk Parameter Table (2nd hard drive)
​4Ah​ Called by RTC for alarm
​70h​ IRQ8: Called by RTC
​74h​ IRQ12: Called by mouse
​75h​ IRQ13: Called by math coprocessor
​76h​ IRQ14: Called by primary IDE controller
​77h​ IRQ15: Called by secondary IDE controller





                                                                                              

Sunday, 19 February 2012

Hello World Operating System

So lets start making our little operating system and as tradition first we will make a hello world operating system.So things you should known before making operating system like what tool you gonna use,language,most important actually understanding what you are dealing with.."concepts".


So what is Operating System???
An operating system (OS) is a set of programs that manage computer hardware resources and provide common services for application software.
                                                         

So actually we are going to build programs which will deals with  hardware  and make it work for us in an efficient way.Its about learning hardware language ,because to give instruction to someone we have do it in his/her language .We will create a communication channel through which different hardware will talk and produce output in a effective way.Well you might be getting confused but dont worry things will get crystal clear as we move ahead.So it takes a long study of different manual of hardware manufacturer to understand the basics language of a hardware like port no.,pin,interrupt no. etc.
But we will not go in detail here right now.well i 'll build os around intel x86 architecture as it is supported by most of the hardware and software community. 
For hardware functions such as input and output and memory allocation, the operating system acts as an intermediary between application programs and the computer hardware, although the application code is usually executed directly by the hardware and will frequently call the OS or be interrupted by it. Operating systems are found on almost any device that contains a computer—from cellular phones and video game consoles to supercomputers and web servers.
So before starting i must warn you its not easy ...its not for beginners.Writing an operating system is a bit harder than writing a conventional program, by its nature: you're actually writing the code that works the machine and runs other people's code. The lack of debuggers, libraries, and standard code makes it the black art that it has become. You can't debug using traditional methods because you're writing at a lower level than the debugger itself (even the debugger calls the operating system).

So tool that we gonna need to make our own os:
1:Bochs or qemu : well qemu is my favorite.
2:dd
Or to make it easy to say we gonna need a linux machine up and working with qemu or bochs installed on it.

Thats it.. now on next post i 'll be explaining how to make our very first hello world operating system....
So stay tuned...