Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
2288 clevermous 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
10051 ace_dent 3
;; Copyright (C) KolibriOS team 2004-2024. All rights reserved. ;;
2288 clevermous 4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
8
 
5852 pathoswith 9
; HDD and CD search
2288 clevermous 10
 
4700 mario79 11
        cmp     [ecx+IDE_DATA.ProgrammingInterface], 0
3785 mario79 12
        je      EndFindHDD
2288 clevermous 13
FindHDD:
4700 mario79 14
        xor     ebx, ebx
15
        inc     ebx
5852 pathoswith 16
        mov     [DeviceNumber], 0
17
        cmp     ecx, IDE_controller_1
18
        jz      .find
19
        add     bl, 5
20
        add     [DeviceNumber], sizeof.HD_DATA*4
4700 mario79 21
        cmp     ecx, IDE_controller_2
5852 pathoswith 22
        jz      .find
4700 mario79 23
        add     bl, 5
5852 pathoswith 24
        add     [DeviceNumber], sizeof.HD_DATA*4
4700 mario79 25
.find:
2288 clevermous 26
        mov     [ChannelNumber], 1
27
        mov     [DiskNumber], 0
5852 pathoswith 28
        call    FindHDD_1
29
        inc     [DiskNumber]
4700 mario79 30
        call    FindHDD_2
2288 clevermous 31
        inc     [ChannelNumber]
5852 pathoswith 32
        dec     [DiskNumber]
33
        call    FindHDD_2
34
        inc     [DiskNumber]
4700 mario79 35
        call    FindHDD_2
2288 clevermous 36
        jmp     EndFindHDD
4700 mario79 37
;-----------------------------------------------------------------------------
38
FindHDD_2:
5852 pathoswith 39
        add     [DeviceNumber], sizeof.HD_DATA
4700 mario79 40
        shl     byte [ebx+DRIVE_DATA], 2
2288 clevermous 41
FindHDD_1:
6464 pathoswith 42
        DEBUGF  1, "K : Channel %d ",[ChannelNumber]:1
3706 mario79 43
        DEBUGF  1, "Disk %d\n",[DiskNumber]:1
6842 pathoswith 44
        push    ecx ebx
2288 clevermous 45
        call    ReadHDD_ID
5030 clevermous 46
        cmp     [DevErrorCode], 7
47
        je      .end
2288 clevermous 48
        cmp     [DevErrorCode], 0
4700 mario79 49
        jne     .FindCD
2288 clevermous 50
        cmp     [Sector512+6], word 16
4700 mario79 51
        ja      .FindCD
2288 clevermous 52
        cmp     [Sector512+12], word 255
4700 mario79 53
        ja      .FindCD
6842 pathoswith 54
        pop     ebx
5852 pathoswith 55
        movzx   eax, [DeviceNumber]
6842 pathoswith 56
        mov     ecx, [Sector512+120]
57
        mov     dword[eax+hd0_data.sectors], ecx
58
        and     dword[eax+hd0_data.sectors+4], 0
5852 pathoswith 59
        bt      word [Sector512+166], 10
6842 pathoswith 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
4700 mario79 66
        jmp     .Print_Device_Name
67
;--------------------------------------
68
.FindCD:
2288 clevermous 69
        call    DeviceReset
70
        cmp     [DevErrorCode], 0
4700 mario79 71
        jne     .end
2288 clevermous 72
        call    ReadCD_ID
73
        cmp     [DevErrorCode], 0
4700 mario79 74
        jne     .end
6842 pathoswith 75
        pop     ebx
76
        inc     byte [ebx+DRIVE_DATA]
4700 mario79 77
;--------------------------------------
6464 pathoswith 78
.Print_Device_Name:
6842 pathoswith 79
        inc     byte [ebx+DRIVE_DATA]
80
        pop     ecx
3711 clevermous 81
        pushad
6464 pathoswith 82
        movzx   ebx, [ChannelNumber]
4772 mario79 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:
3711 clevermous 90
        mov     esi, Sector512+27*2
91
        mov     edi, dev_name
92
        mov     ecx, 20
93
        cld
4772 mario79 94
;--------------------------------------
3702 mario79 95
@@:
3711 clevermous 96
        lodsw
97
        xchg    ah, al
98
        stosw
99
        loop    @b
3702 mario79 100
        DEBUGF 1, "K : Dev: %s \n", dev_name
3881 mario79 101
        xor     eax, eax
102
        mov     ax, [Sector512+64*2]
