Subversion Repositories Kolibri OS

Rev

Rev 1379 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. $Revision: 1410 $
  9.  
  10.  
  11. ;*************************************************************
  12. ;* 13.02.2010 Find all partition and check supported FS
  13. ;* 12.07.2007 Check all 4 entry of MBR and EMBR
  14. ;* 29.04.2006 Elimination of hangup after the
  15. ;*             expiration hd_wait_timeout -  Mario79
  16. ;* 28.01.2006 find all Fat16/32 partition in all input point
  17. ;*            to MBR - Mario79
  18. ;*************************************************************
  19.  
  20. uglobal
  21. align 4
  22.  
  23. ;******************************************************
  24. ; Please do not change this place - variables  in text
  25. ; Mario79
  26. ; START place
  27. ;******************************************************
  28. PARTITION_START      dd 0x3f
  29. PARTITION_END        dd 0
  30. fs_type              db 0       ; 1=NTFS, 2=EXT2/3, 16=FAT16, 32=FAT32
  31. align 4
  32.  
  33. fs_dependent_data_start:
  34. ; FATxx data
  35.  
  36. SECTORS_PER_FAT      dd 0x1f3a
  37. NUMBER_OF_FATS       dd 0x2
  38. SECTORS_PER_CLUSTER  dd 0x8
  39. BYTES_PER_SECTOR     dd 0x200   ; Note: if BPS <> 512 need lots of changes
  40. ROOT_CLUSTER         dd 2       ; first rootdir cluster
  41. FAT_START            dd 0       ; start of fat table
  42. ROOT_START           dd 0       ; start of rootdir (only fat16)
  43. ROOT_SECTORS         dd 0       ; count of rootdir sectors (only fat16)
  44. DATA_START           dd 0       ; start of data area (=first cluster 2)
  45. LAST_CLUSTER         dd 0       ; last availabe cluster
  46. ADR_FSINFO           dd 0       ; used only by fat32
  47.  
  48. fatRESERVED          dd 0x0FFFFFF6
  49. fatBAD               dd 0x0FFFFFF7
  50. fatEND               dd 0x0FFFFFF8
  51. fatMASK              dd 0x0FFFFFFF
  52.  
  53. fatStartScan         dd 2
  54.  
  55. fs_dependent_data_end:
  56. file_system_data_size = $ - PARTITION_START
  57. if file_system_data_size > 96
  58. ERROR: sizeof(file system data) too big!
  59. end if
  60.  
  61. virtual at fs_dependent_data_start
  62. ; NTFS data
  63. ntfs_data:
  64. .sectors_per_cluster    dd      ?
  65. .mft_cluster            dd      ?
  66. .mftmirr_cluster        dd      ?
  67. .frs_size               dd      ?       ; FRS size in bytes
  68. .iab_size               dd      ?       ; IndexAllocationBuffer size in bytes
  69. .frs_buffer             dd      ?
  70. .iab_buffer             dd      ?
  71. .mft_retrieval          dd      ?
  72. .mft_retrieval_size     dd      ?
  73. .mft_retrieval_alloc    dd      ?
  74. .mft_retrieval_end      dd      ?
  75. .cur_index_size         dd      ?
  76. .cur_index_buf          dd      ?
  77. if $ > fs_dependent_data_end
  78. ERROR: increase sizeof(fs_dependent_data)!
  79. end if
  80. end virtual
  81.  
  82. virtual at fs_dependent_data_start
  83. ; EXT2 data
  84. ext2_data:
  85.     .log_block_size                 dd ?
  86.     .block_size                     dd ?
  87.     .count_block_in_block           dd ?
  88.     .blocks_per_group               dd ?
  89.     .inodes_per_group               dd ?
  90.     .global_desc_table              dd ?
  91.     .root_inode                     dd ?        ; pointer to root inode in memory
  92.     .inode_size                     dd ?
  93.     .count_pointer_in_block         dd ?        ;  block_size / 4
  94.     .count_pointer_in_block_square  dd ?        ; (block_size / 4)**2
  95.     .ext2_save_block                dd ?    ; ¡«®ª ­  £«®¡ «ì­ãî 1 ¯à®æ¥¤ãàã
  96.     .ext2_temp_block                dd ?    ; ¡«®ª ¤«ï ¬¥«ª¨å ¯à®æ¥¤ãà
  97.     .ext2_save_inode                dd ?    ; inode ­  £«®¡ «ì­ãî ¯à®æ¥¤ãàã
  98.     .ext2_temp_inode                dd ?    ; inode ¤«ï ¬¥«ª¨å ¯à®æ¥¤ãà
  99. if $ > fs_dependent_data_end
  100. ERROR: increase sizeof(fs_dependent_data)!
  101. end if
  102. end virtual
  103.  
  104. ;***************************************************************************
  105. ; End place
  106. ; Mario79
  107. ;***************************************************************************
  108. endg
  109. iglobal
  110.  
  111.   partition_types:              ; list of fat16/32 partitions
  112.     db    0x04                  ; DOS: fat16 <32M
  113.     db    0x06                  ; DOS: fat16 >32M
  114.     db    0x0b                  ; WIN95: fat32
  115.     db    0x0c                  ; WIN95: fat32, LBA-mapped
  116.     db    0x0e                  ; WIN95: fat16, LBA-mapped
  117.     db    0x14                  ; Hidden DOS: fat16 <32M
  118.     db    0x16                  ; Hidden DOS: fat16 >32M
  119.     db    0x1b                  ; Hidden WIN95: fat32
  120.     db    0x1c                  ; Hidden WIN95: fat32, LBA-mapped
  121.     db    0x1e                  ; Hidden WIN95: fat16, LBA-mapped
  122.     db    0xc4                  ; DRDOS/secured: fat16 <32M
  123.     db    0xc6                  ; DRDOS/secured: fat16 >32M
  124.     db    0xcb                  ; DRDOS/secured: fat32
  125.     db    0xcc                  ; DRDOS/secured: fat32, LBA-mapped
  126.     db    0xce                  ; DRDOS/secured: fat16, LBA-mapped
  127.     db    0xd4                  ; Old Multiuser DOS secured: fat16 <32M
  128.     db    0xd6                  ; Old Multiuser DOS secured: fat16 >32M
  129.     db    0x07                  ; NTFS
  130.     db    0x27                  ; NTFS, hidden
  131.     db    0x83                  ; Linux native file system (ext2fs)
  132.   partition_types_end:
  133.  
  134.  
  135.   extended_types:               ; list of extended partitions
  136.     db    0x05                  ; DOS: extended partition
  137.     db    0x0f                  ; WIN95: extended partition, LBA-mapped
  138.     db    0xc5                  ; DRDOS/secured: extended partition
  139.     db    0xd5                  ; Old Multiuser DOS secured: extended partition
  140.   extended_types_end:
  141.  
  142. endg
  143.  
  144. ; Partition chain used:
  145. ; MBR <---------------------
  146. ; |                         |
  147. ; |-> PARTITION1          |
  148. ; |-> EXTENDED PARTITION -        ;not need be second partition
  149. ; |-> PARTITION3
  150. ; |-> PARTITION4
  151.  
  152. set_PARTITION_variables:
  153. set_FAT32_variables:        ;deprecated
  154.     and   [problem_partition], 0
  155.     call  reserve_hd1
  156.     call  reserve_hd_channel
  157.  
  158.     pushad
  159.  
  160.     cmp   dword [hdpos],0
  161.     je    problem_hd
  162.  
  163.     xor   ecx,ecx               ; partition count
  164.     ;or    edx,-1                ; flag for partition
  165.     xor   eax,eax               ; address MBR
  166.     xor   ebp,ebp               ; extended partition start
  167.  
  168. new_mbr:
  169.     test  ebp,ebp               ; is there extended partition? (MBR or EMBR)
  170.     jnz   extended_already_set  ; yes
  171.     xchg  ebp,eax               ; no. set it now
  172.  
  173. extended_already_set:
  174.     add   eax,ebp               ; mbr=mbr+0, ext_part=ext_start+relat_start
  175.     mov   ebx,buffer
  176.     call  hd_read
  177.     cmp  [hd_error],0
  178.     jne  problem_hd
  179.  
  180.     cmp   word [ebx+0x1fe],0xaa55 ; is it valid boot sector?
  181.     jnz   end_partition_chain
  182.     push  eax                      ; push only one time
  183.     cmp   dword [ebx+0x1be+0xc],0 ; skip over empty partition
  184.     jnz    test_primary_partition_0
  185.     cmp   dword [ebx+0x1be+0xc+16],0
  186.     jnz    test_primary_partition_1
  187.     cmp   dword [ebx+0x1be+0xc+16+16],0
  188.     jnz    test_primary_partition_2
  189.     cmp   dword [ebx+0x1be+0xc+16+16+16],0
  190.     jnz    test_primary_partition_3
  191.     pop   eax
  192.     jmp   end_partition_chain
  193.  
  194. test_primary_partition_0:
  195.     mov   al,[ebx+0x1be+4]      ; get primary partition type
  196.     call  scan_partition_types
  197.     jnz   test_primary_partition_1      ; no. skip over
  198.  
  199.     inc   ecx
  200.     cmp   ecx,[known_part]       ; is it wanted partition?
  201.     jnz   test_primary_partition_1      ; no
  202.  
  203.         pop     eax
  204.         ;mov     edx, eax                ; start sector
  205.         add     eax, [ebx+0x1be+8]      ; add relative start
  206.         ;mov     [PARTITON_START],edx
  207.         ;push    edx
  208.         mov     edx, [ebx+0x1be+12]     ; length
  209.         ;add     edx, eax                ; add length
  210.         ;dec     edx                     ; PARTITION_END is inclusive
  211.         ;mov     [PARTITION_END], edx    ; note that this can be changed
  212.                                         ; when file system data will be available
  213.         mov     cl, [ebx+0x1be+4]       ; fs_type
  214.         ;mov     [fs_type], dl           ; save for FS recognizer (separate FAT vs NTFS)
  215.         ;pop     edx
  216.         jmp     hd_and_partition_ok
  217.  
  218. test_primary_partition_1:
  219.     mov   al,[ebx+0x1be+4+16]      ; get primary partition type
  220.     call  scan_partition_types
  221.     jnz   test_primary_partition_2        ; no. skip over
  222.  
  223.     inc   ecx
  224.     cmp   ecx,[known_part]       ; is it wanted partition?
  225.     jnz   test_primary_partition_2        ; no
  226.  
  227.         pop     eax
  228.         add     eax, [ebx+0x1be+8+16]
  229.         mov     edx, [ebx+0x1be+12+16]
  230.         mov     cl, [ebx+0x1be+4+16]
  231.         jmp     hd_and_partition_ok
  232.  
  233.         ;mov     edx, eax
  234.         ;add     edx, [ebx+0x1be+8+16]
  235.         ;push    edx
  236.         ;add     edx, [ebx+0x1be+12+16]
  237.         ;dec     edx
  238.         ;mov     [PARTITION_END], edx
  239.         ;mov     al, [ebx+0x1be+4+16]
  240.         ;mov     [fs_type], dl
  241.         ;pop     edx
  242.  
  243. test_primary_partition_2:
  244.     mov   al,[ebx+0x1be+4+16+16]      ; get primary partition type
  245.     call  scan_partition_types
  246.     jnz   test_primary_partition_3        ; no. skip over
  247.  
  248.     inc   ecx
  249.     cmp   ecx,[known_part]       ; is it wanted partition?
  250.     jnz   test_primary_partition_3        ; no
  251.  
  252.         pop     eax
  253.         add     eax, [ebx+0x1be+8+16+16]
  254.         mov     edx, [ebx+0x1be+12+16+16]
  255.         mov     cl, [ebx+0x1be+4+16+16]
  256.         jmp     hd_and_partition_ok
  257.         ;mov     edx, eax
  258.         ;add     edx, [ebx+0x1be+8+16+16]
  259.         ;push    edx
  260.         ;add     edx, [ebx+0x1be+12+16+16]
  261.         ;dec     edx
  262.         ;mov     [PARTITION_END], edx
  263.         ;mov     al, [ebx+0x1be+4+16+16]
  264.         ;mov     [fs_type], dl
  265.         ;pop     edx
  266.  
  267. test_primary_partition_3:
  268.     mov   al,[ebx+0x1be+4+16+16+16]      ; get primary partition type
  269.     call  scan_partition_types
  270.     jnz   test_ext_partition_0        ; no. skip over
  271.  
  272.     inc   ecx
  273.     cmp   ecx,[known_part]       ; is it wanted partition?
  274.     jnz   test_ext_partition_0  ; no
  275.  
  276.         pop     eax
  277.         add     eax, [ebx+0x1be+8+16+16+16]
  278.         mov     edx, [ebx+0x1be+12+16+16+16]
  279.         mov     cl, [ebx+0x1be+4+16+16+16]
  280.         jmp     hd_and_partition_ok
  281.  
  282.         ;mov     edx, eax
  283.         ;add     edx, [ebx+0x1be+8+16+16+16]
  284.         ;push    edx
  285.         ;add     edx, [ebx+0x1be+12+16+16+16]
  286.         ;dec     edx
  287.         ;mov     [PARTITION_END], edx
  288.         ;mov     al, [ebx+0x1be+4+16+16+16]
  289.         ;mov     [fs_type], dl
  290.         ;pop     edx
  291.  
  292. test_ext_partition_0:
  293.     pop   eax               ; ¯à®áâ® ¢ëª¨¤ë¢ ¥¬ ¨§ á⥪ 
  294.     mov   al,[ebx+0x1be+4]   ; get extended partition type
  295.     call  scan_extended_types
  296.     jnz   test_ext_partition_1
  297.  
  298.     mov   eax,[ebx+0x1be+8]     ; add relative start
  299.     test  eax,eax               ; is there extended partition?
  300.     jnz   new_mbr               ; yes. read it
  301.  
  302. test_ext_partition_1:
  303.     mov   al,[ebx+0x1be+4+16]   ; get extended partition type
  304.     call  scan_extended_types
  305.     jnz   test_ext_partition_2
  306.  
  307.     mov   eax,[ebx+0x1be+8+16]  ; add relative start
  308.     test  eax,eax               ; is there extended partition?
  309.     jnz   new_mbr               ; yes. read it
  310.  
  311. test_ext_partition_2:
  312.     mov   al,[ebx+0x1be+4+16+16]   ; get extended partition type
  313.     call  scan_extended_types
  314.     jnz   test_ext_partition_3
  315.  
  316.     mov   eax,[ebx+0x1be+8+16+16]     ; add relative start
  317.     test  eax,eax               ; is there extended partition?
  318.     jnz   new_mbr               ; yes. read it
  319.  
  320. test_ext_partition_3:
  321.     mov   al,[ebx+0x1be+4+16+16+16]   ; get extended partition type
  322.     call  scan_extended_types
  323.     jnz   end_partition_chain   ; no. end chain
  324.  
  325.     mov   eax,[ebx+0x1be+8+16+16+16]  ; get start of extended partition
  326.     test  eax,eax               ; is there extended partition?
  327.     jnz   new_mbr               ; yes. read it
  328.  
  329. end_partition_chain:
  330.     ;mov   [partition_count],ecx
  331.  
  332.     ;cmp   edx,-1                ; found wanted partition?
  333.     ;jnz   hd_and_partition_ok   ; yes. install it
  334.     ;jmp   problem_partition_or_fat
  335. problem_hd:
  336.     or    [problem_partition], 2
  337.     jmp   return_from_part_set
  338.  
  339.  
  340. scan_partition_types:
  341.     push  ecx
  342.     mov   edi,partition_types
  343.     mov   ecx,partition_types_end-partition_types
  344.     cld
  345.     repne scasb                 ; is partition type ok?
  346.     pop   ecx
  347.     ret
  348.  
  349. scan_extended_types:
  350.     push  ecx
  351.     mov   edi,extended_types
  352.     mov   ecx,extended_types_end-extended_types
  353.     cld
  354.     repne scasb                 ; is it extended partition?
  355.     pop   ecx
  356.     ret
  357.  
  358. problem_fat_dec_count:          ; bootsector is missing or another problem
  359. ;    dec   [partition_count]     ; remove it from partition_count
  360.  
  361. problem_partition_or_fat:
  362.     or    [problem_partition],1
  363.  
  364. return_from_part_set:
  365.     popad
  366.     ;mov   [fs_type],0
  367.     call  free_hd_channel
  368.     mov   [hd1_status],0        ; free
  369.     ret
  370.  
  371. hd_and_partition_ok:
  372.  
  373. ;eax = PARTITION_START edx=PARTITION_LENGTH cl=fs_type
  374.     mov   [fs_type], cl
  375.     ;mov   eax,edx
  376.     mov   [PARTITION_START],eax
  377.     add   edx, eax
  378.     dec   edx
  379.     mov   [PARTITION_END], edx
  380.  
  381.    ;     mov     edx, [PARTITION_END]
  382.    ;     sub     edx, eax
  383.    ;     inc     edx     ; edx = length of partition § ç¥¬ ®­® ­ ¬??
  384.  
  385. ;    mov   [hd_setup],1
  386.     mov   ebx,buffer
  387.     call  hd_read               ; read boot sector of partition
  388.         cmp     [hd_error], 0
  389.         jz      boot_read_ok
  390.         cmp     [fs_type], 7
  391.         jnz     problem_fat_dec_count
  392. ; NTFS duplicates bootsector:
  393. ; NT4/2k/XP+ saves bootsector copy in the end of disk
  394. ; NT 3.51 saves bootsector copy in the middle of disk
  395.         and     [hd_error], 0
  396.         mov     eax, [PARTITION_END]
  397.         call    hd_read
  398.         cmp     [hd_error], 0
  399.         jnz     @f
  400.         call    ntfs_test_bootsec
  401.         jnc     boot_read_ok
  402. @@:
  403.         and     [hd_error], 0
  404.         mov     eax, edx
  405.         shr     eax, 1
  406.         add     eax, [PARTITION_START]
  407.         call    hd_read
  408.         cmp     [hd_error], 0
  409.         jnz     problem_fat_dec_count   ; no chance...
  410. boot_read_ok:
  411.  
  412. ; if we are running on NTFS, check bootsector
  413.  
  414.         call    ntfs_test_bootsec      ; test ntfs
  415.         jnc     ntfs_setup
  416.  
  417.         call    ext2_test_superblock   ; test ext2fs
  418.         jnc     ext2_setup
  419.  
  420.         mov     eax, [PARTITION_START]  ;ext2 test changes [buffer]
  421.         call    hd_read
  422.         cmp     [hd_error], 0
  423.         jnz     problem_fat_dec_count
  424.  
  425.     cmp   word [ebx+0x1fe],0xaa55 ; is it valid boot sector?
  426.     jnz   problem_fat_dec_count
  427.  
  428.     movzx eax,word [ebx+0xe]    ; sectors reserved
  429.     add   eax,[PARTITION_START]
  430.     mov   [FAT_START],eax       ; fat_start = partition_start + reserved
  431.  
  432.     movzx eax,byte [ebx+0xd]    ; sectors per cluster
  433.     test  eax,eax
  434.     jz    problem_fat_dec_count
  435.     mov   [SECTORS_PER_CLUSTER],eax
  436.  
  437.     movzx ecx,word [ebx+0xb]    ; bytes per sector
  438.     cmp   ecx,0x200
  439.     jnz   problem_fat_dec_count
  440.     mov   [BYTES_PER_SECTOR],ecx
  441.  
  442.     movzx eax,word [ebx+0x11]   ; count of rootdir entries (=0 fat32)
  443.     mov   edx,32
  444.     mul   edx
  445.     dec   ecx
  446.     add   eax,ecx               ; round up if not equal count
  447.     inc   ecx                   ; bytes per sector
  448.     div   ecx
  449.     mov   [ROOT_SECTORS],eax    ; count of rootdir sectors
  450.  
  451.     movzx eax,word [ebx+0x16]   ; sectors per fat <65536
  452.     test  eax,eax
  453.     jnz   fat16_fatsize
  454.     mov   eax,[ebx+0x24]        ; sectors per fat
  455.   fat16_fatsize:
  456.     mov   [SECTORS_PER_FAT],eax
  457.  
  458.     movzx eax,byte [ebx+0x10]   ; number of fats
  459.     test  eax,eax               ; if 0 it's not fat partition
  460.     jz    problem_fat_dec_count
  461.     mov   [NUMBER_OF_FATS],eax
  462.     imul  eax,[SECTORS_PER_FAT]
  463.     add   eax,[FAT_START]
  464.     mov   [ROOT_START],eax      ; rootdir = fat_start + fat_size * fat_count
  465.     add   eax,[ROOT_SECTORS]    ; rootdir sectors should be 0 on fat32
  466.     mov   [DATA_START],eax      ; data area = rootdir + rootdir_size
  467.  
  468.     movzx eax,word [ebx+0x13]   ; total sector count <65536
  469.     test  eax,eax
  470.     jnz   fat16_total
  471.     mov   eax,[ebx+0x20]        ; total sector count
  472.   fat16_total:
  473.     add   eax,[PARTITION_START]
  474.     dec   eax
  475.     mov   [PARTITION_END],eax
  476.     inc   eax
  477.     sub   eax,[DATA_START]      ; eax = count of data sectors
  478.     xor   edx,edx
  479.     div   dword [SECTORS_PER_CLUSTER]
  480.     inc   eax
  481.     mov   [LAST_CLUSTER],eax
  482.     dec   eax                   ; cluster count
  483.     mov   [fatStartScan],2
  484.  
  485.     ; limits by Microsoft Hardware White Paper v1.03
  486.     cmp   eax,4085              ; 0xff5
  487.     jb    problem_fat_dec_count ; fat12 not supported
  488.     cmp   eax,65525             ; 0xfff5
  489.     jb    fat16_partition
  490.  
  491. fat32_partition:
  492.     mov   eax,[ebx+0x2c]        ; rootdir cluster
  493.     mov   [ROOT_CLUSTER],eax
  494.     movzx eax,word [ebx+0x30]   ; fs info sector
  495.     add   eax,[PARTITION_START]
  496.     mov   [ADR_FSINFO],eax
  497.     call  hd_read
  498.     mov   eax,[ebx+0x1ec]
  499.     cmp   eax,-1
  500.     jz    @f
  501.     mov   [fatStartScan],eax
  502. @@:
  503.  
  504.     popad
  505.  
  506.     mov   [fatRESERVED],0x0FFFFFF6
  507.     mov   [fatBAD],0x0FFFFFF7
  508.     mov   [fatEND],0x0FFFFFF8
  509.     mov   [fatMASK],0x0FFFFFFF
  510.     mov   [fs_type],32         ; Fat32
  511.     call  free_hd_channel
  512.     mov   [hd1_status],0        ; free
  513.     ret
  514.  
  515. fat16_partition:
  516.     xor   eax,eax
  517.     mov   [ROOT_CLUSTER],eax
  518.  
  519.     popad
  520.  
  521.     mov   [fatRESERVED],0x0000FFF6
  522.     mov   [fatBAD],0x0000FFF7
  523.     mov   [fatEND],0x0000FFF8
  524.     mov   [fatMASK],0x0000FFFF
  525.     mov   [fs_type],16         ; Fat16
  526.     call  free_hd_channel
  527.     mov   [hd1_status],0        ; free
  528.     ret
  529.  
  530.