[ jew @ 14.03.2006. 19:06 ] @
BOOT: Code: ;************************************************************************* ;the ultimate boot-strap loader ;to load a file from a DOS FAT12 floppy as the OS ;************************************************************************* [BITS 16] [ORG 0x0000] jmp START OEM_ID db "CHAOS-OS" BytesPerSector dw 0x0200 SectorsPerCluster db 0x01 ReservedSectors dw 0x0001 TotalFATs db 0x02 MaxRootEntries dw 0x00E0 TotalSectorsSmall dw 0x0B40 MediaDescriptor db 0xF0 SectorsPerFAT dw 0x0009 SectorsPerTrack dw 0x0012 NumHeads dw 0x0002 HiddenSectors dd 0x00000000 TotalSectorsLarge dd 0x00000000 DriveNumber db 0x00 Flags db 0x00 Signature db 0x29 VolumeID dd 0xFFFFFFFF VolumeLabel db "QUASI BOOT" SystemID db "FAT12 " START: ; code located at 0000:7C00, adjust segment registers cli mov ax, 0x07C0 mov ds, ax mov es, ax mov fs, ax mov gs, ax ; create stack mov ax, 0x0000 mov ss, ax mov sp, 0xFFFF sti ; post message mov si, msgLoading call DisplayMessage LOAD_ROOT: ; compute size of root directory and store in ‘cx’ xor cx, cx xor dx, dx mov ax, 0x0020 ; 32 byte directory entry mul WORD [MaxRootEntries] ; total size of directory div WORD [BytesPerSector] ; sectors used by directory xchg ax, cx ; compute location of root directory and store in ‘ax’ mov al, BYTE [TotalFATs] ; number of FATs mul WORD [SectorsPerFAT] ; sectors used by FATs add ax, WORD [ReservedSectors] ; adjust for bootsector mov WORD [datasector], ax ; base of root directory add WORD [datasector], cx ; read root directory into memory (7C00:0200) mov bx, 0x0200 ; copy root dir above bootcode call ReadSectors ; browse root directory for binary image mov cx, WORD [MaxRootEntries] ; load loop counter mov di, 0x0200 ; locate first root entry .LOOP: push cx mov cx, 0x000B ; eleven character name mov si, ImageName ; image name to find push di rep cmpsb ; test for entry match pop di je LOAD_FAT pop cx add di, 0x0020 ; queue next directory entry loop .LOOP jmp FAILURE LOAD_FAT: ; save starting cluster of boot image mov si, msgCRLF call DisplayMessage mov dx, WORD [di + 0x001A] mov WORD [cluster], dx ; file’s first cluster ; compute size of FAT and store in ‘cx’ xor ax, ax mov al, BYTE [TotalFATs] ; number of FATs mul WORD [SectorsPerFAT] ; sectors used by FATs mov cx, ax ; compute location of FAT and store in ‘ax’ mov ax, WORD [ReservedSectors] ; adjust for bootsector ; read FAT into memory (7C00:0200) mov bx, 0x0200 ; copy FAT above bootcode call ReadSectors ; read image file into memory (0100:0000) mov si, msgCRLF call DisplayMessage mov ax, 0x0100 ; destination of image CS mov es, ax mov bx, 0x0000 ; destination for image IP push bx LOAD_IMAGE: mov ax, WORD [cluster] ; cluster to read pop bx ; buffer to read into call ClusterLBA ; convert cluster to LBA xor cx, cx mov cl, BYTE [SectorsPerCluster] ; sectors to read call ReadSectors push bx ; compute next cluster mov ax, WORD [cluster] ; identify current cluster mov cx, ax ; copy current cluster mov dx, ax ; copy current cluster shr dx, 0x0001 ; ;divide by two add cx, dx ; sum for (3/2) mov bx, 0x0200 ; location of FAT in memory add bx, cx ; index into FAT mov dx, WORD [bx] ; read two bytes from FAT test ax, 0x0001 jnz .ODD_CLUSTER .EVEN_CLUSTER: and dx, 0000111111111111b ; take low twelve bits jmp .DONE .ODD_CLUSTER: shr dx, 0x0004 ; take high twelve bits .DONE: mov WORD [cluster], dx ; store new cluster cmp dx, 0x0FF0 ; test for end of file jb LOAD_IMAGE DONE: mov si, msgCRLF call DisplayMessage push WORD 0x0100 push WORD 0x0000 retf FAILURE: mov si, msgFailure call DisplayMessage mov ah, 0x00 int 0x16 ; await keypress int 0x19 ; warm boot computer ;************************************************************************* ; PROCEDURE DisplayMessage ; display ASCIIZ string at ds:si via BIOS ;************************************************************************* DisplayMessage: lodsb ; load next character or al, al ; test for NUL character jz .DONE mov ah, 0x0E ; BIOS teletype mov bh, 0x00 ; display page 0 mov bl, 0x07 ; text attribute int 0x10 ; invoke BIOS jmp DisplayMessage .DONE: ret ;************************************************************************* ; PROCEDURE ReadSectors ; reads ‘cx’ sectors from disk starting at ‘ax’ into ;memory location ‘es:bx’ ;************************************************************************* ReadSectors: .MAIN mov di, 0x0005 ; five retries for error .SECTORLOOP push ax push bx push cx call LBACHS mov ah, 0x02 ; BIOS read sector mov al, 0x01 ; read one sector mov ch, BYTE [absoluteTrack] ; track mov cl, BYTE [absoluteSector] ; sector mov dh, BYTE [absoluteHead] ; head mov dl, BYTE [DriveNumber] ; drive int 0x13 ; invoke BIOS jnc .SUCCESS ; test for read error xor ax, ax ; BIOS reset disk int 0x13 ; invoke BIOS dec di ; decrement error counter pop cx pop bx pop ax jnz .SECTORLOOP ; attempt to read again int 0x18 .SUCCESS mov si, msgProgress call DisplayMessage pop cx pop bx pop ax add bx, WORD [BytesPerSector] ; queue next buffer inc ax ; queue next sector loop .MAIN ; read next sector ret ;************************************************************************* ; PROCEDURE ClusterLBA ; convert FAT cluster into LBA addressing scheme ; LBA = (cluster - 2) * sectors per cluster ;************************************************************************* ClusterLBA: sub ax, 0x0002 ; zero base cluster number xor cx, cx mov cl, BYTE [SectorsPerCluster] ; convert byte to word mul cx add ax, WORD [datasector] ; base data sector ret ;************************************************************************* ; PROCEDURE LBACHS ; convert ‘ax’ LBA addressing scheme to CHS addressing scheme ; absolute sector = (logical sector / sectors per track) + 1 ; absolute head = (logical sector / sectors per track) MOD number of heads ; absolute track = logical sector / (sectors per track * number of heads) ;************************************************************************* LBACHS: xor dx, dx ; prepare dx:ax for operation div WORD [SectorsPerTrack] ; calculate inc dl ; adjust for sector 0 mov BYTE [absoluteSector], dl xor dx, dx ; prepare dx:ax for operation div WORD [NumHeads] ; calculate mov BYTE [absoluteHead], dl mov BYTE [absoluteTrack], al ret absoluteSector db 0x00 absoluteHead db 0x00 absoluteTrack db 0x00 datasector dw 0x0000 cluster dw 0x0000 ImageName db "KERNEL BIN" msgLoading db 0x0D, 0x0A, "Loading Boot Image ", 0x0D, 0x0A, 0x00 msgCRLF db 0x0D, 0x0A, 0x00 msgProgress db ".", 0x00 msgFailure db 0x0D, 0x0A, "ERROR : Press Any Key to Reboot", 0x00 TIMES 510-($-$$) DB 0 DW 0xAA55 ;************************************************************************* CPU: Code: _get_cpuid: xor EAX,EAX cpuid mov DWORD [VendorSign], EBX mov DWORD [VendorSign+4], EDX mov DWORD [VendorSign+8], ECX mov BYTE [VendorSign+12],0x00 mov si,VendorSign mov al,0x01 int 0x021 ret DATA: Code: [segment .data] strwelcome db "Welcome to ChaOS", 0x00 strPrompt db "ChaOS>>",0x00 cmdLen db 255 OsName db "ChaOS Operating System",0x00 cmdInfo db "info",0x00 cmdExit db "exit",0x00 strUnknown db "unknown command",0x00 cmdCpuid db "cpuid",0x00 logo1 db " _.++. .+. ",0x00 logo2 db " .'///|\Y/|\; ",0x00 logo3 db " : : _ | _ | ",0x00 logo4 db " / `-.' `:' `: ChaOS Operating System",0x00 logo5 db " /|i, : ; ;. ",0x00 logo6 db " , | | |`\ Written in Assembly",0x00 logo7 db " ||Ii : | | ; ",0x00 logo8 db " ; \--gg;-gg; i: ",0x00 logo9 db " ||Ii `._,gg.' | Developer:",0x00 logo10 db " ' .' `**'`. i; Ferhat Ozgur CATAK",0x00 logo11 db " `.\` `. .'`..' / [email protected]",0x00 logo12 db " |`-._ __.-' www.ferhatcatak.cjb.net",0x00 logo13 db " : `. ",0x00 logo14 db " /i,\ , \ ",0x00 logo15 db " / ; : \ ",0x00 logo16 db " :Ii _: \ ; ",0x00 logo17 db " ; ( ; : ",0x00 logo18 db " :i'( _, / ; ",0x00 logo19 db " ;. `'--' / ",0x00 logo20 db " :i\Ii' .' ",0x00 logo21 db " | ; :__.--:*' ",0x00 logo22 db " |Ii| : ; : ",0x00 logo23 db " ; | | | | ",0x00 logo24 db " /Y | | | | [bug] ",0x00 logo25 db " .=-'Y /| ; | | ",0x00 logo26 db " :E .' ; L__:-***-.-***-. ",0x00 logo27 db " `=--' .' _ , ; , ; ",0x00 logo28 db " '----.__.__J--''`*--'' ",0x00 [segment .bss] strCmd resb 256 nCmdSize resb 1 strCmd0 resb 256 ;buffers for the command components strCmd1 resb 256 strCmd2 resb 256 strCmd3 resb 256 strCmd4 resb 256 VendorSign resb 13 EXEC: Code: _exec_cmd: ;Check for Empty Comamnd mov si,strCmd cmp BYTE[si],0x00 je _cmd_done _cmd_ver: mov si,strCmd mov di,cmdInfo mov cx,5 repe cmpsb jne _cmd_cpuid call _display_endl mov si,OsName mov al,0x01 int 0x21 call _display_space jmp _cmd_done _cmd_cpuid: mov si,strCmd mov di,cmdCpuid mov cx,6 repe cmpsb jne _cmd_exit call _display_endl call _get_cpuid jmp _exec_end _cmd_exit: mov si,strCmd mov di,cmdExit mov cx,5 repe cmpsb jne _cmd_unknown jmp _exec_end _cmd_unknown: call _display_endl mov si,strUnknown mov al,0x01 int 0x21 _cmd_done: jmp _shell_begin _exec_end: ret GRAPHICS: Code: ; Display space character _display_space: mov ah, 0x0E ; BIOS teletype mov al, 0x20 ; space character mov bh, 0x00 ; display page 0 mov bl, 0x07 ; text attribute int 0x10 ; invoke BIOS ret ; CRLF _display_endl: mov ah, 0x0E ; BIOS teletype acts on newline! mov al, 0x0D mov bh, 0x00 mov bl, 0x07 int 0x10 mov ah, 0x0E ; BIOS teletype acts on linefeed! mov al, 0x0A mov bh, 0x00 mov bl, 0x07 int 0x10 ret ;** ChaOS>> _display_prompt: mov si, strPrompt ;load message mov al, 0x01 ;request sub-service 0x01 int 0x21 ret ;** Get User Command _get_cmd: mov BYTE[nCmdSize],0x00 ; set 0 cmd length mov di,strCmd _get_cmd_begin: mov ah,0x10 ; read a char int 0x16 cmp al,0x08; check back space je _backspace cmp al, 0x0D ;check if Enter pressed je _enter_key mov bh,[cmdLen] mov bl,[nCmdSize] cmp bh,bl ; is max len reached ? je _get_cmd_begin mov [di],al ; add it to buffer inc di inc BYTE[nCmdSize] ;increment counter mov ah,0x0E ; Display char mov bl,0x07 int 0x10 jmp _get_cmd_begin; _backspace: mov bh,0x00 mov bl,[nCmdSize] cmp bh,bl ; if counter = 0 do nothing je _get_cmd_begin dec BYTE[nCmdSize] dec di mov ah,0x03 ; read cursor position mov bh,0x00 int 0x10 cmp dl,0x00 jne _move_back dec dh mov dl,79 mov ah,0x02 int 0x10 mov ah,0x09 ;display mov al,' ' mov bh,0x00 mov bl,0x07 mov cx,1 int 0x10 jmp _get_cmd_begin _move_back: mov ah,0x0E mov bh,0x00 mov bl,0x07 int 10h mov ah,0x09 mov al,' ' mov bh,0x00 mov bl,0x07 mov cx,1 int 0x10 jmp _get_cmd_begin _enter_key: mov BYTE[di],0x00 ret _disp_logo: mov si,logo1 mov al,0x01 int 0x21 call _display_endl mov si,logo2 mov al,0x01 int 0x21 call _display_endl mov si,logo3 mov al,0x01 int 0x21 call _display_endl mov si,logo4 mov al,0x01 int 0x21 call _display_endl mov si,logo5 mov al,0x01 int 0x21 call _display_endl mov si,logo6 mov al,0x01 int 0x21 call _display_endl mov si,logo7 mov al,0x01 int 0x21 call _display_endl mov si,logo8 mov al,0x01 int 0x21 call _display_endl mov si,logo9 mov al,0x01 int 0x21 call _display_endl mov si,logo10 mov al,0x01 int 0x21 call _display_endl mov si,logo11 mov al,0x01 int 0x21 call _display_endl mov si,logo12 mov al,0x01 int 0x21 call _display_endl mov si,logo13 mov al,0x01 int 0x21 call _display_endl mov si,logo14 mov al,0x01 int 0x21 call _display_endl mov si,logo15 mov al,0x01 int 0x21 call _display_endl mov si,logo16 mov al,0x01 int 0x21 call _display_endl mov si,logo17 mov al,0x01 int 0x21 call _display_endl mov si,logo18 mov al,0x01 int 0x21 call _display_endl mov si,logo19 mov al,0x01 int 0x21 call _display_endl mov si,logo20 mov al,0x01 int 0x21 call _display_endl mov si,logo21 mov al,0x01 int 0x21 call _display_endl mov si,logo22 mov al,0x01 int 0x21 call _display_endl mov si,logo23 mov al,0x01 int 0x21 call _display_endl mov si,logo24 mov al,0x01 int 0x21 call _display_endl mov si,logo25 mov al,0x01 int 0x21 call _display_endl mov si,logo26 mov al,0x01 int 0x21 call _display_endl mov si,logo28 mov al,0x01 int 0x21 call _display_endl ret MAIN: Code: ;*****************start of the kernel code*************** [org 0x000] [bits 16] [SEGMENT .text] ;START ##################################################### mov ax, 0x0100 ;location where kernel is loaded mov ds, ax mov es, ax cli mov ss, ax ;stack segment mov sp, 0xFFFF ;stack pointer at 64k limit sti push dx push es xor ax, ax mov es, ax cli mov word [es:0x21*4], _int0x21 ; setup interrupt service mov [es:0x21*4+2], cs sti pop es pop dx mov ah,00h mov al,0ch int 10h mov si, strwelcome ; load message mov al, 0x01 ; request sub-service 0x01 int 0x21 call _display_endl call _disp_logo call _shell ; call the shell int 0x19 ; reboot ;END ####################################################### _int0x21: _int0x21_ser0x01: ;service 0x01 cmp al, 0x01 ;see if service 0x01 wanted jne _int0x21_end ;goto next check (now it is end) _int0x21_ser0x01_start: lodsb ; load next character or al, al ; test for NUL character jz _int0x21_ser0x01_end mov ah, 0x0E ; BIOS teletype mov bh, 0x00 ; display page 0 mov bl, 0x07 ; text attribute int 0x10 ; invoke BIOS jmp _int0x21_ser0x01_start _int0x21_ser0x01_end: jmp _int0x21_end _int0x21_end: iret %include "graphics.asm" %include "shell.asm" %include "exec.asm" %include "cpu.asm" %include "data.inc" SHELL: Code: _shell: _shell_begin: call _display_endl call _display_prompt call _get_cmd call _exec_cmd jmp _shell_begin Ima tu jos nekih kompajlovanih fileova, ali nije mnogo samo 3. Svi fajlovi su: *.asm Samo da ja nisam pisao ovaj OS ali izgleda da je veoma dobar. |