Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2014. All rights reserved.      ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. $Revision: 4850 $
  9.  
  10.  
  11. ; Kolibri OS support loader for GRUB
  12. ;
  13. ; Copyright (C) Alex Nogueira Teixeira
  14. ; Copyright (C) Diamond
  15. ; Copyright (C) Dmitry Kartashov aka shurf
  16. ; Copyright (C) Serge
  17. ;
  18. ; Distributed under GPL, see file COPYING for details
  19. ;
  20. ; Version 1.0
  21.  
  22. lf              equ     0x0A
  23. cr              equ     0x0D
  24.  
  25. use32
  26.  
  27.  
  28. org 0x100000
  29.  
  30. mboot:
  31.   dd  0x1BADB002
  32.   dd  0x00010003
  33.   dd  -(0x1BADB002 + 0x00010003)
  34.   dd  mboot
  35.   dd  0x100000
  36.   dd  __edata
  37.   dd  __end
  38.   dd  __start
  39.  
  40. align 16
  41. __start:
  42.  
  43. virtual at ebp+3
  44. .BS_OEMName      rb 8
  45. .BPB_BytsPerSec  rw 1           ; bytes per sector
  46. .BPB_SecPerClus  rb 1           ; sectors per cluster
  47. .BPB_RsvdSecCnt  rw 1           ; number of reserver sectors
  48. .BPB_NumFATs     rb 1           ; count of FAT data structures
  49. .BPB_RootEntCnt  rw 1           ; count of 32-byte dir. entries (224*32 = 14 sectors)
  50. .BPB_TotSec16    rw 1           ; count of sectors on the volume (2880 for 1.44 mbytes disk)
  51. .BPB_Media       rb 1           ; f0 - used for removable media
  52. .BPB_FATSz16     rw 1           ; count of sectors by one copy of FAT
  53. .BPB_SecPerTrk   rw 1           ; sectors per track
  54. .BPB_NumHeads    rw 1           ; number of heads
  55. .BPB_HiddSec     rd 1           ; count of hidden sectors
  56. .BPB_TotSec32    rd 1           ; count of sectors on the volume (if > 65535)
  57. end virtual
  58.  
  59.         cld
  60.         mov     esi, mboot
  61.         mov     edi, 0x80000
  62.         mov     ecx, 624/4                      ;magic value
  63.         rep movsd
  64.         jmp     .check_mbi
  65.  
  66. org $-0x80000
  67. align 4
  68. .check_mbi:
  69.         cmp     eax, 0x2BADB002
  70.         mov     esi, sz_invboot
  71.         jne     .panic
  72.  
  73.         bt      dword [ebx], 3
  74.         mov     esi, sz_nomods
  75.         jnc     .panic
  76.  
  77.         mov     edx, [ebx+20]                   ;mods_count
  78.         mov     edi, [ebx+24]                   ;mods_addr
  79.         cmp     edx, 1
  80.         mov     esi, sz_nomods
  81.         jne     .panic
  82.  
  83. .scan_mod:
  84.         mov     ebp, [edi]                      ;image start
  85.         mov     ecx, [edi+4]                    ;image end
  86.         sub     ecx, ebp                        ;image size
  87.         cmp     ecx, 512*18*80*2                ;1.44 floppy
  88.         mov     esi, sz_image
  89.         jne     .panic
  90.  
  91.         mov     [_image_start], ebp
  92.         mov     [_image_size], ecx
  93.  
  94. ; calculate some disk parameters
  95. ; - beginning sector of RootDir
  96.  
  97.         movzx   eax, word [.BPB_FATSz16]
  98.         movzx   ecx, byte [.BPB_NumFATs]
  99.         mul     ecx
  100.         add     ax, [.BPB_RsvdSecCnt]
  101.         mov     [FirstRootDirSecNum], eax
  102.         mov     esi, eax
  103.  
  104. ; - count of sectors in RootDir
  105.         movzx   ebx, word [.BPB_BytsPerSec]
  106.         mov     cl, 5                           ; divide ax by 32
  107.         shr     ebx, cl                         ; bx = directory entries per sector
  108.         movzx   eax, word [.BPB_RootEntCnt]
  109.         xor     edx, edx
  110.         div     ebx
  111.         mov     [RootDirSecs], eax
  112.  
  113.         ; - data start
  114.         add     esi, eax                        ; add beginning sector of RootDir and count sectors in RootDir
  115.         mov     [data_start], esi
  116.  
  117. ; reading root directory
  118. ; al=count root dir sectrors !!!! TODO: al, max 255 sectors !!!!
  119.  
  120.         mov     eax, [FirstRootDirSecNum]
  121.         mul     word [.BPB_BytsPerSec]
  122.         lea     esi, [ebp+eax]
  123.  
  124.         mov     eax, [RootDirSecs]
  125.         mul     word [.BPB_BytsPerSec]
  126.         add     eax, esi                        ; EAX = end of root dir. in buffer pos_read_tmp
  127.  
  128. ; find kernel file in root directory
  129.  
  130. .loop_find_dir_entry:
  131.         push    esi
  132.         mov     ecx, 11
  133.         mov     edi, kernel_name
  134.         rep cmpsb                               ; compare es:si and es:di, cx bytes long
  135.         pop     esi
  136.         je      .found_kernel_file
  137.         add     esi, 32                         ; next dir. entry
  138.         cmp     esi, eax                        ; end of directory
  139.         jb      .loop_find_dir_entry
  140.  
  141.         mov     esi, sz_kernel
  142.         jmp     .panic
  143.  
  144.         ; === KERNEL FOUND. LOADING... ===
  145.  
  146. .found_kernel_file:
  147.  
  148.         movzx   ecx, word [esi+01ah]            ; first cluster of kernel file
  149.  
  150.         ; reading first FAT table
  151.         movzx   eax, word [.BPB_RsvdSecCnt]     ; begin first FAT abs sector number
  152.         mul     word [.BPB_BytsPerSec]
  153.         lea     ebx, [ebp+eax]                  ; FAT address
  154.  
  155. ;ebx = FAT
  156. ;ecx = cluster
  157. ;esi = src
  158. ;edi = dst
  159. ;ebp = image
  160.  
  161. ; copy kernel file
  162.  
  163.         movzx   eax, word [.BPB_BytsPerSec]
  164.         movsx   edx, byte [.BPB_SecPerClus]
  165.         mul     edx
  166.         shr     eax, 2
  167.         mov     [cluster_size], eax
  168.  
  169.         mov     edi, 0x10000                    ;kernel base address
  170.  
  171. .copy_kernel:
  172.  
  173.         ; convert cluster number to sector number
  174.         mov     eax, ecx                        ; data cluster to read
  175.         sub     eax, 2
  176.         movzx   edx, byte [.BPB_SecPerClus]
  177.         mul     edx
  178.         add     eax, [data_start]
  179.         movzx   edx, word [.BPB_BytsPerSec]
  180.         mul     edx
  181.  
  182.         lea     esi, [ebp+eax]
  183.         mov     edx, ecx
  184.         mov     ecx, [cluster_size]
  185.         rep movsd
  186.         mov     ecx, edx
  187.  
  188.         shr     edx, 1
  189.         pushf
  190.         add     edx, ecx                        ; di = bp * 1.5
  191.         mov     ax, word [ebx+edx]             ; read next entry from FAT-chain
  192.         popf
  193.         jc      .move_4_right
  194.         and     ax, 0fffh
  195.         jmp     .verify_end_sector
  196. .move_4_right:
  197.         shr     ax, 4
  198. .verify_end_sector:
  199.         cmp     ax, 0ff8h                       ; last cluster
  200.         jae     .execute_kernel
  201.         movzx   ecx, ax
  202.         jmp     .copy_kernel
  203.  
  204. .execute_kernel:
  205.  
  206.         mov     edi, 0x100000
  207.         mov     esi, [_image_start]
  208.         mov     ecx, [_image_size]
  209.         shr     ecx, 2
  210.         rep movsd
  211.         xor     eax, eax
  212.         mov     ecx, 1024
  213.         rep stosd
  214.  
  215.         xor     ebx, ebx
  216.         xor     ecx, ecx
  217.         xor     edx, edx
  218.         xor     esi, esi
  219.         xor     edi, edi
  220.         xor     ebp, ebp
  221.         xor     esp, esp
  222.  
  223.         lgdt    [.tmp_gdt]
  224.         jmp     far 0x08:.mode_16 and 0xFFFF
  225.  
  226. .panic:
  227.         mov     ebx, sz_halt
  228.         mov     edx, 0xb8000+160*10+2
  229.         mov     ah, 0x07
  230. .line:
  231.         mov     edi, edx
  232. .print:
  233.         lodsb
  234.         test    al, al
  235.         jz      .print_next
  236.         stosw
  237.         jmp     .print
  238.  
  239. .print_next:
  240.         test    ebx, ebx
  241.         jz      ._hlt
  242.  
  243.         mov     esi, ebx
  244.         xor     ebx, ebx
  245.         add     edx, 160
  246.         jmp     .line
  247.  
  248. ._hlt:
  249.         hlt
  250.         jmp     ._hlt
  251.  
  252. align 8
  253. .tmp_gdt: dw 15
  254.           dd .tmp_gdt
  255.           dw 0
  256.  
  257. .code16:  dw 0xFFFF
  258.           dw 0
  259.           db 8
  260.           db 10011010b
  261.           dw 0
  262.  
  263. use16
  264. .mode_16:
  265.         mov     eax, cr0
  266.         and     eax, not 0x80000001
  267.         mov     cr0, eax
  268.         jmp     far 0x8000:.real_mode and 0xFFFF
  269.  
  270. .real_mode:
  271.         xor     eax, eax
  272.         mov     ds, ax
  273.         mov     es, ax
  274.         mov     ss, ax
  275.         mov     gs, ax
  276.         mov     fs, ax
  277.         jmp     far 0x1000:0000
  278.  
  279.  
  280. sz_invboot   db 'Invalid multiboot loader magic value',0
  281. sz_nomods    db 'No image loaded',0
  282. sz_image     db 'Image size invalid',0
  283. sz_halt      db 'Halted',0
  284.  
  285. sz_kernel    db cr
  286. kernel_name  db 'KERNEL  MNT ?',0
  287.  
  288. org $+0x80000
  289. __edata:
  290.  
  291. align 4
  292. _image_start        rd 1
  293. _image_size         rd 1
  294.  
  295. FirstRootDirSecNum  rd 1
  296. RootDirSecs         rd 1
  297. data_start          rd 1
  298. cluster_size        rd 1
  299. __end:
  300.