4772 mario79 103
        DEBUGF  1, "K : PIO possible modes %x\n", al
4624 mario79 104
        mov     ax, [Sector512+51*2]
105
        mov     al, ah
106
        call    convert_Sector512_value
4772 mario79 107
        DEBUGF  1, "K : PIO set mode %x\n", ah
3881 mario79 108
        mov     ax, [Sector512+63*2]
4624 mario79 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
3881 mario79 113
        mov     ax, [Sector512+88*2]
4624 mario79 114
        DEBUGF  1, "K : Ultra DMA possible modes %x\n", al
4772 mario79 115
        mov     [ebx+IDE_DEVICE.UDMA_possible_modes], al
4624 mario79 116
        mov     al, ah
117
        call    convert_Sector512_value
118
        DEBUGF  1, "K : Ultra DMA set mode %x\n", ah
4772 mario79 119
        mov     [ebx+IDE_DEVICE.UDMA_set_mode], ah
4700 mario79 120
        popad
2288 clevermous 121
        ret
6842 pathoswith 122
 
4700 mario79 123
.end:
124
        DEBUGF  1, "K : Device not found\n"
6842 pathoswith 125
        pop     ebx ecx
2288 clevermous 126
        ret
4624 mario79 127
;-----------------------------------------------------------------------------
4772 mario79 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
;-----------------------------------------------------------------------------
4624 mario79 148
convert_Sector512_value:
149
        mov     ecx, 8
150
        xor     ah, ah
4772 mario79 151
;--------------------------------------
4624 mario79 152
@@:
153
        test    al, 1b
154
        jnz     .end
2288 clevermous 155
 
4624 mario79 156
        shr     al, 1
157
        inc     ah
158
        loop    @b
159
 
160
        xor     ah, ah
4772 mario79 161
;--------------------------------------
4624 mario79 162
.end:
163
        ret
164
;-----------------------------------------------------------------------------
8051 rgimad 165
; Address of reading sector in LBA mode
2288 clevermous 166
uglobal
4700 mario79 167
SectorAddress   dd ?
3702 mario79 168
dev_name:
3711 clevermous 169
        rb 41
2288 clevermous 170
endg
4700 mario79 171
;-----------------------------------------------------------------------------
2288 clevermous 172
;*************************************************
8051 rgimad 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.                           *
2288 clevermous 180
;*************************************************
181
ReadHDD_ID:
8051 rgimad 182
; set up CHS mode
2288 clevermous 183
        mov     [ATAAddressMode], 0
8051 rgimad 184
; send device identification command
2288 clevermous 185
        mov     [ATAFeatures], 0
186
        mov     [ATAHead], 0
4700 mario79 187
        mov     [ATACommand], 0xEC
2288 clevermous 188
        call    SendCommandToHDD
8051 rgimad 189
        cmp     [DevErrorCode], 0 ; check the error code
190
        jne     @@End    ; finish, saving the error code
4700 mario79 191
 
192
        mov     dx, [ATABasePortAddr]
8051 rgimad 193
        add     dx, 7    ; address of state register
2288 clevermous 194
        mov     ecx, 0xffff
195
@@WaitCompleet:
8051 rgimad 196
        ; Check command execution time
2288 clevermous 197
        dec     ecx
8051 rgimad 198
        jz      @@Error1 ; timeout error
199
        ; Check if ready or not
4700 mario79 200
        in      al, dx
8051 rgimad 201
        test    al, 80h  ; BSY signal state
2288 clevermous 202
        jnz     @@WaitCompleet
4700 mario79 203
 
8051 rgimad 204
        test    al, 1    ; ERR signal state
2288 clevermous 205
        jnz     @@Error6
4700 mario79 206
 
8051 rgimad 207
        test    al, 08h  ; DRQ signal state
2288 clevermous 208
        jz      @@WaitCompleet
8051 rgimad 209
; Receive data block from controller
4700 mario79 210
        mov     edi, Sector512
8051 rgimad 211
        mov     dx, [ATABasePortAddr]; data register
212
        mov     cx, 256  ; number of word to receive
213
        rep insw         ; receive data block
2288 clevermous 214
        ret
8051 rgimad 215
; write the error code
2288 clevermous 216
@@Error1:
217
        mov     [DevErrorCode], 1
218
        ret
219
@@Error6:
220
        mov     [DevErrorCode], 6
221
@@End:
222
        ret
