Subversion Repositories Kolibri OS

Rev

Rev 6842 | 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. $Revision: 8051 $
  9.  
  10. ; HDD and CD search
  11.  
  12.         cmp     [ecx+IDE_DATA.ProgrammingInterface], 0
  13.         je      EndFindHDD
  14. FindHDD:
  15.         xor     ebx, ebx
  16.         inc     ebx
  17.         mov     [DeviceNumber], 0
  18.         cmp     ecx, IDE_controller_1
  19.         jz      .find
  20.         add     bl, 5
  21.         add     [DeviceNumber], sizeof.HD_DATA*4
  22.         cmp     ecx, IDE_controller_2
  23.         jz      .find
  24.         add     bl, 5
  25.         add     [DeviceNumber], sizeof.HD_DATA*4
  26. .find:
  27.         mov     [ChannelNumber], 1
  28.         mov     [DiskNumber], 0
  29.         call    FindHDD_1
  30.         inc     [DiskNumber]
  31.         call    FindHDD_2
  32.         inc     [ChannelNumber]
  33.         dec     [DiskNumber]
  34.         call    FindHDD_2
  35.         inc     [DiskNumber]
  36.         call    FindHDD_2
  37.         jmp     EndFindHDD
  38. ;-----------------------------------------------------------------------------
  39. FindHDD_2:
  40.         add     [DeviceNumber], sizeof.HD_DATA
  41.         shl     byte [ebx+DRIVE_DATA], 2
  42. FindHDD_1:
  43.         DEBUGF  1, "K : Channel %d ",[ChannelNumber]:1
  44.         DEBUGF  1, "Disk %d\n",[DiskNumber]:1
  45.         push    ecx ebx
  46.         call    ReadHDD_ID
  47.         cmp     [DevErrorCode], 7
  48.         je      .end
  49.         cmp     [DevErrorCode], 0
  50.         jne     .FindCD
  51.         cmp     [Sector512+6], word 16
  52.         ja      .FindCD
  53.         cmp     [Sector512+12], word 255
  54.         ja      .FindCD
  55.         pop     ebx
  56.         movzx   eax, [DeviceNumber]
  57.         mov     ecx, [Sector512+120]
  58.         mov     dword[eax+hd0_data.sectors], ecx
  59.         and     dword[eax+hd0_data.sectors+4], 0
  60.         bt      word [Sector512+166], 10
  61.         jnc     .Print_Device_Name
  62.         mov     [eax+hd0_data.hd48], 1
  63.         mov     ecx, [Sector512+200]
  64.         mov     dword[eax+hd0_data.sectors], ecx
  65.         mov     ecx, [Sector512+204]
  66.         mov     dword[eax+hd0_data.sectors+4], ecx
  67.         jmp     .Print_Device_Name
  68. ;--------------------------------------
  69. .FindCD:
  70.         call    DeviceReset
  71.         cmp     [DevErrorCode], 0
  72.         jne     .end
  73.         call    ReadCD_ID
  74.         cmp     [DevErrorCode], 0
  75.         jne     .end
  76.         pop     ebx
  77.         inc     byte [ebx+DRIVE_DATA]
  78. ;--------------------------------------
  79. .Print_Device_Name:
  80.         inc     byte [ebx+DRIVE_DATA]
  81.         pop     ecx
  82.         pushad
  83.         movzx   ebx, [ChannelNumber]
  84.         dec     ebx
  85.         shl     ebx, 1
  86.         add     bl, [DiskNumber]
  87.         shl     ebx, 1
  88.         call    calculate_IDE_device_values_storage
  89. ;--------------------------------------
  90. .copy_dev_name:
  91.         mov     esi, Sector512+27*2
  92.         mov     edi, dev_name
  93.         mov     ecx, 20
  94.         cld
  95. ;--------------------------------------
  96. @@:
  97.         lodsw
  98.         xchg    ah, al
  99.         stosw
  100.         loop    @b
  101.         DEBUGF 1, "K : Dev: %s \n", dev_name
  102.         xor     eax, eax
  103.         mov     ax, [Sector512+64*2]
  104.         DEBUGF  1, "K : PIO possible modes %x\n", al
  105.         mov     ax, [Sector512+51*2]
  106.         mov     al, ah
  107.         call    convert_Sector512_value
  108.         DEBUGF  1, "K : PIO set mode %x\n", ah
  109.         mov     ax, [Sector512+63*2]
  110.         DEBUGF  1, "K : Multiword DMA possible modes %x\n", al
  111.         mov     al, ah
  112.         call    convert_Sector512_value
  113.         DEBUGF  1, "K : Multiword DMA set mode %x\n", ah
  114.         mov     ax, [Sector512+88*2]
  115.         DEBUGF  1, "K : Ultra DMA possible modes %x\n", al
  116.         mov     [ebx+IDE_DEVICE.UDMA_possible_modes], al
  117.         mov     al, ah
  118.         call    convert_Sector512_value
  119.         DEBUGF  1, "K : Ultra DMA set mode %x\n", ah
  120.         mov     [ebx+IDE_DEVICE.UDMA_set_mode], ah
  121.         popad
  122.         ret
  123.  
  124. .end:
  125.         DEBUGF  1, "K : Device not found\n"
  126.         pop     ebx ecx
  127.         ret
  128. ;-----------------------------------------------------------------------------
  129. calculate_IDE_device_values_storage:
  130.         cmp     ecx, IDE_controller_1
  131.         jne     @f
  132.  
  133.         add     ebx, IDE_device_1
  134.         jmp     .exit
  135. ;--------------------------------------
  136. @@:
  137.         cmp     ecx, IDE_controller_2
  138.         jne     @f
  139.  
  140.         add     ebx, IDE_device_2
  141.         jmp     .exit
  142. ;--------------------------------------
  143. @@:
  144.         add     ebx, IDE_device_3
  145. ;--------------------------------------
  146. .exit:
  147.         ret
  148. ;-----------------------------------------------------------------------------
  149. convert_Sector512_value:
  150.         mov     ecx, 8
  151.         xor     ah, ah
  152. ;--------------------------------------
  153. @@:
  154.         test    al, 1b
  155.         jnz     .end
  156.  
  157.         shr     al, 1
  158.         inc     ah
  159.         loop    @b
  160.  
  161.         xor     ah, ah
  162. ;--------------------------------------
  163. .end:
  164.         ret
  165. ;-----------------------------------------------------------------------------
  166. ; Address of reading sector in LBA mode
  167. uglobal
  168. SectorAddress   dd ?
  169. dev_name:
  170.         rb 41
  171. endg
  172. ;-----------------------------------------------------------------------------
  173. ;*************************************************
  174. ;*     READING THE HARD DISK IDENTIFIER          *
  175. ;* Input parameters are passed through the global*
  176. ;* variables:                                    *
  177. ;* ChannelNumber - channel number (1 or 2);      *
  178. ;* DiskNumber - disk number on channel (0 or 1)  *
  179. ;* Block of identificational data is reading     *
  180. ;* to Sector512 array.                           *
  181. ;*************************************************
  182. ReadHDD_ID:
  183. ; set up CHS mode
  184.         mov     [ATAAddressMode], 0
  185. ; send device identification command
  186.         mov     [ATAFeatures], 0
  187.         mov     [ATAHead], 0
  188.         mov     [ATACommand], 0xEC
  189.         call    SendCommandToHDD
  190.         cmp     [DevErrorCode], 0 ; check the error code
  191.         jne     @@End    ; finish, saving the error code
  192.  
  193.         mov     dx, [ATABasePortAddr]
  194.         add     dx, 7    ; address of state register
  195.         mov     ecx, 0xffff
  196. @@WaitCompleet:
  197.         ; Check command execution time
  198.         dec     ecx
  199.         jz      @@Error1 ; timeout error
  200.         ; Check if ready or not
  201.         in      al, dx
  202.         test    al, 80h  ; BSY signal state
  203.         jnz     @@WaitCompleet
  204.  
  205.         test    al, 1    ; ERR signal state
  206.         jnz     @@Error6
  207.  
  208.         test    al, 08h  ; DRQ signal state
  209.         jz      @@WaitCompleet
  210. ; Receive data block from controller
  211.         mov     edi, Sector512
  212.         mov     dx, [ATABasePortAddr]; data register
  213.         mov     cx, 256  ; number of word to receive
  214.         rep insw         ; receive data block
  215.         ret
  216. ; write the error code
  217. @@Error1:
  218.         mov     [DevErrorCode], 1
  219.         ret
  220. @@Error6:
  221.         mov     [DevErrorCode], 6
  222. @@End:
  223.         ret
  224. ;-----------------------------------------------------------------------------
  225. uglobal
  226. ; Standart base addresses of channels 1 or 2
  227. StandardATABases dw ?, ? ; 1F0h, 170h
  228. ; Channel number
  229. ChannelNumber   db ?
  230. ; Disk number
  231. DiskNumber      db ?
  232. DeviceNumber    db ?
  233. ; Base address of ATA controller's port group
  234. ATABasePortAddr dw ?
  235. ; ATA-command parameters
  236. ATAFeatures     db ? ; features
  237. ATASectorCount  db ? ; count of processing sectors
  238. ATASectorNumber db ? ; initial sector number
  239. ATACylinder     dw ? ; initial cylinder number
  240. ATAHead         db ? ; initial head number
  241. ATAAddressMode  db ? ; addressing mode (0 - CHS, 1 - LBA)
  242. ATACommand      db ? ; executing command number
  243. ; Error code (0 - no errors, 1 - waiting time limit exceed
  244. ; 2 - incorrect code of addressing mode,
  245. ; 3 - incorrect channel number, 4 - incorrect disk number,
  246. ; 5 - incorrect head number, 6 - command execution error,
  247. ; 7 - time out when choosing channel)
  248. DevErrorCode dd ?
  249. endg
  250. ;-----------------------------------------------------------------------------
  251. ;****************************************************
  252. ;*          SEND COMMAND TO GIVEN DISK              *
  253. ;* Input parameters are passed through the global   *
  254. ;* variables:                                       *
  255. ;* ChannelNumber - channel number (1 or 2);         *
  256. ;* DiskNumber - disk number (0 or 1);               *
  257. ;* ATAFeatures - "features";                        *
  258. ;* ATASectorCount - sector count;                   *
  259. ;* ATASectorNumber - initial sector number;         *
  260. ;* ATACylinder - initial cylinder number;           *
  261. ;* ATAHead - initial head number;                   *
  262. ;* ATAAddressMode - addressing mode (0-CHS, 1-LBA); *
  263. ;* ATACommand - command code.                       *
  264. ;* If the function finished successfully:           *
  265. ;* in ATABasePortAddr - base address of HDD;        *
  266. ;* in DevErrorCode - zero.                          *
  267. ;* If error has occured then in DevErrorCode will   *
  268. ;* be the error code.                               *
  269. ;****************************************************
  270. SendCommandToHDD:
  271. ; Check the addressing mode code
  272.         cmp     [ATAAddressMode], 1
  273.         ja      @@Err2
  274. ; Check the channel number correctness
  275.         movzx   ebx, [ChannelNumber]
  276.         dec     ebx
  277.         cmp     ebx, 1
  278.         ja      @@Err3
  279. ; Set the base address
  280.         shl     ebx, 1
  281.         mov     ax, [ebx+StandardATABases]
  282.         mov     [ATABasePortAddr], ax
  283. ; Waiting for HDD ready to receive a command
  284.         ; Choose desired disk
  285.         mov     dx, [ATABasePortAddr]
  286.         add     dx, 6   ; address of the heads register
  287.         mov     al, [DiskNumber]
  288.         cmp     al, 1   ; check the disk number
  289.         ja      @@Err4
  290.  
  291.         shl     al, 4
  292.         or      al, 10100000b
  293.         out     dx, al
  294.         ; Waiting for disk ready
  295.         inc     dx
  296.         mov     ecx, 0xfff
  297. @@WaitHDReady:
  298.         ; Check waiting time
  299.         dec     ecx
  300.         jz      @@Err1
  301.         ; Read the state register
  302.         in      al, dx
  303.         ; Check the state of BSY signal
  304.         test    al, 80h
  305.         jnz     @@WaitHDReady
  306.         ; Check the state of DRQ signal
  307.         test    al, 08h
  308.         jnz     @@WaitHDReady
  309. ; load command to controller's registers
  310.         cli
  311.         mov     dx, [ATABasePortAddr]
  312.         inc     dx      ; "features" register
  313.         mov     al, [ATAFeatures]
  314.         out     dx, AL
  315.         inc     dx      ; sector counter
  316.         mov     al, [ATASectorCount]
  317.         out     dx, AL
  318.         inc     dx      ; sector number register
  319.         mov     al, [ATASectorNumber]
  320.         out     dx, AL
  321.         inc     dx      ; cylinder number (low byte)
  322.         mov     ax, [ATACylinder]
  323.         out     dx, AL
  324.         inc     dx      ; cylinder number (high byte)
  325.         mov     al, AH
  326.         out     dx, AL
  327.         inc     dx      ; head number / disk number
  328.         mov     al, [DiskNumber]
  329.         shl     al, 4
  330.         cmp     [ATAHead], 0xF ; check head number
  331.         ja      @@Err5
  332.  
  333.         or      al, [ATAHead]
  334.         or      al, 10100000b
  335.         mov     ah, [ATAAddressMode]
  336.         shl     ah, 6
  337.         or      al, ah
  338.         out     dx, al
  339. ; Send command
  340.         mov     al, [ATACommand]
  341.         inc     dx      ; command register
  342.         out     dx, al
  343.         sti
  344. ; reset the error sign
  345.         mov     [DevErrorCode], 0
  346.         ret
  347. ; write error code
  348. @@Err1:
  349.         mov     [DevErrorCode], 7
  350.         ret
  351. @@Err2:
  352.         mov     [DevErrorCode], 2
  353.         ret
  354. @@Err3:
  355.         mov     [DevErrorCode], 3
  356.         ret
  357. @@Err4:
  358.         mov     [DevErrorCode], 4
  359.         ret
  360. @@Err5:
  361.         mov     [DevErrorCode], 5
  362. ; finish work
  363.         ret
  364. ;-----------------------------------------------------------------------------
  365. ;*************************************************
  366. ;*     READ ATAPI DEVICE IDENTIFIER              *
  367. ;* Input parameters are passed through the global*
  368. ;* variables:                                    *
  369. ;* ChannelNumber - channel number;               *
  370. ;* DiskNumber - disk number on channel.          *
  371. ;* Block of identificational data is reading     *
  372. ;* to Sector512 array.                           *                           *
  373. ;*************************************************
  374. ReadCD_ID:
  375. ; Set CHS mode
  376.         mov     [ATAAddressMode], 0
  377. ; Send command for device identification
  378.         mov     [ATAFeatures], 0
  379.         mov     [ATASectorCount], 0
  380.         mov     [ATASectorNumber], 0
  381.         mov     [ATACylinder], 0
  382.         mov     [ATAHead], 0
  383.         mov     [ATACommand], 0xA1
  384.         call    SendCommandToHDD
  385.         cmp     [DevErrorCode], 0 ; check the error code
  386.         jne     @@End_1  ; finish, saving the error code
  387. ; Wait for HDD data ready
  388.         mov     dx, [ATABasePortAddr]
  389.         add     dx, 7      ; port 1х7h
  390.         mov     ecx, 0xffff
  391. @@WaitCompleet_1:
  392.         ; Check time
  393.         dec     ecx
  394.         jz      @@Error1_1 ; time out error
  395.         ; Check readyness
  396.         in      al, dx
  397.         test    al, 80h    ; BSY signal state
  398.         jnz     @@WaitCompleet_1
  399.  
  400.         test    al, 1      ; ERR signal state
  401.         jnz     @@Error6_1
  402.  
  403.         test    al, 08h    ; DRQ signal state
  404.         jz      @@WaitCompleet_1
  405. ; Receive data block from controller
  406.         mov     edi, Sector512 ; offset Sector512
  407.         mov     dx, [ATABasePortAddr] ; port 1x0h
  408.         mov     cx, 256    ; words read count
  409.         rep insw
  410.         ret
  411. ; write the error code
  412. @@Error1_1:
  413.         mov     [DevErrorCode], 1
  414.         ret
  415. @@Error6_1:
  416.         mov     [DevErrorCode], 6
  417. @@End_1:
  418.         ret
  419. ;-----------------------------------------------------------------------------
  420. ;*************************************************
  421. ;*                DEVICE RESET                   *
  422. ;* Input parameters are passed through the global*
  423. ;* variables:                                    *
  424. ;* ChannelNumber - channel number (1 or 2);      *
  425. ;* DiskNumber - disk number (0 or 1).            *
  426. ;*************************************************
  427. DeviceReset:
  428. ; Check the channel number correctness
  429.         movzx   ebx, [ChannelNumber]
  430.         dec     ebx
  431.         cmp     ebx, 1
  432.         ja      @@Err3_2
  433. ; Set base address
  434.         shl     ebx, 1
  435.         mov     dx, [ebx+StandardATABases]
  436.         mov     [ATABasePortAddr], dx
  437. ; Choose desired disk
  438.         add     dx, 6   ; address of heads register
  439.         mov     al, [DiskNumber]
  440.         cmp     al, 1   ; check disk number
  441.         ja      @@Err4_2
  442.  
  443.         shl     al, 4
  444.         or      al, 10100000b
  445.         out     dx, al
  446. ; Send the "Reset" command
  447.         mov     al, 0x8
  448.         inc     dx      ; command register
  449.         out     dx, al
  450.         mov     ecx, 0x80000
  451. @@WaitHDReady_1:
  452.         ; Check waiting time
  453.         dec     ecx
  454.         je      @@Err1_2 ; time out error
  455.         ; read the state register
  456.         in      al, dx
  457.         ; Check the state of BSY signal
  458.         test    al, 80h
  459.         jnz     @@WaitHDReady_1
  460. ; reset the error sign
  461.         mov     [DevErrorCode], 0
  462.         ret
  463. ; error processing
  464. @@Err1_2:
  465.         mov     [DevErrorCode], 1
  466.         ret
  467. @@Err3_2:
  468.         mov     [DevErrorCode], 3
  469.         ret
  470. @@Err4_2:
  471.         mov     [DevErrorCode], 4
  472. ; write error code
  473.         ret
  474. ;-----------------------------------------------------------------------------
  475. EndFindHDD:
  476.