Subversion Repositories Kolibri OS

Rev

Rev 8051 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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