4700 mario79 223
;-----------------------------------------------------------------------------
224
uglobal
8051 rgimad 225
; Standart base addresses of channels 1 or 2
4700 mario79 226
StandardATABases dw ?, ? ; 1F0h, 170h
8051 rgimad 227
; Channel number
6464 pathoswith 228
ChannelNumber   db ?
8051 rgimad 229
; Disk number
4700 mario79 230
DiskNumber      db ?
5852 pathoswith 231
DeviceNumber    db ?
8051 rgimad 232
; Base address of ATA controller's port group
4700 mario79 233
ATABasePortAddr dw ?
8051 rgimad 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)
2288 clevermous 247
DevErrorCode dd ?
248
endg
4700 mario79 249
;-----------------------------------------------------------------------------
2288 clevermous 250
;****************************************************
8051 rgimad 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.                               *
2288 clevermous 268
;****************************************************
269
SendCommandToHDD:
8051 rgimad 270
; Check the addressing mode code
2288 clevermous 271
        cmp     [ATAAddressMode], 1
272
        ja      @@Err2
8051 rgimad 273
; Check the channel number correctness
6464 pathoswith 274
        movzx   ebx, [ChannelNumber]
275
        dec     ebx
276
        cmp     ebx, 1
2288 clevermous 277
        ja      @@Err3
8051 rgimad 278
; Set the base address
6464 pathoswith 279
        shl     ebx, 1
4700 mario79 280
        mov     ax, [ebx+StandardATABases]
281
        mov     [ATABasePortAddr], ax
8051 rgimad 282
; Waiting for HDD ready to receive a command
283
        ; Choose desired disk
4700 mario79 284
        mov     dx, [ATABasePortAddr]
8051 rgimad 285
        add     dx, 6   ; address of the heads register
4700 mario79 286
        mov     al, [DiskNumber]
8051 rgimad 287
        cmp     al, 1   ; check the disk number
2288 clevermous 288
        ja      @@Err4
4700 mario79 289
 
290
        shl     al, 4
291
        or      al, 10100000b
292
        out     dx, al
8051 rgimad 293
        ; Waiting for disk ready
4700 mario79 294
        inc     dx
2288 clevermous 295
        mov     ecx, 0xfff
296
@@WaitHDReady:
8051 rgimad 297
        ; Check waiting time
2288 clevermous 298
        dec     ecx
299
        jz      @@Err1
8051 rgimad 300
        ; Read the state register
4700 mario79 301
        in      al, dx
8051 rgimad 302
        ; Check the state of BSY signal
4700 mario79 303
        test    al, 80h
2288 clevermous 304
        jnz     @@WaitHDReady
8051 rgimad 305
        ; Check the state of DRQ signal
4700 mario79 306
        test    al, 08h
2288 clevermous 307
        jnz     @@WaitHDReady
8051 rgimad 308
; load command to controller's registers
2288 clevermous 309
        cli
4700 mario79 310
        mov     dx, [ATABasePortAddr]
8051 rgimad 311
        inc     dx      ; "features" register
4700 mario79 312
        mov     al, [ATAFeatures]
313
        out     dx, AL
8051 rgimad 314
        inc     dx      ; sector counter
4700 mario79 315
        mov     al, [ATASectorCount]
316
        out     dx, AL
8051 rgimad 317
        inc     dx      ; sector number register
4700 mario79 318
        mov     al, [ATASectorNumber]
319
        out     dx, AL
8051 rgimad 320
        inc     dx      ; cylinder number (low byte)
4700 mario79 321
        mov     ax, [ATACylinder]
322
        out     dx, AL
8051 rgimad 323
        inc     dx      ; cylinder number (high byte)
4700 mario79 324
        mov     al, AH
325
        out     dx, AL
8051 rgimad 326
        inc     dx      ; head number / disk number
4700 mario79 327
        mov     al, [DiskNumber]
328
        shl     al, 4
8051 rgimad 329
        cmp     [ATAHead], 0xF ; check head number
2288 clevermous 330
        ja      @@Err5
4700 mario79 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
8051 rgimad 338
; Send command
4700 mario79 339
        mov     al, [ATACommand]
8051 rgimad 340
        inc     dx      ; command register
4700 mario79 341
        out     dx, al
2288 clevermous 342
        sti
8051 rgimad 343
; reset the error sign
2288 clevermous 344
        mov     [DevErrorCode], 0
345
        ret
8051 rgimad 346
; write error code
2288 clevermous 347
@@Err1:
5030 clevermous 348
        mov     [DevErrorCode], 7
