Subversion Repositories Kolibri OS

Rev

Rev 7136 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2015. All rights reserved. ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. ; FAT12 boot sector for Kolibri OS
  9. ;
  10. ; Copyright (C) Alex Nogueira Teixeira
  11. ; Copyright (C) Diamond
  12. ; Copyright (C) Dmitry Kartashov aka shurf
  13. ;
  14. ; Distributed under GPL, see file COPYING for details
  15. ;
  16. ; Version 1.0
  17.  
  18. include "lang.inc"
  19.  
  20. lf              =     0ah
  21. cr              =     0dh
  22.  
  23. pos_read_tmp    =     0700h                   ;position for temporary read
  24. boot_program    =     07c00h                  ;position for boot code
  25. seg_read_kernel =     01000h                  ;segment to kernel read
  26.  
  27.         jmp     start_program
  28.         nop
  29.  
  30. ; Boot Sector and BPB Structure
  31. include 'floppy1440.inc'
  32. ;include 'floppy2880.inc'
  33. ;include 'floppy1680.inc'
  34. ;include 'floppy1743.inc'
  35.  
  36. start_program:
  37. ; <Efremenkov S.V.>
  38.         cld     ;clear direction flag for Phoenix BIOS, see next "lodsb"
  39.         xor     ax, ax
  40.         cli
  41.         mov     ss, ax
  42.         mov     sp, boot_program
  43.         sti
  44. ; <\Efremenkov S.V.>
  45.         push    ss
  46.         pop     ds
  47.  
  48.         ; print loading string
  49.         mov     si, loading+boot_program
  50. loop_loading:
  51.         lodsb
  52.         or      al, al
  53.         jz      read_root_directory
  54.         mov     ah, 0eh
  55.         mov     bx, 7
  56.         int     10h
  57.         jmp     loop_loading
  58.  
  59. read_root_directory:
  60.         push    ss
  61.         pop     es
  62.  
  63.         ; calculate some disk parameters
  64.         ; - beginning sector of RootDir
  65.         mov     ax, word [BPB_FATSz16+boot_program]
  66.         xor     cx, cx
  67.         mov     cl, byte [BPB_NumFATs+boot_program]
  68.         mul     cx
  69.         add     ax, word [BPB_RsvdSecCnt+boot_program]
  70.         mov     word [FirstRootDirSecNum+boot_program], ax      ; 19
  71.         mov     si, ax
  72.  
  73.         ; - count of sectors in RootDir
  74.         mov     bx, word [BPB_BytsPerSec+boot_program]
  75.         mov     cl, 5                           ; divide ax by 32
  76.         shr     bx, cl                          ; bx = directory entries per sector
  77.         mov     ax, word [BPB_RootEntCnt+boot_program]
  78.         xor     dx, dx
  79.         div     bx
  80.         mov     word [RootDirSecs+boot_program], ax             ; 14
  81.  
  82.         ; - data start
  83.         add     si, ax                          ; add beginning sector of RootDir and count sectors in RootDir
  84.         mov     word [data_start+boot_program], si              ; 33
  85.         ; reading root directory
  86.         ; al=count root dir sectrors !!!! TODO: al, max 255 sectors !!!!
  87.         mov     ah, 2                           ; read
  88.         push    ax
  89.  
  90.         mov     ax, word [FirstRootDirSecNum+boot_program]
  91.         call    conv_abs_to_THS                 ; convert abs sector (AX) to BIOS T:H:S (track:head:sector)
  92.         pop     ax
  93.         mov     bx, pos_read_tmp                ; es:bx read buffer
  94.         call    read_sector
  95.  
  96.         mov     si, bx                          ; read buffer address: es:si
  97.         mov     ax, [RootDirSecs+boot_program]
  98.         mul     word [BPB_BytsPerSec+boot_program]
  99.         add     ax, si                          ; AX = end of root dir. in buffer pos_read_tmp
  100.  
  101.         ; find kernel file in root directory
  102. loop_find_dir_entry:
  103.         push    si
  104.         mov     cx, 11
  105.         mov     di, kernel_name+boot_program
  106.         rep cmpsb                               ; compare es:si and es:di, cx bytes long
  107.         pop     si
  108.         je      found_kernel_file
  109.         add     si, 32                          ; next dir. entry
  110.         cmp     si, ax                          ; end of directory
  111.         jb      loop_find_dir_entry
  112.  
  113. file_error_message:
  114.         mov     si, error_message+boot_program
  115.  
  116. loop_error_message:
  117.         lodsb
  118.         or      al, al
  119.         jz      freeze_pc
  120.         mov     ah, 0eh
  121.         mov     bx, 7
  122.         int     10h
  123.         jmp     loop_error_message
  124.  
  125. freeze_pc:
  126.         jmp     $                               ; endless loop
  127.  
  128.         ; === KERNEL FOUND. LOADING... ===
  129.  
  130. found_kernel_file:
  131.         mov     bp, [si+01ah]                   ; first cluster of kernel file
  132.         ; <diamond>
  133.         mov     [cluster1st+boot_program], bp   ; starting cluster of kernel file
  134.         ; <\diamond>
  135.  
  136.         ; reading first FAT table
  137.         mov     ax, word [BPB_RsvdSecCnt+boot_program]  ; begin first FAT abs sector number
  138.         call    conv_abs_to_THS                 ; convert abs sector (AX) to BIOS T:H:S (track:head:sector)
  139.         mov     bx, pos_read_tmp                ; es:bx read position
  140.         mov     ah, 2                           ; ah=2 (read)
  141.         mov     al, byte [BPB_FATSz16+boot_program]     ; FAT size in sectors (TODO: max 255 sectors)
  142.         call    read_sector
  143.         jc      file_error_message              ; read error
  144.  
  145.         mov     ax, seg_read_kernel
  146.         mov     es, ax
  147.         xor     bx, bx                          ; es:bx = 1000h:0000h
  148.  
  149.  
  150.         ; reading kernel file
  151. loop_obtains_kernel_data:
  152.         ; read one cluster of file
  153.         call    obtain_cluster
  154.         jc      file_error_message              ; read error
  155.  
  156.         ; add one cluster length to segment:offset
  157.         push    bx
  158.         mov     bx, es
  159.         mov     ax, word [BPB_BytsPerSec+boot_program]  ;\
  160.         movsx   cx, byte [BPB_SecPerClus+boot_program]  ; | !!! TODO: !!!
  161.         mul     cx                                      ; | out this from loop !!!
  162.         shr     ax, 4                                   ;/
  163.         add     bx, ax
  164.         mov     es, bx
  165.         pop     bx
  166.  
  167.         mov     di, bp
  168.         shr     di, 1
  169.         pushf
  170.         add     di, bp                          ; di = bp * 1.5
  171.         add     di, pos_read_tmp
  172.         mov     ax, [di]                        ; read next entry from FAT-chain
  173.         popf
  174.         jc      move_4_right
  175.         and     ax, 0fffh
  176.         jmp     verify_end_sector
  177. move_4_right:
  178.         mov     cl, 4
  179.         shr     ax, cl
  180. verify_end_sector:
  181.         cmp     ax, 0ff8h                       ; last cluster
  182.         jae     execute_kernel
  183.         mov     bp, ax
  184.         jmp     loop_obtains_kernel_data
  185.  
  186. execute_kernel:
  187.         ; <diamond>
  188.         mov     ax, 'KL'
  189.         push    0
  190.         pop     ds
  191.         mov     si, loader_block+boot_program
  192.         ; </diamond>
  193.         push    word seg_read_kernel
  194.         push    word 0
  195.         retf                                    ; jmp far 1000:0000
  196.  
  197.  
  198. ;------------------------------------------
  199.         ; loading cluster from file to es:bx
  200. obtain_cluster:
  201.         ; bp - cluster number to read
  202.         ; carry = 0 -> read OK
  203.         ; carry = 1 -> read ERROR
  204.  
  205.         ; print one dot
  206.         push    bx
  207.         mov     ax, 0e2eh                       ; ah=0eh (teletype), al='.'
  208.         xor     bh, bh
  209.         int     10h
  210.         pop     bx
  211.  
  212. writesec:
  213.         ; convert cluster number to sector number
  214.         mov     ax, bp                          ; data cluster to read
  215.         sub     ax, 2
  216.         xor     dx, dx
  217.         mov     dl, byte [BPB_SecPerClus+boot_program]
  218.         mul     dx
  219.         add     ax, word [data_start+boot_program]
  220.  
  221.         call    conv_abs_to_THS                 ; convert abs sector (AX) to BIOS T:H:S (track:head:sector)
  222. patchhere:
  223.         mov     ah, 2                           ; ah=2 (read)
  224.         mov     al, byte [BPB_SecPerClus+boot_program]  ; al=(one cluster)
  225.         call    read_sector
  226.         retn
  227. ;------------------------------------------
  228.  
  229. ;------------------------------------------
  230.         ; read sector from disk
  231. read_sector:
  232.         push    bp
  233.         mov     bp, 20                          ; try 20 times
  234. newread:
  235.         dec     bp
  236.         jz      file_error_message
  237.         push    ax bx cx dx
  238.         int     13h
  239.         pop     dx cx bx ax
  240.         jc      newread
  241.         pop     bp
  242.         retn
  243. ;------------------------------------------
  244.         ; convert abs. sector number (AX) to BIOS T:H:S
  245.         ; sector number = (abs.sector%BPB_SecPerTrk)+1
  246.         ; pre.track number = (abs.sector/BPB_SecPerTrk)
  247.         ; head number = pre.track number%BPB_NumHeads
  248.         ; track number = pre.track number/BPB_NumHeads
  249.         ; Return: cl - sector number
  250.         ;         ch - track number
  251.         ;         dl - drive number (0 = a:)
  252.         ;         dh - head number
  253. conv_abs_to_THS:
  254.         push    bx
  255.         mov     bx, word [BPB_SecPerTrk+boot_program]
  256.         xor     dx, dx
  257.         div     bx
  258.         inc     dx
  259.         mov     cl, dl                          ; cl = sector number
  260.         mov     bx, word [BPB_NumHeads+boot_program]
  261.         xor     dx, dx
  262.         div     bx
  263.         ; !!!!!!! ax = track number, dx = head number
  264.         mov     ch, al                          ; ch=track number
  265.         xchg    dh, dl                          ; dh=head number
  266.         mov     dl, 0                           ; dl=0 (drive 0 (a:))
  267.         pop     bx
  268.         retn
  269. ;------------------------------------------
  270.  
  271. if lang eq sp
  272. loading         db      cr,lf,'Iniciando el sistema ',00h
  273. else
  274. loading         db      cr,lf,'Starting system ',00h
  275. end if
  276. error_message   db      13,10
  277. kernel_name     db      'KERNEL  MNT ?',cr,lf,00h
  278. FirstRootDirSecNum      dw      ?
  279. RootDirSecs     dw      ?
  280. data_start      dw      ?
  281.  
  282. ; <diamond>
  283. write1st:
  284.         push    cs
  285.         pop     ds
  286.         mov     byte [patchhere+1+boot_program], 3      ; change ah=2 to ah=3
  287.         mov     bp, [cluster1st+boot_program]
  288.         push    1000h
  289.         pop     es
  290.         xor     bx, bx
  291.         call    writesec
  292.         mov     byte [patchhere+1+boot_program], 2      ; change back ah=3 to ah=2
  293.         retf
  294. cluster1st      dw      ?
  295. loader_block:
  296.                 db      1
  297.                 dw      0
  298.                 dw      write1st+boot_program
  299.                 dw      0
  300. ; <\diamond>
  301.  
  302. times   0x1fe-$ db 00h
  303.  
  304.         db      55h,0aah                        ;boot signature
  305.