Selasa, 25 Januari 2011

Logical Block Addressing And Cylinder Head Sector

Logical Block Addressing (LBA)
Logical block addressing (LBA) is a common scheme used for specifying the location of blocks of data stored on computer storage devices, generally secondary storage systems such as hard disks.

Cylinder-head-sector
Cylinder-head-sector, also known as CHS, was an early method for giving addresses to each physical block of data on a hard disk drive. In the case offloppy drives, for which the same exact diskette medium can be truly low-level formatted to different capacities, this is still true.


If you look at most sites they will give you this set of formulas:
Sector = (LBA mod SectorsPerTrack)+1
Cylinder = (LBA/SectorsPerTrack)/NumHeads
Head = (LBA/SectorsPerTrack) mod NumHeads

Simple Assembly example:

(This is not likely to work if you cut and paste it and is here to show the principle only, look at the next section for a complete example)
LBACHS:
; Set up the registers ready for the divide
MOV ax, [LBAvalue] ; []'s means value at memory location LBAvalue.
; Make the divide
DIV [SectorsPerTrack] ; Carry out the division of ax.
; Put the returned Number Of Tracks some where
MOV [NumTracks], ax   ; Put the quotient into a memory variable
; Sort out the sector value
INC dx        ; Add 1 to the remainder
MOV [Sector], dx     ; Put the remainder into a memory variable

; Set up the registers ready for the divide
MOV ax, [NumTracks]    ; Put the number of tracks in to ax
; Make the divide
DIV [NumHeads]  ; Divide NumTracks (ax) by NumHeads
; Stash the results in some memory locations
MOV [Cylinder], ax     ; Quotient value, the Number of heads to be moved from ax
MOV [Head], dx        ; Remainder value, the cylinder value to be oved from dx

Jumat, 21 Januari 2011

FAT 32 Structure


Master Boot Record
As explain before ,The Master Boot Record is the same for pretty much all Operating Systems. It is located on the first Sector of the Hard Drive, at Cylinder 0, Head 0, Sector 1. It also contains the partition table, which defines the different sections of your hard drive. Basically if anything happens to this little 512 byte section, your hard drive is brain dead. In this article I will explain about FAT 32 structure that we use to make a 32 bit FAT 32 boot loader.

FAT32 Boot Record
This information is located in the first sector of every partition.

OffsetDescriptionSize
00hJump Code + NOP3 Bytes
03hOEM Name 8 Bytes
0BhBytes Per Sector1 Word
0DhSectors Per Cluster1 Byte
0EhReserved Sectors1 Word
10hNumber of Copies of FAT1 Byte
11hMaximum Root DirectoryEntries (N/A for FAT32)1 Word
13hNumber of Sectors inPartition Smaller than 32MB (N/A for FAT32)1 Word
15hMedia Descriptor (F8h forHard Disks)1 Byte
16hSectors Per FAT in Older FAT Systems (N/A for FAT32)1 Word
18hSectors Per Track1 Word
1AhNumber of Heads1 Word
1ChNumber of Hidden Sectors in Partition1 Double Word
20hNumber of Sectors in Partition1 Double Word
24hNumber of Sectors Per FAT1 Double Word
28hFlags (Bits 0-4 IndicateActive FAT Copy) (Bit 7 Indicates whether FAT Mirroringis Enabled or Disabled <Clear is Enabled>) (If FAT Mirroring is Disabled, the FAT Information is only written to the copy indicated by bits 0-4)1 Word
2AhVersion of FAT32 Drive (HighByte = Major Version, Low Byte = Minor Version)1 Word
2ChCluster Number of the Start of the Root Directory1 Double Word
30hSector Number of the File System Information Sector (See Structure Below)(Referenced from the Start of the Partition)1 Word
32hSector Number of the Backup Boot Sector (Referenced from the Start of the Partition)1 Word
34hReserved12 Bytes
40hLogical Drive Number of Partition1 Byte
41hUnused (Could be High Byte of Previous Entry)1 Byte
42hExtended Signature (29h)1 Byte
43hSerial Number of Partition1 Double Word
47hVolume Name of Partition11 Bytes
52hFAT Name (FAT32)8 Bytes
5AhExecutable Code420 Bytes
1FEhBoot Record Signature (55hAAh)2 Bytes

File System Information Sector
Usually this is the Second Sector of the partition, although since there is a reference in the Boot Sector to it, I'm assuming it can be moved around. I never got a complete picture of this one. Although I do know where the important fields are at.

OffsetDescriptionSize
00hFirst Signature (52h 52h 61h41h)1 Double Word
04hUnknown, Currently (Might just be Null)480 Bytes
1E4hSignature of FS Info Sector (72h 72h 41h 61h)1 Double Word
1E8hNumber of Free Clusters (Set to -1 if Unknown)1 Double Word
1EChCluster Number of Cluster that was Most Recently Allocated.1 Double Word
1F0hReserved12 Bytes
1FChUnknown or Null2 Bytes
1FEhBoot Record Signature (55hAAh)2 Bytes

FAT32 Drive Layout 
FAT 32 Drive layout is use when you need to load your kernel or second stage boot loader. FAT 32 Drive lay out makes us know where the kernel located. First you need to load FAT to memory so the first thing we do is compute the sector where FAT located. Second thing you do is to load root directory to memory and we must to compute sector of root directory. From the FAT and root directory you can locate the kernel and load it to memory.
OffsetDescription
Start of PartitionBoot Sector
Start + # of ReservedSectorsFat Tables
Start + # of Reserved + (#of Sectors Per FAT * 2) <Assuming that FAT Mirroring is Enabled, I personally haven't seen a case where it wasn't, but I guess there is always the possibility>Data Area (Starts with Cluster #2)


Cluster Meaning
A Cluster is a Group of Sectors on the Hard Drive that have information in them. A 4K Cluster has 8 Sectors in it (512*8=4096). Each Cluster is given a spot in the FAT Table. When you look at an Entry in the FAT, the number there tells you whether or not that cluster has data in it, and if so, if it is the end of the data or there is another cluster after it. All Data on a Partition starts with Cluster #2. If the FAT Entry is 0, then there is no data in that cluster. If the FAT Entry is 0FFFFFFFh, then it is the last entry in the chain.
This is one of my biggest holes in my information. I am unable to find anyplace that shows what numbers mean what when it comes to the FAT table. I was able to tell the end of the chain just by looking at a FAT32 Drive, but I don't know what stands for a BAD Cluster or what the maximum valid number for showing data is.
For now, you can calculate the maximum valid cluster in a partition with this formula:
( (# of Sectors in Partition) - (# of Sectors per Fat * 2) - (# of Reserved Sectors) ) / (# of Sectors per Cluster)
If there is any remainder in the answer to that formula, it just means that there were a few extra clusters at the end of the partition (probably not enough to make another cluster), so you can just get rid of anything after the decimal point.

Directory Table
Another aspect when looking at a File System at Low Level is the Directory Table. The Directory Table is what stores all of the File and Directory Entries. Basically there is only one difference between the Directory Table of FAT16 and FAT32. The Difference is : the Reserved OS/2 Byte (Offset 20 [14h]) in the Short Filename Structure is replaced with the High Word of the Cluster Number (since it's now 4 bytes instead of 2).

This is the example of my FAT 32 BPB on 2 GB flash disk. I don't develop the boot code yet because is very hard to debug it.

org 7c00h
jmp start
nop
bs_OEM db "MSDOS5.0"

    A_BF_BPB_BytesPerSector       DW    200h
    A_BF_BPB_SectorsPerCluster    DB    8h
    A_BF_BPB_ReservedSectors      DW    01f0h
    A_BF_BPB_NumberOfFATs         DB    02
    A_BF_BPB_RootEntries          DW    0
    A_BF_BPB_TotalSectors         DW    0
    A_BF_BPB_MediaDescriptor      DB    0f8h
    A_BF_BPB_SectorsPerFAT        DW    0
    A_BF_BPB_SectorsPerTrack      DW    03fh
    A_BF_BPB_Heads                DW    0ffh
    A_BF_BPB_HiddenSectors        DW    080h
    A_BF_BPB_HiddenSectorsHigh    DW    0h
    A_BF_BPB_BigTotalSectors      DW    3f00h
    A_BF_BPB_BigTotalSectorsHigh  DW    03ch
    A_BF_BPB_BigSectorsPerFat     DW    0f08h
    A_BF_BPB_BigSectorsPerFatHi   DW    0h
    A_BF_BPB_ExtFlags             DW    0
    A_BF_BPB_FS_Version           DW    0
    A_BF_BPB_RootDirStrtClus      DW    2
    A_BF_BPB_RootDirStrtClusHi    DW    0
    A_BF_BPB_FSInfoSec            DW    1h
    A_BF_BPB_BkUpBootSec          DW    6h
    A_BF_BPB_Reserved             DW    6 DUP (?)

BS_physical db 80h
db 00
BS_bootrecord db 29h
volumeid dd 0AE6DA6B2h
volumelabel db "No Name    "
filesystem db "FAT32   "

source http://www.easeus.com/resource/fat32-disk-structure.htm




Kamis, 20 Januari 2011

The Boot Loader

Booting is a bootstraping process that starts operating system when user turns on a computer system. A boot loader, also called a boot manager, is a small program that places the operating system (OS) of a computer into memory. When a computer is powered-up or restarted, the basic input/output system (BIOS) performs some initial tests, and then transfers control to the master boot record (MBR) where the boot loader resides The boot loader typically loads the main operating system for the computer. The boot loader is on sector 0 head 0 cylinder 0 on the boot drive. Once computer turns on, it loads the boot loader into memory 0000:7c00.
Example of boot loader write in NASM:


; Start matter
[BITS 16]   ; Tells the compiler to make this into 16bit code generation
    ;  code
[ORG 0x7C00]   ; Origin, tells the compiler where the code is going to be
    ;  in memory after it has been loaded. (hex number)

; End matter
times 510-($-$$) db 0 ; Fill the rest of the sector with zero's 


dw 0xAA55 ; Add the boot loader signature to the end 

The size of boot loader must be 512 bytes and end with 0xaa55 bytes. For my operating system I design 32 bit FAT boot loader. The structur of boot loader must be:
1. jump command, NOP command, and OEM name
2. BPB FAT32 
3. Boot code

In computing, the BIOS parameter block, often shortened to BPB, is a data structure in the Volume Boot Record describing the physical layout of a data storage volume. On partitioned devices, such as hard disks, the BPB describes the volume partition, whereas, on unpartitioned devices, such as floppy disks, it describes the entire medium. A basic BPB can appear and be used on any partition, including floppy disks where its presence is often necessary, however, certain filesystems also make use of it in describing basic filesystem structures. Filesystems making use of a BIOS parameter block include FAT16, FAT32HPFS, and NTFS. Due to different types of fields and the amount of data they contain, the length of the BPB is different for FAT16, FAT32, and NTFS boot sectors.

The boot code contain how boot loader determine FAT and Root Directory and load the kernel to the memory. This is my design, I'll give you detail explanation after my boot loader is complete

Senin, 17 Januari 2011

Operating System Glossary

Glossary


Here you find the term explanation supplementing to the most important terms related with operating systems. 


ACPI Advanced Configuration Power Interface stands for energy functions to make it at example possible to connect or remove a laptop computer to the docking station without restart. The OnNow energy saving function can shift an ACPI compatible computer to sleep mode and back to normal mode. Suspend to disk is another mode which awakes the computer from the sleep mode in short time to recover the last running operating system state. Modern mainboards supports the power on of the computer about keyboard input and other interfaces in the BIOS. 

The API, Application Program Interface serves as standardized programming interface between operating systems and programs. Example is the Win32c API in Windows 95 and Windows 98 SE which is partly also compatible with 32-bit Windows programs of Windows NT. 

AppleTalk is the standard network protocol of Macintosh computers for the common access to files and printers. 

BASIC The Beginner's All-purpose Symbolic Instruction Code is a simple programming language for realtime interpreted programs by the BASIC interpreter. 

The Desktop is the essential component of a graphical user interface of a operating position for the function as work desk. The user has comparatively as well access to his documents and tools like on a real work desk. 

GUI The Graphical User Interface was invented as method by research and publications of Dr. V. Bush in 1945 and described as simply access to informations with a graphical interface on computers. At that time, the design model Memex could not be built. With the development of the mouse in the sixties as a pointing equipment for computer it was for the first time possible to use graphical user interfaces efficiently. PARC (Palo Alto Research Centre) started with the first developments of this technique with personal computers at Xerox in the seventies. In the same time period Xerox published the first personal computer with graphical interface and overlaid windows, Xerox Alto was mainly used in education facilities. With the Xerox Star type 8010 the first computer came onto the market with graphical user interface and ethernet connection for the commercial use in 1981. In 1985 the Star technology became transfered into the project Elixir Desktop, a GUI for the PC. Apple used a GUI with the computer system "Lisa" in 1983. The GUI Visi On was released by Visi Corp PC especially for the IBM PC in 1984, Digital Research offered at the same time GEM for Intel/DOS systems. Few time later the X Window System was born at the MIT for UNIX related operating systems 

IPX/SPX (Internetwork Packet eXchange, Sequenced Packet Exchange) is needed for the communication with older Novell NetWare servers up to version 4.x, after that as optional protocol for data transmission. It is a particularly efficient network protocol and designed for smaller networks. 

Multi-Tasking serves for the almost parallel and time limited execute of program parts in an arbitrary number. This happens by the management of the operating system with the grant and revoke of system resources. Inside fast time switches in milli-seconds or with real time operating systems in nano-seconds the access is regulated. 

Computer systems with multi-processors contains at least 2 CPUs. Through this the computer power rises almost proportionally with the number of processors. A part of the performance is needed for the management of the process allocation to coordinate the simultaneous access at shared memory. The operating system is able to distribute the computing load on several processors, named as SMP (symmetrical multi-processing). These processes can be executed parallel at the same time. Condition is the use of especially SMP optimized software. Examples of supporting operating systems are BeOS, Linux and Windows NT/2000 in the corresponding version depending on supported processor number. 

The multi-user ability of a operating system makes it possible to let login and work several users at one computer at the same time. The ressoruces of a multi-user system can be used fully. 

NetBEUI (NetBIOS Extended User Interface) is a network protocol developed by Microsoft which stands out primarily due to his performance for small networks but cannot be routed in networks. 

PDA The Personal Digital Assistent as a small, handy digitally equipment takes over the functions of a memo pad and also for Internet services like WAP and e-mail. Even word processing and other applications are mobile usable. 
POSIX The Portable Operating System for unIX specification serves the uniform use of the API and the development of compatible programs under different operating systems. It made it easier to port and run UNIX applications on different operating systems like Windows NT and others. Also under the different UNIX derivatives a uniform standard was created to protect in-house developments of individually companies. In addition, it guarantees the protection of done investments because applications remain transferably. 

The Protected Mode is supported by all x86 processors since Intel 286. With this mode memory can be addressed up to 4 gbyte. The processing bandwith is 32-Bit, for compatibility reasons 16-Bit programs can be executed furthermore too. For this a virtual engine is required for the use of real mode programs like MS DOS programs under Windows 9 x/NT, which provides a exclusive 16-Bit environment. 

Real mode is an operating mode which was supported before the Intel 286 CPU as the only available mode. It makes the absolute access to working memory and hardware possible. This mode can address up to 1 mbyte RAM, teh processing bandwith is 16-Bit. Example of one real mode operating system is MS-DOS without an Extender. With the Intel 286 CPU the protected mode become introduced, which makes a controlled access to the resources and memory protection possible. 

The TCP/IP (Transmission Control Protocol, Internet Protocol) is the standard network protocol for the internet and large companies. Since 1982 it has replaced the previous protocol NCP (Network Control Protocol). At the moment with version 4 it supports a 32-Bit address range and in the near future with TCP/IP version 6 a bigger address range with 128-Bit address range. 

Thread To accelerate programs on normal computers and multi-processor systems parallizable program code is distributed in so-called Threads, which can be passed on several CPUs or executed with little deferred time on one CPU. Through this the simultaneous write and printing of one or different documents within an application is made possible. 

A Driver is a software which is allocated as software layer between the hardware layer and the application or operating system. Besides the communication between driver software as in the case of a file system encryption these can also be purely virtual. File systems, network protocols, RAM disk and the DOS box are such virtual devices under Windows. As a rule, driver software are primarily developed further with improved efficiency over the time often seen for mainboard chipsets. New characteristics like TwinView for graphics cards or a lower processor load for hard disks operations in the DMA mode are practicable. 
So that an application can access the hardware about the API a call is send to the operating system. After check after permission of the request to the hardware driver the connection to the driver software is made. The direct communication between application and driver software to the hardware is considerably faster than indirect about system calls but carried out without supervisory authority or special protection. About the indirect call to the operating system and interfaces bugs are heavily to trace for the user. Unclear remains most whether the bug was caused itself by the application, the operating system or the driver software. Applications are usable by the uniform driver software interface with considerably lower overhead on a broader hardware basis. The applications has not t oinclude self programmed device drivers to use the hardware equipment. Driver software developers for hardware procducts can just do concentrate her work to the hardware communication under compliance of the operating system to driver software interface communication without the need of special adaptation to single programs. By the different interfaces of driver software for a platform like PowerPC to x86 cannot be ported without adapted source code to another operating systems (like Windows 9x driver for Windows NT), except the source code supports the same mechanism. It remains supplementary to mention that the special customization of applications to single hardware components further is made and definitely makes sense in some areas to reduce the bug vulnerability, obtain the highest possible performance or simply increase the stability. Examples are the optimize of older programs for the Pentium 4 instruction set support or the MacOS with closely developed applications by the same company as Apple. [ Also see Windows device driver models ] 

WDM The Win32 Driver model was designed in 1996 of Microsoft to create a uniform standard for all future Windows driver software. Hardware manufacturers use these interface between application and kernel around to develop compatible driver software for the hardware, graphic card drivers excepted. As advantages have to be called new extensions like plug & play and the energy management for Windows NT 4.0. The uniform driver software for Windows 98 and Windows 2000 as well as a near system imbedding provide better performance and stability. The development overhead is lowered by the standard.