2288 clevermous 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
8051 rgimad 361
; finish work
2288 clevermous 362
        ret
4700 mario79 363
;-----------------------------------------------------------------------------
2288 clevermous 364
;*************************************************
8051 rgimad 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.                           *                           *
2288 clevermous 372
;*************************************************
373
ReadCD_ID:
8051 rgimad 374
; Set CHS mode
2288 clevermous 375
        mov     [ATAAddressMode], 0
8051 rgimad 376
; Send command for device identification
2288 clevermous 377
        mov     [ATAFeatures], 0
378
        mov     [ATASectorCount], 0
379
        mov     [ATASectorNumber], 0
380
        mov     [ATACylinder], 0
381
        mov     [ATAHead], 0
4700 mario79 382
        mov     [ATACommand], 0xA1
2288 clevermous 383
        call    SendCommandToHDD
8051 rgimad 384
        cmp     [DevErrorCode], 0 ; check the error code
385
        jne     @@End_1  ; finish, saving the error code
386
; Wait for HDD data ready
4700 mario79 387
        mov     dx, [ATABasePortAddr]
8051 rgimad 388
        add     dx, 7      ; port 1х7h
2288 clevermous 389
        mov     ecx, 0xffff
390
@@WaitCompleet_1:
8051 rgimad 391
        ; Check time
2288 clevermous 392
        dec     ecx
8051 rgimad 393
        jz      @@Error1_1 ; time out error
394
        ; Check readyness
4700 mario79 395
        in      al, dx
8051 rgimad 396
        test    al, 80h    ; BSY signal state
2288 clevermous 397
        jnz     @@WaitCompleet_1
4700 mario79 398
 
8051 rgimad 399
        test    al, 1      ; ERR signal state
2288 clevermous 400
        jnz     @@Error6_1
4700 mario79 401
 
8051 rgimad 402
        test    al, 08h    ; DRQ signal state
2288 clevermous 403
        jz      @@WaitCompleet_1
8051 rgimad 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
2288 clevermous 408
        rep insw
409
        ret
8051 rgimad 410
; write the error code
2288 clevermous 411
@@Error1_1:
412
        mov     [DevErrorCode], 1
413
        ret
414
@@Error6_1:
415
        mov     [DevErrorCode], 6
416
@@End_1:
417
        ret
4700 mario79 418
;-----------------------------------------------------------------------------
2288 clevermous 419
;*************************************************
8051 rgimad 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).            *
2288 clevermous 425
;*************************************************
426
DeviceReset:
8051 rgimad 427
; Check the channel number correctness
6464 pathoswith 428
        movzx   ebx, [ChannelNumber]
429
        dec     ebx
430
        cmp     ebx, 1
2288 clevermous 431
        ja      @@Err3_2
8051 rgimad 432
; Set base address
6464 pathoswith 433
        shl     ebx, 1
4700 mario79 434
        mov     dx, [ebx+StandardATABases]
435
        mov     [ATABasePortAddr], dx
8051 rgimad 436
; Choose desired disk
437
        add     dx, 6   ; address of heads register
4700 mario79 438
        mov     al, [DiskNumber]
8051 rgimad 439
        cmp     al, 1   ; check disk number
2288 clevermous 440
        ja      @@Err4_2
4700 mario79 441
 
442
        shl     al, 4
443
        or      al, 10100000b
444
        out     dx, al
8051 rgimad 445
; Send the "Reset" command
4700 mario79 446
        mov     al, 0x8
8051 rgimad 447
        inc     dx      ; command register
4700 mario79 448
        out     dx, al
2288 clevermous 449
        mov     ecx, 0x80000
450
@@WaitHDReady_1:
8051 rgimad 451
        ; Check waiting time
2288 clevermous 452
        dec     ecx
8051 rgimad 453
        je      @@Err1_2 ; time out error
454
        ; read the state register
4700 mario79 455
        in      al, dx
8051 rgimad 456
        ; Check the state of BSY signal
4700 mario79 457
        test    al, 80h
2288 clevermous 458
        jnz     @@WaitHDReady_1
8051 rgimad 459
; reset the error sign
2288 clevermous 460
        mov     [DevErrorCode], 0
461
        ret
8051 rgimad 462
; error processing
2288 clevermous 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
8051 rgimad 471
; write error code
2288 clevermous 472
        ret
4700 mario79 473
;-----------------------------------------------------------------------------
2288 clevermous 474
EndFindHDD: